microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
142lines · 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 fs from "fs"; |
| 5 | import * as path from "path"; |
| 6 | import * as http from "http"; |
| 7 | |
| 8 | import {Telemetry} from "../common/telemetry"; |
| 9 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 10 | |
| 11 | // These typings do not reflect the typings as intended to be used |
| 12 | // but rather as they exist in truth, so we can reach into the internals |
| 13 | // and access what we need. |
| 14 | declare module VSCodeDebugAdapter { |
| 15 | class DebugSession { |
| 16 | public static run: Function; |
| 17 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 18 | public start(input: any, output: any): void; |
| 19 | public launchRequest(response: any, args: any): void; |
| 20 | } |
| 21 | class InitializedEvent { |
| 22 | constructor(); |
| 23 | } |
| 24 | class OutputEvent { |
| 25 | constructor(message: string, destination?: string); |
| 26 | } |
| 27 | class TerminatedEvent { |
| 28 | constructor(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | declare class SourceMaps { |
| 33 | public _sourceToGeneratedMaps: {}; |
| 34 | public _generatedToSourceMaps: {}; |
| 35 | public _allSourceMaps: {}; |
| 36 | } |
| 37 | |
| 38 | declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession { |
| 39 | public _sourceMaps: SourceMaps; |
| 40 | } |
| 41 | |
| 42 | interface ILaunchArgs { |
| 43 | platform: string; |
| 44 | target?: string; |
| 45 | internalDebuggerPort?: any; |
| 46 | args: string[]; |
| 47 | } |
| 48 | |
| 49 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 50 | |
| 51 | function bailOut(reason: string): void { |
| 52 | // Things have gone wrong in initialization: Report the error to telemetry and exit |
| 53 | TelemetryHelper.sendSimpleEvent(reason); |
| 54 | Telemetry.sendPendingData().finally(() => { |
| 55 | process.exit(1); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | // Enable telemetry |
| 60 | Telemetry.init("react-native-debug-adapter", version, true).then(() => { |
| 61 | let nodeDebugFolder: string; |
| 62 | let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; |
| 63 | |
| 64 | /* tslint:disable:no-var-requires */ |
| 65 | |
| 66 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 67 | // If it fails, we must not have been in a react native project |
| 68 | try { |
| 69 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; |
| 70 | vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); |
| 71 | } catch (e) { |
| 72 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 73 | return bailOut("cannotFindDebugAdapter"); |
| 74 | } |
| 75 | |
| 76 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 77 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; |
| 78 | vscodeDebugAdapterPackage.DebugSession.run = function() { }; |
| 79 | |
| 80 | let nodeDebug: { NodeDebugSession: typeof NodeDebugSession }; |
| 81 | |
| 82 | try { |
| 83 | nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug")); |
| 84 | } catch (e) { |
| 85 | // Unable to find nodeDebug, but we can make our own communication channel now |
| 86 | const debugSession = new vscodeDebugAdapterPackage.DebugSession(); |
| 87 | // Note: this will not work in the context of debugging the debug adapter and communicating over a socket, |
| 88 | // but in that case we have much better ways to investigate errors. |
| 89 | debugSession.start(process.stdin, process.stdout); |
| 90 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); |
| 91 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); |
| 92 | |
| 93 | return bailOut("cannotFindNodeDebugAdapter"); |
| 94 | } |
| 95 | |
| 96 | /* tslint:enable:no-var-requires */ |
| 97 | |
| 98 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; |
| 99 | |
| 100 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments |
| 101 | const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest; |
| 102 | nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) { |
| 103 | // Create a server waiting for messages to re-initialize the debug session; |
| 104 | const reinitializeServer = http.createServer((req, res) => { |
| 105 | res.statusCode = 404; |
| 106 | if (req.url === "/refreshBreakpoints") { |
| 107 | res.statusCode = 200; |
| 108 | if (this) { |
| 109 | const sourceMaps = this._sourceMaps; |
| 110 | if (sourceMaps) { |
| 111 | // Flush any cached source maps |
| 112 | sourceMaps._allSourceMaps = {}; |
| 113 | sourceMaps._generatedToSourceMaps = {}; |
| 114 | sourceMaps._sourceToGeneratedMaps = {}; |
| 115 | } |
| 116 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 117 | this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); |
| 118 | } |
| 119 | } |
| 120 | res.end(); |
| 121 | }); |
| 122 | const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090; |
| 123 | |
| 124 | reinitializeServer.listen(debugServerListeningPort); |
| 125 | reinitializeServer.on("error", (err: Error) => { |
| 126 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); |
| 127 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr")); |
| 128 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json")); |
| 129 | }); |
| 130 | |
| 131 | // We do not permit arbitrary args to be passed to our process |
| 132 | args.args = [ |
| 133 | args.platform, |
| 134 | debugServerListeningPort.toString(), |
| 135 | args.target || "simulator" |
| 136 | ]; |
| 137 | |
| 138 | originalNodeDebugSessionLaunchRequest.call(this, request, args); |
| 139 | }; |
| 140 | |
| 141 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |
| 142 | }); |