From 3cd6ec92e1505ad6a7559e3f2194755b75aff974 Mon Sep 17 00:00:00 2001 From: keeramis Date: Thu, 25 Jan 2024 23:20:25 -0800 Subject: [PATCH] minor --- src/cellular-device.js | 2 +- src/device.js | 2 +- src/device.test.js | 4 ++-- src/network-device.js | 43 +++++++++++++++----------------------- src/network-device.test.js | 8 ++----- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/cellular-device.js b/src/cellular-device.js index 6e2e3dd..b1e3117 100644 --- a/src/cellular-device.js +++ b/src/cellular-device.js @@ -38,7 +38,7 @@ const CellularDevice = base => class extends base { * * @param {Object} [options] Options. * @param {Number} [options.timeout] Timeout (milliseconds). - * @return {Promise} + * @return {Promise} */ async getIccid({ timeout = globalOptions.requestTimeout } = {}) { const r = await this.sendRequest(Request.CELLULAR_GET_ICCID, null /* msg */, { timeout }); diff --git a/src/device.js b/src/device.js index 5b4c89d..8ce044d 100644 --- a/src/device.js +++ b/src/device.js @@ -463,7 +463,7 @@ class Device extends DeviceBase { * * @return {Promise} List of asssets available on the device. */ - async getAssetInfo({ timeout = globalOptions.timeout }) { + async getAssetInfo({ timeout = globalOptions.timeout } = {}) { if (this.isInDfuMode) { throw new StateError('Cannot get information when the device is in DFU mode'); } diff --git a/src/device.test.js b/src/device.test.js index 0e001fa..cc2b343 100644 --- a/src/device.test.js +++ b/src/device.test.js @@ -246,7 +246,7 @@ describe('Device', () => { sinon.stub(device, 'sendProtobufRequest').resolves(expectedAssetInfo); sinon.stub(device, 'isInDfuMode').value(false); - const result = await device.getAssetInfo({}); + const result = await device.getAssetInfo(); expect(device.sendProtobufRequest).to.have.property('callCount', 1); expect(result).to.eql(expectedAssetInfo); @@ -283,7 +283,7 @@ describe('Device', () => { sinon.stub(device, 'sendProtobufRequest').resolves(expectedAssetInfo); sinon.stub(device, 'isInDfuMode').value(true); - await expect(device.getAssetInfo({})).to.be.eventually.rejectedWith(StateError, 'Cannot get information when the device is in DFU mode'); + await expect(device.getAssetInfo()).to.be.eventually.rejectedWith(StateError, 'Cannot get information when the device is in DFU mode'); }); describe('Device', () => { diff --git a/src/network-device.js b/src/network-device.js index 6e43716..30fbb56 100644 --- a/src/network-device.js +++ b/src/network-device.js @@ -34,21 +34,7 @@ const InterfaceConfigurationSource = fromProtobufEnum(proto.InterfaceConfigurati }); /** -* Converts a given IP address and version into a dotted-decimal format. -* -* @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6' -* @param {string} version 'v4' or 'v6; -* @returns {string} address in dotted-decimal format -*/ -function convertIPAddress(addr, version) { - if (!addr) { - return null; - } - return version === 'v4' ? Address4.fromInteger(addr).address : Address6.fromByteArray(addr).address; -} - -/** -* Converts a given interface IP address object and version into a dotted-decimal format. +* Converts a given interface IP address object with version into a dotted-decimal format. * * @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6' * @param {string} version 'v4' or 'v6; @@ -56,26 +42,31 @@ function convertIPAddress(addr, version) { */ function convertInterfaceAddress(ifaceAddr, version) { let res = null; - if (ifaceAddr && ifaceAddr.address && ifaceAddr.address[version] && ifaceAddr.address[version].address) { - res = convertIPAddress(ifaceAddr.address[version].address, version); + let prefixLength = null; + if (ifaceAddr && ifaceAddr.address && ifaceAddr.address[version]) { + const addrObj = ifaceAddr.address[version]; + res = convertIPAddress(addrObj, version); + } + if (ifaceAddr && ifaceAddr.prefixLength) { + prefixLength = ifaceAddr.prefixLength; } - const prefixLength = ifaceAddr.prefixLength; return res ? `${res}/${prefixLength}` : null; } /** -* Converts a given dns IP address and version into a dotted-decimal format. +* Converts a given IP address object with version into a dotted-decimal address string. * * @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6' * @param {string} version 'v4' or 'v6; * @returns {string} address in dotted-decimal format */ -function convertDnsAddress(dnsAddr, version) { - if (dnsAddr && dnsAddr[version] && dnsAddr[version].address) { - return convertIPAddress(dnsAddr[version].address, version); - } else { - return null; +function convertIPAddress(addr, version) { + if (addr && addr.address) { + const val = addr.address; + const res = version === 'v4' ? Address4.fromInteger(val) : Address6.fromByteArray(val); + return res.address; } + return null; } /** @@ -189,7 +180,7 @@ const NetworkDevice = base => class extends base { addresses: ipv4Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v4')), gateway: convertIPAddress(ipv4Config.gateway, 'v4'), peer: convertIPAddress(ipv4Config.peer, 'v4'), - dns: ipv4Config.dns.map((addr) => convertDnsAddress(addr, 'v4')), + dns: ipv4Config.dns.map((addr) => convertIPAddress(addr, 'v4')), source: InterfaceConfigurationSource.fromProtobuf(ipv4Config.source) }; } @@ -198,7 +189,7 @@ const NetworkDevice = base => class extends base { result.ipv6Config = { addresses: ipv6Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v6')), gateway: convertIPAddress(ipv6Config.gateway, 'v6'), - dns: ipv6Config.dns.map((addr) => convertDnsAddress(addr, 'v6')), + dns: ipv6Config.dns.map((addr) => convertIPAddress(addr, 'v6')), source: InterfaceConfigurationSource.fromProtobuf(ipv6Config.source) }; } diff --git a/src/network-device.test.js b/src/network-device.test.js index 3212dd4..49af383 100644 --- a/src/network-device.test.js +++ b/src/network-device.test.js @@ -39,9 +39,7 @@ describe('NetworkDevice', () => { 'ipv4Config': { 'dns': [ { - 'v4': { - 'address': 3232257567 - } + 'address': 3232257567 } ], 'addresses': [ @@ -58,9 +56,7 @@ describe('NetworkDevice', () => { 'ipv6Config': { 'dns': [ { - 'v6': { - 'address': Buffer.from([2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 15]) - } + 'address': Buffer.from([2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 15]) } ], 'addresses': [