Skip to content

Commit

Permalink
lib: refactor to use validateObject
Browse files Browse the repository at this point in the history
Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

PR-URL: #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>
  • Loading branch information
Lxxyx authored and targos committed Feb 2, 2021
1 parent dcd34b0 commit 67b58f6
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 79 deletions.
12 changes: 5 additions & 7 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ const {
getAllowUnauthorized,
} = require('internal/options');
const {
validateBuffer,
validateCallback,
validateObject,
validateString,
validateBuffer,
validateUint32
} = require('internal/validators');
const traceTls = getOptionValue('--trace-tls');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
} = require('internal/errors');
const {
validateArray,
validateObject,
validateOneOf,
validateString,
} = require('internal/validators');
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {
validateBoolean,
validateInt32,
validateInteger,
validateObject,
validateUint32,
} = require('internal/validators');
const pathModule = require('path');
Expand Down Expand Up @@ -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 };

Expand Down
8 changes: 5 additions & 3 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 6 additions & 5 deletions lib/internal/process/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,17 +22,17 @@ 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);
},
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));
},
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');

Expand Down
10 changes: 7 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ const {
const assert = require('internal/assert');

const { NativeModule } = require('internal/bootstrap/loaders');
const {
validateObject,
} = require('internal/validators');

let hexSlice;

Expand Down Expand Up @@ -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);
}
});
Expand Down
18 changes: 5 additions & 13 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
validateBoolean,
validateFunction,
validateInt32,
validateObject,
validateUint32,
validateString,
} = require('internal/validators');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
StringPrototypeSlice,
StringPrototypeToLowerCase,
} = primordials;
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
Expand All @@ -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;
Expand Down Expand Up @@ -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 || ''}`;
Expand Down
Loading

0 comments on commit 67b58f6

Please sign in to comment.