microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ce5e88eec6f7cd03403994764507be96c6af4624

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

170lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as vscode from "vscode";
5import { ProjectVersionHelper } from "../../common/projectVersionHelper";
6import { logger } from "vscode-debugadapter";
7import { TelemetryHelper } from "../../common/telemetryHelper";
8import { DebugProtocol } from "vscode-debugprotocol";
9import { DirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/directCDPMessageHandler";
10import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase";
11import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter";
12import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper";
13import { ErrorHelper } from "../../common/error/errorHelper";
14import { InternalErrorCode } from "../../common/error/internalErrorCode";
15import * as nls from "vscode-nls";
16nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
17const localize = nls.loadMessageBundle();
18
19export class DirectDebugSession extends DebugSessionBase {
20
21 private debuggerEndpointHelper: DebuggerEndpointHelper;
22 private onDidTerminateDebugSessionHandler: vscode.Disposable;
23
24 constructor(session: vscode.DebugSession) {
25 super(session);
26 this.debuggerEndpointHelper = new DebuggerEndpointHelper();
27
28 this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
29 this.handleTerminateDebugSession.bind(this)
30 );
31 }
32
33 protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
34 let extProps = {
35 platform: {
36 value: launchArgs.platform,
37 isPii: false,
38 },
39 isDirect: {
40 value: true,
41 isPii: false,
42 },
43 };
44
45 return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
46 .then(() => {
47 logger.log("Launching the application");
48 logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
49 return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows");
50 })
51 .then(versions => {
52 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
53 if (launchArgs.platform === "windows") {
54 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
55 }
56 return TelemetryHelper.generate("launch", extProps, (generator) => {
57 return this.appLauncher.launch(launchArgs)
58 .then(() => {
59 if (launchArgs.enableDebug) {
60 launchArgs.port = launchArgs.port || this.appLauncher.getPackagerPort(launchArgs.cwd);
61 this.attachRequest(response, launchArgs).then(() => {
62 resolve();
63 }).catch((e) => reject(e));
64 } else {
65 this.sendResponse(response);
66 resolve();
67 }
68 });
69 });
70 })
71 .catch((err) => {
72 reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err));
73 })
74 )
75 .catch(err => this.showError(err, response));
76 }
77
78 protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
79 let extProps = {
80 platform: {
81 value: attachArgs.platform,
82 isPii: false,
83 },
84 isDirect: {
85 value: true,
86 isPii: false,
87 },
88 };
89
90 this.previousAttachArgs = attachArgs;
91
92 return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
93 .then(() => {
94 logger.log("Attaching to the application");
95 logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
96 return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true);
97 })
98 .then(versions => {
99 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
100 if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
101 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
102 }
103 return TelemetryHelper.generate("attach", extProps, (generator) => {
104 attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
105 logger.log(`Connecting to ${attachArgs.port} port`);
106 return this.appLauncher.getRnCdpProxy().stopServer()
107 .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel))
108 .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint(
109 `http://localhost:${attachArgs.port}`,
110 90,
111 this.cancellationTokenSource.token
112 ))
113 .then((browserInspectUri) => {
114 this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
115 this.establishDebugSession(attachArgs, resolve);
116 })
117 .catch(e => reject(e));
118 });
119 })
120 .catch((err) => {
121 reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err));
122 })
123 )
124 .catch(err => this.showError(err, response));
125 }
126
127 protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
128 this.onDidTerminateDebugSessionHandler.dispose();
129
130 super.disconnectRequest(response, args, request);
131 }
132
133 protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
134 const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
135 attachArgs,
136 this.appLauncher.getCdpProxyPort(),
137 this.session.id
138 );
139
140 vscode.debug.startDebugging(
141 this.appLauncher.getWorkspaceFolder(),
142 attachConfiguration,
143 {
144 parentSession: this.session,
145 consoleMode: vscode.DebugConsoleMode.MergeWithParent,
146 }
147 )
148 .then((childDebugSessionStarted: boolean) => {
149 if (childDebugSessionStarted) {
150 if (resolve) {
151 resolve();
152 }
153 } else {
154 throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"));
155 }
156 },
157 err => {
158 throw err;
159 });
160 }
161
162 private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
163 if (
164 debugSession.configuration.rnDebugSessionId === this.session.id
165 && debugSession.type === this.pwaNodeSessionName
166 ) {
167 this.session.customRequest(this.disconnectCommand, {forcedStop: true});
168 }
169 }
170}
171