microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
710f8655e5c2a63fcc02d594248baaa4ad9e7e6f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

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