From 91f94abbd1737d6e105301ade7503d5cf585e135 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Mon, 18 Jan 2021 19:16:02 +0800 Subject: [PATCH] lib: refactor to use validateArray PR-URL: https://github.com/nodejs/node/pull/36982 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- lib/buffer.js | 5 ++--- .../bootstrap/switches/does_own_process_state.js | 8 ++------ lib/internal/child_process.js | 16 ++++++++++------ lib/internal/dns/utils.js | 7 ++----- lib/internal/process/per_thread.js | 6 ++---- lib/internal/worker.js | 5 ++--- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index c2372a55b4418f..591ddd632383fb 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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(); diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 0d60fb1f4595d1..2924e7f8cc17fa 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -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}]`); } diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 8512ae342fde65..7bd9a058ea56de 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -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); diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 27d25c92ad93aa..40f5ba0088e83e 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -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 diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index b3bd2de58ea3ff..4fdf0ba8764455 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -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); } diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 7d90e0b91c086a..38f3ba66f214e6 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -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); }