@@ -22,8 +22,9 @@ import {
2222 request as undiciRequest ,
2323 Dispatcher ,
2424 Agent ,
25- getGlobalDispatcher ,
2625 Pool ,
26+ getGlobalDispatcher ,
27+ MockAgent ,
2728} from 'undici' ;
2829import undiciSymbols from 'undici/lib/core/symbols.js' ;
2930import { FormData as FormDataNode } from 'formdata-node' ;
@@ -207,10 +208,27 @@ export class HttpClient extends EventEmitter {
207208 }
208209
209210 getDispatcher ( ) {
210- return this . #dispatcher ?? getGlobalDispatcher ( ) ;
211+ if ( this . #dispatcher) {
212+ return this . #dispatcher;
213+ }
214+ // In a multi-version undici environment
215+ // the global dispatcher is the highest version of undici
216+ // which will conflict with the maxRedirects field and report an error
217+ // so we need to create it that use 5.x version
218+ const globalDispatcher = getGlobalDispatcher ( ) ;
219+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
220+ // @ts -ignore
221+ // eslint-disable-next-line no-proto
222+ const proto = globalDispatcher . __proto__ ;
223+ if ( proto !== Agent . prototype && proto !== MockAgent . prototype ) {
224+ const dispatcher = proto . constructor . name === 'MockAgent' ? new MockAgent ( ) : new Agent ( ) ;
225+ this . setDispatcher ( dispatcher ) ;
226+ return dispatcher ;
227+ }
228+ return globalDispatcher ;
211229 }
212230
213- setDispatcher ( dispatcher : Dispatcher ) {
231+ setDispatcher ( dispatcher ? : Dispatcher ) {
214232 this . #dispatcher = dispatcher ;
215233 }
216234
@@ -415,7 +433,7 @@ export class HttpClient extends EventEmitter {
415433 headers,
416434 bodyTimeout,
417435 opaque : internalOpaque ,
418- dispatcher : args . dispatcher ?? this . #dispatcher ,
436+ dispatcher : args . dispatcher ?? this . getDispatcher ( ) ,
419437 signal : args . signal ,
420438 } ;
421439 if ( typeof args . highWaterMark === 'number' ) {
0 commit comments