@@ -55,6 +55,8 @@ const { validateString } = require('internal/validators');
5555const HIGH_WATER_MARK = getDefaultHighWaterMark ( ) ;
5656const { CRLF , debug } = common ;
5757
58+ const kCorked = Symbol ( 'corked' ) ;
59+
5860const RE_CONN_CLOSE = / (?: ^ | \W ) c l o s e (?: $ | \W ) / i;
5961const RE_TE_CHUNKED = common . chunkExpression ;
6062
@@ -98,6 +100,7 @@ function OutgoingMessage() {
98100
99101 this . finished = false ;
100102 this . _headerSent = false ;
103+ this [ kCorked ] = 0 ;
101104
102105 this . socket = null ;
103106 this . connection = null ;
@@ -137,6 +140,13 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
137140 }
138141} ) ;
139142
143+ Object . defineProperty ( OutgoingMessage . prototype , 'writableCorked' , {
144+ get ( ) {
145+ const corked = this . socket ? this . socket . writableCorked : 0 ;
146+ return corked + this [ kCorked ] ;
147+ }
148+ } ) ;
149+
140150Object . defineProperty ( OutgoingMessage . prototype , '_headers' , {
141151 get : internalUtil . deprecate ( function ( ) {
142152 return this . getHeaders ( ) ;
@@ -204,6 +214,21 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
204214 return headers ;
205215} ;
206216
217+ OutgoingMessage . prototype . cork = function ( ) {
218+ if ( this . socket ) {
219+ this . socket . cork ( ) ;
220+ } else {
221+ this [ kCorked ] ++ ;
222+ }
223+ } ;
224+
225+ OutgoingMessage . prototype . uncork = function ( ) {
226+ if ( this . socket ) {
227+ this . socket . uncork ( ) ;
228+ } else if ( this [ kCorked ] ) {
229+ this [ kCorked ] -- ;
230+ }
231+ } ;
207232
208233OutgoingMessage . prototype . setTimeout = function setTimeout ( msecs , callback ) {
209234
@@ -694,7 +719,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
694719 return this ;
695720 }
696721
697- var uncork ;
722+ if ( this . socket ) {
723+ this . socket . cork ( ) ;
724+ }
725+
698726 if ( chunk ) {
699727 if ( typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
700728 throw new ERR_INVALID_ARG_TYPE ( 'chunk' , [ 'string' , 'Buffer' ] , chunk ) ;
@@ -705,10 +733,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
705733 else
706734 this . _contentLength = chunk . length ;
707735 }
708- if ( this . connection ) {
709- this . connection . cork ( ) ;
710- uncork = true ;
711- }
712736 write_ ( this , chunk , encoding , null , true ) ;
713737 } else if ( ! this . _header ) {
714738 this . _contentLength = 0 ;
@@ -727,8 +751,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
727751 this . _send ( '' , 'latin1' , finish ) ;
728752 }
729753
730- if ( uncork )
731- this . connection . uncork ( ) ;
754+ if ( this . socket ) {
755+ // Fully uncork connection on end().
756+ this . socket . _writableState . corked = 1 ;
757+ this . socket . uncork ( ) ;
758+ }
759+ this [ kCorked ] = 0 ;
732760
733761 this . finished = true ;
734762
@@ -789,6 +817,11 @@ OutgoingMessage.prototype._flush = function _flush() {
789817} ;
790818
791819OutgoingMessage . prototype . _flushOutput = function _flushOutput ( socket ) {
820+ while ( this [ kCorked ] ) {
821+ this [ kCorked ] -- ;
822+ socket . cork ( ) ;
823+ }
824+
792825 const outputLength = this . outputData . length ;
793826 if ( outputLength <= 0 )
794827 return undefined ;
0 commit comments