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