microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
198lines · modeblame
2c19da7fRedMickey6 years ago | 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"; | |
984ca036RedMickey6 years ago | 6 | import { logger } from "vscode-debugadapter"; |
2c19da7fRedMickey6 years ago | 7 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; | |
259c018fYuri Skorokhodov5 years ago | 9 | import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler"; |
2c19da7fRedMickey6 years ago | 10 | import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase"; |
1bdccb66RedMickey6 years ago | 11 | import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter"; |
984ca036RedMickey6 years ago | 12 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
5514e287RedMickey6 years ago | 13 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 14 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
2c19da7fRedMickey6 years ago | 15 | import * as nls from "vscode-nls"; |
259c018fYuri Skorokhodov5 years ago | 16 | import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler"; |
| 17 | import { PlatformType } from "../../extension/launchArgs"; | |
| 18 | import { IWDPHelper } from "./IWDPHelper"; | |
| 19 | | |
2d8af448Yuri Skorokhodov6 years ago | 20 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
2c19da7fRedMickey6 years ago | 21 | const localize = nls.loadMessageBundle(); |
| 22 | | |
| 23 | export class DirectDebugSession extends DebugSessionBase { | |
| 24 | | |
984ca036RedMickey6 years ago | 25 | private debuggerEndpointHelper: DebuggerEndpointHelper; |
ebbd64f1RedMickey6 years ago | 26 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
259c018fYuri Skorokhodov5 years ago | 27 | private iOSWKDebugProxyHelper: IWDPHelper; |
984ca036RedMickey6 years ago | 28 | |
2c19da7fRedMickey6 years ago | 29 | constructor(session: vscode.DebugSession) { |
| 30 | super(session); | |
984ca036RedMickey6 years ago | 31 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
259c018fYuri Skorokhodov5 years ago | 32 | this.iOSWKDebugProxyHelper = new IWDPHelper(); |
ebbd64f1RedMickey6 years ago | 33 | |
| 34 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( | |
| 35 | this.handleTerminateDebugSession.bind(this) | |
| 36 | ); | |
2c19da7fRedMickey6 years ago | 37 | } |
| 38 | | |
984ca036RedMickey6 years ago | 39 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 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"); | |
259c018fYuri Skorokhodov5 years ago | 54 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`); |
| 55 | return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === PlatformType.Windows); | |
5514e287RedMickey6 years ago | 56 | }) |
| 57 | .then(versions => { | |
| 58 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); | |
259c018fYuri Skorokhodov5 years ago | 59 | if (launchArgs.platform === PlatformType.Windows) { |
5514e287RedMickey6 years ago | 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(); | |
2c19da7fRedMickey6 years ago | 69 | }).catch((e) => reject(e)); |
5514e287RedMickey6 years ago | 70 | } else { |
| 71 | this.sendResponse(response); | |
| 72 | resolve(); | |
| 73 | } | |
2c19da7fRedMickey6 years ago | 74 | }); |
5514e287RedMickey6 years ago | 75 | }); |
| 76 | }) | |
| 77 | .catch((err) => { | |
| 78 | reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err)); | |
| 79 | }) | |
| 80 | ) | |
259c018fYuri Skorokhodov5 years ago | 81 | .catch(err => this.showError(err, response)); |
2c19da7fRedMickey6 years ago | 82 | } |
| 83 | | |
259c018fYuri Skorokhodov5 years ago | 84 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 85 | let extProps = { |
| 86 | platform: { | |
| 87 | value: attachArgs.platform, | |
| 88 | isPii: false, | |
| 89 | }, | |
| 90 | isDirect: { | |
| 91 | value: true, | |
| 92 | isPii: false, | |
| 93 | }, | |
| 94 | }; | |
| 95 | | |
259c018fYuri Skorokhodov5 years ago | 96 | attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223; |
| 97 | attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322; | |
| 98 | | |
2c19da7fRedMickey6 years ago | 99 | this.previousAttachArgs = attachArgs; |
| 100 | | |
| 101 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) | |
| 102 | .then(() => { | |
| 103 | logger.log("Attaching to the application"); | |
259c018fYuri Skorokhodov5 years ago | 104 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`); |
5514e287RedMickey6 years ago | 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) => { | |
259c018fYuri Skorokhodov5 years ago | 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); | |
5514e287RedMickey6 years ago | 116 | logger.log(`Connecting to ${attachArgs.port} port`); |
| 117 | return this.appLauncher.getRnCdpProxy().stopServer() | |
259c018fYuri Skorokhodov5 years ago | 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 | }) | |
9fc07913JiglioNero5 years ago | 135 | .then(() => this.appLauncher.getPackager().start()) |
5514e287RedMickey6 years ago | 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); | |
2c19da7fRedMickey6 years ago | 144 | }) |
5514e287RedMickey6 years ago | 145 | .catch(e => reject(e)); |
| 146 | }); | |
| 147 | }) | |
| 148 | .catch((err) => { | |
| 149 | reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err)); | |
| 150 | }) | |
| 151 | ) | |
259c018fYuri Skorokhodov5 years ago | 152 | .catch(err => this.showError(err, response)); |
2c19da7fRedMickey6 years ago | 153 | } |
| 154 | | |
984ca036RedMickey6 years ago | 155 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
259c018fYuri Skorokhodov5 years ago | 156 | this.iOSWKDebugProxyHelper.cleanUp(); |
ebbd64f1RedMickey6 years ago | 157 | this.onDidTerminateDebugSessionHandler.dispose(); |
2c19da7fRedMickey6 years ago | 158 | super.disconnectRequest(response, args, request); |
| 159 | } | |
| 160 | | |
5d47053fRedMickey6 years ago | 161 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
1bdccb66RedMickey6 years ago | 162 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 163 | attachArgs, | |
| 164 | this.appLauncher.getCdpProxyPort(), | |
| 165 | this.session.id | |
| 166 | ); | |
b7451aefRedMickey6 years ago | 167 | |
| 168 | vscode.debug.startDebugging( | |
| 169 | this.appLauncher.getWorkspaceFolder(), | |
1bdccb66RedMickey6 years ago | 170 | attachConfiguration, |
ebbd64f1RedMickey6 years ago | 171 | { |
| 172 | parentSession: this.session, | |
| 173 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
| 174 | } | |
b7451aefRedMickey6 years ago | 175 | ) |
259c018fYuri Skorokhodov5 years ago | 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")); | |
b7451aefRedMickey6 years ago | 183 | } |
259c018fYuri Skorokhodov5 years ago | 184 | }, |
| 185 | err => { | |
| 186 | throw err; | |
| 187 | }); | |
b7451aefRedMickey6 years ago | 188 | } |
ebbd64f1RedMickey6 years ago | 189 | |
| 190 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { | |
| 191 | if ( | |
| 192 | debugSession.configuration.rnDebugSessionId === this.session.id | |
| 193 | && debugSession.type === this.pwaNodeSessionName | |
| 194 | ) { | |
a2ddbba5RedMickey5 years ago | 195 | vscode.commands.executeCommand(this.stopCommand, this.session); |
ebbd64f1RedMickey6 years ago | 196 | } |
| 197 | } | |
2c19da7fRedMickey6 years ago | 198 | } |