diff --git a/react-packager/index.js b/react-packager/index.js index c3e5829f..f399eae0 100644 --- a/react-packager/index.js +++ b/react-packager/index.js @@ -60,35 +60,7 @@ exports.createClientFor = function(options) { return SocketInterface.getOrCreateSocketFor(options); }; -process.on('message', function(m) { - if (m && m.type && m.type === 'createSocketServer') { - console.log('server got ipc message', m); - var options = m.data.options; - - // regexp doesn't naturally serialize to json. - options.blacklistRE = new RegExp(options.blacklistRE.source); - - SocketInterface.createSocketServer( - m.data.sockPath, - m.data.options - ).then( - function() { - console.log('succesfully created server', m); - process.send({ type: 'createdServer' }); - }, - function(error) { - console.log('error creating server', error.code); - if (error.code === 'EADDRINUSE') { - // Server already listening, this may happen if multiple - // clients where started in quick succussion (buck). - process.send({ type: 'createdServer' }); - } else { - throw error; - } - } - ).done(); - } -}); +SocketInterface.listenOnServerMessages(); function useGracefulFs() { var fs = require('fs'); diff --git a/react-packager/src/JSTransformer/index.js b/react-packager/src/JSTransformer/index.js index 6906d69f..47019846 100644 --- a/react-packager/src/JSTransformer/index.js +++ b/react-packager/src/JSTransformer/index.js @@ -23,7 +23,7 @@ const readFile = Promise.denodeify(fs.readFile); const MAX_CALLS_PER_WORKER = 600; // Worker will timeout if one of the callers timeout. -const DEFAULT_MAX_CALL_TIME = 30000; +const DEFAULT_MAX_CALL_TIME = 60000; const validateOpts = declareOpts({ projectRoots: { diff --git a/react-packager/src/SocketInterface/SocketClient.js b/react-packager/src/SocketInterface/SocketClient.js index 474223ad..30b599b0 100644 --- a/react-packager/src/SocketInterface/SocketClient.js +++ b/react-packager/src/SocketInterface/SocketClient.js @@ -73,15 +73,15 @@ class SocketClient { const resolver = this._resolvers[message.id]; if (!resolver) { throw new Error( - 'Unrecognized message id (message already resolved or never existed' + 'Unrecognized message id (' + message.id + ') ' + + 'message already resolved or never existed.' ); } delete this._resolvers[message.id]; if (message.type === 'error') { - // TODO convert to an error - resolver.reject(message.data); + resolver.reject(new Error(message.data)); } else { resolver.resolve(message.data); } diff --git a/react-packager/src/SocketInterface/SocketServer.js b/react-packager/src/SocketInterface/SocketServer.js index 693c8e0c..f6faf836 100644 --- a/react-packager/src/SocketInterface/SocketServer.js +++ b/react-packager/src/SocketInterface/SocketServer.js @@ -54,13 +54,13 @@ class SocketServer { const bunser = new bser.BunserBuf(); sock.on('data', (buf) => bunser.append(buf)); - bunser.on('value', (m) => this._handleMessage(sock, m)); } _handleMessage(sock, m) { if (!m || !m.id || !m.data) { console.error('SocketServer recieved a malformed message: %j', m); + return; } debug('got request', m); @@ -119,9 +119,38 @@ class SocketServer { this._dieEventually(); }, MAX_IDLE_TIME); } -} -module.exports = SocketServer; + static listenOnServerIPCMessages() { + process.on('message', (message) => { + if (!(message && message.type && message.type === 'createSocketServer')) { + return; + } + + debug('server got ipc message', message); + + const {options, sockPath} = message.data; + // regexp doesn't naturally serialize to json. + options.blacklistRE = new RegExp(options.blacklistRE.source); + + new SocketServer(sockPath, options).onReady().then( + () => { + debug('succesfully created server'); + process.send({ type: 'createdServer' }); + }, + error => { + debug('error creating server', error.code); + if (error.code === 'EADDRINUSE') { + // Server already listening, this may happen if multiple + // clients where started in quick succussion (buck). + process.send({ type: 'createdServer' }); + } else { + throw error; + } + } + ).done(); + }); + } +} // TODO move this to bser code. function toJSON(object) { @@ -139,3 +168,5 @@ function toJSON(object) { return object; } + +module.exports = SocketServer; diff --git a/react-packager/src/SocketInterface/__tests__/SocketClient-test.js b/react-packager/src/SocketInterface/__tests__/SocketClient-test.js index b77d92a0..c898e927 100644 --- a/react-packager/src/SocketInterface/__tests__/SocketClient-test.js +++ b/react-packager/src/SocketInterface/__tests__/SocketClient-test.js @@ -107,6 +107,6 @@ describe('SocketClient', () => { data: 'some error' }); - return promise.catch(m => expect(m).toBe('some error')); + return promise.catch(m => expect(m.message).toBe('some error')); }); }); diff --git a/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js b/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js index f0940023..c03f3999 100644 --- a/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js +++ b/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js @@ -72,16 +72,4 @@ describe('SocketInterface', () => { }); }); }); - - describe('createSocketServer', () => { - pit('creates a server', () => { - require('../SocketServer').mockImpl((sockPath, options) => { - expect(sockPath).toBe('/socket'); - expect(options).toEqual({ projectRoots: ['/root'] }); - return { onReady: () => Promise.resolve() }; - }); - - return SocketInterface.createSocketServer('/socket', { projectRoots: ['/root'] }); - }); - }); }); diff --git a/react-packager/src/SocketInterface/index.js b/react-packager/src/SocketInterface/index.js index def08090..470d0fc2 100644 --- a/react-packager/src/SocketInterface/index.js +++ b/react-packager/src/SocketInterface/index.js @@ -72,7 +72,7 @@ const SocketInterface = { } ); - child.unref(); + child.unref(); child.on('message', m => { if (m && m.type && m.type === 'createdServer') { @@ -94,9 +94,10 @@ const SocketInterface = { }); }, - createSocketServer(sockPath, options) { - return new SocketServer(sockPath, options).onReady(); + listenOnServerMessages() { + return SocketServer.listenOnServerIPCMessages(); } + }; module.exports = SocketInterface;