microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

302lines · modeblame

2c19da7fRedMickey6 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 { ProjectVersionHelper } from "../../common/projectVersionHelper";
984ca036RedMickey6 years ago6import { logger } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago7import { TelemetryHelper } from "../../common/telemetryHelper";
8import { DebugProtocol } from "vscode-debugprotocol";
259c018fYuri Skorokhodov5 years ago9import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler";
2c19da7fRedMickey6 years ago10import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase";
1bdccb66RedMickey6 years ago11import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter";
984ca036RedMickey6 years ago12import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper";
5514e287RedMickey6 years ago13import { ErrorHelper } from "../../common/error/errorHelper";
14import { InternalErrorCode } from "../../common/error/internalErrorCode";
2c19da7fRedMickey6 years ago15import * as nls from "vscode-nls";
259c018fYuri Skorokhodov5 years ago16import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler";
17import { PlatformType } from "../../extension/launchArgs";
18import { IWDPHelper } from "./IWDPHelper";
6f9a0779JiglioNero5 years ago19import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler";
259c018fYuri Skorokhodov5 years ago20
34472878RedMickey5 years ago21nls.config({
22messageFormat: nls.MessageFormat.bundle,
23bundleFormat: nls.BundleFormat.standalone,
24})();
2c19da7fRedMickey6 years ago25const localize = nls.loadMessageBundle();
26
27export class DirectDebugSession extends DebugSessionBase {
984ca036RedMickey6 years ago28private debuggerEndpointHelper: DebuggerEndpointHelper;
ebbd64f1RedMickey6 years ago29private onDidTerminateDebugSessionHandler: vscode.Disposable;
259c018fYuri Skorokhodov5 years ago30private iOSWKDebugProxyHelper: IWDPHelper;
984ca036RedMickey6 years ago31
2c19da7fRedMickey6 years ago32constructor(session: vscode.DebugSession) {
33super(session);
984ca036RedMickey6 years ago34this.debuggerEndpointHelper = new DebuggerEndpointHelper();
259c018fYuri Skorokhodov5 years ago35this.iOSWKDebugProxyHelper = new IWDPHelper();
ebbd64f1RedMickey6 years ago36
37this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago38this.handleTerminateDebugSession.bind(this),
ebbd64f1RedMickey6 years ago39);
2c19da7fRedMickey6 years ago40}
41
34472878RedMickey5 years ago42protected async launchRequest(
43response: DebugProtocol.LaunchResponse,
44launchArgs: ILaunchRequestArgs,
45// eslint-disable-next-line @typescript-eslint/no-unused-vars
46request?: DebugProtocol.Request,
47): Promise<void> {
2c19da7fRedMickey6 years ago48let extProps = {
49platform: {
50value: launchArgs.platform,
51isPii: false,
52},
53isDirect: {
54value: true,
55isPii: false,
56},
57};
58
34472878RedMickey5 years ago59return new Promise<void>((resolve, reject) =>
60this.initializeSettings(launchArgs)
61.then(() => {
62logger.log("Launching the application");
63logger.verbose(
64`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`,
65);
341dba36Yuri Skorokhodov5 years ago66
34472878RedMickey5 years ago67return ProjectVersionHelper.getReactNativeVersions(
68launchArgs.cwd,
69ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(
70launchArgs,
71),
72);
73})
74.then(versions => {
75extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
76launchArgs,
77versions,
78extProps,
79);
341dba36Yuri Skorokhodov5 years ago80
34472878RedMickey5 years ago81// eslint-disable-next-line @typescript-eslint/no-unused-vars
82return TelemetryHelper.generate("launch", extProps, generator => {
83return this.appLauncher.launch(launchArgs).then(() => {
5514e287RedMickey6 years ago84if (launchArgs.enableDebug) {
34472878RedMickey5 years ago85this.attachRequest(response, launchArgs)
86.then(() => {
87resolve();
88})
89.catch(e => reject(e));
5514e287RedMickey6 years ago90} else {
91this.sendResponse(response);
92resolve();
93}
2c19da7fRedMickey6 years ago94});
34472878RedMickey5 years ago95});
96})
97.catch(err => {
98reject(
99ErrorHelper.getInternalError(
100InternalErrorCode.ApplicationLaunchFailed,
101err.message || err,
102),
103);
104}),
105).catch(err => this.showError(err, response));
2c19da7fRedMickey6 years ago106}
107
34472878RedMickey5 years ago108protected async attachRequest(
109response: DebugProtocol.AttachResponse,
110attachArgs: IAttachRequestArgs,
111// eslint-disable-next-line @typescript-eslint/no-unused-vars
112request?: DebugProtocol.Request,
113): Promise<void> {
2c19da7fRedMickey6 years ago114let extProps = {
115platform: {
116value: attachArgs.platform,
117isPii: false,
118},
119isDirect: {
120value: true,
121isPii: false,
122},
123};
124
259c018fYuri Skorokhodov5 years ago125attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
126attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;
127
2c19da7fRedMickey6 years ago128this.previousAttachArgs = attachArgs;
129
34472878RedMickey5 years ago130return new Promise<void>((resolve, reject) =>
131this.initializeSettings(attachArgs)
132.then(() => {
133logger.log("Attaching to the application");
134logger.verbose(
135`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`,
136);
137return ProjectVersionHelper.getReactNativeVersions(
138attachArgs.cwd,
139ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(
140attachArgs,
141),
142);
143})
144.then(versions => {
145extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
146attachArgs,
147versions,
148extProps,
149);
150
151// eslint-disable-next-line @typescript-eslint/no-unused-vars
152return TelemetryHelper.generate("attach", extProps, generator => {
6f9a0779JiglioNero5 years ago153const port = attachArgs.useHermesEngine
154? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd)
155: attachArgs.platform === PlatformType.iOS
156? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT
157: null;
158if (port === null) {
159return Promise.reject(
160ErrorHelper.getInternalError(
5c9b8eb8RedMickey5 years ago161InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
162attachArgs.platform,
6f9a0779JiglioNero5 years ago163),
164);
165}
166attachArgs.port = port;
34472878RedMickey5 years ago167logger.log(`Connecting to ${attachArgs.port} port`);
168return this.appLauncher
169.getRnCdpProxy()
170.stopServer()
6f9a0779JiglioNero5 years ago171.then(() => {
172const cdpProxy: BaseCDPMessageHandler | null = attachArgs.useHermesEngine
173? new HermesCDPMessageHandler()
174: attachArgs.platform === PlatformType.iOS
175? new IOSDirectCDPMessageHandler()
176: null;
177
178if (!cdpProxy) {
179return Promise.reject(
180ErrorHelper.getInternalError(
5c9b8eb8RedMickey5 years ago181InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
182attachArgs.platform,
6f9a0779JiglioNero5 years ago183),
184);
185}
186return this.appLauncher
34472878RedMickey5 years ago187.getRnCdpProxy()
6f9a0779JiglioNero5 years ago188.initializeServer(cdpProxy, this.cdpProxyLogLevel);
189})
34472878RedMickey5 years ago190.then(() => {
6f9a0779JiglioNero5 years ago191if (
192!attachArgs.useHermesEngine &&
193attachArgs.platform === PlatformType.iOS
194) {
34472878RedMickey5 years ago195return this.iOSWKDebugProxyHelper
196.startiOSWebkitDebugProxy(
197attachArgs.port,
198attachArgs.webkitRangeMin,
199attachArgs.webkitRangeMax,
200)
201.then(() =>
202this.iOSWKDebugProxyHelper.getSimulatorProxyPort(
203attachArgs,
204),
205)
206.then(results => {
207attachArgs.port = results.targetPort;
208});
209} else {
210return Promise.resolve();
211}
212})
213.then(() => this.appLauncher.getPackager().start())
214.then(() =>
215this.debuggerEndpointHelper.retryGetWSEndpoint(
216`http://localhost:${attachArgs.port}`,
21790,
218this.cancellationTokenSource.token,
219),
220)
221.then(browserInspectUri => {
222this.appLauncher
223.getRnCdpProxy()
224.setBrowserInspectUri(browserInspectUri);
225this.establishDebugSession(attachArgs, resolve);
226})
227.catch(e => reject(e));
228});
229})
230.catch(err => {
231reject(
232ErrorHelper.getInternalError(
233InternalErrorCode.CouldNotAttachToDebugger,
234err.message || err,
235),
236);
237}),
238).catch(err => this.showError(err, response));
2c19da7fRedMickey6 years ago239}
240
34472878RedMickey5 years ago241protected async disconnectRequest(
242response: DebugProtocol.DisconnectResponse,
243args: DebugProtocol.DisconnectArguments,
244request?: DebugProtocol.Request,
245): Promise<void> {
259c018fYuri Skorokhodov5 years ago246this.iOSWKDebugProxyHelper.cleanUp();
ebbd64f1RedMickey6 years ago247this.onDidTerminateDebugSessionHandler.dispose();
2c19da7fRedMickey6 years ago248super.disconnectRequest(response, args, request);
249}
250
34472878RedMickey5 years ago251protected establishDebugSession(
252attachArgs: IAttachRequestArgs,
253resolve?: (value?: void | PromiseLike<void> | undefined) => void,
254): void {
1bdccb66RedMickey6 years ago255const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
256attachArgs,
257this.appLauncher.getCdpProxyPort(),
34472878RedMickey5 years ago258this.session.id,
1bdccb66RedMickey6 years ago259);
b7451aefRedMickey6 years ago260
34472878RedMickey5 years ago261vscode.debug
262.startDebugging(this.appLauncher.getWorkspaceFolder(), attachConfiguration, {
ebbd64f1RedMickey6 years ago263parentSession: this.session,
264consoleMode: vscode.DebugConsoleMode.MergeWithParent,
34472878RedMickey5 years ago265})
266.then(
267(childDebugSessionStarted: boolean) => {
268if (childDebugSessionStarted) {
269if (resolve) {
270resolve();
271}
272} else {
273throw new Error(
274localize(
275"CouldNotStartChildDebugSession",
276"Couldn't start child debug session",
277),
278);
259c018fYuri Skorokhodov5 years ago279}
34472878RedMickey5 years ago280},
259c018fYuri Skorokhodov5 years ago281err => {
282throw err;
34472878RedMickey5 years ago283},
284);
b7451aefRedMickey6 years ago285}
ebbd64f1RedMickey6 years ago286
287private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
288if (
34472878RedMickey5 years ago289debugSession.configuration.rnDebugSessionId === this.session.id &&
290debugSession.type === this.pwaNodeSessionName
ebbd64f1RedMickey6 years ago291) {
a2ddbba5RedMickey5 years ago292vscode.commands.executeCommand(this.stopCommand, this.session);
ebbd64f1RedMickey6 years ago293}
294}
6f9a0779JiglioNero5 years ago295
296protected async initializeSettings(args: any): Promise<any> {
297await super.initializeSettings(args);
298if (args.useHermesEngine === undefined) {
299args.useHermesEngine = true;
300}
301}
2c19da7fRedMickey6 years ago302}