tcpie 12.0.0 → 12.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +61 -0
- package/dist/index.js +1 -0
- package/dist/tcpie.js +6 -8
- package/package.json +24 -17
- package/index.js +0 -122
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
|
|
3
|
+
//#region index.d.ts
|
|
4
|
+
/** Target host and port of a tcpie instance. */
|
|
5
|
+
type Target = {
|
|
6
|
+
host: string;
|
|
7
|
+
port: number;
|
|
8
|
+
};
|
|
9
|
+
/** Socket address details, present on per-attempt stats. */
|
|
10
|
+
type SocketDetails = {
|
|
11
|
+
localAddress?: string;
|
|
12
|
+
localPort?: number;
|
|
13
|
+
remoteAddress?: string;
|
|
14
|
+
remotePort?: number;
|
|
15
|
+
};
|
|
16
|
+
/** Statistics emitted on each `connect`, `timeout`, and `error` event. */
|
|
17
|
+
type Stats = {
|
|
18
|
+
/** Number of connection attempts made. */sent: number; /** Number of successful connections. */
|
|
19
|
+
success: number; /** Number of failed connections. */
|
|
20
|
+
failed: number; /** Round-trip time in milliseconds of the last successful connection. */
|
|
21
|
+
rtt?: number; /** Target host and port. */
|
|
22
|
+
target?: Target; /** Socket address details. */
|
|
23
|
+
socket?: SocketDetails;
|
|
24
|
+
};
|
|
25
|
+
/** Summary emitted on the `end` event. */
|
|
26
|
+
type EndStats = {
|
|
27
|
+
/** Number of connection attempts made. */sent: number; /** Number of successful connections. */
|
|
28
|
+
success: number; /** Number of failed connections. */
|
|
29
|
+
failed: number; /** Target host and port. */
|
|
30
|
+
target: Target;
|
|
31
|
+
};
|
|
32
|
+
/** Options for a tcpie instance. */
|
|
33
|
+
type TcpieOpts = {
|
|
34
|
+
/** Milliseconds to wait between connects. Default: `1000`. */interval?: number; /** Connection timeout in milliseconds. Default: `3000`. */
|
|
35
|
+
timeout?: number; /** Number of connects to perform. Default: `Infinity`. */
|
|
36
|
+
count?: number;
|
|
37
|
+
};
|
|
38
|
+
type ResolvedOpts = Required<TcpieOpts>;
|
|
39
|
+
/** A TCP ping instance. Emits `connect`, `timeout`, `error`, and `end` events. */
|
|
40
|
+
declare class Tcpie extends EventEmitter {
|
|
41
|
+
host: string;
|
|
42
|
+
port: number;
|
|
43
|
+
opts: ResolvedOpts;
|
|
44
|
+
stats: Stats;
|
|
45
|
+
private next?;
|
|
46
|
+
private done;
|
|
47
|
+
private abort;
|
|
48
|
+
private socket?;
|
|
49
|
+
private startTime;
|
|
50
|
+
constructor(host: string, port?: number, opts?: TcpieOpts);
|
|
51
|
+
private addDetails;
|
|
52
|
+
private checkEnd;
|
|
53
|
+
/** Start the connection attempts. */
|
|
54
|
+
start(subsequent?: boolean): this;
|
|
55
|
+
/** Stop the connection attempts and emit the `end` event. */
|
|
56
|
+
stop(): this;
|
|
57
|
+
}
|
|
58
|
+
/** Create a new {@link Tcpie} instance. */
|
|
59
|
+
declare function tcpie(host: string, port?: number, opts?: TcpieOpts): Tcpie;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { EndStats, SocketDetails, Stats, Target, Tcpie, TcpieOpts, tcpie };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{EventEmitter as e}from"node:events";import{Socket as t}from"node:net";var n=class extends e{host;port;opts;stats;next;done=!1;abort=!1;socket;startTime=0;constructor(e,t,n){if(super(),typeof e!=`string`)throw Error(`host is required`);this.host=e,this.port=t??80,this.opts={interval:1e3,timeout:3e3,count:1/0,...n},this.stats={sent:0,success:0,failed:0}}addDetails(e){return this.stats.target={host:this.host,port:this.port},this.stats.socket={localAddress:e.localAddress,localPort:e.localPort,remoteAddress:e.remoteAddress,remotePort:e.remotePort},this.stats}checkEnd(){(this.abort||this.stats.failed+this.stats.success>=this.opts.count)&&(this.next&&clearTimeout(this.next),this.emit(`end`,{sent:this.stats.sent,success:this.stats.success,failed:this.stats.failed,target:{host:this.host,port:this.port}}))}start(e){return e||(this.stats.sent=0,this.stats.success=0,this.stats.failed=0),this.next=setTimeout(this.start.bind(this,!0),this.opts.interval),this.done=!1,this.abort=!1,this.socket=new t,this.startTime=performance.now(),this.socket.setTimeout(this.opts.timeout),this.socket.on(`timeout`,()=>{this.done||(this.done=!0,this.stats.sent++,this.stats.failed++,this.emit(`timeout`,this.addDetails(this.socket)),this.socket.destroy(),this.checkEnd())}),this.socket.on(`error`,e=>{this.done||(this.done=!0,this.stats.sent++,this.stats.failed++,this.emit(`error`,e,this.addDetails(this.socket)),this.socket.destroy(),this.checkEnd())}),this.socket.connect(this.port,this.host,()=>{this.done||(this.done=!0,this.stats.sent++,this.stats.success++,this.stats.rtt=performance.now()-this.startTime,this.emit(`connect`,this.addDetails(this.socket)),this.socket.end(),this.checkEnd())}),this}stop(){return this.abort=!0,this.socket.end(),this.checkEnd(),this}};function r(e,t,r){return new n(e,t,r)}export{n as Tcpie,r as tcpie};
|
package/dist/tcpie.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
`&&t.unshift(zt()),t.push(`
|
|
9
|
-
`),(Q._type==="pipe"&&J?Dt:Q).write(t.join(" "))}function Tt(){Q.write(qt),O(1)}function Et(t){return Number.parseFloat(t)*1e3}function zt(){let t=new Date,o=t.getFullYear(),e=t.getMonth()+1,s=t.getDate(),n=t.getHours(),c=t.getMinutes(),r=t.getSeconds();return e<10&&(e=`0${e}`),s<10&&(s=`0${s}`),n<10&&(n=`0${n}`),c<10&&(c=`0${c}`),r<10&&(r=`0${r}`),`${o}-${e}-${s} ${n}:${c}:${r}`}
|
|
2
|
+
import{disableColor as e,green as t,red as n,yellow as r}from"glowie";import{Socket as i,isIP as a}from"node:net";import{lookup as o}from"node:dns";import s,{argv as c,exit as l,stderr as u,stdin as d,stdout as f}from"node:process";import p from"compute-stdev";import{EventEmitter as m}from"node:events";import h from"minimist";import g from"supports-color";var _=class extends m{host;port;opts;stats;next;done=!1;abort=!1;socket;startTime=0;constructor(e,t,n){if(super(),typeof e!=`string`)throw Error(`host is required`);this.host=e,this.port=t??80,this.opts={interval:1e3,timeout:3e3,count:1/0,...n},this.stats={sent:0,success:0,failed:0}}addDetails(e){return this.stats.target={host:this.host,port:this.port},this.stats.socket={localAddress:e.localAddress,localPort:e.localPort,remoteAddress:e.remoteAddress,remotePort:e.remotePort},this.stats}checkEnd(){(this.abort||this.stats.failed+this.stats.success>=this.opts.count)&&(this.next&&clearTimeout(this.next),this.emit(`end`,{sent:this.stats.sent,success:this.stats.success,failed:this.stats.failed,target:{host:this.host,port:this.port}}))}start(e){return e||(this.stats.sent=0,this.stats.success=0,this.stats.failed=0),this.next=setTimeout(this.start.bind(this,!0),this.opts.interval),this.done=!1,this.abort=!1,this.socket=new i,this.startTime=performance.now(),this.socket.setTimeout(this.opts.timeout),this.socket.on(`timeout`,()=>{this.done||(this.done=!0,this.stats.sent++,this.stats.failed++,this.emit(`timeout`,this.addDetails(this.socket)),this.socket.destroy(),this.checkEnd())}),this.socket.on(`error`,e=>{this.done||(this.done=!0,this.stats.sent++,this.stats.failed++,this.emit(`error`,e,this.addDetails(this.socket)),this.socket.destroy(),this.checkEnd())}),this.socket.connect(this.port,this.host,()=>{this.done||(this.done=!0,this.stats.sent++,this.stats.success++,this.stats.rtt=performance.now()-this.startTime,this.emit(`connect`,this.addDetails(this.socket)),this.socket.end(),this.checkEnd())}),this}stop(){return this.abort=!0,this.socket.end(),this.checkEnd(),this}};function v(e,t,n){return new _(e,t,n)}var y=`12.0.1`;const b=h(c.slice(2),{boolean:[`color`,`C`,`timestamp`,`T`,`flood`,`f`,`version`,`v`]}),x=y||`0.0.0`,S=[``,` Usage: tcpie [options] host[:port] [port|22]`,``,` Options:`,``,` -v, --version output version`,` -c, --count <n> number of connects (default: infinite)`,` -i, --interval <n> wait n seconds between connects (default: 1)`,` -t, --timeout <n> connection timeout in seconds (default: 3)`,` -T, --timestamp add timestamps to output`,` -f, --flood flood mode, connect as fast as possible`,` -C, --no-color disable color output`,``,` Examples:`,``,` $ tcpie google.com`,` $ tcpie -i .1 8.8.8.8:53`,` $ tcpie -c5 -t.05 aspmx.l.google.com 25`,``,``].join(`
|
|
3
|
+
`);b.v&&(console.info(x),l(0)),(!b._.length||b._.length>2||b._[1]&&Number.isNaN(Number.parseInt(b._[1])))&&F();let C=b._[0];const w={};let T=Number.parseInt(b._[1]),E=!1;const D=[];let O;typeof C!=`string`&&F();const k=/^(.+):(\d+)$/.exec(C);k?.length===3&&!T&&(C=k[1],T=Number.parseInt(k[2])),T||=22,(b.count||b.c)&&(w.count=Number.parseInt(b.count||b.c)),(b.interval||b.i)&&(w.interval=I(b.interval||b.i)),(b.timeout||b.t)&&(w.timeout=I(b.timeout||b.t)),(b.flood||b.f)&&(w.interval=0),(b.C||!g.stdout)&&e(),a(C)?(j(C,C,T),A(C,T,w)):o(C,(e,t)=>{e?(e.code===`ENOTFOUND`?P(n(`ERROR:`),`Host '${C}' not found`):P(n(`ERROR:`),e.code||``,e.syscall||``),l(1)):(j(C,t,T),A(C,T,w))});function A(e,r,i){let a=v(e,r,i);a.on(`error`,(e,t)=>{O=t,P(n(`error connecting to`,`${t.target.host}:${t.target.port}`),`seq=${t.sent}`,`error=${n(e.code)}`)}).on(`connect`,e=>{O=e,D.push(e.rtt),P(t(`connected to`,`${e.target.host}:${e.target.port}`),`seq=${e.sent}`,e.socket.localPort===void 0?``:`srcport=${e.socket.localPort}`,`time=${N(Number(e.rtt.toFixed(1)))}`)}).on(`timeout`,e=>{O=e,P(n(`timeout connecting to`,`${e.target.host}:${e.target.port}`),`seq=${e.sent}`,e.socket.localPort?`srcport=${e.socket.localPort}`:``)}),d.isTTY?(d.setRawMode(!0),d.on(`data`,e=>{let t=[3,4,26,28];for(let n of e)t.includes(n)&&M()})):(s.on(`SIGINT`,l),s.on(`SIGQUIT`,l),s.on(`SIGTERM`,l),s.on(`SIGTSTP`,l)),s.on(`exit`,M),a.on(`end`,M).start()}function j(e,t,n){P(`TCPIE`,e,`(${t})`,`port`,String(n))}function M(){let e=0,t=1/0,n=0,r=`0`,i=`0`;if(E&&l(+(O.success===0)),O&&O.sent>0){for(let r of D)r<=t&&(t=Number(r.toFixed(3))),r>=n&&(n=Number(r.toFixed(3))),e+=r;r=(e/D.length).toFixed(3),i=p(D).toFixed(3),t===1/0&&(t=0),Number.isNaN(Number(r))&&(r=`0`),E=!0,P(`
|
|
4
|
+
---`,C,`tcpie statistics`,`---`,`\n${O.sent}`,`handshakes attempted,`,String(O.success||`0`),`succeeded,`,`${(O.failed/O.sent*100).toFixed(0)}% failed`,`
|
|
5
|
+
rtt min/avg/max/stdev =`,`${t}/${r}/${n}/${i}`,`ms`),l(+(O.success===0))}else l(1)}function N(e){return e>=150?`${n(String(e))} ms`:e>=75?`${r(String(e))} ms`:`${t(String(e))} ms`}function P(...e){e=e.filter(Boolean),(b.timeout||b.T)&&e[0][0]!==`
|
|
6
|
+
`&&e.unshift(L()),e.push(`
|
|
7
|
+
`),(f._type===`pipe`&&E?u:f).write(e.join(` `))}function F(){f.write(S),l(1)}function I(e){return Number.parseFloat(e)*1e3}function L(){let e=new Date,t=e.getFullYear(),n=String(e.getMonth()+1),r=String(e.getDate()),i=String(e.getHours()),a=String(e.getMinutes()),o=String(e.getSeconds());return Number(n)<10&&(n=`0${n}`),Number(r)<10&&(r=`0${r}`),Number(i)<10&&(i=`0${i}`),Number(a)<10&&(a=`0${a}`),Number(o)<10&&(o=`0${o}`),`${t}-${n}-${r} ${i}:${a}:${o}`}export{};
|
package/package.json
CHANGED
|
@@ -1,38 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tcpie",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.1",
|
|
4
4
|
"description": "Ping any TCP port",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/tcpie",
|
|
7
7
|
"license": "BSD-2-Clause",
|
|
8
|
-
"exports": "./index.js",
|
|
9
|
-
"bin": "./dist/tcpie.js",
|
|
10
8
|
"type": "module",
|
|
11
9
|
"sideEffects": false,
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"exports": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"bin": "./dist/tcpie.js",
|
|
12
14
|
"engines": {
|
|
13
|
-
"node": ">=22"
|
|
15
|
+
"node": ">=22",
|
|
16
|
+
"bun": "*"
|
|
14
17
|
},
|
|
15
18
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
"./dist/tcpie.js"
|
|
19
|
+
"dist"
|
|
18
20
|
],
|
|
19
21
|
"dependencies": {
|
|
20
22
|
"compute-stdev": "1.0.0",
|
|
21
|
-
"glowie": "1.3.
|
|
23
|
+
"glowie": "1.3.5",
|
|
22
24
|
"minimist": "1.2.8",
|
|
23
25
|
"supports-color": "10.2.2"
|
|
24
26
|
},
|
|
25
27
|
"devDependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
28
|
+
"@types/compute-stdev": "1.0.2",
|
|
29
|
+
"@types/minimist": "1.2.5",
|
|
30
|
+
"@types/node": "25.8.0",
|
|
31
|
+
"@typescript/native-preview": "7.0.0-dev.20260515.1",
|
|
32
|
+
"eslint": "10.4.0",
|
|
33
|
+
"eslint-config-silverwind": "133.0.2",
|
|
29
34
|
"jest-extended": "7.0.0",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
35
|
+
"tsdown": "0.22.0",
|
|
36
|
+
"tsdown-config-silverwind": "3.0.1",
|
|
37
|
+
"typescript": "6.0.3",
|
|
38
|
+
"typescript-config-silverwind": "18.0.0",
|
|
39
|
+
"updates": "17.16.12",
|
|
40
|
+
"updates-config-silverwind": "3.0.2",
|
|
41
|
+
"versions": "15.0.4",
|
|
42
|
+
"vitest": "4.1.6",
|
|
43
|
+
"vitest-config-silverwind": "11.3.5"
|
|
37
44
|
}
|
|
38
45
|
}
|
package/index.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import {EventEmitter} from "node:events";
|
|
2
|
-
import {Socket} from "node:net";
|
|
3
|
-
import {inherits} from "node:util";
|
|
4
|
-
|
|
5
|
-
const Tcpie = function(host, port, opts) {
|
|
6
|
-
if (!(this instanceof Tcpie)) return new Tcpie();
|
|
7
|
-
if (typeof host !== "string") throw new Error("host is required");
|
|
8
|
-
if (port === undefined) port = 80;
|
|
9
|
-
|
|
10
|
-
this.host = host;
|
|
11
|
-
this.port = port;
|
|
12
|
-
|
|
13
|
-
this.opts = {interval: 1000,
|
|
14
|
-
timeout: 3000,
|
|
15
|
-
count: Infinity, ...opts};
|
|
16
|
-
|
|
17
|
-
this.stats = {
|
|
18
|
-
sent: 0,
|
|
19
|
-
success: 0,
|
|
20
|
-
failed: 0
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
inherits(Tcpie, EventEmitter);
|
|
25
|
-
|
|
26
|
-
Tcpie.prototype.start = function start(subsequent) {
|
|
27
|
-
if (!subsequent) {
|
|
28
|
-
this.stats.sent = 0;
|
|
29
|
-
this.stats.success = 0;
|
|
30
|
-
this.stats.failed = 0;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this._next = setTimeout(start.bind(this, true), this.opts.interval);
|
|
34
|
-
this._done = false;
|
|
35
|
-
this._abort = false;
|
|
36
|
-
this._socket = new Socket();
|
|
37
|
-
this._startTime = performance.now();
|
|
38
|
-
|
|
39
|
-
this._socket.setTimeout(this.opts.timeout);
|
|
40
|
-
this._socket.on("timeout", () => {
|
|
41
|
-
if (!this._done) {
|
|
42
|
-
this._done = true;
|
|
43
|
-
this.stats.sent++;
|
|
44
|
-
this.stats.failed++;
|
|
45
|
-
this.emit("timeout", addDetails(this, this));
|
|
46
|
-
this._socket.destroy();
|
|
47
|
-
checkEnd(this);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
this._socket.on("error", err => {
|
|
52
|
-
if (!this._done) {
|
|
53
|
-
this._done = true;
|
|
54
|
-
this.stats.sent++;
|
|
55
|
-
this.stats.failed++;
|
|
56
|
-
this.emit("error", err, addDetails(this, this));
|
|
57
|
-
this._socket.destroy();
|
|
58
|
-
checkEnd(this);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
this._socket.connect(this.port, this.host, () => {
|
|
63
|
-
if (!this._done) {
|
|
64
|
-
this._done = true;
|
|
65
|
-
this.stats.sent++;
|
|
66
|
-
this.stats.success++;
|
|
67
|
-
this.stats.rtt = (performance.now() - this._startTime);
|
|
68
|
-
this.emit("connect", addDetails(this, this));
|
|
69
|
-
this._socket.end();
|
|
70
|
-
checkEnd(this);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return this;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
Tcpie.prototype.stop = function stop() {
|
|
78
|
-
this._abort = true;
|
|
79
|
-
this._socket.end();
|
|
80
|
-
checkEnd(this);
|
|
81
|
-
return this;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export function tcpie(...args) {
|
|
85
|
-
return new Tcpie(...args);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// add details to stats object
|
|
89
|
-
function addDetails(that, socket) {
|
|
90
|
-
const ret = that.stats;
|
|
91
|
-
|
|
92
|
-
ret.target = {
|
|
93
|
-
host: that.host,
|
|
94
|
-
port: that.port
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
ret.socket = {
|
|
98
|
-
localAddress: socket.localAddress,
|
|
99
|
-
localPort: socket.localPort,
|
|
100
|
-
remoteAddress: socket.remoteAddress,
|
|
101
|
-
remotePort: socket.remotePort
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
return ret;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// check end condition
|
|
108
|
-
function checkEnd(that) {
|
|
109
|
-
if (that._abort || ((that.stats.failed + that.stats.success) >= that.opts.count)) {
|
|
110
|
-
if (that._next) clearTimeout(that._next);
|
|
111
|
-
|
|
112
|
-
that.emit("end", {
|
|
113
|
-
sent: that.stats.sent,
|
|
114
|
-
success: that.stats.success,
|
|
115
|
-
failed: that.stats.failed,
|
|
116
|
-
target: {
|
|
117
|
-
host: that.host,
|
|
118
|
-
port: that.port
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|