diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 8bc74c9cd43327..70ba3034876dae 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -51,12 +51,15 @@ const { AsyncResource } = require('async_hooks'); const { async_id_symbol } = require('internal/async_hooks').symbols; const { codes: { - ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE, }, } = require('internal/errors'); const { once } = require('internal/util'); -const { validateNumber, validateOneOf } = require('internal/validators'); +const { + validateNumber, + validateOneOf, + validateString +} = require('internal/validators'); const kOnKeylog = Symbol('onkeylog'); const kRequestOptions = Symbol('requestOptions'); @@ -344,10 +347,7 @@ function calculateServerName(options, req) { let servername = options.host; const hostHeader = req.getHeader('host'); if (hostHeader) { - if (typeof hostHeader !== 'string') { - throw new ERR_INVALID_ARG_TYPE('options.headers.host', - 'String', hostHeader); - } + validateString(hostHeader, 'options.headers.host'); // abc => abc // abc:123 => abc diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 66ebc7b77869f7..333b17870df6a5 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -90,6 +90,9 @@ const { validateObject, validateString, validateUint32, + validateBoolean, + validateNumber, + validateFunction } = require('internal/validators'); const { InternalX509Certificate @@ -468,10 +471,8 @@ function TLSSocket(socket, opts) { process.emitWarning('Enabling --trace-tls can expose sensitive data in ' + 'the resulting log.'); } - } else if (typeof enableTrace !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE( - 'options.enableTrace', 'boolean', enableTrace); - } + } else + validateBoolean(enableTrace, 'options.enableTrace'); if (tlsOptions.ALPNProtocols) tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions); @@ -783,11 +784,7 @@ TLSSocket.prototype._init = function(socket, wrap) { } if (options.pskCallback && ssl.enablePskCallback) { - if (typeof options.pskCallback !== 'function') { - throw new ERR_INVALID_ARG_TYPE('pskCallback', - 'function', - options.pskCallback); - } + validateFunction(options.pskCallback, 'pskCallback'); ssl[kOnPskExchange] = options.isServer ? onPskServerCallback : onPskClientCallback; @@ -796,13 +793,7 @@ TLSSocket.prototype._init = function(socket, wrap) { ssl.enablePskCallback(); if (options.pskIdentityHint) { - if (typeof options.pskIdentityHint !== 'string') { - throw new ERR_INVALID_ARG_TYPE( - 'options.pskIdentityHint', - 'string', - options.pskIdentityHint - ); - } + validateString(options.pskIdentityHint, 'options.pskIdentityHint'); ssl.setPskIdentityHint(options.pskIdentityHint); } } @@ -1215,10 +1206,7 @@ function Server(options, listener) { this[kPskCallback] = options.pskCallback; this[kPskIdentityHint] = options.pskIdentityHint; - if (typeof this[kHandshakeTimeout] !== 'number') { - throw new ERR_INVALID_ARG_TYPE( - 'options.handshakeTimeout', 'number', options.handshakeTimeout); - } + validateNumber(this[kHandshakeTimeout], 'options.handshakeTimeout'); if (this[kSNICallback] && typeof this[kSNICallback] !== 'function') { throw new ERR_INVALID_ARG_TYPE( diff --git a/lib/events.js b/lib/events.js index cc29b3358e0ce2..441b3ac4e4f984 100644 --- a/lib/events.js +++ b/lib/events.js @@ -63,6 +63,7 @@ const { const { validateAbortSignal, validateFunction, + validateBoolean } = require('internal/validators'); const kCapture = Symbol('kCapture'); @@ -89,10 +90,7 @@ ObjectDefineProperty(EventEmitter, 'captureRejections', { return EventEmitter.prototype[kCapture]; }, set(value) { - if (typeof value !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE('EventEmitter.captureRejections', - 'boolean', value); - } + validateBoolean(value, 'EventEmitter.captureRejections'); EventEmitter.prototype[kCapture] = value; }, @@ -190,10 +188,7 @@ EventEmitter.init = function(opts) { if (opts?.captureRejections) { - if (typeof opts.captureRejections !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE('options.captureRejections', - 'boolean', opts.captureRejections); - } + validateBoolean(opts.captureRejections, 'options.captureRejections'); this[kCapture] = Boolean(opts.captureRejections); } else { // Assigning the kCapture property directly saves an expensive diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 1108f5efb91392..a840816667821e 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -15,7 +15,8 @@ const { } = require('internal/perf/perf'); const { - validateObject + validateObject, + validateFunction } = require('internal/validators'); const { @@ -57,8 +58,7 @@ function processComplete(name, start, args, histogram) { } function timerify(fn, options = {}) { - if (typeof fn !== 'function') - throw new ERR_INVALID_ARG_TYPE('fn', 'function', fn); + validateFunction(fn, 'fn'); validateObject(options, 'options'); const { diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index e85029735a2c42..3f3c04f4a2a9aa 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -44,6 +44,7 @@ const format = require('internal/util/inspect').format; const { validateArray, validateObject, + validateNumber } = require('internal/validators'); const constants = internalBinding('constants').os.signals; @@ -122,19 +123,13 @@ function wrapProcessMethods(binding) { if (!previousValueIsValid(prevValue.user)) { validateObject(prevValue, 'prevValue'); - if (typeof prevValue.user !== 'number') { - throw new ERR_INVALID_ARG_TYPE('prevValue.user', - 'number', prevValue.user); - } + validateNumber(prevValue.user, 'prevValue.user'); throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user', prevValue.user); } if (!previousValueIsValid(prevValue.system)) { - if (typeof prevValue.system !== 'number') { - throw new ERR_INVALID_ARG_TYPE('prevValue.system', - 'number', prevValue.system); - } + validateNumber(prevValue.system, 'prevValue.system'); throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.system', prevValue.system); } diff --git a/lib/internal/util.js b/lib/internal/util.js index f77f1fc43368c4..d18516fc8f0879 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -36,6 +36,7 @@ const { uvErrmapGet, overrideStackTrace, } = require('internal/errors'); +const { validateFunction } = require('internal/validators'); const { signals } = internalBinding('constants').os; const { getHiddenValue, @@ -300,14 +301,11 @@ const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); function promisify(original) { - if (typeof original !== 'function') - throw new ERR_INVALID_ARG_TYPE('original', 'Function', original); + validateFunction(original, 'original'); if (original[kCustomPromisifiedSymbol]) { const fn = original[kCustomPromisifiedSymbol]; - if (typeof fn !== 'function') { - throw new ERR_INVALID_ARG_TYPE('util.promisify.custom', 'Function', fn); - } + validateFunction(fn, 'util.promisify.custom'); return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, { value: fn, enumerable: false, writable: false, configurable: true }); diff --git a/lib/util.js b/lib/util.js index bcb1c81933c0c0..53886efd949855 100644 --- a/lib/util.js +++ b/lib/util.js @@ -60,7 +60,10 @@ const { inspect } = require('internal/util/inspect'); const { debuglog } = require('internal/util/debuglog'); -const { validateNumber } = require('internal/validators'); +const { + validateNumber, + validateFunction +} = require('internal/validators'); const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; const types = require('internal/util/types'); @@ -285,18 +288,14 @@ const callbackifyOnRejected = hideStackFrames((reason, cb) => { * } */ function callbackify(original) { - if (typeof original !== 'function') { - throw new ERR_INVALID_ARG_TYPE('original', 'Function', original); - } + validateFunction(original, 'original'); // We DO NOT return the promise as it gives the user a false sense that // the promise is actually somehow related to the callback's execution // and that the callback throwing will reject the promise. function callbackified(...args) { const maybeCb = ArrayPrototypePop(args); - if (typeof maybeCb !== 'function') { - throw new ERR_INVALID_ARG_TYPE('last argument', 'Function', maybeCb); - } + validateFunction(maybeCb, 'last argument'); const cb = FunctionPrototypeBind(maybeCb, this); // In true node style we process the callback on `nextTick` with all the // implications (stack, `uncaughtException`, `async_hooks`) diff --git a/lib/vm.js b/lib/vm.js index c873842c434c30..cbf0a1b9c26f7d 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -54,6 +54,7 @@ const { validateBuffer, validateObject, validateOneOf, + validateFunction } = require('internal/validators'); const { kVmBreakFirstLineSymbol, @@ -108,11 +109,8 @@ class Script extends ContextifyScript { } if (importModuleDynamically !== undefined) { - if (typeof importModuleDynamically !== 'function') { - throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically', - 'function', - importModuleDynamically); - } + validateFunction(importModuleDynamically, + 'options.importModuleDynamically'); const { importModuleDynamicallyWrap } = require('internal/vm/module'); const { callbackMap } = internalBinding('module_wrap'); @@ -373,11 +371,8 @@ function compileFunction(code, params, options = {}) { } if (importModuleDynamically !== undefined) { - if (typeof importModuleDynamically !== 'function') { - throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically', - 'function', - importModuleDynamically); - } + validateFunction(importModuleDynamically, + 'options.importModuleDynamically'); const { importModuleDynamicallyWrap } = require('internal/vm/module'); const { callbackMap } = internalBinding('module_wrap'); diff --git a/lib/wasi.js b/lib/wasi.js index b3c14fc2f14840..0b04661ea0882f 100644 --- a/lib/wasi.js +++ b/lib/wasi.js @@ -20,6 +20,7 @@ const { validateBoolean, validateInt32, validateObject, + validateFunction } = require('internal/validators'); const { WASI: _WASI } = internalBinding('wasi'); const kExitCode = Symbol('kExitCode'); @@ -118,10 +119,7 @@ class WASI { const { _start, _initialize } = this[kInstance].exports; - if (typeof _start !== 'function') { - throw new ERR_INVALID_ARG_TYPE( - 'instance.exports._start', 'function', _start); - } + validateFunction(_start, 'instance.exports._start'); if (_initialize !== undefined) { throw new ERR_INVALID_ARG_TYPE( 'instance.exports._initialize', 'undefined', _initialize); diff --git a/lib/zlib.js b/lib/zlib.js index c0c7f601758224..10259d2eed2e64 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -71,6 +71,7 @@ const { const { owner_symbol } = require('internal/async_hooks').symbols; const { validateFunction, + validateNumber } = require('internal/validators'); const kFlushFlag = Symbol('kFlushFlag'); @@ -212,10 +213,7 @@ const checkFiniteNumber = hideStackFrames((number, name) => { return false; } - // Other non-numbers - if (typeof number !== 'number') { - throw new ERR_INVALID_ARG_TYPE(name, 'number', number); - } + validateNumber(number, name); // Infinite numbers throw new ERR_OUT_OF_RANGE(name, 'a finite number', number);