diff --git a/lib/internal/util.js b/lib/internal/util.js index 174385241ce3b5..f8cbac7ec7cbf9 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -26,7 +26,6 @@ const { const { codes: { - ERR_INVALID_ARG_TYPE, ERR_NO_CRYPTO, ERR_UNKNOWN_SIGNAL }, @@ -83,6 +82,8 @@ function isError(e) { // each one once. const codesWarned = new Set(); +let validateString; + // Mark that a method should not be used. // Returns a modified function which warns once by default. // If --no-deprecation is set, then it is a no-op. @@ -91,8 +92,12 @@ function deprecate(fn, msg, code) { return fn; } - if (code !== undefined && typeof code !== 'string') - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); + // Lazy-load to avoid a circular dependency. + if (validateString === undefined) + ({ validateString } = require('internal/validators')); + + if (code !== undefined) + validateString(code, 'code'); let warned = false; function deprecated(...args) { @@ -307,15 +312,20 @@ function getSystemErrorMap() { const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); +let validateFunction; + function promisify(original) { - if (typeof original !== 'function') - throw new ERR_INVALID_ARG_TYPE('original', 'Function', original); + // Lazy-load to avoid a circular dependency. + if (validateFunction === undefined) + ({ validateFunction } = require('internal/validators')); + + 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/internal/validators.js b/lib/internal/validators.js index ebd2b5222dcfe4..3878e17b843774 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -233,6 +233,11 @@ const validateAbortSignal = hideStackFrames((signal, name) => { } }); +const validateFunction = hideStackFrames((value, name) => { + if (typeof value !== 'function') + throw new ERR_INVALID_ARG_TYPE(name, 'Function', value); +}); + module.exports = { isInt32, isUint32, @@ -241,6 +246,7 @@ module.exports = { validateBoolean, validateBuffer, validateEncoding, + validateFunction, validateInt32, validateInteger, validateNumber,