Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/internal/blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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 = [];
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayIsArray,
Float64Array,
Map,
MapPrototypeEntries,
Expand Down Expand Up @@ -33,6 +32,7 @@ const {
} = require('internal/errors');

const {
validateArray,
validateInteger,
validateNumber,
validateObject,
Expand Down Expand Up @@ -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]) ||
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 18 additions & 1 deletion test/parallel/test-perf-hooks-histogram-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
Loading