microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f521686613075eb4a9cebc89a01cbcf289ec258f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

172lines · 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 { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper";
12import * as nls from "vscode-nls";
13const localize = nls.loadMessageBundle();
14
15export class DirectDebugSession extends DebugSessionBase {
16
17 private debuggerEndpointHelper: DebuggerEndpointHelper;
18 private onDidTerminateDebugSessionHandler: vscode.Disposable;
19
20 constructor(session: vscode.DebugSession) {
21 super(session);
22 this.debuggerEndpointHelper = new DebuggerEndpointHelper();
23
24 this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
25 this.handleTerminateDebugSession.bind(this)
26 );
27 }
28
29 protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
30 let extProps = {
31 platform: {
32 value: launchArgs.platform,
33 isPii: false,
34 },
35 isDirect: {
36 value: true,
37 isPii: false,
38 },
39 };
40
41 return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
42 .then(() => {
43 logger.log("Launching the application");
44 logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
45 return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows")
46 .then(versions => {
47 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
48 if (launchArgs.platform === "windows") {
49 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
50 }
51 return TelemetryHelper.generate("launch", extProps, (generator) => {
52 return this.appLauncher.launch(launchArgs)
53 .then(() => {
54 return this.appLauncher.getPackagerPort(launchArgs.cwd);
55 })
56 .then((packagerPort: number) => {
57 launchArgs.port = launchArgs.port || packagerPort;
58 this.attachRequest(response, launchArgs).then(() => {
59 resolve();
60 }).catch((e) => reject(e));
61 }).catch((e) => reject(e));
62 })
63 .catch((err) => {
64 logger.error("An error occurred while launching the application. " + err.message || err);
65 reject(err);
66 });
67 });
68 }))
69 .catch(err => this.showError(err, response));
70 }
71
72 protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
73 let extProps = {
74 platform: {
75 value: attachArgs.platform,
76 isPii: false,
77 },
78 isDirect: {
79 value: true,
80 isPii: false,
81 },
82 };
83
84 this.previousAttachArgs = attachArgs;
85
86 return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
87 .then(() => {
88 logger.log("Attaching to the application");
89 logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
90 return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
91 .then(versions => {
92 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
93 if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
94 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
95 }
96 return TelemetryHelper.generate("attach", extProps, (generator) => {
97 attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
98 logger.log(`Connecting to ${attachArgs.port} port`);
99 return this.appLauncher.getRnCdpProxy().stopServer()
100 .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel))
101 .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint(
102 `http://localhost:${attachArgs.port}`,
103 90,
104 this.cancellationTokenSource.token
105 ))
106 .then((browserInspectUri) => {
107 this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
108 this.establishDebugSession(attachArgs, resolve);
109 })
110 .catch(e => reject(e));
111 })
112 .catch((err) => {
113 logger.error("An error occurred while attaching to the debugger. " + err.message || err);
114 reject(err);
115 });
116 });
117 }))
118 .catch(err => this.showError(err, response));
119 }
120
121 protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
122 this.onDidTerminateDebugSessionHandler.dispose();
123
124 super.disconnectRequest(response, args, request);
125 }
126
127 protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
128 const attachArguments = {
129 type: "pwa-node",
130 request: "attach",
131 name: "Attach",
132 continueOnAttach: true,
133 port: this.appLauncher.getCdpProxyPort(),
134 smartStep: false,
135 skipFiles: attachArgs.skipFiles || [],
136 // The unique identifier of the debug session. It is used to distinguish React Native extension's
137 // debug sessions from other ones. So we can save and process only the extension's debug sessions
138 // in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession".
139 rnDebugSessionId: this.session.id,
140 };
141
142 vscode.debug.startDebugging(
143 this.appLauncher.getWorkspaceFolder(),
144 attachArguments,
145 {
146 parentSession: this.session,
147 consoleMode: vscode.DebugConsoleMode.MergeWithParent,
148 }
149 )
150 .then((childDebugSessionStarted: boolean) => {
151 if (childDebugSessionStarted) {
152 if (resolve) {
153 resolve();
154 }
155 } else {
156 throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"));
157 }
158 },
159 err => {
160 throw err;
161 });
162 }
163
164 private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
165 if (
166 debugSession.configuration.rnDebugSessionId === this.session.id
167 && debugSession.type === this.pwaNodeSessionName
168 ) {
169 this.session.customRequest(this.disconnectCommand, {forcedStop: true});
170 }
171 }
172}
173