microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/reactNativeCDPProxy.ts
217lines · modeblame
f872f4d5RedMickey6 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
09f6024fHeniker4 years ago | 4 | import { IncomingMessage } from "http"; |
f872f4d5RedMickey6 years ago | 5 | import { |
| 6 | Connection, | |
| 7 | Server, | |
| 8 | WebSocketTransport, | |
| 9 | IProtocolCommand, | |
| 10 | IProtocolError, | |
34472878RedMickey5 years ago | 11 | IProtocolSuccess, |
f872f4d5RedMickey6 years ago | 12 | } from "vscode-cdp-proxy"; |
19df32dcRedMickey4 years ago | 13 | import { CancellationToken, EventEmitter } from "vscode"; |
f872f4d5RedMickey6 years ago | 14 | import { OutputChannelLogger } from "../extension/log/OutputChannelLogger"; |
a324603aRedMickey6 years ago | 15 | import { LogLevel } from "../extension/log/LogHelper"; |
d9c9ddcbRedMickey6 years ago | 16 | import { DebuggerEndpointHelper } from "./debuggerEndpointHelper"; |
259c018fYuri Skorokhodov5 years ago | 17 | import { BaseCDPMessageHandler } from "./CDPMessageHandlers/baseCDPMessageHandler"; |
f872f4d5RedMickey6 years ago | 18 | |
| 19 | export class ReactNativeCDPProxy { | |
a324603aRedMickey6 years ago | 20 | private readonly PROXY_LOG_TAGS = { |
| 21 | DEBUGGER_COMMAND: "Command Debugger To Target", | |
| 22 | APPLICATION_COMMAND: "Command Target To Debugger", | |
| 23 | DEBUGGER_REPLY: "Reply From Debugger To Target", | |
| 24 | APPLICATION_REPLY: "Reply From Target To Debugger", | |
| 25 | }; | |
| 26 | | |
d9c9ddcbRedMickey6 years ago | 27 | private server: Server | null; |
| 28 | private hostAddress: string; | |
| 29 | private port: number; | |
ebbd64f1RedMickey6 years ago | 30 | private debuggerTarget: Connection | null; |
984ca036RedMickey6 years ago | 31 | private applicationTarget: Connection | null; |
d9c9ddcbRedMickey6 years ago | 32 | private logger: OutputChannelLogger; |
| 33 | private logLevel: LogLevel; | |
| 34 | private debuggerEndpointHelper: DebuggerEndpointHelper; | |
259c018fYuri Skorokhodov5 years ago | 35 | private CDPMessageHandler: BaseCDPMessageHandler; |
d9c9ddcbRedMickey6 years ago | 36 | private applicationTargetPort: number; |
984ca036RedMickey6 years ago | 37 | private browserInspectUri: string; |
e23d1841RedMickey6 years ago | 38 | private cancellationToken: CancellationToken | undefined; |
19df32dcRedMickey4 years ago | 39 | private applicationTargetEventEmitter: EventEmitter<unknown> = new EventEmitter(); |
| 40 | | |
| 41 | public readonly onApplicationTargetConnectionClosed = this.applicationTargetEventEmitter.event; | |
d9c9ddcbRedMickey6 years ago | 42 | |
a6562589RedMickey6 years ago | 43 | constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) { |
f872f4d5RedMickey6 years ago | 44 | this.port = port; |
| 45 | this.hostAddress = hostAddress; | |
34472878RedMickey5 years ago | 46 | this.logger = OutputChannelLogger.getChannel( |
| 47 | "React Native Chrome Proxy", | |
09f6024fHeniker4 years ago | 48 | process.env.REACT_NATIVE_TOOLS_LAZY_LOGS !== "false", |
34472878RedMickey5 years ago | 49 | false, |
| 50 | true, | |
| 51 | ); | |
f872f4d5RedMickey6 years ago | 52 | this.logLevel = logLevel; |
984ca036RedMickey6 years ago | 53 | this.browserInspectUri = ""; |
d9c9ddcbRedMickey6 years ago | 54 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
f872f4d5RedMickey6 years ago | 55 | } |
| 56 | | |
0d77292aJiglioNero4 years ago | 57 | public async initializeServer( |
259c018fYuri Skorokhodov5 years ago | 58 | CDPMessageHandler: BaseCDPMessageHandler, |
e23d1841RedMickey6 years ago | 59 | logLevel: LogLevel, |
34472878RedMickey5 years ago | 60 | cancellationToken?: CancellationToken, |
e23d1841RedMickey6 years ago | 61 | ): Promise<void> { |
a6562589RedMickey6 years ago | 62 | this.logLevel = logLevel; |
| 63 | this.CDPMessageHandler = CDPMessageHandler; | |
e23d1841RedMickey6 years ago | 64 | this.cancellationToken = cancellationToken; |
a6562589RedMickey6 years ago | 65 | |
0d77292aJiglioNero4 years ago | 66 | this.server = await Server.create({ port: this.port, host: this.hostAddress }); |
| 67 | this.server.onConnection(this.onConnectionHandler.bind(this)); | |
f872f4d5RedMickey6 years ago | 68 | } |
| 69 | | |
984ca036RedMickey6 years ago | 70 | public async stopServer(): Promise<void> { |
f872f4d5RedMickey6 years ago | 71 | if (this.server) { |
| 72 | this.server.dispose(); | |
| 73 | this.server = null; | |
| 74 | } | |
984ca036RedMickey6 years ago | 75 | |
| 76 | if (this.applicationTarget) { | |
| 77 | await this.applicationTarget.close(); | |
| 78 | this.applicationTarget = null; | |
| 79 | } | |
| 80 | | |
| 81 | this.browserInspectUri = ""; | |
e23d1841RedMickey6 years ago | 82 | this.cancellationToken = undefined; |
984ca036RedMickey6 years ago | 83 | } |
| 84 | | |
34472878RedMickey5 years ago | 85 | public setBrowserInspectUri(browserInspectUri: string): void { |
984ca036RedMickey6 years ago | 86 | this.browserInspectUri = browserInspectUri; |
f872f4d5RedMickey6 years ago | 87 | } |
| 88 | | |
d9c9ddcbRedMickey6 years ago | 89 | public setApplicationTargetPort(applicationTargetPort: number): void { |
| 90 | this.applicationTargetPort = applicationTargetPort; | |
f872f4d5RedMickey6 years ago | 91 | } |
| 92 | | |
34472878RedMickey5 years ago | 93 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 94 | private async onConnectionHandler([debuggerTarget, request]: [ | |
| 95 | Connection, | |
| 96 | IncomingMessage, | |
| 97 | ]): Promise<void> { | |
f872f4d5RedMickey6 years ago | 98 | this.debuggerTarget = debuggerTarget; |
| 99 | | |
| 100 | this.debuggerTarget.pause(); // don't listen for events until the target is ready | |
| 101 | | |
984ca036RedMickey6 years ago | 102 | if (!this.browserInspectUri) { |
e23d1841RedMickey6 years ago | 103 | if (this.cancellationToken) { |
| 104 | this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint( | |
| 105 | `http://localhost:${this.applicationTargetPort}`, | |
| 106 | 90, | |
34472878RedMickey5 years ago | 107 | this.cancellationToken, |
e23d1841RedMickey6 years ago | 108 | ); |
| 109 | } else { | |
34472878RedMickey5 years ago | 110 | this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint( |
| 111 | `http://localhost:${this.applicationTargetPort}`, | |
| 112 | ); | |
e23d1841RedMickey6 years ago | 113 | } |
984ca036RedMickey6 years ago | 114 | } |
d9c9ddcbRedMickey6 years ago | 115 | |
34472878RedMickey5 years ago | 116 | this.applicationTarget = new Connection( |
| 117 | await WebSocketTransport.create(this.browserInspectUri), | |
| 118 | ); | |
f872f4d5RedMickey6 years ago | 119 | |
| 120 | this.applicationTarget.onError(this.onApplicationTargetError.bind(this)); | |
| 121 | this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this)); | |
| 122 | | |
| 123 | this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this)); | |
| 124 | this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this)); | |
| 125 | | |
| 126 | this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this)); | |
| 127 | this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this)); | |
| 128 | | |
ebbd64f1RedMickey6 years ago | 129 | this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this)); |
4c757eebRedMickey6 years ago | 130 | this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this)); |
af1b9666RedMickey6 years ago | 131 | |
259c018fYuri Skorokhodov5 years ago | 132 | this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget); |
| 133 | this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget); | |
| 134 | | |
f872f4d5RedMickey6 years ago | 135 | // dequeue any messages we got in the meantime |
| 136 | this.debuggerTarget.unpause(); | |
| 137 | } | |
| 138 | | |
b7451aefRedMickey6 years ago | 139 | private handleDebuggerTargetCommand(event: IProtocolCommand) { |
34472878RedMickey5 years ago | 140 | this.logger.logWithCustomTag( |
| 141 | this.PROXY_LOG_TAGS.DEBUGGER_COMMAND, | |
| 142 | JSON.stringify(event, null, 2), | |
| 143 | this.logLevel, | |
| 144 | ); | |
b7451aefRedMickey6 years ago | 145 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); |
| 146 | | |
| 147 | if (processedMessage.sendBack) { | |
ebbd64f1RedMickey6 years ago | 148 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 149 | } else { |
984ca036RedMickey6 years ago | 150 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 151 | } |
f872f4d5RedMickey6 years ago | 152 | } |
| 153 | | |
b7451aefRedMickey6 years ago | 154 | private handleApplicationTargetCommand(event: IProtocolCommand) { |
34472878RedMickey5 years ago | 155 | this.logger.logWithCustomTag( |
| 156 | this.PROXY_LOG_TAGS.APPLICATION_COMMAND, | |
| 157 | JSON.stringify(event, null, 2), | |
| 158 | this.logLevel, | |
| 159 | ); | |
b7451aefRedMickey6 years ago | 160 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); |
| 161 | | |
| 162 | if (processedMessage.sendBack) { | |
984ca036RedMickey6 years ago | 163 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 164 | } else { |
ebbd64f1RedMickey6 years ago | 165 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 166 | } |
f872f4d5RedMickey6 years ago | 167 | } |
| 168 | | |
b7451aefRedMickey6 years ago | 169 | private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) { |
34472878RedMickey5 years ago | 170 | this.logger.logWithCustomTag( |
| 171 | this.PROXY_LOG_TAGS.DEBUGGER_REPLY, | |
| 172 | JSON.stringify(event, null, 2), | |
| 173 | this.logLevel, | |
| 174 | ); | |
b7451aefRedMickey6 years ago | 175 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); |
| 176 | | |
| 177 | if (processedMessage.sendBack) { | |
ebbd64f1RedMickey6 years ago | 178 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 179 | } else { |
984ca036RedMickey6 years ago | 180 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 181 | } |
f872f4d5RedMickey6 years ago | 182 | } |
| 183 | | |
b7451aefRedMickey6 years ago | 184 | private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) { |
34472878RedMickey5 years ago | 185 | this.logger.logWithCustomTag( |
| 186 | this.PROXY_LOG_TAGS.APPLICATION_REPLY, | |
| 187 | JSON.stringify(event, null, 2), | |
| 188 | this.logLevel, | |
| 189 | ); | |
b7451aefRedMickey6 years ago | 190 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); |
| 191 | | |
| 192 | if (processedMessage.sendBack) { | |
984ca036RedMickey6 years ago | 193 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 194 | } else { |
ebbd64f1RedMickey6 years ago | 195 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 196 | } |
f872f4d5RedMickey6 years ago | 197 | } |
| 198 | | |
| 199 | private onDebuggerTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 200 | this.logger.error("Error on debugger transport", err); |
f872f4d5RedMickey6 years ago | 201 | } |
| 202 | | |
| 203 | private onApplicationTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 204 | this.logger.error("Error on application transport", err); |
f872f4d5RedMickey6 years ago | 205 | } |
| 206 | | |
ebbd64f1RedMickey6 years ago | 207 | private async onApplicationTargetClosed() { |
| 208 | this.applicationTarget = null; | |
19df32dcRedMickey4 years ago | 209 | this.applicationTargetEventEmitter.fire({}); |
ebbd64f1RedMickey6 years ago | 210 | } |
| 211 | | |
4c757eebRedMickey6 years ago | 212 | private async onDebuggerTargetClosed() { |
984ca036RedMickey6 years ago | 213 | this.browserInspectUri = ""; |
34472878RedMickey5 years ago | 214 | this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" }); |
ebbd64f1RedMickey6 years ago | 215 | this.debuggerTarget = null; |
af1b9666RedMickey6 years ago | 216 | } |
f872f4d5RedMickey6 years ago | 217 | } |