Skip to content

Commit 70ce6fc

Browse files
committed
util: rename validateHandler to assertRequestHandler, minor changes in jsdoc
1 parent 9ff7e90 commit 70ce6fc

3 files changed

Lines changed: 74 additions & 21 deletions

File tree

lib/core/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
isIterable,
1616
isBlobLike,
1717
serializePathWithQuery,
18-
validateHandler,
18+
assertRequestHandler,
1919
getServerName,
2020
normalizedMethodRecords
2121
} = require('./util')
@@ -183,7 +183,7 @@ class Request {
183183
throw new InvalidArgumentError('headers must be an object or an array')
184184
}
185185

186-
validateHandler(handler, method, upgrade)
186+
assertRequestHandler(handler, method, upgrade)
187187

188188
this.servername = servername || getServerName(this.host) || null
189189

lib/core/util.js

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ function isDestroyed (body) {
345345
/**
346346
* @param {import ('stream').Stream} stream
347347
* @param {Error} [err]
348-
* @returns
348+
* @returns {void}
349349
*/
350350
function destroy (stream, err) {
351351
if (stream == null || !isStream(stream) || isDestroyed(stream)) {
@@ -478,12 +478,24 @@ function parseRawHeaders (headers) {
478478
return ret
479479
}
480480

481+
/**
482+
* @param {*} buffer
483+
* @returns {buffer is Buffer}
484+
*/
481485
function isBuffer (buffer) {
482486
// See, https://github.com/mcollina/undici/pull/319
483487
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
484488
}
485489

486-
function validateHandler (handler, method, upgrade) {
490+
/**
491+
* Asserts that the handler object is a request handler.
492+
*
493+
* @param {object} handler
494+
* @param {string} method
495+
* @param {string} [upgrade]
496+
* @returns {asserts handler is import('../api/api-request').RequestHandler}
497+
*/
498+
function assertRequestHandler (handler, method, upgrade) {
487499
if (!handler || typeof handler !== 'object') {
488500
throw new InvalidArgumentError('handler must be an object')
489501
}
@@ -560,6 +572,7 @@ function getSocketInfo (socket) {
560572
}
561573

562574
/**
575+
* @param {Iterable} iterable
563576
* @returns {ReadableStream}
564577
*/
565578
function ReadableStreamFrom (iterable) {
@@ -623,23 +636,51 @@ function addAbortListener (signal, listener) {
623636
return () => signal.removeListener('abort', listener)
624637
}
625638

626-
const hasToWellFormed = typeof String.prototype.toWellFormed === 'function'
627-
const hasIsWellFormed = typeof String.prototype.isWellFormed === 'function'
628-
629639
/**
630-
* @param {string} val
640+
* @function
641+
* @param {string} value
642+
* @returns {string}
631643
*/
632-
function toUSVString (val) {
633-
return hasToWellFormed ? `${val}`.toWellFormed() : nodeUtil.toUSVString(val)
634-
}
644+
const toUSVString = (() => {
645+
if (typeof String.prototype.toWellFormed === 'function') {
646+
/**
647+
* @function
648+
* @param {string} value
649+
* @returns {string}
650+
*/
651+
return (value) => `${value}`.toWellFormed()
652+
} else {
653+
/**
654+
* @function
655+
* @param {string} value
656+
* @returns {string}
657+
*/
658+
return nodeUtil.toUSVString
659+
}
660+
})()
635661

636662
/**
637-
* @param {string} val
663+
* @param {*} value
664+
* @returns {boolean}
638665
*/
639666
// TODO: move this to webidl
640-
function isUSVString (val) {
641-
return hasIsWellFormed ? `${val}`.isWellFormed() : toUSVString(val) === `${val}`
642-
}
667+
const isUSVString = (() => {
668+
if (typeof String.prototype.isWellFormed === 'function') {
669+
/**
670+
* @function
671+
* @param {*} value
672+
* @returns {boolean}
673+
*/
674+
return (value) => `${value}`.isWellFormed()
675+
} else {
676+
/**
677+
* @function
678+
* @param {*} value
679+
* @returns {boolean}
680+
*/
681+
return (value) => toUSVString(value) === `${value}`
682+
}
683+
})()
643684

644685
/**
645686
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
@@ -710,11 +751,18 @@ function isValidHeaderValue (characters) {
710751

711752
const rangeHeaderRegex = /^bytes (\d+)-(\d+)\/(\d+)?$/
712753

754+
/**
755+
* @typedef {object} RangeHeader
756+
* @property {number} start
757+
* @property {number | null} end
758+
* @property {number | null} size
759+
*/
760+
713761
/**
714762
* Parse accordingly to RFC 9110
715763
* @see https://www.rfc-editor.org/rfc/rfc9110#field.content-range
716764
* @param {string} [range]
717-
* @returns
765+
* @returns {RangeHeader|null}
718766
*/
719767
function parseRangeHeader (range) {
720768
if (range == null || range === '') return { start: 0, end: null, size: null }
@@ -730,9 +778,11 @@ function parseRangeHeader (range) {
730778
}
731779

732780
/**
733-
* @param {Record<string|symbol, any>} obj
781+
* @template {import("events").EventEmitter} T
782+
* @param {T} obj
734783
* @param {string} name
735-
* @param {Function} listener
784+
* @param {(...args: any[]) => void} listener
785+
* @returns {T}
736786
*/
737787
function addListener (obj, name, listener) {
738788
const listeners = (obj[kListeners] ??= [])
@@ -742,7 +792,9 @@ function addListener (obj, name, listener) {
742792
}
743793

744794
/**
745-
* @param {Record<string|symbol, any>} obj
795+
* @template {import("events").EventEmitter} T
796+
* @param {T} obj
797+
* @returns {T}
746798
*/
747799
function removeAllListeners (obj) {
748800
if (obj[kListeners] != null) {
@@ -751,6 +803,7 @@ function removeAllListeners (obj) {
751803
}
752804
obj[kListeners] = null
753805
}
806+
return obj
754807
}
755808

756809
/**
@@ -821,7 +874,7 @@ module.exports = {
821874
deepClone,
822875
ReadableStreamFrom,
823876
isBuffer,
824-
validateHandler,
877+
assertRequestHandler,
825878
getSocketInfo,
826879
isFormDataLike,
827880
serializePathWithQuery,

lib/handler/redirect-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class RedirectHandler {
3838
throw new InvalidArgumentError('maxRedirections must be a positive number')
3939
}
4040

41-
util.validateHandler(handler, opts.method, opts.upgrade)
41+
util.assertRequestHandler(handler, opts.method, opts.upgrade)
4242

4343
this.dispatch = dispatch
4444
this.location = null

0 commit comments

Comments
 (0)