diff --git a/README.md b/README.md index 7920967..15e564e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ a ping wrapper for nodejs -@last-modified: 2020-12-19 +@last-modified: 2020-12-26 # LICENSE MIT @@ -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 @@ -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 diff --git a/lib/parser/base.js b/lib/parser/base.js index bea52b7..9fe762e 100644 --- a/lib/parser/base.js +++ b/lib/parser/base.js @@ -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 @@ -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', diff --git a/lib/parser/factory.js b/lib/parser/factory.js index a1a29e9..a794f18 100644 --- a/lib/parser/factory.js +++ b/lib/parser/factory.js @@ -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 || {}; @@ -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; diff --git a/lib/parser/linux.js b/lib/parser/linux.js index b9d6df2..2eb7be8 100644 --- a/lib/parser/linux.js +++ b/lib/parser/linux.js @@ -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); diff --git a/lib/parser/mac.js b/lib/parser/mac.js index 05ea851..3954c0d 100644 --- a/lib/parser/mac.js +++ b/lib/parser/mac.js @@ -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); diff --git a/lib/parser/win.js b/lib/parser/win.js index 7a771f7..ee330f8 100644 --- a/lib/parser/win.js +++ b/lib/parser/win.js @@ -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}$/; } diff --git a/lib/ping-promise.js b/lib/ping-promise.js index e96afdd..72974c6 100644 --- a/lib/ping-promise.js +++ b/lib/ping-promise.js @@ -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 () { diff --git a/test/fixture/answer.json b/test/fixture/answer.json index 6a8966d..062b088 100644 --- a/test/fixture/answer.json +++ b/test/fixture/answer.json @@ -1,5 +1,6 @@ { "macos_en_sample1": { + "inputHost": "whatever", "host": "google.com", "numeric_host": "172.217.24.46", "alive": true, @@ -18,6 +19,7 @@ "stddev": "0.424" }, "linux_en_sample1": { + "inputHost": "whatever", "host": "localhost", "numeric_host": "127.0.0.1", "alive": true, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -161,6 +170,7 @@ "packetLoss": "0.000" }, "window_ja_sample1": { + "inputHost": "whatever", "host": "google.com", "numeric_host": "216.58.197.142", "alive": true, @@ -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, @@ -197,6 +208,7 @@ "packetLoss": "0.000" }, "window_zh_sample1": { + "inputHost": "whatever", "host": "google.com", "numeric_host": "216.58.203.14", "alive": true, @@ -215,6 +227,7 @@ "packetLoss": "0.000" }, "window_zh_sample2": { + "inputHost": "whatever", "host": "google.com", "numeric_host": "216.58.203.14", "alive": true, @@ -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, @@ -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, @@ -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,