diff --git a/types/ping/index.d.ts b/types/ping/index.d.ts new file mode 100644 index 0000000000..6fc2f518f9 --- /dev/null +++ b/types/ping/index.d.ts @@ -0,0 +1,94 @@ +// Type definitions for ping 0.2 +// Project: http://github.com/danielzzz/node-ping +// Definitions by: Richard Honor +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export interface PingConfig { + /** + * Map IP address to hostname or not. Default `true` + */ + numeric?: boolean; + /** + * Time duration, in seconds, for ping command to exit. Default `2` on Mac/Linux, `5` on Windows. + */ + timeout?: number; + /** + * Exit after sending number of `ECHO_REQUEST`. Default `1` + */ + min_reply?: number; + /** + * Ping via ipv6 or not. Default `false` + */ + v6?: boolean; + /** + * Source address for sending the ping. + */ + sourceAddr?: string; + /** + * Additional arguments. Default `[]` + */ + extra?: string[]; + } + + export interface PingResponse { + /** + * The input IP address or host. `unknown` if ping fails. + */ + host: string; + /** + * Numeric target IP address + */ + numeric_host?: string; + /** + * `true` for existing host + */ + alive: boolean; + /** + * Raw stdout from system ping + */ + output: string; + /** + * Time (float) in ms for first successful ping response. `unknown` if ping fails. + */ + time: number | 'unknown'; + /** + * Minimum time for collection records. `unknown` if ping fails. + */ + min: string; + /** + * Maximum time for collection records. `unknown` if ping fails. + */ + max: string; + /** + * Average time for collection records. `unknown` if ping fails. + */ + avg: string; + /** + * Standard deviation time for collected records. `unknown` if ping fails. + */ + stddev: string; + } + + export const sys: { + /** + * Performs a system ping utility. + * + * @param addr Hostname or IP address + * @param cb Response callback. + * First argument is successful response boolean. + * Second argument is any error, `null` if no error. + * @param config Optional configuration + */ + probe(addr: string, cb: (isAlive: boolean, error: any) => void, config?: PingConfig): void; + }; + + export const promise: { + /** + * Performs a system ping utility. + * + * @param addr Hostname or IP address + * @param config Optional configuration + */ + probe(addr: string, config?: PingConfig): Promise; + }; diff --git a/types/ping/ping-tests.ts b/types/ping/ping-tests.ts new file mode 100644 index 0000000000..24fb7bb138 --- /dev/null +++ b/types/ping/ping-tests.ts @@ -0,0 +1,38 @@ +import { + PingConfig, + promise, + sys, +} from 'ping'; + +const config: PingConfig = { + numeric: false, + timeout: 1000, + min_reply: 5, + v6: false, + sourceAddr: 'localhost', + extra: [], +}; + +sys.probe('test', (isAlive: boolean, err: any) => { + const alive: boolean = isAlive; +}); + +async function test() { + const res = await promise.probe('test', config); + + const alive: boolean = res.alive; + const host: string = res.host; + const output: string = res.output; + const min: string = res.min; + const max: string = res.max; + const avg: string = res.avg; + const stddev: string = res.stddev; + + if (res.time !== 'unknown') { + const time: number = res.time; + } + + if (res.numeric_host) { + const numHost: string = res.numeric_host; + } +} diff --git a/types/ping/tsconfig.json b/types/ping/tsconfig.json new file mode 100644 index 0000000000..685500223f --- /dev/null +++ b/types/ping/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ping-tests.ts" + ] +} diff --git a/types/ping/tslint.json b/types/ping/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/ping/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }