-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (26 loc) · 598 Bytes
/
index.js
File metadata and controls
32 lines (26 loc) · 598 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import net from 'node:net';
export default async function isPortReachable(port, {host, timeout = 1000} = {}) {
if (typeof host !== 'string') {
throw new TypeError('Specify a `host`');
}
const promise = new Promise(((resolve, reject) => {
const socket = new net.Socket();
const onError = () => {
socket.destroy();
reject();
};
socket.setTimeout(timeout);
socket.once('error', onError);
socket.once('timeout', onError);
socket.connect(port, host, () => {
socket.end();
resolve();
});
}));
try {
await promise;
return true;
} catch {
return false;
}
}