simpler wait-for-port

This commit is contained in:
Rich Harris
2018-03-04 11:25:59 -05:00
committed by GitHub
parent 4d79cb81ed
commit bdb9d49187
4 changed files with 25 additions and 26 deletions

View File

@@ -1,24 +1,25 @@
import waitPort from 'wait-port';
import * as net from 'net';
export function wait_for_port(port: number, cb: () => void) {
waitPort({ port }).then(cb);
export function wait_for_port(port: number, timeout = 5000) {
return new Promise((fulfil, reject) => {
get_connection(port, fulfil);
setTimeout(() => reject(new Error(`timed out waiting for connection`)), timeout);
});
}
// import * as net from 'net';
export function get_connection(port: number, cb: () => void) {
const socket = net.createConnection(port, 'localhost', () => {
cb();
socket.destroy();
});
// export function wait_for_port(port: number, cb: () => void) {
// const socket = net.createConnection(port, 'localhost', () => {
// cb();
// socket.destroy();
// });
socket.on('error', err => {
setTimeout(() => {
get_connection(port, cb);
}, 10);
});
// socket.on('error', err => {
// setTimeout(() => {
// wait_for_port(port, cb);
// }, 100);
// });
// setTimeout(() => {
// socket.destroy();
// }, 100);
// }
setTimeout(() => {
socket.destroy();
}, 1000);
}