@@ -82,6 +82,8 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
82
82
} ) ;
83
83
84
84
const kCorked = Symbol ( 'corked' ) ;
85
+ const kChunkedBuffer = Symbol ( 'kChunkedBuffer' ) ;
86
+ const kChunkedLength = Symbol ( 'kChunkedLength' ) ;
85
87
const kUniqueHeaders = Symbol ( 'kUniqueHeaders' ) ;
86
88
const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
87
89
const kErrored = Symbol ( 'errored' ) ;
@@ -140,6 +142,8 @@ function OutgoingMessage(options) {
140
142
this . finished = false ;
141
143
this . _headerSent = false ;
142
144
this [ kCorked ] = 0 ;
145
+ this [ kChunkedBuffer ] = [ ] ;
146
+ this [ kChunkedLength ] = 0 ;
143
147
this . _closed = false ;
144
148
145
149
this . socket = null ;
@@ -192,7 +196,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
192
196
ObjectDefineProperty ( OutgoingMessage . prototype , 'writableLength' , {
193
197
__proto__ : null ,
194
198
get ( ) {
195
- return this . outputSize + ( this . socket ? this . socket . writableLength : 0 ) ;
199
+ return this . outputSize + this [ kChunkedLength ] + ( this . socket ? this . socket . writableLength : 0 ) ;
196
200
} ,
197
201
} ) ;
198
202
@@ -206,8 +210,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
206
210
ObjectDefineProperty ( OutgoingMessage . prototype , 'writableCorked' , {
207
211
__proto__ : null ,
208
212
get ( ) {
209
- const corked = this . socket ? this . socket . writableCorked : 0 ;
210
- return corked + this [ kCorked ] ;
213
+ return this [ kCorked ] ;
211
214
} ,
212
215
} ) ;
213
216
@@ -299,19 +302,45 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
299
302
} ;
300
303
301
304
OutgoingMessage . prototype . cork = function ( ) {
305
+ this [ kCorked ] ++ ;
302
306
if ( this . socket ) {
303
307
this . socket . cork ( ) ;
304
- } else {
305
- this [ kCorked ] ++ ;
306
308
}
307
309
} ;
308
310
309
311
OutgoingMessage . prototype . uncork = function ( ) {
312
+ this [ kCorked ] -- ;
310
313
if ( this . socket ) {
311
314
this . socket . uncork ( ) ;
312
- } else if ( this [ kCorked ] ) {
313
- this [ kCorked ] -- ;
314
315
}
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
+ assert ( this . chunkedEncoding ) ;
325
+
326
+ let callbacks ;
327
+ this . _send ( NumberPrototypeToString ( len , 16 ) , 'latin1' , null ) ;
328
+ this . _send ( crlf_buf , null , null ) ;
329
+ for ( let n = 0 ; n < buf . length ; n += 3 ) {
330
+ this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
331
+ if ( buf [ n + 2 ] ) {
332
+ callbacks ??= [ ] ;
333
+ callbacks . push ( buf [ n + 2 ] ) ;
334
+ }
335
+ }
336
+ this . _send ( crlf_buf , null , callbacks . length ? ( err ) => {
337
+ for ( const callback of callbacks ) {
338
+ callback ( err ) ;
339
+ }
340
+ } : null ) ;
341
+
342
+ this [ kChunkedBuffer ] . length = 0 ;
343
+ this [ kChunkedLength ] = 0 ;
315
344
} ;
316
345
317
346
OutgoingMessage . prototype . setTimeout = function setTimeout ( msecs , callback ) {
@@ -937,11 +966,18 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
937
966
938
967
let ret ;
939
968
if ( msg . chunkedEncoding && chunk . length !== 0 ) {
940
- len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
941
- msg . _send ( NumberPrototypeToString ( len , 16 ) , 'latin1' , null ) ;
942
- msg . _send ( crlf_buf , null , null ) ;
943
- msg . _send ( chunk , encoding , null , len ) ;
944
- ret = msg . _send ( crlf_buf , null , callback ) ;
969
+ if ( msg [ kCorked ] && msg . _headerSent ) {
970
+ len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
971
+ msg [ kChunkedBuffer ] . push ( chunk , encoding , callback ) ;
972
+ msg [ kChunkedLength ] += len ;
973
+ ret = msg [ kChunkedLength ] < msg [ kHighWaterMark ] ;
974
+ } else {
975
+ len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
976
+ msg . _send ( NumberPrototypeToString ( len , 16 ) , 'latin1' , null ) ;
977
+ msg . _send ( crlf_buf , null , null ) ;
978
+ msg . _send ( chunk , encoding , null , len ) ;
979
+ ret = msg . _send ( crlf_buf , null , callback ) ;
980
+ }
945
981
} else {
946
982
ret = msg . _send ( chunk , encoding , callback , len ) ;
947
983
}
@@ -1068,7 +1104,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
1068
1104
this . socket . _writableState . corked = 1 ;
1069
1105
this . socket . uncork ( ) ;
1070
1106
}
1071
- this [ kCorked ] = 0 ;
1107
+ this [ kCorked ] = 1 ;
1108
+ this . uncork ( ) ;
1072
1109
1073
1110
this . finished = true ;
1074
1111
0 commit comments