microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
36e9730fc3e5568605829ee7ab2d402bca4c18ac

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/appWorker.ts

394lines · modecode

1// 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 WebSocket from "ws";
7import { EventEmitter } from "events";
8import { ensurePackagerRunning } from "../common/packagerStatus";
9import {ErrorHelper} from "../common/error/errorHelper";
10import { logger } from "vscode-chrome-debug-core";
11import {ExecutionsLimiter} from "../common/executionsLimiter";
12import { FileSystem as NodeFileSystem} from "../common/node/fileSystem";
13import { ForkedAppWorker } from "./forkedAppWorker";
14import { ScriptImporter } from "./scriptImporter";
15import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
16import * as nls from "vscode-nls";
17import { InternalErrorCode } from "../common/error/internalErrorCode";
18const localize = nls.loadMessageBundle();
19
20export interface RNAppMessage {
21 method: string;
22 url?: string;
23 // These objects have also other properties but that we don't currently use
24}
25
26export interface IDebuggeeWorker {
27 start(): Q.Promise<any>;
28 stop(): void;
29 postMessage(message: RNAppMessage): void;
30}
31
32function printDebuggingError(error: Error, reason: any) {
33 const nestedError = ErrorHelper.getNestedError(error, InternalErrorCode.DebuggingWontWorkReloadJSAndReconnect, reason);
34
35 logger.error(nestedError.message);
36}
37
38 /** This class will create a SandboxedAppWorker that will run the RN App logic, and then create a socket
39 * and send the RN App messages to the SandboxedAppWorker. The only RN App message that this class handles
40 * is the prepareJSRuntime, which we reply to the RN App that the sandbox was created successfully.
41 * When the socket closes, we'll create a new SandboxedAppWorker and a new socket pair and discard the old ones.
42 */
43
44export class MultipleLifetimesAppWorker extends EventEmitter {
45 public static WORKER_BOOTSTRAP = `
46// Initialize some variables before react-native code would access them
47var onmessage=null, self=global;
48// Cache Node's original require as __debug__.require
49global.__debug__={require: require};
50// Prevent leaking process.versions from debugger process to
51// worker because pure React Native doesn't do that and some packages as js-md5 rely on this behavior
52Object.defineProperty(process, "versions", {
53 value: undefined
54});
55
56// TODO: Replace by url.fileURLToPath method when Node 10 LTS become deprecated
57function fileUrlToPath(url) {
58 if (process.platform === 'win32') {
59 return url.toString().replace('file:///', '');
60 } else {
61 return url.toString().replace('file://', '');
62 }
63}
64
65function getNativeModules() {
66 var NativeModules;
67 try {
68 // This approach is for old RN versions
69 NativeModules = global.require('NativeModules');
70 } catch (err) {
71 // ignore error and try another way for more recent RN versions
72 try {
73 var nativeModuleId;
74 var modules = global.__r.getModules();
75 var ids = Object.keys(modules);
76 for (var i = 0; i < ids.length; i++) {
77 if (modules[ids[i]].verboseName) {
78 var packagePath = new String(modules[ids[i]].verboseName);
79 if (packagePath.indexOf('react-native/Libraries/BatchedBridge/NativeModules.js') > 0) {
80 nativeModuleId = parseInt(ids[i], 10);
81 break;
82 }
83 }
84 }
85 if (nativeModuleId) {
86 NativeModules = global.__r(nativeModuleId);
87 }
88 }
89 catch (err) {
90 // suppress errors
91 }
92 }
93 return NativeModules;
94}
95
96// Originally, this was made for iOS only
97var vscodeHandlers = {
98 'vscode_reloadApp': function () {
99 var NativeModules = getNativeModules();
100 if (NativeModules) {
101 NativeModules.DevMenu.reload();
102 }
103 },
104 'vscode_showDevMenu': function () {
105 var NativeModules = getNativeModules();
106 if (NativeModules) {
107 NativeModules.DevMenu.show();
108 }
109 }
110};
111
112process.on("message", function (message) {
113 if (message.data && vscodeHandlers[message.data.method]) {
114 vscodeHandlers[message.data.method]();
115 } else if(onmessage) {
116 onmessage(message);
117 }
118});
119
120var postMessage = function(message){
121 process.send(message);
122};
123
124if (!self.postMessage) {
125 self.postMessage = postMessage;
126}
127
128var importScripts = (function(){
129 var fs=require('fs'), vm=require('vm');
130 return function(scriptUrl){
131 scriptUrl = fileUrlToPath(scriptUrl);
132 var scriptCode = fs.readFileSync(scriptUrl, 'utf8');
133 // Add a 'debugger;' statement to stop code execution
134 // to wait for the sourcemaps to be processed by the debug adapter
135 vm.runInThisContext('debugger;' + scriptCode, {filename: scriptUrl});
136 };
137})();
138`;
139
140 public static CONSOLE_TRACE_PATCH = `// Worker is ran as nodejs process, so console.trace() writes to stderr and it leads to error in native app
141// To avoid this console.trace() is overridden to print stacktrace via console.log()
142// Please, see Node JS implementation: https://github.com/nodejs/node/blob/master/lib/internal/console/constructor.js
143console.trace = (function() {
144 return function() {
145 try {
146 var err = {
147 name: 'Trace',
148 message: require('util').format.apply(null, arguments)
149 };
150 // Node uses 10, but usually it's not enough for RN app trace
151 Error.stackTraceLimit = 30;
152 Error.captureStackTrace(err, console.trace);
153 console.log(err.stack);
154 } catch (e) {
155 console.error(e);
156 }
157 };
158})();
159`;
160
161 public static PROCESS_TO_STRING_PATCH = `// As worker is ran in node, it breaks broadcast-channels package approach of identifying if it’s ran in node:
162// https://github.com/pubkey/broadcast-channel/blob/master/src/util.js#L64
163// To avoid it if process.toString() is called if will return empty string instead of [object process].
164var nativeObjectToString = Object.prototype.toString;
165Object.prototype.toString = function() {
166 if (this === process) {
167 return '';
168 } else {
169 return nativeObjectToString.call(this);
170 }
171};
172`;
173
174 public static WORKER_DONE = `// Notify debugger that we're done with loading
175// and started listening for IPC messages
176postMessage({workerLoaded:true});`;
177
178 public static FETCH_STUB = `(function(self) {
179'use strict';
180
181if (self.fetch) {
182 return;
183}
184
185self.fetch = fetch;
186
187function fetch(url) {
188 return new Promise((resolve, reject) => {
189 var data = require('fs').readFileSync(fileUrlToPath(url), 'utf8');
190 resolve(
191 {
192 text: function () {
193 return data;
194 }
195 });
196 });
197}
198})(global);
199`;
200
201 private packagerAddress: string;
202 private packagerPort: number;
203 private sourcesStoragePath: string;
204 private projectRootPath: string;
205 private packagerRemoteRoot?: string;
206 private packagerLocalRoot?: string;
207 private debuggerWorkerUrlPath?: string;
208 private socketToApp: WebSocket;
209 private singleLifetimeWorker: IDebuggeeWorker | null;
210 private webSocketConstructor: (url: string) => WebSocket;
211
212 private executionLimiter = new ExecutionsLimiter();
213 private nodeFileSystem = new NodeFileSystem();
214 private scriptImporter: ScriptImporter;
215
216 constructor(
217 attachRequestArguments: any,
218 sourcesStoragePath: string,
219 projectRootPath: string,
220 {
221 webSocketConstructor = (url: string) => new WebSocket(url),
222 } = {}) {
223 super();
224 this.packagerAddress = attachRequestArguments.address || "localhost";
225 this.packagerPort = attachRequestArguments.port;
226 this.packagerRemoteRoot = attachRequestArguments.remoteRoot;
227 this.packagerLocalRoot = attachRequestArguments.localRoot;
228 this.debuggerWorkerUrlPath = attachRequestArguments.debuggerWorkerUrlPath;
229 this.sourcesStoragePath = sourcesStoragePath;
230 this.projectRootPath = projectRootPath;
231 if (!this.sourcesStoragePath)
232 throw ErrorHelper.getInternalError(InternalErrorCode.SourcesStoragePathIsNullOrEmpty);
233 this.webSocketConstructor = webSocketConstructor;
234 this.scriptImporter = new ScriptImporter(this.packagerAddress, this.packagerPort, sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot);
235 }
236
237 public start(retryAttempt: boolean = false): Q.Promise<any> {
238 const errPackagerNotRunning = ErrorHelper.getInternalError(InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort, this.packagerPort);
239
240 return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning)
241 .then(() => {
242 // Don't fetch debugger worker on socket disconnect
243 return retryAttempt ? Q.resolve<void>(void 0) :
244 this.downloadAndPatchDebuggerWorker();
245 })
246 .then(() => this.createSocketToApp(retryAttempt));
247 }
248
249 public stop() {
250 if (this.socketToApp) {
251 this.socketToApp.removeAllListeners();
252 this.socketToApp.close();
253 }
254
255 if (this.singleLifetimeWorker) {
256 this.singleLifetimeWorker.stop();
257 }
258 }
259
260 public downloadAndPatchDebuggerWorker(): Q.Promise<void> {
261 let scriptToRunPath = path.resolve(this.sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
262 return this.scriptImporter.downloadDebuggerWorker(this.sourcesStoragePath, this.projectRootPath, this.debuggerWorkerUrlPath)
263 .then(() => this.nodeFileSystem.readFile(scriptToRunPath, "utf8"))
264 .then((workerContent: string) => {
265 const isHaulProject = ReactNativeProjectHelper.isHaulProject(this.projectRootPath);
266 // Add our customizations to debugger worker to get it working smoothly
267 // in Node env and polyfill WebWorkers API over Node's IPC.
268 const modifiedDebuggeeContent = [
269 MultipleLifetimesAppWorker.WORKER_BOOTSTRAP,
270 MultipleLifetimesAppWorker.CONSOLE_TRACE_PATCH,
271 MultipleLifetimesAppWorker.PROCESS_TO_STRING_PATCH,
272 isHaulProject ? MultipleLifetimesAppWorker.FETCH_STUB : null,
273 workerContent,
274 MultipleLifetimesAppWorker.WORKER_DONE,
275 ].join("\n");
276 return this.nodeFileSystem.writeFile(scriptToRunPath, modifiedDebuggeeContent);
277 });
278 }
279
280 private startNewWorkerLifetime(): Q.Promise<void> {
281 this.singleLifetimeWorker = new ForkedAppWorker(this.packagerAddress, this.packagerPort, this.sourcesStoragePath, this.projectRootPath,
282 (message) => {
283 this.sendMessageToApp(message);
284 },
285 this.packagerRemoteRoot, this.packagerLocalRoot);
286 logger.verbose("A new app worker lifetime was created.");
287 return this.singleLifetimeWorker.start()
288 .then(startedEvent => {
289 this.emit("connected", startedEvent);
290 });
291 }
292
293 private createSocketToApp(retryAttempt: boolean = false): Q.Promise<void> {
294 let deferred = Q.defer<void>();
295 this.socketToApp = this.webSocketConstructor(this.debuggerProxyUrl());
296 this.socketToApp.on("open", () => {
297 this.onSocketOpened();
298 });
299 this.socketToApp.on("close",
300 () => {
301 this.executionLimiter.execute("onSocketClose.msg", /*limitInSeconds*/ 10, () => {
302 /*
303 * It is not the best idea to compare with the message, but this is the only thing React Native gives that is unique when
304 * it closes the socket because it already has a connection to a debugger.
305 * https://github.com/facebook/react-native/blob/588f01e9982775f0699c7bfd56623d4ed3949810/local-cli/server/util/webSocketProxy.js#L38
306 */
307 let msgKey = "_closeMessage";
308 if (this.socketToApp[msgKey] === "Another debugger is already connected") {
309 deferred.reject(ErrorHelper.getInternalError(InternalErrorCode.AnotherDebuggerConnectedToPackager));
310 }
311 logger.log(localize("DisconnectedFromThePackagerToReactNative", "Disconnected from the Proxy (Packager) to the React Native application. Retrying reconnection soon..."));
312 });
313 setTimeout(() => {
314 this.start(true /* retryAttempt */);
315 }, 100);
316 });
317 this.socketToApp.on("message",
318 (message: any) => this.onMessage(message));
319 this.socketToApp.on("error",
320 (error: Error) => {
321 if (retryAttempt) {
322 printDebuggingError(ErrorHelper.getInternalError(InternalErrorCode.ReconnectionToPackagerFailedCheckForErrorsOrRestartReactNative), error);
323 }
324
325 deferred.reject(error);
326 });
327
328 // In an attempt to catch failures in starting the packager on first attempt,
329 // wait for 300 ms before resolving the promise
330 Q.delay(300).done(() => deferred.resolve(void 0));
331 return deferred.promise;
332 }
333
334 private debuggerProxyUrl() {
335 return `ws://${this.packagerAddress}:${this.packagerPort}/debugger-proxy?role=debugger&name=vscode`;
336 }
337
338 private onSocketOpened() {
339 this.executionLimiter.execute("onSocketOpened.msg", /*limitInSeconds*/ 10, () =>
340 logger.log(localize("EstablishedConnectionWithPackagerToReactNativeApp", "Established a connection with the Proxy (Packager) to the React Native application")));
341 }
342
343 private killWorker() {
344 if (!this.singleLifetimeWorker) return;
345 this.singleLifetimeWorker.stop();
346 this.singleLifetimeWorker = null;
347 }
348
349 private onMessage(message: string) {
350 try {
351 logger.verbose("From RN APP: " + message);
352 let object = <RNAppMessage>JSON.parse(message);
353 if (object.method === "prepareJSRuntime") {
354 // In RN 0.40 Android runtime doesn't seem to be sending "$disconnected" event
355 // when user reloads an app, hence we need to try to kill it here either.
356 this.killWorker();
357 // The MultipleLifetimesAppWorker will handle prepareJSRuntime aka create new lifetime
358 this.gotPrepareJSRuntime(object);
359 } else if (object.method === "$disconnected") {
360 // We need to shutdown the current app worker, and create a new lifetime
361 this.killWorker();
362 } else if (object.method) {
363 // All the other messages are handled by the single lifetime worker
364 if (this.singleLifetimeWorker) {
365 this.singleLifetimeWorker.postMessage(object);
366 }
367 } else {
368 // Message doesn't have a method. Ignore it. This is an info message instead of warn because it's normal and expected
369 logger.verbose(`The react-native app sent a message without specifying a method: ${message}`);
370 }
371 } catch (exception) {
372 printDebuggingError(ErrorHelper.getInternalError(InternalErrorCode.FailedToProcessMessageFromReactNativeApp, message), exception);
373 }
374 }
375
376 private gotPrepareJSRuntime(message: any): void {
377 // Create the sandbox, and replay that we finished processing the message
378 this.startNewWorkerLifetime().done(() => {
379 this.sendMessageToApp({ replyID: parseInt(message.id, 10) });
380 }, error => printDebuggingError(ErrorHelper.getInternalError(InternalErrorCode.FailedToPrepareJSRuntimeEnvironment, message), error));
381 }
382
383 private sendMessageToApp(message: any): void {
384 let stringified: string = "";
385 try {
386 stringified = JSON.stringify(message);
387 logger.verbose(`To RN APP: ${stringified}`);
388 this.socketToApp.send(stringified);
389 } catch (exception) {
390 let messageToShow = stringified || ("" + message); // Try to show the stringified version, but show the toString if unavailable
391 printDebuggingError(ErrorHelper.getInternalError(InternalErrorCode.FailedToSendMessageToTheReactNativeApp, messageToShow), exception);
392 }
393 }
394}
395