diff --git a/lib/internal/blocklist.js b/lib/internal/blocklist.js index f290c7ada40..5b5023b0f92 100644 --- a/lib/internal/blocklist.js +++ b/lib/internal/blocklist.js @@ -46,7 +46,7 @@ const { ERR_INVALID_ARG_TYPE, } = require('internal/errors').codes; -const { validateInt32, validateString } = require('internal/validators'); +const { validateArray, validateInt32, validateString } = require('internal/validators'); function parseCIDR(cidr) { validateString(cidr, 'cidr'); @@ -131,9 +131,7 @@ class BlockList { * @param {string} [family] */ addAddresses(addresses, family = 'ipv4') { - if (!ArrayIsArray(addresses)) { - throw new ERR_INVALID_ARG_TYPE('addresses', 'Array', addresses); - } + validateArray(addresses, 'addresses'); validateString(family, 'family'); const handles = []; for (let i = 0; i < addresses.length; i++) { @@ -215,9 +213,7 @@ class BlockList { * @param {string[]} cidrs */ addCIDRs(cidrs) { - if (!ArrayIsArray(cidrs)) { - throw new ERR_INVALID_ARG_TYPE('cidrs', 'Array', cidrs); - } + validateArray(cidrs, 'cidrs'); // Validate and parse all entries first so that an exception mid-array // does not leave the blocklist half-modified. const parsed = []; diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index c16c894dd14..8906b8ed5cf 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -1,7 +1,6 @@ 'use strict'; const { - ArrayIsArray, Float64Array, Map, MapPrototypeEntries, @@ -33,6 +32,7 @@ const { } = require('internal/errors'); const { + validateArray, validateInteger, validateNumber, validateObject, @@ -359,8 +359,7 @@ class Histogram { percentilesAt(percentiles) { if (!isHistogram(this)) throw new ERR_INVALID_THIS('Histogram'); - if (!ArrayIsArray(percentiles)) - throw new ERR_INVALID_ARG_TYPE('percentiles', 'Array', percentiles); + validateArray(percentiles, 'percentiles'); for (let i = 0; i < percentiles.length; i++) { validateNumber(percentiles[i], `percentiles[${i}]`); if (NumberIsNaN(percentiles[i]) || diff --git a/test/parallel/test-blocklist.js b/test/parallel/test-blocklist.js index 08a293bb6a8..ca4e96b0cbd 100644 --- a/test/parallel/test-blocklist.js +++ b/test/parallel/test-blocklist.js @@ -339,6 +339,34 @@ const util = require('util'); }); } +{ + // addAddresses() and addCIDRs() must throw the same errors for non-array + // input regardless of how the checks are implemented internally. + const blockList = new BlockList(); + for (const [value, received] of [ + ['x', "type string ('x')"], + [123, 'type number (123)'], + [{}, 'an instance of Object'], + [null, 'null'], + [undefined, 'undefined'], + [1n, 'type bigint (1n)'], + [true, 'type boolean (true)'], + ]) { + assert.throws(() => blockList.addAddresses(value), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "addresses" argument must be an instance of Array. ' + + `Received ${received}`, + }); + assert.throws(() => blockList.addCIDRs(value), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "cidrs" argument must be an instance of Array. ' + + `Received ${received}`, + }); + } +} + { // Test addAddresses() batch insert. const blockList = new BlockList(); diff --git a/test/parallel/test-perf-hooks-histogram-analysis.js b/test/parallel/test-perf-hooks-histogram-analysis.js index acc2b5a7eb2..7069ec92ac9 100644 --- a/test/parallel/test-perf-hooks-histogram-analysis.js +++ b/test/parallel/test-perf-hooks-histogram-analysis.js @@ -229,7 +229,24 @@ const { inspect } = require('util'); const unsorted = h.percentilesAt([99, 50, 90]); assert.strictEqual(unsorted.get(50), h.percentile(50)); - // Validation + // Validation: non-array input must throw the same error regardless of how + // the check is implemented internally. + for (const [value, received] of [ + ['x', "type string ('x')"], + [123, 'type number (123)'], + [{}, 'an instance of Object'], + [null, 'null'], + [undefined, 'undefined'], + [1n, 'type bigint (1n)'], + [true, 'type boolean (true)'], + ]) { + assert.throws(() => h.percentilesAt(value), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "percentiles" argument must be an instance of Array. ' + + `Received ${received}`, + }); + } assert.throws(() => h.percentilesAt('not array'), { code: 'ERR_INVALID_ARG_TYPE' }); assert.throws(() => h.percentilesAt([0]),