@@ -7,10 +7,7 @@ The names of passes are exported as WEB_PASSES from this module.
77
88*/
99
10- import type {
11- IncomingMessage as Request ,
12- ServerResponse as Response ,
13- } from "node:http" ;
10+ import type { IncomingMessage as Request , ServerResponse as Response } from "node:http" ;
1411import * as http from "node:http" ;
1512import * as https from "node:https" ;
1613import type { Socket } from "node:net" ;
@@ -41,10 +38,7 @@ const nativeAgents = { http, https };
4138
4239// Sets `content-length` to '0' if request is of DELETE type.
4340export function deleteLength ( req : Request ) {
44- if (
45- ( req . method === "DELETE" || req . method === "OPTIONS" ) &&
46- ! req . headers [ "content-length" ]
47- ) {
41+ if ( ( req . method === "DELETE" || req . method === "OPTIONS" ) && ! req . headers [ "content-length" ] ) {
4842 req . headers [ "content-length" ] = "0" ;
4943 delete req . headers [ "transfer-encoding" ] ;
5044 }
@@ -72,13 +66,10 @@ export function XHeaders(req: Request, _res: Response, options: ServerOptions) {
7266
7367 for ( const header of [ "for" , "port" , "proto" ] as const ) {
7468 req . headers [ "x-forwarded-" + header ] =
75- ( req . headers [ "x-forwarded-" + header ] || "" ) +
76- ( req . headers [ "x-forwarded-" + header ] ? "," : "" ) +
77- values [ header ] ;
69+ ( req . headers [ "x-forwarded-" + header ] || "" ) + ( req . headers [ "x-forwarded-" + header ] ? "," : "" ) + values [ header ] ;
7870 }
7971
80- req . headers [ "x-forwarded-host" ] =
81- req . headers [ "x-forwarded-host" ] || req . headers [ "host" ] || "" ;
72+ req . headers [ "x-forwarded-host" ] = req . headers [ "x-forwarded-host" ] || req . headers [ "host" ] || "" ;
8273}
8374
8475// Does the actual proxying. If `forward` is enabled fires up
@@ -106,12 +97,7 @@ export function stream(
10697 if ( options . forward ) {
10798 // forward enabled, so just pipe the request
10899 const proto = options . forward . protocol === "https:" ? https : http ;
109- const outgoingOptions = common . setupOutgoing (
110- options . ssl || { } ,
111- options ,
112- req ,
113- "forward" ,
114- ) ;
100+ const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req , "forward" ) ;
115101 const forwardReq = proto . request ( outgoingOptions ) ;
116102
117103 // error handler (e.g. ECONNRESET, ECONNREFUSED)
@@ -162,15 +148,9 @@ export function stream(
162148 req . on ( "error" , proxyError ) ;
163149 proxyReq . on ( "error" , proxyError ) ;
164150
165- function createErrorHandler (
166- proxyReq : http . ClientRequest ,
167- url : NormalizeProxyTarget < ProxyTargetUrl > ,
168- ) {
151+ function createErrorHandler ( proxyReq : http . ClientRequest , url : NormalizeProxyTarget < ProxyTargetUrl > ) {
169152 return ( err : Error ) => {
170- if (
171- req . socket . destroyed &&
172- ( err as NodeJS . ErrnoException ) . code === "ECONNRESET"
173- ) {
153+ if ( req . socket . destroyed && ( err as NodeJS . ErrnoException ) . code === "ECONNRESET" ) {
174154 server . emit ( "econnreset" , err , req , res , url ) ;
175155 proxyReq . destroy ( ) ;
176156 return ;
@@ -236,10 +216,7 @@ async function stream2(
236216 } ;
237217
238218 req . on ( "error" , ( err : Error ) => {
239- if (
240- req . socket . destroyed &&
241- ( err as NodeJS . ErrnoException ) . code === "ECONNRESET"
242- ) {
219+ if ( req . socket . destroyed && ( err as NodeJS . ErrnoException ) . code === "ECONNRESET" ) {
243220 const target = options . target || options . forward ;
244221 if ( target ) {
245222 server . emit ( "econnreset" , err , req , res , target ) ;
@@ -249,19 +226,13 @@ async function stream2(
249226 handleError ( err ) ;
250227 } ) ;
251228
252- const fetchOptions =
253- options . fetch === true ? ( { } as FetchOptions ) : options . fetch ;
229+ const fetchOptions = options . fetch === true ? ( { } as FetchOptions ) : options . fetch ;
254230 if ( ! fetchOptions ) {
255231 throw new Error ( "stream2 called without fetch options" ) ;
256232 }
257233
258234 if ( options . forward ) {
259- const outgoingOptions = common . setupOutgoing (
260- options . ssl || { } ,
261- options ,
262- req ,
263- "forward" ,
264- ) ;
235+ const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req , "forward" ) ;
265236
266237 const requestOptions : RequestInit = {
267238 method : outgoingOptions . method ,
@@ -290,10 +261,7 @@ async function stream2(
290261 }
291262
292263 try {
293- const result = await fetch (
294- new URL ( outgoingOptions . url ) . origin + outgoingOptions . path ,
295- requestOptions ,
296- ) ;
264+ const result = await fetch ( new URL ( outgoingOptions . url ) . origin + outgoingOptions . path , requestOptions ) ;
297265
298266 // Call onAfterResponse callback for forward requests (though they typically don't expect responses)
299267 if ( fetchOptions . onAfterResponse ) {
@@ -341,6 +309,7 @@ async function stream2(
341309 requestOptions . body = options . buffer as Stream . Readable ;
342310 } else if ( req . method !== "GET" && req . method !== "HEAD" ) {
343311 requestOptions . body = req ;
312+ requestOptions . duplex = "half" ;
344313 }
345314
346315 // Call onBeforeRequest callback before making the request
@@ -354,10 +323,7 @@ async function stream2(
354323 }
355324
356325 try {
357- const response = await fetch (
358- new URL ( outgoingOptions . url ) . origin + outgoingOptions . path ,
359- requestOptions ,
360- ) ;
326+ const response = await fetch ( new URL ( outgoingOptions . url ) . origin + outgoingOptions . path , requestOptions ) ;
361327
362328 // Call onAfterResponse callback after receiving the response
363329 if ( fetchOptions . onAfterResponse ) {
@@ -402,9 +368,7 @@ async function stream2(
402368
403369 if ( ! res . writableEnded ) {
404370 // Allow us to listen for when the proxy has completed
405- const nodeStream = response . body
406- ? Readable . from ( response . body as AsyncIterable < Uint8Array > )
407- : null ;
371+ const nodeStream = response . body ? Readable . from ( response . body as AsyncIterable < Uint8Array > ) : null ;
408372
409373 if ( nodeStream ) {
410374 nodeStream . on ( "error" , ( err ) => {
@@ -428,9 +392,7 @@ async function stream2(
428392 server ?. emit ( "end" , req , res , fakeProxyRes ) ;
429393 }
430394 } catch ( err ) {
431- if ( err ) {
432- handleError ( err as Error , options . target ) ;
433- }
395+ handleError ( err as Error , options . target ) ;
434396 }
435397}
436398
0 commit comments