microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
142lines · modeblame
65bb0c85Jimmy Thomson10 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 | | |
d976d077Meena Kunnathur Balakrishnan10 years ago | 4 | import * as fs from "fs"; |
65bb0c85Jimmy Thomson10 years ago | 5 | import * as path from "path"; |
| 6 | import * as http from "http"; | |
| 7 | | |
d976d077Meena Kunnathur Balakrishnan10 years ago | 8 | import {Telemetry} from "../common/telemetry"; |
dd442738Jimmy Thomson10 years ago | 9 | import {TelemetryHelper} from "../common/telemetryHelper"; |
d976d077Meena Kunnathur Balakrishnan10 years ago | 10 | |
65bb0c85Jimmy Thomson10 years ago | 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; | |
5547a16fJimmy Thomson10 years ago | 17 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 18 | public start(input: any, output: any): void; | |
| 19 | public launchRequest(response: any, args: any): void; | |
65bb0c85Jimmy Thomson10 years ago | 20 | } |
| 21 | class InitializedEvent { | |
5547a16fJimmy Thomson10 years ago | 22 | constructor(); |
| 23 | } | |
| 24 | class OutputEvent { | |
| 25 | constructor(message: string, destination?: string); | |
| 26 | } | |
| 27 | class TerminatedEvent { | |
| 28 | constructor(); | |
65bb0c85Jimmy Thomson10 years ago | 29 | } |
| 30 | } | |
| 31 | | |
| 32 | declare class SourceMaps { | |
| 33 | public _sourceToGeneratedMaps: {}; | |
| 34 | public _generatedToSourceMaps: {}; | |
| 35 | public _allSourceMaps: {}; | |
| 36 | } | |
| 37 | | |
5547a16fJimmy Thomson10 years ago | 38 | declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession { |
65bb0c85Jimmy Thomson10 years ago | 39 | public _sourceMaps: SourceMaps; |
| 40 | } | |
| 41 | | |
5e54f6f2Jimmy Thomson10 years ago | 42 | interface ILaunchArgs { |
| 43 | platform: string; | |
| 44 | target?: string; | |
| 45 | internalDebuggerPort?: any; | |
| 46 | args: string[]; | |
| 47 | } | |
| 48 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 49 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
5547a16fJimmy Thomson10 years ago | 50 | |
642490c1Jimmy Thomson10 years ago | 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 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 59 | // Enable telemetry |
48efc4d6Meena Kunnathur Balakrishnan10 years ago | 60 | Telemetry.init("react-native-debug-adapter", version, true).then(() => { |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 61 | let nodeDebugFolder: string; |
| 62 | let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; | |
b8ef4af9Jimmy Thomson10 years ago | 63 | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 64 | /* tslint:disable:no-var-requires */ |
65bb0c85Jimmy Thomson10 years ago | 65 | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 66 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 67 | // If it fails, we must not have been in a react native project | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 68 | try { |
| 69 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; | |
| 70 | vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); | |
| 71 | } catch (e) { | |
642490c1Jimmy Thomson10 years ago | 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"); | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 74 | } |
65bb0c85Jimmy Thomson10 years ago | 75 | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 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()); | |
dd442738Jimmy Thomson10 years ago | 92 | |
642490c1Jimmy Thomson10 years ago | 93 | return bailOut("cannotFindNodeDebugAdapter"); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 94 | } |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 95 | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 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 = {}; | |
48efc4d6Meena Kunnathur Balakrishnan10 years ago | 115 | } |
4881129dMeena Kunnathur Balakrishnan10 years ago | 116 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 117 | this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); | |
| 118 | } | |
| 119 | } | |
| 120 | res.end(); | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 121 | }); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 122 | const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090; |
| 123 | | |
| 124 | reinitializeServer.listen(debugServerListeningPort); | |
| 125 | reinitializeServer.on("error", (err: Error) => { | |
642490c1Jimmy Thomson10 years ago | 126 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 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); | |
d976d077Meena Kunnathur Balakrishnan10 years ago | 142 | }); |