microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.16

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/forkedAppWorker.ts

152lines · modeblame

e45838cbVladimir Kotikov9 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
4import * as Q from "q";
5import * as path from "path";
6eeec3c0Serge Svekolnikov8 years ago6import * as url from "url";
e45838cbVladimir Kotikov9 years ago7import * as child_process from "child_process";
5c8365a6Artem Egorov8 years ago8import {ScriptImporter, DownloadedScript} from "./scriptImporter";
e45838cbVladimir Kotikov9 years ago9
0a68f8dbArtem Egorov8 years ago10import { logger } from "vscode-chrome-debug-core";
e45838cbVladimir Kotikov9 years ago11import { ErrorHelper } from "../common/error/errorHelper";
12import { IDebuggeeWorker, RNAppMessage } from "./appWorker";
7daed3fcArtem Egorov8 years ago13import { RemoteExtension } from "../common/remoteExtension";
e45838cbVladimir Kotikov9 years ago14
15function printDebuggingError(message: string, reason: any) {
0a68f8dbArtem Egorov8 years ago16const nestedError = ErrorHelper.getNestedWarning(reason, `${message}. Debugging won't work: Try reloading the JS from inside the app, or Reconnect the VS Code debugger`);
17
18logger.error(nestedError.message);
e45838cbVladimir Kotikov9 years ago19}
20
21/** This class will run the RN App logic inside a forked Node process. The framework to run the logic is provided by the file
22* debuggerWorker.js (designed to run on a WebWorker). We add a couple of tweaks (mostly to polyfill WebWorker API) to that
23* file and load it inside of a process.
24* On this side we listen to IPC messages and either respond to them or redirect them to packager via MultipleLifetimeAppWorker's
25* instance. We also intercept packager's signal to load the bundle's code and mutate the message with path to file we've downloaded
26* to let importScripts function take this file.
27*/
28export class ForkedAppWorker implements IDebuggeeWorker {
29
6eeec3c0Serge Svekolnikov8 years ago30protected scriptImporter: ScriptImporter;
31protected debuggeeProcess: child_process.ChildProcess | null = null;
e45838cbVladimir Kotikov9 years ago32/** A deferred that we use to make sure that worker has been loaded completely defore start sending IPC messages */
6eeec3c0Serge Svekolnikov8 years ago33protected workerLoaded = Q.defer<void>();
5c8365a6Artem Egorov8 years ago34private bundleLoaded: Q.Deferred<void>;
7daed3fcArtem Egorov8 years ago35private remoteExtension: RemoteExtension;
e45838cbVladimir Kotikov9 years ago36
37constructor(
6eeec3c0Serge Svekolnikov8 years ago38private packagerAddress: string,
e45838cbVladimir Kotikov9 years ago39private packagerPort: number,
40private sourcesStoragePath: string,
7daed3fcArtem Egorov8 years ago41private projectRootPath: string,
6eeec3c0Serge Svekolnikov8 years ago42private postReplyToApp: (message: any) => void,
43private packagerRemoteRoot?: string,
44private packagerLocalRoot?: string
e45838cbVladimir Kotikov9 years ago45) {
6eeec3c0Serge Svekolnikov8 years ago46this.scriptImporter = new ScriptImporter(this.packagerAddress, this.packagerPort, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot);
7daed3fcArtem Egorov8 years ago47
48this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath);
49
50this.remoteExtension.api.Debugger.onShowDevMenu(() => {
51this.postMessage({
52method: "vscode_showDevMenu",
53});
54});
55
56this.remoteExtension.api.Debugger.onReloadApp(() => {
57this.postMessage({
58method: "vscode_reloadApp",
59});
60});
e45838cbVladimir Kotikov9 years ago61}
62
63public stop() {
64if (this.debuggeeProcess) {
0a68f8dbArtem Egorov8 years ago65logger.verbose(`About to kill debuggee with pid ${this.debuggeeProcess.pid}`);
e45838cbVladimir Kotikov9 years ago66this.debuggeeProcess.kill();
67this.debuggeeProcess = null;
68}
69}
70
71public start(): Q.Promise<number> {
72let scriptToRunPath = path.resolve(this.sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
cc70057dVladimir Kotikov9 years ago73const port = Math.round(Math.random() * 40000 + 3000);
e45838cbVladimir Kotikov9 years ago74
cc70057dVladimir Kotikov9 years ago75// Note that we set --debug-brk flag to pause the process on the first line - this is
76// required for debug adapter to set the breakpoints BEFORE the debuggee has started.
77// The adapter will continue execution once it's done with breakpoints.
634e3ecaRuslan Bikkinin7 years ago78const nodeArgs = [`--inspect=${port}`, "--debug-brk", scriptToRunPath];
cc70057dVladimir Kotikov9 years ago79// Start child Node process in debugging mode
634e3ecaRuslan Bikkinin7 years ago80this.debuggeeProcess = child_process.spawn("node", nodeArgs, {
81stdio: ["pipe", "pipe", "pipe", "ipc"],
82})
cc70057dVladimir Kotikov9 years ago83.on("message", (message: any) => {
84// 'workerLoaded' is a special message that indicates that worker is done with loading.
85// We need to wait for it before doing any IPC because process.send doesn't seems to care
6eeec3c0Serge Svekolnikov8 years ago86// about whether the message has been received or not and the first messages are often get
cc70057dVladimir Kotikov9 years ago87// discarded by spawned process
88if (message && message.workerLoaded) {
89this.workerLoaded.resolve(void 0);
90return;
91}
e45838cbVladimir Kotikov9 years ago92
cc70057dVladimir Kotikov9 years ago93this.postReplyToApp(message);
94})
95.on("error", (error: Error) => {
0a68f8dbArtem Egorov8 years ago96printDebuggingError("React Native worker process thrown an error", error);
e45838cbVladimir Kotikov9 years ago97});
cc70057dVladimir Kotikov9 years ago98
99// Resolve with port debugger server is listening on
100// This will be sent to subscribers of MLAppWorker in "connected" event
0a68f8dbArtem Egorov8 years ago101logger.verbose(`Spawned debuggee process with pid ${this.debuggeeProcess.pid} listening to ${port}`);
cc70057dVladimir Kotikov9 years ago102
103return Q.resolve(port);
e45838cbVladimir Kotikov9 years ago104}
105
6eeec3c0Serge Svekolnikov8 years ago106public postMessage(rnMessage: RNAppMessage): Q.Promise<RNAppMessage> {
e45838cbVladimir Kotikov9 years ago107// Before sending messages, make sure that the worker is loaded
6eeec3c0Serge Svekolnikov8 years ago108const promise = this.workerLoaded.promise
936b1ceaArtem Egorov8 years ago109.then(() => {
110if (rnMessage.method !== "executeApplicationScript") {
111// Before sending messages, make sure that the app script executed
112if (this.bundleLoaded) {
113return this.bundleLoaded.promise.then(() => {
114return rnMessage;
115});
116} else {
117return rnMessage;
118}
119} else {
120this.bundleLoaded = Q.defer<void>();
121// When packager asks worker to load bundle we download that bundle and
122// then set url field to point to that downloaded bundle, so the worker
123// will take our modified bundle
5c8365a6Artem Egorov8 years ago124if (rnMessage.url) {
6eeec3c0Serge Svekolnikov8 years ago125const packagerUrl = url.parse(rnMessage.url);
126packagerUrl.host = `${this.packagerAddress}:${this.packagerPort}`;
127rnMessage = {
128...rnMessage,
129url: url.format(packagerUrl),
130};
0a68f8dbArtem Egorov8 years ago131logger.verbose("Packager requested runtime to load script from " + rnMessage.url);
979d7bfemax-mironov8 years ago132return this.scriptImporter.downloadAppScript(<string>rnMessage.url, this.projectRootPath)
5c8365a6Artem Egorov8 years ago133.then((downloadedScript: DownloadedScript) => {
134this.bundleLoaded.resolve(void 0);
135return Object.assign({}, rnMessage, { url: downloadedScript.filepath });
136});
137} else {
138throw Error("RNMessage with method 'executeApplicationScript' doesn't have 'url' property");
139}
936b1ceaArtem Egorov8 years ago140}
6eeec3c0Serge Svekolnikov8 years ago141});
142promise.done(
5c8365a6Artem Egorov8 years ago143(message: RNAppMessage) => {
144if (this.debuggeeProcess) {
145this.debuggeeProcess.send({ data: message });
146}
147},
148(reason) => printDebuggingError(`Couldn't import script at <${rnMessage.url}>`, reason));
6eeec3c0Serge Svekolnikov8 years ago149
150return promise;
e45838cbVladimir Kotikov9 years ago151}
152}