Skip to content

Commit

Permalink
Replace applescript with browser-launcher2
Browse files Browse the repository at this point in the history
This allows...

1. launching Chrome on platforms other than OS X
2. users to launch their own instance of Chrome (e.g. via command line)
   rather than being forced to use the default instance (i.e.
   `tell application "Chrome"` always used the default instance)

`isDebuggerConnected()` addresses the problem in facebook#510 where the dev tools
would only open once per server session.

Add a '--dangerouslyDisableChromeDebuggerWebSecurity' flag to
packager.js to enable Chrome '--disable-web-security' flag.

This allows users to inspect network requests in Chrome by commenting
the xhr polyfill in InitializeJavaScriptAppEngine.js:
  facebook#934 (comment)

Usage:

    node packager.js --dangerouslyDisableChromeDebuggerWebSecurity

or:

    packager.sh --dangerouslyDisableChromeDebuggerWebSecurity
  • Loading branch information
elliottsj committed Oct 14, 2015
1 parent f03c7b5 commit dd8d641
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"absolute-path": "^0.0.0",
"babel": "^5.8.23",
"babel-core": "^5.8.23",
"browser-launcher2": "0.4.6",
"bser": "^1.0.2",
"chalk": "^1.1.1",
"connect": "^2.8.3",
Expand Down
79 changes: 66 additions & 13 deletions packager/getDevToolsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,49 @@
var execFile = require('child_process').execFile;
var fs = require('fs');
var path = require('path');
var launcher = require('browser-launcher2');

module.exports = function(options) {
function closeChromeInstance(instance) {
return new Promise(function(resolve, reject) {
if (!instance) {
resolve();
return;
}
instance.stop(function(err) {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

function launchChrome(url, options) {
return new Promise(function(resolve, reject) {
launcher(function(err, launch) {
if (err) {
console.error('Failed to initialize browser-launcher2', err);
reject(err);
return;
}
launch(url, {
browser: 'chrome',
options: options,
}, function(err, instance) {
if (err) {
console.error('Failed to launch chrome', err);
reject(err);
return;
}
resolve(instance);
});
});
});
}

module.exports = function(options, isDebuggerConnected) {
var chromeInstance;
return function(req, res, next) {
if (req.url === '/debugger-ui') {
var debuggerPath = path.join(__dirname, 'debugger.html');
Expand All @@ -29,20 +70,32 @@ module.exports = function(options) {
'If you still need this, please let us know.'
);
} else if (req.url === '/launch-chrome-devtools') {
if (isDebuggerConnected()) {
// Dev tools are already open; no need to open another session
res.end('OK');
return;
}
var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
var script = 'launchChromeDevTools.applescript';
var chromeOptions =
options.dangerouslyDisableChromeDebuggerWebSecurity ?
['--disable-web-security'] :
[];
console.log('Launching Dev Tools...');
execFile(
path.join(__dirname, script), [debuggerURL],
function(err, stdout, stderr) {
if (err) {
console.log('Failed to run ' + script, err);
}
console.log(stdout);
console.warn(stderr);
}
);
res.end('OK');
closeChromeInstance(chromeInstance)
.then(function() {
return launchChrome(debuggerURL, chromeOptions)
})
.then(function(instance) {
// Keep a reference to the Chrome instance and unset it if Chrome stops
chromeInstance = instance;
chromeInstance.on('stop', function() {
chromeInstance = null;
});
res.end('OK');
})
.catch(function(err) {
next(err);
});
} else {
next();
}
Expand Down
47 changes: 0 additions & 47 deletions packager/launchChromeDevTools.applescript

This file was deleted.

13 changes: 11 additions & 2 deletions packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ var options = parseCommandLine([{
command: 'reset-cache',
description: 'Removes cached files',
default: false,
}, {
command: 'dangerouslyDisableChromeDebuggerWebSecurity',
description: 'Disable the Chrome debugger\'s same-origin policy'
}]);

if (options.projectRoots) {
Expand Down Expand Up @@ -160,7 +163,13 @@ var server = runServer(options, function() {
console.log('\nReact packager ready.\n');
});

webSocketProxy.attachToServer(server, '/debugger-proxy');
var proxy = webSocketProxy.attachToServer(server, '/debugger-proxy');

function isDebuggerConnected() {
// Debugger is connected if the app and at least one browser are connected
// to the websocket proxy.
return proxy.clientsCount() >= 2;
}

function getAppMiddleware(options) {
var transformerPath = options.transformer;
Expand Down Expand Up @@ -191,7 +200,7 @@ function runServer(
) {
var app = connect()
.use(loadRawBodyMiddleware)
.use(getDevToolsMiddleware(options))
.use(getDevToolsMiddleware(options, isDebuggerConnected))
.use(openStackFrameInEditorMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
Expand Down
7 changes: 6 additions & 1 deletion packager/webSocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ function attachToServer(server, path) {
});
});

return wss;
return {
server: wss,
clientsCount: function() {
return clients.length;
}
};
}

module.exports = {
Expand Down

0 comments on commit dd8d641

Please sign in to comment.