@@ -35,10 +35,18 @@ const {
35
35
} = require ( 'internal/util/types' ) ;
36
36
const { signals } = internalBinding ( 'constants' ) . os ;
37
37
38
+ /**
39
+ * @param {* } value
40
+ * @returns {boolean }
41
+ */
38
42
function isInt32 ( value ) {
39
43
return value === ( value | 0 ) ;
40
44
}
41
45
46
+ /**
47
+ * @param {* } value
48
+ * @returns {boolean }
49
+ */
42
50
function isUint32 ( value ) {
43
51
return value === ( value >>> 0 ) ;
44
52
}
@@ -71,6 +79,16 @@ function parseFileMode(value, name, def) {
71
79
return value ;
72
80
}
73
81
82
+ /**
83
+ * @callback validateInteger
84
+ * @param {* } value
85
+ * @param {string } name
86
+ * @param {number } [min]
87
+ * @param {number } [max]
88
+ * @returns {asserts value is number }
89
+ */
90
+
91
+ /** @type {validateInteger } */
74
92
const validateInteger = hideStackFrames (
75
93
( value , name , min = NumberMIN_SAFE_INTEGER , max = NumberMAX_SAFE_INTEGER ) => {
76
94
if ( typeof value !== 'number' )
@@ -82,6 +100,16 @@ const validateInteger = hideStackFrames(
82
100
}
83
101
) ;
84
102
103
+ /**
104
+ * @callback validateInt32
105
+ * @param {* } value
106
+ * @param {string } name
107
+ * @param {number } [min]
108
+ * @param {number } [max]
109
+ * @returns {asserts value is number }
110
+ */
111
+
112
+ /** @type {validateInt32 } */
85
113
const validateInt32 = hideStackFrames (
86
114
( value , name , min = - 2147483648 , max = 2147483647 ) => {
87
115
// The defaults for min and max correspond to the limits of 32-bit integers.
@@ -97,7 +125,16 @@ const validateInt32 = hideStackFrames(
97
125
}
98
126
) ;
99
127
100
- const validateUint32 = hideStackFrames ( ( value , name , positive ) => {
128
+ /**
129
+ * @callback validateUint32
130
+ * @param {* } value
131
+ * @param {string } name
132
+ * @param {number|boolean } [positive=false]
133
+ * @returns {asserts value is number }
134
+ */
135
+
136
+ /** @type {validateUint32 } */
137
+ const validateUint32 = hideStackFrames ( ( value , name , positive = false ) => {
101
138
if ( typeof value !== 'number' ) {
102
139
throw new ERR_INVALID_ARG_TYPE ( name , 'number' , value ) ;
103
140
}
@@ -112,11 +149,29 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
112
149
}
113
150
} ) ;
114
151
152
+ /**
153
+ * @callback validateString
154
+ * @param {* } value
155
+ * @param {string } name
156
+ * @returns {asserts value is string }
157
+ */
158
+
159
+ /** @type {validateString } */
115
160
function validateString ( value , name ) {
116
161
if ( typeof value !== 'string' )
117
162
throw new ERR_INVALID_ARG_TYPE ( name , 'string' , value ) ;
118
163
}
119
164
165
+ /**
166
+ * @callback validateNumber
167
+ * @param {* } value
168
+ * @param {string } name
169
+ * @param {number } [min]
170
+ * @param {number } [max]
171
+ * @returns {asserts value is number }
172
+ */
173
+
174
+ /** @type {validateNumber } */
120
175
function validateNumber ( value , name , min = undefined , max ) {
121
176
if ( typeof value !== 'number' )
122
177
throw new ERR_INVALID_ARG_TYPE ( name , 'number' , value ) ;
@@ -130,6 +185,15 @@ function validateNumber(value, name, min = undefined, max) {
130
185
}
131
186
}
132
187
188
+ /**
189
+ * @callback validateOneOf
190
+ * @template T
191
+ * @param {T } value
192
+ * @param {string } name
193
+ * @param {T[] } oneOf
194
+ */
195
+
196
+ /** @type {validateOneOf } */
133
197
const validateOneOf = hideStackFrames ( ( value , name , oneOf ) => {
134
198
if ( ! ArrayPrototypeIncludes ( oneOf , value ) ) {
135
199
const allowed = ArrayPrototypeJoin (
@@ -141,6 +205,14 @@ const validateOneOf = hideStackFrames((value, name, oneOf) => {
141
205
}
142
206
} ) ;
143
207
208
+ /**
209
+ * @callback validateBoolean
210
+ * @param {* } value
211
+ * @param {string } name
212
+ * @returns {asserts value is boolean }
213
+ */
214
+
215
+ /** @type {validateBoolean } */
144
216
function validateBoolean ( value , name ) {
145
217
if ( typeof value !== 'boolean' )
146
218
throw new ERR_INVALID_ARG_TYPE ( name , 'boolean' , value ) ;
@@ -153,16 +225,19 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) {
153
225
}
154
226
155
227
/**
156
- * @param {unknown } value
228
+ * @callback validateObject
229
+ * @param {* } value
157
230
* @param {string } name
158
231
* @param {{
159
232
* allowArray?: boolean,
160
233
* allowFunction?: boolean,
161
234
* nullable?: boolean
162
235
* }} [options]
163
236
*/
237
+
238
+ /** @type {validateObject } */
164
239
const validateObject = hideStackFrames (
165
- ( value , name , options ) => {
240
+ ( value , name , options = null ) => {
166
241
const allowArray = getOwnPropertyValueOrDefault ( options , 'allowArray' , false ) ;
167
242
const allowFunction = getOwnPropertyValueOrDefault ( options , 'allowFunction' , false ) ;
168
243
const nullable = getOwnPropertyValueOrDefault ( options , 'nullable' , false ) ;
@@ -175,6 +250,15 @@ const validateObject = hideStackFrames(
175
250
}
176
251
} ) ;
177
252
253
+ /**
254
+ * @callback validateArray
255
+ * @param {* } value
256
+ * @param {string } name
257
+ * @param {number } [minLength]
258
+ * @returns {asserts value is any[] }
259
+ */
260
+
261
+ /** @type {validateArray } */
178
262
const validateArray = hideStackFrames ( ( value , name , minLength = 0 ) => {
179
263
if ( ! ArrayIsArray ( value ) ) {
180
264
throw new ERR_INVALID_ARG_TYPE ( name , 'Array' , value ) ;
@@ -185,6 +269,12 @@ const validateArray = hideStackFrames((value, name, minLength = 0) => {
185
269
}
186
270
} ) ;
187
271
272
+ // eslint-disable-next-line jsdoc/require-returns-check
273
+ /**
274
+ * @param {* } signal
275
+ * @param {string } [name='signal']
276
+ * @returns {asserts signal is keyof signals }
277
+ */
188
278
function validateSignalName ( signal , name = 'signal' ) {
189
279
validateString ( signal , name ) ;
190
280
@@ -198,6 +288,14 @@ function validateSignalName(signal, name = 'signal') {
198
288
}
199
289
}
200
290
291
+ /**
292
+ * @callback validateBuffer
293
+ * @param {* } buffer
294
+ * @param {string } [name='buffer']
295
+ * @returns {asserts buffer is ArrayBufferView }
296
+ */
297
+
298
+ /** @type {validateBuffer } */
201
299
const validateBuffer = hideStackFrames ( ( buffer , name = 'buffer' ) => {
202
300
if ( ! isArrayBufferView ( buffer ) ) {
203
301
throw new ERR_INVALID_ARG_TYPE ( name ,
@@ -206,6 +304,10 @@ const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
206
304
}
207
305
} ) ;
208
306
307
+ /**
308
+ * @param {string } data
309
+ * @param {string } encoding
310
+ */
209
311
function validateEncoding ( data , encoding ) {
210
312
const normalizedEncoding = normalizeEncoding ( encoding ) ;
211
313
const length = data . length ;
@@ -216,8 +318,14 @@ function validateEncoding(data, encoding) {
216
318
}
217
319
}
218
320
219
- // Check that the port number is not NaN when coerced to a number,
220
- // is an integer and that it falls within the legal range of port numbers.
321
+ /**
322
+ * Check that the port number is not NaN when coerced to a number,
323
+ * is an integer and that it falls within the legal range of port numbers.
324
+ * @param {* } port
325
+ * @param {string } [name='Port']
326
+ * @param {boolean } [allowZero=true]
327
+ * @returns {number }
328
+ */
221
329
function validatePort ( port , name = 'Port' , allowZero = true ) {
222
330
if ( ( typeof port !== 'number' && typeof port !== 'string' ) ||
223
331
( typeof port === 'string' && StringPrototypeTrim ( port ) . length === 0 ) ||
@@ -234,6 +342,13 @@ const validateCallback = hideStackFrames((callback) => {
234
342
throw new ERR_INVALID_CALLBACK ( callback ) ;
235
343
} ) ;
236
344
345
+ /**
346
+ * @callback validateAbortSignal
347
+ * @param {* } signal
348
+ * @param {string } name
349
+ */
350
+
351
+ /** @type {validateAbortSignal } */
237
352
const validateAbortSignal = hideStackFrames ( ( signal , name ) => {
238
353
if ( signal !== undefined &&
239
354
( signal === null ||
@@ -243,21 +358,51 @@ const validateAbortSignal = hideStackFrames((signal, name) => {
243
358
}
244
359
} ) ;
245
360
361
+ /**
362
+ * @callback validateFunction
363
+ * @param {* } value
364
+ * @param {string } name
365
+ * @returns {asserts value is Function }
366
+ */
367
+
368
+ /** @type {validateFunction } */
246
369
const validateFunction = hideStackFrames ( ( value , name ) => {
247
370
if ( typeof value !== 'function' )
248
371
throw new ERR_INVALID_ARG_TYPE ( name , 'Function' , value ) ;
249
372
} ) ;
250
373
374
+ /**
375
+ * @callback validatePlainFunction
376
+ * @param {* } value
377
+ * @param {string } name
378
+ * @returns {asserts value is Function }
379
+ */
380
+
381
+ /** @type {validatePlainFunction } */
251
382
const validatePlainFunction = hideStackFrames ( ( value , name ) => {
252
383
if ( typeof value !== 'function' || isAsyncFunction ( value ) )
253
384
throw new ERR_INVALID_ARG_TYPE ( name , 'Function' , value ) ;
254
385
} ) ;
255
386
387
+ /**
388
+ * @callback validateUndefined
389
+ * @param {* } value
390
+ * @param {string } name
391
+ * @returns {asserts value is undefined }
392
+ */
393
+
394
+ /** @type {validateUndefined } */
256
395
const validateUndefined = hideStackFrames ( ( value , name ) => {
257
396
if ( value !== undefined )
258
397
throw new ERR_INVALID_ARG_TYPE ( name , 'undefined' , value ) ;
259
398
} ) ;
260
399
400
+ /**
401
+ * @template T
402
+ * @param {T } value
403
+ * @param {string } name
404
+ * @param {T[] } union
405
+ */
261
406
function validateUnion ( value , name , union ) {
262
407
if ( ! ArrayPrototypeIncludes ( union , value ) ) {
263
408
throw new ERR_INVALID_ARG_TYPE ( name , `('${ ArrayPrototypeJoin ( union , '|' ) } ')` , value ) ;
0 commit comments