Skip to content

Commit

Permalink
refactor: port redirect handler to new hooks (#3879)
Browse files Browse the repository at this point in the history
* refactor: port redirect handler to new hooks

* fixup
  • Loading branch information
ronag authored Nov 25, 2024
1 parent 71b6b0b commit 303b2ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 42 deletions.
4 changes: 1 addition & 3 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/handler/decorator-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const assert = require('node:assert')

/**
* @deprecated
*/
module.exports = class DecoratorHandler {
#handler
#onCompleteCalled = false
Expand Down
53 changes: 15 additions & 38 deletions lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
}
Expand All @@ -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)))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/handler/unwrap-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = class UnwrapHandler {
}

onError (err) {
if (!this.#handler.onError) {
if (!this.#handler.onResponseError) {
throw new InvalidArgumentError('invalid onError method')
}

Expand Down

0 comments on commit 303b2ca

Please sign in to comment.