diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index b5fbe4d36b2578..4e1c35b7916fbf 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -84,9 +84,10 @@ const { getAllowUnauthorized, } = require('internal/options'); const { + validateBuffer, validateCallback, + validateObject, validateString, - validateBuffer, 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; diff --git a/lib/child_process.js b/lib/child_process.js index f51072126e21cd..02a12010c5ca56 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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) { diff --git a/lib/inspector.js b/lib/inspector.js index 30b2e6407ce659..d7518eff261f3a 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -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); diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index e9e9a101970509..dd1b911a4ba3e5 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -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, diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 605ffac79ef7b5..f498e844f4f8a8 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -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 }; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 2353fd8c3cbcf2..ca93cba2962fcd 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -47,6 +47,7 @@ const { validateAbortSignal, validateBoolean, validateInt32, + validateObject, validateUint32 } = require('internal/validators'); const pathModule = require('path'); @@ -768,8 +769,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 }; diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index 4fdf0ba8764455..ec53859d97ddf1 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -34,7 +34,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) { @@ -108,8 +111,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', diff --git a/lib/internal/process/report.js b/lib/internal/process/report.js index dcb8f8ed0cd221..f34b71abe57d46 100644 --- a/lib/internal/process/report.js +++ b/lib/internal/process/report.js @@ -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)); }, diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 8eee330beced3a..b75961e29a794d 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -8,6 +8,9 @@ const { ERR_STREAM_PREMATURE_CLOSE } = require('internal/errors').codes; const { once } = require('internal/util'); +const { + validateObject, +} = require('internal/validators'); function isSocket(stream) { return ( @@ -65,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'); } if (typeof callback !== 'function') { throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback); diff --git a/lib/internal/url.js b/lib/internal/url.js index 3c90ec3d3672a8..e48811a9c3ca61 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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, diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c40083a1ad497b..41d95b468d14f3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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); } }); diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index 0d49aed62b05e1..0f7b1af4af8784 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -41,6 +41,7 @@ const { const { validateBoolean, validateInt32, + validateObject, validateUint32, validateString, } = require('internal/validators'); @@ -91,9 +92,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); @@ -205,9 +204,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) { @@ -262,10 +259,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, @@ -412,9 +406,7 @@ class SyntheticModule extends Module { evaluateCallback); } - if (typeof options !== 'object' || options === null) { - throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); - } + validateObject(options, 'options'); const { context, identifier } = options; diff --git a/lib/path.js b/lib/path.js index 246bace17873b6..f3d7f42fc1f433 100644 --- a/lib/path.js +++ b/lib/path.js @@ -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 || ''}`; diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index a5b0fdf1f60028..1cfc349242a117 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -68,7 +68,10 @@ const { kHandle, } = require('internal/histogram'); -const { validateCallback } = require('internal/validators'); +const { + validateCallback, + validateObject, +} = require('internal/validators'); const { setImmediate } = require('timers'); const kCallback = Symbol('callback'); @@ -380,9 +383,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); @@ -636,9 +637,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', diff --git a/lib/repl.js b/lib/repl.js index b1905195ab4b47..379d057c6424ad 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -141,6 +141,10 @@ const { } = require('internal/errors'); const { sendInspectorCommand } = require('internal/util/inspector'); const { getOptionValue } = require('internal/options'); +const { + validateObject, +} = require('internal/validators'); + const experimentalREPLAwait = getOptionValue( '--experimental-repl-await' ); @@ -722,9 +726,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, diff --git a/lib/trace_events.js b/lib/trace_events.js index 85101e7930d977..5211f8b0b1fc74 100644 --- a/lib/trace_events.js +++ b/lib/trace_events.js @@ -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[]', diff --git a/lib/vm.js b/lib/vm.js index 565b444dc697cd..56f4a7c3af1875 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -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 {