From 6bebdc29518251d5db35d6c216bc24f644c0872e Mon Sep 17 00:00:00 2001 From: JunHwan Choi Date: Mon, 17 Aug 2026 14:04:53 +0900 Subject: [PATCH] lib: use validateArray for array arguments Three call sites still duplicated the ArrayIsArray check that validateArray already performs. Both files were already importing other validators next to these checks. The error code, argument name and expected type are unchanged, so the thrown error stays identical. The ArrayIsArray primordial is no longer used in histogram.js and is dropped from its destructuring; blocklist.js still uses it elsewhere. The existing tests only asserted the error code, so assertions covering the full error message are added for all three call sites. They pass both before and after this change. Refs: https://github.com/nodejs/node/pull/64959 Assisted-by: claude:fable-5 Signed-off-by: JunHwan Choi --- lib/internal/blocklist.js | 10 ++----- lib/internal/histogram.js | 5 ++-- test/parallel/test-blocklist.js | 28 +++++++++++++++++++ .../test-perf-hooks-histogram-analysis.js | 19 ++++++++++++- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/internal/blocklist.js b/lib/internal/blocklist.js index f290c7ada405..5b5023b0f923 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 c16c894dd147..8906b8ed5cff 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 08a293bb6a81..ca4e96b0cbdd 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 acc2b5a7eb2d..7069ec92ac92 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]),