You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.0 KiB
JavaScript
58 lines
1.0 KiB
JavaScript
6 years ago
|
'use strict';
|
||
|
|
||
|
const net = require("net");
|
||
|
const Promise = require("bluebird");
|
||
|
|
||
|
function listening(port, cb) {
|
||
|
let done = false;
|
||
|
let sock = new net.Socket();
|
||
|
sock.setTimeout(50);
|
||
|
sock.on("connect", () => {
|
||
|
done = true;
|
||
|
cb(null, true);
|
||
|
sock.destroy();
|
||
|
}).on("timeout", () => {
|
||
|
if (done) { return; }
|
||
|
cb(null, false);
|
||
|
}).on("error", (err) => {
|
||
|
if (done) { return; }
|
||
|
cb(null, false);
|
||
|
}).connect(port);
|
||
|
}
|
||
|
|
||
|
function waitForServer(port, cb) {
|
||
|
setTimeout(() => {
|
||
|
listening(port, (err, isListening) => {
|
||
|
if (err != null) {
|
||
|
cb(err);
|
||
|
} else {
|
||
|
if (isListening) {
|
||
|
cb(null, true);
|
||
|
} else {
|
||
|
waitForServer(port, cb);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}, 100);
|
||
|
}
|
||
|
|
||
|
module.exports = function serverWaiter(port) {
|
||
|
let isWaiting = false;
|
||
|
|
||
|
return {
|
||
|
reportRestart: function() {
|
||
|
isWaiting = true;
|
||
|
},
|
||
|
isWaiting: function() {
|
||
|
return isWaiting;
|
||
|
},
|
||
|
wait: function() {
|
||
|
return Promise.try(() => {
|
||
|
return Promise.promisify(waitForServer)(port);
|
||
|
}).then((result) => {
|
||
|
isWaiting = false;
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|