From 515163eff4a6fa32a5102701d5f81470329c33e0 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Mon, 12 Jan 2026 21:25:23 +0100 Subject: [PATCH] code cleanup --- lib/memory.js | 46 +++++++++++++++++++++++----------------------- lib/network.js | 36 ++++++++++++++++++------------------ lib/printer.js | 8 ++++---- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/lib/memory.js b/lib/memory.js index ac984e2..8ea1985 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -161,7 +161,7 @@ function mem(callback) { if (_linux) { try { - fs.readFile('/proc/meminfo', function (error, stdout) { + fs.readFile('/proc/meminfo', (error, stdout) => { if (!error) { const lines = stdout.toString().split('\n'); result.total = parseInt(util.getValue(lines, 'memtotal'), 10); @@ -199,7 +199,7 @@ function mem(callback) { } resolve(result); }); - } catch (e) { + } catch { if (callback) { callback(result); } @@ -210,9 +210,9 @@ function mem(callback) { try { exec( '/sbin/sysctl hw.realmem hw.physmem vm.stats.vm.v_page_count vm.stats.vm.v_wire_count vm.stats.vm.v_active_count vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count vm.stats.vm.v_page_size', - function (error, stdout) { + (error, stdout) => { if (!error) { - let lines = stdout.toString().split('\n'); + const lines = stdout.toString().split('\n'); const pagesize = parseInt(util.getValue(lines, 'vm.stats.vm.v_page_size'), 10); const inactive = parseInt(util.getValue(lines, 'vm.stats.vm.v_inactive_count'), 10) * pagesize; const cache = parseInt(util.getValue(lines, 'vm.stats.vm.v_cache_count'), 10) * pagesize; @@ -236,7 +236,7 @@ function mem(callback) { resolve(result); } ); - } catch (e) { + } catch { if (callback) { callback(result); } @@ -254,11 +254,11 @@ function mem(callback) { try { let sysPpageSize = util.toInt(execSync('sysctl -n vm.pagesize').toString()); pageSize = sysPpageSize || pageSize; - } catch (e) { + } catch { util.noop(); } try { - exec('vm_stat 2>/dev/null | egrep "Pages active|Pages inactive"', function (error, stdout) { + exec('vm_stat 2>/dev/null | egrep "Pages active|Pages inactive"', (error, stdout) => { if (!error) { let lines = stdout.toString().split('\n'); result.active = (parseInt(util.getValue(lines, 'Pages active'), 10) || 0) * pageSize; @@ -266,7 +266,7 @@ function mem(callback) { result.buffcache = result.used - result.active; result.available = result.free + result.buffcache; } - exec('sysctl -n vm.swapusage 2>/dev/null', function (error, stdout) { + exec('sysctl -n vm.swapusage 2>/dev/null', (error, stdout) => { if (!error) { let lines = stdout.toString().split('\n'); if (lines.length > 0) { @@ -291,7 +291,7 @@ function mem(callback) { resolve(result); }); }); - } catch (e) { + } catch { if (callback) { callback(result); } @@ -308,7 +308,7 @@ function mem(callback) { .split('\r\n') .filter((line) => line.trim() !== '') .filter((line, idx) => idx > 0); - lines.forEach(function (line) { + lines.forEach((line) => { if (line !== '') { line = line.trim().split(/\s\s+/); swaptotal = swaptotal + (parseInt(line[0], 10) || 0); @@ -325,7 +325,7 @@ function mem(callback) { } resolve(result); }); - } catch (e) { + } catch { if (callback) { callback(result); } @@ -354,12 +354,12 @@ function memLayout(callback) { if (_linux || _freebsd || _openbsd || _netbsd) { exec( 'export LC_ALL=C; dmidecode -t memory 2>/dev/null | grep -iE "Size:|Type|Speed|Manufacturer|Form Factor|Locator|Memory Device|Serial Number|Voltage|Part Number"; unset LC_ALL', - function (error, stdout) { + (error, stdout) => { if (!error) { - let devices = stdout.toString().split('Memory Device'); + const devices = stdout.toString().split('Memory Device'); devices.shift(); - devices.forEach(function (device) { - let lines = device.split('\n'); + devices.forEach((device) => { + const lines = device.split('\n'); const sizeString = util.getValue(lines, 'Size'); const size = sizeString.indexOf('GB') >= 0 ? parseInt(sizeString, 10) * 1024 * 1024 * 1024 : parseInt(sizeString, 10) * 1024 * 1024; let bank = util.getValue(lines, 'Bank Locator'); @@ -457,7 +457,7 @@ function memLayout(callback) { result[0].voltageMax = voltage; } } - } catch (e) { + } catch { util.noop(); } } @@ -470,7 +470,7 @@ function memLayout(callback) { } if (_darwin) { - exec('system_profiler SPMemoryDataType', function (error, stdout) { + exec('system_profiler SPMemoryDataType', (error, stdout) => { if (!error) { const allLines = stdout.toString().split('\n'); const eccStatus = util.getValue(allLines, 'ecc', ':', true).toLowerCase(); @@ -481,8 +481,8 @@ function memLayout(callback) { hasBank = false; } devices.shift(); - devices.forEach(function (device) { - let lines = device.split('\n'); + devices.forEach((device) => { + const lines = device.split('\n'); const bank = (hasBank ? 'BANK ' : 'DIMM') + lines[0].trim().split('/')[0]; const size = parseInt(util.getValue(lines, ' Size')); if (size) { @@ -567,10 +567,10 @@ function memLayout(callback) { ) .then((stdout, error) => { if (!error) { - let devices = stdout.toString().split(/\n\s*\n/); + const devices = stdout.toString().split(/\n\s*\n/); devices.shift(); - devices.forEach(function (device) { - let lines = device.split('\r\n'); + devices.forEach((device) => { + const lines = device.split('\r\n'); const dataWidth = util.toInt(util.getValue(lines, 'DataWidth', ':')); const totalWidth = util.toInt(util.getValue(lines, 'TotalWidth', ':')); const size = parseInt(util.getValue(lines, 'Capacity', ':'), 10) || 0; @@ -599,7 +599,7 @@ function memLayout(callback) { } resolve(result); }); - } catch (e) { + } catch { if (callback) { callback(result); } diff --git a/lib/network.js b/lib/network.js index 83650a6..85e1d0a 100644 --- a/lib/network.js +++ b/lib/network.js @@ -119,7 +119,7 @@ function getDefaultNetworkInterface() { ifacename = ifacename.split(':')[1].trim(); } } - } catch (e) { + } catch { util.noop(); } if (ifacename) { @@ -143,7 +143,7 @@ function getMacAddresses() { } else { pathToIp = ''; } - } catch (e) { + } catch { pathToIp = ''; } } @@ -172,14 +172,14 @@ function getMacAddresses() { } } } - } catch (e) { + } catch { util.noop(); } } if (_darwin) { try { const cmd = '/sbin/ifconfig'; - let res = execSync(cmd); + const res = execSync(cmd); const lines = res.toString().split('\n'); for (let i = 0; i < lines.length; i++) { if (lines[i] && lines[i][0] !== '\t' && lines[i].indexOf(':') > 0) { @@ -193,7 +193,7 @@ function getMacAddresses() { } } } - } catch (e) { + } catch { util.noop(); } } @@ -223,11 +223,11 @@ function parseLinesWindowsNics(sections, nconfigsections) { try { if ({}.hasOwnProperty.call(sections, i)) { if (sections[i].trim() !== '') { - let lines = sections[i].trim().split('\r\n'); + const lines = sections[i].trim().split('\r\n'); let linesNicConfig = null; try { linesNicConfig = nconfigsections && nconfigsections[i] ? nconfigsections[i].trim().split('\r\n') : []; - } catch (e) { + } catch { util.noop(); } const netEnabled = util.getValue(lines, 'NetEnabled', ':'); @@ -252,7 +252,7 @@ function parseLinesWindowsNics(sections, nconfigsections) { } } } - } catch (e) { + } catch { util.noop(); } } @@ -271,7 +271,7 @@ function getWindowsNics() { const nconfigsections = (data[1] || '').split(/\n\s*\n/); resolve(parseLinesWindowsNics(nsections, nconfigsections)); }); - } catch (e) { + } catch { resolve([]); } }); @@ -319,7 +319,7 @@ function getWindowsDNSsuffixes() { }); return dnsSuffixes; - } catch (error) { + } catch { return { primaryDNS: '', exitCode: 0, @@ -345,7 +345,7 @@ function getWindowsIfaceDNSsuffix(ifaces, ifacename) { dnsSuffix = ''; } return dnsSuffix; - } catch (error) { + } catch { return 'Unknown'; } } @@ -355,7 +355,7 @@ function getWindowsWiredProfilesInformation() { const result = execSync('netsh lan show profiles', util.execOptsWin); const profileList = result.split('\r\nProfile on interface'); return profileList; - } catch (error) { + } catch { if (error.status === 1 && error.stdout.includes('AutoConfig')) { return 'Disabled'; } @@ -369,7 +369,7 @@ function getWindowsWirelessIfaceSSID(interfaceName) { const SSID = result.split('\r\n').shift(); const parseSSID = SSID.split(':').pop().trim(); return parseSSID; - } catch (error) { + } catch { return 'Unknown'; } } @@ -406,7 +406,7 @@ function getWindowsIEEE8021x(connectionType, iface, ifaces) { i8021x.protocol = protocol8021x.split(':').pop(); i8021x.state = 'Enabled'; } - } catch (error) { + } catch { return i8021x; } } else if (connectionType === 'wireless') { @@ -434,7 +434,7 @@ function getWindowsIEEE8021x(connectionType, iface, ifaces) { i8021x.state = i8021xState.split(':').pop(); i8021x.protocol = i8021xProtocol.split(':').pop(); } - } catch (error) { + } catch { if (error.status === 1 && error.stdout.includes('AutoConfig')) { i8021x.state = 'Disabled'; i8021x.protocol = 'Not defined'; @@ -465,9 +465,9 @@ function splitSectionsNics(lines) { } function parseLinesDarwinNics(sections) { - let nics = []; + const nics = []; sections.forEach((section) => { - let nic = { + const nic = { iface: '', mtu: null, mac: '', @@ -1477,7 +1477,7 @@ function networkStatsSingle(iface) { result.operstate = (result.operstate || '').toLowerCase(); result.operstate = result.operstate === 'active' ? 'up' : result.operstate === 'inactive' ? 'down' : 'unknown'; cmd = 'netstat -bdI ' + ifaceSanitized; // lgtm [js/shell-command-constructed-from-input] - exec(cmd, function (error, stdout) { + exec(cmd, (error, stdout) => { if (!error) { lines = stdout.toString().split('\n'); // if there is less than 2 lines, no information for this interface was found diff --git a/lib/printer.js b/lib/printer.js index b48c8b6..12dbb83 100644 --- a/lib/printer.js +++ b/lib/printer.js @@ -117,7 +117,7 @@ function printer(callback) { let result = []; if (_linux || _freebsd || _openbsd || _netbsd) { let cmd = 'cat /etc/cups/printers.conf 2>/dev/null'; - exec(cmd, function (error, stdout) { + exec(cmd, (error, stdout) => { // printers.conf if (!error) { const parts = stdout.toString().split(' { const parts = ('\n' + stdout.toString()).split('\nprinter '); for (let i = 1; i < parts.length; i++) { const printers = parseLinuxLpstatPrinter(parts[i].split('\n'), i); @@ -162,7 +162,7 @@ function printer(callback) { } if (_darwin) { let cmd = 'system_profiler SPPrintersDataType -json'; - exec(cmd, function (error, stdout) { + exec(cmd, (error, stdout) => { if (!error) { try { const outObj = JSON.parse(stdout.toString()); @@ -172,7 +172,7 @@ function printer(callback) { result.push(printer); } } - } catch (e) { + } catch { util.noop(); } }