@@ -51,41 +51,43 @@ export async function writeResponseToNodeResponse(
5151 return ;
5252 }
5353
54- try {
55- const reader = body . getReader ( ) ;
56- function cancelStream ( error ?: Error ) {
57- reader . cancel ( error ) . catch ( ( ) => {
58- // eslint-disable-next-line no-console
59- console . error (
60- `An error occurred while writing the response body for: ${ destination . req . url } .` ,
61- error ,
62- ) ;
63- } ) ;
64- if ( error ) {
65- destination . destroy ( error ) ;
66- }
54+ const reader = body . getReader ( ) ;
55+ function cancelStream ( error ?: Error ) {
56+ reader . cancel ( error ) . catch ( ( ) => {
57+ // eslint-disable-next-line no-console
58+ console . error (
59+ `An error occurred while writing the response body for: ${ destination . req . url } .` ,
60+ error ,
61+ ) ;
62+ } ) ;
63+ if ( error ) {
64+ destination . destroy ( error ) ;
6765 }
66+ }
6867
69- function handleStream ( {
70- done,
71- value,
72- } : ReadableStreamReadResult < Uint8Array > ) : void | Promise < void > {
73- try {
74- if ( done ) {
75- // End the response
76- destination . end ( ) ;
77- } else if ( ( destination as ServerResponse ) . write ( value ) ) {
78- // Continue reading recursively
68+ function handleStream ( {
69+ done,
70+ value,
71+ } : ReadableStreamReadResult < Uint8Array > ) : void | Promise < void > {
72+ try {
73+ if ( done ) {
74+ // End the response
75+ destination . end ( ) ;
76+ } else if ( ( destination as ServerResponse ) . write ( value ) ) {
77+ // Continue reading recursively
78+ reader . read ( ) . then ( handleStream , cancelStream ) ;
79+ } else {
80+ // Wait for the drain event to continue reading
81+ destination . once ( 'drain' , ( ) => {
7982 reader . read ( ) . then ( handleStream , cancelStream ) ;
80- } else {
81- // Wait for the drain event to continue reading
82- destination . once ( 'drain' , ( ) => reader . read ( ) . then ( handleStream , cancelStream ) ) ;
83- }
84- } catch ( error ) {
85- cancelStream ( error instanceof Error ? error : undefined ) ;
83+ } ) ;
8684 }
85+ } catch ( error ) {
86+ cancelStream ( error instanceof Error ? error : undefined ) ;
8787 }
88+ }
8889
90+ try {
8991 destination . on ( 'close' , cancelStream ) ;
9092 destination . on ( 'error' , cancelStream ) ;
9193 reader . read ( ) . then ( handleStream , cancelStream ) ;
0 commit comments