diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index d2eb7befeaea37..c8e412b55902eb 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -49,7 +49,7 @@ const { ERR_TLS_SESSION_ATTACK, ERR_TLS_SNI_FROM_SERVER } = require('internal/errors').codes; -const { validateString } = require('internal/validators'); +const { validateObject, validateString } = require('internal/validators'); const kConnectOptions = Symbol('connect-options'); const kDisableRenegotiation = Symbol('disable-renegotiation'); const kErrorEmitted = Symbol('error-emitted'); @@ -885,8 +885,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 5640b13ffea953..334ab0780f99a3 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -37,7 +37,11 @@ const { ERR_INVALID_OPT_VALUE, ERR_OUT_OF_RANGE } = require('internal/errors').codes; -const { validateString, isInt32 } = require('internal/validators'); +const { + validateString, + isInt32, + validateObject +} = require('internal/validators'); const child_process = require('internal/child_process'); const { _validateStdio, @@ -417,8 +421,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 aac864901c92bc..107d91d1d11652 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -6,10 +6,9 @@ const { ERR_INSPECTOR_CLOSED, ERR_INSPECTOR_NOT_AVAILABLE, ERR_INSPECTOR_NOT_CONNECTED, - ERR_INVALID_ARG_TYPE, ERR_INVALID_CALLBACK } = require('internal/errors').codes; -const { validateString } = require('internal/validators'); +const { validateObject, validateString } = require('internal/validators'); const util = require('util'); const { Connection, open, url } = internalBinding('inspector'); @@ -63,9 +62,7 @@ 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 && typeof callback !== 'function') { throw new ERR_INVALID_CALLBACK(); } diff --git a/lib/internal/assert.js b/lib/internal/assert.js index 29769fc5617b95..2ada6138b37596 100644 --- a/lib/internal/assert.js +++ b/lib/internal/assert.js @@ -1,9 +1,7 @@ 'use strict'; const { inspect } = require('util'); -const { codes: { - ERR_INVALID_ARG_TYPE -} } = require('internal/errors'); +const { validateObject } = require('internal/validators'); let blue = ''; let green = ''; @@ -289,9 +287,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'); var { actual, expected, diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 498dbf6a1cad75..71c2470c252ea2 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -14,7 +14,7 @@ const { ERR_MISSING_ARGS } } = require('internal/errors'); -const { validateString } = require('internal/validators'); +const { validateObject, validateString } = require('internal/validators'); const EventEmitter = require('events'); const net = require('net'); const dgram = require('dgram'); @@ -308,9 +308,7 @@ ChildProcess.prototype.spawn = function(options) { var ipcFd; var i; - 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 var stdio = options.stdio || 'pipe'; diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js index 7c0c4110439860..64b14d5ada5455 100644 --- a/lib/internal/crypto/keygen.js +++ b/lib/internal/crypto/keygen.js @@ -16,9 +16,13 @@ const { PrivateKeyObject } = require('internal/crypto/keys'); const { customPromisifyArgs } = require('internal/util'); -const { isUint32, validateString } = require('internal/validators'); const { - ERR_INVALID_ARG_TYPE, + isUint32, + validateObject, + validateString +} = require('internal/validators'); + +const { ERR_INVALID_ARG_VALUE, ERR_INVALID_CALLBACK, ERR_INVALID_OPT_VALUE @@ -115,8 +119,7 @@ function parseKeyEncoding(keyType, options) { function check(type, options, callback) { validateString(type, 'type'); - if (options == null || typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + validateObject(options, 'options'); // These will be set after parsing the type and type-specific options to make // the order a bit more intuitive. diff --git a/lib/internal/url.js b/lib/internal/url.js index 45ed236511ac9a..0d3da4e4a8dc7f 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -7,6 +7,7 @@ const { isHexTable } = require('internal/querystring'); +const { validateObject } = require('internal/validators'); const { getConstructorOf, removeColors } = require('internal/util'); const { ERR_ARG_NOT_ITERABLE, @@ -380,8 +381,7 @@ Object.defineProperties(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 256a4a8b06904b..f01698228fb596 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -20,12 +20,8 @@ const { removeColors } = require('internal/util'); -const { - codes: { - ERR_INVALID_ARG_TYPE - }, - isStackOverflowError -} = require('internal/errors'); +const { isStackOverflowError } = require('internal/errors'); +const { validateObject } = require('internal/validators'); const types = internalBinding('types'); Object.assign(types, require('internal/util/types')); @@ -200,9 +196,7 @@ Object.defineProperty(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 Object.assign(inspectDefaultOptions, options); } }); diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 0ecf286266678a..4202db163c7360 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -130,6 +130,12 @@ function validateNumber(value, name) { throw new ERR_INVALID_ARG_TYPE(name, 'number', value); } +function validateObject(value, name) { + if (typeof value !== 'object' || value === null) { + throw new ERR_INVALID_ARG_TYPE(name, 'object', value); + } +} + module.exports = { isInt32, isUint32, @@ -138,5 +144,6 @@ module.exports = { validateInt32, validateUint32, validateString, - validateNumber + validateNumber, + validateObject }; diff --git a/lib/internal/vm/source_text_module.js b/lib/internal/vm/source_text_module.js index 8b107a9b74866f..657c602e6b158c 100644 --- a/lib/internal/vm/source_text_module.js +++ b/lib/internal/vm/source_text_module.js @@ -21,6 +21,7 @@ const { SafePromise } = require('internal/safe_globals'); const { validateInt32, validateUint32, + validateObject, validateString } = require('internal/validators'); @@ -59,8 +60,7 @@ class SourceTextModule { emitExperimentalWarning('vm.SourceTextModule'); validateString(src, 'src'); - if (typeof options !== 'object' || options === null) - throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); + validateObject(options, 'options'); const { context, @@ -71,9 +71,7 @@ class SourceTextModule { } = options; if (context !== undefined) { - if (typeof context !== 'object' || context === null) { - throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context); - } + validateObject(context, 'options.context'); if (!isContext(context)) { throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context', context); @@ -218,9 +216,7 @@ class SourceTextModule { } async evaluate(options = {}) { - 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) { diff --git a/lib/path.js b/lib/path.js index 7ea431d1d00bb4..f3f57f0324f8ee 100644 --- a/lib/path.js +++ b/lib/path.js @@ -21,7 +21,6 @@ 'use strict'; -const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { CHAR_UPPERCASE_A, CHAR_LOWERCASE_A, @@ -33,7 +32,7 @@ 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; @@ -892,9 +891,7 @@ const win32 = { format: function format(pathObject) { - if (pathObject === null || typeof pathObject !== 'object') { - throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject); - } + validateObject(pathObject, 'pathObject'); return _format('\\', pathObject); }, @@ -1415,9 +1412,7 @@ const posix = { format: function format(pathObject) { - if (pathObject === null || typeof pathObject !== 'object') { - throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject); - } + validateObject(pathObject, 'pathObject'); return _format('/', pathObject); }, diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index cb1640ef9bd679..b45d9e464a40a0 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -33,6 +33,7 @@ const { const { AsyncResource } = require('async_hooks'); const L = require('internal/linkedlist'); const kInspect = require('internal/util').customInspectSymbol; +const { validateObject } = require('internal/validators'); const kCallback = Symbol('callback'); const kTypes = Symbol('types'); @@ -324,9 +325,7 @@ class PerformanceObserver extends AsyncResource { observe(options) { const errors = lazyErrors(); - if (typeof options !== 'object' || options == null) { - throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options); - } + validateObject(options, 'options'); if (!Array.isArray(options.entryTypes)) { throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options); } diff --git a/lib/trace_events.js b/lib/trace_events.js index d2977679e168b8..94c9bf4b5e0365 100644 --- a/lib/trace_events.js +++ b/lib/trace_events.js @@ -12,6 +12,7 @@ const { ERR_TRACE_EVENTS_UNAVAILABLE, ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; +const { validateObject } = require('internal/validators'); const { isMainThread } = require('internal/worker'); if (!hasTracing || !isMainThread) @@ -70,8 +71,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 (!Array.isArray(options.categories)) { throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]', diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index db00ed2836327f..7715d0ebadcd72 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -404,7 +404,7 @@ assert.throws(() => { { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]', - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + `Received type ${typeof input}` }); }); diff --git a/test/parallel/test-child-process-constructor.js b/test/parallel/test-child-process-constructor.js index bf7fffd9c5456f..b946843a7ac709 100644 --- a/test/parallel/test-child-process-constructor.js +++ b/test/parallel/test-child-process-constructor.js @@ -19,7 +19,7 @@ function typeName(value) { }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + `Received type ${typeName(options)}` }); }); diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index c50093301f1edf..15e9b782a77319 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -226,7 +226,7 @@ function checkFormat(path, testCases) { }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "pathObject" argument must be of type Object. ' + + message: 'The "pathObject" argument must be of type object. ' + `Received type ${typeof pathObject}` }); }); diff --git a/test/parallel/test-performanceobserver.js b/test/parallel/test-performanceobserver.js index 06c1281666bfd5..ed8a1bfb94a70f 100644 --- a/test/parallel/test-performanceobserver.js +++ b/test/parallel/test-performanceobserver.js @@ -45,7 +45,7 @@ assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION], 0); { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + `Received type ${typeof input}` }); }); diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index e5c3e369e80390..157370bee66a6e 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -27,7 +27,7 @@ assert.strictEqual( { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]', - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + `Received type ${typeof value}` } ); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 0d9cbdbe9d25fe..81e8a97b2e8511 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1186,7 +1186,7 @@ if (typeof Symbol !== 'undefined') { }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + 'Received type object' } ); @@ -1196,7 +1196,7 @@ if (typeof Symbol !== 'undefined') { }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + 'Received type string' } ); diff --git a/test/parallel/test-vm-module-errors.js b/test/parallel/test-vm-module-errors.js index a343a542a10abd..9e3afac05b45a1 100644 --- a/test/parallel/test-vm-module-errors.js +++ b/test/parallel/test-vm-module-errors.js @@ -137,7 +137,7 @@ async function checkModuleState() { await m.evaluate(false); }, { code: 'ERR_INVALID_ARG_TYPE', - message: 'The "options" argument must be of type Object. ' + + message: 'The "options" argument must be of type object. ' + 'Received type boolean' }); diff --git a/test/sequential/test-inspector-module.js b/test/sequential/test-inspector-module.js index 26bb7fe9262889..3c0f819ea8c1e8 100644 --- a/test/sequential/test-inspector-module.js +++ b/test/sequential/test-inspector-module.js @@ -40,7 +40,7 @@ session.post('Runtime.evaluate', { expression: '2 + 2' }); code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: - 'The "params" argument must be of type Object. ' + + 'The "params" argument must be of type object. ' + `Received type ${typeof i}` } );