Comparing version 0.2.1 to 0.2.2
@@ -19,3 +19,3 @@ var ping = require("../index"); | ||
timeout: 10, | ||
extra: ["-i 2"], | ||
extra: ["-i", "2"], | ||
}) | ||
@@ -34,3 +34,3 @@ .then(function (res) { | ||
// Below extra arguments may not work in platforms other than linux | ||
extra: ["-i 2"], | ||
extra: ["-i", "2"], | ||
}) | ||
@@ -37,0 +37,0 @@ .then(function (res) { |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -6,2 +8,3 @@ * A builder builds command line arguments for ping in linux environment | ||
var util = require('util'); | ||
var builder = {}; | ||
@@ -8,0 +11,0 @@ |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -6,2 +8,3 @@ * A builder builds command line arguments for ping in mac environment | ||
var util = require('util'); | ||
var builder = {}; | ||
@@ -8,0 +11,0 @@ |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -6,2 +8,3 @@ * A builder builds command line arguments for ping in window environment | ||
var util = require('util'); | ||
var builder = {}; | ||
@@ -51,2 +54,5 @@ | ||
if (_config.timeout) { | ||
// refs #56: Unit problem | ||
// Our timeout is in second while timeout in window is in milliseconds | ||
// so we need to convert our units accordingly | ||
ret = ret.concat([ | ||
@@ -53,0 +59,0 @@ '-w', |
@@ -0,1 +1,3 @@ | ||
/* eslint-disable */ | ||
/** | ||
@@ -2,0 +4,0 @@ * LICENSE MIT |
@@ -20,7 +20,7 @@ 'use strict'; | ||
var Q = require('q'); | ||
var __ = require('underscore'); | ||
// Our library | ||
var linuxBuilder = require('./builder/linux'); | ||
var macBuilder = require('./builder/mac'); | ||
var winBuilder = require('./builder/win'); | ||
var builderFactory = require('./builder/factory'); | ||
var parserFactory = require('./parser/factory'); | ||
@@ -35,29 +35,22 @@ /** | ||
function probe(addr, config) { | ||
var p = os.platform(); | ||
var ls = null; | ||
var outstring = ''; | ||
var deferred = Q.defer(); | ||
var args = []; | ||
// Do not reassign function argument | ||
var _config = config || {}; | ||
if (p === 'linux') { | ||
// linux | ||
args = linuxBuilder.getResult(addr, _config); | ||
ls = cp.spawn('/bin/ping', args); | ||
} else if (p.match(/^win/)) { | ||
// 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); | ||
} | ||
// Convert callback base system command to promise base | ||
var deferred = Q.defer(); | ||
ls.on('error', function () { | ||
// Spawn a ping process | ||
var ping = null; | ||
var platform = os.platform(); | ||
var argumentBuilder = builderFactory.createBuilder(platform); | ||
ping = cp.spawn( | ||
builderFactory.getExecutablePath(platform), | ||
argumentBuilder.getResult(addr, _config) | ||
); | ||
// Initial parser | ||
var parser = parserFactory.createParser(platform); | ||
// Register events from system ping | ||
ping.once('error', function () { | ||
var err = new Error( | ||
@@ -73,39 +66,20 @@ util.format( | ||
ls.stdout.on('data', function (data) { | ||
outstring += String(data); | ||
// Cache all lines from the system ping | ||
var outstring = []; | ||
ping.stdout.on('data', function (data) { | ||
outstring.push(String(data)); | ||
}); | ||
ls.on('close', function (code) { | ||
var result = code === 0; | ||
var time; | ||
var lines = outstring.split('\n'); | ||
// 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/)) { | ||
// this is my solution on Chinese Windows8 64bit | ||
result = false; | ||
for (var i = 0; i < lines.length; i++) { | ||
var line = lines[i]; | ||
if (line.search(/TTL=[0-9]+/i) > 0) { | ||
result = true; | ||
break; | ||
} | ||
} | ||
} | ||
// Parse lines we have on closing system ping | ||
ping.once('close', function () { | ||
// Merge lines we have and split it by \n | ||
var lines = outstring.join('').split('\n'); | ||
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; | ||
} | ||
} | ||
deferred.resolve({ | ||
host: addr, | ||
alive: result, | ||
output: outstring, | ||
time: time, | ||
}); | ||
// Parse line one by one | ||
__.each(lines, parser.eat, parser); | ||
// Get result | ||
var ret = parser.getResult(); | ||
deferred.resolve(ret); | ||
}); | ||
@@ -112,0 +86,0 @@ |
{ | ||
"author": "danielzzz <daniel@zelisko.net> (http://daniel.zelisko.net)", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "grunt test" | ||
}, | ||
"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)", | ||
"BlessJah <blessjah@jacekowski.org>" | ||
"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)", | ||
"BlessJah <blessjah@jacekowski.org>", | ||
"jritsema" | ||
], | ||
"name": "ping", | ||
"description": "a simple wrapper for ping", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"homepage": "http://github.com/danielzzz/node-ping", | ||
@@ -35,6 +36,18 @@ "license": "MIT", | ||
"dependencies": { | ||
"q": "1.x" | ||
"q": "1.x", | ||
"underscore": "^1.8.3" | ||
}, | ||
"devDependencies": { | ||
"babel-eslint": "^7.0.0", | ||
"chai": "2.3.0", | ||
"eslint": "^3.8.1", | ||
"eslint-config-airbnb": "^12.0.0", | ||
"eslint-config-eslint": "^3.0.0", | ||
"eslint-plugin-import": "^1.16.0", | ||
"eslint-plugin-jsx-a11y": "^2.2.3", | ||
"eslint-plugin-react": "^6.4.1", | ||
"glob": "^7.1.1", | ||
"grunt": "^1.0.1", | ||
"grunt-mocha-test": "^0.13.2", | ||
"gruntify-eslint": "^3.1.0", | ||
"mocha": "2.5.3", | ||
@@ -41,0 +54,0 @@ "sinon": "1.17.6" |
#NODE-PING | ||
a ping wrapper for nodejs | ||
@last-modified: 2016-04-24 00:00 | ||
@last-modified: 2016-10-21 12:43 | ||
@@ -72,11 +73,13 @@ #LICENSE MIT | ||
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"], | ||
}).then(function (res) { | ||
console.log(res); | ||
}); | ||
```js | ||
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"], | ||
}).then(function (res) { | ||
console.log(res); | ||
}); | ||
}); | ||
``` | ||
@@ -87,3 +90,3 @@ ### Support configuration | ||
``` | ||
```js | ||
/** | ||
@@ -116,15 +119,14 @@ * Cross platform config representation | ||
/** | ||
* Resolved response | ||
* Parsed response | ||
* @typedef {object} PingResponse | ||
* @param {string} host - The input IP address or HOST | ||
* @param {string} numeric_host - Target IP address | ||
* @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 | ||
* @param {string} min - Minimum time for collection records | ||
* @param {string} max - Maximum time for collection records | ||
* @param {string} avg - Average time for collection records | ||
* @param {string} stddev - Standard deviation time for collected records | ||
*/ | ||
{ | ||
host: addr, | ||
alive: result, | ||
output: outstring, | ||
time: time, | ||
} | ||
``` | ||
@@ -143,1 +145,8 @@ | ||
without corresponding arguments. | ||
# Contributing | ||
Before opening a pull request please make sure your changes follow the | ||
[contribution guidelines][1]. | ||
[1]: https://github.com/danielzzz/node-ping/blob/master/CONTRIBUTING.md |
@@ -0,146 +1,187 @@ | ||
'use strict'; | ||
/* global describe it before after*/ | ||
'use strict' | ||
var expect = require('chai').expect | ||
var sinon = require('sinon') | ||
var cp = require('child_process') | ||
var os = require('os') | ||
var events = require('events') | ||
var ping = require('..') | ||
/* eslint no-unused-expressions: 0 */ | ||
var windows_output = "\n\ | ||
Pinging www.some-domain.com [127.0.0.1] with 32 bytes of\n\ | ||
\n\ | ||
Reply from 127.0.0.1: bytes=32 time=564ms TTL=237\n\ | ||
Reply from 127.0.0.1: bytes=32 time=555ms TTL=237\n\ | ||
Reply from 127.0.0.1: bytes=32 time=554ms TTL=237\n\ | ||
Reply from 127.0.0.1: bytes=32 time=548ms TTL=237\n\ | ||
\n\ | ||
Ping statistics for 127.0.0.1:\n\ | ||
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)\n\ | ||
Approximate round trip times in milli-seconds:\n\ | ||
Minimum = 548ms, Maximum = 564ms, Average = 555ms\n\ | ||
" | ||
var expect = require('chai').expect; | ||
var sinon = require('sinon'); | ||
var os = require('os'); | ||
var cp = require('child_process'); | ||
var q = require('q'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var events = require('events'); | ||
var emitter = new events.EventEmitter() | ||
emitter.stdout = emitter | ||
var loadFixturePath = require('./load-fixture-path'); | ||
var ping = require('..'); | ||
function fakePing () { | ||
windows_output.split('\n').forEach(function (line) { | ||
emitter.emit('data', line) | ||
}) | ||
emitter.emit('close', 0) | ||
} | ||
// Some constants | ||
var ANSWER = require('./fixture/answer'); | ||
var PLATFORMS = [ | ||
'window', | ||
'darwin', | ||
'freebsd', | ||
// 'aix', | ||
'linux', | ||
]; | ||
var PLATFORM_TO_EXTRA_ARGUMENTS = { | ||
window: ['-n', '2'], | ||
darwin: ['-c', '2'], | ||
freebsd: ['-c', '2'], | ||
linux: ['-c', '2'], | ||
}; | ||
describe('Ping', function () { | ||
var host = '127.0.0.1' | ||
describe('runs in callback mode', function () { | ||
it('plain pings ' + host, function (done) { | ||
ping.sys.probe(host, function (isAlive) { | ||
expect(isAlive).to.be.true | ||
done() | ||
}) | ||
}) | ||
it('pings ' + host + ' with custom config', function (done) { | ||
ping.sys.probe(host, function (isAlive) { | ||
expect(isAlive).to.be.true | ||
done() | ||
}, {extra: ['-i 2']}) | ||
}) | ||
it('pings ' + host + ' with some default argument gone', function (done) { | ||
ping.sys.probe(host, function (isAlive) { | ||
expect(isAlive).to.be.true | ||
done() | ||
}, {extra: ['-i 2'], timeout: false}) | ||
}) | ||
}) | ||
describe('runs in promise mode', function () { | ||
it('plain pings ' + host, function () { | ||
var promise = ping.promise.probe(host) | ||
.then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.be.above(0) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
it('pings ' + host + ' with custom config', function () { | ||
var promise = ping.promise.probe(host, { | ||
timeout: 10, | ||
extra: ['-i 2'] | ||
}).then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.be.above(0) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
it('pings ' + host + ' with some default argument gone', function () { | ||
var promise = ping.promise.probe(host, { | ||
timeout: false, | ||
extra: ['-i 2'] | ||
}).then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.be.above(0) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
}) | ||
describe('runs in a simulated Windows environment', function () { | ||
// Pretend we're in Windows to test windows-specific features | ||
before(function () { | ||
this.stubs = [ | ||
sinon.stub(cp, 'spawn', function () { return emitter }), | ||
sinon.stub(os, 'platform', function () { return 'windows' }) | ||
] | ||
}) | ||
after(function () { | ||
this.stubs.forEach(function (stub) { | ||
stub.restore() | ||
}) | ||
}) | ||
it('plain pings ' + host, function () { | ||
var promise = ping.promise.probe(host) | ||
.then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.equal(564) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
it('pings ' + host + ' with custom config', function () { | ||
var promise = ping.promise.probe(host, { | ||
timeout: 10, | ||
extra: ['-i 2'] | ||
}).then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.equal(564) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
it('pings ' + host + ' with some default argument gone', function () { | ||
var promise = ping.promise.probe(host, { | ||
timeout: false, | ||
extra: ['-i 2'] | ||
}).then(function (res) { | ||
expect(res.alive).to.be.true | ||
expect(res.time).to.equal(564) | ||
expect(res.host).to.equal(host) | ||
expect(res.output).to.not.be.empty | ||
}) | ||
fakePing() | ||
return promise | ||
}) | ||
}) | ||
}) | ||
var pathToAnswerKey = function (p) { | ||
var basename = path.posix.basename(p, '.txt'); | ||
var dirname = path.posix.basename(path.posix.dirname(p)); | ||
var osname = path.posix.basename( | ||
path.posix.dirname(path.posix.dirname(p)) | ||
); | ||
return [osname, dirname, basename].join('_'); | ||
}; | ||
var mockOutSpawn = function (fp) { | ||
return function () { | ||
var e = new events.EventEmitter(); | ||
e.stdout = e; | ||
var s = fs.createReadStream(fp); | ||
s.on('data', function (line) { | ||
e.emit('data', line); | ||
}); | ||
s.on('close', function () { | ||
e.emit('close', 0); | ||
}); | ||
return e; | ||
}; | ||
}; | ||
var createTestCase = function (platform, pingExecution) { | ||
var stubs = []; | ||
describe(util.format('On %s platform', platform), function () { | ||
var fixturePaths = loadFixturePath(platform); | ||
before(function () { | ||
stubs.push( | ||
sinon.stub(os, 'platform', function () { return platform; }) | ||
); | ||
}); | ||
after(function () { | ||
stubs.forEach(function (stub) { | ||
stub.restore(); | ||
}); | ||
}); | ||
describe('runs with default config', function () { | ||
fixturePaths.forEach(function (fp) { | ||
it( | ||
util.format('Using |%s|', pathToAnswerKey(fp)), | ||
function () { | ||
return pingExecution(fp); | ||
} | ||
); | ||
}); | ||
}); | ||
describe('runs with custom config', function () { | ||
fixturePaths.forEach(function (fp) { | ||
it( | ||
util.format('Using |%s|', pathToAnswerKey(fp)), | ||
function () { | ||
return pingExecution(fp, { | ||
timeout: 10, | ||
extra: PLATFORM_TO_EXTRA_ARGUMENTS[platform], | ||
}); | ||
} | ||
); | ||
}); | ||
}); | ||
describe('runs with custom config with default gone', function () { | ||
fixturePaths.forEach(function (fp) { | ||
it( | ||
util.format('Using |%s|', pathToAnswerKey(fp)), | ||
function () { | ||
return pingExecution(fp, { | ||
timeout: false, | ||
extra: PLATFORM_TO_EXTRA_ARGUMENTS[platform], | ||
}); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}; | ||
describe('Ping in callback mode', function () { | ||
var pingExecution = function (fp, args) { | ||
var deferred = q.defer(); | ||
var stub = sinon.stub( | ||
cp, | ||
'spawn', | ||
mockOutSpawn(fp) | ||
); | ||
var cb = function (isAlive, err) { | ||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
deferred.resolve(isAlive); | ||
} | ||
}; | ||
if (args) { | ||
ping.sys.probe('whatever', cb, args); | ||
} else { | ||
ping.sys.probe('whatever', cb); | ||
} | ||
stub.restore(); | ||
return deferred.promise.then(function (data) { | ||
var answerKey = pathToAnswerKey(fp); | ||
var actualIsAlive = data; | ||
var expectIsAlive = ANSWER[answerKey].alive; | ||
expect(actualIsAlive).to.equal(expectIsAlive); | ||
}); | ||
}; | ||
PLATFORMS.forEach(function (platform) { | ||
createTestCase(platform, pingExecution); | ||
}); | ||
}); | ||
describe('Ping in promise mode', function () { | ||
var pingExecution = function (fp, args) { | ||
var stub = sinon.stub( | ||
cp, | ||
'spawn', | ||
mockOutSpawn(fp) | ||
); | ||
var ret = null; | ||
if (args) { | ||
ret = ping.promise.probe('whatever', args); | ||
} else { | ||
ret = ping.promise.probe('whatever'); | ||
} | ||
stub.restore(); | ||
return ret.then(function (data) { | ||
var answerKey = pathToAnswerKey(fp); | ||
var actualData = data; | ||
var expectData = ANSWER[answerKey]; | ||
expect(actualData).to.deep.equal(expectData); | ||
}); | ||
}; | ||
PLATFORMS.forEach(function (platform) { | ||
createTestCase(platform, pingExecution); | ||
}); | ||
}); |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
74582
36
2258
149
2
14
2
4
+ Addedunderscore@^1.8.3
+ Addedunderscore@1.13.7(transitive)