From 2c54d64aed09fe6ce0532faec1b81b3d6e75176f Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 16 Mar 2021 11:17:37 +0100 Subject: [PATCH] code refactoring --- CHANGELOG.md | 1 + docs/history.html | 5 +++++ docs/index.html | 6 +++--- lib/internet.js | 18 +++++++++--------- lib/processes.js | 12 +++++------- lib/util.js | 4 ++-- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 424601d..9912deb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ For major (breaking) changes - **version 4, 3 and 2** - see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 5.6.6 | 2021-03-16 | code refactoring | | 5.6.5 | 2021-03-15 | `cpuTemperature()` fix (linux) | | 5.6.4 | 2021-03-15 | `sanitizeShellString()` and other security improvements | | 5.6.3 | 2021-03-14 | `sanitizeShellString()` improvement | diff --git a/docs/history.html b/docs/history.html index 79a20ab..a5fd53c 100644 --- a/docs/history.html +++ b/docs/history.html @@ -56,6 +56,11 @@ + + 5.6.6 + 2021-03-16 + code refactoring + 5.6.5 2021-03-15 diff --git a/docs/index.html b/docs/index.html index f87088f..ac465fb 100644 --- a/docs/index.html +++ b/docs/index.html @@ -170,7 +170,7 @@
systeminformation
 
-
New Version: 5.6.5
+
New Version: 5.6.6
@@ -201,7 +201,7 @@
-
14,185
+
14,225
Lines of code
@@ -209,7 +209,7 @@
Downloads last month
-
397
+
398
Dependents
diff --git a/lib/internet.js b/lib/internet.js index 2dc0d5a..23e6d6f 100644 --- a/lib/internet.js +++ b/lib/internet.js @@ -67,7 +67,7 @@ function inetChecksite(url, callback) { let args = ['-I', '--connect-timeout', '5', '-m', '5']; args.push(urlSanitized); let cmd = 'curl'; - util.execSave(cmd, args).then((stdout) => { + util.execSafe(cmd, args).then((stdout) => { const lines = stdout.split('\n'); let statusCode = lines[0] && lines[0].indexOf(' ') >= 0 ? parseInt(lines[0].split(' ')[1], 10) : 404; result.status = statusCode || 404; @@ -161,18 +161,18 @@ function inetLatency(host, callback) { let filt; if (_linux || _freebsd || _openbsd || _netbsd || _darwin) { if (_linux) { - params = '-c 2 -w 3 ' + hostSanitized; + params = ['-c', '2', '-w', '3', hostSanitized]; filt = 'rtt'; } if (_freebsd || _openbsd || _netbsd) { - params = '-c 2 -t 3 ' + hostSanitized; + params = ['-c', '2', '-t', '3', hostSanitized]; filt = 'round-trip'; } if (_darwin) { - params = '-c2 -t3 ' + hostSanitized; + params = ['-c2', '-t3', hostSanitized]; filt = 'avg'; } - util.execSave('ping', params.split(' ')).then((stdout) => { + util.execSafe('ping', params).then((stdout) => { let result = null; if (stdout) { const lines = stdout.split('\n').filter(line => line.indexOf(filt) >= 0).join('\n'); @@ -190,9 +190,9 @@ function inetLatency(host, callback) { }); } if (_sunos) { - const params = '-s -a ' + hostSanitized + ' 56 2'; + const params = ['-s', '-a', hostSanitized, '56', '2']; const filt = 'avg'; - util.execSave('ping', params.split(' '), { timeout: 3000 }).then((stdout) => { + util.execSafe('ping', params, { timeout: 3000 }).then((stdout) => { let result = null; if (stdout) { const lines = stdout.split('\n').filter(line => line.indexOf(filt) >= 0).join('\n'); @@ -211,8 +211,8 @@ function inetLatency(host, callback) { if (_windows) { let result = null; try { - const params = hostSanitized + ' -n 1'; - util.execSave('ping', params.split(' '), util.execOptsWin).then((stdout) => { + const params = [hostSanitized, '-n', '1']; + util.execSafe('ping', params, util.execOptsWin).then((stdout) => { if (stdout) { let lines = stdout.split('\r\n'); lines.shift(); diff --git a/lib/processes.js b/lib/processes.js index 510577a..c569a22 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -18,8 +18,6 @@ const fs = require('fs'); const path = require('path'); const exec = require('child_process').exec; const execSync = require('child_process').execSync; -const execFile = require('child_process').execFile; - const util = require('./util'); @@ -170,7 +168,7 @@ function services(srv, callback) { } let args = (_darwin) ? ['-caxo', 'pcpu,pmem,pid,command'] : ['-axo', 'pcpu,pmem,pid,command']; if (srvString !== '' && srvs.length > 0) { - util.execSave('ps', args).then((stdout) => { + util.execSafe('ps', args).then((stdout) => { if (stdout) { let lines = stdout.replace(/ +/g, ' ').replace(/,+/g, '.').split('\n'); srvs.forEach(function (srv) { @@ -268,7 +266,7 @@ function services(srv, callback) { } } else { args = ['-o', 'comm']; - util.execSave('ps', args).then((stdout) => { + util.execSafe('ps', args).then((stdout) => { if (stdout) { let lines = stdout.replace(/ +/g, ' ').replace(/,+/g, '.').split('\n'); srvs.forEach(function (srv) { @@ -1058,9 +1056,9 @@ function processLoad(proc, callback) { } if (_darwin || _linux || _freebsd || _openbsd || _netbsd) { - const params = '-axo pid,pcpu,pmem,comm'; - execFile('ps', params.split(' '), { maxBuffer: 1024 * 20000 }, function (error, stdout) { - if (!error) { + const params = ['-axo', 'pid,pcpu,pmem,comm']; + util.execSafe('ps', params).then((stdout) => { + if (stdout) { let procStats = []; let lines = stdout.toString().split('\n').filter(function (line) { if (processesString === '*') { return true; } diff --git a/lib/util.js b/lib/util.js index d5c689f..d7cd6bf 100644 --- a/lib/util.js +++ b/lib/util.js @@ -390,7 +390,7 @@ function powerShell(cmd) { }); } -function execSave(cmd, args, options) { +function execSafe(cmd, args, options) { let result = ''; options = options || {}; @@ -962,7 +962,7 @@ exports.wmic = wmic; exports.darwinXcodeExists = darwinXcodeExists; exports.getVboxmanage = getVboxmanage; exports.powerShell = powerShell; -exports.execSave = execSave; +exports.execSafe = execSafe; exports.nanoSeconds = nanoSeconds; exports.countUniqueLines = countUniqueLines; exports.countLines = countLines;