microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/forkedAppWorker.ts

102lines · 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>();
31
32constructor(
33private packagerPort: number,
34private sourcesStoragePath: string,
35private postReplyToApp: (message: any) => void
36) {
37this.scriptImporter = new ScriptImporter(packagerPort, sourcesStoragePath);
38}
39
40public stop() {
41if (this.debuggeeProcess) {
a8b90ac7Vladimir Kotikov9 years ago42Log.logInternalMessage(LogLevel.Info, `About to kill debuggee with pid ${this.debuggeeProcess.pid}`);
e45838cbVladimir Kotikov9 years ago43this.debuggeeProcess.kill();
44this.debuggeeProcess = null;
45}
46}
47
48public start(): Q.Promise<number> {
49let scriptToRunPath = path.resolve(this.sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
cc70057dVladimir Kotikov9 years ago50const port = Math.round(Math.random() * 40000 + 3000);
e45838cbVladimir Kotikov9 years ago51
cc70057dVladimir Kotikov9 years ago52// Note that we set --debug-brk flag to pause the process on the first line - this is
53// required for debug adapter to set the breakpoints BEFORE the debuggee has started.
54// The adapter will continue execution once it's done with breakpoints.
55const nodeArgs = [`--inspect=${port}`, "--debug-brk", scriptToRunPath];
56// Start child Node process in debugging mode
57this.debuggeeProcess = child_process.spawn("node", nodeArgs, {
58stdio: ["pipe", "pipe", "pipe", "ipc"],
e45838cbVladimir Kotikov9 years ago59})
cc70057dVladimir Kotikov9 years ago60.on("message", (message: any) => {
61// 'workerLoaded' is a special message that indicates that worker is done with loading.
62// We need to wait for it before doing any IPC because process.send doesn't seems to care
63// about whether the messahe has been received or not and the first messages are often get
64// discarded by spawned process
65if (message && message.workerLoaded) {
66this.workerLoaded.resolve(void 0);
67return;
68}
e45838cbVladimir Kotikov9 years ago69
cc70057dVladimir Kotikov9 years ago70this.postReplyToApp(message);
71})
72.on("error", (error: Error) => {
73Log.logWarning(error);
e45838cbVladimir Kotikov9 years ago74});
cc70057dVladimir Kotikov9 years ago75
76// Resolve with port debugger server is listening on
77// This will be sent to subscribers of MLAppWorker in "connected" event
78Log.logInternalMessage(LogLevel.Info,
79`Spawned debuggee process with pid ${this.debuggeeProcess.pid} listening to ${port}`);
80
81return Q.resolve(port);
e45838cbVladimir Kotikov9 years ago82}
83
84public postMessage(rnMessage: RNAppMessage): void {
85// Before sending messages, make sure that the worker is loaded
86this.workerLoaded.promise
87.then(() => {
88if (rnMessage.method !== "executeApplicationScript") return Q.resolve(rnMessage);
89
a8b90ac7Vladimir Kotikov9 years ago90// When packager asks worker to load bundle we download that bundle and
91// then set url field to point to that downloaded bundle, so the worker
e45838cbVladimir Kotikov9 years ago92// will take our modified bundle
a8b90ac7Vladimir Kotikov9 years ago93Log.logInternalMessage(LogLevel.Info, "Packager requested runtime to load script from " + rnMessage.url);
e45838cbVladimir Kotikov9 years ago94return this.scriptImporter.downloadAppScript(rnMessage.url)
e38efae1Vladimir Kotikov9 years ago95.then(downloadedScript => {
96return Object.assign({}, rnMessage, { url: downloadedScript.filepath });
97});
e45838cbVladimir Kotikov9 years ago98})
99.done((message: RNAppMessage) => this.debuggeeProcess.send({ data: message }),
100reason => printDebuggingError(`Couldn't import script at <${rnMessage.url}>`, reason));
101}
102}