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