[react-packager] Cleanup socket interface code

This commit is contained in:
Amjad Masad 2015-08-25 17:39:42 -07:00
parent 782aafd16a
commit 84f498cd2b
7 changed files with 44 additions and 52 deletions

View File

@ -60,35 +60,7 @@ exports.createClientFor = function(options) {
return SocketInterface.getOrCreateSocketFor(options); return SocketInterface.getOrCreateSocketFor(options);
}; };
process.on('message', function(m) { SocketInterface.listenOnServerMessages();
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();
}
});
function useGracefulFs() { function useGracefulFs() {
var fs = require('fs'); var fs = require('fs');

View File

@ -23,7 +23,7 @@ const readFile = Promise.denodeify(fs.readFile);
const MAX_CALLS_PER_WORKER = 600; const MAX_CALLS_PER_WORKER = 600;
// Worker will timeout if one of the callers timeout. // Worker will timeout if one of the callers timeout.
const DEFAULT_MAX_CALL_TIME = 30000; const DEFAULT_MAX_CALL_TIME = 60000;
const validateOpts = declareOpts({ const validateOpts = declareOpts({
projectRoots: { projectRoots: {

View File

@ -73,15 +73,15 @@ class SocketClient {
const resolver = this._resolvers[message.id]; const resolver = this._resolvers[message.id];
if (!resolver) { if (!resolver) {
throw new Error( 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]; delete this._resolvers[message.id];
if (message.type === 'error') { if (message.type === 'error') {
// TODO convert to an error resolver.reject(new Error(message.data));
resolver.reject(message.data);
} else { } else {
resolver.resolve(message.data); resolver.resolve(message.data);
} }

View File

@ -54,13 +54,13 @@ class SocketServer {
const bunser = new bser.BunserBuf(); const bunser = new bser.BunserBuf();
sock.on('data', (buf) => bunser.append(buf)); sock.on('data', (buf) => bunser.append(buf));
bunser.on('value', (m) => this._handleMessage(sock, m)); bunser.on('value', (m) => this._handleMessage(sock, m));
} }
_handleMessage(sock, m) { _handleMessage(sock, m) {
if (!m || !m.id || !m.data) { if (!m || !m.id || !m.data) {
console.error('SocketServer recieved a malformed message: %j', m); console.error('SocketServer recieved a malformed message: %j', m);
return;
} }
debug('got request', m); debug('got request', m);
@ -119,9 +119,38 @@ class SocketServer {
this._dieEventually(); this._dieEventually();
}, MAX_IDLE_TIME); }, 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. // TODO move this to bser code.
function toJSON(object) { function toJSON(object) {
@ -139,3 +168,5 @@ function toJSON(object) {
return object; return object;
} }
module.exports = SocketServer;

View File

@ -107,6 +107,6 @@ describe('SocketClient', () => {
data: 'some error' data: 'some error'
}); });
return promise.catch(m => expect(m).toBe('some error')); return promise.catch(m => expect(m.message).toBe('some error'));
}); });
}); });

View File

@ -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'] });
});
});
}); });

View File

@ -94,9 +94,10 @@ const SocketInterface = {
}); });
}, },
createSocketServer(sockPath, options) { listenOnServerMessages() {
return new SocketServer(sockPath, options).onReady(); return SocketServer.listenOnServerIPCMessages();
} }
}; };
module.exports = SocketInterface; module.exports = SocketInterface;