Comparing version 0.1.10 to 0.2.0
@@ -7,2 +7,3 @@ //-------- example ----------------------- | ||
hosts.forEach(function(host){ | ||
// Running with default config | ||
ping.sys.probe(host, function(isAlive){ | ||
@@ -12,3 +13,15 @@ var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
}); | ||
// Running with custom config | ||
ping.sys.probe(host, function(isAlive){ | ||
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
console.log(msg); | ||
}, {extra: ["-i 2"]}); | ||
// Running ping with some default argument gone | ||
ping.sys.probe(host, function(isAlive){ | ||
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
console.log(msg); | ||
}, {extra: ["-i 2"], timeout: false}); | ||
}); | ||
@@ -5,2 +5,3 @@ var ping = require("../index"); | ||
// Running with default config | ||
hosts.forEach(function (host) { | ||
@@ -14,11 +15,27 @@ ping.promise.probe(host) | ||
// Running with custom config | ||
hosts.forEach(function (host) { | ||
// WARNING: -i 2 argument may not work in other platform like window | ||
ping.promise.probe(host, { | ||
timeout: 10, | ||
extra: ["-i", "2"] | ||
extra: ["-i 2"], | ||
}) | ||
.then(function (res) { | ||
console.log(res); | ||
console.log(res); | ||
}) | ||
.done(); | ||
}); | ||
// Running ping with some default argument gone | ||
hosts.forEach(function (host) { | ||
// WARNING: -i 2 argument may not work in other platform like window | ||
ping.promise.probe(host, { | ||
timeout: false, | ||
// Below extra arguments may not work in platforms other than linux | ||
extra: ["-i 2"], | ||
}) | ||
.then(function (res) { | ||
console.log(res); | ||
}) | ||
.done(); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -11,20 +13,21 @@ * LICENSE MIT | ||
//system library | ||
var sys = require('util'), | ||
cp = require('child_process'), | ||
os = require('os'); | ||
// System library | ||
var util = require('util'); | ||
var cp = require('child_process'); | ||
var os = require('os'); | ||
//3rd-party library | ||
var Q = require("q"); | ||
// 3rd-party library | ||
var Q = require('q'); | ||
// Our library | ||
var linuxBuilder = require('./builder/linux'); | ||
var macBuilder = require('./builder/mac'); | ||
var winBuilder = require('./builder/win'); | ||
/** | ||
* Class::PromisePing | ||
* | ||
* @param addr string | ||
* @param config dict | ||
* { | ||
* options: [...] | ||
* } | ||
* | ||
* @return Class::Q promise | ||
* @param {string} addr - Hostname or ip addres | ||
* @param {PingConfig} config - Configuration for command ping | ||
* @return {Promise} | ||
*/ | ||
@@ -34,64 +37,37 @@ function probe(addr, config) { | ||
var ls = null; | ||
var outstring = ""; | ||
var outstring = ''; | ||
var deferred = Q.defer(); | ||
var default_cfg = { | ||
"numeric": true, | ||
"timeout": 2, | ||
"min_reply": 1, | ||
"extra": [] | ||
}; | ||
var args = []; | ||
config = config ? config : default_cfg; | ||
// Do not reassign function argument | ||
var _config = config || {}; | ||
if (p === 'linux') { | ||
//linux | ||
args = []; | ||
if (config.numeric !== false) { | ||
args.push("-n"); | ||
} | ||
if (config.timeout !== false) { | ||
args.push(sys.format("-w %d", | ||
config.timeout ? | ||
config.timeout : default_cfg.timeout)); | ||
} | ||
if (config.min_reply !== false) { | ||
args.push(sys.format("-c %d", | ||
config.min_reply ? | ||
config.min_reply : default_cfg.min_reply)); | ||
} | ||
if (config.extra !== false) { | ||
args = args.concat(config.extra ? | ||
config.extra : default_cfg.extra); | ||
} | ||
args.push(addr); | ||
// linux | ||
args = linuxBuilder.getResult(addr, _config); | ||
ls = cp.spawn('/bin/ping', args); | ||
} else if (p.match(/^win/)) { | ||
//windows | ||
args = ['-n', '1', '-w', '5000']; | ||
//TODO: No idea for how to write ping commands in Window | ||
if (config.extra !== false) { | ||
args = args.concat(config.extra ? | ||
config.extra : default_cfg.extra); | ||
} | ||
args.push(addr); | ||
ls = cp.spawn('C:/windows/system32/ping.exe', args); | ||
} else if (p === 'darwin') { | ||
//mac osx | ||
args = []; | ||
//TODO: No idea for how to write ping commands in MAC osx | ||
if (config.extra !== false) { | ||
args = args.concat(config.extra ? | ||
config.extra : default_cfg.extra); | ||
} | ||
args.push(addr); | ||
// windows | ||
args = winBuilder.getResult(addr, _config); | ||
ls = cp.spawn(process.env.SystemRoot + '/system32/ping.exe', args); | ||
} else if (p === 'darwin' || p === 'freebsd') { | ||
// mac osx | ||
args = macBuilder.getResult(addr, _config); | ||
ls = cp.spawn('/sbin/ping', args); | ||
} else if (p === 'aix') { | ||
// aix | ||
args = linuxBuilder.getResult(addr, _config); | ||
ls = cp.spawn('/usr/sbin/ping', args); | ||
} | ||
ls.on('error', function (e) { | ||
ls.on('error', function () { | ||
var err = new Error( | ||
"ping.probe: there was an error while executing the ping program. check the path or permissions..."); | ||
util.format( | ||
'ping.probe: %s. %s', | ||
'there was an error while executing the ping program. ', | ||
'Check the path or permissions...' | ||
) | ||
); | ||
deferred.reject(err); | ||
}); | ||
ls.stdout.on('data', function (data) { | ||
@@ -101,10 +77,6 @@ outstring += String(data); | ||
/* | ||
ls.stderr.on('data', function (data) { | ||
//sys.print('stderr: ' + data); | ||
}); | ||
**/ | ||
ls.on('exit', function (code) { | ||
ls.on('close', function (code) { | ||
var result; | ||
var time; | ||
var lines = outstring.split('\n'); | ||
// workaround for windows machines | ||
@@ -115,11 +87,11 @@ // if host is unreachable ping will return | ||
if (p.match(/^win/)) { | ||
var lines = outstring.split('\n'); | ||
// this is my solution on Chinese Windows8 64bit | ||
result = false; | ||
for (var t = 0; t < lines.length; t++) { | ||
if (lines[t].search(/TTL=[0-9]+/i) > 0) { | ||
result = true; | ||
result = false; | ||
for (var line in lines) { | ||
if (line.search(/TTL=[0-9]+/i) > 0) { | ||
result = true; | ||
break; | ||
} | ||
} | ||
// below is not working on My Chinese Windows8 64bit | ||
@@ -134,5 +106,12 @@ /* | ||
*/ | ||
} else { | ||
result = code === 0; | ||
} | ||
else { | ||
result = (code === 0) ? true : false; | ||
for (var t = 0; t < lines.length; t++) { | ||
var match = /time=([0-9\.]+)\s*ms/i.exec(lines[t]); | ||
if (match) { | ||
time = parseFloat(match[1], 10); | ||
break; | ||
} | ||
} | ||
@@ -142,5 +121,7 @@ deferred.resolve({ | ||
alive: result, | ||
output: outstring | ||
output: outstring, | ||
time: time, | ||
}); | ||
}); | ||
return deferred.promise; | ||
@@ -147,0 +128,0 @@ } |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -11,78 +13,34 @@ * LICENSE MIT | ||
//system library | ||
var sys = require('util'), | ||
cp = require('child_process'), | ||
os = require('os'); | ||
// Promise implementation | ||
var ping = require('./ping-promise'); | ||
// TODO: | ||
// 1. Port round trip time to this callback | ||
// 2. However, it may breaks backward compatability | ||
// 3. Need discussion | ||
/** | ||
* Callback after probing given host | ||
* @callback probeCallback | ||
* @param {boolean} isAlive - Whether target is alive or not | ||
* @param {Object} error - Null if no error occurs | ||
*/ | ||
/** | ||
* Class::Ping construtor | ||
* | ||
* @param addr string | ||
* @param cb function (data, err) | ||
* arguments order is based on compatabile issue | ||
* @param {string} addr - Hostname or ip addres | ||
* @param {probeCallback} cb - Callback | ||
* @param {PingConfig} config - Configuration for command ping | ||
*/ | ||
function probe(addr, cb) { | ||
var p = os.platform(); | ||
var ls = null; | ||
var outstring = ""; | ||
function probe(addr, cb, config) { | ||
// Do not reassign function parameter | ||
var _config = config || {}; | ||
if (p === 'linux') { | ||
//linux | ||
ls = cp.spawn('/bin/ping', ['-n', '-w 2', '-c 1', addr]); | ||
} else if (p.match(/^win/)) { | ||
//windows | ||
ls = cp.spawn('C:/windows/system32/ping.exe', ['-n', '1', '-w', '5000', addr]); | ||
} else if (p === 'darwin') { | ||
//mac osx | ||
ls = cp.spawn('/sbin/ping', ['-n', '-t 2', '-c 1', addr]); | ||
} | ||
ls.on('error', function (e) { | ||
var err = new Error('ping.probe: there was an error while executing the ping program. check the path or permissions...'); | ||
cb(null, err); | ||
}); | ||
ls.stdout.on('data', function (data) { | ||
outstring += String(data); | ||
}); | ||
ls.stderr.on('data', function (data) { | ||
//sys.print('stderr: ' + data); | ||
}); | ||
ls.on('exit', function (code) { | ||
var result; | ||
// workaround for windows machines | ||
// if host is unreachable ping will return | ||
// a successfull error code | ||
// so we need to handle this ourself | ||
if (p.match(/^win/)) { | ||
var lines = outstring.split('\n'); | ||
result = false; | ||
for (var t = 0; t < lines.length; t++) { | ||
if (lines[t].search(/TTL=[0-9]+/i) > 0) { | ||
result = true; | ||
break; | ||
} | ||
} | ||
// below is not working on My Chinese Windows8 64bit | ||
/* | ||
for (var t = 0; t < lines.length; t++) { | ||
if (lines[t].match (/[0-9]:/)) { | ||
result = (lines[t].indexOf ("=") != -1); | ||
break; | ||
} | ||
} | ||
*/ | ||
} else { | ||
result = (code === 0) ? true : false; | ||
} | ||
if (cb) { | ||
cb(result, null); | ||
} | ||
}); | ||
return ping.probe(addr, _config).then(function (res) { | ||
cb(res.alive, null); | ||
}).catch(function (err) { | ||
cb(null, err); | ||
}).done(); | ||
} | ||
exports.probe = probe; |
{ | ||
"author": "danielzzz <daniel@zelisko.net> (http://daniel.zelisko.net)", | ||
"contributors": [ | ||
"Mond Wan <mondwan.1015@gmail.com>", | ||
"dougluce <doug@tenousiperochhelical.con.com>", | ||
"weihua44", | ||
"GermanBluefox", | ||
"mabukar", | ||
"microacup <xiangmain@gmail.com>", | ||
"Andrew Fadeev", | ||
"Joshua Pruitt <firefly777@gmail.com>", | ||
"Stephan van Rooij <stephan@svrooij.nl> (http://svrooij.nl)", | ||
"Krispin Schulz <krispinone@googlemail.com> (http://kr1sp1n.io)", | ||
"Kathy Hill", | ||
"mrMuppet", | ||
"Adam Heath <adam@adamheath.me> (http://www.adamheath.me)" | ||
], | ||
"name": "ping", | ||
"description": "a simple wrapper for ping", | ||
"version": "0.1.10", | ||
"version": "0.2.0", | ||
"homepage": "http://github.com/danielzzz/node-ping", | ||
@@ -7,0 +22,0 @@ "repository": { |
121
README.md
#NODE-PING | ||
a ping wrapper for nodejs | ||
@last-modified: 2016-04-24 00:00 | ||
#LICENSE MIT | ||
@@ -20,38 +22,119 @@ | ||
Below are examples extracted from `examples` | ||
##Tradition calls | ||
var ping = require('ping'); | ||
```js | ||
var ping = require('ping'); | ||
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com']; | ||
hosts.forEach(function(host){ | ||
ping.sys.probe(host, function(isAlive){ | ||
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
console.log(msg); | ||
}); | ||
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com']; | ||
hosts.forEach(function(host){ | ||
ping.sys.probe(host, function(isAlive){ | ||
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
console.log(msg); | ||
}); | ||
}); | ||
``` | ||
##Tradition calls with configuration | ||
```js | ||
var cfg = { | ||
timeout: 10, | ||
// WARNING: -i 2 may not work in other platform like window | ||
extra: ["-i 2"], | ||
}; | ||
hosts.forEach(function(host){ | ||
ping.sys.probe(host, function(isAlive){ | ||
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | ||
console.log(msg); | ||
}, cfg); | ||
}); | ||
``` | ||
##Promise wrapper | ||
var ping = require('ping'); | ||
```js | ||
var ping = require('ping'); | ||
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com']; | ||
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com']; | ||
hosts.forEach(function (host) { | ||
ping.promise.probe(host) | ||
.then(function (res) { | ||
console.log(res); | ||
}); | ||
}); | ||
hosts.forEach(function (host) { | ||
ping.promise.probe(host) | ||
.then(function (res) { | ||
console.log(res); | ||
}); | ||
}); | ||
``` | ||
##Promise Wrapper with configable ping options | ||
//Only promise wrapper supports configable ping options | ||
hosts.forEach(function (host) { | ||
// WARNING: -i 2 argument may not work in other platform like window | ||
ping.promise.probe(host, { | ||
timeout: 10, | ||
extra: ["-i 2"] | ||
extra: ["-i 2"], | ||
}).then(function (res) { | ||
console.log(res); | ||
}); | ||
console.log(res); | ||
}); | ||
}); | ||
### Support configuration | ||
Below is the possible configuration | ||
``` | ||
/** | ||
* Cross platform config representation | ||
* @typedef {Object} PingConfig | ||
* @property {boolean} numeric - Map IP address to hostname or not | ||
* @property {number} timeout - Time duration for ping command to exit | ||
* @property {number} min_reply - Exit after sending number of ECHO_REQUEST | ||
* @property {string[]} extra - Optional options does not provided | ||
*/ | ||
``` | ||
### Output specification | ||
* For callback based implementaiton: | ||
```js | ||
/** | ||
* Callback after probing given host | ||
* @callback probeCallback | ||
* @param {boolean} isAlive - Whether target is alive or not | ||
* @param {Object} error - Null if no error occurs | ||
*/ | ||
``` | ||
* For promise based implementation | ||
```js | ||
/** | ||
* Resolved response | ||
* @param {string} host - The input IP address or HOST | ||
* @param {boolean} alive - True for existed host | ||
* @param {string} output - Raw stdout from system ping | ||
* @param {number} time - Time (float) in ms for first successful ping response | ||
*/ | ||
{ | ||
host: addr, | ||
alive: result, | ||
output: outstring, | ||
time: time, | ||
} | ||
``` | ||
#### Note | ||
* Since `ping` in this module relies on the `ping` from underlying platform, | ||
arguments in `PingConfig.extra` will definitely be varied across different | ||
platforms. | ||
* However, `numeric`, `timeout` and `min_reply` have been abstracted. Values for | ||
them are expected to be cross platform. | ||
* By setting `numeric`, `timeout` or `min_reply` to false, you can run `ping` | ||
without corresponding arguments. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
17228
12
424
140
1