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