From 67b58f68c92f9ea8ad5742d8995d73629803a36a Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Thu, 21 Jan 2021 19:03:00 +0800 Subject: [PATCH] lib: refactor to use validateObject Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Co-authored-by: Antoine du Hamel PR-URL: https://github.com/nodejs/node/pull/37028 Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/_tls_wrap.js | 12 +++++------- lib/child_process.js | 7 ++++--- lib/inspector.js | 6 +++--- lib/internal/assert/assertion_error.js | 10 ++++------ lib/internal/child_process.js | 10 ++++------ lib/internal/fs/utils.js | 4 ++-- lib/internal/process/per_thread.js | 8 +++++--- lib/internal/process/report.js | 11 ++++++----- lib/internal/streams/end-of-stream.js | 6 +++--- lib/internal/url.js | 10 +++++++--- lib/internal/util/inspect.js | 7 ++++--- lib/internal/vm/module.js | 18 +++++------------- lib/path.js | 10 +++++----- lib/perf_hooks.js | 9 +++------ lib/repl.js | 13 ++++++------- lib/trace_events.js | 6 ++++-- lib/vm.js | 4 ++-- 17 files changed, 72 insertions(+), 79 deletions(-) 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 7bd9a058ea56de..8a57c40d76f823 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 6b50a60051634a..41b74ce652137a 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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 }; diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index c5ec2609bcfa8b..04da1a862ca7c0 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -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', 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 308ce16ea4903e..83e025c809b968 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -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 isRequest(stream) { @@ -60,8 +60,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'); 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 3c79aac6d8527a..56164b0d8b98d4 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -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; 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 0d8d85e0f9461d..4572ec88446af2 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -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', diff --git a/lib/repl.js b/lib/repl.js index eecfc964e87511..09c3a1fabb5204 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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, 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 {