microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
prepare-for-1.13.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/cdp-proxy/reactNativeCDPProxy.ts

219lines · 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();
9f8c460dEzio Li2 years ago40private errorEventEmitter: EventEmitter<Error> = new EventEmitter();
19df32dcRedMickey4 years ago41
9f8c460dEzio Li2 years ago42public readonly onError = this.errorEventEmitter.event;
19df32dcRedMickey4 years ago43public readonly onApplicationTargetConnectionClosed = this.applicationTargetEventEmitter.event;
d9c9ddcbRedMickey6 years ago44
a6562589RedMickey6 years ago45constructor(hostAddress: string, port: number, logLevel: LogLevel = LogLevel.None) {
f872f4d5RedMickey6 years ago46this.port = port;
47this.hostAddress = hostAddress;
34472878RedMickey5 years ago48this.logger = OutputChannelLogger.getChannel(
49"React Native Chrome Proxy",
09f6024fHeniker4 years ago50process.env.REACT_NATIVE_TOOLS_LAZY_LOGS !== "false",
34472878RedMickey5 years ago51false,
52true,
53);
f872f4d5RedMickey6 years ago54this.logLevel = logLevel;
984ca036RedMickey6 years ago55this.browserInspectUri = "";
d9c9ddcbRedMickey6 years ago56this.debuggerEndpointHelper = new DebuggerEndpointHelper();
f872f4d5RedMickey6 years ago57}
58
0d77292aJiglioNero4 years ago59public async initializeServer(
259c018fYuri Skorokhodov5 years ago60CDPMessageHandler: BaseCDPMessageHandler,
e23d1841RedMickey6 years ago61logLevel: LogLevel,
34472878RedMickey5 years ago62cancellationToken?: CancellationToken,
e23d1841RedMickey6 years ago63): Promise<void> {
a6562589RedMickey6 years ago64this.logLevel = logLevel;
65this.CDPMessageHandler = CDPMessageHandler;
e23d1841RedMickey6 years ago66this.cancellationToken = cancellationToken;
a6562589RedMickey6 years ago67
0d77292aJiglioNero4 years ago68this.server = await Server.create({ port: this.port, host: this.hostAddress });
69this.server.onConnection(this.onConnectionHandler.bind(this));
f872f4d5RedMickey6 years ago70}
71
984ca036RedMickey6 years ago72public async stopServer(): Promise<void> {
f872f4d5RedMickey6 years ago73if (this.server) {
74this.server.dispose();
75this.server = null;
76}
984ca036RedMickey6 years ago77
78if (this.applicationTarget) {
79await this.applicationTarget.close();
80this.applicationTarget = null;
81}
82
83this.browserInspectUri = "";
e23d1841RedMickey6 years ago84this.cancellationToken = undefined;
984ca036RedMickey6 years ago85}
86
34472878RedMickey5 years ago87public setBrowserInspectUri(browserInspectUri: string): void {
984ca036RedMickey6 years ago88this.browserInspectUri = browserInspectUri;
f872f4d5RedMickey6 years ago89}
90
d9c9ddcbRedMickey6 years ago91public setApplicationTargetPort(applicationTargetPort: number): void {
92this.applicationTargetPort = applicationTargetPort;
f872f4d5RedMickey6 years ago93}
94
34472878RedMickey5 years ago95// eslint-disable-next-line @typescript-eslint/no-unused-vars
96private async onConnectionHandler([debuggerTarget, request]: [
97Connection,
98IncomingMessage,
99]): Promise<void> {
f872f4d5RedMickey6 years ago100this.debuggerTarget = debuggerTarget;
101
102this.debuggerTarget.pause(); // don't listen for events until the target is ready
103
984ca036RedMickey6 years ago104if (!this.browserInspectUri) {
e23d1841RedMickey6 years ago105if (this.cancellationToken) {
106this.browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
107`http://localhost:${this.applicationTargetPort}`,
10890,
34472878RedMickey5 years ago109this.cancellationToken,
e23d1841RedMickey6 years ago110);
111} else {
34472878RedMickey5 years ago112this.browserInspectUri = await this.debuggerEndpointHelper.getWSEndpoint(
113`http://localhost:${this.applicationTargetPort}`,
114);
e23d1841RedMickey6 years ago115}
984ca036RedMickey6 years ago116}
d9c9ddcbRedMickey6 years ago117
34472878RedMickey5 years ago118this.applicationTarget = new Connection(
119await WebSocketTransport.create(this.browserInspectUri),
120);
f872f4d5RedMickey6 years ago121
122this.applicationTarget.onError(this.onApplicationTargetError.bind(this));
123this.debuggerTarget.onError(this.onDebuggerTargetError.bind(this));
124
125this.applicationTarget.onCommand(this.handleApplicationTargetCommand.bind(this));
126this.debuggerTarget.onCommand(this.handleDebuggerTargetCommand.bind(this));
127
128this.applicationTarget.onReply(this.handleApplicationTargetReply.bind(this));
129this.debuggerTarget.onReply(this.handleDebuggerTargetReply.bind(this));
130
ebbd64f1RedMickey6 years ago131this.applicationTarget.onEnd(this.onApplicationTargetClosed.bind(this));
4c757eebRedMickey6 years ago132this.debuggerTarget.onEnd(this.onDebuggerTargetClosed.bind(this));
af1b9666RedMickey6 years ago133
259c018fYuri Skorokhodov5 years ago134this.CDPMessageHandler?.setApplicationTarget(this.applicationTarget);
135this.CDPMessageHandler?.setDebuggerTarget(this.debuggerTarget);
136
f872f4d5RedMickey6 years ago137// dequeue any messages we got in the meantime
138this.debuggerTarget.unpause();
139}
140
b7451aefRedMickey6 years ago141private handleDebuggerTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago142this.logger.logWithCustomTag(
143this.PROXY_LOG_TAGS.DEBUGGER_COMMAND,
144JSON.stringify(event, null, 2),
145this.logLevel,
146);
b7451aefRedMickey6 years ago147const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
148
149if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago150this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago151} else {
984ca036RedMickey6 years ago152this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago153}
f872f4d5RedMickey6 years ago154}
155
b7451aefRedMickey6 years ago156private handleApplicationTargetCommand(event: IProtocolCommand) {
34472878RedMickey5 years ago157this.logger.logWithCustomTag(
158this.PROXY_LOG_TAGS.APPLICATION_COMMAND,
159JSON.stringify(event, null, 2),
160this.logLevel,
161);
b7451aefRedMickey6 years ago162const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
163
164if (processedMessage.sendBack) {
984ca036RedMickey6 years ago165this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago166} else {
ebbd64f1RedMickey6 years ago167this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago168}
f872f4d5RedMickey6 years ago169}
170
b7451aefRedMickey6 years ago171private handleDebuggerTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago172this.logger.logWithCustomTag(
173this.PROXY_LOG_TAGS.DEBUGGER_REPLY,
174JSON.stringify(event, null, 2),
175this.logLevel,
176);
b7451aefRedMickey6 years ago177const processedMessage = this.CDPMessageHandler.processDebuggerCDPMessage(event);
178
179if (processedMessage.sendBack) {
ebbd64f1RedMickey6 years ago180this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago181} else {
984ca036RedMickey6 years ago182this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago183}
f872f4d5RedMickey6 years ago184}
185
b7451aefRedMickey6 years ago186private handleApplicationTargetReply(event: IProtocolError | IProtocolSuccess) {
34472878RedMickey5 years ago187this.logger.logWithCustomTag(
188this.PROXY_LOG_TAGS.APPLICATION_REPLY,
189JSON.stringify(event, null, 2),
190this.logLevel,
191);
b7451aefRedMickey6 years ago192const processedMessage = this.CDPMessageHandler.processApplicationCDPMessage(event);
193
194if (processedMessage.sendBack) {
984ca036RedMickey6 years ago195this.applicationTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago196} else {
ebbd64f1RedMickey6 years ago197this.debuggerTarget?.send(processedMessage.event);
b7451aefRedMickey6 years ago198}
f872f4d5RedMickey6 years ago199}
200
201private onDebuggerTargetError(err: Error) {
a324603aRedMickey6 years ago202this.logger.error("Error on debugger transport", err);
f872f4d5RedMickey6 years ago203}
204
205private onApplicationTargetError(err: Error) {
a324603aRedMickey6 years ago206this.logger.error("Error on application transport", err);
f872f4d5RedMickey6 years ago207}
208
ebbd64f1RedMickey6 years ago209private async onApplicationTargetClosed() {
210this.applicationTarget = null;
19df32dcRedMickey4 years ago211this.applicationTargetEventEmitter.fire({});
ebbd64f1RedMickey6 years ago212}
213
4c757eebRedMickey6 years ago214private async onDebuggerTargetClosed() {
984ca036RedMickey6 years ago215this.browserInspectUri = "";
34472878RedMickey5 years ago216this.CDPMessageHandler.processDebuggerCDPMessage({ method: "close" });
ebbd64f1RedMickey6 years ago217this.debuggerTarget = null;
af1b9666RedMickey6 years ago218}
f872f4d5RedMickey6 years ago219}