Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: use validators #39663

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
// 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) {
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);
// 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
});
Expand Down