Skip to content

Commit

Permalink
lib: use validators
Browse files Browse the repository at this point in the history
Used more validators for the sake of consistency.
  • Loading branch information
VoltrexKeyva committed Aug 5, 2021
1 parent 16a9ab1 commit 969ab81
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
const {
hideStackFrames,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_NO_CRYPTO,
ERR_UNKNOWN_SIGNAL
},
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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
});
Expand Down

0 comments on commit 969ab81

Please sign in to comment.