@juit/lib-ping
Version:
ICMPv4/ICMPv6 Ping library for NodeJS
60 lines (59 loc) • 2.71 kB
TypeScript
/** Options to create a {@link Pinger} instance */
export interface PingerOptions {
/** The protocol: either `ipv4` or `ipv6` */
protocol?: 'ipv4' | 'ipv6';
/** An optional IP address or used to ping _from_ */
from?: string;
/** An optional source interface name to bind to for pinging */
source?: string;
/** The timeout **in milliseconds** after which a packet is considered _lost_ (default: 30000 - 30 sec) */
timeout?: number;
/** The interval **in milliseconds** used to ping the remote host (default: 1000 - 1 sec) */
interval?: number;
}
/**
* Asynchronously create a new {@link Pinger} instance.
*
* @param to The IP address or host name to ping. If this parameter is an `IPv6`
* _address_ (e.g. `::1`) the protocol used will default to `IPv6`
* otherwise it will default to `IPv4`.
*/
export declare function createPinger(to: string, options?: PingerOptions): Promise<Pinger>;
export interface Pinger {
/** An optional IP address or used to ping _from_ */
readonly from?: string | undefined;
/** An optional source interface name to bind to for pinging */
readonly source?: string | undefined;
/** The IP address or host name to ping. */
readonly target: string;
/** The timeout **in milliseconds** after which a packet is considered _lost_ (default: 30000 - 30 sec) */
readonly timeout: number;
/** The interval **in milliseconds** used to ping the remote host (default: 1000 - 1 sec) */
readonly interval: number;
/** The protocol: either `ipv4` or `ipv6` */
readonly protocol: 'ipv4' | 'ipv6';
/** A flag indicating whether this pinger is _running_ */
readonly running: boolean;
/** A flag indicating whether this pinger was _closed_ */
readonly closed: boolean;
start(): void;
stop(): void;
close(): Promise<void>;
stats(): PingerStats;
ping(): Promise<void>;
ping(callback: (error: Error | null) => void): void;
on(event: 'error', handler: (error: Error) => void): void;
off(event: 'error', handler: (error: Error) => void): void;
once(event: 'error', handler: (error: Error) => void): void;
on(event: 'warning', handler: (code: string, message: string) => void): void;
off(event: 'warning', handler: (code: string, message: string) => void): void;
once(event: 'warning', handler: (code: string, message: string) => void): void;
on(event: 'pong', handler: (latency: number) => void): void;
off(event: 'pong', handler: (latency: number) => void): void;
once(event: 'pong', handler: (latency: number) => void): void;
}
export interface PingerStats {
sent: number;
received: number;
latency: number;
}