@@ -64,15 +64,15 @@ async function pipeNdjsonWithAbort(options: NdjsonPipeOptions): Promise<NdjsonPi
6464 let aborted = false ;
6565 let reader : ReadableStreamDefaultReader < Uint8Array > | null = null ;
6666
67- // Handle client disconnect
67+ // Handle client disconnect (only listen on res, not req — req 'close' fires
68+ // prematurely in Node 20+ once the request body is consumed by express.json())
6869 const onClose = ( ) => {
6970 aborted = true ;
7071 controller . abort ( ) ;
7172 if ( reader ) {
7273 void reader . cancel ( ) ;
7374 }
7475 } ;
75- req . on ( 'close' , onClose ) ;
7676 res . on ( 'close' , onClose ) ;
7777
7878 try {
@@ -136,7 +136,6 @@ async function pipeNdjsonWithAbort(options: NdjsonPipeOptions): Promise<NdjsonPi
136136
137137 return { lastEvent, aborted } ;
138138 } finally {
139- req . off ( 'close' , onClose ) ;
140139 res . off ( 'close' , onClose ) ;
141140 if ( reader ) {
142141 void reader . cancel ( ) ;
@@ -173,6 +172,9 @@ async function handleEmbeddedExecute(req: Request, res: Response): Promise<void>
173172
174173 if ( aborted ) {
175174 console . log ( `\x1b[34m[Client → Embedded]\x1b[0m Aborted` ) ;
175+ if ( res . writable ) {
176+ res . end ( ) ;
177+ }
176178 return ;
177179 }
178180
@@ -191,6 +193,9 @@ async function handleEmbeddedExecute(req: Request, res: Response): Promise<void>
191193 } catch ( error ) {
192194 if ( ( error as Error ) . name === 'AbortError' ) {
193195 console . log ( `\x1b[34m[Client → Embedded]\x1b[0m Aborted` ) ;
196+ if ( res . writable ) {
197+ res . end ( ) ;
198+ }
194199 return ;
195200 }
196201 console . error ( `\x1b[31m[Client → Embedded error]\x1b[0m` , error ) ;
@@ -232,6 +237,9 @@ app.post('/api/execute/lambda', async (req: Request, res: Response) => {
232237
233238 if ( aborted ) {
234239 console . log ( `\x1b[34m[Client → Broker → Lambda]\x1b[0m Aborted` ) ;
240+ if ( res . writable ) {
241+ res . end ( ) ;
242+ }
235243 return ;
236244 }
237245
@@ -250,6 +258,9 @@ app.post('/api/execute/lambda', async (req: Request, res: Response) => {
250258 } catch ( error ) {
251259 if ( ( error as Error ) . name === 'AbortError' ) {
252260 console . log ( `\x1b[34m[Client → Broker → Lambda]\x1b[0m Aborted` ) ;
261+ if ( res . writable ) {
262+ res . end ( ) ;
263+ }
253264 return ;
254265 }
255266 console . error ( `\x1b[31m[Client → Broker → Lambda error]\x1b[0m` , error ) ;
0 commit comments