Skip to content

Commit

Permalink
lib: refactor to reuse validators
Browse files Browse the repository at this point in the history
PR-URL: #38608
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
pd4d10 authored and danielleadams committed May 31, 2021
1 parent 929e8df commit 2268d1c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 72 deletions.
12 changes: 6 additions & 6 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ const { AsyncResource } = require('async_hooks');
const { async_id_symbol } = require('internal/async_hooks').symbols;
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
},
} = require('internal/errors');
const { once } = require('internal/util');
const { validateNumber, validateOneOf } = require('internal/validators');
const {
validateNumber,
validateOneOf,
validateString,
} = require('internal/validators');

const kOnKeylog = Symbol('onkeylog');
const kRequestOptions = Symbol('requestOptions');
Expand Down Expand Up @@ -344,10 +347,7 @@ function calculateServerName(options, req) {
let servername = options.host;
const hostHeader = req.getHeader('host');
if (hostHeader) {
if (typeof hostHeader !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.headers.host',
'String', hostHeader);
}
validateString(hostHeader, 'options.headers.host');

// abc => abc
// abc:123 => abc
Expand Down
27 changes: 8 additions & 19 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ const {
getAllowUnauthorized,
} = require('internal/options');
const {
validateBoolean,
validateBuffer,
validateCallback,
validateFunction,
validateInt32,
validateNumber,
validateObject,
validateString,
validateUint32,
Expand Down Expand Up @@ -468,9 +471,8 @@ function TLSSocket(socket, opts) {
process.emitWarning('Enabling --trace-tls can expose sensitive data in ' +
'the resulting log.');
}
} else if (typeof enableTrace !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(
'options.enableTrace', 'boolean', enableTrace);
} else {
validateBoolean(enableTrace, 'options.enableTrace');
}

if (tlsOptions.ALPNProtocols)
Expand Down Expand Up @@ -783,11 +785,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
}

