Skip to content

Commit dbaf498

Browse files
VoltrexKeyvatargos
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 9c33e4b commit dbaf498

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/internal/util.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const {
2626

2727
const {
2828
codes: {
29-
ERR_INVALID_ARG_TYPE,
3029
ERR_NO_CRYPTO,
3130
ERR_UNKNOWN_SIGNAL
3231
},
@@ -83,6 +82,8 @@ function isError(e) {
8382
// each one once.
8483
const codesWarned = new Set();
8584

85+
let validateString;
86+
8687
// Mark that a method should not be used.
8788
// Returns a modified function which warns once by default.
8889
// If --no-deprecation is set, then it is a no-op.
@@ -91,8 +92,12 @@ function deprecate(fn, msg, code) {
9192
return fn;
9293
}
9394

94-
if (code !== undefined && typeof code !== 'string')
95-
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
95+
// Lazy-load to avoid a circular dependency.
96+
if (validateString === undefined)
97+
({ validateString } = require('internal/validators'));
98+
99+
if (code !== undefined)
100+
validateString(code, 'code');
96101

97102
let warned = false;
98103
function deprecated(...args) {
@@ -307,15 +312,20 @@ function getSystemErrorMap() {
307312
const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
308313
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
309314

315+
let validateFunction;
316+
310317
function promisify(original) {
311-
if (typeof original !== 'function')
312-
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
318+
// Lazy-load to avoid a circular dependency.
319+
if (validateFunction === undefined)
320+
({ validateFunction } = require('internal/validators'));
321+
322+
validateFunction(original, 'original');
313323

314324
if (original[kCustomPromisifiedSymbol]) {
315325
const fn = original[kCustomPromisifiedSymbol];
316-
if (typeof fn !== 'function') {
317-
throw new ERR_INVALID_ARG_TYPE('util.promisify.custom', 'Function', fn);
318-
}
326+
327+
validateFunction(fn, 'util.promisify.custom');
328+
319329
return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
320330
value: fn, enumerable: false, writable: false, configurable: true
321331
});

lib/internal/validators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ const validateAbortSignal = hideStackFrames((signal, name) => {
233233
}
234234
});
235235

236+
const validateFunction = hideStackFrames((value, name) => {
237+
if (typeof value !== 'function')
238+
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
239+
});
240+
236241
module.exports = {
237242
isInt32,
238243
isUint32,
@@ -241,6 +246,7 @@ module.exports = {
241246
validateBoolean,
242247
validateBuffer,
243248
validateEncoding,
249+
validateFunction,
244250
validateInt32,
245251
validateInteger,
246252
validateNumber,

0 commit comments

Comments
 (0)