microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
65bb0c85df6a89b406b370fd653d87f0a7043304

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
4import * as path from "path";
5import * 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.
10declare module VSCodeDebugAdapter {
11 class DebugSession {
12 public static run: Function;
13 }
14 class InitializedEvent {
15 // Nothing relevant
16 }
17}
18
19declare class SourceMaps {
20 public _sourceToGeneratedMaps: {};
21 public _generatedToSourceMaps: {};
22 public _allSourceMaps: {};
23}
24
25declare 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
33const nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
34
35const 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
38const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
39vscodeDebugAdapterPackage.DebugSession.run = function () {};
40
41const nodeDebug: {NodeDebugSession: typeof NodeDebugSession} = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
42
43vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
44
45// Intercept the "start" instance method of NodeDebugSession to keep a reference to the instance itself
46let nodeDebugSession: NodeDebugSession;
47const originalNodeDebugSessionStart = nodeDebug.NodeDebugSession.prototype.start;
48nodeDebug.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;
54const 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:
70const ourDebugPortIndexSentinel = process.argv.indexOf("--reactNativeDebuggerPort");
71const debugServerListeningPort = parseInt(process.argv[ourDebugPortIndexSentinel + 1], 10) || 8080;
72
73reinitializeServer.listen(debugServerListeningPort);
74
75// Launch the modified debug adapter
76vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);