react-native/packager/webSocketProxy.js
Spencer Elliott c1faabcdf6 Replace applescript with https://github.com/sindresorhus/opn
Summary: This allows opening the Chrome debugger on OS X, Linux, and Windows, and succeeds the previous PR which used [browser-launcher2](https://github.com/benderjs/browser-launcher2) and included a `--dangerouslyDisableChromeDebuggerWebSecurity` option: https://github.com/facebook/react-native/pull/2406

[opn](https://github.com/sindresorhus/opn) is cross-platform and much simpler than browser-launcher2 (since we don't have to manage the opened Chrome instance; the OS will just use the default instance).
Closes https://github.com/facebook/react-native/pull/3394

Reviewed By: mkonicek

Differential Revision: D2550996

Pulled By: frantic

fb-gh-sync-id: fa4cbe55542562f30f77e0a6ab4bc53980ee13aa
2015-10-23 11:32:35 -07:00

73 lines
1.7 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
function attachToServer(server, path) {
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: server,
path: path
});
var clients = [];
function sendSpecial(message) {
clients.forEach(function (cn) {
try {
cn.send(JSON.stringify(message));
} catch(e) {
// Sometimes this call throws 'not opened'
}
});
}
wss.on('connection', function(ws) {
var id = Math.random().toString(15).slice(10, 20);
sendSpecial({$open: id});
clients.push(ws);
var allClientsExcept = function(ws) {
return clients.filter(function(cn) { return cn !== ws; });
};
ws.onerror = function() {
clients = allClientsExcept(ws);
sendSpecial({$error: id});
};
ws.onclose = function() {
clients = allClientsExcept(ws);
sendSpecial({$close: id});
};
ws.on('message', function(message) {
allClientsExcept(ws).forEach(function(cn) {
try {
cn.send(message);
} catch(e) {
// Sometimes this call throws 'not opened'
}
});
});
});
return {
server: wss,
isChromeConnected: () =>
clients
.map(ws => ws.upgradeReq.headers['user-agent'])
.filter(Boolean)
.some(userAgent => userAgent.includes('Chrome'))
};
}
module.exports = {
attachToServer: attachToServer
};