@@ -22,8 +22,9 @@ import {
22
22
request as undiciRequest ,
23
23
Dispatcher ,
24
24
Agent ,
25
- getGlobalDispatcher ,
26
25
Pool ,
26
+ getGlobalDispatcher ,
27
+ MockAgent ,
27
28
} from 'undici' ;
28
29
import undiciSymbols from 'undici/lib/core/symbols.js' ;
29
30
import { FormData as FormDataNode } from 'formdata-node' ;
@@ -207,10 +208,23 @@ export class HttpClient extends EventEmitter {
207
208
}
208
209
209
210
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
+ if ( ! ( globalDispatcher instanceof Agent ) && ! ( globalDispatcher instanceof MockAgent ) ) {
220
+ const dispatcher = globalDispatcher . constructor . name === 'MockAgent' ? new MockAgent ( ) : new Agent ( ) ;
221
+ this . setDispatcher ( dispatcher ) ;
222
+ return dispatcher ;
223
+ }
224
+ return globalDispatcher ;
211
225
}
212
226
213
- setDispatcher ( dispatcher : Dispatcher ) {
227
+ setDispatcher ( dispatcher ? : Dispatcher ) {
214
228
this . #dispatcher = dispatcher ;
215
229
}
216
230
@@ -415,7 +429,7 @@ export class HttpClient extends EventEmitter {
415
429
headers,
416
430
bodyTimeout,
417
431
opaque : internalOpaque ,
418
- dispatcher : args . dispatcher ?? this . #dispatcher ,
432
+ dispatcher : args . dispatcher ?? this . getDispatcher ( ) ,
419
433
signal : args . signal ,
420
434
} ;
421
435
if ( typeof args . highWaterMark === 'number' ) {
0 commit comments