@@ -38,7 +38,13 @@ const { getDefaultHighWaterMark } = require('internal/streams/state');
3838const assert = require ( 'internal/assert' ) ;
3939const EE = require ( 'events' ) ;
4040const Stream = require ( 'stream' ) ;
41- const { kOutHeaders, utcDate, kNeedDrain } = require ( 'internal/http' ) ;
41+ const {
42+ kOnOutgoingBodyChunkSent,
43+ kOnOutgoingBodySent,
44+ kOutHeaders,
45+ utcDate,
46+ kNeedDrain,
47+ } = require ( 'internal/http' ) ;
4248const { Buffer } = require ( 'buffer' ) ;
4349const {
4450 _checkIsHttpToken : checkIsHttpToken ,
@@ -87,6 +93,7 @@ const kBytesWritten = Symbol('kBytesWritten');
8793const kErrored = Symbol ( 'errored' ) ;
8894const kHighWaterMark = Symbol ( 'kHighWaterMark' ) ;
8995const kRejectNonStandardBodyWrites = Symbol ( 'kRejectNonStandardBodyWrites' ) ;
96+ const kHasOutgoingBodyChunks = Symbol ( 'kHasOutgoingBodyChunks' ) ;
9097
9198const nop = ( ) => { } ;
9299
@@ -155,6 +162,7 @@ function OutgoingMessage(options) {
155162 this [ kErrored ] = null ;
156163 this [ kHighWaterMark ] = options ?. highWaterMark ?? getDefaultHighWaterMark ( ) ;
157164 this [ kRejectNonStandardBodyWrites ] = options ?. rejectNonStandardBodyWrites ?? false ;
165+ this [ kHasOutgoingBodyChunks ] = false ;
158166}
159167ObjectSetPrototypeOf ( OutgoingMessage . prototype , Stream . prototype ) ;
160168ObjectSetPrototypeOf ( OutgoingMessage , Stream ) ;
@@ -1002,6 +1010,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
10021010 process . nextTick ( connectionCorkNT , msg . socket ) ;
10031011 }
10041012
1013+ if ( chunk . length !== 0 ) {
1014+ msg [ kHasOutgoingBodyChunks ] = true ;
1015+ msg [ kOnOutgoingBodyChunkSent ] ?. ( chunk , encoding ) ;
1016+ }
1017+
10051018 let ret ;
10061019 if ( msg . chunkedEncoding && chunk . length !== 0 ) {
10071020 len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
@@ -1151,6 +1164,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
11511164
11521165 this . finished = true ;
11531166
1167+ if ( this [ kHasOutgoingBodyChunks ] ) {
1168+ this [ kOnOutgoingBodySent ] ?. ( ) ;
1169+ }
1170+
11541171 // There is the first message on the outgoing queue, and we've sent
11551172 // everything to the socket.
11561173 debug ( 'outgoing message end.' ) ;
0 commit comments