11import { STATUS_CODES } from 'node:http' ;
22import { URLSearchParams } from 'node:url' ;
33import { types } from 'node:util' ;
4- import {
5- type RequestInit ,
6- request ,
7- Headers ,
8- FormData as UndiciFormData ,
9- Agent ,
10- getGlobalDispatcher ,
11- Dispatcher ,
12- } from 'undici' ;
4+ import { type RequestInit , request , Headers , FormData as UndiciFormData , Agent } from 'undici' ;
135import type { HeaderRecord } from 'undici/types/header.js' ;
146import type { ResponseLike } from '../shared.js' ;
157
168export type RequestOptions = Exclude < Parameters < typeof request > [ 1 ] , undefined > ;
179
10+ let localAgent : Agent | null = null ;
11+
1812export async function makeRequest ( url : string , init : RequestInit ) : Promise < ResponseLike > {
1913 // The cast is necessary because `headers` and `method` are narrower types in `undici.request`
2014 // our request path guarantees they are of acceptable type for `undici.request`
@@ -24,9 +18,11 @@ export async function makeRequest(url: string, init: RequestInit): Promise<Respo
2418 } as RequestOptions ;
2519
2620 // Mismatched dispatchers from the Node.js-bundled undici and package-installed undici breaks file uploads.
21+ // So we ensure that we always pass an Agent to request()
2722 // https://github.com/nodejs/node/issues/59012
28- if ( ! options . dispatcher && ! ( getGlobalDispatcher ( ) instanceof Dispatcher ) ) {
29- options . dispatcher = new Agent ( ) ;
23+ if ( ! options . dispatcher ) {
24+ localAgent ??= new Agent ( ) ;
25+ options . dispatcher = localAgent ;
3026 }
3127
3228 const res = await request ( url , options ) ;
0 commit comments