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 <devjunsday@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65344
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
JunHwan Choi authored and Antoine du Hamel committed 2026-09-19 12:07:51 +02:00
1 parent f784058482
commit 3e4fc50a4f
4 files changed
+51 -11

No files matched your search

+3 -7
View File
@@ -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 = [];
+2 -3
View File
@@ -1,7 +1,6 @@
'use strict';
const {
ArrayIsArray,
Float64Array,
Map,
MapPrototypeEntries,
@@ -33,6 +32,7 @@ const {
} = require('internal/errors');
const {
validateArray,
validateInteger,
validateNumber,
validateObject,
@@ -539,8 +539,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]) ||
+28
View File
@@ -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();
@@ -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]),