-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
122 lines (103 loc) · 2.65 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {EventEmitter} from "node:events";
import {Socket} from "node:net";
import {inherits} from "node:util";
const Tcpie = function(host, port, opts) {
if (!(this instanceof Tcpie)) return new Tcpie();
if (typeof host !== "string") throw new Error("host is required");
if (port === undefined) port = 80;
this.host = host;
this.port = port;
this.opts = {interval: 1000,
timeout: 3000,
count: Infinity, ...opts};
this.stats = {
sent: 0,
success: 0,
failed: 0
};
};
inherits(Tcpie, EventEmitter);
Tcpie.prototype.start = function start(subsequent) {
if (!subsequent) {
this.stats.sent = 0;
this.stats.success = 0;
this.stats.failed = 0;
}
this._next = setTimeout(start.bind(this, true), this.opts.interval);
this._done = false;
this._abort = false;
this._socket = new Socket();
this._startTime = performance.now();
this._socket.setTimeout(this.opts.timeout);
this._socket.on("timeout", () => {
if (!this._done) {
this._done = true;
this.stats.sent++;
this.stats.failed++;
this.emit("timeout", addDetails(this, this));
this._socket.destroy();
checkEnd(this);
}
});
this._socket.on("error", err => {
if (!this._done) {
this._done = true;
this.stats.sent++;
this.stats.failed++;
this.emit("error", err, addDetails(this, this));
this._socket.destroy();
checkEnd(this);
}
});
this._socket.connect(this.port, this.host, () => {
if (!this._done) {
this._done = true;
this.stats.sent++;
this.stats.success++;
this.stats.rtt = (performance.now() - this._startTime);
this.emit("connect", addDetails(this, this));
this._socket.end();
checkEnd(this);
}
});
return this;
};
Tcpie.prototype.stop = function stop() {
this._abort = true;
this._socket.end();
checkEnd(this);
return this;
};
export function tcpie(...args) {
return new Tcpie(...args);
}
// add details to stats object
function addDetails(that, socket) {
const ret = that.stats;
ret.target = {
host: that.host,
port: that.port
};
ret.socket = {
localAddress: socket.localAddress,
localPort: socket.localPort,
remoteAddress: socket.remoteAddress,
remotePort: socket.remotePort
};
return ret;
}
// check end condition
function checkEnd(that) {
if (that._abort || ((that.stats.failed + that.stats.success) >= that.opts.count)) {
if (that._next) clearTimeout(that._next);
that.emit("end", {
sent: that.stats.sent,
success: that.stats.success,
failed: that.stats.failed,
target: {
host: that.host,
port: that.port
}
});
}
}