Skip to content

Commit 7fe1b5e

Browse files
Lxxyxdanielleadams
authored andcommitted
lib: refactor to use validateCallback
PR-URL: #36609 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 1fc30a8 commit 7fe1b5e

20 files changed

+114
-125
lines changed

lib/_tls_wrap.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const { connResetException, codes } = require('internal/errors');
6767
const {
6868
ERR_INVALID_ARG_TYPE,
6969
ERR_INVALID_ARG_VALUE,
70-
ERR_INVALID_CALLBACK,
7170
ERR_MULTIPLE_CALLBACK,
7271
ERR_SOCKET_CLOSED,
7372
ERR_TLS_DH_PARAM_SIZE,
@@ -85,6 +84,7 @@ const {
8584
getAllowUnauthorized,
8685
} = require('internal/options');
8786
const {
87+
validateCallback,
8888
validateString,
8989
validateBuffer,
9090
validateUint32
@@ -825,8 +825,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
825825
TLSSocket.prototype.renegotiate = function(options, callback) {
826826
if (options === null || typeof options !== 'object')
827827
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
828-
if (callback !== undefined && typeof callback !== 'function')
829-
throw new ERR_INVALID_CALLBACK(callback);
828+
if (callback !== undefined) {
829+
validateCallback(callback);
830+
}
830831

831832
debug('%s renegotiate()',
832833
this._tlsOptions.isServer ? 'server' : 'client',

lib/dns.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ const {
4545
const {
4646
ERR_INVALID_ARG_TYPE,
4747
ERR_INVALID_ARG_VALUE,
48-
ERR_INVALID_CALLBACK,
4948
ERR_MISSING_ARGS,
5049
} = errors.codes;
5150
const {
51+
validateCallback,
5252
validatePort,
5353
validateString,
5454
validateOneOf,
@@ -101,20 +101,24 @@ function lookup(hostname, options, callback) {
101101
// Parse arguments
102102
if (hostname && typeof hostname !== 'string') {
103103
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
104-
} else if (typeof options === 'function') {
104+
}
105+
106+
if (typeof options === 'function') {
105107
callback = options;
106108
family = 0;
107-
} else if (typeof callback !== 'function') {
108-
throw new ERR_INVALID_CALLBACK(callback);
109-
} else if (options !== null && typeof options === 'object') {
110-
hints = options.hints >>> 0;
111-
family = options.family >>> 0;
112-
all = options.all === true;
113-
verbatim = options.verbatim === true;
114-
115-
validateHints(hints);
116109
} else {
117-
family = options >>> 0;
110+
validateCallback(callback);
111+
112+
if (options !== null && typeof options === 'object') {
113+
hints = options.hints >>> 0;
114+
family = options.family >>> 0;
115+
all = options.all === true;
116+
verbatim = options.verbatim === true;
117+
118+
validateHints(hints);
119+
} else {
120+
family = options >>> 0;
121+
}
118122
}
119123

120124
validateOneOf(family, 'family', [0, 4, 6]);
@@ -177,8 +181,7 @@ function lookupService(address, port, callback) {
177181

178182
validatePort(port);
179183

180-
if (typeof callback !== 'function')
181-
throw new ERR_INVALID_CALLBACK(callback);
184+
validateCallback(callback);
182185

183186
port = +port;
184187

@@ -217,9 +220,7 @@ function resolver(bindingName) {
217220
}
218221

219222
validateString(name, 'name');
220-
if (typeof callback !== 'function') {
221-
throw new ERR_INVALID_CALLBACK(callback);
222-
}
223+
validateCallback(callback);
223224

224225
const req = new QueryReqWrap();
225226
req.bindingName = bindingName;

lib/fs.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const {
7575
ERR_FS_FILE_TOO_LARGE,
7676
ERR_INVALID_ARG_VALUE,
7777
ERR_INVALID_ARG_TYPE,
78-
ERR_INVALID_CALLBACK,
7978
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM
8079
},
8180
hideStackFrames,
@@ -125,6 +124,7 @@ const {
125124
isUint32,
126125
parseFileMode,
127126
validateBuffer,
127+
validateCallback,
128128
validateInteger,
129129
validateInt32
130130
} = require('internal/validators');
@@ -170,19 +170,16 @@ function showTruncateDeprecation() {
170170
}
171171

172172
function maybeCallback(cb) {
173-
if (typeof cb === 'function')
174-
return cb;
173+
validateCallback(cb);
175174

176-
throw new ERR_INVALID_CALLBACK(cb);
175+
return cb;
177176
}
178177

179178
// Ensure that callbacks run in the global context. Only use this function
180179
// for callbacks that are passed to the binding layer, callbacks that are
181180
// invoked from JS already run in the proper scope.
182181
function makeCallback(cb) {
183-
if (typeof cb !== 'function') {
184-
throw new ERR_INVALID_CALLBACK(cb);
185-
}
182+
validateCallback(cb);
186183

187184
return (...args) => cb(...args);
188185
}
@@ -191,9 +188,7 @@ function makeCallback(cb) {
191188
// an optimization, since the data passed back to the callback needs to be
192189
// transformed anyway.
193190
function makeStatsCallback(cb) {
194-
if (typeof cb !== 'function') {
195-
throw new ERR_INVALID_CALLBACK(cb);
196-
}
191+
validateCallback(cb);
197192

198193
return (err, stats) => {
199194
if (err) return cb(err);
@@ -2014,8 +2009,8 @@ function copyFile(src, dest, mode, callback) {
20142009
if (typeof mode === 'function') {
20152010
callback = mode;
20162011
mode = 0;
2017-
} else if (typeof callback !== 'function') {
2018-
throw new ERR_INVALID_CALLBACK(callback);
2012+
} else {
2013+
validateCallback(callback);
20192014
}
20202015

20212016
src = getValidatedPath(src, 'src');

lib/inspector.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const {
1717
ERR_INSPECTOR_NOT_ACTIVE,
1818
ERR_INSPECTOR_NOT_WORKER,
1919
ERR_INVALID_ARG_TYPE,
20-
ERR_INVALID_CALLBACK
2120
} = require('internal/errors').codes;
2221

2322
const { hasInspector } = internalBinding('config');
@@ -26,7 +25,10 @@ if (!hasInspector)
2625

2726
const EventEmitter = require('events');
2827
const { queueMicrotask } = require('internal/process/task_queues');
29-
const { validateString } = require('internal/validators');
28+
const {
29+
validateCallback,
30+
validateString,
31+
} = require('internal/validators');
3032
const { isMainThread } = require('worker_threads');
3133

3234
const {
@@ -100,8 +102,8 @@ class Session extends EventEmitter {
100102
if (params && typeof params !== 'object') {
101103
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
102104
}
103-
if (callback && typeof callback !== 'function') {
104-
throw new ERR_INVALID_CALLBACK(callback);
105+
if (callback) {
106+
validateCallback(callback);
105107
}
106108

107109
if (!this[connectionSymbol]) {

lib/internal/crypto/diffiehellman.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ const {
3131
ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE,
3232
ERR_INVALID_ARG_TYPE,
3333
ERR_INVALID_ARG_VALUE,
34-
ERR_INVALID_CALLBACK,
3534
}
3635
} = require('internal/errors');
3736

3837
const {
38+
validateCallback,
3939
validateInt32,
4040
validateObject,
4141
validateString,
@@ -325,8 +325,7 @@ function deriveBitsECDH(name, publicKey, privateKey, callback) {
325325
validateString(name, 'name');
326326
validateObject(publicKey, 'publicKey');
327327
validateObject(privateKey, 'privateKey');
328-
if (typeof callback !== 'function')
329-
throw new ERR_INVALID_CALLBACK(callback);
328+
validateCallback(callback);
330329
const job = new ECDHBitsJob(kCryptoJobAsync, name, publicKey, privateKey);
331330
job.ondone = (error, bits) => {
332331
if (error) return FunctionPrototypeCall(callback, job, error);
@@ -340,8 +339,7 @@ function deriveBitsECDH(name, publicKey, privateKey, callback) {
340339
function deriveBitsDH(publicKey, privateKey, callback) {
341340
validateObject(publicKey, 'publicKey');
342341
validateObject(privateKey, 'privateKey');
343-
if (typeof callback !== 'function')
344-
throw new ERR_INVALID_CALLBACK(callback);
342+
validateCallback(callback);
345343
const job = new DHBitsJob(kCryptoJobAsync, publicKey, privateKey);
346344
job.ondone = (error, bits) => {
347345
if (error) return FunctionPrototypeCall(callback, job, error);

lib/internal/crypto/hkdf.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
} = internalBinding('crypto');
1414

1515
const {
16+
validateCallback,
1617
validateInteger,
1718
validateString,
1819
validateUint32,
@@ -41,7 +42,6 @@ const {
4142

4243
const {
4344
codes: {
44-
ERR_INVALID_CALLBACK,
4545
ERR_INVALID_ARG_TYPE,
4646
ERR_OUT_OF_RANGE,
4747
ERR_MISSING_OPTION,
@@ -112,8 +112,7 @@ function hkdf(hash, key, salt, info, length, callback) {
112112
length,
113113
} = validateParameters(hash, key, salt, info, length));
114114

115-
if (typeof callback !== 'function')
116-
throw new ERR_INVALID_CALLBACK(callback);
115+
validateCallback(callback);
117116

118117
const job = new HKDFJob(kCryptoJobAsync, hash, key, salt, info, length);
119118

lib/internal/crypto/keygen.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const { customPromisifyArgs } = require('internal/util');
4040

4141
const {
4242
isUint32,
43+
validateCallback,
4344
validateString,
4445
validateInteger,
4546
validateObject,
@@ -50,7 +51,6 @@ const {
5051
codes: {
5152
ERR_INCOMPATIBLE_OPTION_PAIR,
5253
ERR_INVALID_ARG_VALUE,
53-
ERR_INVALID_CALLBACK,
5454
ERR_MISSING_OPTION,
5555
}
5656
} = require('internal/errors');
@@ -68,8 +68,7 @@ function generateKeyPair(type, options, callback) {
6868
callback = options;
6969
options = undefined;
7070
}
71-
if (typeof callback !== 'function')
72-
throw new ERR_INVALID_CALLBACK(callback);
71+
validateCallback(callback);
7372

7473
const job = createJob(kCryptoJobAsync, type, options);
7574

@@ -353,8 +352,7 @@ function generateKey(type, options, callback) {
353352
options = undefined;
354353
}
355354

356-
if (typeof callback !== 'function')
357-
throw new ERR_INVALID_CALLBACK(callback);
355+
validateCallback(callback);
358356

359357
const job = generateKeyJob(kCryptoJobAsync, type, options);
360358

lib/internal/crypto/pbkdf2.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const {
1414
} = internalBinding('crypto');
1515

1616
const {
17+
validateCallback,
1718
validateInteger,
1819
validateUint32,
1920
} = require('internal/validators');
2021

2122
const {
2223
ERR_INVALID_ARG_TYPE,
23-
ERR_INVALID_CALLBACK,
2424
ERR_MISSING_OPTION,
2525
} = require('internal/errors').codes;
2626

@@ -41,8 +41,7 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {
4141
({ password, salt, iterations, keylen, digest } =
4242
check(password, salt, iterations, keylen, digest));
4343

44-
if (typeof callback !== 'function')
45-
throw new ERR_INVALID_CALLBACK(callback);
44+
validateCallback(callback);
4645

4746
const job = new PBKDF2Job(
4847
kCryptoJobAsync,

lib/internal/crypto/random.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ const { kMaxLength } = require('buffer');
2323
const {
2424
codes: {
2525
ERR_INVALID_ARG_TYPE,
26-
ERR_INVALID_CALLBACK,
2726
ERR_OUT_OF_RANGE,
2827
}
2928
} = require('internal/errors');
3029

31-
const { validateNumber } = require('internal/validators');
30+
const {
31+
validateNumber,
32+
validateCallback,
33+
} = require('internal/validators');
3234

3335
const {
3436
isArrayBufferView,
@@ -73,8 +75,9 @@ function assertSize(size, elementSize, offset, length) {
7375

7476
function randomBytes(size, callback) {
7577
size = assertSize(size, 1, 0, Infinity);
76-
if (callback !== undefined && typeof callback !== 'function')
77-
throw new ERR_INVALID_CALLBACK(callback);
78+
if (callback !== undefined) {
79+
validateCallback(callback);
80+
}
7881

7982
const buf = new FastBuffer(size);
8083

@@ -141,8 +144,8 @@ function randomFill(buf, offset, size, callback) {
141144
} else if (typeof size === 'function') {
142145
callback = size;
143146
size = buf.byteLength - offset;
144-
} else if (typeof callback !== 'function') {
145-
throw new ERR_INVALID_CALLBACK(callback);
147+
} else {
148+
validateCallback(callback);
146149
}
147150

148151
offset = assertOffset(offset, elementSize, buf.byteLength);
@@ -188,8 +191,8 @@ function randomInt(min, max, callback) {
188191
}
189192

190193
const isSync = typeof callback === 'undefined';
191-
if (!isSync && typeof callback !== 'function') {
192-
throw new ERR_INVALID_CALLBACK(callback);
194+
if (!isSync) {
195+
validateCallback(callback);
193196
}
194197
if (!NumberIsSafeInteger(min)) {
195198
throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);

lib/internal/crypto/scrypt.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
} = internalBinding('crypto');
1515

1616
const {
17+
validateCallback,
1718
validateInteger,
1819
validateUint32,
1920
} = require('internal/validators');
@@ -22,7 +23,6 @@ const {
2223
codes: {
2324
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
2425
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
25-
ERR_INVALID_CALLBACK,
2626
}
2727
} = require('internal/errors');
2828

@@ -50,8 +50,7 @@ function scrypt(password, salt, keylen, options, callback = defaults) {
5050
const { N, r, p, maxmem } = options;
5151
({ password, salt, keylen } = options);
5252

53-
if (typeof callback !== 'function')
54-
throw new ERR_INVALID_CALLBACK(callback);
53+
validateCallback(callback);
5554

5655
const job = new ScryptJob(
5756
kCryptoJobAsync, password, salt, N, r, p, maxmem, keylen);

0 commit comments

Comments
 (0)