microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2743f19c79a39f12f3096db564c6e7f6926df2d5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

136lines · 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";
acf08bc2dlebu10 years ago10import {ILaunchArgs} from "../common/launchArgs";
d976d077Meena Kunnathur Balakrishnan10 years ago11
65bb0c85Jimmy Thomson10 years ago12// 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 {
16class DebugSession {
17public static run: Function;
5547a16fJimmy Thomson10 years ago18public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void;
19public start(input: any, output: any): void;
20public launchRequest(response: any, args: any): void;
65bb0c85Jimmy Thomson10 years ago21}
22class InitializedEvent {
5547a16fJimmy Thomson10 years ago23constructor();
24}
25class OutputEvent {
26constructor(message: string, destination?: string);
27}
28class TerminatedEvent {
29constructor();
65bb0c85Jimmy Thomson10 years ago30}
31}
32
33declare class SourceMaps {
34public _sourceToGeneratedMaps: {};
35public _generatedToSourceMaps: {};
36public _allSourceMaps: {};
37}
38
5547a16fJimmy Thomson10 years ago39declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
65bb0c85Jimmy Thomson10 years ago40public _sourceMaps: SourceMaps;
41}
42
2d3a052eMeena Kunnathur Balakrishnan10 years ago43let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
5547a16fJimmy Thomson10 years ago44
642490c1Jimmy Thomson10 years ago45function bailOut(reason: string): void {
46// Things have gone wrong in initialization: Report the error to telemetry and exit
47TelemetryHelper.sendSimpleEvent(reason);
48Telemetry.sendPendingData().finally(() => {
49process.exit(1);
50});
51}
52
2d3a052eMeena Kunnathur Balakrishnan10 years ago53// Enable telemetry
48efc4d6Meena Kunnathur Balakrishnan10 years ago54Telemetry.init("react-native-debug-adapter", version, true).then(() => {
2d3a052eMeena Kunnathur Balakrishnan10 years ago55let nodeDebugFolder: string;
56let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
b8ef4af9Jimmy Thomson10 years ago57
2d3a052eMeena Kunnathur Balakrishnan10 years ago58/* tslint:disable:no-var-requires */
65bb0c85Jimmy Thomson10 years ago59
2d3a052eMeena Kunnathur Balakrishnan10 years ago60// nodeDebugLocation.json is dynamically generated on extension activation.
61// If it fails, we must not have been in a react native project
4881129dMeena Kunnathur Balakrishnan10 years ago62try {
63nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
64vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
65} catch (e) {
642490c1Jimmy Thomson10 years ago66// Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
67return bailOut("cannotFindDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago68}
65bb0c85Jimmy Thomson10 years ago69
4881129dMeena Kunnathur Balakrishnan10 years ago70// Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
71const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
72vscodeDebugAdapterPackage.DebugSession.run = function() { };
73
74let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
75
76try {
77nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
78} catch (e) {
79// Unable to find nodeDebug, but we can make our own communication channel now
80const debugSession = new vscodeDebugAdapterPackage.DebugSession();
81// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
82// but in that case we have much better ways to investigate errors.
83debugSession.start(process.stdin, process.stdout);
84debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
85debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
dd442738Jimmy Thomson10 years ago86
642490c1Jimmy Thomson10 years ago87return bailOut("cannotFindNodeDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago88}
2d3a052eMeena Kunnathur Balakrishnan10 years ago89
4881129dMeena Kunnathur Balakrishnan10 years ago90/* tslint:enable:no-var-requires */
91
92vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
93
94// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
95const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
96nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
97// Create a server waiting for messages to re-initialize the debug session;
98const reinitializeServer = http.createServer((req, res) => {
99res.statusCode = 404;
100if (req.url === "/refreshBreakpoints") {
101res.statusCode = 200;
102if (this) {
103const sourceMaps = this._sourceMaps;
104if (sourceMaps) {
105// Flush any cached source maps
106sourceMaps._allSourceMaps = {};
107sourceMaps._generatedToSourceMaps = {};
108sourceMaps._sourceToGeneratedMaps = {};
48efc4d6Meena Kunnathur Balakrishnan10 years ago109}
4881129dMeena Kunnathur Balakrishnan10 years ago110// Send an "initialized" event to trigger breakpoints to be re-sent
111this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
112}
113}
114res.end();
2d3a052eMeena Kunnathur Balakrishnan10 years ago115});
4881129dMeena Kunnathur Balakrishnan10 years ago116const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
117
118reinitializeServer.listen(debugServerListeningPort);
119reinitializeServer.on("error", (err: Error) => {
642490c1Jimmy Thomson10 years ago120TelemetryHelper.sendSimpleEvent("reinitializeServerError");
4881129dMeena Kunnathur Balakrishnan10 years ago121this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
122this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
123});
124
125// We do not permit arbitrary args to be passed to our process
126args.args = [
127args.platform,
128debugServerListeningPort.toString(),
129args.target || "simulator"
130];
131
132originalNodeDebugSessionLaunchRequest.call(this, request, args);
133};
134
135vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
d976d077Meena Kunnathur Balakrishnan10 years ago136});