Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "npm run test",
"request": "launch",
"runtimeArgs": [
"run-script",
"test"
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}

]
}
4 changes: 3 additions & 1 deletion lib/parser/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ LinuxParser.prototype._processHeader = function (line) {
// Get host and numeric_host
var tokens = line.split(' ');
var isProbablyIPv4 = tokens[1].indexOf('(') === -1;
var ipv4Regex = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;

if (isProbablyIPv4) {
this._response.host = tokens[1];
this._response.numeric_host = tokens[2].slice(1, -1);
var ipv4Match = tokens[2].match(ipv4Regex);
this._response.numeric_host = ipv4Match ? ipv4Match[1] : tokens[2].slice(1, -1);
} else {
// Normalise into either a 2 or 3 element array
var foundAddresses = tokens
Expand Down
17 changes: 9 additions & 8 deletions lib/parser/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var util = require('util');
var base = require('./base');
const {getFloatOrUnknown} = require('../utils');

/**
* @module ping/parser/mac
Expand Down Expand Up @@ -42,7 +43,7 @@ MacParser.prototype._processBody = function (line) {
if (count >= 3) {
var regExp = /([0-9.]+)[ ]*ms/;
var match = regExp.exec(line);
this._times.push(parseFloat(match[1], 10));
this._times.push(getFloatOrUnknown(match[1]));
}

// Change state if it see a '---'
Expand All @@ -56,26 +57,26 @@ MacParser.prototype._processBody = function (line) {
* @param {string} line - A line from system ping
*/
MacParser.prototype._processFooter = function (line) {
var packetLoss = line.match(/ ([\d.]+)%/);
var packetLoss = line.match(/ ([\d.]+(\.?[\d]*))%/);
if (packetLoss) {
this._response.packetLoss = parseFloat(packetLoss[1], 10);
this._response.packetLoss = getFloatOrUnknown(packetLoss[1]);
}

// XXX: Assume number of keywords '/' more than 3
var count = (line.match(/[/]/g) || []).length;
if (count >= 3) {
var regExp = /([0-9.]+)/g;
var regExp = /(([0-9.]+)|nan)/g;
// XXX: Assume min avg max stddev
var m1 = regExp.exec(line);
var m2 = regExp.exec(line);
var m3 = regExp.exec(line);
var m4 = regExp.exec(line);

if (m1 && m2 && m3 && m4) {
this._response.min = parseFloat(m1[1], 10);
this._response.avg = parseFloat(m2[1], 10);
this._response.max = parseFloat(m3[1], 10);
this._response.stddev = parseFloat(m4[1], 10);
this._response.min = getFloatOrUnknown(m1[1]);
this._response.avg = getFloatOrUnknown(m2[1]);
this._response.max = getFloatOrUnknown(m3[1]);
this._response.stddev = getFloatOrUnknown(m4[1]);
this._changeState(base.STATES.END);
}

Expand Down
13 changes: 7 additions & 6 deletions lib/parser/win.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var util = require('util');
var base = require('./base');
const {getFloatOrUnknown} = require('../utils');

/**
* @module ping/parser/win
Expand Down Expand Up @@ -100,7 +101,7 @@ WinParser.prototype._processIPV6Body = function (line) {
var regExp = /([0-9.]+)/;
var match = regExp.exec(timeKVP);

this._times.push(parseFloat(match[1], 10));
this._times.push(getFloatOrUnknown(match[1]));
}
};

Expand Down Expand Up @@ -133,7 +134,7 @@ WinParser.prototype._processIPV4Body = function (line) {
var regExp = /([0-9.]+)/;
var match = regExp.exec(timeKVP);

this._times.push(parseFloat(match[1], 10));
this._times.push(getFloatOrUnknown(match[1]));
}
};

Expand Down Expand Up @@ -163,7 +164,7 @@ WinParser.prototype._processBody = function (line) {
WinParser.prototype._processFooter = function (line) {
var packetLoss = line.match(/([\d.]+)%/);
if (packetLoss) {
this._response.packetLoss = parseFloat(packetLoss[1], 10);
this._response.packetLoss = getFloatOrUnknown(packetLoss[1]);
}

// XXX: Assume there is a keyword ms
Expand All @@ -175,9 +176,9 @@ WinParser.prototype._processFooter = function (line) {
var m3 = regExp.exec(line);

if (m1 && m2 && m3) {
this._response.min = parseFloat(m1[1], 10);
this._response.max = parseFloat(m2[1], 10);
this._response.avg = parseFloat(m3[1], 10);
this._response.min = getFloatOrUnknown(m1[1]);
this._response.max = getFloatOrUnknown(m2[1]);
this._response.avg = getFloatOrUnknown(m3[1]);
this._changeState(base.STATES.END);
}
}
Expand Down
23 changes: 23 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Utilities for ping module
* @module ping/utils
*/

'use strict';

/**
* Convert a value to float or 'unknown' if not a number
* @param {string} value - Value to convert
* @returns {number|string} Converted float or 'unknown' string if NaN
*/
function getFloatOrUnknown(value) {
var parsed = parseFloat(value);
if (isNaN(parsed)) {
return 'unknown';
}
return parsed;
}

module.exports = {
getFloatOrUnknown: getFloatOrUnknown,
};
49 changes: 47 additions & 2 deletions test/fixture/answer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
"packetLoss": "unknown",
"stddev": "unknown"
},
"macos_en_sample3": {
"inputHost": "whatever",
"host": "www.taobao.com.danuoyi.tbcache.com",
"numeric_host": "122.225.217.183",
"alive": true,
"output": "PING www.taobao.com.danuoyi.tbcache.com (122.225.217.183): 56 data bytes\n64 bytes from 122.225.217.183: icmp_seq=0 ttl=53 kj=4.628 ms\n\n--- www.taobao.com.danuoyi.tbcache.com ping statistics ---\n1 packets transmitted, 1 packets received, 0.0% packet loss\nround-trip min/avg/max/stddev = 4.628/4.628/4.628/nan ms\n",
"time": 4.628,
"times": [
4.628
],
"min": "4.628",
"max": "4.628",
"avg": "4.628",
"stddev": "0.000",
"packetLoss": "0.000"
},
"linux_en_sample1": {
"inputHost": "whatever",
"host": "localhost",
Expand Down Expand Up @@ -91,6 +107,36 @@
"packetLoss": "100.000",
"stddev": "unknown"
},
"linux_en_sample4": {
"inputHost": "whatever",
"host": "google.com",
"numeric_host": "142.251.29.102",
"alive": true,
"output": "PING google.com (142.251.29.102) 56(84) bytes of data.\n64 bytes from uv-in-f102.1e100.net (142.251.29.102): icmp_seq=1 ttl=110 time=9.16 ms\n\n--- google.com ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 9.162/9.162/9.162/0.000 ms\n",
"time": 9.16,
"times": [
9.16
],
"min": "9.162",
"max": "9.162",
"avg": "9.162",
"packetLoss": "0.000",
"stddev": "0.000"
},
"linux_en_sample5": {
"inputHost": "whatever",
"host": "1.1.1.1",
"alive": false,
"output": "PING 1.1.1.1 (1.1.1.1): 56 data bytes\nping: permission denied (are you root?)\n",
"time": "unknown",
"times": [],
"min": "unknown",
"max": "unknown",
"avg": "unknown",
"stddev": "unknown",
"packetLoss": "unknown",
"numeric_host": "1.1.1.1"
},
"linux_en_v6_sample1": {
"inputHost": "whatever",
"host": "2606:4700:4700::1111",
Expand Down Expand Up @@ -142,8 +188,7 @@
"max": "564.000",
"avg": "555.000",
"stddev": "5.723",
"packetLoss": "0.000",
"stddev": "5.723"
"packetLoss": "0.000"
},
"window_fr_sample1": {
"inputHost": "whatever",
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/linux/en/sample4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PING google.com (142.251.29.102) 56(84) bytes of data.
64 bytes from uv-in-f102.1e100.net (142.251.29.102): icmp_seq=1 ttl=110 time=9.16 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 9.162/9.162/9.162/0.000 ms
2 changes: 2 additions & 0 deletions test/fixture/linux/en/sample5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PING 1.1.1.1 (1.1.1.1): 56 data bytes
ping: permission denied (are you root?)
6 changes: 6 additions & 0 deletions test/fixture/macos/en/sample3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PING www.taobao.com.danuoyi.tbcache.com (122.225.217.183): 56 data bytes
64 bytes from 122.225.217.183: icmp_seq=0 ttl=53 kj=4.628 ms

--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 4.628/4.628/4.628/nan ms
7 changes: 7 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Convert a value to float or 'unknown' if not a number
* @param {string} value - Value to convert
* @returns {number|string} Converted float or 'unknown' string if NaN
*/
export function getFloatOrUnknown(value: string): number | string;
//# sourceMappingURL=utils.d.ts.map