microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
341dba36c6e7ac21203bd58ef861c4803cd85fc0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

195lines · 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 { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler";
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";
16import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler";
17import { PlatformType } from "../../extension/launchArgs";
18import { IWDPHelper } from "./IWDPHelper";
19
20nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
21const localize = nls.loadMessageBundle();
22
23export class DirectDebugSession extends DebugSessionBase {
24
25 private debuggerEndpointHelper: DebuggerEndpointHelper;
26 private onDidTerminateDebugSessionHandler: vscode.Disposable;
27 private iOSWKDebugProxyHelper: IWDPHelper;
28
29 constructor(session: vscode.DebugSession) {
30 super(session);
31 this.debuggerEndpointHelper = new DebuggerEndpointHelper();
32 this.iOSWKDebugProxyHelper = new IWDPHelper();
33
34 this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
35 this.handleTerminateDebugSession.bind(this)
36 );
37 }
38
39 protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
40 let extProps = {
41 platform: {
42 value: launchArgs.platform,
43 isPii: false,
44 },
45 isDirect: {
46 value: true,
47 isPii: false,
48 },
49 };
50
51 return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
52 .then(() => {
53 logger.log("Launching the application");
54 logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
55
56 return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs));
57 })
58 .then(versions => {
59 extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(launchArgs, versions, extProps);
60
61 return TelemetryHelper.generate("launch", extProps, (generator) => {
62 return this.appLauncher.launch(launchArgs)
63 .then(() => {
64 if (launchArgs.enableDebug) {
65 launchArgs.port = launchArgs.port || this.appLauncher.getPackagerPort(launchArgs.cwd);
66 this.attachRequest(response, launchArgs).then(() => {
67 resolve();
68 }).catch((e) => reject(e));
69 } else {
70 this.sendResponse(response);
71 resolve();
72 }
73 });
74 });
75 })
76 .catch((err) => {
77 reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err));
78 })
79 )
80 .catch(err => this.showError(err, response));
81 }
82
83 protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
84 let extProps = {
85 platform: {
86 value: attachArgs.platform,
87 isPii: false,
88 },
89 isDirect: {
90 value: true,
91 isPii: false,
92 },
93 };
94
95 attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
96 attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;
97
98 this.previousAttachArgs = attachArgs;
99
100 return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
101 .then(() => {
102 logger.log("Attaching to the application");
103 logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`);
104 return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs));
105 })
106 .then(versions => {
107 extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(attachArgs, versions, extProps);
108
109 return TelemetryHelper.generate("attach", extProps, (generator) => {
110 attachArgs.port = attachArgs.platform === PlatformType.iOS ?
111 attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT :
112 attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
113 logger.log(`Connecting to ${attachArgs.port} port`);
114 return this.appLauncher.getRnCdpProxy().stopServer()
115 .then(() => this.appLauncher.getRnCdpProxy().initializeServer(
116 attachArgs.platform === PlatformType.iOS ?
117 new IOSDirectCDPMessageHandler() :
118 new HermesCDPMessageHandler(),
119 this.cdpProxyLogLevel)
120 )
121 .then(() => {
122 if (attachArgs.platform === PlatformType.iOS) {
123 return this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(attachArgs.port, attachArgs.webkitRangeMin, attachArgs.webkitRangeMax)
124 .then(() => this.iOSWKDebugProxyHelper.getSimulatorProxyPort(attachArgs))
125 .then((results) => {
126 attachArgs.port = results.targetPort;
127 });
128 } else {
129 return Promise.resolve();
130 }
131 })
132 .then(() => this.appLauncher.getPackager().start())
133 .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint(
134 `http://localhost:${attachArgs.port}`,
135 90,
136 this.cancellationTokenSource.token
137 ))
138 .then((browserInspectUri) => {
139 this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
140 this.establishDebugSession(attachArgs, resolve);
141 })
142 .catch(e => reject(e));
143 });
144 })
145 .catch((err) => {
146 reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err));
147 })
148 )
149 .catch(err => this.showError(err, response));
150 }
151
152 protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
153 this.iOSWKDebugProxyHelper.cleanUp();
154 this.onDidTerminateDebugSessionHandler.dispose();
155 super.disconnectRequest(response, args, request);
156 }
157
158 protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
159 const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
160 attachArgs,
161 this.appLauncher.getCdpProxyPort(),
162 this.session.id
163 );
164
165 vscode.debug.startDebugging(
166 this.appLauncher.getWorkspaceFolder(),
167 attachConfiguration,
168 {
169 parentSession: this.session,
170 consoleMode: vscode.DebugConsoleMode.MergeWithParent,
171 }
172 )
173 .then((childDebugSessionStarted: boolean) => {
174 if (childDebugSessionStarted) {
175 if (resolve) {
176 resolve();
177 }
178 } else {
179 throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"));
180 }
181 },
182 err => {
183 throw err;
184 });
185 }
186
187 private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
188 if (
189 debugSession.configuration.rnDebugSessionId === this.session.id
190 && debugSession.type === this.pwaNodeSessionName
191 ) {
192 vscode.commands.executeCommand(this.stopCommand, this.session);
193 }
194 }
195}
196