microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
35c2ec2833ec53811abd9d218636e80551b192a8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

144lines · 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
35c2ec28dlebu10 years ago42interface ILaunchArgs {
43platform: string;
44target?: string;
45internalDebuggerPort?: any;
46internalExtensionPort?: number;
47args: string[];
48}
49
2d3a052eMeena Kunnathur Balakrishnan10 years ago50let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
5547a16fJimmy Thomson10 years ago51
642490c1Jimmy Thomson10 years ago52function bailOut(reason: string): void {
53// Things have gone wrong in initialization: Report the error to telemetry and exit
54TelemetryHelper.sendSimpleEvent(reason);
55Telemetry.sendPendingData().finally(() => {
56process.exit(1);
57});
58}
59
2d3a052eMeena Kunnathur Balakrishnan10 years ago60// Enable telemetry
48efc4d6Meena Kunnathur Balakrishnan10 years ago61Telemetry.init("react-native-debug-adapter", version, true).then(() => {
2d3a052eMeena Kunnathur Balakrishnan10 years ago62let nodeDebugFolder: string;
63let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
b8ef4af9Jimmy Thomson10 years ago64
2d3a052eMeena Kunnathur Balakrishnan10 years ago65/* tslint:disable:no-var-requires */
65bb0c85Jimmy Thomson10 years ago66
2d3a052eMeena Kunnathur Balakrishnan10 years ago67// nodeDebugLocation.json is dynamically generated on extension activation.
68// If it fails, we must not have been in a react native project
4881129dMeena Kunnathur Balakrishnan10 years ago69try {
70nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
71vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
72} catch (e) {
642490c1Jimmy Thomson10 years ago73// Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
74return bailOut("cannotFindDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago75}
65bb0c85Jimmy Thomson10 years ago76
4881129dMeena Kunnathur Balakrishnan10 years ago77// Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
78const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
79vscodeDebugAdapterPackage.DebugSession.run = function() { };
80
81let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
82
83try {
84nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
85} catch (e) {
86// Unable to find nodeDebug, but we can make our own communication channel now
87const debugSession = new vscodeDebugAdapterPackage.DebugSession();
88// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
89// but in that case we have much better ways to investigate errors.
90debugSession.start(process.stdin, process.stdout);
91debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
92debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
dd442738Jimmy Thomson10 years ago93
642490c1Jimmy Thomson10 years ago94return bailOut("cannotFindNodeDebugAdapter");
4881129dMeena Kunnathur Balakrishnan10 years ago95}
2d3a052eMeena Kunnathur Balakrishnan10 years ago96
4881129dMeena Kunnathur Balakrishnan10 years ago97/* tslint:enable:no-var-requires */
98
99vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
100
101// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
102const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
103nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
104// Create a server waiting for messages to re-initialize the debug session;
105const reinitializeServer = http.createServer((req, res) => {
106res.statusCode = 404;
107if (req.url === "/refreshBreakpoints") {
108res.statusCode = 200;
109if (this) {
110const sourceMaps = this._sourceMaps;
111if (sourceMaps) {
112// Flush any cached source maps
113sourceMaps._allSourceMaps = {};
114sourceMaps._generatedToSourceMaps = {};
115sourceMaps._sourceToGeneratedMaps = {};
48efc4d6Meena Kunnathur Balakrishnan10 years ago116}
4881129dMeena Kunnathur Balakrishnan10 years ago117// Send an "initialized" event to trigger breakpoints to be re-sent
118this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
119}
120}
121res.end();
2d3a052eMeena Kunnathur Balakrishnan10 years ago122});
4881129dMeena Kunnathur Balakrishnan10 years ago123const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
124
125reinitializeServer.listen(debugServerListeningPort);
126reinitializeServer.on("error", (err: Error) => {
642490c1Jimmy Thomson10 years ago127TelemetryHelper.sendSimpleEvent("reinitializeServerError");
4881129dMeena Kunnathur Balakrishnan10 years ago128this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
129this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
130});
131
132// We do not permit arbitrary args to be passed to our process
133args.args = [
134args.platform,
135debugServerListeningPort.toString(),
35c2ec28dlebu10 years ago136args.internalExtensionPort.toString(),
4881129dMeena Kunnathur Balakrishnan10 years ago137args.target || "simulator"
138];
139
140originalNodeDebugSessionLaunchRequest.call(this, request, args);
141};
142
143vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
d976d077Meena Kunnathur Balakrishnan10 years ago144});