microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
acf08bc211b251f5aae90239291dbef21894989c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

136lines · 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 fs from "fs";
5import * as path from "path";
6import * as http from "http";
7
8import {Telemetry} from "../common/telemetry";
9import {TelemetryHelper} from "../common/telemetryHelper";
10import {ILaunchArgs} from "../common/launchArgs";
11
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.
15declare module VSCodeDebugAdapter {
16 class DebugSession {
17 public static run: Function;
18 public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void;
19 public start(input: any, output: any): void;
20 public launchRequest(response: any, args: any): void;
21 }
22 class InitializedEvent {
23 constructor();
24 }
25 class OutputEvent {
26 constructor(message: string, destination?: string);
27 }
28 class TerminatedEvent {
29 constructor();
30 }
31}
32
33declare class SourceMaps {
34 public _sourceToGeneratedMaps: {};
35 public _generatedToSourceMaps: {};
36 public _allSourceMaps: {};
37}
38
39declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
40 public _sourceMaps: SourceMaps;
41}
42
43let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
44
45function 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
53// Enable telemetry
54Telemetry.init("react-native-debug-adapter", version, true).then(() => {
55 let nodeDebugFolder: string;
56 let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
57
58 /* tslint:disable:no-var-requires */
59
60 // nodeDebugLocation.json is dynamically generated on extension activation.
61 // If it fails, we must not have been in a react native project
62 try {
63 nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
64 vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
65 } catch (e) {
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");
68 }
69
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());
86
87 return bailOut("cannotFindNodeDebugAdapter");
88 }
89
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 = {};
109 }
110 // Send an "initialized" event to trigger breakpoints to be re-sent
111 this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
112 }
113 }
114 res.end();
115 });
116 const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
117
118 reinitializeServer.listen(debugServerListeningPort);
119 reinitializeServer.on("error", (err: Error) => {
120 TelemetryHelper.sendSimpleEvent("reinitializeServerError");
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);
136});