From 303b2ca6b20292d4c9b54d241a4704c3bdce82d4 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 25 Nov 2024 21:03:03 +0100 Subject: [PATCH] refactor: port redirect handler to new hooks (#3879) * refactor: port redirect handler to new hooks * fixup --- lib/handler/cache-handler.js | 4 +-- lib/handler/decorator-handler.js | 3 ++ lib/handler/redirect-handler.js | 53 +++++++++----------------------- lib/handler/unwrap-handler.js | 2 +- 4 files changed, 20 insertions(+), 42 deletions(-) diff --git a/lib/handler/cache-handler.js b/lib/handler/cache-handler.js index d0f00330143..b5c8f383445 100644 --- a/lib/handler/cache-handler.js +++ b/lib/handler/cache-handler.js @@ -38,9 +38,7 @@ class CacheHandler { * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey * @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler */ - constructor (opts, cacheKey, handler) { - const { store } = opts - + constructor ({ store }, cacheKey, handler) { this.#store = store this.#cacheKey = cacheKey this.#handler = handler diff --git a/lib/handler/decorator-handler.js b/lib/handler/decorator-handler.js index b0966c2eca9..8a0d6c588a7 100644 --- a/lib/handler/decorator-handler.js +++ b/lib/handler/decorator-handler.js @@ -2,6 +2,9 @@ const assert = require('node:assert') +/** + * @deprecated + */ module.exports = class DecoratorHandler { #handler #onCompleteCalled = false diff --git a/lib/handler/redirect-handler.js b/lib/handler/redirect-handler.js index df2baee19d5..d6f1995ced1 100644 --- a/lib/handler/redirect-handler.js +++ b/lib/handler/redirect-handler.js @@ -42,7 +42,6 @@ class RedirectHandler { this.dispatch = dispatch this.location = null - this.abort = null this.opts = { ...opts, maxRedirections: 0 } // opts must be a copy this.maxRedirections = maxRedirections this.handler = handler @@ -83,20 +82,15 @@ class RedirectHandler { } } - onConnect (abort) { - this.abort = abort - this.handler.onConnect(abort, { history: this.history }) + onRequestStart (controller, context) { + this.handler.onRequestStart?.(controller, { ...context, history: this.history }) } - onUpgrade (statusCode, headers, socket) { - this.handler.onUpgrade(statusCode, headers, socket) + onRequestUpgrade (controller, statusCode, headers, socket) { + this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket) } - onError (error) { - this.handler.onError(error) - } - - onHeaders (statusCode, rawHeaders, resume, statusText) { + onResponseStart (controller, statusCode, statusMessage, headers) { if (this.opts.throwOnMaxRedirect && this.history.length >= this.maxRedirections) { throw new Error('max redirects') } @@ -122,16 +116,17 @@ class RedirectHandler { this.opts.body = null } - this.location = this.history.length >= this.maxRedirections || util.isDisturbed(this.opts.body) + this.location = this.history.length >= this.maxRedirections || util.isDisturbed(this.opts.body) || redirectableStatusCodes.indexOf(statusCode) === -1 ? null - : parseLocation(statusCode, rawHeaders) + : headers.location if (this.opts.origin) { this.history.push(new URL(this.opts.path, this.opts.origin)) } if (!this.location) { - return this.handler.onHeaders(statusCode, rawHeaders, resume, statusText) + this.handler.onResponseStart?.(controller, statusCode, statusMessage, headers) + return } const { origin, pathname, search } = util.parseURL(new URL(this.location, this.opts.origin && new URL(this.opts.path, this.opts.origin))) @@ -147,7 +142,7 @@ class RedirectHandler { this.opts.query = null } - onData (chunk) { + onResponseData (controller, chunk) { if (this.location) { /* https://tools.ietf.org/html/rfc7231#section-6.4 @@ -167,11 +162,11 @@ class RedirectHandler { servers and browsers implementors, we ignore the body as there is no specified way to eventually parse it. */ } else { - return this.handler.onData(chunk) + this.handler.onResponseData?.(controller, chunk) } } - onComplete (trailers) { + onResponseEnd (controller, trailers) { if (this.location) { /* https://tools.ietf.org/html/rfc7231#section-6.4 @@ -181,32 +176,14 @@ class RedirectHandler { See comment on onData method above for more detailed information. */ - - this.location = null - this.abort = null - this.dispatch(this.opts, this) } else { - this.handler.onComplete(trailers) + this.handler.onResponseEnd(controller, trailers) } } - onBodySent (chunk) { - if (this.handler.onBodySent) { - this.handler.onBodySent(chunk) - } - } -} - -function parseLocation (statusCode, rawHeaders) { - if (redirectableStatusCodes.indexOf(statusCode) === -1) { - return null - } - - for (let i = 0; i < rawHeaders.length; i += 2) { - if (rawHeaders[i].length === 8 && util.headerNameToString(rawHeaders[i]) === 'location') { - return rawHeaders[i + 1] - } + onResponseError (controller, error) { + this.handler.onResponseError?.(controller, error) } } diff --git a/lib/handler/unwrap-handler.js b/lib/handler/unwrap-handler.js index 3b38ac264d0..a6b44d99876 100644 --- a/lib/handler/unwrap-handler.js +++ b/lib/handler/unwrap-handler.js @@ -87,7 +87,7 @@ module.exports = class UnwrapHandler { } onError (err) { - if (!this.#handler.onError) { + if (!this.#handler.onResponseError) { throw new InvalidArgumentError('invalid onError method') }