microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/cdp-proxy/reactNativeCDPProxy.ts

213lines · 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
4import {
5Connection,
6Server,
7WebSocketTransport,
8IProtocolCommand,
9IProtocolError,
34472878RedMickey5 years ago10IProtocolSuccess,
f872f4d5RedMickey6 years ago11} from "vscode-cdp-proxy";
af1b9666RedMickey6 years ago12import { IncomingMessage } from "http";
e23d1841RedMickey6 years ago13import { CancellationToken } 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;
d9c9ddcbRedMickey6 years ago39
a6562589RedMickey6 years ago40constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) {
f872f4d5RedMickey6 years ago41this.port = port;
42this.hostAddress = hostAddress;
34472878RedMickey5 years ago43this.logger = OutputChannelLogger.getChannel(
44"React Native Chrome Proxy",
5c9b8eb8RedMickey5 years ago45process.env.REACT_NATIVE_TOOLS_LAZY_LOGS === "false" ? false : true,
34472878RedMickey5 years ago46false,
47true,
48);
f872f4d5RedMickey6 years ago49this.logLevel = logLevel;
984ca036RedMickey6 years ago50this.browserInspectUri = "";
d9c9ddcbRedMickey6 years ago51this.debuggerEndpointHelper = new DebuggerEndpointHelper();
f872f4d5RedMickey6 years ago52}
53
0d77292aJiglioNero4 years ago54public async initializeServer(
259c018fYuri Skorokhodov5 years ago55CDPMessageHandler: BaseCDPMessageHandler,
e23d1841RedMickey6 years ago56logLevel: LogLevel,
34472878RedMickey5 years ago57cancellationToken?: CancellationToken,
e23d1841RedMickey6 years ago58): Promise<void> {
a6562589RedMickey6 years ago59this.logLevel = logLevel;
60this.CDPMessageHandler = CDPMessageHandler;
e23d1841RedMickey6 years ago61this.cancellationToken = cancellationToken;
a6562589RedMickey6 years ago62
0d77292aJiglioNero4 years ago63this.server = await Server.create({ port: this.port, host: this.hostAddress });
64this.server.onConnection(this.onConnectionHandler.bind(this));
f872f4d5RedMickey6 years ago65}
66
984ca036RedMickey6 years ago67public async stopServer(): Promise<void> {
f872f4d5RedMickey6 years ago68if (this.server) {
69this.server.dispose();
70this.server = null;
71}
984ca036RedMickey6 years ago72
73if (this.applicationTarget) {
74await this.applicationTarget.close();
75this.applicationTarget = null;
76}
77
78this.browserInspectUri = "";
e23d1841RedMickey6 years ago79this.cancellationToken = undefined;
984ca036RedMickey6 years ago80}
81
34472878RedMickey5 years ago82public setBrowserInspectUri(browserInspectUri: string): void {
984ca036RedMickey6 years ago83this.browserInspectUri = browserInspectUri;
f872f4d5RedMickey6 years ago84}
85
d9c9ddcbRedMickey6 years ago86public setApplicationTargetPort(applicationTargetPort: number): void {
87this.applicationTargetPort = applicationTargetPort;
f872f4d5RedMickey6 years ago88}
89
34472878RedMickey5 years ago90// eslint-disable-next-line @typescript-eslint/no-unused-vars
91private async onConnectionHandler([debuggerTarget, request]: [
92Connection,
93IncomingMessage,
94]): Promise<void> {
f872f4d5RedMickey6 years ago95this.debuggerTarget = debuggerTarget;
96
97this.debuggerTarget.pause(); // don't listen for events until the target is ready
98
984ca036RedMickey6 years ago99if (!this.browserInspectUri) {
e23d1841RedMickey6 years ago100if (this.cancellationToken) {
101this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
102`http://localhost:${this.applicationTargetPort}`,
10390,
34472878RedMickey5 years ago104this.cancellationToken,
e23d1841RedMickey6 years ago105);
106} else {
34472878RedMickey5 years ago107this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint(
108`http://localhost:${this.applicationTargetPort}`,
109);
e23d1841RedMickey6 years ago110}
984ca036RedMickey6 years ago111}
d9c9ddcbRedMickey6 years ago112
34472878RedMickey5 years ago113this.applicationTarget = new Connection(
114await WebSocketTransport.create(this.browserInspectUri),
115);
f872f4d5RedMickey6 years ago116
117this.applicationTarget.onError(this.onApplicationTargetError.bind(this));
118this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this));
119
120this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this));
121this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this));
122
123this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this));
124this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this));
125
ebbd64f1RedMickey6 years ago126this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this));
4c757eebRedMickey6 years ago127this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this));
af1b9666RedMickey6 years ago128
259c018fYuri Skorokhodov5 years ago129this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget);
130this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget);
131
f872f4d5RedMickey6 years ago132// dequeue any messages we got in the meantime
133this.debuggerTarget.unpause();
134}
135
b7451aefRedMickey6 years ago136private handleDebuggerTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago137this.logger.logWithCustomTag(
138this.PROXY_LOG_TAGS.DEBUGGER_COMMAND,
139JSON.stringify(event, null, 2),
140this.logLevel,
141);
b7451aefRedMickey6 years ago142const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
143
144if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago145this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago146} else {
984ca036RedMickey6 years ago147this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago148}
f872f4d5RedMickey6 years ago149}
150
b7451aefRedMickey6 years ago151private handleApplicationTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago152this.logger.logWithCustomTag(
153this.PROXY_LOG_TAGS.APPLICATION_COMMAND,
154JSON.stringify(event, null, 2),
155this.logLevel,
156);
b7451aefRedMickey6 years ago157const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
158
159if (processedMessage.sendBack) {
984ca036RedMickey6 years ago160this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago161} else {
ebbd64f1RedMickey6 years ago162this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago163}
f872f4d5RedMickey6 years ago164}
165
b7451aefRedMickey6 years ago166private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago167this.logger.logWithCustomTag(
168this.PROXY_LOG_TAGS.DEBUGGER_REPLY,
169JSON.stringify(event, null, 2),
170this.logLevel,
171);
b7451aefRedMickey6 years ago172const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
173
174if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago175this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago176} else {
984ca036RedMickey6 years ago177this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago178}
f872f4d5RedMickey6 years ago179}
180
b7451aefRedMickey6 years ago181private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago182this.logger.logWithCustomTag(
183this.PROXY_LOG_TAGS.APPLICATION_REPLY,
184JSON.stringify(event, null, 2),
185this.logLevel,
186);
b7451aefRedMickey6 years ago187const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
188
189if (processedMessage.sendBack) {
984ca036RedMickey6 years ago190this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago191} else {
ebbd64f1RedMickey6 years ago192this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago193}
f872f4d5RedMickey6 years ago194}
195
196private onDebuggerTargetError(err: Error) {
a324603aRedMickey6 years ago197this.logger.error("Error on debugger transport", err);
f872f4d5RedMickey6 years ago198}
199
200private onApplicationTargetError(err: Error) {
a324603aRedMickey6 years ago201this.logger.error("Error on application transport", err);
f872f4d5RedMickey6 years ago202}
203
ebbd64f1RedMickey6 years ago204private async onApplicationTargetClosed() {
205this.applicationTarget = null;
206}
207
4c757eebRedMickey6 years ago208private async onDebuggerTargetClosed() {
984ca036RedMickey6 years ago209this.browserInspectUri = "";
34472878RedMickey5 years ago210this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" });
ebbd64f1RedMickey6 years ago211this.debuggerTarget = null;
af1b9666RedMickey6 years ago212}
f872f4d5RedMickey6 years ago213}