Skip to content

Commit

Permalink
lib: refactor to use validateArray
Browse files Browse the repository at this point in the history
PR-URL: #36982
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Lxxyx authored and ruyadorno committed Jan 21, 2021
1 parent 6125701 commit 91f94ab
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const {
hideStackFrames
} = require('internal/errors');
const {
validateArray,
validateBuffer,
validateInteger,
validateString
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 2 additions & 6 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}]`);
}
Expand Down
16 changes: 10 additions & 6 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypeMap,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// thread and the worker threads.

const {
ArrayIsArray,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSplice,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 91f94ab

Please sign in to comment.