Skip to content

Commit

Permalink
util: rename validateHandler to assertRequestHandler, minor changes i…
Browse files Browse the repository at this point in the history
…n jsdoc
  • Loading branch information
Uzlopak committed Sep 11, 2024
1 parent 9ff7e90 commit 70ce6fc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
isIterable,
isBlobLike,
serializePathWithQuery,
validateHandler,
assertRequestHandler,
getServerName,
normalizedMethodRecords
} = require('./util')
Expand Down Expand Up @@ -183,7 +183,7 @@ class Request {
throw new InvalidArgumentError('headers must be an object or an array')
}

validateHandler(handler, method, upgrade)
assertRequestHandler(handler, method, upgrade)

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

Expand Down
89 changes: 71 additions & 18 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function isDestroyed (body) {
/**
* @param {import ('stream').Stream} stream
* @param {Error} [err]
* @returns
* @returns {void}
*/
function destroy (stream, err) {
if (stream == null || !isStream(stream) || isDestroyed(stream)) {
Expand Down Expand Up @@ -478,12 +478,24 @@ function parseRawHeaders (headers) {
return ret
}

/**
* @param {*} buffer
* @returns {buffer is Buffer}
*/
function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
}

function validateHandler (handler, method, upgrade) {
/**
* Asserts that the handler object is a request handler.
*
* @param {object} handler
* @param {string} method
* @param {string} [upgrade]
* @returns {asserts handler is import('../api/api-request').RequestHandler}
*/
function assertRequestHandler (handler, method, upgrade) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler must be an object')
}
Expand Down Expand Up @@ -560,6 +572,7 @@ function getSocketInfo (socket) {
}

/**
* @param {Iterable} iterable
* @returns {ReadableStream}
*/
function ReadableStreamFrom (iterable) {
Expand Down Expand Up @@ -623,23 +636,51 @@ function addAbortListener (signal, listener) {
return () => signal.removeListener('abort', listener)
}

const hasToWellFormed = typeof String.prototype.toWellFormed === 'function'
const hasIsWellFormed = typeof String.prototype.isWellFormed === 'function'

/**
* @param {string} val
* @function
* @param {string} value
* @returns {string}
*/
function toUSVString (val) {
return hasToWellFormed ? `${val}`.toWellFormed() : nodeUtil.toUSVString(val)
}
const toUSVString = (() => {
if (typeof String.prototype.toWellFormed === 'function') {
/**
* @function
* @param {string} value
* @returns {string}
*/
return (value) => `${value}`.toWellFormed()
} else {
/**
* @function
* @param {string} value
* @returns {string}
*/
return nodeUtil.toUSVString
}
})()

/**
* @param {string} val
* @param {*} value
* @returns {boolean}
*/
// TODO: move this to webidl
function isUSVString (val) {
return hasIsWellFormed ? `${val}`.isWellFormed() : toUSVString(val) === `${val}`
}
const isUSVString = (() => {
if (typeof String.prototype.isWellFormed === 'function') {
/**
* @function
* @param {*} value
* @returns {boolean}
*/
return (value) => `${value}`.isWellFormed()
} else {
/**
* @function
* @param {*} value
* @returns {boolean}
*/
return (value) => toUSVString(value) === `${value}`
}
})()

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

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

/**
* @typedef {object} RangeHeader
* @property {number} start
* @property {number | null} end
* @property {number | null} size
*/

/**
* Parse accordingly to RFC 9110
* @see https://www.rfc-editor.org/rfc/rfc9110#field.content-range
* @param {string} [range]
* @returns
* @returns {RangeHeader|null}
*/
function parseRangeHeader (range) {
if (range == null || range === '') return { start: 0, end: null, size: null }
Expand All @@ -730,9 +778,11 @@ function parseRangeHeader (range) {
}

/**
* @param {Record<string|symbol, any>} obj
* @template {import("events").EventEmitter} T
* @param {T} obj
* @param {string} name
* @param {Function} listener
* @param {(...args: any[]) => void} listener
* @returns {T}
*/
function addListener (obj, name, listener) {
const listeners = (obj[kListeners] ??= [])
Expand All @@ -742,7 +792,9 @@ function addListener (obj, name, listener) {
}

/**
* @param {Record<string|symbol, any>} obj
* @template {import("events").EventEmitter} T
* @param {T} obj
* @returns {T}
*/
function removeAllListeners (obj) {
if (obj[kListeners] != null) {
Expand All @@ -751,6 +803,7 @@ function removeAllListeners (obj) {
}
obj[kListeners] = null
}
return obj
}

/**
Expand Down Expand Up @@ -821,7 +874,7 @@ module.exports = {
deepClone,
ReadableStreamFrom,
isBuffer,
validateHandler,
assertRequestHandler,
getSocketInfo,
isFormDataLike,
serializePathWithQuery,
Expand Down
2 changes: 1 addition & 1 deletion lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RedirectHandler {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

util.validateHandler(handler, opts.method, opts.upgrade)
util.assertRequestHandler(handler, opts.method, opts.upgrade)

this.dispatch = dispatch
this.location = null
Expand Down

0 comments on commit 70ce6fc

Please sign in to comment.