microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/forkedAppWorker.ts
156lines · modeblame
e45838cbVladimir Kotikov9 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as Q from "q"; | |
| 5 | import * as path from "path"; | |
6eeec3c0Serge Svekolnikov8 years ago | 6 | import * as url from "url"; |
e45838cbVladimir Kotikov9 years ago | 7 | import * as child_process from "child_process"; |
5c8365a6Artem Egorov8 years ago | 8 | import {ScriptImporter, DownloadedScript} from "./scriptImporter"; |
e45838cbVladimir Kotikov9 years ago | 9 | |
0a68f8dbArtem Egorov8 years ago | 10 | import { logger } from "vscode-chrome-debug-core"; |
e45838cbVladimir Kotikov9 years ago | 11 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 12 | import { IDebuggeeWorker, RNAppMessage } from "./appWorker"; | |
7daed3fcArtem Egorov8 years ago | 13 | import { RemoteExtension } from "../common/remoteExtension"; |
d124bf0eYuri Skorokhodov7 years ago | 14 | import * as nls from "vscode-nls"; |
| 15 | const localize = nls.loadMessageBundle(); | |
e45838cbVladimir Kotikov9 years ago | 16 | |
| 17 | function printDebuggingError(message: string, reason: any) { | |
d124bf0eYuri Skorokhodov7 years ago | 18 | const nestedError = ErrorHelper.getNestedWarning(reason, localize("DebuggingWontWorkReloadJSAndReconnect", "{0}. Debugging won't work: Try reloading the JS from inside the app, or Reconnect the VS Code debugger", message)); |
0a68f8dbArtem Egorov8 years ago | 19 | |
| 20 | logger.error(nestedError.message); | |
e45838cbVladimir Kotikov9 years ago | 21 | } |
| 22 | | |
| 23 | /** This class will run the RN App logic inside a forked Node process. The framework to run the logic is provided by the file | |
| 24 | * debuggerWorker.js (designed to run on a WebWorker). We add a couple of tweaks (mostly to polyfill WebWorker API) to that | |
| 25 | * file and load it inside of a process. | |
| 26 | * On this side we listen to IPC messages and either respond to them or redirect them to packager via MultipleLifetimeAppWorker's | |
| 27 | * instance. We also intercept packager's signal to load the bundle's code and mutate the message with path to file we've downloaded | |
| 28 | * to let importScripts function take this file. | |
| 29 | */ | |
| 30 | export class ForkedAppWorker implements IDebuggeeWorker { | |
| 31 | | |
6eeec3c0Serge Svekolnikov8 years ago | 32 | protected scriptImporter: ScriptImporter; |
| 33 | protected debuggeeProcess: child_process.ChildProcess | null = null; | |
e45838cbVladimir Kotikov9 years ago | 34 | /** A deferred that we use to make sure that worker has been loaded completely defore start sending IPC messages */ |
6eeec3c0Serge Svekolnikov8 years ago | 35 | protected workerLoaded = Q.defer<void>(); |
5c8365a6Artem Egorov8 years ago | 36 | private bundleLoaded: Q.Deferred<void>; |
7daed3fcArtem Egorov8 years ago | 37 | private remoteExtension: RemoteExtension; |
e45838cbVladimir Kotikov9 years ago | 38 | |
| 39 | constructor( | |
6eeec3c0Serge Svekolnikov8 years ago | 40 | private packagerAddress: string, |
e45838cbVladimir Kotikov9 years ago | 41 | private packagerPort: number, |
| 42 | private sourcesStoragePath: string, | |
7daed3fcArtem Egorov8 years ago | 43 | private projectRootPath: string, |
6eeec3c0Serge Svekolnikov8 years ago | 44 | private postReplyToApp: (message: any) => void, |
| 45 | private packagerRemoteRoot?: string, | |
| 46 | private packagerLocalRoot?: string | |
e45838cbVladimir Kotikov9 years ago | 47 | ) { |
6eeec3c0Serge Svekolnikov8 years ago | 48 | this.scriptImporter = new ScriptImporter(this.packagerAddress, this.packagerPort, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
7daed3fcArtem Egorov8 years ago | 49 | |
| 50 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); | |
| 51 | | |
| 52 | this.remoteExtension.api.Debugger.onShowDevMenu(() => { | |
| 53 | this.postMessage({ | |
| 54 | method: "vscode_showDevMenu", | |
| 55 | }); | |
| 56 | }); | |
| 57 | | |
| 58 | this.remoteExtension.api.Debugger.onReloadApp(() => { | |
| 59 | this.postMessage({ | |
| 60 | method: "vscode_reloadApp", | |
| 61 | }); | |
| 62 | }); | |
e45838cbVladimir Kotikov9 years ago | 63 | } |
| 64 | | |
| 65 | public stop() { | |
| 66 | if (this.debuggeeProcess) { | |
0a68f8dbArtem Egorov8 years ago | 67 | logger.verbose(`About to kill debuggee with pid ${this.debuggeeProcess.pid}`); |
e45838cbVladimir Kotikov9 years ago | 68 | this.debuggeeProcess.kill(); |
| 69 | this.debuggeeProcess = null; | |
| 70 | } | |
| 71 | } | |
| 72 | | |
| 73 | public start(): Q.Promise<number> { | |
| 74 | let scriptToRunPath = path.resolve(this.sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); | |
cc70057dVladimir Kotikov9 years ago | 75 | const port = Math.round(Math.random() * 40000 + 3000); |
e45838cbVladimir Kotikov9 years ago | 76 | |
cc70057dVladimir Kotikov9 years ago | 77 | // Note that we set --debug-brk flag to pause the process on the first line - this is |
| 78 | // required for debug adapter to set the breakpoints BEFORE the debuggee has started. | |
| 79 | // The adapter will continue execution once it's done with breakpoints. | |
f533409bRuslan Bikkinin7 years ago | 80 | const nodeArgs = [`--inspect=${port}`, "--debug-brk", scriptToRunPath]; |
cc70057dVladimir Kotikov9 years ago | 81 | // Start child Node process in debugging mode |
f533409bRuslan Bikkinin7 years ago | 82 | // Using fork instead of spawn causes breakage of piping between app worker and VS Code debug console, e.g. console.log() in application |
| 83 | // wouldn't work. Please see https://github.com/Microsoft/vscode-react-native/issues/758 | |
| 84 | this.debuggeeProcess = child_process.spawn("node", nodeArgs, { | |
| 85 | stdio: ["pipe", "pipe", "pipe", "ipc"], | |
| 86 | }) | |
cc70057dVladimir Kotikov9 years ago | 87 | .on("message", (message: any) => { |
| 88 | // 'workerLoaded' is a special message that indicates that worker is done with loading. | |
| 89 | // We need to wait for it before doing any IPC because process.send doesn't seems to care | |
6eeec3c0Serge Svekolnikov8 years ago | 90 | // about whether the message has been received or not and the first messages are often get |
cc70057dVladimir Kotikov9 years ago | 91 | // discarded by spawned process |
| 92 | if (message && message.workerLoaded) { | |
| 93 | this.workerLoaded.resolve(void 0); | |
| 94 | return; | |
| 95 | } | |
e45838cbVladimir Kotikov9 years ago | 96 | |
cc70057dVladimir Kotikov9 years ago | 97 | this.postReplyToApp(message); |
| 98 | }) | |
| 99 | .on("error", (error: Error) => { | |
d124bf0eYuri Skorokhodov7 years ago | 100 | printDebuggingError(localize("ReactNativeWorkerProcessThrownAnError", "React Native worker process thrown an error"), error); |
e45838cbVladimir Kotikov9 years ago | 101 | }); |
cc70057dVladimir Kotikov9 years ago | 102 | |
| 103 | // Resolve with port debugger server is listening on | |
| 104 | // This will be sent to subscribers of MLAppWorker in "connected" event | |
0a68f8dbArtem Egorov8 years ago | 105 | logger.verbose(`Spawned debuggee process with pid ${this.debuggeeProcess.pid} listening to ${port}`); |
cc70057dVladimir Kotikov9 years ago | 106 | |
| 107 | return Q.resolve(port); | |
e45838cbVladimir Kotikov9 years ago | 108 | } |
| 109 | | |
6eeec3c0Serge Svekolnikov8 years ago | 110 | public postMessage(rnMessage: RNAppMessage): Q.Promise<RNAppMessage> { |
e45838cbVladimir Kotikov9 years ago | 111 | // Before sending messages, make sure that the worker is loaded |
6eeec3c0Serge Svekolnikov8 years ago | 112 | const promise = this.workerLoaded.promise |
936b1ceaArtem Egorov9 years ago | 113 | .then(() => { |
| 114 | if (rnMessage.method !== "executeApplicationScript") { | |
| 115 | // Before sending messages, make sure that the app script executed | |
| 116 | if (this.bundleLoaded) { | |
| 117 | return this.bundleLoaded.promise.then(() => { | |
| 118 | return rnMessage; | |
| 119 | }); | |
| 120 | } else { | |
| 121 | return rnMessage; | |
| 122 | } | |
| 123 | } else { | |
| 124 | this.bundleLoaded = Q.defer<void>(); | |
| 125 | // When packager asks worker to load bundle we download that bundle and | |
| 126 | // then set url field to point to that downloaded bundle, so the worker | |
| 127 | // will take our modified bundle | |
5c8365a6Artem Egorov8 years ago | 128 | if (rnMessage.url) { |
6eeec3c0Serge Svekolnikov8 years ago | 129 | const packagerUrl = url.parse(rnMessage.url); |
| 130 | packagerUrl.host = `${this.packagerAddress}:${this.packagerPort}`; | |
| 131 | rnMessage = { | |
| 132 | ...rnMessage, | |
| 133 | url: url.format(packagerUrl), | |
| 134 | }; | |
d124bf0eYuri Skorokhodov7 years ago | 135 | logger.verbose(`Packager requested runtime to load script from ${rnMessage.url}`); |
979d7bfemax-mironov8 years ago | 136 | return this.scriptImporter.downloadAppScript(<string>rnMessage.url, this.projectRootPath) |
5c8365a6Artem Egorov8 years ago | 137 | .then((downloadedScript: DownloadedScript) => { |
| 138 | this.bundleLoaded.resolve(void 0); | |
| 139 | return Object.assign({}, rnMessage, { url: downloadedScript.filepath }); | |
| 140 | }); | |
| 141 | } else { | |
d124bf0eYuri Skorokhodov7 years ago | 142 | throw Error(localize("RNMessageWithMethodExecuteApplicationScriptDoesntHaveURLProperty", "RNMessage with method 'executeApplicationScript' doesn't have 'url' property")); |
5c8365a6Artem Egorov8 years ago | 143 | } |
936b1ceaArtem Egorov9 years ago | 144 | } |
6eeec3c0Serge Svekolnikov8 years ago | 145 | }); |
| 146 | promise.done( | |
5c8365a6Artem Egorov8 years ago | 147 | (message: RNAppMessage) => { |
| 148 | if (this.debuggeeProcess) { | |
| 149 | this.debuggeeProcess.send({ data: message }); | |
| 150 | } | |
| 151 | }, | |
d124bf0eYuri Skorokhodov7 years ago | 152 | (reason) => printDebuggingError(localize("CouldntImportScriptAt", "Couldn't import script at <{0}>", rnMessage.url), reason)); |
6eeec3c0Serge Svekolnikov8 years ago | 153 | |
| 154 | return promise; | |
e45838cbVladimir Kotikov9 years ago | 155 | } |
| 156 | } |