lib: refactor to use validateObject

Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/37028
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
authored and Antoine du Hamel committed 2021-01-28 12:53:50 +01:00
1 parent 8c9dc4e9e6
commit 029d1fd797
17 files changed
+73 -80

No files matched your search

+6 -8
View File
@@ -84,9 +84,10 @@ const {
getAllowUnauthorized,
} = require('internal/options');
const {
validateCallback,
validateString,
validateBuffer,
validateCallback,
validateObject,
validateString,
validateUint32
} = require('internal/validators');
const traceTls = getOptionValue('--trace-tls');
@@ -364,8 +365,7 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
if (ret == null)
return undefined;
if (typeof ret !== 'object')
throw new ERR_INVALID_ARG_TYPE('ret', 'Object', ret);
validateObject(ret, 'ret');
validateBuffer(ret.psk, 'psk');
if (ret.psk.length > maxPskLen) {
@@ -823,8 +823,7 @@ 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);
validateObject(options, 'options');
if (callback !== undefined) {
validateCallback(callback);
}
@@ -1237,8 +1236,7 @@ exports.createServer = function createServer(options, listener) {
Server.prototype.setSecureContext = function(options) {
if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
validateObject(options, 'options');
if (options.pfx)
this.pfx = options.pfx;
+4 -3
View File
@@ -72,9 +72,10 @@ const {
} = errorCodes;
const { clearTimeout, setTimeout } = require('timers');
const {
validateString,
isInt32,
validateAbortSignal,
validateObject,
validateString,
} = require('internal/validators');
const child_process = require('internal/child_process');
const {
@@ -452,8 +453,8 @@ function normalizeSpawnArguments(file, args, options) {
if (options === undefined)
options = {};
else if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
else
validateObject(options, 'options');
// Validate the cwd, if present.
if (options.cwd != null) {
+3 -3
View File
@@ -16,7 +16,6 @@ const {
ERR_INSPECTOR_NOT_CONNECTED,
ERR_INSPECTOR_NOT_ACTIVE,
ERR_INSPECTOR_NOT_WORKER,
ERR_INVALID_ARG_TYPE,
} = require('internal/errors').codes;
const { hasInspector } = internalBinding('config');
@@ -27,6 +26,7 @@ const EventEmitter = require('events');
const { queueMicrotask } = require('internal/process/task_queues');
const {
validateCallback,
validateObject,
validateString,
} = require('internal/validators');
const { isMainThread } = require('worker_threads');
@@ -99,8 +99,8 @@ class Session extends EventEmitter {
callback = params;
params = null;
}
if (params && typeof params !== 'object') {
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
if (params) {
validateObject(params, 'params');
}
if (callback) {
validateCallback(callback);
+4 -6
View File
@@ -18,12 +18,12 @@ const {
} = primordials;
const { inspect } = require('internal/util/inspect');
const { codes: {
ERR_INVALID_ARG_TYPE
} } = require('internal/errors');
const {
removeColors,
} = require('internal/util');
const {
validateObject,
} = require('internal/validators');
let blue = '';
let green = '';
@@ -315,9 +315,7 @@ function createErrDiff(actual, expected, operator) {
class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
const {
message,
operator,
+4 -6
View File
@@ -31,6 +31,7 @@ const {
} = require('internal/errors');
const {
validateArray,
validateObject,
validateOneOf,
validateString,
} = require('internal/validators');
@@ -345,9 +346,7 @@ function closePendingHandle(target) {
ChildProcess.prototype.spawn = function(options) {
let i = 0;
if (options === null || typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
// If no `stdio` option was given - use default
let stdio = options.stdio || 'pipe';
@@ -713,9 +712,8 @@ function setupChannel(target, channel, serializationMode) {
} else if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (options !== undefined &&
(options === null || typeof options !== 'object')) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
} else if (options !== undefined) {
validateObject(options, 'options');
}
options = { swallowErrors: false, ...options };
+2 -2
View File
@@ -48,6 +48,7 @@ const {
validateBoolean,
validateInt32,
validateInteger,
validateObject,
validateUint32,
} = require('internal/validators');
const pathModule = require('path');
@@ -762,8 +763,7 @@ const validateRmdirOptions = hideStackFrames(
(options, defaults = defaultRmdirOptions) => {
if (options === undefined)
return defaults;
if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
validateObject(options, 'options');
options = { ...defaults, ...options };
+5 -3
View File
@@ -35,7 +35,10 @@ const {
}
} = require('internal/errors');
const format = require('internal/util/inspect').format;
const { validateArray } = require('internal/validators');
const {
validateArray,
validateObject,
} = require('internal/validators');
const constants = internalBinding('constants').os.signals;
function assert(x, msg) {
@@ -109,8 +112,7 @@ function wrapProcessMethods(binding) {
// If a previous value was passed in, ensure it has the correct shape.
if (prevValue) {
if (!previousValueIsValid(prevValue.user)) {
if (typeof prevValue !== 'object')
throw new ERR_INVALID_ARG_TYPE('prevValue', 'object', prevValue);
validateObject(prevValue, 'prevValue');
if (typeof prevValue.user !== 'number') {
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
+6 -5
View File
@@ -4,9 +4,10 @@ const {
ERR_SYNTHETIC
} = require('internal/errors').codes;
const {
validateBoolean,
validateObject,
validateSignalName,
validateString,
validateBoolean,
} = require('internal/validators');
const nr = internalBinding('report');
const {
@@ -21,8 +22,8 @@ const report = {
throw new ERR_INVALID_ARG_TYPE('file', 'String', file);
} else if (err === undefined) {
err = new ERR_SYNTHETIC();
} else if (err === null || typeof err !== 'object') {
throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
} else {
validateObject(err, 'err');
}
return nr.writeReport('JavaScript API', 'API', file, err);
@@ -30,8 +31,8 @@ const report = {
getReport(err) {
if (err === undefined)
err = new ERR_SYNTHETIC();
else if (err === null || typeof err !== 'object')
throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
else
validateObject(err, 'err');
return JSONParse(nr.getReport(err));
},
+3 -3
View File
@@ -4,12 +4,12 @@
'use strict';
const {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_PREMATURE_CLOSE
} = require('internal/errors').codes;
const { once } = require('internal/util');
const {
validateFunction,
validateObject,
} = require('internal/validators');
function isSocket(stream) {
@@ -68,8 +68,8 @@ function eos(stream, options, callback) {
options = {};
} else if (options == null) {
options = {};
} else if (typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
} else {
validateObject(options, 'options');
}
validateFunction(callback, 'callback');
+7 -3
View File
@@ -64,7 +64,10 @@ const {
} = require('internal/constants');
const path = require('path');
const { validateCallback } = require('internal/validators');
const {
validateCallback,
validateObject,
} = require('internal/validators');
// Lazy loaded for startup performance.
let querystring;
@@ -413,8 +416,9 @@ ObjectDefineProperties(URL.prototype, {
configurable: false,
// eslint-disable-next-line func-name-matching
value: function format(options) {
if (options && typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
if (options)
validateObject(options, 'options');
options = {
fragment: true,
unicode: false,
+4 -3
View File
@@ -135,6 +135,9 @@ const {
const assert = require('internal/assert');
const { NativeModule } = require('internal/bootstrap/loaders');
const {
validateObject,
} = require('internal/validators');
let hexSlice;
@@ -338,9 +341,7 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
return inspectDefaultOptions;
},
set(options) {
if (options === null || typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
return ObjectAssign(inspectDefaultOptions, options);
}
});
+5 -13
View File
@@ -42,6 +42,7 @@ const {
validateBoolean,
validateFunction,
validateInt32,
validateObject,
validateUint32,
validateString,
} = require('internal/validators');
@@ -92,9 +93,7 @@ class Module {
} = options;
if (context !== undefined) {
if (typeof context !== 'object' || context === null) {
throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context);
}
validateObject(context, 'context');
if (!isContext(context)) {
throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context',
context);
@@ -204,9 +203,7 @@ class Module {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
let timeout = options.timeout;
if (timeout === undefined) {
@@ -261,10 +258,7 @@ class SourceTextModule extends Module {
constructor(sourceText, options = {}) {
validateString(sourceText, 'sourceText');
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
const {
lineOffset = 0,
@@ -408,9 +402,7 @@ class SyntheticModule extends Module {
}
validateFunction(evaluateCallback, 'evaluateCallback');
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
const { context, identifier } = options;
+5 -5
View File
@@ -28,7 +28,6 @@ const {
StringPrototypeSlice,
StringPrototypeToLowerCase,
} = primordials;
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
@@ -40,7 +39,10 @@ const {
CHAR_COLON,
CHAR_QUESTION_MARK,
} = require('internal/constants');
const { validateString } = require('internal/validators');
const {
validateObject,
validateString,
} = require('internal/validators');
function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
@@ -121,9 +123,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
}
function _format(sep, pathObject) {
if (pathObject === null || typeof pathObject !== 'object') {
throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject);
}
validateObject(pathObject, 'pathObject');
const dir = pathObject.dir || pathObject.root;
const base = pathObject.base ||
`${pathObject.name || ''}${pathObject.ext || ''}`;
+3 -6
View File
@@ -71,6 +71,7 @@ const {
const {
validateCallback,
validateFunction,
validateObject,
} = require('internal/validators');
const { setImmediate } = require('timers');
@@ -383,9 +384,7 @@ class PerformanceObserver {
}
observe(options) {
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
const { entryTypes } = options;
if (!ArrayIsArray(entryTypes)) {
throw new ERR_INVALID_ARG_VALUE('options.entryTypes', entryTypes);
@@ -637,9 +636,7 @@ class ELDHistogram extends Histogram {
}
function monitorEventLoopDelay(options = {}) {
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
const { resolution = 10 } = options;
if (typeof resolution !== 'number') {
throw new ERR_INVALID_ARG_TYPE('options.resolution',
+6 -7
View File
@@ -132,7 +132,6 @@ let debug = require('internal/util/debuglog').debuglog('repl', (fn) => {
const {
codes: {
ERR_CANNOT_WATCH_SIGINT,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_REPL_EVAL_CONFIG,
ERR_INVALID_REPL_INPUT,
ERR_SCRIPT_EXECUTION_INTERRUPTED,
@@ -141,6 +140,11 @@ const {
} = require('internal/errors');
const { sendInspectorCommand } = require('internal/util/inspector');
const { getOptionValue } = require('internal/options');
const {
validateFunction,
validateObject,
} = require('internal/validators');
const experimentalREPLAwait = getOptionValue(
'--experimental-repl-await'
);
@@ -166,9 +170,6 @@ const {
} = internalBinding('contextify');
const history = require('internal/repl/history');
const {
validateFunction,
} = require('internal/validators');
let nextREPLResourceNumber = 1;
// This prevents v8 code cache from getting confused and using a different
@@ -726,9 +727,7 @@ function REPLServer(prompt,
return writer.options;
},
set(options) {
if (options === null || typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
validateObject(options, 'options');
return ObjectAssign(writer.options, options);
},
enumerable: true,
+4 -2
View File
@@ -27,6 +27,9 @@ if (!hasTracing || !ownsProcessState)
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');
const {
validateObject,
} = require('internal/validators');
const enabledTracingObjects = new SafeSet();
@@ -80,8 +83,7 @@ class Tracing {
}
function createTracing(options) {
if (typeof options !== 'object' || options === null)
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
validateObject(options, 'options');
if (!ArrayIsArray(options.categories)) {
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
+2 -2
View File
@@ -66,8 +66,8 @@ class Script extends ContextifyScript {
code = `${code}`;
if (typeof options === 'string') {
options = { filename: options };
} else if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
} else {
validateObject(options, 'options');
}
const {