From 864a983aacf09ca1152d8fe82367fd0d19e16daf Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 5 Feb 2019 20:34:25 +0100 Subject: [PATCH] inetLatency() Windows fix parse chinese output --- CHANGELOG.md | 1 + docs/history.html | 7 ++++++- lib/internet.js | 6 +++--- lib/util.js | 7 ++++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de7c86b..8b3be0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 4.0.8 | 2019-02-05 | `inetLatency()` Windows fix parse chinese output | | 4.0.7 | 2019-02-05 | `inetLatency()` Windows fix | | 4.0.6 | 2019-02-04 | powershell catch error | | 4.0.5 | 2019-02-03 | updated docs | diff --git a/docs/history.html b/docs/history.html index 7e10b2b..650e80e 100644 --- a/docs/history.html +++ b/docs/history.html @@ -80,9 +80,14 @@ + + 4.0.8 + 2019-02-05 + inetLatency() latency fix parse chinese output + 4.0.7 - 2019-02-06 + 2019-02-05 inetLatency() latency Windows fix diff --git a/lib/internet.js b/lib/internet.js index c21b63d..755a788 100644 --- a/lib/internet.js +++ b/lib/internet.js @@ -160,10 +160,10 @@ function inetLatency(host, callback) { let lines = stdout.toString().split('\r\n'); lines.shift(); lines.forEach(function (line) { - if (line.toLowerCase().indexOf('ms,') > -1) { + if ((line.toLowerCase().match(/ms/g) || []).length === 3) { let l = line.replace(/ +/g, ' ').split(' '); - if (l.length > 8) { - result = parseFloat(l[9]); + if (l.length > 6) { + result = parseFloat(l[l.length - 1]); } } }); diff --git a/lib/util.js b/lib/util.js index 04571ad..cc1031e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -220,7 +220,7 @@ function powerShell(cmd) { let result = ''; - return new Promise((resolve, reject) => { + return new Promise((resolve) => { process.nextTick(() => { try { const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], { @@ -231,7 +231,7 @@ function powerShell(cmd) { child.stdout.on('data', function (data) { result = result + data.toString('utf8'); }); - child.stderr.on('data', function (data) { + child.stderr.on('data', function () { child.kill(); resolve(result); }); @@ -246,7 +246,7 @@ function powerShell(cmd) { try { child.stdin.write(cmd + os.EOL); child.stdin.write('exit' + os.EOL); - child.stdin.end() + child.stdin.end(); } catch (e) { child.kill(); resolve(result); @@ -260,6 +260,7 @@ function powerShell(cmd) { }); }); } + function getCodepage() { if (_windows) { if (!codepage) {