Skip to content

Commit 96b1293

Browse files
DevJunztrivikr
authored andcommitted
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: #64959 Assisted-by: claude:fable-5 Signed-off-by: JunHwan Choi <devjunsday@gmail.com> PR-URL: #65344 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent e7a69c4 commit 96b1293

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

lib/internal/blocklist.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const {
4646
ERR_INVALID_ARG_TYPE,
4747
} = require('internal/errors').codes;
4848

49-
const { validateInt32, validateString } = require('internal/validators');
49+
const { validateArray, validateInt32, validateString } = require('internal/validators');
5050

5151
function parseCIDR(cidr) {
5252
validateString(cidr, 'cidr');
@@ -131,9 +131,7 @@ class BlockList {
131131
* @param {string} [family]
132132
*/
133133
addAddresses(addresses, family = 'ipv4') {
134-
if (!ArrayIsArray(addresses)) {
135-
throw new ERR_INVALID_ARG_TYPE('addresses', 'Array', addresses);
136-
}
134+
validateArray(addresses, 'addresses');
137135
validateString(family, 'family');
138136
const handles = [];
139137
for (let i = 0; i < addresses.length; i++) {
@@ -215,9 +213,7 @@ class BlockList {
215213
* @param {string[]} cidrs
216214
*/
217215
addCIDRs(cidrs) {
218-
if (!ArrayIsArray(cidrs)) {
219-
throw new ERR_INVALID_ARG_TYPE('cidrs', 'Array', cidrs);
220-
}
216+
validateArray(cidrs, 'cidrs');
221217
// Validate and parse all entries first so that an exception mid-array
222218
// does not leave the blocklist half-modified.
223219
const parsed = [];

lib/internal/histogram.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayIsArray,
54
Float64Array,
65
Map,
76
MapPrototypeEntries,
@@ -33,6 +32,7 @@ const {
3332
} = require('internal/errors');
3433

3534
const {
35+
validateArray,
3636
validateInteger,
3737
validateNumber,
3838
validateObject,
@@ -539,8 +539,7 @@ class Histogram {
539539
percentilesAt(percentiles) {
540540
if (!isHistogram(this))
541541
throw new ERR_INVALID_THIS('Histogram');
542-
if (!ArrayIsArray(percentiles))
543-
throw new ERR_INVALID_ARG_TYPE('percentiles', 'Array', percentiles);
542+
validateArray(percentiles, 'percentiles');
544543
for (let i = 0; i < percentiles.length; i++) {
545544
validateNumber(percentiles[i], `percentiles[${i}]`);
546545
if (NumberIsNaN(percentiles[i]) ||

test/parallel/test-blocklist.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,34 @@ const util = require('util');
339339
});
340340
}
341341

342+
{
343+
// addAddresses() and addCIDRs() must throw the same errors for non-array
344+
// input regardless of how the checks are implemented internally.
345+
const blockList = new BlockList();
346+
for (const [value, received] of [
347+
['x', "type string ('x')"],
348+
[123, 'type number (123)'],
349+
[{}, 'an instance of Object'],
350+
[null, 'null'],
351+
[undefined, 'undefined'],
352+
[1n, 'type bigint (1n)'],
353+
[true, 'type boolean (true)'],
354+
]) {
355+
assert.throws(() => blockList.addAddresses(value), {
356+
code: 'ERR_INVALID_ARG_TYPE',
357+
name: 'TypeError',
358+
message: 'The "addresses" argument must be an instance of Array. ' +
359+
`Received ${received}`,
360+
});
361+
assert.throws(() => blockList.addCIDRs(value), {
362+
code: 'ERR_INVALID_ARG_TYPE',
363+
name: 'TypeError',
364+
message: 'The "cidrs" argument must be an instance of Array. ' +
365+
`Received ${received}`,
366+
});
367+
}
368+
}
369+
342370
{
343371
// Test addAddresses() batch insert.
344372
const blockList = new BlockList();

test/parallel/test-perf-hooks-histogram-analysis.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,24 @@ const { inspect } = require('util');
229229
const unsorted = h.percentilesAt([99, 50, 90]);
230230
assert.strictEqual(unsorted.get(50), h.percentile(50));
231231

232-
// Validation
232+
// Validation: non-array input must throw the same error regardless of how
233+
// the check is implemented internally.
234+
for (const [value, received] of [
235+
['x', "type string ('x')"],
236+
[123, 'type number (123)'],
237+
[{}, 'an instance of Object'],
238+
[null, 'null'],
239+
[undefined, 'undefined'],
240+
[1n, 'type bigint (1n)'],
241+
[true, 'type boolean (true)'],
242+
]) {
243+
assert.throws(() => h.percentilesAt(value), {
244+
code: 'ERR_INVALID_ARG_TYPE',
245+
name: 'TypeError',
246+
message: 'The "percentiles" argument must be an instance of Array. ' +
247+
`Received ${received}`,
248+
});
249+
}
233250
assert.throws(() => h.percentilesAt('not array'),
234251
{ code: 'ERR_INVALID_ARG_TYPE' });
235252
assert.throws(() => h.percentilesAt([0]),

0 commit comments

Comments
 (0)