mirror of https://github.com/status-im/metro.git
[react-packager] Make sure server is listening on socket
This commit is contained in:
parent
dbdf7f366f
commit
971b70d72f
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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,66 +39,76 @@ const SocketInterface = {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (fs.existsSync(sockPath)) {
|
if (fs.existsSync(sockPath)) {
|
||||||
resolve(SocketClient.create(sockPath));
|
var sock = net.connect(sockPath);
|
||||||
return;
|
sock.on('connect', () => {
|
||||||
}
|
sock.end();
|
||||||
|
|
||||||
const logPath = path.join(tmpdir, 'react-packager.log');
|
|
||||||
|
|
||||||
const timeout = setTimeout(
|
|
||||||
() => reject(
|
|
||||||
new Error(
|
|
||||||
'Took too long to start server. Server logs: \n' +
|
|
||||||
fs.readFileSync(logPath, 'utf8')
|
|
||||||
)
|
|
||||||
),
|
|
||||||
CREATE_SERVER_TIMEOUT,
|
|
||||||
);
|
|
||||||
|
|
||||||
const log = fs.openSync(logPath, 'a');
|
|
||||||
|
|
||||||
// Enable server debugging by default since it's going to a log file.
|
|
||||||
const env = _.clone(process.env);
|
|
||||||
env.DEBUG = 'ReactPackager:SocketServer';
|
|
||||||
|
|
||||||
// We have to go through the main entry point to make sure
|
|
||||||
// we go through the babel require hook.
|
|
||||||
const child = spawn(
|
|
||||||
process.execPath,
|
|
||||||
[path.join(__dirname, '..', '..', 'index.js')],
|
|
||||||
{
|
|
||||||
detached: true,
|
|
||||||
env: env,
|
|
||||||
stdio: ['ipc', log, log]
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
child.unref();
|
|
||||||
|
|
||||||
child.on('message', m => {
|
|
||||||
if (m && m.type && m.type === 'createdServer') {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
child.disconnect();
|
|
||||||
resolve(SocketClient.create(sockPath));
|
resolve(SocketClient.create(sockPath));
|
||||||
}
|
});
|
||||||
});
|
sock.on('error', (e) => {
|
||||||
|
fs.unlinkSync(sockPath);
|
||||||
|
createServer(resolve, reject, options, sockPath);
|
||||||
if (options.blacklistRE) {
|
});
|
||||||
options.blacklistRE = { source: options.blacklistRE.source };
|
} else {
|
||||||
|
createServer(resolve, reject, options, sockPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
child.send({
|
|
||||||
type: 'createSocketServer',
|
|
||||||
data: { sockPath, options }
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
listenOnServerMessages() {
|
listenOnServerMessages() {
|
||||||
return SocketServer.listenOnServerIPCMessages();
|
return SocketServer.listenOnServerIPCMessages();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function createServer(resolve, reject, options, sockPath) {
|
||||||
|
const logPath = path.join(tmpdir, 'react-packager.log');
|
||||||
|
|
||||||
|
const timeout = setTimeout(
|
||||||
|
() => reject(
|
||||||
|
new Error(
|
||||||
|
'Took too long to start server. Server logs: \n' +
|
||||||
|
fs.readFileSync(logPath, 'utf8')
|
||||||
|
)
|
||||||
|
),
|
||||||
|
CREATE_SERVER_TIMEOUT,
|
||||||
|
);
|
||||||
|
|
||||||
|
const log = fs.openSync(logPath, 'a');
|
||||||
|
|
||||||
|
// Enable server debugging by default since it's going to a log file.
|
||||||
|
const env = _.clone(process.env);
|
||||||
|
env.DEBUG = 'ReactPackager:SocketServer';
|
||||||
|
|
||||||
|
// We have to go through the main entry point to make sure
|
||||||
|
// we go through the babel require hook.
|
||||||
|
const child = spawn(
|
||||||
|
process.execPath,
|
||||||
|
[path.join(__dirname, '..', '..', 'index.js')],
|
||||||
|
{
|
||||||
|
detached: true,
|
||||||
|
env: env,
|
||||||
|
stdio: ['ipc', log, log]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
child.unref();
|
||||||
|
|
||||||
|
child.on('message', m => {
|
||||||
|
if (m && m.type && m.type === 'createdServer') {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
child.disconnect();
|
||||||
|
|
||||||
|
resolve(SocketClient.create(sockPath));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.blacklistRE) {
|
||||||
|
options.blacklistRE = { source: options.blacklistRE.source };
|
||||||
|
}
|
||||||
|
|
||||||
|
child.send({
|
||||||
|
type: 'createSocketServer',
|
||||||
|
data: { sockPath, options }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = SocketInterface;
|
module.exports = SocketInterface;
|
||||||
|
|
Loading…
Reference in New Issue