Skip to content

Commit e6fa5bf

Browse files
authored
lib: use validateArray for array arguments
Replace manual ArrayIsArray checks that throw ERR_INVALID_ARG_TYPE with the shared validateArray helper. The error code, argument name and expected type are unchanged, so the thrown error stays identical. Signed-off-by: greenhead <shren0812@gmail.com> PR-URL: #64959 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com>
1 parent d846ffd commit e6fa5bf

5 files changed

Lines changed: 12 additions & 26 deletions

File tree

lib/internal/streams/iter/broadcast.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
} = require('internal/errors');
3434
const {
3535
validateAbortSignal,
36+
validateArray,
3637
validateInteger,
3738
validateObject,
3839
} = require('internal/validators');
@@ -559,9 +560,7 @@ class BroadcastWriter {
559560
}
560561

561562
writev(chunks, options) {
562-
if (!ArrayIsArray(chunks)) {
563-
throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks);
564-
}
563+
validateArray(chunks, 'chunks');
565564
const signal = getWriterSignal(options);
566565
// Fast path: no signal, writer open, buffer has space
567566
if (this.#canUseWriteFastPath(signal)) {
@@ -620,9 +619,7 @@ class BroadcastWriter {
620619
}
621620

622621
writevSync(chunks) {
623-
if (!ArrayIsArray(chunks)) {
624-
throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks);
625-
}
622+
validateArray(chunks, 'chunks');
626623
if (this.#isClosedOrAborted()) return false;
627624
if (!this.#broadcast[kCanWrite]()) return false;
628625
const converted = convertChunks(chunks);

lib/internal/streams/iter/classic.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// toWritable(writer) -- stream/iter Writer -> classic Writable
1313

1414
const {
15-
ArrayIsArray,
1615
ArrayPrototypePush,
1716
NumberMAX_SAFE_INTEGER,
1817
Promise,
@@ -41,6 +40,7 @@ const {
4140
} = require('internal/errors');
4241

4342
const {
43+
validateArray,
4444
validateInteger,
4545
validateObject,
4646
} = require('internal/validators');
@@ -619,9 +619,7 @@ function fromWritable(writable, options = kNullPrototype) {
619619
},
620620

621621
writev(chunks, options) {
622-
if (!ArrayIsArray(chunks)) {
623-
throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks);
624-
}
622+
validateArray(chunks, 'chunks');
625623
getWriterSignal(options);
626624
if (!isWritable()) {
627625
return PromiseReject(new ERR_STREAM_WRITE_AFTER_END());

lib/internal/streams/iter/push.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// with built-in backpressure.
77

88
const {
9-
ArrayIsArray,
109
ArrayPrototypePush,
1110
PromisePrototypeThen,
1211
PromiseReject,
@@ -21,13 +20,13 @@ const {
2120

2221
const {
2322
codes: {
24-
ERR_INVALID_ARG_TYPE,
2523
ERR_INVALID_STATE,
2624
},
2725
} = require('internal/errors');
2826
const { lazyDOMException } = require('internal/util');
2927
const {
3028
validateAbortSignal,
29+
validateArray,
3130
validateInteger,
3231
} = require('internal/validators');
3332

@@ -629,9 +628,7 @@ class PushWriter {
629628
}
630629

631630
writev(chunks, options) {
632-
if (!ArrayIsArray(chunks)) {
633-
throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks);
634-
}
631+
validateArray(chunks, 'chunks');
635632
const signal = getWriterSignal(options);
636633
if (!signal && this.#queue.canWriteSync()) {
637634
const bytes = convertChunks(chunks);
@@ -648,9 +645,7 @@ class PushWriter {
648645
}
649646

650647
writevSync(chunks) {
651-
if (!ArrayIsArray(chunks)) {
652-
throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks);
653-
}
648+
validateArray(chunks, 'chunks');
654649
const bytes = convertChunks(chunks);
655650
return this.#queue.writeSync(bytes);
656651
}

lib/internal/tls/secure-context.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
} = require('internal/util/types');
2727

2828
const {
29+
validateArray,
2930
validateBuffer,
3031
validateInt32,
3132
validateObject,
@@ -213,10 +214,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options')
213214
}
214215

215216
if (certificateCompression != null) {
216-
if (!ArrayIsArray(certificateCompression)) {
217-
throw new ERR_INVALID_ARG_TYPE(
218-
`${name}.certificateCompression`, 'Array', certificateCompression);
219-
}
217+
validateArray(certificateCompression, `${name}.certificateCompression`);
220218

221219
if (certificateCompression.length > 0) {
222220
// Pack length + algorithm IDs into a single Uint32 for a cheap

lib/tls.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const { canonicalizeIP } = internalBinding('cares_wrap');
7070
const tlsCommon = require('internal/tls/common');
7171
const tlsWrap = require('internal/tls/wrap');
7272
const { domainToASCII } = require('internal/url');
73-
const { validateString } = require('internal/validators');
73+
const { validateArray, validateString } = require('internal/validators');
7474

7575
const {
7676
namespace: {
@@ -206,9 +206,7 @@ function getCACertificates(type = 'default') {
206206
exports.getCACertificates = getCACertificates;
207207

208208
function setDefaultCACertificates(certs) {
209-
if (!ArrayIsArray(certs)) {
210-
throw new ERR_INVALID_ARG_TYPE('certs', 'Array', certs);
211-
}
209+
validateArray(certs, 'certs');
212210

213211
// Verify that all elements in the array are strings
214212
for (let i = 0; i < certs.length; i++) {

0 commit comments

Comments
 (0)