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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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;

0 commit comments

Comments
 (0)