@@ -227,26 +227,50 @@ async function stream2(
227227 } ) ;
228228
229229 const customFetch = options . fetch || fetch ;
230-
231230 const fetchOptions = options . fetchOptions ?? { } as FetchOptions ;
232231
233-
234- if ( options . forward ) {
235- const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req , "forward" ) ;
236-
232+ const prepareRequest = ( outgoing : common . Outgoing ) => {
237233 const requestOptions : RequestInit = {
238- method : outgoingOptions . method ,
234+ method : outgoing . method ,
235+ ...fetchOptions . requestOptions ,
239236 } ;
240237
238+ const headers = new Headers ( fetchOptions . requestOptions ?. headers ) ;
239+
240+ if ( ! fetchOptions . requestOptions ?. headers && outgoing . headers ) {
241+ for ( const [ key , value ] of Object . entries ( outgoing . headers ) ) {
242+ if ( typeof key === "string" ) {
243+ if ( Array . isArray ( value ) ) {
244+ for ( const v of value ) {
245+ headers . append ( key , v as string ) ;
246+ }
247+ } else if ( value != null ) {
248+ headers . append ( key , value as string ) ;
249+ }
250+ }
251+ }
252+ }
253+
254+ if ( options . auth ) {
255+ headers . set ( "authorization" , `Basic ${ Buffer . from ( options . auth ) . toString ( "base64" ) } ` ) ;
256+ }
257+
258+ requestOptions . headers = headers ;
241259
242- // Handle request body
243260 if ( options . buffer ) {
244261 requestOptions . body = options . buffer as Stream . Readable ;
245262 } else if ( req . method !== "GET" && req . method !== "HEAD" ) {
246263 requestOptions . body = req ;
247264 requestOptions . duplex = "half" ;
248265 }
249266
267+ return requestOptions ;
268+ } ;
269+
270+ if ( options . forward ) {
271+ const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req , "forward" ) ;
272+ const requestOptions = prepareRequest ( outgoingOptions ) ;
273+
250274 // Call onBeforeRequest callback before making the forward request
251275 if ( fetchOptions . onBeforeRequest ) {
252276 try {
@@ -279,31 +303,7 @@ async function stream2(
279303 }
280304
281305 const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req ) ;
282-
283- // Remove symbols from headers
284- const requestOptions : RequestInit = {
285- method : outgoingOptions . method ,
286- headers : Object . fromEntries (
287- Object . entries ( outgoingOptions . headers || { } ) . filter ( ( [ key , _value ] ) => {
288- return typeof key === "string" ;
289- } ) ,
290- ) as RequestInit [ "headers" ] ,
291- ...fetchOptions . requestOptions ,
292- } ;
293-
294- if ( options . auth ) {
295- requestOptions . headers = {
296- ...requestOptions . headers ,
297- authorization : `Basic ${ Buffer . from ( options . auth ) . toString ( "base64" ) } ` ,
298- } ;
299- }
300-
301- if ( options . buffer ) {
302- requestOptions . body = options . buffer as Stream . Readable ;
303- } else if ( req . method !== "GET" && req . method !== "HEAD" ) {
304- requestOptions . body = req ;
305- requestOptions . duplex = "half" ;
306- }
306+ const requestOptions = prepareRequest ( outgoingOptions ) ;
307307
308308 // Call onBeforeRequest callback before making the request
309309 if ( fetchOptions . onBeforeRequest ) {
0 commit comments