From 969ab818aa9079593ba4d93252c377c05233e0a8 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Thu, 5 Aug 2021 18:12:21 +0430 Subject: [PATCH] lib: use validators Used more validators for the sake of consistency. --- lib/internal/util.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 9158fc8e52431e..0f661cd56a9be1 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -30,7 +30,6 @@ const { const { hideStackFrames, codes: { - ERR_INVALID_ARG_TYPE, ERR_NO_CRYPTO, ERR_UNKNOWN_SIGNAL }, @@ -75,6 +74,8 @@ function isError(e) { // each one once. const codesWarned = new SafeSet(); +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. @@ -83,8 +84,12 @@ function deprecate(fn, msg, code) { return fn; } - if (code !== undefined && typeof code !== 'string') - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); + // Load-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) { @@ -300,15 +305,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); + // Load-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 });