microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a503690b21087a9ed082683b27b60d42ca536723

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

146lines · 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 {ServerDefaultParams} from "../common/extensionMessaging";
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
43interface ILaunchArgs {
44 platform: string;
45 target?: string;
46 internalDebuggerPort?: any;
47 internalExtensionPort?: any;
48 args: string[];
49}
50
51let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
52
53function bailOut(reason: string): void {
54 // Things have gone wrong in initialization: Report the error to telemetry and exit
55 TelemetryHelper.sendSimpleEvent(reason);
56 Telemetry.sendPendingData().finally(() => {
57 process.exit(1);
58 });
59}
60
61// Enable telemetry
62Telemetry.init("react-native-debug-adapter", version, true).then(() => {
63 let nodeDebugFolder: string;
64 let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
65
66 /* tslint:disable:no-var-requires */
67
68 // nodeDebugLocation.json is dynamically generated on extension activation.
69 // If it fails, we must not have been in a react native project
70 try {
71 nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
72 vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
73 } catch (e) {
74 // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
75 return bailOut("cannotFindDebugAdapter");
76 }
77
78 // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
79 const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
80 vscodeDebugAdapterPackage.DebugSession.run = function() { };
81
82 let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
83
84 try {
85 nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
86 } catch (e) {
87 // Unable to find nodeDebug, but we can make our own communication channel now
88 const debugSession = new vscodeDebugAdapterPackage.DebugSession();
89 // Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
90 // but in that case we have much better ways to investigate errors.
91 debugSession.start(process.stdin, process.stdout);
92 debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
93 debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
94
95 return bailOut("cannotFindNodeDebugAdapter");
96 }
97
98 /* tslint:enable:no-var-requires */
99
100 vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
101
102 // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
103 const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
104 nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
105 // Create a server waiting for messages to re-initialize the debug session;
106 const reinitializeServer = http.createServer((req, res) => {
107 res.statusCode = 404;
108 if (req.url === "/refreshBreakpoints") {
109 res.statusCode = 200;
110 if (this) {
111 const sourceMaps = this._sourceMaps;
112 if (sourceMaps) {
113 // Flush any cached source maps
114 sourceMaps._allSourceMaps = {};
115 sourceMaps._generatedToSourceMaps = {};
116 sourceMaps._sourceToGeneratedMaps = {};
117 }
118 // Send an "initialized" event to trigger breakpoints to be re-sent
119 this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
120 }
121 }
122 res.end();
123 });
124 const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
125 const internalExtensionPort = parseInt(args.internalExtensionPort, 10) || ServerDefaultParams.PORT;
126
127 reinitializeServer.listen(debugServerListeningPort);
128 reinitializeServer.on("error", (err: Error) => {
129 TelemetryHelper.sendSimpleEvent("reinitializeServerError");
130 this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
131 this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
132 });
133
134 // We do not permit arbitrary args to be passed to our process
135 args.args = [
136 args.platform,
137 debugServerListeningPort.toString(),
138 internalExtensionPort.toString(),
139 args.target || "simulator"
140 ];
141
142 originalNodeDebugSessionLaunchRequest.call(this, request, args);
143 };
144
145 vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
146});