Skip to content

Commit 79dd591

Browse files
committed
lib: enforce use of Array from primordials
PR-URL: #30635 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent ac7beba commit 79dd591

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+140
-69
lines changed

lib/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ rules:
99
- groups: [[ "&&", "||" ]]
1010
no-restricted-globals:
1111
- error
12+
- name: Array
13+
message: "Use `const { Array } = primordials;` instead of the global."
1214
- name: JSON
1315
message: "Use `const { JSON } = primordials;` instead of the global."
1416
- name: Math

lib/_http_client.js

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

2424
const {
25+
ArrayIsArray,
2526
ObjectAssign,
2627
ObjectKeys,
2728
ObjectSetPrototypeOf,
@@ -218,7 +219,7 @@ function ClientRequest(input, options, cb) {
218219
}
219220
}
220221

221-
const headersArray = Array.isArray(options.headers);
222+
const headersArray = ArrayIsArray(options.headers);
222223
if (!headersArray) {
223224
if (options.headers) {
224225
const keys = ObjectKeys(options.headers);

lib/_http_outgoing.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
ArrayIsArray,
2526
ObjectCreate,
2627
ObjectDefineProperty,
2728
ObjectKeys,
@@ -350,7 +351,7 @@ function _storeHeader(firstLine, headers) {
350351
const entry = headers[key];
351352
processHeader(this, state, entry[0], entry[1], false);
352353
}
353-
} else if (Array.isArray(headers)) {
354+
} else if (ArrayIsArray(headers)) {
354355
for (const entry of headers) {
355356
processHeader(this, state, entry[0], entry[1], true);
356357
}
@@ -444,7 +445,7 @@ function _storeHeader(firstLine, headers) {
444445
function processHeader(self, state, key, value, validate) {
445446
if (validate)
446447
validateHeaderName(key);
447-
if (Array.isArray(value)) {
448+
if (ArrayIsArray(value)) {
448449
if (value.length < 2 || !isCookieField(key)) {
449450
for (var i = 0; i < value.length; i++)
450451
storeHeader(self, state, key, value[i], validate);
@@ -686,7 +687,7 @@ function connectionCorkNT(conn) {
686687
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
687688
this._trailer = '';
688689
const keys = ObjectKeys(headers);
689-
const isArray = Array.isArray(headers);
690+
const isArray = ArrayIsArray(headers);
690691
var field, value;
691692
for (var i = 0, l = keys.length; i < l; i++) {
692693
var key = keys[i];

lib/_stream_readable.js

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

2424
const {
25+
ArrayIsArray,
2526
ObjectDefineProperty,
2627
ObjectSetPrototypeOf,
2728
} = primordials;
@@ -70,7 +71,7 @@ function prependListener(emitter, event, fn) {
7071
// the prependListener() method. The goal is to eventually remove this hack.
7172
if (!emitter._events || !emitter._events[event])
7273
emitter.on(event, fn);
73-
else if (Array.isArray(emitter._events[event]))
74+
else if (ArrayIsArray(emitter._events[event]))
7475
emitter._events[event].unshift(fn);
7576
else
7677
emitter._events[event] = [fn, emitter._events[event]];

lib/_stream_writable.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'use strict';
2727

2828
const {
29+
Array,
2930
ObjectDefineProperty,
3031
ObjectSetPrototypeOf,
3132
} = primordials;

lib/_tls_common.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
ArrayIsArray,
2526
ObjectCreate,
2627
} = primordials;
2728

@@ -105,7 +106,7 @@ exports.createSecureContext = function createSecureContext(options) {
105106
// Add CA before the cert to be able to load cert's issuer in C++ code.
106107
const { ca } = options;
107108
if (ca) {
108-
if (Array.isArray(ca)) {
109+
if (ArrayIsArray(ca)) {
109110
for (i = 0; i < ca.length; ++i) {
110111
val = ca[i];
111112
validateKeyOrCertOption('ca', val);
@@ -121,7 +122,7 @@ exports.createSecureContext = function createSecureContext(options) {
121122

122123
const { cert } = options;
123124
if (cert) {
124-
if (Array.isArray(cert)) {
125+
if (ArrayIsArray(cert)) {
125126
for (i = 0; i < cert.length; ++i) {
126127
val = cert[i];
127128
validateKeyOrCertOption('cert', val);
@@ -140,7 +141,7 @@ exports.createSecureContext = function createSecureContext(options) {
140141
const key = options.key;
141142
const passphrase = options.passphrase;
142143
if (key) {
143-
if (Array.isArray(key)) {
144+
if (ArrayIsArray(key)) {
144145
for (i = 0; i < key.length; ++i) {
145146
val = key[i];
146147
// eslint-disable-next-line eqeqeq
@@ -240,7 +241,7 @@ exports.createSecureContext = function createSecureContext(options) {
240241
}
241242

242243
if (options.crl) {
243-
if (Array.isArray(options.crl)) {
244+
if (ArrayIsArray(options.crl)) {
244245
for (i = 0; i < options.crl.length; i++) {
245246
c.context.addCRL(options.crl[i]);
246247
}
@@ -257,7 +258,7 @@ exports.createSecureContext = function createSecureContext(options) {
257258
if (!toBuf)
258259
toBuf = require('internal/crypto/util').toBuf;
259260

260-
if (Array.isArray(options.pfx)) {
261+
if (ArrayIsArray(options.pfx)) {
261262
for (i = 0; i < options.pfx.length; i++) {
262263
const pfx = options.pfx[i];
263264
const raw = pfx.buf ? pfx.buf : pfx;

lib/buffer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
'use strict';
2323

2424
const {
25+
Array,
26+
ArrayIsArray,
2527
MathFloor,
2628
MathMin,
2729
MathTrunc,
@@ -483,7 +485,7 @@ function fromObject(obj) {
483485
return fromArrayLike(obj);
484486
}
485487

486-
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
488+
if (obj.type === 'Buffer' && ArrayIsArray(obj.data)) {
487489
return fromArrayLike(obj.data);
488490
}
489491
}
@@ -518,7 +520,7 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
518520

519521
Buffer.concat = function concat(list, length) {
520522
let i;
521-
if (!Array.isArray(list)) {
523+
if (!ArrayIsArray(list)) {
522524
throw new ERR_INVALID_ARG_TYPE('list', 'Array', list);
523525
}
524526

lib/child_process.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
ArrayIsArray,
2526
ObjectAssign,
2627
ObjectDefineProperty,
2728
ObjectPrototypeHasOwnProperty,
@@ -63,7 +64,7 @@ function fork(modulePath /* , args, options */) {
6364
let options = {};
6465
let args = [];
6566
let pos = 1;
66-
if (pos < arguments.length && Array.isArray(arguments[pos])) {
67+
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
6768
args = arguments[pos++];
6869
}
6970

@@ -96,7 +97,7 @@ function fork(modulePath /* , args, options */) {
9697

9798
if (typeof options.stdio === 'string') {
9899
options.stdio = stdioStringToArray(options.stdio, 'ipc');
99-
} else if (!Array.isArray(options.stdio)) {
100+
} else if (!ArrayIsArray(options.stdio)) {
100101
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
101102
// and stderr from the parent if silent isn't set.
102103
options.stdio = stdioStringToArray(
@@ -186,7 +187,7 @@ function execFile(file /* , args, options, callback */) {
186187

187188
// Parse the optional positional parameters.
188189
let pos = 1;
189-
if (pos < arguments.length && Array.isArray(arguments[pos])) {
190+
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
190191
args = arguments[pos++];
191192
} else if (pos < arguments.length && arguments[pos] == null) {
192193
pos++;
@@ -404,7 +405,7 @@ function normalizeSpawnArguments(file, args, options) {
404405
if (file.length === 0)
405406
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');
406407

407-
if (Array.isArray(args)) {
408+
if (ArrayIsArray(args)) {
408409
args = args.slice(0);
409410
} else if (args == null) {
410411
args = [];

lib/dgram.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
'use strict';
2323

2424
const {
25+
Array,
26+
ArrayIsArray,
2527
ObjectDefineProperty,
2628
ObjectSetPrototypeOf,
2729
} = primordials;
@@ -592,7 +594,7 @@ Socket.prototype.send = function(buffer,
592594
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
593595
}
594596

595-
if (!Array.isArray(buffer)) {
597+
if (!ArrayIsArray(buffer)) {
596598
if (typeof buffer === 'string') {
597599
list = [ Buffer.from(buffer) ];
598600
} else if (!isUint8Array(buffer)) {

lib/domain.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// unless they address existing, critical bugs.
2828

2929
const {
30+
Array,
3031
ObjectDefineProperty,
3132
ReflectApply,
3233
} = primordials;

lib/events.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
Array,
2526
MathMin,
2627
ObjectCreate,
2728
ObjectDefineProperty,

lib/internal/child_process.js

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

33
const {
4+
ArrayIsArray,
45
ObjectDefineProperty,
56
ObjectSetPrototypeOf,
67
} = primordials;
@@ -357,7 +358,7 @@ ChildProcess.prototype.spawn = function(options) {
357358
// Let child process know about opened IPC channel
358359
if (options.envPairs === undefined)
359360
options.envPairs = [];
360-
else if (!Array.isArray(options.envPairs)) {
361+
else if (!ArrayIsArray(options.envPairs)) {
361362
throw new ERR_INVALID_ARG_TYPE('options.envPairs',
362363
'Array',
363364
options.envPairs);
@@ -370,7 +371,7 @@ ChildProcess.prototype.spawn = function(options) {
370371
validateString(options.file, 'options.file');
371372
this.spawnfile = options.file;
372373

373-
if (Array.isArray(options.args))
374+
if (ArrayIsArray(options.args))
374375
this.spawnargs = options.args;
375376
else if (options.args === undefined)
376377
this.spawnargs = [];
@@ -606,7 +607,7 @@ function setupChannel(target, channel, serializationMode) {
606607
}
607608
}
608609

609-
assert(Array.isArray(target._handleQueue));
610+
assert(ArrayIsArray(target._handleQueue));
610611
const queue = target._handleQueue;
611612
target._handleQueue = null;
612613

@@ -905,7 +906,7 @@ function getValidStdio(stdio, sync) {
905906
// Replace shortcut with an array
906907
if (typeof stdio === 'string') {
907908
stdio = stdioStringToArray(stdio);
908-
} else if (!Array.isArray(stdio)) {
909+
} else if (!ArrayIsArray(stdio)) {
909910
throw new ERR_INVALID_OPT_VALUE('stdio', inspect(stdio));
910911
}
911912

lib/internal/console/constructor.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// console. It's exported for backwards compatibility.
55

66
const {
7+
ArrayFrom,
8+
ArrayIsArray,
79
ObjectDefineProperties,
810
ObjectDefineProperty,
911
ObjectKeys,
@@ -39,11 +41,6 @@ const kTraceBegin = 'b'.charCodeAt(0);
3941
const kTraceEnd = 'e'.charCodeAt(0);
4042
const kTraceInstant = 'n'.charCodeAt(0);
4143

42-
const {
43-
isArray: ArrayIsArray,
44-
from: ArrayFrom,
45-
} = Array;
46-
4744
// Lazy loaded for startup performance.
4845
let cliTable;
4946

lib/internal/dns/utils.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
'use strict';
2+
3+
const {
4+
ArrayIsArray,
5+
} = primordials;
6+
27
const errors = require('internal/errors');
38
const { isIP } = require('internal/net');
49
const {
@@ -38,7 +43,7 @@ class Resolver {
3843
}
3944

4045
setServers(servers) {
41-
if (!Array.isArray(servers)) {
46+
if (!ArrayIsArray(servers)) {
4247
throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
4348
}
4449

lib/internal/errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// message may change, the code should not.
1212

1313
const {
14+
ArrayIsArray,
1415
MathAbs,
1516
ObjectDefineProperty,
1617
ObjectKeys,
@@ -611,7 +612,7 @@ function isStackOverflowError(err) {
611612

612613
function oneOf(expected, thing) {
613614
assert(typeof thing === 'string', '`thing` has to be of type string');
614-
if (Array.isArray(expected)) {
615+
if (ArrayIsArray(expected)) {
615616
const len = expected.length;
616617
assert(len > 0,
617618
'At least one expected value needs to be specified');

lib/internal/fixed_queue.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
const {
4+
Array,
5+
} = primordials;
6+
37
// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
48
const kSize = 2048;
59
const kMask = kSize - 1;

lib/internal/fs/streams.js

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

33
const {
4+
Array,
45
MathMin,
56
ObjectDefineProperty,
67
ObjectSetPrototypeOf,

lib/internal/fs/utils.js

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

33
const {
4+
ArrayIsArray,
45
ObjectSetPrototypeOf,
56
ReflectOwnKeys,
67
} = primordials;
@@ -542,7 +543,7 @@ const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
542543
});
543544

544545
const validateBufferArray = hideStackFrames((buffers, propName = 'buffers') => {
545-
if (!Array.isArray(buffers))
546+
if (!ArrayIsArray(buffers))
546547
throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers);
547548

548549
for (let i = 0; i < buffers.length; i++) {

lib/internal/http2/compat.js

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

33
const {
4+
ArrayIsArray,
45
ObjectAssign,
56
ObjectCreate,
67
ObjectKeys,
@@ -617,7 +618,7 @@ class Http2ServerResponse extends Stream {
617618
headers = statusMessage;
618619

619620
let i;
620-
if (Array.isArray(headers)) {
621+
if (ArrayIsArray(headers)) {
621622
for (i = 0; i < headers.length; i++) {
622623
const header = headers[i];
623624
this[kSetHeader](header[0], header[1]);

0 commit comments

Comments
 (0)