microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/forkedAppWorker.ts

114lines · 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";
6import * as child_process from "child_process";
7import {ScriptImporter} from "./scriptImporter";
8
9import { Log } from "../common/log/log";
a8b90ac7Vladimir Kotikov9 years ago10import { LogLevel } from "../common/log/logHelper";
e45838cbVladimir Kotikov9 years ago11import { ErrorHelper } from "../common/error/errorHelper";
12import { IDebuggeeWorker, RNAppMessage } from "./appWorker";
13
14function printDebuggingError(message: string, reason: any) {
15Log.logWarning(ErrorHelper.getNestedWarning(reason, `${message}. Debugging won't work: Try reloading the JS from inside the app, or Reconnect the VS Code debugger`));
16}
17
18/** This class will run the RN App logic inside a forked Node process. The framework to run the logic is provided by the file
19* debuggerWorker.js (designed to run on a WebWorker). We add a couple of tweaks (mostly to polyfill WebWorker API) to that
20* file and load it inside of a process.
21* On this side we listen to IPC messages and either respond to them or redirect them to packager via MultipleLifetimeAppWorker's
22* instance. We also intercept packager's signal to load the bundle's code and mutate the message with path to file we've downloaded
23* to let importScripts function take this file.
24*/
25export class ForkedAppWorker implements IDebuggeeWorker {
26
27private scriptImporter: ScriptImporter;
28private debuggeeProcess: child_process.ChildProcess = null;
29/** A deferred that we use to make sure that worker has been loaded completely defore start sending IPC messages */
30private workerLoaded = Q.defer<void>();
936b1ceaArtem Egorov8 years ago31private bundleLoaded;
e45838cbVladimir Kotikov9 years ago32
33constructor(
34private packagerPort: number,
35private sourcesStoragePath: string,
36private postReplyToApp: (message: any) => void
37) {
38this.scriptImporter = new ScriptImporter(packagerPort, sourcesStoragePath);
39}
40
41public stop() {
42if (this.debuggeeProcess) {
a8b90ac7Vladimir Kotikov9 years ago43Log.logInternalMessage(LogLevel.Info, `About to kill debuggee with pid ${this.debuggeeProcess.pid}`);
e45838cbVladimir Kotikov9 years ago44this.debuggeeProcess.kill();
45this.debuggeeProcess = null;
46}
47}
48
49public start(): Q.Promise<number> {
50let scriptToRunPath = path.resolve(this.sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
cc70057dVladimir Kotikov9 years ago51const port = Math.round(Math.random() * 40000 + 3000);
e45838cbVladimir Kotikov9 years ago52
cc70057dVladimir Kotikov9 years ago53// Note that we set --debug-brk flag to pause the process on the first line - this is
54// required for debug adapter to set the breakpoints BEFORE the debuggee has started.
55// The adapter will continue execution once it's done with breakpoints.
56const nodeArgs = [`--inspect=${port}`, "--debug-brk", scriptToRunPath];
57// Start child Node process in debugging mode
58this.debuggeeProcess = child_process.spawn("node", nodeArgs, {
59stdio: ["pipe", "pipe", "pipe", "ipc"],
e45838cbVladimir Kotikov9 years ago60})
cc70057dVladimir Kotikov9 years ago61.on("message", (message: any) => {
62// 'workerLoaded' is a special message that indicates that worker is done with loading.
63// We need to wait for it before doing any IPC because process.send doesn't seems to care
64// about whether the messahe has been received or not and the first messages are often get
65// discarded by spawned process
66if (message && message.workerLoaded) {
67this.workerLoaded.resolve(void 0);
68return;
69}
e45838cbVladimir Kotikov9 years ago70
cc70057dVladimir Kotikov9 years ago71this.postReplyToApp(message);
72})
73.on("error", (error: Error) => {
74Log.logWarning(error);
e45838cbVladimir Kotikov9 years ago75});
cc70057dVladimir Kotikov9 years ago76
77// Resolve with port debugger server is listening on
78// This will be sent to subscribers of MLAppWorker in "connected" event
79Log.logInternalMessage(LogLevel.Info,
80`Spawned debuggee process with pid ${this.debuggeeProcess.pid} listening to ${port}`);
81
82return Q.resolve(port);
e45838cbVladimir Kotikov9 years ago83}
84
85public postMessage(rnMessage: RNAppMessage): void {
86// Before sending messages, make sure that the worker is loaded
87this.workerLoaded.promise
936b1ceaArtem Egorov8 years ago88.then(() => {
89if (rnMessage.method !== "executeApplicationScript") {
90// Before sending messages, make sure that the app script executed
91if (this.bundleLoaded) {
92return this.bundleLoaded.promise.then(() => {
93return rnMessage;
94});
95} else {
96return rnMessage;
97}
98} else {
99this.bundleLoaded = Q.defer<void>();
100// When packager asks worker to load bundle we download that bundle and
101// then set url field to point to that downloaded bundle, so the worker
102// will take our modified bundle
103Log.logInternalMessage(LogLevel.Info, "Packager requested runtime to load script from " + rnMessage.url);
104return this.scriptImporter.downloadAppScript(rnMessage.url)
105.then(downloadedScript => {
106this.bundleLoaded.resolve(void 0);
107return Object.assign({}, rnMessage, { url: downloadedScript.filepath });
108});
109}
110})
111.done((message: RNAppMessage) => this.debuggeeProcess.send({ data: message }),
e45838cbVladimir Kotikov9 years ago112reason => printDebuggingError(`Couldn't import script at <${rnMessage.url}>`, reason));
113}
114}