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

File tree

15 files changed

+79
-79
lines changed

15 files changed

+79
-79
lines changed

lib/assert.js

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 14 additions & 29 deletions
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 3 deletions
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

0 commit comments

Comments
 (0)