microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b031edc774df5bdef810349bdd6d2c40c7acfadd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

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