microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/appWorker.ts

482lines · modeblame

9f036952Nisheet Jain10 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
cc70057dVladimir Kotikov9 years ago4import * as path from "path";
5940f996RedMickey5 years ago5import * as vscode from "vscode";
ea8a5f88digeff10 years ago6import * as WebSocket from "ws";
e45838cbVladimir Kotikov9 years ago7import { EventEmitter } from "events";
0a68f8dbArtem Egorov8 years ago8import { ensurePackagerRunning } from "../common/packagerStatus";
34472878RedMickey5 years ago9import { ErrorHelper } from "../common/error/errorHelper";
a6562589RedMickey6 years ago10import { logger } from "vscode-debugadapter";
34472878RedMickey5 years ago11import { ExecutionsLimiter } from "../common/executionsLimiter";
b05b5086Vladimir Kotikov9 years ago12import { ForkedAppWorker } from "./forkedAppWorker";
cc70057dVladimir Kotikov9 years ago13import { ScriptImporter } from "./scriptImporter";
bb869343Serge Svekolnikov8 years ago14import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
d124bf0eYuri Skorokhodov7 years ago15import * as nls from "vscode-nls";
16import { InternalErrorCode } from "../common/error/internalErrorCode";
ce5e88eeYuri Skorokhodov5 years ago17import { FileSystem } from "../common/node/fileSystem";
18import { PromiseUtil } from "../common/node/promise";
34472878RedMickey5 years ago19nls.config({
20messageFormat: nls.MessageFormat.bundle,
21bundleFormat: nls.BundleFormat.standalone,
22})();
d124bf0eYuri Skorokhodov7 years ago23const localize = nls.loadMessageBundle();
4677921cdigeff10 years ago24
e45838cbVladimir Kotikov9 years ago25export interface RNAppMessage {
ea8a5f88digeff10 years ago26method: string;
e45838cbVladimir Kotikov9 years ago27url?: string;
ea8a5f88digeff10 years ago28// These objects have also other properties but that we don't currently use
29}
5d4d4de0digeff10 years ago30
e45838cbVladimir Kotikov9 years ago31export interface IDebuggeeWorker {
ce5e88eeYuri Skorokhodov5 years ago32start(): Promise<any>;
e45838cbVladimir Kotikov9 years ago33stop(): void;
34postMessage(message: RNAppMessage): void;
35}
36
1758f9a6Yuri Skorokhodov7 years ago37function printDebuggingError(error: Error, reason: any) {
34472878RedMickey5 years ago38const nestedError = ErrorHelper.getNestedError(
39error,
40InternalErrorCode.DebuggingWontWorkReloadJSAndReconnect,
41reason,
42);
0a68f8dbArtem Egorov8 years ago43
44logger.error(nestedError.message);
039239d1Vladimir Kotikov9 years ago45}
46
34472878RedMickey5 years ago47/** This class will create a SandboxedAppWorker that will run the RN App logic, and then create a socket
48* and send the RN App messages to the SandboxedAppWorker. The only RN App message that this class handles
49* is the prepareJSRuntime, which we reply to the RN App that the sandbox was created successfully.
50* When the socket closes, we'll create a new SandboxedAppWorker and a new socket pair and discard the old ones.
51*/
039239d1Vladimir Kotikov9 years ago52
53export class MultipleLifetimesAppWorker extends EventEmitter {
54public static WORKER_BOOTSTRAP = `
cc70057dVladimir Kotikov9 years ago55// Initialize some variables before react-native code would access them
d64a6928Vladimir Kotikov9 years ago56var onmessage=null, self=global;
cc70057dVladimir Kotikov9 years ago57// Cache Node's original require as __debug__.require
d64a6928Vladimir Kotikov9 years ago58global.__debug__={require: require};
0864d702Ruslan Bikkinin7 years ago59// Prevent leaking process.versions from debugger process to
60// worker because pure React Native doesn't do that and some packages as js-md5 rely on this behavior
61Object.defineProperty(process, "versions", {
62value: undefined
63});
7daed3fcArtem Egorov8 years ago64
4cf8fdf4Yuri Skorokhodov6 years ago65// TODO: Replace by url.fileURLToPath method when Node 10 LTS become deprecated
66function fileUrlToPath(url) {
67if (process.platform === 'win32') {
68return url.toString().replace('file:///', '');
69} else {
70return url.toString().replace('file://', '');
71}
72}
73
2ecfbd20Yuri Skorokhodov7 years ago74function getNativeModules() {
75var NativeModules;
76try {
77// This approach is for old RN versions
78NativeModules = global.require('NativeModules');
79} catch (err) {
80// ignore error and try another way for more recent RN versions
81try {
82var nativeModuleId;
83var modules = global.__r.getModules();
84var ids = Object.keys(modules);
85for (var i = 0; i < ids.length; i++) {
86if (modules[ids[i]].verboseName) {
87var packagePath = new String(modules[ids[i]].verboseName);
c3481846Yuri Skorokhodov5 years ago88if (packagePath.indexOf('Libraries/BatchedBridge/NativeModules.js') > 0 || packagePath.indexOf('Libraries\\\\BatchedBridge\\\\NativeModules.js') > 0) {
2ecfbd20Yuri Skorokhodov7 years ago89nativeModuleId = parseInt(ids[i], 10);
90break;
91}
92}
93}
94if (nativeModuleId) {
95NativeModules = global.__r(nativeModuleId);
96}
97}
98catch (err) {
99// suppress errors
100}
101}
102return NativeModules;
103}
104
105// Originally, this was made for iOS only
7daed3fcArtem Egorov8 years ago106var vscodeHandlers = {
107'vscode_reloadApp': function () {
2ecfbd20Yuri Skorokhodov7 years ago108var NativeModules = getNativeModules();
cfbe5cc9etatanova5 years ago109if (NativeModules && NativeModules.DevSettings) {
110NativeModules.DevSettings.reload();
7daed3fcArtem Egorov8 years ago111}
112},
113'vscode_showDevMenu': function () {
2ecfbd20Yuri Skorokhodov7 years ago114var NativeModules = getNativeModules();
4dfc9ffdRedMickey5 years ago115if (NativeModules && NativeModules.DevMenu) {
2ecfbd20Yuri Skorokhodov7 years ago116NativeModules.DevMenu.show();
7daed3fcArtem Egorov8 years ago117}
118}
119};
120
121process.on("message", function (message) {
122if (message.data && vscodeHandlers[message.data.method]) {
123vscodeHandlers[message.data.method]();
124} else if(onmessage) {
125onmessage(message);
126}
cc70057dVladimir Kotikov9 years ago127});
7daed3fcArtem Egorov8 years ago128
cc70057dVladimir Kotikov9 years ago129var postMessage = function(message){
130process.send(message);
131};
bb869343Serge Svekolnikov8 years ago132
133if (!self.postMessage) {
134self.postMessage = postMessage;
135}
136
cc70057dVladimir Kotikov9 years ago137var importScripts = (function(){
138var fs=require('fs'), vm=require('vm');
139return function(scriptUrl){
4cf8fdf4Yuri Skorokhodov6 years ago140scriptUrl = fileUrlToPath(scriptUrl);
141var scriptCode = fs.readFileSync(scriptUrl, 'utf8');
e67ace8aYuri Skorokhodov6 years ago142// Add a 'debugger;' statement to stop code execution
143// to wait for the sourcemaps to be processed by the debug adapter
144vm.runInThisContext('debugger;' + scriptCode, {filename: scriptUrl});
cc70057dVladimir Kotikov9 years ago145};
4cf8fdf4Yuri Skorokhodov6 years ago146})();
147`;
cc70057dVladimir Kotikov9 years ago148
cf6dd6b9Yuri Skorokhodov7 years ago149public static CONSOLE_TRACE_PATCH = `// Worker is ran as nodejs process, so console.trace() writes to stderr and it leads to error in native app
150// To avoid this console.trace() is overridden to print stacktrace via console.log()
151// Please, see Node JS implementation: https://github.com/nodejs/node/blob/master/lib/internal/console/constructor.js
152console.trace = (function() {
153return function() {
154try {
155var err = {
156name: 'Trace',
157message: require('util').format.apply(null, arguments)
158};
159// Node uses 10, but usually it's not enough for RN app trace
160Error.stackTraceLimit = 30;
161Error.captureStackTrace(err, console.trace);
162console.log(err.stack);
163} catch (e) {
164console.error(e);
165}
166};
4cf8fdf4Yuri Skorokhodov6 years ago167})();
168`;
cf6dd6b9Yuri Skorokhodov7 years ago169
13a99427Yuri Skorokhodov6 years ago170public 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:
171// https://github.com/pubkey/broadcast-channel/blob/master/src/util.js#L64
172// To avoid it if process.toString() is called if will return empty string instead of [object process].
173var nativeObjectToString = Object.prototype.toString;
174Object.prototype.toString = function() {
175if (this === process) {
176return '';
177} else {
178return nativeObjectToString.call(this);
179}
f4e3ce39Yuri Skorokhodov6 years ago180};
13a99427Yuri Skorokhodov6 years ago181`;
182
039239d1Vladimir Kotikov9 years ago183public static WORKER_DONE = `// Notify debugger that we're done with loading
cc70057dVladimir Kotikov9 years ago184// and started listening for IPC messages
185postMessage({workerLoaded:true});`;
186
bb869343Serge Svekolnikov8 years ago187public static FETCH_STUB = `(function(self) {
4cf8fdf4Yuri Skorokhodov6 years ago188'use strict';
bb869343Serge Svekolnikov8 years ago189
4cf8fdf4Yuri Skorokhodov6 years ago190if (self.fetch) {
191return;
192}
193
194self.fetch = fetch;
bb869343Serge Svekolnikov8 years ago195
4cf8fdf4Yuri Skorokhodov6 years ago196function fetch(url) {
197return new Promise((resolve, reject) => {
198var data = require('fs').readFileSync(fileUrlToPath(url), 'utf8');
199resolve(
200{
201text: function () {
202return data;
203}
bb869343Serge Svekolnikov8 years ago204});
4cf8fdf4Yuri Skorokhodov6 years ago205});
206}
207})(global);
208`;
bb869343Serge Svekolnikov8 years ago209
6eeec3c0Serge Svekolnikov8 years ago210private packagerAddress: string;
e3ae4227digeff10 years ago211private packagerPort: number;
4677921cdigeff10 years ago212private sourcesStoragePath: string;
7daed3fcArtem Egorov8 years ago213private projectRootPath: string;
6eeec3c0Serge Svekolnikov8 years ago214private packagerRemoteRoot?: string;
215private packagerLocalRoot?: string;
cf911877Yuri Skorokhodov7 years ago216private debuggerWorkerUrlPath?: string;
ea8a5f88digeff10 years ago217private socketToApp: WebSocket;
5940f996RedMickey5 years ago218private cancellationToken: vscode.CancellationToken;
5c8365a6Artem Egorov8 years ago219private singleLifetimeWorker: IDebuggeeWorker | null;
3b6023b2Jimmy Thomson10 years ago220private webSocketConstructor: (url: string) => WebSocket;
221
7cc67271digeff10 years ago222private executionLimiter = new ExecutionsLimiter();
ce5e88eeYuri Skorokhodov5 years ago223private nodeFileSystem = new FileSystem();
cc70057dVladimir Kotikov9 years ago224private scriptImporter: ScriptImporter;
7cc67271digeff10 years ago225
6eeec3c0Serge Svekolnikov8 years ago226constructor(
227attachRequestArguments: any,
228sourcesStoragePath: string,
229projectRootPath: string,
5940f996RedMickey5 years ago230cancellationToken: vscode.CancellationToken,
34472878RedMickey5 years ago231{ webSocketConstructor = (url: string) => new WebSocket(url) } = {},
232) {
e45838cbVladimir Kotikov9 years ago233super();
6eeec3c0Serge Svekolnikov8 years ago234this.packagerAddress = attachRequestArguments.address || "localhost";
235this.packagerPort = attachRequestArguments.port;
236this.packagerRemoteRoot = attachRequestArguments.remoteRoot;
237this.packagerLocalRoot = attachRequestArguments.localRoot;
cf911877Yuri Skorokhodov7 years ago238this.debuggerWorkerUrlPath = attachRequestArguments.debuggerWorkerUrlPath;
4677921cdigeff10 years ago239this.sourcesStoragePath = sourcesStoragePath;
7daed3fcArtem Egorov8 years ago240this.projectRootPath = projectRootPath;
5940f996RedMickey5 years ago241this.cancellationToken = cancellationToken;
1758f9a6Yuri Skorokhodov7 years ago242if (!this.sourcesStoragePath)
243throw ErrorHelper.getInternalError(InternalErrorCode.SourcesStoragePathIsNullOrEmpty);
3b6023b2Jimmy Thomson10 years ago244this.webSocketConstructor = webSocketConstructor;
34472878RedMickey5 years ago245this.scriptImporter = new ScriptImporter(
246this.packagerAddress,
247this.packagerPort,
248sourcesStoragePath,
249this.packagerRemoteRoot,
250this.packagerLocalRoot,
251);
4677921cdigeff10 years ago252}
253
0d77292aJiglioNero4 years ago254public async start(retryAttempt: boolean = false): Promise<void> {
34472878RedMickey5 years ago255const errPackagerNotRunning = ErrorHelper.getInternalError(
256InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort,
257this.packagerPort,
258);
0a68f8dbArtem Egorov8 years ago259
0d77292aJiglioNero4 years ago260await ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning);
261// Don't fetch debugger worker on socket disconnect
262if (!retryAttempt) {
263await this.downloadAndPatchDebuggerWorker();
264}
265return this.createSocketToApp(retryAttempt);
ff7dce65digeff10 years ago266}
267
34472878RedMickey5 years ago268public stop(): void {
e45838cbVladimir Kotikov9 years ago269if (this.socketToApp) {
270this.socketToApp.removeAllListeners();
271this.socketToApp.close();
272}
273
274if (this.singleLifetimeWorker) {
275this.singleLifetimeWorker.stop();
276}
277}
278
0d77292aJiglioNero4 years ago279public async downloadAndPatchDebuggerWorker(): Promise<void> {
34472878RedMickey5 years ago280let scriptToRunPath = path.resolve(
281this.sourcesStoragePath,
282ScriptImporter.DEBUGGER_WORKER_FILENAME,
283);
0d77292aJiglioNero4 years ago284
285await this.scriptImporter.downloadDebuggerWorker(
286this.sourcesStoragePath,
287this.projectRootPath,
288this.debuggerWorkerUrlPath,
289);
290const workerContent = await this.nodeFileSystem.readFile(scriptToRunPath, "utf8");
291const isHaulProject = ReactNativeProjectHelper.isHaulProject(this.projectRootPath);
292// Add our customizations to debugger worker to get it working smoothly
293// in Node env and polyfill WebWorkers API over Node's IPC.
294const modifiedDebuggeeContent = [
295MultipleLifetimesAppWorker.WORKER_BOOTSTRAP,
296MultipleLifetimesAppWorker.CONSOLE_TRACE_PATCH,
297MultipleLifetimesAppWorker.PROCESS_TO_STRING_PATCH,
298isHaulProject ? MultipleLifetimesAppWorker.FETCH_STUB : null,
299workerContent,
300MultipleLifetimesAppWorker.WORKER_DONE,
301].join("\n");
302return this.nodeFileSystem.writeFile(scriptToRunPath, modifiedDebuggeeContent);
cc70057dVladimir Kotikov9 years ago303}
304
7e74daf7Yuri Skorokhodov6 years ago305public showDevMenuCommand(): void {
306if (this.singleLifetimeWorker) {
307this.singleLifetimeWorker.postMessage({
308method: "vscode_showDevMenu",
309});
310}
311}
312
313public reloadAppCommand(): void {
314if (this.singleLifetimeWorker) {
315this.singleLifetimeWorker.postMessage({
316method: "vscode_reloadApp",
317});
318}
319}
320
0d77292aJiglioNero4 years ago321private async startNewWorkerLifetime(): Promise<void> {
34472878RedMickey5 years ago322this.singleLifetimeWorker = new ForkedAppWorker(
323this.packagerAddress,
324this.packagerPort,
325this.sourcesStoragePath,
326this.projectRootPath,
327message => {
6eeec3c0Serge Svekolnikov8 years ago328this.sendMessageToApp(message);
329},
34472878RedMickey5 years ago330this.packagerRemoteRoot,
331this.packagerLocalRoot,
332);
0a68f8dbArtem Egorov8 years ago333logger.verbose("A new app worker lifetime was created.");
0d77292aJiglioNero4 years ago334const startedEvent = await this.singleLifetimeWorker.start();
335this.emit("connected", startedEvent);
4677921cdigeff10 years ago336}
337
0d77292aJiglioNero4 years ago338private async createSocketToApp(retryAttempt: boolean = false): Promise<void> {
ce5e88eeYuri Skorokhodov5 years ago339return new Promise((resolve, reject) => {
340this.socketToApp = this.webSocketConstructor(this.debuggerProxyUrl());
341this.socketToApp.on("open", () => {
342this.onSocketOpened();
299b0557Patricio Beltran10 years ago343});
34472878RedMickey5 years ago344this.socketToApp.on("close", () => {
345this.executionLimiter.execute("onSocketClose.msg", /*limitInSeconds*/ 10, () => {
346/*
347* It is not the best idea to compare with the message, but this is the only thing React Native gives that is unique when
348* it closes the socket because it already has a connection to a debugger.
349* https://github.com/facebook/react-native/blob/588f01e9982775f0699c7bfd56623d4ed3949810/local-cli/server/util/webSocketProxy.js#L38
350*/
351let msgKey = "_closeMessage";
352if (this.socketToApp[msgKey] === "Another debugger is already connected") {
353reject(
354ErrorHelper.getInternalError(
355InternalErrorCode.AnotherDebuggerConnectedToPackager,
356),
357);
ce5e88eeYuri Skorokhodov5 years ago358}
34472878RedMickey5 years ago359logger.log(
360localize(
361"DisconnectedFromThePackagerToReactNative",
362"Disconnected from the Proxy (Packager) to the React Native application. Retrying reconnection soon...",
363),
364);
ce5e88eeYuri Skorokhodov5 years ago365});
5940f996RedMickey5 years ago366if (!this.cancellationToken.isCancellationRequested) {
367setTimeout(() => {
368this.start(true /* retryAttempt */);
369}, 100);
370}
34472878RedMickey5 years ago371});
372this.socketToApp.on("message", (message: any) => this.onMessage(message));
373this.socketToApp.on("error", (error: Error) => {
374if (retryAttempt) {
375printDebuggingError(
376ErrorHelper.getInternalError(
377InternalErrorCode.ReconnectionToPackagerFailedCheckForErrorsOrRestartReactNative,
378),
379error,
380);
381}
382
383reject(error);
384});
32cab018Meena Kunnathur Balakrishnan10 years ago385
ce5e88eeYuri Skorokhodov5 years ago386// In an attempt to catch failures in starting the packager on first attempt,
387// wait for 300 ms before resolving the promise
259c018fYuri Skorokhodov5 years ago388PromiseUtil.delay(300).then(() => resolve());
ce5e88eeYuri Skorokhodov5 years ago389});
4677921cdigeff10 years ago390}
391
392private debuggerProxyUrl() {
6eeec3c0Serge Svekolnikov8 years ago393return `ws://${this.packagerAddress}:${this.packagerPort}/debugger-proxy?role=debugger&name=vscode`;
4677921cdigeff10 years ago394}
395
ea8a5f88digeff10 years ago396private onSocketOpened() {
7cc67271digeff10 years ago397this.executionLimiter.execute("onSocketOpened.msg", /*limitInSeconds*/ 10, () =>
34472878RedMickey5 years ago398logger.log(
399localize(
400"EstablishedConnectionWithPackagerToReactNativeApp",
401"Established a connection with the Proxy (Packager) to the React Native application",
402),
403),
404);
4677921cdigeff10 years ago405}
406
9174feb7Vladimir Kotikov9 years ago407private killWorker() {
408if (!this.singleLifetimeWorker) return;
409this.singleLifetimeWorker.stop();
410this.singleLifetimeWorker = null;
411}
e7b314e8Vladimir Kotikov9 years ago412
9174feb7Vladimir Kotikov9 years ago413private onMessage(message: string) {
5d4d4de0digeff10 years ago414try {
0a68f8dbArtem Egorov8 years ago415logger.verbose("From RN APP: " + message);
ea8a5f88digeff10 years ago416let object = <RNAppMessage>JSON.parse(message);
5d4d4de0digeff10 years ago417if (object.method === "prepareJSRuntime") {
e7b314e8Vladimir Kotikov9 years ago418// In RN 0.40 Android runtime doesn't seem to be sending "$disconnected" event
419// when user reloads an app, hence we need to try to kill it here either.
9174feb7Vladimir Kotikov9 years ago420this.killWorker();
5d4d4de0digeff10 years ago421// The MultipleLifetimesAppWorker will handle prepareJSRuntime aka create new lifetime
422this.gotPrepareJSRuntime(object);
ff7dce65digeff10 years ago423} else if (object.method === "$disconnected") {
424// We need to shutdown the current app worker, and create a new lifetime
9174feb7Vladimir Kotikov9 years ago425this.killWorker();
5d4d4de0digeff10 years ago426} else if (object.method) {
427// All the other messages are handled by the single lifetime worker
5c8365a6Artem Egorov8 years ago428if (this.singleLifetimeWorker) {
429this.singleLifetimeWorker.postMessage(object);
430}
5d4d4de0digeff10 years ago431} else {
ea8a5f88digeff10 years ago432// Message doesn't have a method. Ignore it. This is an info message instead of warn because it's normal and expected
34472878RedMickey5 years ago433logger.verbose(
434`The react-native app sent a message without specifying a method: ${message}`,
435);
5d4d4de0digeff10 years ago436}
437} catch (exception) {
34472878RedMickey5 years ago438printDebuggingError(
439ErrorHelper.getInternalError(
440InternalErrorCode.FailedToProcessMessageFromReactNativeApp,
441message,
442),
443exception,
444);
4677921cdigeff10 years ago445}
446}
447
448private gotPrepareJSRuntime(message: any): void {
449// Create the sandbox, and replay that we finished processing the message
34472878RedMickey5 years ago450this.startNewWorkerLifetime().then(
451() => {
452this.sendMessageToApp({ replyID: parseInt(message.id, 10) });
453},
454error =>
455printDebuggingError(
456ErrorHelper.getInternalError(
457InternalErrorCode.FailedToPrepareJSRuntimeEnvironment,
458message,
459),
460error,
461),
462);
4677921cdigeff10 years ago463}
464
ff7dce65digeff10 years ago465private sendMessageToApp(message: any): void {
5c8365a6Artem Egorov8 years ago466let stringified: string = "";
354c28a1digeff10 years ago467try {
468stringified = JSON.stringify(message);
d124bf0eYuri Skorokhodov7 years ago469logger.verbose(`To RN APP: ${stringified}`);
354c28a1digeff10 years ago470this.socketToApp.send(stringified);
471} catch (exception) {
34472878RedMickey5 years ago472let messageToShow = stringified || "" + message; // Try to show the stringified version, but show the toString if unavailable
473printDebuggingError(
474ErrorHelper.getInternalError(
475InternalErrorCode.FailedToSendMessageToTheReactNativeApp,
476messageToShow,
477),
478exception,
479);
354c28a1digeff10 years ago480}
4677921cdigeff10 years ago481}
482}