@@ -124,7 +124,7 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {
124124 this . outputEncodings . unshift ( 'binary' ) ;
125125 this . outputCallbacks . unshift ( null ) ;
126126 this . outputSize += this . _header . length ;
127- if ( this . _onPendingData !== null )
127+ if ( typeof this . _onPendingData === 'function' )
128128 this . _onPendingData ( this . _header . length ) ;
129129 }
130130 this . _headerSent = true ;
@@ -147,20 +147,7 @@ OutgoingMessage.prototype._writeRaw = function(data, encoding, callback) {
147147 // There might be pending data in the this.output buffer.
148148 var outputLength = this . output . length ;
149149 if ( outputLength > 0 ) {
150- var output = this . output ;
151- var outputEncodings = this . outputEncodings ;
152- var outputCallbacks = this . outputCallbacks ;
153- for ( var i = 0 ; i < outputLength ; i ++ ) {
154- connection . write ( output [ i ] , outputEncodings [ i ] ,
155- outputCallbacks [ i ] ) ;
156- }
157-
158- this . output = [ ] ;
159- this . outputEncodings = [ ] ;
160- this . outputCallbacks = [ ] ;
161- if ( this . _onPendingData !== null )
162- this . _onPendingData ( - this . outputSize ) ;
163- this . outputSize = 0 ;
150+ this . _flushOutput ( connection ) ;
164151 } else if ( data . length === 0 ) {
165152 if ( typeof callback === 'function' )
166153 process . nextTick ( callback ) ;
@@ -185,7 +172,7 @@ OutgoingMessage.prototype._buffer = function(data, encoding, callback) {
185172 this . outputEncodings . push ( encoding ) ;
186173 this . outputCallbacks . push ( callback ) ;
187174 this . outputSize += data . length ;
188- if ( this . _onPendingData !== null )
175+ if ( typeof this . _onPendingData === 'function' )
189176 this . _onPendingData ( data . length ) ;
190177 return false ;
191178} ;
@@ -618,24 +605,11 @@ OutgoingMessage.prototype._finish = function() {
618605// to attempt to flush any pending messages out to the socket.
619606OutgoingMessage . prototype . _flush = function ( ) {
620607 var socket = this . socket ;
621- var outputLength , ret ;
608+ var ret ;
622609
623610 if ( socket && socket . writable ) {
624611 // There might be remaining data in this.output; write it out
625- outputLength = this . output . length ;
626- if ( outputLength > 0 ) {
627- var output = this . output ;
628- var outputEncodings = this . outputEncodings ;
629- var outputCallbacks = this . outputCallbacks ;
630- for ( var i = 0 ; i < outputLength ; i ++ ) {
631- ret = socket . write ( output [ i ] , outputEncodings [ i ] ,
632- outputCallbacks [ i ] ) ;
633- }
634-
635- this . output = [ ] ;
636- this . outputEncodings = [ ] ;
637- this . outputCallbacks = [ ] ;
638- }
612+ ret = this . _flushOutput ( socket ) ;
639613
640614 if ( this . finished ) {
641615 // This is a queue to the server or client to bring in the next this.
@@ -647,6 +621,32 @@ OutgoingMessage.prototype._flush = function() {
647621 }
648622} ;
649623
624+ OutgoingMessage . prototype . _flushOutput = function _flushOutput ( socket ) {
625+ var ret ;
626+ var outputLength = this . output . length ;
627+ if ( outputLength <= 0 )
628+ return ret ;
629+
630+ var output = this . output ;
631+ var outputEncodings = this . outputEncodings ;
632+ var outputCallbacks = this . outputCallbacks ;
633+ socket . cork ( ) ;
634+ for ( var i = 0 ; i < outputLength ; i ++ ) {
635+ ret = socket . write ( output [ i ] , outputEncodings [ i ] ,
636+ outputCallbacks [ i ] ) ;
637+ }
638+ socket . uncork ( ) ;
639+
640+ this . output = [ ] ;
641+ this . outputEncodings = [ ] ;
642+ this . outputCallbacks = [ ] ;
643+ if ( typeof this . _onPendingData === 'function' )
644+ this . _onPendingData ( - this . outputSize ) ;
645+ this . outputSize = 0 ;
646+
647+ return ret ;
648+ } ;
649+
650650
651651OutgoingMessage . prototype . flushHeaders = function ( ) {
652652 if ( ! this . _header ) {
0 commit comments