@@ -90,6 +90,7 @@ const kBytesWritten = Symbol('kBytesWritten');
9090const kErrored = Symbol ( 'errored' ) ;
9191const kHighWaterMark = Symbol ( 'kHighWaterMark' ) ;
9292const kRejectNonStandardBodyWrites = Symbol ( 'kRejectNonStandardBodyWrites' ) ;
93+ const kSocket = Symbol ( 'kSocket' ) ;
9394
9495const nop = ( ) => { } ;
9596
@@ -211,8 +212,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
211212ObjectDefineProperty ( OutgoingMessage . prototype , 'writableCorked' , {
212213 __proto__ : null ,
213214 get ( ) {
214- return this [ kCorked ] ;
215- } ,
215+ return this [ kSocket ] ? this [ kSocket ] . writableCorked : this [ kCorked ] ;
216+ }
216217} ) ;
217218
218219ObjectDefineProperty ( OutgoingMessage . prototype , '_headers' , {
@@ -260,6 +261,20 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'socket', {
260261 } ,
261262} ) ;
262263
264+ ObjectDefineProperty ( OutgoingMessage . prototype , 'socket' , {
265+ __proto__ : null ,
266+ get : function ( ) {
267+ return this [ kSocket ] ;
268+ } ,
269+ set : function ( val ) {
270+ for ( let n = 0 ; n < this [ kCorked ] ; n ++ ) {
271+ val ?. cork ( ) ;
272+ }
273+ this [ kCorked ] = 0 ;
274+ this [ kSocket ] = val ;
275+ }
276+ } ) ;
277+
263278ObjectDefineProperty ( OutgoingMessage . prototype , '_headerNames' , {
264279 __proto__ : null ,
265280 get : internalUtil . deprecate ( function ( ) {
@@ -1000,9 +1015,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
10001015 }
10011016 }
10021017
1003- if ( ! fromEnd && msg . socket && ! msg . socket . writableCorked ) {
1004- msg . socket . cork ( ) ;
1005- process . nextTick ( connectionCorkNT , msg . socket ) ;
1018+ if ( ! fromEnd && ! msg . writableCorked ) {
1019+ msg . cork ( ) ;
1020+ process . nextTick ( connectionUncorkNT , msg ) ;
10061021 }
10071022
10081023 let ret ;
@@ -1027,8 +1042,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
10271042}
10281043
10291044
1030- function connectionCorkNT ( conn ) {
1031- conn . uncork ( ) ;
1045+ function connectionUncorkNT ( msg ) {
1046+ msg . uncork ( ) ;
10321047}
10331048
10341049OutgoingMessage . prototype . addTrailers = function addTrailers ( headers ) {
@@ -1143,9 +1158,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
11431158 // Fully uncork connection on end().
11441159 this [ kSocket ] . _writableState . corked = 1 ;
11451160 this [ kSocket ] . uncork ( ) ;
1161+ } else {
1162+ this [ kCorked ] = 0 ;
11461163 }
1147- this [ kCorked ] = 1 ;
1148- this . uncork ( ) ;
11491164
11501165 this . finished = true ;
11511166
0 commit comments