microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
76lines · 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 path from "path"; |
| 5 | import * as http from "http"; |
| 6 | |
| 7 | // These typings do not reflect the typings as intended to be used |
| 8 | // but rather as they exist in truth, so we can reach into the internals |
| 9 | // and access what we need. |
| 10 | declare module VSCodeDebugAdapter { |
| 11 | class DebugSession { |
| 12 | public static run: Function; |
| 13 | } |
| 14 | class InitializedEvent { |
| 15 | // Nothing relevant |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | declare class SourceMaps { |
| 20 | public _sourceToGeneratedMaps: {}; |
| 21 | public _generatedToSourceMaps: {}; |
| 22 | public _allSourceMaps: {}; |
| 23 | } |
| 24 | |
| 25 | declare class NodeDebugSession { |
| 26 | public _sourceMaps: SourceMaps; |
| 27 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 28 | public start(): any; |
| 29 | } |
| 30 | |
| 31 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 32 | // If it fails, we must not have been in a react native project |
| 33 | const nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; |
| 34 | |
| 35 | const vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); |
| 36 | |
| 37 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 38 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; |
| 39 | vscodeDebugAdapterPackage.DebugSession.run = function () {}; |
| 40 | |
| 41 | const nodeDebug: {NodeDebugSession: typeof NodeDebugSession} = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug")); |
| 42 | |
| 43 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; |
| 44 | |
| 45 | // Intercept the "start" instance method of NodeDebugSession to keep a reference to the instance itself |
| 46 | let nodeDebugSession: NodeDebugSession; |
| 47 | const originalNodeDebugSessionStart = nodeDebug.NodeDebugSession.prototype.start; |
| 48 | nodeDebug.NodeDebugSession.prototype.start = function () { |
| 49 | nodeDebugSession = this; |
| 50 | return originalNodeDebugSessionStart.apply(this, arguments); |
| 51 | } |
| 52 | |
| 53 | // Create a server waiting for messages to re-initialize the debug session; |
| 54 | const reinitializeServer = http.createServer((request, response) => { |
| 55 | if (nodeDebugSession) { |
| 56 | const sourceMaps = nodeDebugSession._sourceMaps; |
| 57 | if (sourceMaps) { |
| 58 | // Flush any cached source maps |
| 59 | sourceMaps._allSourceMaps = {}; |
| 60 | sourceMaps._generatedToSourceMaps = {}; |
| 61 | sourceMaps._sourceToGeneratedMaps = {}; |
| 62 | } |
| 63 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 64 | nodeDebugSession.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); |
| 65 | } |
| 66 | response.end(); |
| 67 | }); |
| 68 | |
| 69 | // Determine which port to listen on: |
| 70 | const ourDebugPortIndexSentinel = process.argv.indexOf("--reactNativeDebuggerPort"); |
| 71 | const debugServerListeningPort = parseInt(process.argv[ourDebugPortIndexSentinel + 1], 10) || 8080; |
| 72 | |
| 73 | reinitializeServer.listen(debugServerListeningPort); |
| 74 | |
| 75 | // Launch the modified debug adapter |
| 76 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |