[ping] Add new definition file for ping (#35618)

* Add definitions for node-ping

* Fix linting and test errors
This commit is contained in:
Richard Honor 2019-05-22 17:04:27 +01:00 committed by Ryan Cavanaugh
parent 70eeed4f7d
commit b244a1ab2e
4 changed files with 156 additions and 0 deletions

94
types/ping/index.d.ts vendored Normal file
View File

@ -0,0 +1,94 @@
// Type definitions for ping 0.2
// Project: http://github.com/danielzzz/node-ping
// Definitions by: Richard Honor <https://github.com/RMHonor>
// 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<PingResponse>;
};

38
types/ping/ping-tests.ts Normal file
View File

@ -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;
}
}

23
types/ping/tsconfig.json Normal file
View File

@ -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"
]
}

1
types/ping/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }