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/debugger/rnDebugSession.ts

228lines · modeblame

6e1bcd36RedMickey6 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 * as vscode from "vscode";
5import * as path from "path";
6import * as mkdirp from "mkdirp";
2c19da7fRedMickey6 years ago7import { logger } from "vscode-debugadapter";
6e1bcd36RedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
9import { ProjectVersionHelper } from "../common/projectVersionHelper";
10import { TelemetryHelper } from "../common/telemetryHelper";
11import { MultipleLifetimesAppWorker } from "./appWorker";
a6562589RedMickey6 years ago12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
2c19da7fRedMickey6 years ago13import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase";
6e1bcd36RedMickey6 years ago14import * as nls from "vscode-nls";
15const localize = nls.loadMessageBundle();
16
2c19da7fRedMickey6 years ago17export class RNDebugSession extends DebugSessionBase {
6e1bcd36RedMickey6 years ago18
4c757eebRedMickey6 years ago19private readonly terminateCommand: string;
20private readonly pwaNodeSessionName: string;
f872f4d5RedMickey6 years ago21
e23d1841RedMickey6 years ago22private appWorker: MultipleLifetimesAppWorker | null;
4c757eebRedMickey6 years ago23private nodeSession: vscode.DebugSession | null;
6e491635RedMickey6 years ago24private onDidStartDebugSessionHandler: vscode.Disposable;
25private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago26
2c19da7fRedMickey6 years ago27constructor(session: vscode.DebugSession) {
28super(session);
4c757eebRedMickey6 years ago29
30// constants definition
31this.terminateCommand = "terminate"; // the "terminate" command is sent from the client to the debug adapter in order to give the debuggee a chance for terminating itself
32this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
33
34// variables definition
e23d1841RedMickey6 years ago35this.appWorker = null;
36
6e491635RedMickey6 years ago37this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
4c757eebRedMickey6 years ago38this.handleStartDebugSession.bind(this)
39);
40
6e491635RedMickey6 years ago41this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
4c757eebRedMickey6 years ago42this.handleTerminateDebugSession.bind(this)
43);
6e1bcd36RedMickey6 years ago44}
45
984ca036RedMickey6 years ago46protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago47return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
48.then(() => {
49logger.log("Launching the application");
50logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
51
52this.appLauncher.launch(launchArgs)
53.then(() => {
54return this.appLauncher.getPackagerPort(launchArgs.cwd);
55})
56.then((packagerPort: number) => {
57launchArgs.port = launchArgs.port || packagerPort;
58this.attachRequest(response, launchArgs).then(() => {
59resolve();
60}).catch((e) => reject(e));
61})
62.catch((err) => {
2c19da7fRedMickey6 years ago63logger.error("An error occurred while launching the application. " + err.message || err);
6e1bcd36RedMickey6 years ago64reject(err);
65});
984ca036RedMickey6 years ago66}))
e23d1841RedMickey6 years ago67.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago68}
69
984ca036RedMickey6 years ago70protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago71let extProps = {
72platform: {
73value: attachArgs.platform,
74isPii: false,
75},
76};
77
78this.previousAttachArgs = attachArgs;
79return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
80.then(() => {
81logger.log("Attaching to the application");
82logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
83return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
84.then(versions => {
85extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
86if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
87extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
88}
89return TelemetryHelper.generate("attach", extProps, (generator) => {
a324603aRedMickey6 years ago90attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
984ca036RedMickey6 years ago91return this.appLauncher.getRnCdpProxy().stopServer()
92.then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel))
f872f4d5RedMickey6 years ago93.then(() => {
94logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
95
96const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
97// Create folder if not exist to avoid problems if
98// RN project root is not a ${workspaceFolder}
99mkdirp.sync(sourcesStoragePath);
100
101// If launch is invoked first time, appWorker is undefined, so create it here
102this.appWorker = new MultipleLifetimesAppWorker(
103attachArgs,
104sourcesStoragePath,
105this.projectRootPath,
a324603aRedMickey6 years ago106undefined
107);
7e74daf7Yuri Skorokhodov6 years ago108this.appLauncher.setAppWorker(this.appWorker);
f872f4d5RedMickey6 years ago109
110this.appWorker.on("connected", (port: number) => {
111logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
112
a6562589RedMickey6 years ago113this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
4c757eebRedMickey6 years ago114
115if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
116return;
117}
d9c9ddcbRedMickey6 years ago118
4c757eebRedMickey6 years ago119if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
120this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
121this.establishDebugSession(resolve);
122} else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
123if (this.nodeSession) {
124this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
125this.nodeSession.customRequest(this.terminateCommand);
126}
f872f4d5RedMickey6 years ago127}
128});
129return this.appWorker.start();
6e1bcd36RedMickey6 years ago130});
131})
132.catch((err) => {
133logger.error("An error occurred while attaching to the debugger. " + err.message || err);
134reject(err);
135});
136});
984ca036RedMickey6 years ago137}))
e23d1841RedMickey6 years ago138.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago139}
140
984ca036RedMickey6 years ago141protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago142// The client is about to disconnect so first we need to stop app worker
143if (this.appWorker) {
144this.appWorker.stop();
145}
146
6e491635RedMickey6 years ago147this.onDidStartDebugSessionHandler.dispose();
148this.onDidTerminateDebugSessionHandler.dispose();
149
6e1bcd36RedMickey6 years ago150super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago151}
152
b7451aefRedMickey6 years ago153protected establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
a6562589RedMickey6 years ago154const attachArguments = {
155type: "pwa-node",
156request: "attach",
157name: "Attach",
158continueOnAttach: true,
159port: this.appLauncher.getCdpProxyPort(),
160smartStep: false,
161// The unique identifier of the debug session. It is used to distinguish React Native extension's
162// debug sessions from other ones. So we can save and process only the extension's debug sessions
163// in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession".
164rnDebugSessionId: this.session.id,
165};
166
167vscode.debug.startDebugging(
168this.appLauncher.getWorkspaceFolder(),
169attachArguments,
170this.session
171)
172.then((childDebugSessionStarted: boolean) => {
173if (childDebugSessionStarted) {
174this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
175this.setConnectionAllowedIfPossible();
176if (resolve) {
177this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
178resolve();
4c757eebRedMickey6 years ago179}
a6562589RedMickey6 years ago180} else {
4c757eebRedMickey6 years ago181this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
182this.setConnectionAllowedIfPossible();
183this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago184throw new Error("Cannot start child debug session");
185}
186},
187err => {
188this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
189this.setConnectionAllowedIfPossible();
4c757eebRedMickey6 years ago190this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago191throw err;
192});
4c757eebRedMickey6 years ago193}
6e1bcd36RedMickey6 years ago194
4c757eebRedMickey6 years ago195private handleStartDebugSession(debugSession: vscode.DebugSession) {
196if (
197debugSession.configuration.rnDebugSessionId === this.session.id
198&& debugSession.type === this.pwaNodeSessionName
199) {
200this.nodeSession = debugSession;
201}
202}
203
204private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
205if (
206debugSession.configuration.rnDebugSessionId === this.session.id
2c19da7fRedMickey6 years ago207&& this.debugSessionStatus === DebugSessionStatus.ConnectionPending
4c757eebRedMickey6 years ago208&& debugSession.type === this.pwaNodeSessionName
209) {
2c19da7fRedMickey6 years ago210this.establishDebugSession();
4c757eebRedMickey6 years ago211}
212}
213
214private setConnectionAllowedIfPossible(): void {
215if (
216this.debugSessionStatus === DebugSessionStatus.ConnectionDone
217|| this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
218) {
219this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
220}
221}
222
223private resetFirstConnectionStatus(): void {
224if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
225this.debugSessionStatus = DebugSessionStatus.FirstConnection;
226}
227}
6e1bcd36RedMickey6 years ago228}