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: refactor to use validateCallback #36609

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const { connResetException, codes } = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
ERR_MULTIPLE_CALLBACK,
ERR_SOCKET_CLOSED,
ERR_TLS_DH_PARAM_SIZE,
Expand All @@ -85,6 +84,7 @@ const {
getAllowUnauthorized,
} = require('internal/options');
const {
validateCallback,
validateString,
validateBuffer,
validateUint32
Expand Down Expand Up @@ -825,8 +825,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
TLSSocket.prototype.renegotiate = function(options, callback) {
if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
if (callback !== undefined) {
validateCallback(callback);
}

debug('%s renegotiate()',
this._tlsOptions.isServer ? 'server' : 'client',
Expand Down
35 changes: 18 additions & 17 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const {
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
ERR_MISSING_ARGS,
} = errors.codes;
const {
validateCallback,
validatePort,
validateString,
validateOneOf,
Expand Down Expand Up @@ -101,20 +101,24 @@ function lookup(hostname, options, callback) {
// Parse arguments
if (hostname && typeof hostname !== 'string') {
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
} else if (typeof options === 'function') {
}

if (typeof options === 'function') {
callback = options;
family = 0;
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
} else if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
all = options.all === true;
verbatim = options.verbatim === true;

validateHints(hints);
} else {
family = options >>> 0;
validateCallback(callback);

if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
all = options.all === true;
verbatim = options.verbatim === true;

validateHints(hints);
} else {
family = options >>> 0;
}
}

validateOneOf(family, 'family', [0, 4, 6]);
Expand Down Expand Up @@ -177,8 +181,7 @@ function lookupService(address, port, callback) {

validatePort(port);

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

port = +port;

Expand Down Expand Up @@ -217,9 +220,7 @@ function resolver(bindingName) {
}

validateString(name, 'name');
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
validateCallback(callback);

const req = new QueryReqWrap();
req.bindingName = bindingName;
Expand Down
19 changes: 7 additions & 12 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM
},
hideStackFrames,
Expand Down Expand Up @@ -125,6 +124,7 @@ const {
isUint32,
parseFileMode,
validateBuffer,
validateCallback,
validateInteger,
validateInt32
} = require('internal/validators');
Expand Down Expand Up @@ -170,19 +170,16 @@ function showTruncateDeprecation() {
}

function maybeCallback(cb) {
if (typeof cb === 'function')
return cb;
validateCallback(cb);

throw new ERR_INVALID_CALLBACK(cb);
return cb;
}

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
function makeCallback(cb) {
if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK(cb);
}
validateCallback(cb);

return (...args) => cb(...args);
}
Expand All @@ -191,9 +188,7 @@ function makeCallback(cb) {
// an optimization, since the data passed back to the callback needs to be
// transformed anyway.
function makeStatsCallback(cb) {
if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK(cb);
}
validateCallback(cb);

return (err, stats) => {
if (err) return cb(err);
Expand Down Expand Up @@ -2014,8 +2009,8 @@ function copyFile(src, dest, mode, callback) {
if (typeof mode === 'function') {
callback = mode;
mode = 0;
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
} else {
validateCallback(callback);
}

src = getValidatedPath(src, 'src');
Expand Down
10 changes: 6 additions & 4 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const {
ERR_INSPECTOR_NOT_ACTIVE,
ERR_INSPECTOR_NOT_WORKER,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK
} = require('internal/errors').codes;

const { hasInspector } = internalBinding('config');
Expand All @@ -26,7 +25,10 @@ if (!hasInspector)

const EventEmitter = require('events');
const { queueMicrotask } = require('internal/process/task_queues');
const { validateString } = require('internal/validators');
const {
validateCallback,
validateString,
} = require('internal/validators');
const { isMainThread } = require('worker_threads');

const {
Expand Down Expand Up @@ -100,8 +102,8 @@ class Session extends EventEmitter {
if (params && typeof params !== 'object') {
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
}
if (callback && typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
if (callback) {
validateCallback(callback);
}

if (!this[connectionSymbol]) {
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const {
ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
}
} = require('internal/errors');

const {
validateCallback,
validateInt32,
validateObject,
validateString,
Expand Down Expand Up @@ -325,8 +325,7 @@ function deriveBitsECDH(name, publicKey, privateKey, callback) {
validateString(name, 'name');
validateObject(publicKey, 'publicKey');
validateObject(privateKey, 'privateKey');
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);
const job = new ECDHBitsJob(kCryptoJobAsync, name, publicKey, privateKey);
job.ondone = (error, bits) => {
if (error) return FunctionPrototypeCall(callback, job, error);
Expand All @@ -340,8 +339,7 @@ function deriveBitsECDH(name, publicKey, privateKey, callback) {
function deriveBitsDH(publicKey, privateKey, callback) {
validateObject(publicKey, 'publicKey');
validateObject(privateKey, 'privateKey');
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);
const job = new DHBitsJob(kCryptoJobAsync, publicKey, privateKey);
job.ondone = (error, bits) => {
if (error) return FunctionPrototypeCall(callback, job, error);
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/hkdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
} = internalBinding('crypto');

const {
validateCallback,
validateInteger,
validateString,
validateUint32,
Expand Down Expand Up @@ -41,7 +42,6 @@ const {

const {
codes: {
ERR_INVALID_CALLBACK,
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_MISSING_OPTION,
Expand Down Expand Up @@ -112,8 +112,7 @@ function hkdf(hash, key, salt, info, length, callback) {
length,
} = validateParameters(hash, key, salt, info, length));

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

const job = new HKDFJob(kCryptoJobAsync, hash, key, salt, info, length);

Expand Down
8 changes: 3 additions & 5 deletions lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const { customPromisifyArgs } = require('internal/util');

const {
isUint32,
validateCallback,
validateString,
validateInteger,
validateObject,
Expand All @@ -50,7 +51,6 @@ const {
codes: {
ERR_INCOMPATIBLE_OPTION_PAIR,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
ERR_MISSING_OPTION,
}
} = require('internal/errors');
Expand All @@ -68,8 +68,7 @@ function generateKeyPair(type, options, callback) {
callback = options;
options = undefined;
}
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

const job = createJob(kCryptoJobAsync, type, options);

Expand Down Expand Up @@ -353,8 +352,7 @@ function generateKey(type, options, callback) {
options = undefined;
}

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

const job = generateKeyJob(kCryptoJobAsync, type, options);

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const {
} = internalBinding('crypto');

const {
validateCallback,
validateInteger,
validateUint32,
} = require('internal/validators');

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_MISSING_OPTION,
} = require('internal/errors').codes;

Expand All @@ -41,8 +41,7 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {
({ password, salt, iterations, keylen, digest } =
check(password, salt, iterations, keylen, digest));

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

const job = new PBKDF2Job(
kCryptoJobAsync,
Expand Down
19 changes: 11 additions & 8 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ const { kMaxLength } = require('buffer');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE,
}
} = require('internal/errors');

const { validateNumber } = require('internal/validators');
const {
validateNumber,
validateCallback,
} = require('internal/validators');

const {
isArrayBufferView,
Expand Down Expand Up @@ -73,8 +75,9 @@ function assertSize(size, elementSize, offset, length) {

function randomBytes(size, callback) {
size = assertSize(size, 1, 0, Infinity);
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
if (callback !== undefined) {
validateCallback(callback);
}

const buf = new FastBuffer(size);

Expand Down Expand Up @@ -141,8 +144,8 @@ function randomFill(buf, offset, size, callback) {
} else if (typeof size === 'function') {
callback = size;
size = buf.byteLength - offset;
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
} else {
validateCallback(callback);
}

offset = assertOffset(offset, elementSize, buf.byteLength);
Expand Down Expand Up @@ -188,8 +191,8 @@ function randomInt(min, max, callback) {
}

const isSync = typeof callback === 'undefined';
if (!isSync && typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
if (!isSync) {
validateCallback(callback);
}
if (!NumberIsSafeInteger(min)) {
throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
} = internalBinding('crypto');

const {
validateCallback,
validateInteger,
validateUint32,
} = require('internal/validators');
Expand All @@ -22,7 +23,6 @@ const {
codes: {
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
ERR_INVALID_CALLBACK,
}
} = require('internal/errors');

Expand Down Expand Up @@ -50,8 +50,7 @@ function scrypt(password, salt, keylen, options, callback = defaults) {
const { N, r, p, maxmem } = options;
({ password, salt, keylen } = options);

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
validateCallback(callback);

const job = new ScryptJob(
kCryptoJobAsync, password, salt, N, r, p, maxmem, keylen);
Expand Down
Loading