the editable site (validation-lab)

This commit is contained in:
kpgarrod committed 2026-11-21 08:03:55 +01:00
commit 3ac81d5a52
13454 files changed
+2227005

No files matched your search

+1222
View File
File diff suppressed because it is too large. Load diff
+134
View File
@@ -0,0 +1,134 @@
'use strict'
const assert = require('node:assert')
/**
* This takes care of revalidation requests we send to the origin. If we get
* a response indicating that what we have is cached (via a HTTP 304), we can
* continue using the cached value. Otherwise, we'll receive the new response
* here, which we then just pass on to the next handler (most likely a
* CacheHandler). Note that this assumes the proper headers were already
* included in the request to tell the origin that we want to revalidate the
* response (i.e. if-modified-since or if-none-match).
*
* @see https://www.rfc-editor.org/rfc/rfc9111.html#name-validation
*
* @implements {import('../../types/dispatcher.d.ts').default.DispatchHandler}
*/
class CacheRevalidationHandler {
#successful = false
/**
* @type {((success: boolean, context?: any, statusCode?: number, headers?: import('../../types/header.d.ts').IncomingHttpHeaders) => void) | null}
*/
#callback
/**
* @type {(import('../../types/dispatcher.d.ts').default.DispatchHandler)}
*/
#handler
#context
/**
* @type {boolean}
*/
#allowErrorStatusCodes
/**
* @param {(success: boolean, context?: any, statusCode?: number, headers?: import('../../types/header.d.ts').IncomingHttpHeaders) => void} callback Function to call if the cached value is valid
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
* @param {boolean} allowErrorStatusCodes
*/
constructor (callback, handler, allowErrorStatusCodes) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function')
}
this.#callback = callback
this.#handler = handler
this.#allowErrorStatusCodes = allowErrorStatusCodes
}
onRequestStart (_, context) {
this.#successful = false
this.#context = context
}
onRequestUpgrade (controller, statusCode, headers, socket) {
this.#handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
}
onResponseStart (
controller,
statusCode,
headers,
statusMessage
) {
assert(this.#callback != null)
// https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-a-validation-respo
// https://datatracker.ietf.org/doc/html/rfc5861#section-4
this.#successful = statusCode === 304 ||
(this.#allowErrorStatusCodes && statusCode >= 500 && statusCode <= 504)
this.#callback(this.#successful, this.#context, statusCode, headers)
this.#callback = null
if (this.#successful) {
return true
}
this.#handler.onRequestStart?.(controller, this.#context)
this.#handler.onResponseStart?.(
controller,
statusCode,
headers,
statusMessage
)
}
onResponseData (controller, chunk) {
if (this.#successful) {
return
}
return this.#handler.onResponseData?.(controller, chunk)
}
onResponseEnd (controller, trailers) {
if (this.#successful) {
return
}
this.#handler.onResponseEnd?.(controller, trailers)
}
onResponseError (controller, err) {
if (this.#successful) {
return
}
if (this.#callback) {
// Serve the stale cached response on a connection error, per stale-if-error:
// RFC 5861 counts an unreachable origin (a would-be 5xx) as an error.
// https://datatracker.ietf.org/doc/html/rfc5861#section-4
if (this.#allowErrorStatusCodes) {
this.#successful = true
this.#callback(true, this.#context)
this.#callback = null
return
}
this.#callback(false)
this.#callback = null
}
if (typeof this.#handler.onResponseError === 'function') {
this.#handler.onResponseError(controller, err)
} else {
throw err
}
}
}
module.exports = CacheRevalidationHandler
+76
View File
@@ -0,0 +1,76 @@
'use strict'
const assert = require('node:assert')
/**
* @deprecated
*/
module.exports = class DecoratorHandler {
#handler
#onCompleteCalled = false
#onErrorCalled = false
#onResponseStartCalled = false
constructor (handler) {
if (typeof handler !== 'object' || handler === null) {
throw new TypeError('handler must be an object')
}
this.#handler = handler
}
onRequestStart (...args) {
this.#handler.onRequestStart?.(...args)
}
onRequestUpgrade (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)
return this.#handler.onRequestUpgrade?.(...args)
}
onResponseStart (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)
// Interim 1xx responses (e.g. 103 Early Hints) come before the final one.
const statusCode = args[1]
if (statusCode >= 200) {
assert(!this.#onResponseStartCalled)
this.#onResponseStartCalled = true
}
return this.#handler.onResponseStart?.(...args)
}
onResponseData (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)
return this.#handler.onResponseData?.(...args)
}
onResponseEnd (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)
this.#onCompleteCalled = true
return this.#handler.onResponseEnd?.(...args)
}
onResponseError (...args) {
this.#onErrorCalled = true
return this.#handler.onResponseError?.(...args)
}
/**
* @deprecated
*/
onBodySent (...args) {
return this.#handler.onBodySent?.(...args)
}
onRequestSent (...args) {
return this.#handler.onRequestSent?.(...args)
}
}
+540
View File
@@ -0,0 +1,540 @@
'use strict'
const { RequestAbortedError } = require('../core/errors')
/**
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
*/
const DEFAULT_MAX_BUFFER_SIZE = 5 * 1024 * 1024
/**
* @typedef {Object} WaitingHandler
* @property {DispatchHandler} handler
* @property {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @property {Buffer[]} bufferedChunks
* @property {number} bufferedBytes
* @property {object | null} pendingTrailers
* @property {boolean} done
* @property {boolean} [pendingStart] onRequestStart deferred until the primary request's context is known
*/
/**
* Handler that forwards response events to multiple waiting handlers.
* Used for request deduplication.
*
* @implements {DispatchHandler}
*/
class DeduplicationHandler {
/**
* @type {DispatchHandler}
*/
#primaryHandler
/**
* Whether the primary request has started (onRequestStart received). Until
* then the request context is unknown and cannot be given to waiting handlers.
* @type {boolean}
*/
#requestStarted = false
/**
* The primary request context, replayed to waiting handlers so they observe
* the same redirect history as the primary.
* @type {any}
*/
#context = null
/**
* @type {WaitingHandler[]}
*/
#waitingHandlers = []
/**
* @type {number}
*/
#maxBufferSize = DEFAULT_MAX_BUFFER_SIZE
/**
* @type {number}
*/
#statusCode = 0
/**
* @type {Record<string, string | string[]>}
*/
#headers = {}
/**
* @type {string}
*/
#statusMessage = ''
/**
* @type {boolean}
*/
#aborted = false
/**
* @type {boolean}
*/
#responseStarted = false
/**
* @type {boolean}
*/
#responseDataStarted = false
/**
* @type {boolean}
*/
#completed = false
/**
* @type {import('../../types/dispatcher.d.ts').default.DispatchController | null}
*/
#controller = null
/**
* @type {(() => void) | null}
*/
#onComplete = null
/**
* @param {DispatchHandler} primaryHandler The primary handler
* @param {() => void} onComplete Callback when request completes
* @param {number} [maxBufferSize] Maximum paused buffer size per waiting handler
*/
constructor (primaryHandler, onComplete, maxBufferSize = DEFAULT_MAX_BUFFER_SIZE) {
this.#primaryHandler = primaryHandler
this.#onComplete = onComplete
this.#maxBufferSize = maxBufferSize
}
/**
* Add a waiting handler that will receive response events.
* Returns false if deduplication can no longer safely attach this handler.
*
* @param {DispatchHandler} handler
* @returns {boolean}
*/
addWaitingHandler (handler) {
if (this.#completed || this.#responseDataStarted) {
return false
}
const waitingHandler = this.#createWaitingHandler(handler)
const waitingController = waitingHandler.controller
try {
if (this.#requestStarted) {
// The primary request has started, so its context (including the
// redirect history) is known and can be delivered immediately.
handler.onRequestStart?.(waitingController, this.#context)
if (waitingController.aborted) {
waitingHandler.done = true
return true
}
if (this.#responseStarted) {
handler.onResponseStart?.(
waitingController,
this.#statusCode,
this.#headers,
this.#statusMessage
)
}
} else {
// The primary request has not started yet. Defer onRequestStart until
// its context is known, so the waiting handler observes the same
// redirect history as the primary (a cache handler relies on it to
// reject cross-origin redirect responses).
waitingHandler.pendingStart = true
}
} catch {
// Ignore errors from waiting handlers
waitingHandler.done = true
return true
}
if (!waitingController.aborted) {
this.#waitingHandlers.push(waitingHandler)
}
return true
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {any} context
*/
onRequestStart (controller, context) {
this.#controller = controller
if (!this.#requestStarted) {
this.#requestStarted = true
this.#context = context
// Deliver the now-known context to handlers that joined before the
// primary request started.
for (const waitingHandler of this.#waitingHandlers) {
if (waitingHandler.pendingStart && !waitingHandler.done && !waitingHandler.controller.aborted) {
waitingHandler.pendingStart = false
try {
waitingHandler.handler.onRequestStart?.(waitingHandler.controller, context)
} catch {
// Ignore errors from waiting handlers
waitingHandler.done = true
}
}
}
this.#pruneDoneWaitingHandlers()
}
this.#primaryHandler.onRequestStart?.(controller, context)
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {number} statusCode
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} headers
* @param {Socket} socket
*/
onRequestUpgrade (controller, statusCode, headers, socket) {
this.#primaryHandler.onRequestUpgrade?.(controller, statusCode, headers, socket)
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {number} statusCode
* @param {Record<string, string | string[]>} headers
* @param {string} statusMessage
*/
onResponseStart (controller, statusCode, headers, statusMessage) {
this.#responseStarted = true
this.#statusCode = statusCode
this.#headers = headers
this.#statusMessage = statusMessage
this.#primaryHandler.onResponseStart?.(controller, statusCode, headers, statusMessage)
for (const waitingHandler of this.#waitingHandlers) {
const { handler, controller: waitingController } = waitingHandler
if (waitingHandler.done || waitingController.aborted) {
waitingHandler.done = true
continue
}
try {
handler.onResponseStart?.(
waitingController,
statusCode,
headers,
statusMessage
)
} catch {
// Ignore errors from waiting handlers
}
if (waitingController.aborted) {
waitingHandler.done = true
}
}
this.#pruneDoneWaitingHandlers()
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {Buffer} chunk
*/
onResponseData (controller, chunk) {
if (this.#aborted || this.#completed) {
return
}
this.#responseDataStarted = true
this.#primaryHandler.onResponseData?.(controller, chunk)
for (const waitingHandler of this.#waitingHandlers) {
const { handler, controller: waitingController } = waitingHandler
if (waitingHandler.done || waitingController.aborted) {
waitingHandler.done = true
continue
}
if (waitingController.paused) {
this.#bufferWaitingChunk(waitingHandler, chunk)
continue
}
try {
handler.onResponseData?.(waitingController, chunk)
} catch {
// Ignore errors from waiting handlers
}
if (waitingController.aborted) {
waitingHandler.done = true
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0
}
}
this.#pruneDoneWaitingHandlers()
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {object} trailers
*/
onResponseEnd (controller, trailers) {
if (this.#aborted || this.#completed) {
return
}
this.#completed = true
// Remove the completed request before callbacks can redispatch into this
// interceptor. Cleanup must not depend on downstream handlers succeeding.
this.#cleanup()
try {
this.#primaryHandler.onResponseEnd?.(controller, trailers)
} catch (err) {
// Completion callbacks may throw to initiate a retry. The transport's
// error callback would be ignored now that this handler is completed.
this.#primaryHandler.onResponseError?.(controller, err)
} finally {
for (const waitingHandler of this.#waitingHandlers) {
if (waitingHandler.done || waitingHandler.controller.aborted) {
waitingHandler.done = true
continue
}
this.#flushWaitingHandler(waitingHandler)
if (waitingHandler.done || waitingHandler.controller.aborted) {
waitingHandler.done = true
continue
}
if (waitingHandler.controller.paused) {
waitingHandler.pendingTrailers = trailers
continue
}
this.#endWaitingHandler(waitingHandler, trailers)
}
this.#pruneDoneWaitingHandlers()
}
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
* @param {Error} err
*/
onResponseError (controller, err) {
if (this.#completed) {
return
}
this.#aborted = true
this.#completed = true
this.#cleanup()
try {
this.#primaryHandler.onResponseError?.(controller, err)
} finally {
for (const waitingHandler of this.#waitingHandlers) {
this.#errorWaitingHandler(waitingHandler, err)
}
this.#waitingHandlers = []
}
}
#cleanup () {
const onComplete = this.#onComplete
this.#onComplete = null
onComplete?.()
}
/**
* @param {DispatchHandler} handler
* @returns {WaitingHandler}
*/
#createWaitingHandler (handler) {
/** @type {WaitingHandler} */
const waitingHandler = {
handler,
controller: null,
bufferedChunks: [],
bufferedBytes: 0,
pendingTrailers: null,
done: false,
pendingStart: false
}
const state = {
aborted: false,
paused: false,
reason: null
}
waitingHandler.controller = {
resume: () => {
if (state.aborted) {
return
}
state.paused = false
this.#flushWaitingHandler(waitingHandler)
if (
this.#completed &&
waitingHandler.pendingTrailers &&
waitingHandler.bufferedChunks.length === 0 &&
!state.paused &&
!state.aborted
) {
this.#endWaitingHandler(waitingHandler, waitingHandler.pendingTrailers)
}
this.#pruneDoneWaitingHandlers()
},
pause: () => {
if (!state.aborted) {
state.paused = true
}
},
get paused () { return state.paused },
get aborted () { return state.aborted },
get reason () { return state.reason },
abort: (reason) => {
if (state.aborted) {
return
}
state.aborted = true
state.reason = reason ?? null
waitingHandler.done = true
waitingHandler.pendingTrailers = null
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0
try {
handler.onResponseError?.(waitingHandler.controller, state.reason ?? new RequestAbortedError())
} catch {
// Ignore errors from waiting handlers
}
}
}
return waitingHandler
}
/**
* @param {WaitingHandler} waitingHandler
* @param {Buffer} chunk
*/
#bufferWaitingChunk (waitingHandler, chunk) {
if (waitingHandler.done || waitingHandler.controller.aborted) {
waitingHandler.done = true
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0
return
}
const bufferedChunk = Buffer.from(chunk)
waitingHandler.bufferedChunks.push(bufferedChunk)
waitingHandler.bufferedBytes += bufferedChunk.length
if (waitingHandler.bufferedBytes > this.#maxBufferSize) {
const err = new RequestAbortedError(`Deduplicated waiting handler exceeded maxBufferSize (${this.#maxBufferSize} bytes) while paused`)
waitingHandler.controller.abort(err)
}
}
/**
* @param {WaitingHandler} waitingHandler
*/
#flushWaitingHandler (waitingHandler) {
const { handler, controller } = waitingHandler
while (
!waitingHandler.done &&
!controller.aborted &&
!controller.paused &&
waitingHandler.bufferedChunks.length > 0
) {
const bufferedChunk = waitingHandler.bufferedChunks.shift()
waitingHandler.bufferedBytes -= bufferedChunk.length
try {
handler.onResponseData?.(controller, bufferedChunk)
} catch {
// Ignore errors from waiting handlers
}
if (controller.aborted) {
waitingHandler.done = true
waitingHandler.pendingTrailers = null
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0
break
}
}
}
/**
* @param {WaitingHandler} waitingHandler
* @param {object} trailers
*/
#endWaitingHandler (waitingHandler, trailers) {
waitingHandler.done = true
waitingHandler.pendingTrailers = null
try {
waitingHandler.handler.onResponseEnd?.(waitingHandler.controller, trailers)
} catch (err) {
// This is a response failure, not a consumer abort. Aborting the waiting
// controller here would prevent downstream handlers from retrying.
try {
waitingHandler.handler.onResponseError?.(waitingHandler.controller, err)
} catch {
// An error callback must not prevent other waiting handlers completing.
}
}
}
/**
* @param {WaitingHandler} waitingHandler
* @param {Error} err
*/
#errorWaitingHandler (waitingHandler, err) {
if (waitingHandler.done) {
return
}
waitingHandler.done = true
waitingHandler.pendingTrailers = null
waitingHandler.bufferedChunks = []
waitingHandler.bufferedBytes = 0
// A response failure is not a consumer abort: retry handlers must be able
// to retry it just as they would a failure of the primary request.
try {
waitingHandler.handler.onResponseError?.(waitingHandler.controller, err)
} catch {
// Ignore errors from waiting handlers
}
}
#pruneDoneWaitingHandlers () {
this.#waitingHandlers = this.#waitingHandlers.filter(waitingHandler => waitingHandler.done === false)
}
}
module.exports = DeduplicationHandler
+284
View File
@@ -0,0 +1,284 @@
'use strict'
const util = require('../core/util')
const assert = require('node:assert')
const { InvalidArgumentError, MaxRedirectsError } = require('../core/errors')
const { kRequestOrigin, kRedirectCacheControl } = require('../core/symbols')
const redirectableStatusCodes = [300, 301, 302, 303, 307, 308]
const noop = () => {}
class RedirectHandler {
static buildDispatch (dispatcher, maxRedirections) {
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}
const dispatch = dispatcher.dispatch.bind(dispatcher)
return (opts, originalHandler) => dispatch(opts, new RedirectHandler(dispatch, maxRedirections, opts, originalHandler))
}
constructor (dispatch, maxRedirections, opts, handler) {
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}
if (opts.throwOnMaxRedirect != null && typeof opts.throwOnMaxRedirect !== 'boolean') {
throw new InvalidArgumentError('throwOnMaxRedirect must be a boolean')
}
this.dispatch = dispatch
this.location = null
const { maxRedirections: _, stripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect, ...cleanOpts } = opts
this.opts = cleanOpts // opts must be a copy, exclude maxRedirections
this.opts.body = util.wrapRequestBody(this.opts.body)
this.stripHeadersOnRedirect = normalizeStripHeaders(stripHeadersOnRedirect, 'stripHeadersOnRedirect')
this.stripHeadersOnCrossOriginRedirect = normalizeStripHeaders(stripHeadersOnCrossOriginRedirect, 'stripHeadersOnCrossOriginRedirect')
this.maxRedirections = maxRedirections
this.handler = handler
this.history = []
// Per-hop metadata for each intermediate (suppressed) redirect hop: the
// hop's status code and response headers. An outer cache uses it to honor a
// hop's storage policy (e.g. `no-store`, `no-cache`, `Vary: *`) that would
// otherwise be lost because the hop's response is not forwarded. It travels
// as a non-enumerable property of the history array so it reaches the cache
// without becoming part of the public request/response context.
Object.defineProperty(this.history, kRedirectCacheControl, {
value: [],
enumerable: false
})
}
onRequestStart (controller, context) {
this.handler.onRequestStart?.(controller, { ...context, history: this.history })
}
onBodySent (chunk) {
this.handler.onBodySent?.(chunk)
}
onRequestSent () {
this.handler.onRequestSent?.()
}
onRequestUpgrade (controller, statusCode, headers, socket) {
this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
}
onResponseStart (controller, statusCode, headers, statusMessage) {
if (statusCode < 200) {
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
return
}
let removeContentHeaders = statusCode === 303
// https://tools.ietf.org/html/rfc7231#section-6.4.2
// https://fetch.spec.whatwg.org/#http-redirect-fetch
// In case of HTTP 301 or 302 with POST, change the method to GET
// QUERY is safe (RFC 10008) and should not change method like GET.
if ((statusCode === 301 || statusCode === 302) && this.opts.method === 'POST') {
this.opts.method = 'GET'
if (util.isStream(this.opts.body)) {
util.destroy(this.opts.body.on('error', noop))
}
this.opts.body = null
removeContentHeaders = true
}
// https://tools.ietf.org/html/rfc7231#section-6.4.4
// In case of HTTP 303, always replace method to be either HEAD or GET
if (statusCode === 303 && this.opts.method !== 'HEAD') {
this.opts.method = 'GET'
if (util.isStream(this.opts.body)) {
util.destroy(this.opts.body.on('error', noop))
}
this.opts.body = null
}
this.location = util.isDisturbed(this.opts.body) || redirectableStatusCodes.indexOf(statusCode) === -1
? null
: headers.location || null
// Only a redirect that would otherwise be followed counts against the
// limit. The final response after exactly maxRedirections redirects, or a
// 3xx without a Location header, is delivered as usual.
if (this.location && this.history.length >= this.maxRedirections) {
if (this.opts.throwOnMaxRedirect) {
throw new MaxRedirectsError()
}
this.location = null
}
const requestOrigin = this.opts[kRequestOrigin] === undefined
? this.opts.origin
: this.opts[kRequestOrigin]
if (requestOrigin) {
this.history.push(new URL(this.opts.path, requestOrigin))
}
if (!this.location) {
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
return
}
// This hop's response is suppressed (only its Location is used). Record its
// status and headers so an outer cache can honor a restrictive policy that
// would otherwise be lost when the final response is stored.
this.history[kRedirectCacheControl].push({
statusCode,
headers: { ...headers },
receivedAt: Date.now()
})
const baseUrl = requestOrigin
? new URL(this.opts.path, requestOrigin)
: undefined
const { origin, pathname, search } = util.parseURL(new URL(this.location, baseUrl))
const path = search ? `${pathname}${search}` : pathname
// Check for redirect loops by seeing if we've already visited this URL in our history
// This catches the case where Client/Pool try to handle cross-origin redirects but fail
// and keep redirecting to the same URL in an infinite loop
const redirectUrlString = `${origin}${path}`
for (const historyUrl of this.history) {
if (historyUrl.toString() === redirectUrlString) {
throw new InvalidArgumentError(`Redirect loop detected. Cannot redirect to ${origin}. This typically happens when using a Client or Pool with cross-origin redirects. Use an Agent for cross-origin redirects.`)
}
}
// Remove headers referring to the original URL.
// By default it is Host only. A 303 or a 301/302 POST-to-GET redirect also removes all Content-* headers.
// https://tools.ietf.org/html/rfc7231#section-6.4
this.opts.headers = cleanRequestHeaders(this.opts.headers, removeContentHeaders, requestOrigin !== origin, this.stripHeadersOnRedirect, this.stripHeadersOnCrossOriginRedirect)
this.opts.path = path
this.opts.origin = origin
this.opts[kRequestOrigin] = origin
this.opts.query = null
}
onResponseData (controller, chunk) {
if (this.location) {
/*
https://tools.ietf.org/html/rfc7231#section-6.4
TLDR: undici always ignores 3xx response bodies.
Redirection is used to serve the requested resource from another URL, so it assumes that
no body is generated (and thus can be ignored). Even though generating a body is not prohibited.
For status 301, 302, 303, 307 and 308 (the latter from RFC 7238), the specs mention that the body usually
(which means it's optional and not mandated) contain just an hyperlink to the value of
the Location response header, so the body can be ignored safely.
For status 300, which is "Multiple Choices", the spec mentions both generating a Location
response header AND a response body with the other possible location to follow.
Since the spec explicitly chooses not to specify a format for such body and leave it to
servers and browsers implementors, we ignore the body as there is no specified way to eventually parse it.
*/
} else {
this.handler.onResponseData?.(controller, chunk)
}
}
onResponseEnd (controller, trailers) {
if (this.location) {
/*
https://tools.ietf.org/html/rfc7231#section-6.4
TLDR: undici always ignores 3xx response trailers as they are not expected in case of redirections
and neither are useful if present.
See comment on onData method above for more detailed information.
*/
this.dispatch(this.opts, this)
} else {
this.handler.onResponseEnd(controller, trailers)
}
}
onResponseError (controller, error) {
this.handler.onResponseError?.(controller, error)
}
}
// https://tools.ietf.org/html/rfc7231#section-6.4.4
function shouldRemoveHeader (header, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin) {
const name = util.headerNameToString(header)
if (name === 'host') {
return true
}
if (stripHeaders?.has(name) || (unknownOrigin && stripHeadersOnCrossOrigin?.has(name))) {
return true
}
if (removeContent && name.startsWith('content-')) {
return true
}
if (unknownOrigin) {
return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
}
return false
}
// https://tools.ietf.org/html/rfc7231#section-6.4
function normalizeStripHeaders (headers, optionName) {
if (headers == null) {
return null
}
if (!Array.isArray(headers)) {
throw new InvalidArgumentError(`${optionName} must be an array`)
}
const normalized = new Set()
for (const header of headers) {
if (typeof header !== 'string') {
throw new InvalidArgumentError(`${optionName} must contain header names`)
}
normalized.add(util.headerNameToString(header))
}
return normalized
}
function cleanRequestHeaders (headers, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin) {
const ret = []
if (Array.isArray(headers)) {
// Headers may be either a flat alternating [name, value, name, value, ...]
// list or an iterable of [name, value] pairs. A plain JavaScript array of
// tuples (e.g. `Object.entries(headers)`) satisfies the iterable-of-pairs
// public type, so we must distinguish the two representations before
// iterating. Treating every Array as a flat list misreads each tuple as a
// header name/value pair and defeats the cross-origin credential stripping.
if (headers.length > 0 && Array.isArray(headers[0])) {
// Array of [name, value] pairs.
for (const [key, value] of headers) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
ret.push(key, value)
}
}
} else {
// Flat alternating [name, value, name, value, ...] list.
for (let i = 0; i < headers.length; i += 2) {
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
ret.push(headers[i], headers[i + 1])
}
}
}
} else if (headers && typeof headers === 'object') {
const entries = util.hasSafeIterator(headers) ? headers : Object.entries(headers)
for (const [key, value] of entries) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
ret.push(key, value)
}
}
} else {
assert(headers == null, 'headers must be an object or an array')
}
return ret
}
module.exports = RedirectHandler
+712
View File
@@ -0,0 +1,712 @@
'use strict'
const assert = require('node:assert')
const { kRetryHandlerDefaultRetry, kQueuedAbort } = require('../core/symbols')
const { RequestRetryError, RequestAbortedError } = require('../core/errors')
const {
hasSafeIterator,
isDisturbed,
parseRangeHeader,
wrapRequestBody
} = require('../core/util')
function calculateRetryAfterHeader (retryAfter) {
const retryTime = new Date(retryAfter).getTime()
return isNaN(retryTime) ? null : retryTime - Date.now()
}
function validatePartialResponseContentLength (headers, range, statusCode, retryCount) {
const contentLength = headers['content-length']
if (contentLength == null) {
return
}
if (!Number.isFinite(range.start) || !Number.isFinite(range.end)) {
return
}
const length = Number(contentLength)
const expectedLength = range.end - range.start + 1
if (!Number.isFinite(length) || length !== expectedLength) {
throw new RequestRetryError('Content-Length mismatch', statusCode, {
headers,
data: { count: retryCount }
})
}
}
// A stable controller handed to the downstream handler for the lifetime of the
// request. Each transparent retry/resume is a *separate* dispatch with its
// *own* connection controller. Without a stable proxy the downstream body keeps
// flow-controlling the original (now-dead) controller while data flows on the
// new one: backpressure pauses the new connection's controller, but the
// consumer's resume() targets the old one, so the resumed body stalls forever.
// The proxy always forwards to the controller of the currently active connection.
// An abort is additionally reported to the handler so it can cancel a pending
// retry backoff instead of letting the request hang until the backoff elapses.
// The notification is a private callback the handler hands over on construction,
// so nothing outside the handler can trigger it.
class RetryController {
#onAbort
#paused = false
#target = null
constructor (onAbort) {
this.#onAbort = onAbort
}
set target (target) {
this.#target = target
if (this.#paused) {
target?.pause()
}
}
get target () { return this.#target }
pause () {
this.#paused = true
this.#target?.pause()
}
resume () {
this.#paused = false
this.#target?.resume()
}
abort (reason) {
this.#target?.abort(reason)
this.#onAbort(reason)
}
get paused () { return this.#paused || (this.#target?.paused ?? false) }
get aborted () { return this.#target?.aborted ?? false }
get reason () { return this.#target?.reason ?? null }
get rawHeaders () { return this.#target?.rawHeaders ?? null }
set rawHeaders (value) {
if (this.#target) {
this.#target.rawHeaders = value
}
}
get rawTrailers () { return this.#target?.rawTrailers ?? null }
set rawTrailers (value) {
if (this.#target) {
this.#target.rawTrailers = value
}
}
}
class RetryHandler {
// Takes the retry that waits for a connection out of its queue.
#queuedCancel = null
// Whether retries are dispatched with a hook that sets #queuedCancel.
#tracksQueuedRetry = false
constructor (opts, { dispatch, handler }) {
const { retryOptions, ...dispatchOpts } = opts
const {
// Retry scoped
retry: retryFn,
maxRetries,
maxTimeout,
minTimeout,
timeoutFactor,
// Response scoped
methods,
errorCodes,
retryAfter,
statusCodes,
throwOnError
} = retryOptions ?? {}
this.error = null
this.dispatch = dispatch
this.handler = handler
this.opts = { ...dispatchOpts, body: wrapRequestBody(opts.body) }
this.retryOpts = {
throwOnError: throwOnError ?? true,
retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry],
retryAfter: retryAfter ?? true,
maxTimeout: maxTimeout ?? 30 * 1000, // 30s,
minTimeout: minTimeout ?? 500, // .5s
timeoutFactor: timeoutFactor ?? 2,
maxRetries: maxRetries ?? 5,
// What errors we should retry
methods: methods ?? ['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', 'QUERY'],
// Indicates which errors to retry
statusCodes: statusCodes ?? [500, 502, 503, 504, 429],
// List of errors to retry
errorCodes: errorCodes ?? [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE',
'UND_ERR_SOCKET'
]
}
this.retryCount = 0
this.retryCountCheckpoint = 0
this.headersSent = false
this.start = 0
this.end = null
this.etag = null
this.statusCode = null
this.headers = null
this.controllerProxy = new RetryController(reason => this.#onAbort(reason))
// A retry decision is in flight (the policy may be holding a backoff
// timer). While pending, a consumer abort cancels the wait.
this.retryPending = false
// Backoff timer returned by the retry policy, so #onAbort can cancel it.
// Null for custom policies that do not return their timer.
this.retryTimer = null
// A response can complete while its controller is paused if the peer closes
// the connection. Hold its body until the retry policy decides whether to
// discard it for a retry or forward it as the final response.
this.pendingResponseData = null
this.pendingResponseTrailers = null
this.pendingResponseEnded = false
// Set once an abort during the backoff delivered the terminal error
// downstream; late policy callbacks and connection errors are then moot.
this.aborted = false
}
onResponseStartWithRetry (controller, statusCode, headers, statusMessage, err) {
if (this.retryOpts.throwOnError) {
// Preserve old behavior for status codes that are not eligible for retry
if (this.retryOpts.statusCodes.includes(statusCode) === false) {
if (this.headersSent) {
// The downstream handler already received the response from an
// earlier attempt. Forwarding this response would replace the
// downstream body and leave the original body pending forever.
this.handler.onResponseError?.(this.controllerProxy, err)
} else {
this.headersSent = true
this.checkpointResponseEnd(headers)
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
}
} else {
this.error = err
}
return
}
if (isDisturbed(this.opts.body)) {
this.headersSent = true
this.checkpointResponseEnd(headers)
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
return
}
function shouldRetry (passedErr) {
if (this.aborted) {
// Aborted while the policy was deciding; the decision is moot.
return
}
this.retryPending = false
this.retryTimer = null
const pendingData = this.pendingResponseData
const pendingTrailers = this.pendingResponseTrailers
const pendingEnd = this.pendingResponseEnded
this.pendingResponseData = null
this.pendingResponseTrailers = null
this.pendingResponseEnded = false
if (passedErr) {
if (this.headersSent) {
// The downstream handler already received the response from an
// earlier attempt. Forwarding this response would replace the
// downstream body and leave the original body pending forever.
this.handler.onResponseError?.(this.controllerProxy, passedErr)
} else {
this.headersSent = true
this.checkpointResponseEnd(headers)
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
controller.resume()
if (pendingEnd) {
for (const chunk of pendingData) {
this.onResponseData(controller, chunk)
}
this.onResponseEnd(controller, pendingTrailers)
}
return
}
controller.resume()
return
}
this.error = err
controller.resume()
if (pendingEnd) {
this.onResponseEnd(controller, pendingTrailers)
}
}
// The pause()/resume() pair (here and in shouldRetry) acts on THIS
// connection's controller -- never the downstream proxy. We hold this exact
// connection while the retry policy decides (possibly after a timeout) and
// must resume the same one. Routing this through controllerProxy would risk
// resuming a different connection if a later dispatch re-points the proxy in
// between, leaving this one paused forever -- the very stall the proxy exists
// to prevent.
controller.pause()
// The default policy returns its backoff timer so an abort can cancel it;
// a custom policy may return anything (or nothing), which is ignored.
this.retryPending = true
this.pendingResponseData = []
this.pendingResponseTrailers = null
this.pendingResponseEnded = false
this.retryTimer = this.retryOpts.retry(
err,
{
state: { counter: this.retryCount },
opts: { retryOptions: this.retryOpts, ...this.opts }
},
shouldRetry.bind(this)
) ?? null
}
checkpointResponseEnd (headers) {
if (this.end == null && this.opts.method !== 'HEAD') {
const contentLength = headers['content-length']
this.end = contentLength != null ? Number(contentLength) - 1 : null
assert(
this.end == null || Number.isFinite(this.end),
'invalid content-length'
)
this.resume = this.end != null
}
}
onRequestStart (controller, context) {
// request.js creates a fresh RequestController per dispatch and passes that
// same instance to every later callback of the dispatch. onRequestStart is
// the first callback (it is where the controller is created), so re-pointing
// the proxy here is enough to keep it on the active connection across every
// transparent retry/resume.
this.controllerProxy.target = controller
if (!this.headersSent) {
this.handler.onRequestStart?.(this.controllerProxy, context)
}
}
onBodySent (chunk) {
this.handler.onBodySent?.(chunk)
}
onRequestSent () {
this.handler.onRequestSent?.()
}
onRequestUpgrade (_controller, statusCode, headers, socket) {
this.handler.onRequestUpgrade?.(this.controllerProxy, statusCode, headers, socket)
}
static [kRetryHandlerDefaultRetry] (err, { state, opts }, cb) {
const { statusCode, code, headers } = err
const { method, retryOptions } = opts
const {
maxRetries,
minTimeout,
maxTimeout,
timeoutFactor,
statusCodes,
errorCodes,
methods,
retryAfter
} = retryOptions
const { counter } = state
// Any code that is not a Undici's originated and allowed to retry
if (code && code !== 'UND_ERR_REQ_RETRY' && !errorCodes.includes(code)) {
cb(err)
return
}
// If a set of method are provided and the current method is not in the list
if (Array.isArray(methods) && !methods.includes(method)) {
cb(err)
return
}
// If a set of status code are provided and the current status code is not in the list
if (
statusCode != null &&
Array.isArray(statusCodes) &&
!statusCodes.includes(statusCode)
) {
cb(err)
return
}
// If we reached the max number of retries
if (counter > maxRetries) {
cb(err)
return
}
let retryAfterHeader = retryAfter === false ? undefined : headers?.['retry-after']
if (retryAfterHeader) {
retryAfterHeader = Number(retryAfterHeader)
retryAfterHeader = Number.isNaN(retryAfterHeader)
? calculateRetryAfterHeader(headers['retry-after'])
: retryAfterHeader * 1e3 // Retry-After is in seconds
}
const retryTimeout =
retryAfterHeader === 0
? 0
: retryAfterHeader > 0
? Math.min(retryAfterHeader, maxTimeout)
: Math.min(minTimeout * timeoutFactor ** (counter - 1), maxTimeout)
// Return the backoff timer so the handler can cancel it when the
// consumer aborts while the retry decision is pending.
return setTimeout(() => cb(null), retryTimeout)
}
onResponseStart (controller, statusCode, headers, statusMessage) {
if (statusCode < 200) {
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
return
}
this.error = null
this.retryCount += 1
this.statusCode = statusCode
this.headers = headers
// Checkpoint for resume from where we left it
if (this.headersSent) {
// Only Partial Content 206 supposed to provide Content-Range,
// any other status code that partially consumed the payload
// should not be retried because it would result in downstream
// wrongly concatenate multiple responses.
if (statusCode !== 206 && (this.start > 0 || statusCode !== 200)) {
throw new RequestRetryError('server does not support the range header and the payload was partially consumed', statusCode, {
headers,
data: { count: this.retryCount }
})
}
const contentRange = parseRangeHeader(headers['content-range'])
// If no content range
if (!contentRange) {
// We always throw here as we want to indicate that we entred unexpected path
throw new RequestRetryError('Content-Range mismatch', statusCode, {
headers,
data: { count: this.retryCount }
})
}
// Let's start with a weak etag check
if (this.etag != null && this.etag !== headers.etag) {
// We always throw here as we want to indicate that we entred unexpected path
throw new RequestRetryError('ETag mismatch', statusCode, {
headers,
data: { count: this.retryCount }
})
}
validatePartialResponseContentLength(headers, contentRange, statusCode, this.retryCount)
const { start, size, end = size ? size - 1 : null } = contentRange
if (this.start !== start || (this.end != null && this.end !== end)) {
throw new RequestRetryError('Content-Range mismatch', statusCode, {
headers,
data: { count: this.retryCount }
})
}
return
}
if (statusCode >= 300) {
const err = new RequestRetryError('Request failed', statusCode, {
headers,
data: {
count: this.retryCount
}
})
this.onResponseStartWithRetry(controller, statusCode, headers, statusMessage, err)
return
}
if (this.end == null) {
if (statusCode === 206) {
// First time we receive 206
const range = parseRangeHeader(headers['content-range'])
if (range == null || range.end == null) {
this.headersSent = true
this.handler.onResponseStart?.(
this.controllerProxy,
statusCode,
headers,
statusMessage
)
return
}
validatePartialResponseContentLength(headers, range, statusCode, this.retryCount)
const { start, size, end = size ? size - 1 : null } = range
assert(
start != null && Number.isFinite(start),
'content-range mismatch'
)
assert(end != null && Number.isFinite(end), 'invalid content-length')
this.start = start
this.end = end
}
// We make our best to checkpoint the body for further range headers
if (this.end == null && this.opts.method !== 'HEAD') {
const contentLength = headers['content-length']
this.end = contentLength != null ? Number(contentLength) - 1 : null
}
assert(Number.isFinite(this.start))
assert(
this.end == null || Number.isFinite(this.end),
'invalid content-length'
)
this.resume = true
this.etag = headers.etag != null ? headers.etag : null
// Weak etags are not useful for comparison nor cache
// for instance not safe to assume if the response is byte-per-byte
// equal
if (
this.etag != null &&
this.etag[0] === 'W' &&
this.etag[1] === '/'
) {
this.etag = null
}
this.headersSent = true
this.handler.onResponseStart?.(
this.controllerProxy,
statusCode,
headers,
statusMessage
)
} else {
throw new RequestRetryError('Request failed', statusCode, {
headers,
data: { count: this.retryCount }
})
}
}
onResponseData (_controller, chunk) {
if (this.pendingResponseData !== null) {
this.pendingResponseData.push(chunk)
return
}
if (this.error) {
return
}
this.start += chunk.length
this.handler.onResponseData?.(this.controllerProxy, chunk)
}
onResponseEnd (_controller, trailers) {
if (this.pendingResponseData !== null) {
this.pendingResponseTrailers = trailers
this.pendingResponseEnded = true
return
}
if (this.error && this.retryOpts.throwOnError) {
throw this.error
}
if (!this.error) {
// Verify that the received body length matches the expected range
// when we have a finite end position (from Content-Length or Content-Range)
if (this.end != null && Number.isFinite(this.end)) {
if (this.start !== this.end + 1) {
throw new RequestRetryError('Content-Range mismatch', this.statusCode, {
headers: this.headers,
data: { count: this.retryCount }
})
}
}
this.retryCount = 0
return this.handler.onResponseEnd?.(this.controllerProxy, trailers)
}
this.retry()
}
retry () {
if (this.start !== 0) {
this.opts = {
...this.opts,
headers: buildResumeHeaders(this.opts.headers, `bytes=${this.start}-${this.end ?? ''}`, this.etag)
}
}
if (!this.#tracksQueuedRetry) {
// The consumer's controller still targets the earlier attempt, so an
// abort would not reach a retry that waits for a connection. Keep the
// function that takes it out of its queue; the caller's hook still runs.
this.#tracksQueuedRetry = true
const onQueued = this.opts[kQueuedAbort]
this.opts[kQueuedAbort] = (cancel) => {
this.#queuedCancel = cancel
onQueued?.(cancel)
}
}
try {
this.retryCountCheckpoint = this.retryCount
this.dispatch(this.opts, this)
} catch (err) {
this.handler.onResponseError?.(this.controllerProxy, err)
}
}
onResponseError (controller, err) {
if (this.aborted) {
// #onAbort already delivered the terminal error downstream; the late
// error of the torn-down connection must not be forwarded twice.
return
}
// controller is THIS failed connection (not the proxy): we inspect whether
// the consumer aborted it to decide retry-vs-propagate.
if (controller?.aborted || isDisturbed(this.opts.body) || (this.headersSent && !this.resume)) {
this.handler.onResponseError?.(this.controllerProxy, err)
return
}
function shouldRetry (returnedErr) {
if (this.aborted) {
// Aborted while the policy was deciding; the decision is moot.
return
}
this.retryPending = false
this.retryTimer = null
if (!returnedErr) {
this.retry()
return
}
this.handler?.onResponseError?.(this.controllerProxy, returnedErr)
}
// We reconcile in case of a mix between network errors
// and server error response
if (this.retryCount - this.retryCountCheckpoint > 0) {
// We count the difference between the last checkpoint and the current retry count
this.retryCount =
this.retryCountCheckpoint +
(this.retryCount - this.retryCountCheckpoint)
} else {
this.retryCount += 1
}
this.retryPending = true
this.retryTimer = this.retryOpts.retry(
err,
{
state: { counter: this.retryCount },
opts: { retryOptions: this.retryOpts, ...this.opts }
},
shouldRetry.bind(this)
) ?? null
}
#onAbort (reason) {
// A retry that waits for a connection is taken out of its queue: its
// dispatcher fails it with an aborted controller, so onResponseError passes
// the abort on instead of retrying. Once the retry started, this does nothing.
const cancel = this.#queuedCancel
if (cancel !== null) {
this.#queuedCancel = null
cancel(reason ?? new RequestAbortedError())
}
// A consumer abort lands on the controller proxy. If the retry policy is
// still deciding (typically holding a backoff timer), cancel the wait and
// surface the abort immediately instead of letting the request hang until
// the backoff elapses.
if (!this.retryPending) {
return
}
this.aborted = true
this.retryPending = false
clearTimeout(this.retryTimer)
this.retryTimer = null
this.pendingResponseData = null
this.pendingResponseTrailers = null
this.pendingResponseEnded = false
this.handler.onResponseError?.(this.controllerProxy, reason ?? new RequestAbortedError())
}
}
function isReplacedHeader (key, etag) {
const name = key.toLowerCase()
return name === 'range' || (etag != null && name === 'if-match')
}
function buildResumeHeaders (headers, range, etag) {
if (Array.isArray(headers) || (headers && typeof headers === 'object' && hasSafeIterator(headers))) {
const ret = []
if (Array.isArray(headers)) {
for (let i = 0; i < headers.length; i += 2) {
if (!isReplacedHeader(headers[i], etag)) {
ret.push(headers[i], headers[i + 1])
}
}
} else {
for (const [key, value] of headers) {
if (!isReplacedHeader(key, etag)) {
ret.push(key, value)
}
}
}
ret.push('range', range)
// Weak etag check - weak etags will make comparison algorithms never match
if (etag != null) {
ret.push('if-match', etag)
}
return ret
}
const ret = {}
for (const key in headers) {
if (Object.hasOwn(headers, key) && !isReplacedHeader(key, etag)) {
ret[key] = headers[key]
}
}
ret.range = range
if (etag != null) {
ret['if-match'] = etag
}
return ret
}
module.exports = RetryHandler