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