@@ -396,7 +396,7 @@ class Response extends Writable {
396396 // Attempt to stream the chunk using appropriate uWS.Response chunk serving method
397397 // This will depend on whether a total_size is specified or not
398398 let sent , finished
399- // const lastOffset = this.write_offset
399+ const lastOffset = this . write_offset
400400 if ( totalSize ) {
401401 // Attempt to stream the current chunk using uWS.tryEnd with a total size
402402 const [ ok , done ] = this . raw_response . tryEnd ( chunk , totalSize )
@@ -418,12 +418,20 @@ class Response extends Writable {
418418 stream . pause ( )
419419
420420 // Bind a drain handler which will resume the once the backpressure is cleared
421- this . drain ( ( ) => {
422- // Resume the stream if it is paused
423- if ( stream . isPaused ( ) ) stream . resume ( )
424-
425- // Return the write boolean to uWS based on whether the stream is active or not
426- return ! stream . isPaused ( )
421+ this . drain ( ( offset ) => {
422+ if ( lastOffset !== - 1 ) {
423+ const [ ok , done ] = this . raw_response . tryEnd ( chunk . slice ( offset - lastOffset ) , totalSize )
424+ if ( done ) {
425+ if ( ! stream . destroyed ) stream . destroy ( )
426+ } else if ( ok ) {
427+ // Resume the stream if it is paused
428+ if ( stream . isPaused ( ) ) stream . resume ( )
429+ }
430+ /* We always have to return true/false in onWritable.
431+ * If you did not send anything, return true for success. */
432+ return ok
433+ }
434+ return true
427435 } )
428436 }
429437 }
0 commit comments