@@ -2090,44 +2090,38 @@ ReadStream.prototype._read = function(n) {
2090
2090
}
2091
2091
} ;
2092
2092
2093
-
2094
2093
ReadStream . prototype . _destroy = function ( err , cb ) {
2095
- this . close ( function ( err2 ) {
2096
- cb ( err || err2 ) ;
2097
- } ) ;
2098
- } ;
2099
-
2100
-
2101
- ReadStream . prototype . close = function ( cb ) {
2102
- if ( cb )
2103
- this . once ( 'close' , cb ) ;
2104
-
2105
2094
if ( this . closed || typeof this . fd !== 'number' ) {
2106
2095
if ( typeof this . fd !== 'number' ) {
2107
- this . once ( 'open' , closeOnOpen ) ;
2096
+ this . once ( 'open' , closeFsStream . bind ( null , this , cb , err ) ) ;
2108
2097
return ;
2109
2098
}
2110
- return process . nextTick ( ( ) => this . emit ( 'close' ) ) ;
2099
+
2100
+ return process . nextTick ( ( ) => {
2101
+ cb ( err ) ;
2102
+ this . emit ( 'close' ) ;
2103
+ } ) ;
2111
2104
}
2112
2105
2113
2106
this . closed = true ;
2114
2107
2115
- fs . close ( this . fd , ( er ) => {
2116
- if ( er )
2117
- this . emit ( 'error' , er ) ;
2118
- else
2119
- this . emit ( 'close' ) ;
2120
- } ) ;
2121
-
2108
+ closeFsStream ( this , cb ) ;
2122
2109
this . fd = null ;
2123
2110
} ;
2124
2111
2125
- // needed because as it will be called with arguments
2126
- // that does not match this.close() signature
2127
- function closeOnOpen ( fd ) {
2128
- this . close ( ) ;
2112
+ function closeFsStream ( stream , cb , err ) {
2113
+ fs . close ( stream . fd , ( er ) => {
2114
+ er = er || err ;
2115
+ cb ( er ) ;
2116
+ if ( ! er )
2117
+ stream . emit ( 'close' ) ;
2118
+ } ) ;
2129
2119
}
2130
2120
2121
+ ReadStream . prototype . close = function ( cb ) {
2122
+ this . destroy ( null , cb ) ;
2123
+ } ;
2124
+
2131
2125
fs . createWriteStream = function ( path , options ) {
2132
2126
return new WriteStream ( path , options ) ;
2133
2127
} ;
@@ -2179,7 +2173,7 @@ function WriteStream(path, options) {
2179
2173
// dispose on finish.
2180
2174
this . once ( 'finish' , function ( ) {
2181
2175
if ( this . autoClose ) {
2182
- this . close ( ) ;
2176
+ this . destroy ( ) ;
2183
2177
}
2184
2178
} ) ;
2185
2179
}
@@ -2276,7 +2270,21 @@ WriteStream.prototype._writev = function(data, cb) {
2276
2270
2277
2271
2278
2272
WriteStream . prototype . _destroy = ReadStream . prototype . _destroy ;
2279
- WriteStream . prototype . close = ReadStream . prototype . close ;
2273
+ WriteStream . prototype . close = function ( cb ) {
2274
+ if ( this . _writableState . ending ) {
2275
+ this . on ( 'close' , cb ) ;
2276
+ return ;
2277
+ }
2278
+
2279
+ if ( this . _writableState . ended ) {
2280
+ process . nextTick ( cb ) ;
2281
+ return ;
2282
+ }
2283
+
2284
+ // we use end() instead of destroy() because of
2285
+ // https://github.com/nodejs/node/issues/2006
2286
+ this . end ( cb ) ;
2287
+ } ;
2280
2288
2281
2289
// There is no shutdown() for files.
2282
2290
WriteStream . prototype . destroySoon = WriteStream . prototype . end ;
0 commit comments