microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8953be57c8d06daf5c9528afecd340c9f3ccfa29

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

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