[react-packager] Cleanup socket interface code
This commit is contained in:
parent
2b7dd4650f
commit
074069e465
|
@ -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');
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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'] });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue