@@ -5,7 +5,6 @@ const { kBodyUsed } = require('../core/symbols')
55const assert = require ( 'node:assert' )
66const { InvalidArgumentError } = require ( '../core/errors' )
77const EE = require ( 'node:events' )
8- const Dispatcher = require ( '../dispatcher' )
98
109const redirectableStatusCodes = [ 300 , 301 , 302 , 303 , 307 , 308 ]
1110
@@ -25,14 +24,27 @@ class BodyAsyncIterable {
2524}
2625
2726class RedirectHandler {
28- constructor ( dispatcher , dispatcherOpts , redirectOpts , handler ) {
29- const { maxRedirections } = redirectOpts ?? { }
27+ static buildDispatch ( dispatcher , maxRedirections ) {
28+ if ( maxRedirections != null && ( ! Number . isInteger ( maxRedirections ) || maxRedirections < 0 ) ) {
29+ throw new InvalidArgumentError ( 'maxRedirections must be a positive number' )
30+ }
31+
32+ const dispatch = dispatcher . dispatch . bind ( dispatcher )
33+ return ( opts , originalHandler ) => dispatch ( opts , new RedirectHandler ( dispatch , maxRedirections , opts , originalHandler ) )
34+ }
35+
36+ constructor ( dispatch , maxRedirections , opts , handler ) {
37+ if ( maxRedirections != null && ( ! Number . isInteger ( maxRedirections ) || maxRedirections < 0 ) ) {
38+ throw new InvalidArgumentError ( 'maxRedirections must be a positive number' )
39+ }
40+
41+ util . validateHandler ( handler , opts . method , opts . upgrade )
3042
31- this . dispatcher = dispatcher
43+ this . dispatch = dispatch
3244 this . location = null
3345 this . abort = null
34- this . opts = { ...dispatcherOpts } // opts must be a copy
35- this . maxRedirections = maxRedirections ?? 0
46+ this . opts = { ...opts , maxRedirections : 0 } // opts must be a copy
47+ this . maxRedirections = maxRedirections
3648 this . handler = handler
3749 this . history = [ ]
3850 this . redirectionLimitReached = false
@@ -165,7 +177,7 @@ class RedirectHandler {
165177 this . location = null
166178 this . abort = null
167179
168- this . dispatcher . dispatch ( this . opts , this )
180+ this . dispatch ( this . opts , this )
169181 } else {
170182 this . handler . onComplete ( trailers )
171183 }
@@ -220,38 +232,4 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
220232 return ret
221233}
222234
223- class RedirectDispatcher extends Dispatcher {
224- #opts
225- #dispatcher
226-
227- constructor ( dispatcher , opts ) {
228- super ( )
229-
230- this . #dispatcher = dispatcher
231- this . #opts = opts
232- }
233-
234- dispatch ( opts , handler ) {
235- return this . #dispatcher. dispatch ( opts , new RedirectHandler ( this . #dispatcher, opts , this . #opts, handler ) )
236- }
237-
238- close ( ...args ) {
239- return this . #dispatcher. close ( ...args )
240- }
241-
242- destroy ( ...args ) {
243- return this . #dispatcher. destroy ( ...args )
244- }
245- }
246-
247- module . exports = ( opts ) => {
248- if ( opts ?. maxRedirections == null || opts ?. maxRedirections === 0 ) {
249- return null
250- }
251-
252- if ( ! Number . isInteger ( opts . maxRedirections ) || opts . maxRedirections < 0 ) {
253- throw new InvalidArgumentError ( 'maxRedirections must be a positive number' )
254- }
255-
256- return ( dispatcher ) => new RedirectDispatcher ( dispatcher , opts )
257- }
235+ module . exports = RedirectHandler
0 commit comments