microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
914a7c909092cd9effc56a839d104f0932af80e9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

142lines · modeblame

65bb0c85Jimmy Thomson10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
d976d077Meena Kunnathur Balakrishnan10 years ago4import * as fs from "fs";
65bb0c85Jimmy Thomson10 years ago5import * as path from "path";
6import * as http from "http";
7
d976d077Meena Kunnathur Balakrishnan10 years ago8import {Telemetry} from "../common/telemetry";
dd442738Jimmy Thomson10 years ago9import {TelemetryHelper} from "../common/telemetryHelper";
d976d077Meena Kunnathur Balakrishnan10 years ago10
65bb0c85Jimmy Thomson10 years ago11// 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 {
15class DebugSession {
16public static run: Function;
5547a16fJimmy Thomson10 years ago17public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void;
18public start(input: any, output: any): void;
19public launchRequest(response: any, args: any): void;
65bb0c85Jimmy Thomson10 years ago20}
21class InitializedEvent {
5547a16fJimmy Thomson10 years ago22constructor();
23}
24class OutputEvent {
25constructor(message: string, destination?: string);
26}
27class TerminatedEvent {
28constructor();
65bb0c85Jimmy Thomson10 years ago29}
30}
31
32declare class SourceMaps {
33public _sourceToGeneratedMaps: {};
34public _generatedToSourceMaps: {};
35public _allSourceMaps: {};
36}
37
5547a16fJimmy Thomson10 years ago38declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
65bb0c85Jimmy Thomson10 years ago39public _sourceMaps: SourceMaps;
40}
41
5e54f6f2Jimmy Thomson10 years ago42interface ILaunchArgs {
43platform: string;
44target?: string;
45internalDebuggerPort?: any;
46args: string[];
47}
48
2d3a052eMeena Kunnathur Balakrishnan10 years ago49let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
5547a16fJimmy Thomson10 years ago50
642490c1Jimmy Thomson10 years ago51function bailOut(reason: string): void {
52// Things have gone wrong in initialization: Report the error to telemetry and exit
53TelemetryHelper.sendSimpleEvent(reason);
54Telemetry.sendPendingData().finally(() => {
55process.exit(1);
56});
57}
58
2d3a052eMeena Kunnathur Balakrishnan10 years ago59// Enable telemetry
48efc4d6Meena Kunnathur Balakrishnan10 years ago60Telemetry.init("react-native-debug-adapter", version, true).then(() => {
2d3a052eMeena Kunnathur Balakrishnan10 years ago61let nodeDebugFolder: string;
62let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
b8ef4af9Jimmy Thomson10 years ago63
2d3a052eMeena Kunnathur Balakrishnan10 years ago64/* tslint:disable:no-var-requires */
65bb0c85Jimmy Thomson10 years ago65
2d3a052eMeena Kunnathur Balakrishnan10 years ago66// nodeDebugLocation.json is dynamically generated on extension activation.
67// If it fails, we must not have been in a react native project
4881129dMeena Kunnathur Balakrishnan10 years ago68try {
69nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
70vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
71} catch (e) {
642490c1Jimmy Thomson10 years ago72// Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
73return bailOut("cannotFindDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago74}
65bb0c85Jimmy Thomson10 years ago75
4881129dMeena Kunnathur Balakrishnan10 years ago76// Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
77const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
78vscodeDebugAdapterPackage.DebugSession.run = function() { };
79
80let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
81
82try {
83nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
84} catch (e) {
85// Unable to find nodeDebug, but we can make our own communication channel now
86const debugSession = new vscodeDebugAdapterPackage.DebugSession();
87// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
88// but in that case we have much better ways to investigate errors.
89debugSession.start(process.stdin, process.stdout);
90debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
91debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
dd442738Jimmy Thomson10 years ago92
642490c1Jimmy Thomson10 years ago93return bailOut("cannotFindNodeDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago94}
2d3a052eMeena Kunnathur Balakrishnan10 years ago95
4881129dMeena Kunnathur Balakrishnan10 years ago96/* tslint:enable:no-var-requires */
97
98vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
99
100// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
101const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
102nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
103// Create a server waiting for messages to re-initialize the debug session;
104const reinitializeServer = http.createServer((req, res) => {
105res.statusCode = 404;
106if (req.url === "/refreshBreakpoints") {
107res.statusCode = 200;
108if (this) {
109const sourceMaps = this._sourceMaps;
110if (sourceMaps) {
111// Flush any cached source maps
112sourceMaps._allSourceMaps = {};
113sourceMaps._generatedToSourceMaps = {};
114sourceMaps._sourceToGeneratedMaps = {};
48efc4d6Meena Kunnathur Balakrishnan10 years ago115}
4881129dMeena Kunnathur Balakrishnan10 years ago116// Send an "initialized" event to trigger breakpoints to be re-sent
117this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
118}
119}
120res.end();
2d3a052eMeena Kunnathur Balakrishnan10 years ago121});
4881129dMeena Kunnathur Balakrishnan10 years ago122const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
123
124reinitializeServer.listen(debugServerListeningPort);
125reinitializeServer.on("error", (err: Error) => {
642490c1Jimmy Thomson10 years ago126TelemetryHelper.sendSimpleEvent("reinitializeServerError");
4881129dMeena Kunnathur Balakrishnan10 years ago127this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
128this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
129});
130
131// We do not permit arbitrary args to be passed to our process
132args.args = [
133args.platform,
134debugServerListeningPort.toString(),
135args.target || "simulator"
136];
137
138originalNodeDebugSessionLaunchRequest.call(this, request, args);
139};
140
141vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
d976d077Meena Kunnathur Balakrishnan10 years ago142});