lib: refactor to use validateArray

PR-URL: https://github.com/nodejs/node/pull/36982
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
ZiJian Liu 2021-01-18 19:16:02 +08:00 committed by Antoine du Hamel
parent 900c9f5041
commit e279304954
6 changed files with 20 additions and 27 deletions

View File

@ -100,6 +100,7 @@ const {
hideStackFrames
} = require('internal/errors');
const {
validateArray,
validateBuffer,
validateInteger,
validateString
@ -534,9 +535,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) {
if (!ArrayIsArray(list)) {
throw new ERR_INVALID_ARG_TYPE('list', 'Array', list);
}
validateArray(list, 'list');
if (list.length === 0)
return new FastBuffer();

View File

@ -24,13 +24,11 @@ if (credentials.implementsPosixCredentials) {
const {
parseFileMode,
validateArray,
validateString
} = require('internal/validators');
function wrapPosixCredentialSetters(credentials) {
const {
ArrayIsArray,
} = primordials;
const {
codes: {
ERR_INVALID_ARG_TYPE,
@ -63,9 +61,7 @@ function wrapPosixCredentialSetters(credentials) {
}
function setgroups(groups) {
if (!ArrayIsArray(groups)) {
throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups);
}
validateArray(groups, 'groups');
for (let i = 0; i < groups.length; i++) {
validateId(groups[i], `groups[${i}]`);
}

View File

@ -29,7 +29,11 @@ const {
ERR_MISSING_ARGS
}
} = require('internal/errors');
const { validateString, validateOneOf } = require('internal/validators');
const {
validateArray,
validateOneOf,
validateString,
} = require('internal/validators');
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
@ -377,12 +381,12 @@ ChildProcess.prototype.spawn = function(options) {
validateString(options.file, 'options.file');
this.spawnfile = options.file;
if (ArrayIsArray(options.args))
this.spawnargs = options.args;
else if (options.args === undefined)
if (options.args === undefined) {
this.spawnargs = [];
else
throw new ERR_INVALID_ARG_TYPE('options.args', 'Array', options.args);
} else {
validateArray(options.args, 'options.args');
this.spawnargs = options.args;
}
const err = this._handle.spawn(options);

View File

@ -1,7 +1,6 @@
'use strict';
const {
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypeMap,
@ -14,7 +13,7 @@ const {
const errors = require('internal/errors');
const { isIP } = require('internal/net');
const { validateInt32 } = require('internal/validators');
const { validateArray, validateInt32 } = require('internal/validators');
const {
ChannelWrap,
strerror,
@ -60,9 +59,7 @@ class Resolver {
}
setServers(servers) {
if (!ArrayIsArray(servers)) {
throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
}
validateArray(servers, 'servers');
// Cache the original servers because in the event of an error while
// setting the servers, c-ares won't have any servers available for

View File

@ -5,7 +5,6 @@
// thread and the worker threads.
const {
ArrayIsArray,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSplice,
@ -35,6 +34,7 @@ const {
}
} = require('internal/errors');
const format = require('internal/util/inspect').format;
const { validateArray } = require('internal/validators');
const constants = internalBinding('constants').os.signals;
function assert(x, msg) {
@ -55,9 +55,7 @@ function getFastAPIs(binding) {
_hrtime.hrtime();
if (time !== undefined) {
if (!ArrayIsArray(time)) {
throw new ERR_INVALID_ARG_TYPE('time', 'Array', time);
}
validateArray(time, 'time');
if (time.length !== 2) {
throw new ERR_OUT_OF_RANGE('time', 2, time.length);
}

View File

@ -57,6 +57,7 @@ const {
} = workerIo;
const { deserializeError } = require('internal/error_serdes');
const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url');
const { validateArray } = require('internal/validators');
const {
ownsProcessState,
@ -109,9 +110,7 @@ class Worker extends EventEmitter {
}
let argv;
if (options.argv) {
if (!ArrayIsArray(options.argv)) {
throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv);
}
validateArray(options.argv, 'options.argv');
argv = ArrayPrototypeMap(options.argv, String);
}