Skip to content

Commit 5227c5e

Browse files
Lxxyxtargos
authored andcommitted
lib: refactor to use validateFunction
add validateFunction and refactor to use validateFunction PR-URL: #37045 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent da07eb6 commit 5227c5e

15 files changed

+79
-79
lines changed

lib/assert.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const { isError } = require('internal/util');
6868

6969
const errorCache = new SafeMap();
7070
const CallTracker = require('internal/assert/calltracker');
71+
const {
72+
validateFunction,
73+
} = require('internal/validators');
7174

7275
let isDeepEqual;
7376
let isDeepStrictEqual;
@@ -693,9 +696,7 @@ function expectedException(actual, expected, message, fn) {
693696
}
694697

695698
function getActual(fn) {
696-
if (typeof fn !== 'function') {
697-
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
698-
}
699+
validateFunction(fn, 'fn');
699700
try {
700701
fn();
701702
} catch (e) {

lib/async_hooks.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const {
1616
const {
1717
ERR_ASYNC_CALLBACK,
1818
ERR_ASYNC_TYPE,
19-
ERR_INVALID_ARG_TYPE,
2019
ERR_INVALID_ASYNC_ID
2120
} = require('internal/errors').codes;
22-
const { validateString } = require('internal/validators');
21+
const {
22+
validateFunction,
23+
validateString,
24+
} = require('internal/validators');
2325
const internal_async_hooks = require('internal/async_hooks');
2426

2527
// Get functions
@@ -221,8 +223,7 @@ class AsyncResource {
221223
}
222224

223225
bind(fn) {
224-
if (typeof fn !== 'function')
225-
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
226+
validateFunction(fn, 'fn');
226227
const ret = FunctionPrototypeBind(this.runInAsyncScope, this, fn);
227228
ObjectDefineProperties(ret, {
228229
'length': {

lib/diagnostics_channel.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const {
1515
ERR_INVALID_ARG_TYPE,
1616
}
1717
} = require('internal/errors');
18+
const {
19+
validateFunction,
20+
} = require('internal/validators');
1821

1922
const { triggerUncaughtException } = internalBinding('errors');
2023

@@ -23,10 +26,7 @@ const { WeakReference } = internalBinding('util');
2326
// TODO(qard): should there be a C++ channel interface?
2427
class ActiveChannel {
2528
subscribe(subscription) {
26-
if (typeof subscription !== 'function') {
27-
throw new ERR_INVALID_ARG_TYPE('subscription', ['function'],
28-
subscription);
29-
}
29+
validateFunction(subscription, 'subscription');
3030
ArrayPrototypePush(this._subscribers, subscription);
3131
}
3232

lib/events.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const {
6666
} = require('internal/util/inspect');
6767

6868
const {
69-
validateAbortSignal
69+
validateAbortSignal,
70+
validateFunction,
7071
} = require('internal/validators');
7172

7273
const kCapture = Symbol('kCapture');
@@ -130,9 +131,7 @@ let defaultMaxListeners = 10;
130131
let isEventTarget;
131132

132133
function checkListener(listener) {
133-
if (typeof listener !== 'function') {
134-
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
135-
}
134+
validateFunction(listener, 'listener');
136135
}
137136

138137
ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {

lib/fs.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ const {
126126
validateBoolean,
127127
validateBuffer,
128128
validateCallback,
129+
validateFunction,
129130
validateInteger,
130-
validateInt32
131+
validateInt32,
131132
} = require('internal/validators');
132133
// 2 ** 32 - 1
133134
const kMaxUserId = 4294967295;
@@ -1628,9 +1629,7 @@ function watchFile(filename, options, listener) {
16281629
...options
16291630
};
16301631

1631-
if (typeof listener !== 'function') {
1632-
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
1633-
}
1632+
validateFunction(listener, 'listener');
16341633

16351634
stat = statWatchers.get(filename);
16361635

lib/internal/dgram.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ const {
88
const { codes } = require('internal/errors');
99
const { UDP } = internalBinding('udp_wrap');
1010
const { guessHandleType } = internalBinding('util');
11-
const { isInt32 } = require('internal/validators');
11+
const {
12+
isInt32,
13+
validateFunction,
14+
} = require('internal/validators');
1215
const { UV_EINVAL } = internalBinding('uv');
13-
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
16+
const {
17+
ERR_SOCKET_BAD_TYPE,
18+
} = codes;
1419
const kStateSymbol = Symbol('state symbol');
1520
let dns; // Lazy load for startup performance.
1621

@@ -31,8 +36,8 @@ function newHandle(type, lookup) {
3136
}
3237

3338
lookup = dns.lookup;
34-
} else if (typeof lookup !== 'function') {
35-
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
39+
} else {
40+
validateFunction(lookup, 'lookup');
3641
}
3742

3843
if (type === 'udp4') {

lib/internal/fs/streams.js

+14-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ const {
1717
ERR_METHOD_NOT_IMPLEMENTED,
1818
} = require('internal/errors').codes;
1919
const { deprecate } = require('internal/util');
20-
const { validateInteger } = require('internal/validators');
20+
const {
21+
validateFunction,
22+
validateInteger,
23+
} = require('internal/validators');
2124
const { errorOrDestroy } = require('internal/streams/destroy');
2225
const fs = require('fs');
2326
const { kRef, kUnref, FileHandle } = require('internal/fs/promises');
@@ -155,20 +158,9 @@ function ReadStream(path, options) {
155158

156159
this[kFs] = options.fs || fs;
157160

158-
if (typeof this[kFs].open !== 'function') {
159-
throw new ERR_INVALID_ARG_TYPE('options.fs.open', 'function',
160-
this[kFs].open);
161-
}
162-
163-
if (typeof this[kFs].read !== 'function') {
164-
throw new ERR_INVALID_ARG_TYPE('options.fs.read', 'function',
165-
this[kFs].read);
166-
}
167-
168-
if (typeof this[kFs].close !== 'function') {
169-
throw new ERR_INVALID_ARG_TYPE('options.fs.close', 'function',
170-
this[kFs].close);
171-
}
161+
validateFunction(this[kFs].open, 'options.fs.open');
162+
validateFunction(this[kFs].read, 'options.fs.read');
163+
validateFunction(this[kFs].close, 'options.fs.close');
172164

173165
options.autoDestroy = options.autoClose === undefined ?
174166
true : options.autoClose;
@@ -310,30 +302,23 @@ function WriteStream(path, options) {
310302
options.decodeStrings = true;
311303

312304
this[kFs] = options.fs || fs;
313-
if (typeof this[kFs].open !== 'function') {
314-
throw new ERR_INVALID_ARG_TYPE('options.fs.open', 'function',
315-
this[kFs].open);
316-
}
305+
306+
validateFunction(this[kFs].open, 'options.fs.open');
317307

318308
if (!this[kFs].write && !this[kFs].writev) {
319309
throw new ERR_INVALID_ARG_TYPE('options.fs.write', 'function',
320310
this[kFs].write);
321311
}
322312

323-
if (this[kFs].write && typeof this[kFs].write !== 'function') {
324-
throw new ERR_INVALID_ARG_TYPE('options.fs.write', 'function',
325-
this[kFs].write);
313+
if (this[kFs].write) {
314+
validateFunction(this[kFs].write, 'options.fs.write');
326315
}
327316

328-
if (this[kFs].writev && typeof this[kFs].writev !== 'function') {
329-
throw new ERR_INVALID_ARG_TYPE('options.fs.writev', 'function',
330-
this[kFs].writev);
317+
if (this[kFs].writev) {
318+
validateFunction(this[kFs].writev, 'options.fs.writev');
331319
}
332320

333-
if (typeof this[kFs].close !== 'function') {
334-
throw new ERR_INVALID_ARG_TYPE('options.fs.close', 'function',
335-
this[kFs].close);
336-
}
321+
validateFunction(this[kFs].close, 'options.fs.close');
337322

338323
// It's enough to override either, in which case only one will be used.
339324
if (!this[kFs].write) {

lib/internal/process/task_queues.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ const {
3333
emitDestroy,
3434
symbols: { async_id_symbol, trigger_async_id_symbol }
3535
} = require('internal/async_hooks');
36-
const {
37-
ERR_INVALID_ARG_TYPE
38-
} = require('internal/errors').codes;
3936
const FixedQueue = require('internal/fixed_queue');
4037

41-
const { validateCallback } = require('internal/validators');
38+
const {
39+
validateCallback,
40+
validateFunction,
41+
} = require('internal/validators');
4242

4343
// *Must* match Environment::TickInfo::Fields in src/env.h.
4444
const kHasTickScheduled = 0;
@@ -154,9 +154,7 @@ function runMicrotask() {
154154
}
155155

156156
function queueMicrotask(callback) {
157-
if (typeof callback !== 'function') {
158-
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
159-
}
157+
validateFunction(callback, 'callback');
160158

161159
const asyncResource = createMicrotaskResource();
162160
asyncResource.callback = callback;

lib/internal/quic/util.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const {
116116
const {
117117
validateBoolean,
118118
validateBuffer,
119+
validateFunction,
119120
validateInteger,
120121
validateObject,
121122
validatePort,
@@ -175,8 +176,9 @@ function validateCloseCode(code) {
175176
}
176177

177178
function validateLookup(lookup) {
178-
if (lookup && typeof lookup !== 'function')
179-
throw new ERR_INVALID_ARG_TYPE('options.lookup', 'function', lookup);
179+
if (lookup) {
180+
validateFunction(lookup, 'options.lookup');
181+
}
180182
}
181183

182184
function validatePreferredAddress(address) {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const {
88
ERR_STREAM_PREMATURE_CLOSE
99
} = require('internal/errors').codes;
1010
const { once } = require('internal/util');
11+
const {
12+
validateFunction,
13+
} = require('internal/validators');
1114

1215
function isRequest(stream) {
1316
return stream.setHeader && typeof stream.abort === 'function';
@@ -60,9 +63,7 @@ function eos(stream, options, callback) {
6063
} else if (typeof options !== 'object') {
6164
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
6265
}
63-
if (typeof callback !== 'function') {
64-
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
65-
}
66+
validateFunction(callback, 'callback');
6667

6768
callback = once(callback);
6869

lib/internal/validators.js

+6
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ const validateAbortSignal = hideStackFrames((signal, name) => {
228228
}
229229
});
230230

231+
const validateFunction = hideStackFrames((value, name) => {
232+
if (typeof value !== 'function')
233+
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
234+
});
235+
231236
module.exports = {
232237
isInt32,
233238
isUint32,
@@ -236,6 +241,7 @@ module.exports = {
236241
validateBoolean,
237242
validateBuffer,
238243
validateEncoding,
244+
validateFunction,
239245
validateInt32,
240246
validateInteger,
241247
validateNumber,

lib/internal/vm/module.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
} = require('internal/errors').codes;
4141
const {
4242
validateBoolean,
43+
validateFunction,
4344
validateInt32,
4445
validateUint32,
4546
validateString,
@@ -187,9 +188,7 @@ class Module {
187188
if (this[kWrap] === undefined) {
188189
throw new ERR_VM_MODULE_NOT_MODULE();
189190
}
190-
if (typeof linker !== 'function') {
191-
throw new ERR_INVALID_ARG_TYPE('linker', 'function', linker);
192-
}
191+
validateFunction(linker, 'linker');
193192
if (this.status === 'linked') {
194193
throw new ERR_VM_MODULE_ALREADY_LINKED();
195194
}
@@ -407,10 +406,7 @@ class SyntheticModule extends Module {
407406
}
408407
});
409408
}
410-
if (typeof evaluateCallback !== 'function') {
411-
throw new ERR_INVALID_ARG_TYPE('evaluateCallback', 'function',
412-
evaluateCallback);
413-
}
409+
validateFunction(evaluateCallback, 'evaluateCallback');
414410

415411
if (typeof options !== 'object' || options === null) {
416412
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);

lib/perf_hooks.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ const {
6868
kHandle,
6969
} = require('internal/histogram');
7070

71-
const { validateCallback } = require('internal/validators');
71+
const {
72+
validateCallback,
73+
validateFunction,
74+
} = require('internal/validators');
7275

7376
const { setImmediate } = require('timers');
7477
const kCallback = Symbol('callback');
@@ -463,9 +466,7 @@ class Performance {
463466
}
464467

465468
timerify(fn) {
466-
if (typeof fn !== 'function') {
467-
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
468-
}
469+
validateFunction(fn, 'fn');
469470
if (fn[kTimerified])
470471
return fn[kTimerified];
471472
const ret = timerify(fn, fn.length);

lib/repl.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ const {
166166
} = internalBinding('contextify');
167167

168168
const history = require('internal/repl/history');
169+
const {
170+
validateFunction,
171+
} = require('internal/validators');
172+
169173
let nextREPLResourceNumber = 1;
170174
// This prevents v8 code cache from getting confused and using a different
171175
// cache from a resource of the same name
@@ -1432,8 +1436,8 @@ REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
14321436
REPLServer.prototype.defineCommand = function(keyword, cmd) {
14331437
if (typeof cmd === 'function') {
14341438
cmd = { action: cmd };
1435-
} else if (typeof cmd.action !== 'function') {
1436-
throw new ERR_INVALID_ARG_TYPE('cmd.action', 'Function', cmd.action);
1439+
} else {
1440+
validateFunction(cmd.action, 'cmd.action');
14371441
}
14381442
this.commands[keyword] = cmd;
14391443
};

lib/zlib.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ const {
6969
kMaxLength
7070
} = require('buffer');
7171
const { owner_symbol } = require('internal/async_hooks').symbols;
72+
const {
73+
validateFunction,
74+
} = require('internal/validators');
7275

7376
const kFlushFlag = Symbol('kFlushFlag');
7477
const kError = Symbol('kError');
@@ -107,8 +110,7 @@ for (const ckey of ObjectKeys(codes)) {
107110
}
108111

109112
function zlibBuffer(engine, buffer, callback) {
110-
if (typeof callback !== 'function')
111-
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
113+
validateFunction(callback, 'callback');
112114
// Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a
113115
// Buffer without copying.
114116
if (isArrayBufferView(buffer) &&

0 commit comments

Comments
 (0)