microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
5940f996e19e33b2c611c0d447a7ffb66d249d80

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/cdp-proxy/reactNativeCDPProxy.ts

215lines · 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",
45true,
46false,
47true,
48);
f872f4d5RedMickey6 years ago49this.logLevel = logLevel;
984ca036RedMickey6 years ago50this.browserInspectUri = "";
d9c9ddcbRedMickey6 years ago51this.debuggerEndpointHelper = new DebuggerEndpointHelper();
f872f4d5RedMickey6 years ago52}
53
e23d1841RedMickey6 years ago54public 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
34472878RedMickey5 years ago63return Server.create({ port: this.port, host: this.hostAddress }).then((server: Server) => {
64this.server = server;
65this.server.onConnection(this.onConnectionHandler.bind(this));
66});
f872f4d5RedMickey6 years ago67}
68
984ca036RedMickey6 years ago69public async stopServer(): Promise<void> {
f872f4d5RedMickey6 years ago70if (this.server) {
71this.server.dispose();
72this.server = null;
73}
984ca036RedMickey6 years ago74
75if (this.applicationTarget) {
76await this.applicationTarget.close();
77this.applicationTarget = null;
78}
79
80this.browserInspectUri = "";
e23d1841RedMickey6 years ago81this.cancellationToken = undefined;
984ca036RedMickey6 years ago82}
83
34472878RedMickey5 years ago84public setBrowserInspectUri(browserInspectUri: string): void {
984ca036RedMickey6 years ago85this.browserInspectUri = browserInspectUri;
f872f4d5RedMickey6 years ago86}
87
d9c9ddcbRedMickey6 years ago88public setApplicationTargetPort(applicationTargetPort: number): void {
89this.applicationTargetPort = applicationTargetPort;
f872f4d5RedMickey6 years ago90}
91
34472878RedMickey5 years ago92// eslint-disable-next-line @typescript-eslint/no-unused-vars
93private async onConnectionHandler([debuggerTarget, request]: [
94Connection,
95IncomingMessage,
96]): Promise<void> {
f872f4d5RedMickey6 years ago97this.debuggerTarget = debuggerTarget;
98
99this.debuggerTarget.pause(); // don't listen for events until the target is ready
100
984ca036RedMickey6 years ago101if (!this.browserInspectUri) {
e23d1841RedMickey6 years ago102if (this.cancellationToken) {
103this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
104`http://localhost:${this.applicationTargetPort}`,
10590,
34472878RedMickey5 years ago106this.cancellationToken,
e23d1841RedMickey6 years ago107);
108} else {
34472878RedMickey5 years ago109this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint(
110`http://localhost:${this.applicationTargetPort}`,
111);
e23d1841RedMickey6 years ago112}
984ca036RedMickey6 years ago113}
d9c9ddcbRedMickey6 years ago114
34472878RedMickey5 years ago115this.applicationTarget = new Connection(
116await WebSocketTransport.create(this.browserInspectUri),
117);
f872f4d5RedMickey6 years ago118
119this.applicationTarget.onError(this.onApplicationTargetError.bind(this));
120this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this));
121
122this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this));
123this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this));
124
125this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this));
126this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this));
127
ebbd64f1RedMickey6 years ago128this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this));
4c757eebRedMickey6 years ago129this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this));
af1b9666RedMickey6 years ago130
259c018fYuri Skorokhodov5 years ago131this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget);
132this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget);
133
f872f4d5RedMickey6 years ago134// dequeue any messages we got in the meantime
135this.debuggerTarget.unpause();
136}
137
b7451aefRedMickey6 years ago138private handleDebuggerTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago139this.logger.logWithCustomTag(
140this.PROXY_LOG_TAGS.DEBUGGER_COMMAND,
141JSON.stringify(event, null, 2),
142this.logLevel,
143);
b7451aefRedMickey6 years ago144const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
145
146if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago147this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago148} else {
984ca036RedMickey6 years ago149this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago150}
f872f4d5RedMickey6 years ago151}
152
b7451aefRedMickey6 years ago153private handleApplicationTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago154this.logger.logWithCustomTag(
155this.PROXY_LOG_TAGS.APPLICATION_COMMAND,
156JSON.stringify(event, null, 2),
157this.logLevel,
158);
b7451aefRedMickey6 years ago159const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
160
161if (processedMessage.sendBack) {
984ca036RedMickey6 years ago162this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago163} else {
ebbd64f1RedMickey6 years ago164this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago165}
f872f4d5RedMickey6 years ago166}
167
b7451aefRedMickey6 years ago168private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago169this.logger.logWithCustomTag(
170this.PROXY_LOG_TAGS.DEBUGGER_REPLY,
171JSON.stringify(event, null, 2),
172this.logLevel,
173);
b7451aefRedMickey6 years ago174const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
175
176if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago177this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago178} else {
984ca036RedMickey6 years ago179this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago180}
f872f4d5RedMickey6 years ago181}
182
b7451aefRedMickey6 years ago183private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago184this.logger.logWithCustomTag(
185this.PROXY_LOG_TAGS.APPLICATION_REPLY,
186JSON.stringify(event, null, 2),
187this.logLevel,
188);
b7451aefRedMickey6 years ago189const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
190
191if (processedMessage.sendBack) {
984ca036RedMickey6 years ago192this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago193} else {
ebbd64f1RedMickey6 years ago194this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago195}
f872f4d5RedMickey6 years ago196}
197
198private onDebuggerTargetError(err: Error) {
a324603aRedMickey6 years ago199this.logger.error("Error on debugger transport", err);
f872f4d5RedMickey6 years ago200}
201
202private onApplicationTargetError(err: Error) {
a324603aRedMickey6 years ago203this.logger.error("Error on application transport", err);
f872f4d5RedMickey6 years ago204}
205
ebbd64f1RedMickey6 years ago206private async onApplicationTargetClosed() {
207this.applicationTarget = null;
208}
209
4c757eebRedMickey6 years ago210private async onDebuggerTargetClosed() {
984ca036RedMickey6 years ago211this.browserInspectUri = "";
34472878RedMickey5 years ago212this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" });
ebbd64f1RedMickey6 years ago213this.debuggerTarget = null;
af1b9666RedMickey6 years ago214}
f872f4d5RedMickey6 years ago215}