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