@@ -82,6 +82,8 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
8282} ) ;
8383
8484const kCorked = Symbol ( 'corked' ) ;
85+ const kChunkedBuffer = Symbol ( 'kChunkedBuffer' ) ;
86+ const kChunkedLength = Symbol ( 'kChunkedLength' ) ;
8587const kUniqueHeaders = Symbol ( 'kUniqueHeaders' ) ;
8688const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
8789const kErrored = Symbol ( 'errored' ) ;
@@ -140,6 +142,8 @@ function OutgoingMessage(options) {
140142 this . finished = false ;
141143 this . _headerSent = false ;
142144 this [ kCorked ] = 0 ;
145+ this [ kChunkedBuffer ] = [ ] ;
146+ this [ kChunkedLength ] = 0 ;
143147 this . _closed = false ;
144148
145149 this . socket = null ;
@@ -206,8 +210,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
206210ObjectDefineProperty ( OutgoingMessage . prototype , 'writableCorked' , {
207211 __proto__ : null ,
208212 get ( ) {
209- const corked = this . socket ? this . socket . writableCorked : 0 ;
210- return corked + this [ kCorked ] ;
213+ return this [ kCorked ] ;
211214 } ,
212215} ) ;
213216
@@ -299,19 +302,49 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
299302} ;
300303
301304OutgoingMessage . prototype . cork = function ( ) {
305+ this [ kCorked ] ++ ;
302306 if ( this . socket ) {
303307 this . socket . cork ( ) ;
304- } else {
305- this [ kCorked ] ++ ;
306308 }
307309} ;
308310
309311OutgoingMessage . prototype . uncork = function ( ) {
312+ this [ kCorked ] -- ;
310313 if ( this . socket ) {
311314 this . socket . uncork ( ) ;
312- } else if ( this [ kCorked ] ) {
313- this [ kCorked ] -- ;
314315 }
316+
317+ if ( this [ kCorked ] || this [ kChunkedBuffer ] . length === 0 ) {
318+ return ;
319+ }
320+
321+ const len = this [ kChunkedLength ] ;
322+ const buf = this [ kChunkedBuffer ] ;
323+
324+ if ( this . chunkedEncoding ) {
325+ let callbacks ;
326+ this . _send ( NumberPrototypeToString ( len , 16 ) , 'latin1' , null ) ;
327+ this . _send ( crlf_buf , null , null ) ;
328+ for ( let n = 0 ; n < buf . length ; n += 3 ) {
329+ this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
330+ if ( buf [ n + 2 ] ) {
331+ callbacks ??= [ ] ;
332+ callbacks . push ( buf [ n + 2 ] ) ;
333+ }
334+ }
335+ this . _send ( crlf_buf , null , callbacks . length ? ( err ) => {
336+ for ( const callback of callbacks ) {
337+ callback ( err ) ;
338+ }
339+ } : null ) ;
340+ } else {
341+ for ( let n = 0 ; n < buf . length ; n += 3 ) {
342+ this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , buf [ n + 2 ] ) ;
343+ }
344+ }
345+
346+ this [ kChunkedBuffer ] . length = 0 ;
347+ this [ kChunkedLength ] = 0 ;
315348} ;
316349
317350OutgoingMessage . prototype . setTimeout = function setTimeout ( msecs , callback ) {
@@ -936,14 +969,19 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
936969 }
937970
938971 let ret ;
939- if ( msg . chunkedEncoding && chunk . length !== 0 ) {
972+ if ( msg [ kCorked ] && msg . _headerSent ) {
973+ len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
974+ msg [ kChunkedBuffer ] . push ( chunk , encoding , callback ) ;
975+ msg [ kChunkedLength ] += len ;
976+ ret = msg [ kChunkedLength ] < msg [ kHighWaterMark ] ;
977+ } else if ( msg . chunkedEncoding && chunk . length !== 0 ) {
940978 len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
941979 msg . _send ( NumberPrototypeToString ( len , 16 ) , 'latin1' , null ) ;
942980 msg . _send ( crlf_buf , null , null ) ;
943- msg . _send ( chunk , encoding , null , len ) ;
981+ msg . _send ( chunk , encoding , null ) ;
944982 ret = msg . _send ( crlf_buf , null , callback ) ;
945983 } else {
946- ret = msg . _send ( chunk , encoding , callback , len ) ;
984+ ret = msg . _send ( chunk , encoding , callback ) ;
947985 }
948986
949987 debug ( 'write ret = ' + ret ) ;
@@ -1068,7 +1106,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
10681106 this . socket . _writableState . corked = 1 ;
10691107 this . socket . uncork ( ) ;
10701108 }
1071- this [ kCorked ] = 0 ;
1109+ this [ kCorked ] = 1 ;
1110+ this . uncork ( ) ;
10721111
10731112 this . finished = true ;
10741113
0 commit comments