Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2268d1c

Browse filesBrowse files
pd4d10danielleadams
authored andcommitted
lib: refactor to reuse validators
PR-URL: #38608 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 929e8df commit 2268d1c
Copy full SHA for 2268d1c

File tree

Expand file treeCollapse file tree

9 files changed

+41
-72
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

9 files changed

+41
-72
lines changed
Open diff view settings
Collapse file

‎lib/_http_agent.js‎

Copy file name to clipboardExpand all lines: lib/_http_agent.js
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ const { AsyncResource } = require('async_hooks');
5151
const { async_id_symbol } = require('internal/async_hooks').symbols;
5252
const {
5353
codes: {
54-
ERR_INVALID_ARG_TYPE,
5554
ERR_OUT_OF_RANGE,
5655
},
5756
} = require('internal/errors');
5857
const { once } = require('internal/util');
59-
const { validateNumber, validateOneOf } = require('internal/validators');
58+
const {
59+
validateNumber,
60+
validateOneOf,
61+
validateString,
62+
} = require('internal/validators');
6063

6164
const kOnKeylog = Symbol('onkeylog');
6265
const kRequestOptions = Symbol('requestOptions');
@@ -344,10 +347,7 @@ function calculateServerName(options, req) {
344347
let servername = options.host;
345348
const hostHeader = req.getHeader('host');
346349
if (hostHeader) {
347-
if (typeof hostHeader !== 'string') {
348-
throw new ERR_INVALID_ARG_TYPE('options.headers.host',
349-
'String', hostHeader);
350-
}
350+
validateString(hostHeader, 'options.headers.host');
351351

352352
// abc => abc
353353
// abc:123 => abc
Collapse file

‎lib/_tls_wrap.js‎

Copy file name to clipboardExpand all lines: lib/_tls_wrap.js
+8-19Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ const {
8484
getAllowUnauthorized,
8585
} = require('internal/options');
8686
const {
87+
validateBoolean,
8788
validateBuffer,
8889
validateCallback,
90+
validateFunction,
8991
validateInt32,
92+
validateNumber,
9093
validateObject,
9194
validateString,
9295
validateUint32,
@@ -468,9 +471,8 @@ function TLSSocket(socket, opts) {
468471
process.emitWarning('Enabling --trace-tls can expose sensitive data in ' +
469472
'the resulting log.');
470473
}
471-
} else if (typeof enableTrace !== 'boolean') {
472-
throw new ERR_INVALID_ARG_TYPE(
473-
'options.enableTrace', 'boolean', enableTrace);
474+
} else {
475+
validateBoolean(enableTrace, 'options.enableTrace');
474476
}
475477

476478
if (tlsOptions.ALPNProtocols)
@@ -783,11 +785,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
783785
}
784786

785787
if (options.pskCallback && ssl.enablePskCallback) {
786-
if (typeof options.pskCallback !== 'function') {
787-
throw new ERR_INVALID_ARG_TYPE('pskCallback',
788-
'function',
789-
options.pskCallback);
790-
}
788+
validateFunction(options.pskCallback, 'pskCallback');
791789

792790
ssl[kOnPskExchange] = options.isServer ?
793791
onPskServerCallback : onPskClientCallback;
@@ -796,13 +794,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
796794
ssl.enablePskCallback();
797795

798796
if (options.pskIdentityHint) {
799-
if (typeof options.pskIdentityHint !== 'string') {
800-
throw new ERR_INVALID_ARG_TYPE(
801-
'options.pskIdentityHint',
802-
'string',
803-
options.pskIdentityHint
804-
);
805-
}
797+
validateString(options.pskIdentityHint, 'options.pskIdentityHint');
806798
ssl.setPskIdentityHint(options.pskIdentityHint);
807799
}
808800
}
@@ -1215,10 +1207,7 @@ function Server(options, listener) {
12151207
this[kPskCallback] = options.pskCallback;
12161208
this[kPskIdentityHint] = options.pskIdentityHint;
12171209

1218-
if (typeof this[kHandshakeTimeout] !== 'number') {
1219-
throw new ERR_INVALID_ARG_TYPE(
1220-
'options.handshakeTimeout', 'number', options.handshakeTimeout);
1221-
}
1210+
validateNumber(this[kHandshakeTimeout], 'options.handshakeTimeout');
12221211

12231212
if (this[kSNICallback] && typeof this[kSNICallback] !== 'function') {
12241213
throw new ERR_INVALID_ARG_TYPE(
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+3-8Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const {
6262

6363
const {
6464
validateAbortSignal,
65+
validateBoolean,
6566
validateFunction,
6667
} = require('internal/validators');
6768

@@ -89,10 +90,7 @@ ObjectDefineProperty(EventEmitter, 'captureRejections', {
8990
return EventEmitter.prototype[kCapture];
9091
},
9192
set(value) {
92-
if (typeof value !== 'boolean') {
93-
throw new ERR_INVALID_ARG_TYPE('EventEmitter.captureRejections',
94-
'boolean', value);
95-
}
93+
validateBoolean(value, 'EventEmitter.captureRejections');
9694

9795
EventEmitter.prototype[kCapture] = value;
9896
},
@@ -190,10 +188,7 @@ EventEmitter.init = function(opts) {
190188

191189

192190
if (opts?.captureRejections) {
193-
if (typeof opts.captureRejections !== 'boolean') {
194-
throw new ERR_INVALID_ARG_TYPE('options.captureRejections',
195-
'boolean', opts.captureRejections);
196-
}
191+
validateBoolean(opts.captureRejections, 'options.captureRejections');
197192
this[kCapture] = Boolean(opts.captureRejections);
198193
} else {
199194
// Assigning the kCapture property directly saves an expensive
Collapse file

‎lib/internal/perf/timerify.js‎

Copy file name to clipboardExpand all lines: lib/internal/perf/timerify.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const {
1515
} = require('internal/perf/perf');
1616

1717
const {
18-
validateObject
18+
validateFunction,
19+
validateObject,
1920
} = require('internal/validators');
2021

2122
const {
@@ -57,8 +58,7 @@ function processComplete(name, start, args, histogram) {
5758
}
5859

5960
function timerify(fn, options = {}) {
60-
if (typeof fn !== 'function')
61-
throw new ERR_INVALID_ARG_TYPE('fn', 'function', fn);
61+
validateFunction(fn, 'fn');
6262

6363
validateObject(options, 'options');
6464
const {
Collapse file

‎lib/internal/process/per_thread.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/per_thread.js
+3-8Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
const format = require('internal/util/inspect').format;
4444
const {
4545
validateArray,
46+
validateNumber,
4647
validateObject,
4748
} = require('internal/validators');
4849
const constants = internalBinding('constants').os.signals;
@@ -122,19 +123,13 @@ function wrapProcessMethods(binding) {
122123
if (!previousValueIsValid(prevValue.user)) {
123124
validateObject(prevValue, 'prevValue');
124125

125-
if (typeof prevValue.user !== 'number') {
126-
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
127-
'number', prevValue.user);
128-
}
126+
validateNumber(prevValue.user, 'prevValue.user');
129127
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user',
130128
prevValue.user);
131129
}
132130

133131
if (!previousValueIsValid(prevValue.system)) {
134-
if (typeof prevValue.system !== 'number') {
135-
throw new ERR_INVALID_ARG_TYPE('prevValue.system',
136-
'number', prevValue.system);
137-
}
132+
validateNumber(prevValue.system, 'prevValue.system');
138133
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.system',
139134
prevValue.system);
140135
}
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ const {
6060
inspect
6161
} = require('internal/util/inspect');
6262
const { debuglog } = require('internal/util/debuglog');
63-
const { validateNumber } = require('internal/validators');
63+
const {
64+
validateFunction,
65+
validateNumber,
66+
} = require('internal/validators');
6467
const { TextDecoder, TextEncoder } = require('internal/encoding');
6568
const { isBuffer } = require('buffer').Buffer;
6669
const types = require('internal/util/types');
@@ -285,18 +288,14 @@ const callbackifyOnRejected = hideStackFrames((reason, cb) => {
285288
* }
286289
*/
287290
function callbackify(original) {
288-
if (typeof original !== 'function') {
289-
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
290-
}
291+
validateFunction(original, 'original');
291292

292293
// We DO NOT return the promise as it gives the user a false sense that
293294
// the promise is actually somehow related to the callback's execution
294295
// and that the callback throwing will reject the promise.
295296
function callbackified(...args) {
296297
const maybeCb = ArrayPrototypePop(args);
297-
if (typeof maybeCb !== 'function') {
298-
throw new ERR_INVALID_ARG_TYPE('last argument', 'Function', maybeCb);
299-
}
298+
validateFunction(maybeCb, 'last argument');
300299
const cb = FunctionPrototypeBind(maybeCb, this);
301300
// In true node style we process the callback on `nextTick` with all the
302301
// implications (stack, `uncaughtException`, `async_hooks`)
Collapse file

‎lib/vm.js‎

Copy file name to clipboardExpand all lines: lib/vm.js
+8-13Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ const {
4646
isArrayBufferView,
4747
} = require('internal/util/types');
4848
const {
49-
validateInt32,
50-
validateUint32,
51-
validateString,
5249
validateArray,
5350
validateBoolean,
5451
validateBuffer,
52+
validateFunction,
53+
validateInt32,
5554
validateObject,
5655
validateOneOf,
56+
validateString,
57+
validateUint32,
5758
} = require('internal/validators');
5859
const {
5960
kVmBreakFirstLineSymbol,
@@ -108,11 +109,8 @@ class Script extends ContextifyScript {
108109
}
109110

110111
if (importModuleDynamically !== undefined) {
111-
if (typeof importModuleDynamically !== 'function') {
112-
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
113-
'function',
114-
importModuleDynamically);
115-
}
112+
validateFunction(importModuleDynamically,
113+
'options.importModuleDynamically');
116114
const { importModuleDynamicallyWrap } =
117115
require('internal/vm/module');
118116
const { callbackMap } = internalBinding('module_wrap');
@@ -373,11 +371,8 @@ function compileFunction(code, params, options = {}) {
373371
}
374372

375373
if (importModuleDynamically !== undefined) {
376-
if (typeof importModuleDynamically !== 'function') {
377-
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
378-
'function',
379-
importModuleDynamically);
380-
}
374+
validateFunction(importModuleDynamically,
375+
'options.importModuleDynamically');
381376
const { importModuleDynamicallyWrap } =
382377
require('internal/vm/module');
383378
const { callbackMap } = internalBinding('module_wrap');
Collapse file

‎lib/wasi.js‎

Copy file name to clipboardExpand all lines: lib/wasi.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { isArrayBuffer } = require('internal/util/types');
1818
const {
1919
validateArray,
2020
validateBoolean,
21+
validateFunction,
2122
validateInt32,
2223
validateObject,
2324
} = require('internal/validators');
@@ -118,10 +119,7 @@ class WASI {
118119

119120
const { _start, _initialize } = this[kInstance].exports;
120121

121-
if (typeof _start !== 'function') {
122-
throw new ERR_INVALID_ARG_TYPE(
123-
'instance.exports._start', 'function', _start);
124-
}
122+
validateFunction(_start, 'instance.exports._start');
125123
if (_initialize !== undefined) {
126124
throw new ERR_INVALID_ARG_TYPE(
127125
'instance.exports._initialize', 'undefined', _initialize);
Collapse file

‎lib/zlib.js‎

Copy file name to clipboardExpand all lines: lib/zlib.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171
const { owner_symbol } = require('internal/async_hooks').symbols;
7272
const {
7373
validateFunction,
74+
validateNumber,
7475
} = require('internal/validators');
7576

7677
const kFlushFlag = Symbol('kFlushFlag');
@@ -212,10 +213,7 @@ const checkFiniteNumber = hideStackFrames((number, name) => {
212213
return false;
213214
}
214215

215-
// Other non-numbers
216-
if (typeof number !== 'number') {
217-
throw new ERR_INVALID_ARG_TYPE(name, 'number', number);
218-
}
216+
validateNumber(number, name);
219217

220218
// Infinite numbers
221219
throw new ERR_OUT_OF_RANGE(name, 'a finite number', number);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.