From 03beacf80af347ad0d44e20ff96e93144e3eab53 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sat, 14 Jan 2023 18:52:26 +0900 Subject: [PATCH] lib: refactor to use validate function Throwing error after checking type is repeated. So replace it with validate function. PR-URL: https://github.com/nodejs/node/pull/46101 Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Minwoo Jung --- lib/_http_client.js | 7 +++---- lib/_tls_wrap.js | 19 +++++++------------ lib/async_hooks.js | 7 ++----- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 22d102b59754eb..74f7af7e2e0d9b 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -223,11 +223,10 @@ function ClientRequest(input, options, cb) { this.maxHeaderSize = maxHeaderSize; const insecureHTTPParser = options.insecureHTTPParser; - if (insecureHTTPParser !== undefined && - typeof insecureHTTPParser !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE( - 'options.insecureHTTPParser', 'boolean', insecureHTTPParser); + if (insecureHTTPParser !== undefined) { + validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser'); } + this.insecureHTTPParser = insecureHTTPParser; if (options.joinDuplicateHeaders !== undefined) { diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 65db04e76901b0..78fe3191dc86d5 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -1224,21 +1224,16 @@ function Server(options, listener) { validateNumber(this[kHandshakeTimeout], 'options.handshakeTimeout'); - if (this[kSNICallback] && typeof this[kSNICallback] !== 'function') { - throw new ERR_INVALID_ARG_TYPE( - 'options.SNICallback', 'function', options.SNICallback); + if (this[kSNICallback]) { + validateFunction(this[kSNICallback], 'options.SNICallback'); } - if (this[kPskCallback] && typeof this[kPskCallback] !== 'function') { - throw new ERR_INVALID_ARG_TYPE( - 'options.pskCallback', 'function', options.pskCallback); + if (this[kPskCallback]) { + validateFunction(this[kPskCallback], 'options.pskCallback'); } - if (this[kPskIdentityHint] && typeof this[kPskIdentityHint] !== 'string') { - throw new ERR_INVALID_ARG_TYPE( - 'options.pskIdentityHint', - 'string', - options.pskIdentityHint - ); + + if (this[kPskIdentityHint]) { + validateString(this[kPskIdentityHint], 'options.pskIdentityHint'); } // constructor call diff --git a/lib/async_hooks.js b/lib/async_hooks.js index c120a4828855e9..bf76874ffe2c1b 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -18,7 +18,6 @@ const { const { ERR_ASYNC_CALLBACK, ERR_ASYNC_TYPE, - ERR_INVALID_ARG_TYPE, ERR_INVALID_ASYNC_ID } = require('internal/errors').codes; const { kEmptyObject } = require('internal/util'); @@ -280,10 +279,8 @@ class AsyncLocalStorage { validateObject(options, 'options'); const { onPropagate = null } = options; - if (onPropagate !== null && typeof onPropagate !== 'function') { - throw new ERR_INVALID_ARG_TYPE('options.onPropagate', - 'function', - onPropagate); + if (onPropagate !== null) { + validateFunction(onPropagate, 'options.onPropagate'); } this.kResourceStore = Symbol('kResourceStore');