microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e2bb5edaf243abd50ab0b1dfb12a6f8c5fccceab

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

183lines · 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 {RemoteExtension} from "../common/remoteExtension";
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 public disconnectRequest(response: any, args: any): void;
22 }
23 class InitializedEvent {
24 constructor();
25 }
26 class OutputEvent {
27 constructor(message: string, destination?: string);
28 }
29 class TerminatedEvent {
30 constructor();
31 }
32}
33
34declare class SourceMaps {
35 public _sourceToGeneratedMaps: {};
36 public _generatedToSourceMaps: {};
37 public _allSourceMaps: {};
38}
39
40declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
41 public _sourceMaps: SourceMaps;
42}
43
44interface ILaunchArgs {
45 platform: string;
46 target?: string;
47 internalDebuggerPort?: any;
48 args: string[];
49 logCatArguments: any;
50 program: string;
51}
52
53let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
54
55function bailOut(reason: string): void {
56 // Things have gone wrong in initialization: Report the error to telemetry and exit
57 TelemetryHelper.sendSimpleEvent(reason);
58 Telemetry.sendPendingData().finally(() => {
59 process.exit(1);
60 });
61}
62
63function parseLogCatArguments(userProvidedLogCatArguments: any) {
64 return Array.isArray(userProvidedLogCatArguments)
65 ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments
66 : userProvidedLogCatArguments; // If not, we leave it as-is
67}
68
69function isNullOrUndefined(value: any): boolean {
70 return typeof value === "undefined" || value === null;
71}
72
73let projectRootPath: string = null;
74
75// Enable telemetry
76Telemetry.init("react-native-debug-adapter", version, true).then(() => {
77 let nodeDebugFolder: string;
78 let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
79
80 /* tslint:disable:no-var-requires */
81
82 // nodeDebugLocation.json is dynamically generated on extension activation.
83 // If it fails, we must not have been in a react native project
84 try {
85 nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
86 vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
87 } catch (e) {
88 // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
89 return bailOut("cannotFindDebugAdapter");
90 }
91
92 // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
93 const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
94 vscodeDebugAdapterPackage.DebugSession.run = function() { };
95
96 let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
97
98 try {
99 nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
100 } catch (e) {
101 // Unable to find nodeDebug, but we can make our own communication channel now
102 const debugSession = new vscodeDebugAdapterPackage.DebugSession();
103 // Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
104 // but in that case we have much better ways to investigate errors.
105 debugSession.start(process.stdin, process.stdout);
106 debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
107 debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
108
109 return bailOut("cannotFindNodeDebugAdapter");
110 }
111
112 /* tslint:enable:no-var-requires */
113
114 vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
115
116 // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
117 const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
118 nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
119 projectRootPath = path.resolve(args.program, "../..");
120
121 // Create a server waiting for messages to re-initialize the debug session;
122 const reinitializeServer = http.createServer((req, res) => {
123 res.statusCode = 404;
124 if (req.url === "/refreshBreakpoints") {
125 res.statusCode = 200;
126 if (this) {
127 const sourceMaps = this._sourceMaps;
128 if (sourceMaps) {
129 // Flush any cached source maps
130 sourceMaps._allSourceMaps = {};
131 sourceMaps._generatedToSourceMaps = {};
132 sourceMaps._sourceToGeneratedMaps = {};
133 }
134 // Send an "initialized" event to trigger breakpoints to be re-sent
135 this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
136 }
137 }
138 res.end();
139 });
140 const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
141
142 reinitializeServer.listen(debugServerListeningPort);
143 reinitializeServer.on("error", (err: Error) => {
144 TelemetryHelper.sendSimpleEvent("reinitializeServerError");
145 this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
146 this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
147 });
148
149 // We do not permit arbitrary args to be passed to our process
150 args.args = [
151 args.platform,
152 debugServerListeningPort.toString(),
153 args.target || "simulator",
154 ];
155
156 if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise)
157 args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]);
158 }
159
160 originalNodeDebugSessionLaunchRequest.call(this, request, args);
161 };
162
163 // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
164 const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest;
165 function customDisconnectRequest(response: any, args: any): void {
166 try {
167 // First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
168 const remoteExtension = new RemoteExtension(projectRootPath);
169 remoteExtension.stopMonitoringLocat()
170 .finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args))
171 .done(() => {}, reason => // We just print a warning if something fails
172 process.stderr.write(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`));
173 } catch (exception) {
174 // This is a "nice to have" feature, so we just fire the message and forget. We don't event handle
175 // errors in the response promise
176 process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`);
177 originalNodeDebugSessionDisconnectRequest.call(this, response, args);
178 }
179 }
180 nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest;
181
182 vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
183});
184