microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
198lines · 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 | |
| 4 | import * as vscode from "vscode"; |
| 5 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; |
| 6 | import { logger } from "vscode-debugadapter"; |
| 7 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 9 | import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler"; |
| 10 | import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase"; |
| 11 | import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter"; |
| 12 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
| 13 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 14 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 15 | import * as nls from "vscode-nls"; |
| 16 | import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler"; |
| 17 | import { PlatformType } from "../../extension/launchArgs"; |
| 18 | import { IWDPHelper } from "./IWDPHelper"; |
| 19 | |
| 20 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 21 | const localize = nls.loadMessageBundle(); |
| 22 | |
| 23 | export 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 | return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === PlatformType.Windows); |
| 56 | }) |
| 57 | .then(versions => { |
| 58 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 59 | if (launchArgs.platform === PlatformType.Windows) { |
| 60 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 61 | } |
| 62 | return TelemetryHelper.generate("launch", extProps, (generator) => { |
| 63 | return this.appLauncher.launch(launchArgs) |
| 64 | .then(() => { |
| 65 | if (launchArgs.enableDebug) { |
| 66 | launchArgs.port = launchArgs.port || this.appLauncher.getPackagerPort(launchArgs.cwd); |
| 67 | this.attachRequest(response, launchArgs).then(() => { |
| 68 | resolve(); |
| 69 | }).catch((e) => reject(e)); |
| 70 | } else { |
| 71 | this.sendResponse(response); |
| 72 | resolve(); |
| 73 | } |
| 74 | }); |
| 75 | }); |
| 76 | }) |
| 77 | .catch((err) => { |
| 78 | reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err)); |
| 79 | }) |
| 80 | ) |
| 81 | .catch(err => this.showError(err, response)); |
| 82 | } |
| 83 | |
| 84 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
| 85 | let extProps = { |
| 86 | platform: { |
| 87 | value: attachArgs.platform, |
| 88 | isPii: false, |
| 89 | }, |
| 90 | isDirect: { |
| 91 | value: true, |
| 92 | isPii: false, |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223; |
| 97 | attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322; |
| 98 | |
| 99 | this.previousAttachArgs = attachArgs; |
| 100 | |
| 101 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) |
| 102 | .then(() => { |
| 103 | logger.log("Attaching to the application"); |
| 104 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`); |
| 105 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true); |
| 106 | }) |
| 107 | .then(versions => { |
| 108 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 109 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { |
| 110 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 111 | } |
| 112 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 113 | attachArgs.port = attachArgs.platform === PlatformType.iOS ? |
| 114 | attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT : |
| 115 | attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); |
| 116 | logger.log(`Connecting to ${attachArgs.port} port`); |
| 117 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 118 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer( |
| 119 | attachArgs.platform === PlatformType.iOS ? |
| 120 | new IOSDirectCDPMessageHandler() : |
| 121 | new HermesCDPMessageHandler(), |
| 122 | this.cdpProxyLogLevel) |
| 123 | ) |
| 124 | .then(() => { |
| 125 | if (attachArgs.platform === PlatformType.iOS) { |
| 126 | return this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(attachArgs.port, attachArgs.webkitRangeMin, attachArgs.webkitRangeMax) |
| 127 | .then(() => this.iOSWKDebugProxyHelper.getSimulatorProxyPort(attachArgs)) |
| 128 | .then((results) => { |
| 129 | attachArgs.port = results.targetPort; |
| 130 | }); |
| 131 | } else { |
| 132 | return Promise.resolve(); |
| 133 | } |
| 134 | }) |
| 135 | .then(() => this.appLauncher.getPackager().start()) |
| 136 | .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint( |
| 137 | `http://localhost:${attachArgs.port}`, |
| 138 | 90, |
| 139 | this.cancellationTokenSource.token |
| 140 | )) |
| 141 | .then((browserInspectUri) => { |
| 142 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); |
| 143 | this.establishDebugSession(attachArgs, resolve); |
| 144 | }) |
| 145 | .catch(e => reject(e)); |
| 146 | }); |
| 147 | }) |
| 148 | .catch((err) => { |
| 149 | reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err)); |
| 150 | }) |
| 151 | ) |
| 152 | .catch(err => this.showError(err, response)); |
| 153 | } |
| 154 | |
| 155 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
| 156 | this.iOSWKDebugProxyHelper.cleanUp(); |
| 157 | this.onDidTerminateDebugSessionHandler.dispose(); |
| 158 | super.disconnectRequest(response, args, request); |
| 159 | } |
| 160 | |
| 161 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
| 162 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 163 | attachArgs, |
| 164 | this.appLauncher.getCdpProxyPort(), |
| 165 | this.session.id |
| 166 | ); |
| 167 | |
| 168 | vscode.debug.startDebugging( |
| 169 | this.appLauncher.getWorkspaceFolder(), |
| 170 | attachConfiguration, |
| 171 | { |
| 172 | parentSession: this.session, |
| 173 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, |
| 174 | } |
| 175 | ) |
| 176 | .then((childDebugSessionStarted: boolean) => { |
| 177 | if (childDebugSessionStarted) { |
| 178 | if (resolve) { |
| 179 | resolve(); |
| 180 | } |
| 181 | } else { |
| 182 | throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session")); |
| 183 | } |
| 184 | }, |
| 185 | err => { |
| 186 | throw err; |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { |
| 191 | if ( |
| 192 | debugSession.configuration.rnDebugSessionId === this.session.id |
| 193 | && debugSession.type === this.pwaNodeSessionName |
| 194 | ) { |
| 195 | vscode.commands.executeCommand(this.stopCommand, this.session); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |