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