microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/reactNativeCDPProxy.ts
227lines · 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 | | |
176f99c8ConnorQi013 months ago | 27 | private server: Server | null = null; |
d9c9ddcbRedMickey6 years ago | 28 | private hostAddress: string; |
| 29 | private port: number; | |
176f99c8ConnorQi013 months ago | 30 | private debuggerTarget: Connection | null = null; |
| 31 | private applicationTarget: Connection | null = null; | |
d9c9ddcbRedMickey6 years ago | 32 | private logger: OutputChannelLogger; |
| 33 | private logLevel: LogLevel; | |
| 34 | private debuggerEndpointHelper: DebuggerEndpointHelper; | |
176f99c8ConnorQi013 months ago | 35 | private CDPMessageHandler!: BaseCDPMessageHandler; |
| 36 | private applicationTargetPort: number = 0; | |
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> { | |
f665013bLucy Gramley1 months ago | 100 | // Only allow connections without an Origin header (i.e., from vscode-js-debug's |
| 101 | // raw WebSocket client). Browser-initiated WebSocket connections always include | |
| 102 | // an Origin header and should be rejected. | |
| 103 | if (request.headers.origin) { | |
c37dc1d6lucygramley1 months ago | 104 | await debuggerTarget.close(); |
f665013bLucy Gramley1 months ago | 105 | return; |
| 106 | } | |
| 107 | | |
f872f4d5RedMickey6 years ago | 108 | this.debuggerTarget = debuggerTarget; |
| 109 | | |
| 110 | this.debuggerTarget.pause(); // don't listen for events until the target is ready | |
| 111 | | |
984ca036RedMickey6 years ago | 112 | if (!this.browserInspectUri) { |
e23d1841RedMickey6 years ago | 113 | if (this.cancellationToken) { |
| 114 | this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint( | |
| 115 | `http://localhost:${this.applicationTargetPort}`, | |
| 116 | 90, | |
34472878RedMickey5 years ago | 117 | this.cancellationToken, |
e23d1841RedMickey6 years ago | 118 | ); |
| 119 | } else { | |
34472878RedMickey5 years ago | 120 | this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint( |
| 121 | `http://localhost:${this.applicationTargetPort}`, | |
| 122 | ); | |
e23d1841RedMickey6 years ago | 123 | } |
984ca036RedMickey6 years ago | 124 | } |
d9c9ddcbRedMickey6 years ago | 125 | |
34472878RedMickey5 years ago | 126 | this.applicationTarget = new Connection( |
| 127 | await WebSocketTransport.create(this.browserInspectUri), | |
| 128 | ); | |
f872f4d5RedMickey6 years ago | 129 | |
| 130 | this.applicationTarget.onError(this.onApplicationTargetError.bind(this)); | |
| 131 | this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this)); | |
| 132 | | |
| 133 | this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this)); | |
| 134 | this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this)); | |
| 135 | | |
| 136 | this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this)); | |
| 137 | this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this)); | |
| 138 | | |
ebbd64f1RedMickey6 years ago | 139 | this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this)); |
4c757eebRedMickey6 years ago | 140 | this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this)); |
af1b9666RedMickey6 years ago | 141 | |
259c018fYuri Skorokhodov5 years ago | 142 | this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget); |
| 143 | this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget); | |
| 144 | | |
f872f4d5RedMickey6 years ago | 145 | // dequeue any messages we got in the meantime |
| 146 | this.debuggerTarget.unpause(); | |
| 147 | } | |
| 148 | | |
b7451aefRedMickey6 years ago | 149 | private handleDebuggerTargetCommand(event: IProtocolCommand) { |
34472878RedMickey5 years ago | 150 | this.logger.logWithCustomTag( |
| 151 | this.PROXY_LOG_TAGS.DEBUGGER_COMMAND, | |
| 152 | JSON.stringify(event, null, 2), | |
| 153 | this.logLevel, | |
| 154 | ); | |
b7451aefRedMickey6 years ago | 155 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); |
| 156 | | |
| 157 | if (processedMessage.sendBack) { | |
ebbd64f1RedMickey6 years ago | 158 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 159 | } else { |
984ca036RedMickey6 years ago | 160 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 161 | } |
f872f4d5RedMickey6 years ago | 162 | } |
| 163 | | |
b7451aefRedMickey6 years ago | 164 | private handleApplicationTargetCommand(event: IProtocolCommand) { |
34472878RedMickey5 years ago | 165 | this.logger.logWithCustomTag( |
| 166 | this.PROXY_LOG_TAGS.APPLICATION_COMMAND, | |
| 167 | JSON.stringify(event, null, 2), | |
| 168 | this.logLevel, | |
| 169 | ); | |
b7451aefRedMickey6 years ago | 170 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); |
| 171 | | |
| 172 | if (processedMessage.sendBack) { | |
984ca036RedMickey6 years ago | 173 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 174 | } else { |
ebbd64f1RedMickey6 years ago | 175 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 176 | } |
f872f4d5RedMickey6 years ago | 177 | } |
| 178 | | |
b7451aefRedMickey6 years ago | 179 | private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) { |
34472878RedMickey5 years ago | 180 | this.logger.logWithCustomTag( |
| 181 | this.PROXY_LOG_TAGS.DEBUGGER_REPLY, | |
| 182 | JSON.stringify(event, null, 2), | |
| 183 | this.logLevel, | |
| 184 | ); | |
b7451aefRedMickey6 years ago | 185 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); |
| 186 | | |
| 187 | if (processedMessage.sendBack) { | |
ebbd64f1RedMickey6 years ago | 188 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 189 | } else { |
984ca036RedMickey6 years ago | 190 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 191 | } |
f872f4d5RedMickey6 years ago | 192 | } |
| 193 | | |
b7451aefRedMickey6 years ago | 194 | private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) { |
34472878RedMickey5 years ago | 195 | this.logger.logWithCustomTag( |
| 196 | this.PROXY_LOG_TAGS.APPLICATION_REPLY, | |
| 197 | JSON.stringify(event, null, 2), | |
| 198 | this.logLevel, | |
| 199 | ); | |
b7451aefRedMickey6 years ago | 200 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); |
| 201 | | |
| 202 | if (processedMessage.sendBack) { | |
984ca036RedMickey6 years ago | 203 | this.applicationTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 204 | } else { |
ebbd64f1RedMickey6 years ago | 205 | this.debuggerTarget?.send(processedMessage.event); |
b7451aefRedMickey6 years ago | 206 | } |
f872f4d5RedMickey6 years ago | 207 | } |
| 208 | | |
| 209 | private onDebuggerTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 210 | this.logger.error("Error on debugger transport", err); |
f872f4d5RedMickey6 years ago | 211 | } |
| 212 | | |
| 213 | private onApplicationTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 214 | this.logger.error("Error on application transport", err); |
f872f4d5RedMickey6 years ago | 215 | } |
| 216 | | |
ebbd64f1RedMickey6 years ago | 217 | private async onApplicationTargetClosed() { |
| 218 | this.applicationTarget = null; | |
19df32dcRedMickey4 years ago | 219 | this.applicationTargetEventEmitter.fire({}); |
ebbd64f1RedMickey6 years ago | 220 | } |
| 221 | | |
4c757eebRedMickey6 years ago | 222 | private async onDebuggerTargetClosed() { |
984ca036RedMickey6 years ago | 223 | this.browserInspectUri = ""; |
34472878RedMickey5 years ago | 224 | this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" }); |
ebbd64f1RedMickey6 years ago | 225 | this.debuggerTarget = null; |
af1b9666RedMickey6 years ago | 226 | } |
f872f4d5RedMickey6 years ago | 227 | } |