microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/cdp-proxy/reactNativeCDPProxy.ts

217lines · modeblame

f872f4d5RedMickey6 years ago1// 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 ago4import { IncomingMessage } from "http";
f872f4d5RedMickey6 years ago5import {
6Connection,
7Server,
8WebSocketTransport,
9IProtocolCommand,
10IProtocolError,
34472878RedMickey5 years ago11IProtocolSuccess,
f872f4d5RedMickey6 years ago12} from "vscode-cdp-proxy";
19df32dcRedMickey4 years ago13import { CancellationToken, EventEmitter } from "vscode";
f872f4d5RedMickey6 years ago14import { OutputChannelLogger } from "../extension/log/OutputChannelLogger";
a324603aRedMickey6 years ago15import { LogLevel } from "../extension/log/LogHelper";
d9c9ddcbRedMickey6 years ago16import { DebuggerEndpointHelper } from "./debuggerEndpointHelper";
259c018fYuri Skorokhodov5 years ago17import { BaseCDPMessageHandler } from "./CDPMessageHandlers/baseCDPMessageHandler";
f872f4d5RedMickey6 years ago18
19export class ReactNativeCDPProxy {
a324603aRedMickey6 years ago20private readonly PROXY_LOG_TAGS = {
21DEBUGGER_COMMAND: "Command Debugger To Target",
22APPLICATION_COMMAND: "Command Target To Debugger",
23DEBUGGER_REPLY: "Reply From Debugger To Target",
24APPLICATION_REPLY: "Reply From Target To Debugger",
25};
26
d9c9ddcbRedMickey6 years ago27private server: Server | null;
28private hostAddress: string;
29private port: number;
ebbd64f1RedMickey6 years ago30private debuggerTarget: Connection | null;
984ca036RedMickey6 years ago31private applicationTarget: Connection | null;
d9c9ddcbRedMickey6 years ago32private logger: OutputChannelLogger;
33private logLevel: LogLevel;
34private debuggerEndpointHelper: DebuggerEndpointHelper;
259c018fYuri Skorokhodov5 years ago35private CDPMessageHandler: BaseCDPMessageHandler;
d9c9ddcbRedMickey6 years ago36private applicationTargetPort: number;
984ca036RedMickey6 years ago37private browserInspectUri: string;
e23d1841RedMickey6 years ago38private cancellationToken: CancellationToken | undefined;
19df32dcRedMickey4 years ago39private applicationTargetEventEmitter: EventEmitter<unknown> = new EventEmitter();
40
41public readonly onApplicationTargetConnectionClosed = this.applicationTargetEventEmitter.event;
d9c9ddcbRedMickey6 years ago42
a6562589RedMickey6 years ago43constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) {
f872f4d5RedMickey6 years ago44this.port = port;
45this.hostAddress = hostAddress;
34472878RedMickey5 years ago46this.logger = OutputChannelLogger.getChannel(
47"React Native Chrome Proxy",
09f6024fHeniker4 years ago48process.env.REACT_NATIVE_TOOLS_LAZY_LOGS !== "false",
34472878RedMickey5 years ago49false,
50true,
51);
f872f4d5RedMickey6 years ago52this.logLevel = logLevel;
984ca036RedMickey6 years ago53this.browserInspectUri = "";
d9c9ddcbRedMickey6 years ago54this.debuggerEndpointHelper = new DebuggerEndpointHelper();
f872f4d5RedMickey6 years ago55}
56
0d77292aJiglioNero4 years ago57public async initializeServer(
259c018fYuri Skorokhodov5 years ago58CDPMessageHandler: BaseCDPMessageHandler,
e23d1841RedMickey6 years ago59logLevel: LogLevel,
34472878RedMickey5 years ago60cancellationToken?: CancellationToken,
e23d1841RedMickey6 years ago61): Promise<void> {
a6562589RedMickey6 years ago62this.logLevel = logLevel;
63this.CDPMessageHandler = CDPMessageHandler;
e23d1841RedMickey6 years ago64this.cancellationToken = cancellationToken;
a6562589RedMickey6 years ago65
0d77292aJiglioNero4 years ago66this.server = await Server.create({ port: this.port, host: this.hostAddress });
67this.server.onConnection(this.onConnectionHandler.bind(this));
f872f4d5RedMickey6 years ago68}
69
984ca036RedMickey6 years ago70public async stopServer(): Promise<void> {
f872f4d5RedMickey6 years ago71if (this.server) {
72this.server.dispose();
73this.server = null;
74}
984ca036RedMickey6 years ago75
76if (this.applicationTarget) {
77await this.applicationTarget.close();
78this.applicationTarget = null;
79}
80
81this.browserInspectUri = "";
e23d1841RedMickey6 years ago82this.cancellationToken = undefined;
984ca036RedMickey6 years ago83}
84
34472878RedMickey5 years ago85public setBrowserInspectUri(browserInspectUri: string): void {
984ca036RedMickey6 years ago86this.browserInspectUri = browserInspectUri;
f872f4d5RedMickey6 years ago87}
88
d9c9ddcbRedMickey6 years ago89public setApplicationTargetPort(applicationTargetPort: number): void {
90this.applicationTargetPort = applicationTargetPort;
f872f4d5RedMickey6 years ago91}
92
34472878RedMickey5 years ago93// eslint-disable-next-line @typescript-eslint/no-unused-vars
94private async onConnectionHandler([debuggerTarget, request]: [
95Connection,
96IncomingMessage,
97]): Promise<void> {
f872f4d5RedMickey6 years ago98this.debuggerTarget = debuggerTarget;
99
100this.debuggerTarget.pause(); // don't listen for events until the target is ready
101
984ca036RedMickey6 years ago102if (!this.browserInspectUri) {
e23d1841RedMickey6 years ago103if (this.cancellationToken) {
104this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
105`http://localhost:${this.applicationTargetPort}`,
10690,
34472878RedMickey5 years ago107this.cancellationToken,
e23d1841RedMickey6 years ago108);
109} else {
34472878RedMickey5 years ago110this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint(
111`http://localhost:${this.applicationTargetPort}`,
112);
e23d1841RedMickey6 years ago113}
984ca036RedMickey6 years ago114}
d9c9ddcbRedMickey6 years ago115
34472878RedMickey5 years ago116this.applicationTarget = new Connection(
117await WebSocketTransport.create(this.browserInspectUri),
118);
f872f4d5RedMickey6 years ago119
120this.applicationTarget.onError(this.onApplicationTargetError.bind(this));
121this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this));
122
123this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this));
124this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this));
125
126this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this));
127this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this));
128
ebbd64f1RedMickey6 years ago129this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this));
4c757eebRedMickey6 years ago130this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this));
af1b9666RedMickey6 years ago131
259c018fYuri Skorokhodov5 years ago132this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget);
133this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget);
134
f872f4d5RedMickey6 years ago135// dequeue any messages we got in the meantime
136this.debuggerTarget.unpause();
137}
138
b7451aefRedMickey6 years ago139private handleDebuggerTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago140this.logger.logWithCustomTag(
141this.PROXY_LOG_TAGS.DEBUGGER_COMMAND,
142JSON.stringify(event, null, 2),
143this.logLevel,
144);
b7451aefRedMickey6 years ago145const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
146
147if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago148this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago149} else {
984ca036RedMickey6 years ago150this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago151}
f872f4d5RedMickey6 years ago152}
153
b7451aefRedMickey6 years ago154private handleApplicationTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago155this.logger.logWithCustomTag(
156this.PROXY_LOG_TAGS.APPLICATION_COMMAND,
157JSON.stringify(event, null, 2),
158this.logLevel,
159);
b7451aefRedMickey6 years ago160const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
161
162if (processedMessage.sendBack) {
984ca036RedMickey6 years ago163this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago164} else {
ebbd64f1RedMickey6 years ago165this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago166}
f872f4d5RedMickey6 years ago167}
168
b7451aefRedMickey6 years ago169private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago170this.logger.logWithCustomTag(
171this.PROXY_LOG_TAGS.DEBUGGER_REPLY,
172JSON.stringify(event, null, 2),
173this.logLevel,
174);
b7451aefRedMickey6 years ago175const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
176
177if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago178this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago179} else {
984ca036RedMickey6 years ago180this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago181}
f872f4d5RedMickey6 years ago182}
183
b7451aefRedMickey6 years ago184private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago185this.logger.logWithCustomTag(
186this.PROXY_LOG_TAGS.APPLICATION_REPLY,
187JSON.stringify(event, null, 2),
188this.logLevel,
189);
b7451aefRedMickey6 years ago190const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
191
192if (processedMessage.sendBack) {
984ca036RedMickey6 years ago193this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago194} else {
ebbd64f1RedMickey6 years ago195this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago196}
f872f4d5RedMickey6 years ago197}
198
199private onDebuggerTargetError(err: Error) {
a324603aRedMickey6 years ago200this.logger.error("Error on debugger transport", err);
f872f4d5RedMickey6 years ago201}
202
203private onApplicationTargetError(err: Error) {
a324603aRedMickey6 years ago204this.logger.error("Error on application transport", err);
f872f4d5RedMickey6 years ago205}
206
ebbd64f1RedMickey6 years ago207private async onApplicationTargetClosed() {
208this.applicationTarget = null;
19df32dcRedMickey4 years ago209this.applicationTargetEventEmitter.fire({});
ebbd64f1RedMickey6 years ago210}
211
4c757eebRedMickey6 years ago212private async onDebuggerTargetClosed() {
984ca036RedMickey6 years ago213this.browserInspectUri = "";
34472878RedMickey5 years ago214this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" });
ebbd64f1RedMickey6 years ago215this.debuggerTarget = null;
af1b9666RedMickey6 years ago216}
f872f4d5RedMickey6 years ago217}