@@ -67,6 +67,7 @@ const {
6767 ERR_STREAM_DESTROYED ,
6868 ERR_STREAM_NULL_VALUES ,
6969 ERR_STREAM_WRITE_AFTER_END ,
70+ ERR_UNKNOWN_ENCODING ,
7071 } ,
7172 hideStackFrames,
7273} = require ( 'internal/errors' ) ;
@@ -230,7 +231,12 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
230231ObjectDefineProperty ( OutgoingMessage . prototype , 'writableLength' , {
231232 __proto__ : null ,
232233 get ( ) {
233- return this . outputSize + this [ kChunkedLength ] + ( this [ kSocket ] ? this [ kSocket ] . writableLength : 0 ) ;
234+ let bufferedLength = this [ kChunkedLength ] ;
235+ if ( bufferedLength !== 0 ) {
236+ bufferedLength += bufferedLength . toString ( 16 ) . length + 4 +
237+ ( ! this . _headerSent && this . _header !== null ? this . _header . length : 0 ) ;
238+ }
239+ return this . outputSize + bufferedLength + ( this [ kSocket ] ? this [ kSocket ] . writableLength : 0 ) ;
234240 } ,
235241} ) ;
236242
@@ -301,37 +307,45 @@ OutgoingMessage.prototype.cork = function cork() {
301307
302308OutgoingMessage . prototype . uncork = function uncork ( ) {
303309 this [ kCorked ] -- ;
304- if ( this [ kSocket ] ) {
305- this [ kSocket ] . uncork ( ) ;
306- }
307-
308- if ( this [ kCorked ] || this [ kChunkedBuffer ] . length === 0 ) {
309- return ;
310- }
311-
312- const len = this [ kChunkedLength ] ;
313- const buf = this [ kChunkedBuffer ] ;
310+ const socket = this [ kSocket ] ;
311+ try {
312+ if ( this [ kCorked ] || this [ kChunkedBuffer ] . length === 0 ) {
313+ return ;
314+ }
314315
315- assert ( this . chunkedEncoding ) ;
316+ const len = this [ kChunkedLength ] ;
317+ const buf = this [ kChunkedBuffer ] ;
318+ if ( this . destroyed || socket ?. destroyed ) {
319+ const error = this [ kErrored ] || socket ?. _writableState ?. errored || new ERR_STREAM_DESTROYED ( 'write' ) ;
320+ for ( let n = 2 ; n < buf . length ; n += 3 ) {
321+ if ( buf [ n ] !== nop ) {
322+ process . nextTick ( buf [ n ] , error ) ;
323+ }
324+ }
325+ buf . length = 0 ;
326+ this [ kChunkedLength ] = 0 ;
327+ return ;
328+ }
329+ assert ( this . chunkedEncoding ) ;
316330
317- let callbacks ;
318- this . _send ( len . toString ( 16 ) , 'latin1' , null ) ;
319- this . _send ( crlf_buf , null , null ) ;
320- for ( let n = 0 ; n < buf . length ; n += 3 ) {
321- this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
322- if ( buf [ n + 2 ] ) {
323- callbacks ??= [ ] ;
331+ const callbacks = [ ] ;
332+ this . _send ( len . toString ( 16 ) , 'latin1' , null ) ;
333+ this . _send ( crlf_buf , null , null ) ;
334+ for ( let n = 0 ; n < buf . length ; n += 3 ) {
335+ this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
324336 callbacks . push ( buf [ n + 2 ] ) ;
325337 }
326- }
327- this . _send ( crlf_buf , null , callbacks . length ? ( err ) => {
328- for ( const callback of callbacks ) {
329- callback ( err ) ;
330- }
331- } : null ) ;
338+ this . _send ( crlf_buf , null , ( err ) => {
339+ for ( const callback of callbacks ) {
340+ callback ( err ) ;
341+ }
342+ } ) ;
332343
333- this [ kChunkedBuffer ] . length = 0 ;
334- this [ kChunkedLength ] = 0 ;
344+ this [ kChunkedBuffer ] . length = 0 ;
345+ this [ kChunkedLength ] = 0 ;
346+ } finally {
347+ socket ?. uncork ( ) ;
348+ }
335349
336350 // If we had a pending drain and flushed all data, emit the drain event.
337351 if ( this [ kNeedDrain ] && this . writableLength === 0 ) {
@@ -367,6 +381,10 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
367381 this . destroyed = true ;
368382
369383 this [ kErrored ] = error ;
384+ if ( this [ kChunkedBuffer ] . length !== 0 ) {
385+ this [ kCorked ] = 1 ;
386+ this . uncork ( ) ;
387+ }
370388
371389 if ( this [ kSocket ] ) {
372390 this [ kSocket ] . destroy ( error ) ;
@@ -1006,24 +1024,34 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
10061024 }
10071025 }
10081026
1027+ if ( msg . chunkedEncoding && typeof chunk === 'string' && encoding && ! Buffer . isEncoding ( encoding ) ) {
1028+ throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
1029+ }
1030+
10091031 if ( ! fromEnd && msg . socket && ! msg . socket . writableCorked ) {
1010- msg . socket . cork ( ) ;
1011- process . nextTick ( connectionCorkNT , msg . socket ) ;
1032+ msg . cork ( ) ;
1033+ process . nextTick ( connectionCorkNT , msg ) ;
10121034 }
10131035
10141036 let ret ;
10151037 if ( msg . chunkedEncoding && chunk . length !== 0 ) {
10161038 len ??= typeof chunk === 'string' ? Buffer . byteLength ( chunk , encoding ) : chunk . byteLength ;
1017- if ( msg [ kCorked ] && msg . _headerSent ) {
1039+ if ( msg [ kCorked ] ) {
1040+ if ( typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
1041+ chunk = Stream . _uint8ArrayToBuffer ( chunk ) ;
1042+ }
10181043 msg [ kChunkedBuffer ] . push ( chunk , encoding , callback ) ;
10191044 msg [ kChunkedLength ] += len ;
1020- ret = msg [ kChunkedLength ] < msg [ kHighWaterMark ] ;
1045+ ret = msg . writableLength < msg . writableHighWaterMark ;
10211046 } else {
10221047 msg . _send ( len . toString ( 16 ) , 'latin1' , null ) ;
10231048 msg . _send ( crlf_buf , null , null ) ;
10241049 msg . _send ( chunk , encoding , null , len ) ;
10251050 ret = msg . _send ( crlf_buf , null , callback ) ;
10261051 }
1052+ } else if ( msg . chunkedEncoding && msg [ kCorked ] && msg [ kChunkedBuffer ] . length !== 0 ) {
1053+ msg [ kChunkedBuffer ] . push ( chunk , encoding , callback ) ;
1054+ ret = msg . writableLength < msg . writableHighWaterMark ;
10271055 } else {
10281056 ret = msg . _send ( chunk , encoding , callback , len ) ;
10291057 }
@@ -1052,8 +1080,10 @@ function maybePrepareFinalChunk(msg, chunk, encoding) {
10521080 return ! ! msg . _header && msg . _hasBody && ! msg . chunkedEncoding ;
10531081}
10541082
1055- function connectionCorkNT ( conn ) {
1056- conn . uncork ( ) ;
1083+ function connectionCorkNT ( msg ) {
1084+ if ( msg [ kCorked ] ) {
1085+ msg . uncork ( ) ;
1086+ }
10571087}
10581088
10591089OutgoingMessage . prototype . addTrailers = function addTrailers ( headers ) {
@@ -1208,6 +1238,13 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
12081238 throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH ( this [ kBytesWritten ] , this . _contentLength ) ;
12091239 }
12101240
1241+ // Flush buffered chunks before terminating the response.
1242+ if ( this [ kChunkedBuffer ] . length !== 0 ) {
1243+ this [ kSocket ] ?. cork ( ) ;
1244+ this [ kCorked ] = 1 ;
1245+ this . uncork ( ) ;
1246+ }
1247+
12111248 if ( finishCallback === null ) {
12121249 // If we didn't early finish, send the last data and schedule 'finish' now:
12131250 finishCallback = onFinish . bind ( undefined , this ) ;
0 commit comments