Skip to content

Commit 67b58f6

Browse files
Lxxyxtargos
authored andcommitted
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: #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>
1 parent dcd34b0 commit 67b58f6

17 files changed

+72
-79
lines changed

lib/_tls_wrap.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ const {
8484
getAllowUnauthorized,
8585
} = require('internal/options');
8686
const {
87+
validateBuffer,
8788
validateCallback,
89+
validateObject,
8890
validateString,
89-
validateBuffer,
9091
validateUint32
9192
} = require('internal/validators');
9293
const traceTls = getOptionValue('--trace-tls');
@@ -364,8 +365,7 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
364365
if (ret == null)
365366
return undefined;
366367

367-
if (typeof ret !== 'object')
368-
throw new ERR_INVALID_ARG_TYPE('ret', 'Object', ret);
368+
validateObject(ret, 'ret');
369369

370370
validateBuffer(ret.psk, 'psk');
371371
if (ret.psk.length > maxPskLen) {
@@ -823,8 +823,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
823823
};
824824

825825
TLSSocket.prototype.renegotiate = function(options, callback) {
826-
if (options === null || typeof options !== 'object')
827-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
826+
validateObject(options, 'options');
828827
if (callback !== undefined) {
829828
validateCallback(callback);
830829
}
@@ -1237,8 +1236,7 @@ exports.createServer = function createServer(options, listener) {
12371236

12381237

12391238
Server.prototype.setSecureContext = function(options) {
1240-
if (options === null || typeof options !== 'object')
1241-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
1239+
validateObject(options, 'options');
12421240

12431241
if (options.pfx)
12441242
this.pfx = options.pfx;

lib/child_process.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ const {
7272
} = errorCodes;
7373
const { clearTimeout, setTimeout } = require('timers');
7474
const {
75-
validateString,
7675
isInt32,
7776
validateAbortSignal,
77+
validateObject,
78+
validateString,
7879
} = require('internal/validators');
7980
const child_process = require('internal/child_process');
8081
const {
@@ -452,8 +453,8 @@ function normalizeSpawnArguments(file, args, options) {
452453

453454
if (options === undefined)
454455
options = {};
455-
else if (options === null || typeof options !== 'object')
456-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
456+
else
457+
validateObject(options, 'options');
457458

458459
// Validate the cwd, if present.
459460
if (options.cwd != null) {

lib/inspector.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const {
1616
ERR_INSPECTOR_NOT_CONNECTED,
1717
ERR_INSPECTOR_NOT_ACTIVE,
1818
ERR_INSPECTOR_NOT_WORKER,
19-
ERR_INVALID_ARG_TYPE,
2019
} = require('internal/errors').codes;
2120

2221
const { hasInspector } = internalBinding('config');
@@ -27,6 +26,7 @@ const EventEmitter = require('events');
2726
const { queueMicrotask } = require('internal/process/task_queues');
2827
const {
2928
validateCallback,
29+
validateObject,
3030
validateString,
3131
} = require('internal/validators');
3232
const { isMainThread } = require('worker_threads');
@@ -99,8 +99,8 @@ class Session extends EventEmitter {
9999
callback = params;
100100
params = null;
101101
}
102-
if (params && typeof params !== 'object') {
103-
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
102+
if (params) {
103+
validateObject(params, 'params');
104104
}
105105
if (callback) {
106106
validateCallback(callback);

lib/internal/assert/assertion_error.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ const {
1818
} = primordials;
1919

2020
const { inspect } = require('internal/util/inspect');
21-
const { codes: {
22-
ERR_INVALID_ARG_TYPE
23-
} } = require('internal/errors');
2421
const {
2522
removeColors,
2623
} = require('internal/util');
24+
const {
25+
validateObject,
26+
} = require('internal/validators');
2727

2828
let blue = '';
2929
let green = '';
@@ -315,9 +315,7 @@ function createErrDiff(actual, expected, operator) {
315315

316316
class AssertionError extends Error {
317317
constructor(options) {
318-
if (typeof options !== 'object' || options === null) {
319-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
320-
}
318+
validateObject(options, 'options');
321319
const {
322320
message,
323321
operator,

lib/internal/child_process.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
} = require('internal/errors');
3232
const {
3333
validateArray,
34+
validateObject,
3435
validateOneOf,
3536
validateString,
3637
} = require('internal/validators');
@@ -345,9 +346,7 @@ function closePendingHandle(target) {
345346
ChildProcess.prototype.spawn = function(options) {
346347
let i = 0;
347348

348-
if (options === null || typeof options !== 'object') {
349-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
350-
}
349+
validateObject(options, 'options');
351350

352351
// If no `stdio` option was given - use default
353352
let stdio = options.stdio || 'pipe';
@@ -713,9 +712,8 @@ function setupChannel(target, channel, serializationMode) {
713712
} else if (typeof options === 'function') {
714713
callback = options;
715714
options = undefined;
716-
} else if (options !== undefined &&
717-
(options === null || typeof options !== 'object')) {
718-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
715+
} else if (options !== undefined) {
716+
validateObject(options, 'options');
719717
}
720718

721719
options = { swallowErrors: false, ...options };

lib/internal/fs/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const {
4848
validateBoolean,
4949
validateInt32,
5050
validateInteger,
51+
validateObject,
5152
validateUint32,
5253
} = require('internal/validators');
5354
const pathModule = require('path');
@@ -762,8 +763,7 @@ const validateRmdirOptions = hideStackFrames(
762763
(options, defaults = defaultRmdirOptions) => {
763764
if (options === undefined)
764765
return defaults;
765-
if (options === null || typeof options !== 'object')
766-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
766+
validateObject(options, 'options');
767767

768768
options = { ...defaults, ...options };
769769

lib/internal/process/per_thread.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ const {
3535
}
3636
} = require('internal/errors');
3737
const format = require('internal/util/inspect').format;
38-
const { validateArray } = require('internal/validators');
38+
const {
39+
validateArray,
40+
validateObject,
41+
} = require('internal/validators');
3942
const constants = internalBinding('constants').os.signals;
4043

4144
function assert(x, msg) {
@@ -109,8 +112,7 @@ function wrapProcessMethods(binding) {
109112
// If a previous value was passed in, ensure it has the correct shape.
110113
if (prevValue) {
111114
if (!previousValueIsValid(prevValue.user)) {
112-
if (typeof prevValue !== 'object')
113-
throw new ERR_INVALID_ARG_TYPE('prevValue', 'object', prevValue);
115+
validateObject(prevValue, 'prevValue');
114116

115117
if (typeof prevValue.user !== 'number') {
116118
throw new ERR_INVALID_ARG_TYPE('prevValue.user',

lib/internal/process/report.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const {
44
ERR_SYNTHETIC
55
} = require('internal/errors').codes;
66
const {
7+
validateBoolean,
8+
validateObject,
79
validateSignalName,
810
validateString,
9-
validateBoolean,
1011
} = require('internal/validators');
1112
const nr = internalBinding('report');
1213
const {
@@ -21,17 +22,17 @@ const report = {
2122
throw new ERR_INVALID_ARG_TYPE('file', 'String', file);
2223
} else if (err === undefined) {
2324
err = new ERR_SYNTHETIC();
24-
} else if (err === null || typeof err !== 'object') {
25-
throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
25+
} else {
26+
validateObject(err, 'err');
2627
}
2728

2829
return nr.writeReport('JavaScript API', 'API', file, err);
2930
},
3031
getReport(err) {
3132
if (err === undefined)
3233
err = new ERR_SYNTHETIC();
33-
else if (err === null || typeof err !== 'object')
34-
throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
34+
else
35+
validateObject(err, 'err');
3536

3637
return JSONParse(nr.getReport(err));
3738
},

lib/internal/streams/end-of-stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
'use strict';
55

66
const {
7-
ERR_INVALID_ARG_TYPE,
87
ERR_STREAM_PREMATURE_CLOSE
98
} = require('internal/errors').codes;
109
const { once } = require('internal/util');
1110
const {
1211
validateFunction,
12+
validateObject,
1313
} = require('internal/validators');
1414

1515
function isRequest(stream) {
@@ -60,8 +60,8 @@ function eos(stream, options, callback) {
6060
options = {};
6161
} else if (options == null) {
6262
options = {};
63-
} else if (typeof options !== 'object') {
64-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
63+
} else {
64+
validateObject(options, 'options');
6565
}
6666
validateFunction(callback, 'callback');
6767

lib/internal/url.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ const {
6464
} = require('internal/constants');
6565
const path = require('path');
6666

67-
const { validateCallback } = require('internal/validators');
67+
const {
68+
validateCallback,
69+
validateObject,
70+
} = require('internal/validators');
6871

6972
// Lazy loaded for startup performance.
7073
let querystring;
@@ -413,8 +416,9 @@ ObjectDefineProperties(URL.prototype, {
413416
configurable: false,
414417
// eslint-disable-next-line func-name-matching
415418
value: function format(options) {
416-
if (options && typeof options !== 'object')
417-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
419+
if (options)
420+
validateObject(options, 'options');
421+
418422
options = {
419423
fragment: true,
420424
unicode: false,

lib/internal/util/inspect.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ const {
135135
const assert = require('internal/assert');
136136

137137
const { NativeModule } = require('internal/bootstrap/loaders');
138+
const {
139+
validateObject,
140+
} = require('internal/validators');
138141

139142
let hexSlice;
140143

@@ -338,9 +341,7 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
338341
return inspectDefaultOptions;
339342
},
340343
set(options) {
341-
if (options === null || typeof options !== 'object') {
342-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
343-
}
344+
validateObject(options, 'options');
344345
return ObjectAssign(inspectDefaultOptions, options);
345346
}
346347
});

lib/internal/vm/module.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
validateBoolean,
4343
validateFunction,
4444
validateInt32,
45+
validateObject,
4546
validateUint32,
4647
validateString,
4748
} = require('internal/validators');
@@ -92,9 +93,7 @@ class Module {
9293
} = options;
9394

9495
if (context !== undefined) {
95-
if (typeof context !== 'object' || context === null) {
96-
throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context);
97-
}
96+
validateObject(context, 'context');
9897
if (!isContext(context)) {
9998
throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context',
10099
context);
@@ -204,9 +203,7 @@ class Module {
204203
throw new ERR_VM_MODULE_NOT_MODULE();
205204
}
206205

207-
if (typeof options !== 'object' || options === null) {
208-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
209-
}
206+
validateObject(options, 'options');
210207

211208
let timeout = options.timeout;
212209
if (timeout === undefined) {
@@ -261,10 +258,7 @@ class SourceTextModule extends Module {
261258

262259
constructor(sourceText, options = {}) {
263260
validateString(sourceText, 'sourceText');
264-
265-
if (typeof options !== 'object' || options === null) {
266-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
267-
}
261+
validateObject(options, 'options');
268262

269263
const {
270264
lineOffset = 0,
@@ -408,9 +402,7 @@ class SyntheticModule extends Module {
408402
}
409403
validateFunction(evaluateCallback, 'evaluateCallback');
410404

411-
if (typeof options !== 'object' || options === null) {
412-
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
413-
}
405+
validateObject(options, 'options');
414406

415407
const { context, identifier } = options;
416408

lib/path.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const {
2828
StringPrototypeSlice,
2929
StringPrototypeToLowerCase,
3030
} = primordials;
31-
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
3231
const {
3332
CHAR_UPPERCASE_A,
3433
CHAR_LOWERCASE_A,
@@ -40,7 +39,10 @@ const {
4039
CHAR_COLON,
4140
CHAR_QUESTION_MARK,
4241
} = require('internal/constants');
43-
const { validateString } = require('internal/validators');
42+
const {
43+
validateObject,
44+
validateString,
45+
} = require('internal/validators');
4446

4547
function isPathSeparator(code) {
4648
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
@@ -121,9 +123,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
121123
}
122124

123125
function _format(sep, pathObject) {
124-
if (pathObject === null || typeof pathObject !== 'object') {
125-
throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject);
126-
}
126+
validateObject(pathObject, 'pathObject');
127127
const dir = pathObject.dir || pathObject.root;
128128
const base = pathObject.base ||
129129
`${pathObject.name || ''}${pathObject.ext || ''}`;

0 commit comments

Comments
 (0)