if (options.pskCallback && ssl.enablePskCallback) {
if (typeof options.pskCallback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('pskCallback',
'function',
options.pskCallback);
}
validateFunction(options.pskCallback, 'pskCallback');

ssl[kOnPskExchange] = options.isServer ?
onPskServerCallback : onPskClientCallback;
Expand All @@ -796,13 +794,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
ssl.enablePskCallback();

if (options.pskIdentityHint) {
if (typeof options.pskIdentityHint !== 'string') {
throw new ERR_INVALID_ARG_TYPE(
'options.pskIdentityHint',
'string',
options.pskIdentityHint
);
}
validateString(options.pskIdentityHint, 'options.pskIdentityHint');
ssl.setPskIdentityHint(options.pskIdentityHint);
}
}
Expand Down Expand Up @@ -1215,10 +1207,7 @@ function Server(options, listener) {
this[kPskCallback] = options.pskCallback;
this[kPskIdentityHint] = options.pskIdentityHint;

if (typeof this[kHandshakeTimeout] !== 'number') {
throw new ERR_INVALID_ARG_TYPE(
'options.handshakeTimeout', 'number', options.handshakeTimeout);
}
validateNumber(this[kHandshakeTimeout], 'options.handshakeTimeout');

if (this[kSNICallback] && typeof this[kSNICallback] !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
Expand Down
11 changes: 3 additions & 8 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const {

const {
validateAbortSignal,
validateBoolean,
validateFunction,
} = require('internal/validators');

Expand Down Expand Up @@ -89,10 +90,7 @@ ObjectDefineProperty(EventEmitter, 'captureRejections', {
return EventEmitter.prototype[kCapture];
},
set(value) {
if (typeof value !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('EventEmitter.captureRejections',
'boolean', value);
}
validateBoolean(value, 'EventEmitter.captureRejections');

EventEmitter.prototype[kCapture] = value;
},
Expand Down Expand Up @@ -190,10 +188,7 @@ EventEmitter.init = function(opts) {


if (opts?.captureRejections) {
if (typeof opts.captureRejections !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('options.captureRejections',
'boolean', opts.captureRejections);
}
validateBoolean(opts.captureRejections, 'options.captureRejections');
this[kCapture] = Boolean(opts.captureRejections);
} else {
// Assigning the kCapture property directly saves an expensive
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/perf/timerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
} = require('internal/perf/perf');

const {
validateObject
validateFunction,
validateObject,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -57,8 +58,7 @@ function processComplete(name, start, args, histogram) {
}

function timerify(fn, options = {}) {
if (typeof fn !== 'function')
throw new ERR_INVALID_ARG_TYPE('fn', 'function', fn);
validateFunction(fn, 'fn');

validateObject(options, 'options');
const {
Expand Down
11 changes: 3 additions & 8 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
const format = require('internal/util/inspect').format;
const {
validateArray,
validateNumber,
validateObject,
} = require('internal/validators');
const constants = internalBinding('constants').os.signals;
Expand Down Expand Up @@ -122,19 +123,13 @@ function wrapProcessMethods(binding) {
if (!previousValueIsValid(prevValue.user)) {
validateObject(prevValue, 'prevValue');

if (typeof prevValue.user !== 'number') {
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
'number', prevValue.user);
}
validateNumber(prevValue.user, 'prevValue.user');
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user',
prevValue.user);
}

if (!previousValueIsValid(prevValue.system)) {
if (typeof prevValue.system !== 'number') {
throw new ERR_INVALID_ARG_TYPE('prevValue.system',
'number', prevValue.system);
}
validateNumber(prevValue.system, 'prevValue.system');
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.system',
prevValue.system);
}
Expand Down
13 changes: 6 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ const {
inspect
} = require('internal/util/inspect');
const { debuglog } = require('internal/util/debuglog');
const { validateNumber } = require('internal/validators');
const {
validateFunction,
validateNumber,
} = require('internal/validators');
const { TextDecoder, TextEncoder } = require('internal/encoding');
const { isBuffer } = require('buffer').Buffer;
const types = require('internal/util/types');
Expand Down Expand Up @@ -285,18 +288,14 @@ const callbackifyOnRejected = hideStackFrames((reason, cb) => {
* }
*/
function callbackify(original) {
if (typeof original !== 'function') {
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
}
validateFunction(original, 'original');

// We DO NOT return the promise as it gives the user a false sense that
// the promise is actually somehow related to the callback's execution
// and that the callback throwing will reject the promise.
function callbackified(...args) {
const maybeCb = ArrayPrototypePop(args);
if (typeof maybeCb !== 'function') {
throw new ERR_INVALID_ARG_TYPE('last argument', 'Function', maybeCb);
}
validateFunction(maybeCb, 'last argument');
const cb = FunctionPrototypeBind(maybeCb, this);
// In true node style we process the callback on `nextTick` with all the
// implications (stack, `uncaughtException`, `async_hooks`)
Expand Down
21 changes: 8 additions & 13 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ const {
isArrayBufferView,
} = require('internal/util/types');
const {
validateInt32,
validateUint32,
validateString,
validateArray,
validateBoolean,
validateBuffer,
validateFunction,
validateInt32,
validateObject,
validateOneOf,
validateString,
validateUint32,
} = require('internal/validators');
const {
kVmBreakFirstLineSymbol,
Expand Down Expand Up @@ -108,11 +109,8 @@ class Script extends ContextifyScript {
}

if (importModuleDynamically !== undefined) {
if (typeof importModuleDynamically !== 'function') {
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
'function',
importModuleDynamically);
}
validateFunction(importModuleDynamically,
'options.importModuleDynamically');
const { importModuleDynamicallyWrap } =
require('internal/vm/module');
const { callbackMap } = internalBinding('module_wrap');
Expand Down Expand Up @@ -373,11 +371,8 @@ function compileFunction(code, params, options = {}) {
}

if (importModuleDynamically !== undefined) {
if (typeof importModuleDynamically !== 'function') {
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
'function',
importModuleDynamically);
}
validateFunction(importModuleDynamically,
'options.importModuleDynamically');
const { importModuleDynamicallyWrap } =
require('internal/vm/module');
const { callbackMap } = internalBinding('module_wrap');
Expand Down
6 changes: 2 additions & 4 deletions lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { isArrayBuffer } = require('internal/util/types');
const {
validateArray,
validateBoolean,
validateFunction,
validateInt32,
validateObject,
} = require('internal/validators');
Expand Down Expand Up @@ -118,10 +119,7 @@ class WASI {

const { _start, _initialize } = this[kInstance].exports;

if (typeof _start !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
'instance.exports._start', 'function', _start);
}
validateFunction(_start, 'instance.exports._start');
if (_initialize !== undefined) {
throw new ERR_INVALID_ARG_TYPE(
'instance.exports._initialize', 'undefined', _initialize);
Expand Down
6 changes: 2 additions & 4 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const {
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
validateFunction,
validateNumber,
} = require('internal/validators');

const kFlushFlag = Symbol('kFlushFlag');
Expand Down Expand Up @@ -212,10 +213,7 @@ const checkFiniteNumber = hideStackFrames((number, name) => {
return false;
}

// Other non-numbers
if (typeof number !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', number);
}
validateNumber(number, name);

// Infinite numbers
throw new ERR_OUT_OF_RANGE(name, 'a finite number', number);
Expand Down

0 comments on commit 2268d1c

Please sign in to comment.