Skip to content

Commit

Permalink
Include inputHost in the PingResponse
Browse files Browse the repository at this point in the history
refs #133
  • Loading branch information
Mond Wan committed Dec 26, 2020
1 parent 575d0a8 commit 5241d4f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

a ping wrapper for nodejs

@last-modified: 2020-12-19
@last-modified: 2020-12-26

# LICENSE MIT

Expand Down Expand Up @@ -154,7 +154,8 @@ Below is the possible configuration
/**
* Parsed response
* @typedef {object} PingResponse
* @param {string} host - The input IP address or HOST
* @param {string} inputHost - The input IP address or HOST
* @param {string} host - Parsed host from system command's output
* @param {string} numeric_host - Target IP address
* @param {boolean} alive - True for existed host
* @param {string} output - Raw stdout from system ping
Expand Down Expand Up @@ -189,6 +190,9 @@ Try to install package `iputils`. For example, running `apk add iputils`
* For questions regarding to the implementation of `timeout`, and `deadline`, please checkout discussions in
[#101](https://github.com/danielzzz/node-ping/issues/101)

* For questions regarding to the defintions of `host`, `inputHost`, and `numeric_host`, please checkout
discussions in [#133](https://github.com/danielzzz/node-ping/issues/133)

# Contributing

Before opening a pull request please make sure your changes follow the
Expand Down
5 changes: 4 additions & 1 deletion lib/parser/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var __ = require('underscore');
/**
* Parsed response
* @typedef {object} PingResponse
* @param {string} inputHost - The input IP address or HOST
* @param {string} host - The input IP address or HOST
* @param {string} numeric_host - Target IP address
* @param {boolean} alive - True for existed host
Expand All @@ -22,14 +23,16 @@ var __ = require('underscore');
/**
* @constructor
*
* @param {string} addr - Hostname or ip addres
* @param {PingConfig} config - Config object in probe()
*/
function parser(config) {
function parser(addr, config) {
// Initial state is 0
this._state = 0;

// Initial cache value
this._response = {
inputHost: addr,
host: 'unknown',
alive: false,
output: 'unknown',
Expand Down
9 changes: 5 additions & 4 deletions lib/parser/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ function factory() {}

/**
* Create a parser for a given platform
* @param {string} addr - Hostname or ip addres
* @param {string} platform - Name of the platform
* @param {PingConfig} [config] - Config object in probe()
* @return {object} - Parser
* @throw if given platform is not supported
*/
factory.createParser = function (platform, config) {
factory.createParser = function (addr, platform, config) {
// Avoid function reassignment
var _config = config || {};

Expand All @@ -30,11 +31,11 @@ factory.createParser = function (platform, config) {

var ret = null;
if (builderFactory.isWindow(platform)) {
ret = new WinParser(_config);
ret = new WinParser(addr, _config);
} else if (builderFactory.isMacOS(platform)) {
ret = new MacParser(_config);
ret = new MacParser(addr, _config);
} else if (builderFactory.isLinux(platform)) {
ret = new LinuxParser(_config);
ret = new LinuxParser(addr, _config);
}

return ret;
Expand Down
6 changes: 4 additions & 2 deletions lib/parser/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ var MacParser = require('./mac');

/**
* @constructor
*
* @param {string} addr - Hostname or ip addres
* @param {PingConfig} config - Config object in probe()
*/
function LinuxParser(config) {
base.call(this, config);
function LinuxParser(addr, config) {
base.call(this, addr, config);
}

util.inherits(LinuxParser, base);
Expand Down
6 changes: 4 additions & 2 deletions lib/parser/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ var base = require('./base');

/**
* @constructor
*
* @param {string} addr - Hostname or ip addres
* @param {PingConfig} config - Config object in probe()
*/
function MacParser(config) {
base.call(this, config);
function MacParser(addr, config) {
base.call(this, addr, config);
}

util.inherits(MacParser, base);
Expand Down
6 changes: 4 additions & 2 deletions lib/parser/win.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ var base = require('./base');

/**
* @constructor
*
* @param {string} addr - Hostname or ip addres
* @param {PingConfig} config - Config object in probe()
*/
function WinParser(config) {
base.call(this, config);
function WinParser(addr, config) {
base.call(this, addr, config);
this._ipv4Regex = /^([0-9]{1,3}\.){3}[0-9]{1,3}$/;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ping-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function _probe(addr, config) {
}

// Initial parser
var parser = parserFactory.createParser(platform, _config);
var parser = parserFactory.createParser(addr, platform, _config);

// Register events from system ping
ping.once('error', function () {
Expand Down
19 changes: 18 additions & 1 deletion test/fixture/answer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"macos_en_sample1": {
"inputHost": "whatever",
"host": "google.com",
"numeric_host": "172.217.24.46",
"alive": true,
Expand All @@ -18,6 +19,7 @@
"stddev": "0.424"
},
"linux_en_sample1": {
"inputHost": "whatever",
"host": "localhost",
"numeric_host": "127.0.0.1",
"alive": true,
Expand All @@ -36,6 +38,7 @@
"packetLoss": "0.000"
},
"linux_en_sample2": {
"inputHost": "whatever",
"host": "10.48.249.8",
"numeric_host": "10.48.249.8",
"alive": true,
Expand All @@ -61,6 +64,7 @@
"stddev": "228.927"
},
"linux_en_sample3": {
"inputHost": "whatever",
"host": "10.48.249.150",
"numeric_host": "10.48.249.150",
"alive": false,
Expand All @@ -74,6 +78,7 @@
"stddev": "unknown"
},
"linux_en_v6_sample1": {
"inputHost": "whatever",
"host": "2606:4700:4700::1111",
"numeric_host": "2606:4700:4700::1111",
"alive": true,
Expand All @@ -90,6 +95,7 @@
"stddev": "0.170"
},
"linux_en_v6_sample2": {
"inputHost": "whatever",
"host": "one.one.one.one",
"numeric_host": "2606:4700:4700::1111",
"alive": true,
Expand All @@ -106,6 +112,7 @@
"stddev": "0.117"
},
"window_en_sample1": {
"inputHost": "whatever",
"host": "www.some-domain.com",
"numeric_host": "127.0.0.1",
"alive": true,
Expand All @@ -125,6 +132,7 @@
"stddev": "5.723"
},
"window_fr_sample1": {
"inputHost": "whatever",
"host": "127.0.0.1",
"numeric_host": "127.0.0.1",
"alive": true,
Expand All @@ -143,6 +151,7 @@
"packetLoss": "0.000"
},
"window_fr_sample2": {
"inputHost": "whatever",
"host": "8.8.8.8",
"numeric_host": "8.8.8.8",
"alive": true,
Expand All @@ -161,6 +170,7 @@
"packetLoss": "0.000"
},
"window_ja_sample1": {
"inputHost": "whatever",
"host": "google.com",
"numeric_host": "216.58.197.142",
"alive": true,
Expand All @@ -179,6 +189,7 @@
"packetLoss": "0.000"
},
"window_ja_sample2": {
"inputHost": "whatever",
"host": "8.8.8.8",
"numeric_host": "8.8.8.8",
"alive": true,
Expand All @@ -197,6 +208,7 @@
"packetLoss": "0.000"
},
"window_zh_sample1": {
"inputHost": "whatever",
"host": "google.com",
"numeric_host": "216.58.203.14",
"alive": true,
Expand All @@ -215,6 +227,7 @@
"packetLoss": "0.000"
},
"window_zh_sample2": {
"inputHost": "whatever",
"host": "google.com",
"numeric_host": "216.58.203.14",
"alive": true,
Expand All @@ -233,6 +246,7 @@
"packetLoss": "0.000"
},
"window_zh_sample3": {
"inputHost": "whatever",
"host": "127.0.0.1",
"numeric_host": "127.0.0.1",
"alive": true,
Expand All @@ -251,6 +265,7 @@
"packetLoss": "0.000"
},
"window_ru_sample1": {
"inputHost": "whatever",
"host": "8.8.8.8",
"numeric_host": "8.8.8.8",
"alive": true,
Expand All @@ -268,7 +283,9 @@
"stddev": "2.121",
"packetLoss": "0.000"
},
"window_de_v6_sample": { "host": "google.de",
"window_de_v6_sample": {
"inputHost": "whatever",
"host": "google.de",
"alive": true,
"output": "Ping wird ausgeführt für google.de [2a00:1450:4001:810::2003] von 3001:4cb0:0:f282:ddf1:bec9:1e0:bfa9 mit 32 Bytes Daten:\nAntwort von 2a00:1450:4001:810::2003: Zeit=11ms\nAntwort von 2a00:1450:4001:810::2003: Zeit=11ms\nAntwort von 2a00:1450:4001:810::2003: Zeit=18ms\nAntwort von 2a00:1450:4001:810::2003: Zeit=11ms\n\nPing-Statistik für 2a00:1450:4001:810::2003:\n Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0\n (0% Verlust),\nCa. Zeitangaben in Millisek.:\n Minimum = 11ms, Maximum = 18ms, Mittelwert = 12ms\n",
"time": 11,
Expand Down

0 comments on commit 5241d4f

Please sign in to comment.