From c9b9c01a89775f8bf3592ccd99faa0d974196596 Mon Sep 17 00:00:00 2001 From: greenhead Date: Sun, 2 Aug 2026 22:59:13 +0900 Subject: [PATCH] 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 --- lib/internal/streams/iter/broadcast.js | 9 +++------ lib/internal/streams/iter/classic.js | 6 ++---- lib/internal/streams/iter/push.js | 11 +++-------- lib/internal/tls/secure-context.js | 6 ++---- lib/tls.js | 6 ++---- 5 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lib/internal/streams/iter/broadcast.js b/lib/internal/streams/iter/broadcast.js index 808c254425bd..50c7bf0f9cb2 100644 --- a/lib/internal/streams/iter/broadcast.js +++ b/lib/internal/streams/iter/broadcast.js @@ -33,6 +33,7 @@ const { } = require('internal/errors'); const { validateAbortSignal, + validateArray, validateInteger, validateObject, } = require('internal/validators'); @@ -554,9 +555,7 @@ class BroadcastWriter { } writev(chunks, options) { - if (!ArrayIsArray(chunks)) { - throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks); - } + validateArray(chunks, 'chunks'); const signal = getWriterSignal(options); // Fast path: no signal, writer open, buffer has space if (this.#canUseWriteFastPath(signal)) { @@ -615,9 +614,7 @@ class BroadcastWriter { } writevSync(chunks) { - if (!ArrayIsArray(chunks)) { - throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks); - } + validateArray(chunks, 'chunks'); if (this.#isClosedOrAborted()) return false; if (!this.#broadcast[kCanWrite]()) return false; const converted = convertChunks(chunks); diff --git a/lib/internal/streams/iter/classic.js b/lib/internal/streams/iter/classic.js index 6796fa4cefc3..0769348a3981 100644 --- a/lib/internal/streams/iter/classic.js +++ b/lib/internal/streams/iter/classic.js @@ -12,7 +12,6 @@ // toWritable(writer) -- stream/iter Writer -> classic Writable const { - ArrayIsArray, ArrayPrototypePush, NumberMAX_SAFE_INTEGER, Promise, @@ -41,6 +40,7 @@ const { } = require('internal/errors'); const { + validateArray, validateInteger, validateObject, } = require('internal/validators'); @@ -619,9 +619,7 @@ function fromWritable(writable, options = kNullPrototype) { }, writev(chunks, options) { - if (!ArrayIsArray(chunks)) { - throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks); - } + validateArray(chunks, 'chunks'); getWriterSignal(options); if (!isWritable()) { return PromiseReject(new ERR_STREAM_WRITE_AFTER_END()); diff --git a/lib/internal/streams/iter/push.js b/lib/internal/streams/iter/push.js index 1586ee6e743b..ffac6dea9307 100644 --- a/lib/internal/streams/iter/push.js +++ b/lib/internal/streams/iter/push.js @@ -6,7 +6,6 @@ // with built-in backpressure. const { - ArrayIsArray, ArrayPrototypePush, PromisePrototypeThen, PromiseReject, @@ -21,13 +20,13 @@ const { const { codes: { - ERR_INVALID_ARG_TYPE, ERR_INVALID_STATE, }, } = require('internal/errors'); const { isError, lazyDOMException } = require('internal/util'); const { validateAbortSignal, + validateArray, validateInteger, } = require('internal/validators'); @@ -631,9 +630,7 @@ class PushWriter { } writev(chunks, options) { - if (!ArrayIsArray(chunks)) { - throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks); - } + validateArray(chunks, 'chunks'); const signal = getWriterSignal(options); if (!signal && this.#queue.canWriteSync()) { const bytes = convertChunks(chunks); @@ -650,9 +647,7 @@ class PushWriter { } writevSync(chunks) { - if (!ArrayIsArray(chunks)) { - throw new ERR_INVALID_ARG_TYPE('chunks', 'Array', chunks); - } + validateArray(chunks, 'chunks'); const bytes = convertChunks(chunks); return this.#queue.writeSync(bytes); } diff --git a/lib/internal/tls/secure-context.js b/lib/internal/tls/secure-context.js index 3e05bdbc3a8b..597d4fce9271 100644 --- a/lib/internal/tls/secure-context.js +++ b/lib/internal/tls/secure-context.js @@ -26,6 +26,7 @@ const { } = require('internal/util/types'); const { + validateArray, validateBuffer, validateInt32, validateObject, @@ -213,10 +214,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options') } if (certificateCompression != null) { - if (!ArrayIsArray(certificateCompression)) { - throw new ERR_INVALID_ARG_TYPE( - `${name}.certificateCompression`, 'Array', certificateCompression); - } + validateArray(certificateCompression, `${name}.certificateCompression`); if (certificateCompression.length > 0) { // Pack length + algorithm IDs into a single Uint32 for a cheap diff --git a/lib/tls.js b/lib/tls.js index 296f6189da17..d2ecc7f0a583 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -70,7 +70,7 @@ const { canonicalizeIP } = internalBinding('cares_wrap'); const tlsCommon = require('internal/tls/common'); const tlsWrap = require('internal/tls/wrap'); const { domainToASCII } = require('internal/url'); -const { validateString } = require('internal/validators'); +const { validateArray, validateString } = require('internal/validators'); const { namespace: { @@ -206,9 +206,7 @@ function getCACertificates(type = 'default') { exports.getCACertificates = getCACertificates; function setDefaultCACertificates(certs) { - if (!ArrayIsArray(certs)) { - throw new ERR_INVALID_ARG_TYPE('certs', 'Array', certs); - } + validateArray(certs, 'certs'); // Verify that all elements in the array are strings for (let i = 0; i < certs.length; i++) {