microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e23d18413023bf3cf53b60f216b32f9b33263a28

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/cdp-proxy/reactNativeCDPProxy.ts

179lines · 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,
10IProtocolSuccess
11} 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";
a6562589RedMickey6 years ago17import { ICDPMessageHandler } from "./CDPMessageHandlers/ICDPMessageHandler";
f872f4d5RedMickey6 years ago18
19export class ReactNativeCDPProxy {
20
a324603aRedMickey6 years ago21private readonly PROXY_LOG_TAGS = {
22DEBUGGER_COMMAND: "Command Debugger To Target",
23APPLICATION_COMMAND: "Command Target To Debugger",
24DEBUGGER_REPLY: "Reply From Debugger To Target",
25APPLICATION_REPLY: "Reply From Target To Debugger",
26};
27
d9c9ddcbRedMickey6 years ago28private server: Server | null;
29private hostAddress: string;
30private port: number;
31private debuggerTarget: Connection;
984ca036RedMickey6 years ago32private applicationTarget: Connection | null;
d9c9ddcbRedMickey6 years ago33private logger: OutputChannelLogger;
34private logLevel: LogLevel;
35private debuggerEndpointHelper: DebuggerEndpointHelper;
a6562589RedMickey6 years ago36private CDPMessageHandler: ICDPMessageHandler;
d9c9ddcbRedMickey6 years ago37private applicationTargetPort: number;
984ca036RedMickey6 years ago38private browserInspectUri: string;
e23d1841RedMickey6 years ago39private cancellationToken: CancellationToken | undefined;
d9c9ddcbRedMickey6 years ago40
a6562589RedMickey6 years ago41constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) {
f872f4d5RedMickey6 years ago42this.port = port;
43this.hostAddress = hostAddress;
a324603aRedMickey6 years ago44this.logger = OutputChannelLogger.getChannel("React Native Chrome Proxy", true, false, true);
f872f4d5RedMickey6 years ago45this.logLevel = logLevel;
984ca036RedMickey6 years ago46this.browserInspectUri = "";
d9c9ddcbRedMickey6 years ago47this.debuggerEndpointHelper = new DebuggerEndpointHelper();
f872f4d5RedMickey6 years ago48}
49
e23d1841RedMickey6 years ago50public initializeServer(
51CDPMessageHandler: ICDPMessageHandler,
52logLevel: LogLevel,
53cancellationToken?: CancellationToken
54): Promise<void> {
a6562589RedMickey6 years ago55this.logLevel = logLevel;
56this.CDPMessageHandler = CDPMessageHandler;
e23d1841RedMickey6 years ago57this.cancellationToken = cancellationToken;
a6562589RedMickey6 years ago58
f872f4d5RedMickey6 years ago59return Server.create({ port: this.port, host: this.hostAddress })
60.then((server: Server) => {
61this.server = server;
62this.server.onConnection(this.onConnectionHandler.bind(this));
63});
64}
65
984ca036RedMickey6 years ago66public async stopServer(): Promise<void> {
f872f4d5RedMickey6 years ago67if (this.server) {
68this.server.dispose();
69this.server = null;
70}
984ca036RedMickey6 years ago71
72if (this.applicationTarget) {
73await this.applicationTarget.close();
74this.applicationTarget = null;
75}
76
77this.browserInspectUri = "";
e23d1841RedMickey6 years ago78this.cancellationToken = undefined;
984ca036RedMickey6 years ago79}
80
81public setBrowserInspectUri(browserInspectUri: string) {
82this.browserInspectUri = browserInspectUri;
f872f4d5RedMickey6 years ago83}
84
d9c9ddcbRedMickey6 years ago85public setApplicationTargetPort(applicationTargetPort: number): void {
86this.applicationTargetPort = applicationTargetPort;
f872f4d5RedMickey6 years ago87}
88
89private async onConnectionHandler([debuggerTarget, request]: [Connection, IncomingMessage]): Promise<void> {
90this.debuggerTarget = debuggerTarget;
91
92this.debuggerTarget.pause(); // don't listen for events until the target is ready
93
984ca036RedMickey6 years ago94if (!this.browserInspectUri) {
e23d1841RedMickey6 years ago95if (this.cancellationToken) {
96this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
97`http://localhost:${this.applicationTargetPort}`,
9890,
99this.cancellationToken
100);
101} else {
102this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint(`http://localhost:${this.applicationTargetPort}`);
103}
984ca036RedMickey6 years ago104}
d9c9ddcbRedMickey6 years ago105
984ca036RedMickey6 years ago106this.applicationTarget = new Connection(await WebSocketTransport.create(this.browserInspectUri));
f872f4d5RedMickey6 years ago107
108this.applicationTarget.onError(this.onApplicationTargetError.bind(this));
109this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this));
110
111this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this));
112this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this));
113
114this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this));
115this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this));
116
4c757eebRedMickey6 years ago117this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this));
af1b9666RedMickey6 years ago118
f872f4d5RedMickey6 years ago119// dequeue any messages we got in the meantime
120this.debuggerTarget.unpause();
121}
122
b7451aefRedMickey6 years ago123private handleDebuggerTargetCommand(event: IProtocolCommand) {
124this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.DEBUGGER_COMMAND, JSON.stringify(event, null , 2), this.logLevel);
125const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
126
127if (processedMessage.sendBack) {
128this.debuggerTarget.send(processedMessage.event);
129} else {
984ca036RedMickey6 years ago130this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago131}
f872f4d5RedMickey6 years ago132}
133
b7451aefRedMickey6 years ago134private handleApplicationTargetCommand(event: IProtocolCommand) {
135this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.APPLICATION_COMMAND, JSON.stringify(event, null , 2), this.logLevel);
136const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
137
138if (processedMessage.sendBack) {
984ca036RedMickey6 years ago139this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago140} else {
141this.debuggerTarget.send(processedMessage.event);
142}
f872f4d5RedMickey6 years ago143}
144
b7451aefRedMickey6 years ago145private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) {
146this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.DEBUGGER_REPLY, JSON.stringify(event, null , 2), this.logLevel);
147const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
148
149if (processedMessage.sendBack) {
150this.debuggerTarget.send(processedMessage.event);
151} else {
984ca036RedMickey6 years ago152this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago153}
f872f4d5RedMickey6 years ago154}
155
b7451aefRedMickey6 years ago156private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) {
157this.logger.logWithCustomTag(this.PROXY_LOG_TAGS.APPLICATION_REPLY, JSON.stringify(event, null , 2), this.logLevel);
158const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
159
160if (processedMessage.sendBack) {
984ca036RedMickey6 years ago161this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago162} else {
163this.debuggerTarget.send(processedMessage.event);
164}
f872f4d5RedMickey6 years ago165}
166
167private onDebuggerTargetError(err: Error) {
a324603aRedMickey6 years ago168this.logger.error("Error on debugger transport", err);
f872f4d5RedMickey6 years ago169}
170
171private onApplicationTargetError(err: Error) {
a324603aRedMickey6 years ago172this.logger.error("Error on application transport", err);
f872f4d5RedMickey6 years ago173}
174
4c757eebRedMickey6 years ago175private async onDebuggerTargetClosed() {
984ca036RedMickey6 years ago176this.browserInspectUri = "";
b7451aefRedMickey6 years ago177this.CDPMessageHandler.processDebuggerCDPMessage({method: "close"});
af1b9666RedMickey6 years ago178}
f872f4d5RedMickey6 years ago179}