This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (51 loc) · 1.6 KB
/
index.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const Net = require('net');
const Performance = require('perf_hooks').performance;
const defaults = {
timeout: 1000
};
module.exports = (options = {}) => {
const config = getConfig();
return (domain) => {
return new Promise((resolve => {
const socket = new Net.Socket();
let opened;
let closed;
let status = null;
socket.on('connect', function () {
opened = Performance.now();
status = 'open';
setTimeout(() => { socket.destroy(); }, 10);
})
socket.setTimeout(config.timeout);
socket.on('timeout', function () {
status = 'closed';
socket.destroy();
})
socket.on('error', function () {
/* $lab:coverage:off$ */
status = 'closed';
/* $lab:coverage:on$ */
})
socket.on('close', function () {
closed = Performance.now();
const connectionReset = closed - opened < 10;
resolve(status === 'open' && !connectionReset);
})
socket.connect(443, domain);
}));
};
function getConfig() {
const config = {};
if (options.timeout) {
if (typeof options.timeout !== 'number') {
throw new Error('options.timeout must be a number');
}
config.timeout = options.timeout;
}
else {
config.timeout = defaults.timeout;
}
return config;
}
};