From 68b4e5b27301c39c595382c8ea5a02f2fc94659b Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 30 May 2023 07:19:51 +0200 Subject: [PATCH] usb() fix parsing JSON (mac OS) --- CHANGELOG.md | 1 + docs/history.html | 5 +++++ lib/osinfo.js | 2 +- lib/usb.js | 20 ++++++++++++++++++-- lib/util.js | 12 ++++++------ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dae05f..9cd97a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ For major (breaking) changes - **version 4, 3 and 2** - see end of page. | Version | Date | Comment | | ------- | ---------- | --------------------------------------------------------------------------------------------------- | +| 5.17.16 | 2023-05-30 | `usb()` fix parsing JSON (mac OS) | | 5.17.15 | 2023-05-29 | `powershell()` added NoProfile to speed up powershell (windows) | | 5.17.14 | 2023-05-29 | `diskLayout()`, `osInfo()` fix parsing issues (mac OS) | | 5.17.13 | 2023-05-24 | `typings` fix typings dynamicData, networkInterfaceDatass | diff --git a/docs/history.html b/docs/history.html index ab79b36..04fe5e5 100644 --- a/docs/history.html +++ b/docs/history.html @@ -57,6 +57,11 @@ + + 5.17.16 + 2023-05-30 + usb() fix parsing JSON (mac OS) + 5.17.15 2023-05-29 diff --git a/lib/osinfo.js b/lib/osinfo.js index 3160084..154ca38 100644 --- a/lib/osinfo.js +++ b/lib/osinfo.js @@ -1053,7 +1053,7 @@ function getUniqueMacAdresses() { for (let dev in ifaces) { if ({}.hasOwnProperty.call(ifaces, dev)) { ifaces[dev].forEach(function (details) { - if (details?.mac && details.mac !== '00:00:00:00:00:00') { + if (details && details.mac && details.mac !== '00:00:00:00:00:00') { const mac = details.mac.toLowerCase(); if (macs.indexOf(mac) === -1) { macs.push(mac); diff --git a/lib/usb.js b/lib/usb.js index 9b9f897..b882520 100644 --- a/lib/usb.js +++ b/lib/usb.js @@ -125,20 +125,36 @@ function parseDarwinUsb(usb, id) { if (lines[i] !== '{' && lines[i] !== '}' && lines[i + 1] && lines[i + 1].trim() !== '}') { lines[i] = lines[i] + ','; } + + lines[i] = lines[i].replace(':Yes,', ':"Yes",'); lines[i] = lines[i].replace(': Yes,', ': "Yes",'); + lines[i] = lines[i].replace(': Yes', ': "Yes"'); + lines[i] = lines[i].replace(':No,', ':"No",'); lines[i] = lines[i].replace(': No,', ': "No",'); + lines[i] = lines[i].replace(': No', ': "No"'); + + // In this case (("com.apple.developer.driverkit.transport.usb")) + lines[i] = lines[i].replace('((', '').replace('))', ''); + + // In case we have <923c11> we need make it "<923c11>" for correct JSON parse + const match = /<(\w+)>/.exec(lines[i]); + if (match) { + const number = match[0]; + lines[i] = lines[i].replace(number, `"${number}"`); + } } const usbObj = JSON.parse(lines.join('\n')); - const removableDrive = usbObj['Built-In'].toLowerCase() !== 'yes' && usbObj['non-removable'].toLowerCase() === 'no'; + const removableDrive = (usbObj['Built-In'] ? usbObj['Built-In'].toLowerCase() !== 'yes' : true) && (usbObj['non-removable'] ? usbObj['non-removable'].toLowerCase() === 'no' : true); result.bus = null; result.deviceId = null; result.id = usbObj['USB Address'] || null; result.name = usbObj['kUSBProductString'] || usbObj['USB Product Name'] || null; result.type = getDarwinUsbType((usbObj['kUSBProductString'] || usbObj['USB Product Name'] || '').toLowerCase() + (removableDrive ? ' removable' : '')); - result.removable = usbObj['non-removable'].toLowerCase() === 'no'; + result.removable = usbObj['non-removable'] ? usbObj['non-removable'].toLowerCase() || '' === 'no' : true; result.vendor = usbObj['kUSBVendorString'] || usbObj['USB Vendor Name'] || null; result.manufacturer = usbObj['kUSBVendorString'] || usbObj['USB Vendor Name'] || null; + result.maxPower = null; result.serialNumber = usbObj['kUSBSerialNumberString'] || null; diff --git a/lib/util.js b/lib/util.js index 9aac93f..df27342 100644 --- a/lib/util.js +++ b/lib/util.js @@ -315,7 +315,7 @@ function getWmic() { if (!fs.existsSync(wmicPath)) { try { const wmicPathArray = execSync('WHERE WMIC', execOptsWin).toString().split('\r\n'); - if (wmicPathArray?.length) { + if (wmicPathArray && wmicPathArray.length) { wmicPath = wmicPathArray[0]; } else { wmicPath = 'wmic'; @@ -385,7 +385,7 @@ function powerShellStart() { encoding: 'UTF-8', env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' }) }); - if (_psChild?.pid) { + if (_psChild && _psChild.pid) { _psPersistent = true; _psChild.stdout.on('data', function (data) { _psResult = _psResult + data.toString('utf8'); @@ -436,7 +436,7 @@ function powerShell(cmd) { start: new Date() }); try { - if (_psChild?.pid) { + if (_psChild && _psChild.pid) { _psChild.stdin.write(_psToUTF8 + 'echo ' + _psCmdStart + id + _psIdSeperator + '; ' + os.EOL + cmd + os.EOL + 'echo ' + _psCmdSeperator + os.EOL); } } catch (e) { @@ -464,7 +464,7 @@ function powerShell(cmd) { resolve(result); }); } - if (child?.pid) { + if (child && child.pid) { child.stdout.on('data', function (data) { result = result + data.toString('utf8'); }); @@ -514,7 +514,7 @@ function execSafe(cmd, args, options) { resolve(result); }); } - if (child?.pid) { + if (child && child.pid) { child.stdout.on('data', function (data) { result += data.toString(); }); @@ -576,7 +576,7 @@ function smartMonToolsInstalled() { if (_windows) { try { const pathArray = execSync('WHERE smartctl 2>nul', execOptsWin).toString().split('\r\n'); - if (pathArray?.length) { + if (pathArray && pathArray.length) { _smartMonToolsInstalled = pathArray[0].indexOf(':\\') >= 0; } else { _smartMonToolsInstalled = false;