microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/reactNativeCDPProxy.ts
147lines · 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"; |
f872f4d5RedMickey6 years ago | 13 | import { OutputChannelLogger } from "../extension/log/OutputChannelLogger"; |
a324603aRedMickey6 years ago | 14 | import { LogLevel } from "../extension/log/LogHelper"; |
d9c9ddcbRedMickey6 years ago | 15 | import { DebuggerEndpointHelper } from "./debuggerEndpointHelper"; |
a6562589RedMickey6 years ago | 16 | import { ICDPMessageHandler } from "./CDPMessageHandlers/ICDPMessageHandler"; |
f872f4d5RedMickey6 years ago | 17 | |
| 18 | export class ReactNativeCDPProxy { | |
| 19 | | |
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; | |
| 30 | private debuggerTarget: Connection; | |
| 31 | private applicationTarget: Connection; | |
| 32 | private logger: OutputChannelLogger; | |
| 33 | private logLevel: LogLevel; | |
| 34 | private debuggerEndpointHelper: DebuggerEndpointHelper; | |
a6562589RedMickey6 years ago | 35 | private CDPMessageHandler: ICDPMessageHandler; |
d9c9ddcbRedMickey6 years ago | 36 | private applicationTargetPort: number; |
| 37 | | |
a6562589RedMickey6 years ago | 38 | constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) { |
f872f4d5RedMickey6 years ago | 39 | this.port = port; |
| 40 | this.hostAddress = hostAddress; | |
a324603aRedMickey6 years ago | 41 | this.logger = OutputChannelLogger.getChannel("React Native Chrome Proxy", true, false, true); |
f872f4d5RedMickey6 years ago | 42 | this.logLevel = logLevel; |
d9c9ddcbRedMickey6 years ago | 43 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
f872f4d5RedMickey6 years ago | 44 | } |
| 45 | | |
a6562589RedMickey6 years ago | 46 | public initializeServer(CDPMessageHandler: ICDPMessageHandler, logLevel: LogLevel): Promise<void> { |
| 47 | this.logLevel = logLevel; | |
| 48 | this.CDPMessageHandler = CDPMessageHandler; | |
| 49 | | |
f872f4d5RedMickey6 years ago | 50 | return Server.create({ port: this.port, host: this.hostAddress }) |
| 51 | .then((server: Server) => { | |
| 52 | this.server = server; | |
| 53 | this.server.onConnection(this.onConnectionHandler.bind(this)); | |
| 54 | }); | |
| 55 | } | |
| 56 | | |
| 57 | public stopServer(): void { | |
| 58 | if (this.server) { | |
| 59 | this.server.dispose(); | |
| 60 | this.server = null; | |
| 61 | } | |
| 62 | } | |
| 63 | | |
d9c9ddcbRedMickey6 years ago | 64 | public setApplicationTargetPort(applicationTargetPort: number): void { |
| 65 | this.applicationTargetPort = applicationTargetPort; | |
f872f4d5RedMickey6 years ago | 66 | } |
| 67 | | |
| 68 | private async onConnectionHandler([debuggerTarget, request]: [Connection, IncomingMessage]): Promise<void> { | |
| 69 | this.debuggerTarget = debuggerTarget; | |
| 70 | | |
| 71 | this.debuggerTarget.pause(); // don't listen for events until the target is ready | |
| 72 | | |
b7451aefRedMickey6 years ago | 73 | const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(`http://localhost:${this.applicationTargetPort}`, 100); |
d9c9ddcbRedMickey6 years ago | 74 | |
f872f4d5RedMickey6 years ago | 75 | this.applicationTarget = new Connection(await WebSocketTransport.create(browserInspectUri)); |
| 76 | | |
| 77 | this.applicationTarget.onError(this.onApplicationTargetError.bind(this)); | |
| 78 | this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this)); | |
| 79 | | |
| 80 | this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this)); | |
| 81 | this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this)); | |
| 82 | | |
| 83 | this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this)); | |
| 84 | this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this)); | |
| 85 | | |
4c757eebRedMickey6 years ago | 86 | this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this)); |
af1b9666RedMickey6 years ago | 87 | |
f872f4d5RedMickey6 years ago | 88 | // dequeue any messages we got in the meantime |
| 89 | this.debuggerTarget.unpause(); | |
| 90 | } | |
| 91 | | |
b7451aefRedMickey6 years ago | 92 | private handleDebuggerTargetCommand(event: IProtocolCommand) { |
| 93 | this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.DEBUGGER_COMMAND, JSON.stringify(event, null , 2), this.logLevel); | |
| 94 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); | |
| 95 | | |
| 96 | if (processedMessage.sendBack) { | |
| 97 | this.debuggerTarget.send(processedMessage.event); | |
| 98 | } else { | |
| 99 | this.applicationTarget.send(processedMessage.event); | |
| 100 | } | |
f872f4d5RedMickey6 years ago | 101 | } |
| 102 | | |
b7451aefRedMickey6 years ago | 103 | private handleApplicationTargetCommand(event: IProtocolCommand) { |
| 104 | this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.APPLICATION_COMMAND, JSON.stringify(event, null , 2), this.logLevel); | |
| 105 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); | |
| 106 | | |
| 107 | if (processedMessage.sendBack) { | |
| 108 | this.applicationTarget.send(processedMessage.event); | |
| 109 | } else { | |
| 110 | this.debuggerTarget.send(processedMessage.event); | |
| 111 | } | |
f872f4d5RedMickey6 years ago | 112 | } |
| 113 | | |
b7451aefRedMickey6 years ago | 114 | private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) { |
| 115 | this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.DEBUGGER_REPLY, JSON.stringify(event, null , 2), this.logLevel); | |
| 116 | const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event); | |
| 117 | | |
| 118 | if (processedMessage.sendBack) { | |
| 119 | this.debuggerTarget.send(processedMessage.event); | |
| 120 | } else { | |
| 121 | this.applicationTarget.send(processedMessage.event); | |
| 122 | } | |
f872f4d5RedMickey6 years ago | 123 | } |
| 124 | | |
b7451aefRedMickey6 years ago | 125 | private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) { |
| 126 | this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.APPLICATION_REPLY, JSON.stringify(event, null , 2), this.logLevel); | |
| 127 | const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event); | |
| 128 | | |
| 129 | if (processedMessage.sendBack) { | |
| 130 | this.applicationTarget.send(processedMessage.event); | |
| 131 | } else { | |
| 132 | this.debuggerTarget.send(processedMessage.event); | |
| 133 | } | |
f872f4d5RedMickey6 years ago | 134 | } |
| 135 | | |
| 136 | private onDebuggerTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 137 | this.logger.error("Error on debugger transport", err); |
f872f4d5RedMickey6 years ago | 138 | } |
| 139 | | |
| 140 | private onApplicationTargetError(err: Error) { | |
a324603aRedMickey6 years ago | 141 | this.logger.error("Error on application transport", err); |
f872f4d5RedMickey6 years ago | 142 | } |
| 143 | | |
4c757eebRedMickey6 years ago | 144 | private async onDebuggerTargetClosed() { |
b7451aefRedMickey6 years ago | 145 | this.CDPMessageHandler.processDebuggerCDPMessage({method: "close"}); |
af1b9666RedMickey6 years ago | 146 | } |
f872f4d5RedMickey6 years ago | 147 | } |