@@ -119,6 +119,9 @@ function Socket(options) {
119
119
120
120
stream . Duplex . call ( this , options ) ;
121
121
122
+ // NOTE: do it here to avoid copying `options`
123
+ this . _writableState . _end = _end ;
124
+
122
125
if ( options . handle ) {
123
126
this . _handle = options . handle ; // private
124
127
} else if ( options . fd !== undefined ) {
@@ -199,32 +202,7 @@ function onSocketFinish() {
199
202
if ( ! this . _handle || ! this . _handle . shutdown )
200
203
return this . destroy ( ) ;
201
204
202
- var req = new ShutdownWrap ( ) ;
203
- req . oncomplete = afterShutdown ;
204
- req . handle = this . _handle ;
205
- var err = this . _handle . shutdown ( req ) ;
206
-
207
- if ( err )
208
- return this . _destroy ( errnoException ( err , 'shutdown' ) ) ;
209
- }
210
-
211
-
212
- function afterShutdown ( status , handle , req ) {
213
- var self = handle . owner ;
214
-
215
- debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
216
- self . _readableState ) ;
217
-
218
- // callback may come after call to destroy.
219
- if ( self . destroyed )
220
- return ;
221
-
222
- if ( self . _readableState . ended ) {
223
- debug ( 'readableState ended, destroying' ) ;
224
- self . destroy ( ) ;
225
- } else {
226
- self . once ( '_socketEnd' , self . destroy ) ;
227
- }
205
+ this . once ( '_socketEnd' , this . destroy ) ;
228
206
}
229
207
230
208
// the EOF has been received, and no more bytes are coming.
@@ -690,6 +668,50 @@ Socket.prototype._write = function(data, encoding, cb) {
690
668
this . _writeGeneric ( false , data , encoding , cb ) ;
691
669
} ;
692
670
671
+ function _end ( socket , cb ) {
672
+ debug ( '_end' ) ;
673
+
674
+ // If still connecting - defer handling 'finish' until 'connect' will happen
675
+ if ( socket . _connecting ) {
676
+ debug ( '_end: not yet connected' ) ;
677
+ return socket . once ( 'connect' , function ( ) {
678
+ _end ( socket , cb ) ;
679
+ } ) ;
680
+ }
681
+
682
+ if ( ! socket . readable || socket . _readableState . ended ) {
683
+ debug ( '_end: not readable or ended' ) ;
684
+ return cb ( ) ;
685
+ }
686
+
687
+ // otherwise, just shutdown, or destroy() if not possible
688
+ if ( ! socket . _handle || ! socket . _handle . shutdown ) {
689
+ debug ( '_end: no handle or handle does not support shutdown' ) ;
690
+ return cb ( ) ;
691
+ }
692
+
693
+ var req = new ShutdownWrap ( ) ;
694
+ req . oncomplete = afterShutdown ;
695
+ req . handle = this . _handle ;
696
+ req . flushCb = cb ;
697
+ var err = socket . _handle . shutdown ( req ) ;
698
+
699
+ if ( err ) {
700
+ debug ( '_end: errno %s' , err ) ;
701
+ return cb ( errnoException ( err , 'shutdown' ) ) ;
702
+ }
703
+ }
704
+
705
+
706
+ function afterShutdown ( status , handle , req ) {
707
+ var self = handle . owner ;
708
+
709
+ debug ( 'afterShutdown destroyed=%j' , self . destroyed ,
710
+ self . _readableState ) ;
711
+
712
+ req . flushCb ( ) ;
713
+ }
714
+
693
715
function createWriteReq ( req , handle , data , encoding ) {
694
716
switch ( encoding ) {
695
717
case 'binary' :
@@ -864,6 +886,7 @@ Socket.prototype.connect = function(options, cb) {
864
886
this . _readableState . endEmitted = false ;
865
887
this . _writableState . ended = false ;
866
888
this . _writableState . ending = false ;
889
+ this . _writableState . flushed = false ;
867
890
this . _writableState . finished = false ;
868
891
this . _writableState . errorEmitted = false ;
869
892
this . destroyed = false ;
0 commit comments