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.
@@ -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 {createRequire} from 'module';const require = createRequire(import.meta.url);
3
- var vt=Object.create;var j=Object.defineProperty;var Rt=Object.getOwnPropertyDescriptor;var $t=Object.getOwnPropertyNames;var Ot=Object.getPrototypeOf,It=Object.prototype.hasOwnProperty;var k=(t,o)=>()=>(o||t((o={exports:{}}).exports,o),o.exports);var Ct=(t,o,e,s)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of $t(o))!It.call(t,n)&&n!==e&&j(t,n,{get:()=>o[n],enumerable:!(s=Rt(o,n))||s.enumerable});return t};var tt=(t,o,e)=>(e=t!=null?vt(Ot(t)):{},Ct(o||!t||!t.__esModule?j(e,"default",{value:t,enumerable:!0}):e,t));var it=k((Zt,st)=>{(function(){"use strict";function t(o){if(!Array.isArray(o))throw new TypeError("stdev()::invalid input argument. Must provide an array.");var e=o.length,s=0,n=0,c=0,r=0;if(e<2)return 0;for(var A=0;A<e;A++)s+=1,r=o[A]-n,n+=r/s,c+=r*(o[A]-n);return Math.sqrt(c/(s-1))}st.exports=t})()});var lt=k((kt,ut)=>{"use strict";function At(t,o){var e=t;o.slice(0,-1).forEach(function(n){e=e[n]||{}});var s=o[o.length-1];return s in e}function ct(t){return typeof t=="number"||/^0x[0-9a-f]+$/i.test(t)?!0:/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(t)}function ft(t,o){return o==="constructor"&&typeof t[o]=="function"||o==="__proto__"}ut.exports=function(t,o){o||(o={});var e={bools:{},strings:{},unknownFn:null};typeof o.unknown=="function"&&(e.unknownFn=o.unknown),typeof o.boolean=="boolean"&&o.boolean?e.allBools=!0:[].concat(o.boolean).filter(Boolean).forEach(function(i){e.bools[i]=!0});var s={};function n(i){return s[i].some(function(m){return e.bools[m]})}Object.keys(o.alias||{}).forEach(function(i){s[i]=[].concat(o.alias[i]),s[i].forEach(function(m){s[m]=[i].concat(s[i].filter(function(I){return m!==I}))})}),[].concat(o.string).filter(Boolean).forEach(function(i){e.strings[i]=!0,s[i]&&[].concat(s[i]).forEach(function(m){e.strings[m]=!0})});var c=o.default||{},r={_:[]};function A(i,m){return e.allBools&&/^--[^=]+$/.test(m)||e.strings[i]||e.bools[i]||s[i]}function B(i,m,I){for(var u=i,S=0;S<m.length-1;S++){var $=m[S];if(ft(u,$))return;u[$]===void 0&&(u[$]={}),(u[$]===Object.prototype||u[$]===Number.prototype||u[$]===String.prototype)&&(u[$]={}),u[$]===Array.prototype&&(u[$]=[]),u=u[$]}var C=m[m.length-1];ft(u,C)||((u===Object.prototype||u===Number.prototype||u===String.prototype)&&(u={}),u===Array.prototype&&(u=[]),u[C]===void 0||e.bools[C]||typeof u[C]=="boolean"?u[C]=I:Array.isArray(u[C])?u[C].push(I):u[C]=[u[C],I])}function h(i,m,I){if(!(I&&e.unknownFn&&!A(i,I)&&e.unknownFn(I)===!1)){var u=!e.strings[i]&&ct(m)?Number(m):m;B(r,i.split("."),u),(s[i]||[]).forEach(function(S){B(r,S.split("."),u)})}}Object.keys(e.bools).forEach(function(i){h(i,c[i]===void 0?!1:c[i])});var U=[];t.indexOf("--")!==-1&&(U=t.slice(t.indexOf("--")+1),t=t.slice(0,t.indexOf("--")));for(var d=0;d<t.length;d++){var f=t[d],p,_;if(/^--.+=/.test(f)){var X=f.match(/^--([^=]+)=([\s\S]*)$/);p=X[1];var V=X[2];e.bools[p]&&(V=V!=="false"),h(p,V,f)}else if(/^--no-.+/.test(f))p=f.match(/^--no-(.+)/)[1],h(p,!1,f);else if(/^--.+/.test(f))p=f.match(/^--(.+)/)[1],_=t[d+1],_!==void 0&&!/^(-|--)[^-]/.test(_)&&!e.bools[p]&&!e.allBools&&(!s[p]||!n(p))?(h(p,_,f),d+=1):/^(true|false)$/.test(_)?(h(p,_==="true",f),d+=1):h(p,e.strings[p]?"":!0,f);else if(/^-[^-]+/.test(f)){for(var v=f.slice(1,-1).split(""),G=!1,b=0;b<v.length;b++){if(_=f.slice(b+2),_==="-"){h(v[b],_,f);continue}if(/[A-Za-z]/.test(v[b])&&_[0]==="="){h(v[b],_.slice(1),f),G=!0;break}if(/[A-Za-z]/.test(v[b])&&/-?\d+(\.\d*)?(e-?\d+)?$/.test(_)){h(v[b],_,f),G=!0;break}if(v[b+1]&&v[b+1].match(/\W/)){h(v[b],f.slice(b+2),f),G=!0;break}else h(v[b],e.strings[v[b]]?"":!0,f)}p=f.slice(-1)[0],!G&&p!=="-"&&(t[d+1]&&!/^(-|--)[^-]/.test(t[d+1])&&!e.bools[p]&&(!s[p]||!n(p))?(h(p,t[d+1],f),d+=1):t[d+1]&&/^(true|false)$/.test(t[d+1])?(h(p,t[d+1]==="true",f),d+=1):h(p,e.strings[p]?"":!0,f))}else if((!e.unknownFn||e.unknownFn(f)!==!1)&&r._.push(e.strings._||!ct(f)?f:Number(f)),o.stopEarly){r._.push.apply(r._,t.slice(d+1));break}}return Object.keys(c).forEach(function(i){At(r,i.split("."))||(B(r,i.split("."),c[i]),(s[i]||[]).forEach(function(m){B(r,m.split("."),c[i])}))}),o["--"]?r["--"]=U.slice():U.forEach(function(i){r._.push(i)}),r}});var ot=!0;function et(){ot=!1}function q(t,o,e){let s=t.join(" ");return ot?`\x1B[${o}m${s}\x1B[${e}m`:s}var w=(...t)=>q(t,31,39),K=(...t)=>q(t,32,39),nt=(...t)=>q(t,33,39);var bt=tt(it(),1);import{isIP as Bt}from"node:net";import{lookup as Gt}from"node:dns";import x,{exit as O,argv as Lt,stdin as W,stdout as Q,stderr as Dt}from"node:process";import{EventEmitter as Nt}from"node:events";import{Socket as wt}from"node:net";import{inherits as gt}from"node:util";var M=function(t,o,e){if(!(this instanceof M))return new M;if(typeof t!="string")throw new Error("host is required");o===void 0&&(o=80),this.host=t,this.port=o,this.opts={interval:1e3,timeout:3e3,count:1/0,...e},this.stats={sent:0,success:0,failed:0}};gt(M,Nt);M.prototype.start=function t(o){return o||(this.stats.sent=0,this.stats.success=0,this.stats.failed=0),this._next=setTimeout(t.bind(this,!0),this.opts.interval),this._done=!1,this._abort=!1,this._socket=new wt,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",z(this,this)),this._socket.destroy(),L(this))}),this._socket.on("error",e=>{this._done||(this._done=!0,this.stats.sent++,this.stats.failed++,this.emit("error",e,z(this,this)),this._socket.destroy(),L(this))}),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",z(this,this)),this._socket.end(),L(this))}),this};M.prototype.stop=function(){return this._abort=!0,this._socket.end(),L(this),this};function rt(...t){return new M(...t)}function z(t,o){let e=t.stats;return e.target={host:t.host,port:t.port},e.socket={localAddress:o.localAddress,localPort:o.localPort,remoteAddress:o.remoteAddress,remotePort:o.remotePort},e}function L(t){(t._abort||t.stats.failed+t.stats.success>=t.opts.count)&&(t._next&&clearTimeout(t._next),t.emit("end",{sent:t.stats.sent,success:t.stats.success,failed:t.stats.failed,target:{host:t.host,port:t.port}}))}var _t=tt(lt(),1);import H from"node:process";import Mt from"node:os";import at from"node:tty";function T(t,o=globalThis.Deno?globalThis.Deno.args:H.argv){let e=t.startsWith("-")?"":t.length===1?"-":"--",s=o.indexOf(e+t),n=o.indexOf("--");return s!==-1&&(n===-1||s<n)}var{env:l}=H,D;T("no-color")||T("no-colors")||T("color=false")||T("color=never")?D=0:(T("color")||T("colors")||T("color=true")||T("color=always"))&&(D=1);function Ft(){if(!("FORCE_COLOR"in l))return;if(l.FORCE_COLOR==="true")return 1;if(l.FORCE_COLOR==="false")return 0;if(l.FORCE_COLOR.length===0)return 1;let t=Math.min(Number.parseInt(l.FORCE_COLOR,10),3);if([0,1,2,3].includes(t))return t}function St(t){return t===0?!1:{level:t,hasBasic:!0,has256:t>=2,has16m:t>=3}}function xt(t,{streamIsTTY:o,sniffFlags:e=!0}={}){let s=Ft();s!==void 0&&(D=s);let n=e?D:s;if(n===0)return 0;if(e){if(T("color=16m")||T("color=full")||T("color=truecolor"))return 3;if(T("color=256"))return 2}if("TF_BUILD"in l&&"AGENT_NAME"in l)return 1;if(t&&!o&&n===void 0)return 0;let c=n||0;if(l.TERM==="dumb")return c;if(H.platform==="win32"){let r=Mt.release().split(".");return Number(r[0])>=10&&Number(r[2])>=10586?Number(r[2])>=14931?3:2:1}if("CI"in l)return["GITHUB_ACTIONS","GITEA_ACTIONS","CIRCLECI"].some(r=>r in l)?3:["TRAVIS","APPVEYOR","GITLAB_CI","BUILDKITE","DRONE"].some(r=>r in l)||l.CI_NAME==="codeship"?1:c;if("TEAMCITY_VERSION"in l)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(l.TEAMCITY_VERSION)?1:0;if(l.COLORTERM==="truecolor"||l.TERM==="xterm-kitty"||l.TERM==="xterm-ghostty"||l.TERM==="wezterm")return 3;if("TERM_PROGRAM"in l){let r=Number.parseInt((l.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(l.TERM_PROGRAM){case"iTerm.app":return r>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(l.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(l.TERM)||"COLORTERM"in l?1:c}function pt(t,o={}){let e=xt(t,{streamIsTTY:t&&t.isTTY,...o});return St(e)}var Pt={stdout:pt({isTTY:at.isatty(1)}),stderr:pt({isTTY:at.isatty(2)})},mt=Pt;var a=(0,_t.default)(Lt.slice(2),{boolean:["color","C","timestamp","T","flood","f","version","v"]}),Yt="12.0.0",yt=1,Y=3,Ut=0,Vt=22,qt=[""," 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(`
4
- `);a.v&&(console.info(Yt),O(0));(!a._.length||a._.length>2||a._[1]&&Number.isNaN(Number.parseInt(a._[1])))&&Tt();var E=a._[0],F={},N=Number.parseInt(a._[1]),J=!1,y=[],R;typeof E!="string"&&Tt();var P=/^(.+):(\d+)$/.exec(E);(P==null?void 0:P.length)===3&&!N&&(E=P[1],N=P[2]);N||(N=Vt);(a.count||a.c)&&(F.count=Number.parseInt(a.count||a.c));(a.interval||a.i)&&(F.interval=Et(a.interval||a.i));(a.timeout||a.t)&&(F.timeout=Et(a.timeout||a.t));(a.flood||a.f)&&(F.interval=0);(a.C||!mt.stdout)&&et();Bt(E)?(ht(E,E,N),dt(E,N,F)):Gt(E,(t,o)=>{t?(t.code==="ENOTFOUND"?g(w("ERROR:"),`Host '${E}' not found`):g(w("ERROR:"),t.code,t.syscall||""),O(1)):(ht(E,o,N),dt(E,N,F))});function dt(t,o,e){let s=rt(t,o,e);s.on("error",(n,c)=>{R=c,g(w("error connecting to",`${c.target.host}:${c.target.port}`),`seq=${c.sent}`,`error=${w(n.code)}`)}).on("connect",n=>{R=n,y.push(n.rtt),g(K("connected to",`${n.target.host}:${n.target.port}`),`seq=${n.sent}`,n.socket.localPort!==void 0?`srcport=${n.socket.localPort}`:"",`time=${Kt(n.rtt.toFixed(yt))}`)}).on("timeout",n=>{R=n,g(w("timeout connecting to",`${n.target.host}:${n.target.port}`),`seq=${n.sent}`,n.socket.localPort&&`srcport=${n.socket.localPort}`)}),W.isTTY?(W.setRawMode(!0),W.on("data",n=>{let c=[3,4,26,28];for(let r of n)c.includes(r)&&Z()})):(x.on("SIGINT",O),x.on("SIGQUIT",O),x.on("SIGTERM",O),x.on("SIGTSTP",O)),x.on("exit",Z),s.on("end",Z).start()}function ht(t,o,e){g("TCPIE",t,`(${o})`,"port",String(e))}function Z(){let t=0,o=1/0,e=0,s,n;if(J&&O(R.success===0&&1||0),R&&R.sent>0){for(let c of y)c<=o&&(o=c.toFixed(Y)),c>=e&&(e=c.toFixed(Y)),t+=c;s=(t/y.length).toFixed(Y),n=(0,bt.default)(y).toFixed(Y),o===1/0&&(o="0"),Number.isNaN(s)&&(s="0"),J=!0,g(`
5
- ---`,E,"tcpie statistics","---",`
6
- ${R.sent}`,"handshakes attempted,",R.success||"0","succeeded,",`${(R.failed/R.sent*100).toFixed(Ut)}% failed`,`
7
- rtt min/avg/max/stdev =`,`${o}/${s}/${e}/${n}`,"ms"),O(R.success===0&&1||0)}else O(1)}function Kt(t){return t>=150?`${w(t)} ms`:t>=75?`${nt(t)} ms`:`${K(t)} ms`}function g(...t){t=t.filter(Boolean),(a.timeout||a.T)&&t[0][0]!==`
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.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
- "./index.js",
17
- "./dist/tcpie.js"
19
+ "dist"
18
20
  ],
19
21
  "dependencies": {
20
22
  "compute-stdev": "1.0.0",
21
- "glowie": "1.3.4",
23
+ "glowie": "1.3.5",
22
24
  "minimist": "1.2.8",
23
25
  "supports-color": "10.2.2"
24
26
  },
25
27
  "devDependencies": {
26
- "esbuild": "0.28.0",
27
- "eslint": "9.39.4",
28
- "eslint-config-silverwind": "131.0.4",
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
- "typescript": "6.0.2",
31
- "typescript-config-silverwind": "17.0.0",
32
- "updates": "17.15.2",
33
- "updates-config-silverwind": "3.0.0",
34
- "versions": "15.0.0",
35
- "vitest": "4.1.4",
36
- "vitest-config-silverwind": "11.3.3"
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
- }