[react-packager] Make sure server is listening on socket

This commit is contained in:
Martín Bigio 2015-08-26 16:30:54 -07:00
parent dbdf7f366f
commit 971b70d72f
2 changed files with 75 additions and 52 deletions

View File

@ -26,6 +26,17 @@ describe('SocketInterface', () => {
pit('creates socket path by hashing options', () => { pit('creates socket path by hashing options', () => {
const fs = require('fs'); const fs = require('fs');
fs.existsSync = jest.genMockFn().mockImpl(() => true); fs.existsSync = jest.genMockFn().mockImpl(() => true);
fs.unlinkSync = jest.genMockFn();
let callback;
require('child_process').spawn.mockImpl(() => ({
on: (event, cb) => callback = cb,
send: (message) => {
setImmediate(() => callback({ type: 'createdServer' }));
},
unref: () => undefined,
disconnect: () => undefined,
}));
// Check that given two equivelant server options, we end up with the same // Check that given two equivelant server options, we end up with the same
// socket path. // socket path.
@ -49,6 +60,7 @@ describe('SocketInterface', () => {
pit('should fork a server', () => { pit('should fork a server', () => {
const fs = require('fs'); const fs = require('fs');
fs.existsSync = jest.genMockFn().mockImpl(() => false); fs.existsSync = jest.genMockFn().mockImpl(() => false);
fs.unlinkSync = jest.genMockFn();
let sockPath; let sockPath;
let callback; let callback;

View File

@ -14,6 +14,7 @@ const SocketServer = require('./SocketServer');
const _ = require('underscore'); const _ = require('underscore');
const crypto = require('crypto'); const crypto = require('crypto');
const fs = require('fs'); const fs = require('fs');
const net = require('net');
const path = require('path'); const path = require('path');
const tmpdir = require('os').tmpdir(); const tmpdir = require('os').tmpdir();
const {spawn} = require('child_process'); const {spawn} = require('child_process');
@ -38,10 +39,27 @@ const SocketInterface = {
); );
if (fs.existsSync(sockPath)) { if (fs.existsSync(sockPath)) {
var sock = net.connect(sockPath);
sock.on('connect', () => {
sock.end();
resolve(SocketClient.create(sockPath)); resolve(SocketClient.create(sockPath));
return; });
sock.on('error', (e) => {
fs.unlinkSync(sockPath);
createServer(resolve, reject, options, sockPath);
});
} else {
createServer(resolve, reject, options, sockPath);
} }
});
},
listenOnServerMessages() {
return SocketServer.listenOnServerIPCMessages();
}
};
function createServer(resolve, reject, options, sockPath) {
const logPath = path.join(tmpdir, 'react-packager.log'); const logPath = path.join(tmpdir, 'react-packager.log');
const timeout = setTimeout( const timeout = setTimeout(
@ -78,11 +96,11 @@ const SocketInterface = {
if (m && m.type && m.type === 'createdServer') { if (m && m.type && m.type === 'createdServer') {
clearTimeout(timeout); clearTimeout(timeout);
child.disconnect(); child.disconnect();
resolve(SocketClient.create(sockPath)); resolve(SocketClient.create(sockPath));
} }
}); });
if (options.blacklistRE) { if (options.blacklistRE) {
options.blacklistRE = { source: options.blacklistRE.source }; options.blacklistRE = { source: options.blacklistRE.source };
} }
@ -91,13 +109,6 @@ const SocketInterface = {
type: 'createSocketServer', type: 'createSocketServer',
data: { sockPath, options } data: { sockPath, options }
}); });
}); }
},
listenOnServerMessages() {
return SocketServer.listenOnServerIPCMessages();
}
};
module.exports = SocketInterface; module.exports = SocketInterface;