From d43b532789a6a31065eaea4d84964a3456e7a492 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Tue, 7 Feb 2023 00:44:04 +0900 Subject: [PATCH] lib: refactor to use `validateBuffer` Use validateBuffer to remove duplicate implementation. PR-URL: https://github.com/nodejs/node/pull/46489 Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Rafael Gonzaga Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Trivikram Kamat --- lib/internal/http2/core.js | 17 +++++++---------- lib/internal/tls/secure-context.js | 8 ++------ lib/vm.js | 12 +++--------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 5c2959d0f26..fd87910b876 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -120,14 +120,15 @@ const { } = require('internal/errors'); const { isUint32, + validateAbortSignal, + validateBoolean, + validateBuffer, validateFunction, validateInt32, validateInteger, validateNumber, validateString, validateUint32, - validateAbortSignal, - validateBoolean, } = require('internal/validators'); const fsPromisesInternal = require('internal/fs/promises'); const { utcDate } = require('internal/http'); @@ -1377,10 +1378,8 @@ class Http2Session extends EventEmitter { callback = payload; payload = undefined; } - if (payload && !isArrayBufferView(payload)) { - throw new ERR_INVALID_ARG_TYPE('payload', - ['Buffer', 'TypedArray', 'DataView'], - payload); + if (payload) { + validateBuffer(payload, 'payload'); } if (payload && payload.length !== 8) { throw new ERR_HTTP2_PING_LENGTH(); @@ -1506,10 +1505,8 @@ class Http2Session extends EventEmitter { if (this.destroyed) throw new ERR_HTTP2_INVALID_SESSION(); - if (opaqueData !== undefined && !isArrayBufferView(opaqueData)) { - throw new ERR_INVALID_ARG_TYPE('opaqueData', - ['Buffer', 'TypedArray', 'DataView'], - opaqueData); + if (opaqueData !== undefined) { + validateBuffer(opaqueData, 'opaqueData'); } validateNumber(code, 'code'); validateNumber(lastStreamID, 'lastStreamID'); diff --git a/lib/internal/tls/secure-context.js b/lib/internal/tls/secure-context.js index 3d2e4d6623e..b5b1048484d 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 { + validateBuffer, validateInt32, validateObject, validateString, @@ -292,12 +293,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options') } if (ticketKeys !== undefined && ticketKeys !== null) { - if (!isArrayBufferView(ticketKeys)) { - throw new ERR_INVALID_ARG_TYPE( - `${name}.ticketKeys`, - ['Buffer', 'TypedArray', 'DataView'], - ticketKeys); - } + validateBuffer(ticketKeys, `${name}.ticketKeys`); if (ticketKeys.byteLength !== 48) { throw new ERR_INVALID_ARG_VALUE( `${name}.ticketKeys`, diff --git a/lib/vm.js b/lib/vm.js index dea01215662..8e677ee0811 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -39,11 +39,9 @@ const { ERR_CONTEXT_NOT_INITIALIZED, ERR_INVALID_ARG_TYPE, } = require('internal/errors').codes; -const { - isArrayBufferView, -} = require('internal/util/types'); const { validateBoolean, + validateBuffer, validateFunction, validateInt32, validateObject, @@ -84,12 +82,8 @@ class Script extends ContextifyScript { validateString(filename, 'options.filename'); validateInt32(lineOffset, 'options.lineOffset'); validateInt32(columnOffset, 'options.columnOffset'); - if (cachedData !== undefined && !isArrayBufferView(cachedData)) { - throw new ERR_INVALID_ARG_TYPE( - 'options.cachedData', - ['Buffer', 'TypedArray', 'DataView'], - cachedData - ); + if (cachedData !== undefined) { + validateBuffer(cachedData, 'options.cachedData'); } validateBoolean(produceCachedData, 'options.produceCachedData');