Skip to content

Commit 8aeba3c

Browse files
Lxxyxruyadorno
authored andcommitted
lib: refactor to use validateArray
PR-URL: #36982 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 6ef54bb commit 8aeba3c

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

lib/buffer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const {
100100
hideStackFrames
101101
} = require('internal/errors');
102102
const {
103+
validateArray,
103104
validateBuffer,
104105
validateInteger,
105106
validateString
@@ -534,9 +535,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
534535
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
535536

536537
Buffer.concat = function concat(list, length) {
537-
if (!ArrayIsArray(list)) {
538-
throw new ERR_INVALID_ARG_TYPE('list', 'Array', list);
539-
}
538+
validateArray(list, 'list');
540539

541540
if (list.length === 0)
542541
return new FastBuffer();

lib/internal/bootstrap/switches/does_own_process_state.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ if (credentials.implementsPosixCredentials) {
2424

2525
const {
2626
parseFileMode,
27+
validateArray,
2728
validateString
2829
} = require('internal/validators');
2930

3031
function wrapPosixCredentialSetters(credentials) {
31-
const {
32-
ArrayIsArray,
33-
} = primordials;
3432
const {
3533
codes: {
3634
ERR_INVALID_ARG_TYPE,
@@ -63,9 +61,7 @@ function wrapPosixCredentialSetters(credentials) {
6361
}
6462

6563
function setgroups(groups) {
66-
if (!ArrayIsArray(groups)) {
67-
throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups);
68-
}
64+
validateArray(groups, 'groups');
6965
for (let i = 0; i < groups.length; i++) {
7066
validateId(groups[i], `groups[${i}]`);
7167
}

lib/internal/child_process.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ const {
2929
ERR_MISSING_ARGS
3030
}
3131
} = require('internal/errors');
32-
const { validateString, validateOneOf } = require('internal/validators');
32+
const {
33+
validateArray,
34+
validateOneOf,
35+
validateString,
36+
} = require('internal/validators');
3337
const EventEmitter = require('events');
3438
const net = require('net');
3539
const dgram = require('dgram');
@@ -377,12 +381,12 @@ ChildProcess.prototype.spawn = function(options) {
377381
validateString(options.file, 'options.file');
378382
this.spawnfile = options.file;
379383

380-
if (ArrayIsArray(options.args))
381-
this.spawnargs = options.args;
382-
else if (options.args === undefined)
384+
if (options.args === undefined) {
383385
this.spawnargs = [];
384-
else
385-
throw new ERR_INVALID_ARG_TYPE('options.args', 'Array', options.args);
386+
} else {
387+
validateArray(options.args, 'options.args');
388+
this.spawnargs = options.args;
389+
}
386390

387391
const err = this._handle.spawn(options);
388392

lib/internal/dns/utils.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayIsArray,
54
ArrayPrototypeForEach,
65
ArrayPrototypeJoin,
76
ArrayPrototypeMap,
@@ -14,7 +13,7 @@ const {
1413

1514
const errors = require('internal/errors');
1615
const { isIP } = require('internal/net');
17-
const { validateInt32 } = require('internal/validators');
16+
const { validateArray, validateInt32 } = require('internal/validators');
1817
const {
1918
ChannelWrap,
2019
strerror,
@@ -60,9 +59,7 @@ class Resolver {
6059
}
6160

6261
setServers(servers) {
63-
if (!ArrayIsArray(servers)) {
64-
throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
65-
}
62+
validateArray(servers, 'servers');
6663

6764
// Cache the original servers because in the event of an error while
6865
// setting the servers, c-ares won't have any servers available for

lib/internal/process/per_thread.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// thread and the worker threads.
66

77
const {
8-
ArrayIsArray,
98
ArrayPrototypeMap,
109
ArrayPrototypePush,
1110
ArrayPrototypeSplice,
@@ -35,6 +34,7 @@ const {
3534
}
3635
} = require('internal/errors');
3736
const format = require('internal/util/inspect').format;
37+
const { validateArray } = require('internal/validators');
3838
const constants = internalBinding('constants').os.signals;
3939

4040
function assert(x, msg) {
@@ -55,9 +55,7 @@ function getFastAPIs(binding) {
5555
_hrtime.hrtime();
5656

5757
if (time !== undefined) {
58-
if (!ArrayIsArray(time)) {
59-
throw new ERR_INVALID_ARG_TYPE('time', 'Array', time);
60-
}
58+
validateArray(time, 'time');
6159
if (time.length !== 2) {
6260
throw new ERR_OUT_OF_RANGE('time', 2, time.length);
6361
}

lib/internal/worker.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757
} = workerIo;
5858
const { deserializeError } = require('internal/error_serdes');
5959
const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url');
60+
const { validateArray } = require('internal/validators');
6061

6162
const {
6263
ownsProcessState,
@@ -109,9 +110,7 @@ class Worker extends EventEmitter {
109110
}
110111
let argv;
111112
if (options.argv) {
112-
if (!ArrayIsArray(options.argv)) {
113-
throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv);
114-
}
113+
validateArray(options.argv, 'options.argv');
115114
argv = ArrayPrototypeMap(options.argv, String);
116115
}
117116

0 commit comments

Comments
 (0)