Skip to content

Commit c68415c

Browse files
VoltrexKeyvadanielleadams
authored andcommitted
lib: use validators
Used more validators for the sake of consistency. PR-URL: #39663 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent be2d60d commit c68415c

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/internal/util.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const {
3030
const {
3131
hideStackFrames,
3232
codes: {
33-
ERR_INVALID_ARG_TYPE,
3433
ERR_NO_CRYPTO,
3534
ERR_UNKNOWN_SIGNAL
3635
},
@@ -75,6 +74,8 @@ function isError(e) {
7574
// each one once.
7675
const codesWarned = new SafeSet();
7776

77+
let validateString;
78+
7879
// Mark that a method should not be used.
7980
// Returns a modified function which warns once by default.
8081
// If --no-deprecation is set, then it is a no-op.
@@ -83,8 +84,12 @@ function deprecate(fn, msg, code) {
8384
return fn;
8485
}
8586

86-
if (code !== undefined && typeof code !== 'string')
87-
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
87+
// Lazy-load to avoid a circular dependency.
88+
if (validateString === undefined)
89+
({ validateString } = require('internal/validators'));
90+
91+
if (code !== undefined)
92+
validateString(code, 'code');
8893

8994
let warned = false;
9095
function deprecated(...args) {
@@ -300,15 +305,20 @@ function getSystemErrorMap() {
300305
const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
301306
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
302307

308+
let validateFunction;
309+
303310
function promisify(original) {
304-
if (typeof original !== 'function')
305-
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
311+
// Lazy-load to avoid a circular dependency.
312+
if (validateFunction === undefined)
313+
({ validateFunction } = require('internal/validators'));
314+
315+
validateFunction(original, 'original');
306316

307317
if (original[kCustomPromisifiedSymbol]) {
308318
const fn = original[kCustomPromisifiedSymbol];
309-
if (typeof fn !== 'function') {
310-
throw new ERR_INVALID_ARG_TYPE('util.promisify.custom', 'Function', fn);
311-
}
319+
320+
validateFunction(fn, 'util.promisify.custom');
321+
312322
return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
313323
value: fn, enumerable: false, writable: false, configurable: true
314324
});

0 commit comments

Comments
 (0)