@@ -5,7 +5,7 @@ var util = require('util');
5
5
var fs = require ( 'graceful-fs' ) ;
6
6
var assign = require ( 'object.assign' ) ;
7
7
var date = require ( 'value-or-function' ) . date ;
8
- var FlushWriteStream = require ( 'flush-write- stream' ) ;
8
+ var Writable = require ( 'readable- stream' ) . Writable ;
9
9
10
10
var constants = require ( './constants' ) ;
11
11
@@ -359,7 +359,7 @@ function WriteStream(path, options, flush) {
359
359
360
360
options = options || { } ;
361
361
362
- FlushWriteStream . call ( this , options , worker , cleanup ) ;
362
+ Writable . call ( this , options ) ;
363
363
364
364
this . flush = flush ;
365
365
this . path = path ;
@@ -377,7 +377,7 @@ function WriteStream(path, options, flush) {
377
377
this . once ( 'finish' , this . close ) ;
378
378
}
379
379
380
- util . inherits ( WriteStream , FlushWriteStream ) ;
380
+ util . inherits ( WriteStream , Writable ) ;
381
381
382
382
WriteStream . prototype . open = function ( ) {
383
383
var self = this ;
@@ -398,12 +398,57 @@ WriteStream.prototype.open = function() {
398
398
399
399
// Use our `end` method since it is patched for flush
400
400
WriteStream . prototype . destroySoon = WriteStream . prototype . end ;
401
- // Use node's `fs.WriteStream` methods
402
- WriteStream . prototype . _destroy = fs . WriteStream . prototype . _destroy ;
403
- WriteStream . prototype . destroy = fs . WriteStream . prototype . destroy ;
404
- WriteStream . prototype . close = fs . WriteStream . prototype . close ;
405
401
406
- function worker ( data , encoding , callback ) {
402
+ WriteStream . prototype . _destroy = function ( err , cb ) {
403
+ this . close ( function ( err2 ) {
404
+ cb ( err || err2 ) ;
405
+ } ) ;
406
+ } ;
407
+
408
+ WriteStream . prototype . close = function ( cb ) {
409
+ var that = this ;
410
+
411
+ if ( cb ) {
412
+ this . once ( 'close' , cb ) ;
413
+ }
414
+
415
+ if ( this . closed || typeof this . fd !== 'number' ) {
416
+ if ( typeof this . fd !== 'number' ) {
417
+ this . once ( 'open' , closeOnOpen ) ;
418
+ return ;
419
+ }
420
+
421
+ return process . nextTick ( function ( ) {
422
+ that . emit ( 'close' ) ;
423
+ } ) ;
424
+ }
425
+
426
+ this . closed = true ;
427
+
428
+ fs . close ( this . fd , function ( er ) {
429
+ if ( er ) {
430
+ that . emit ( 'error' , er ) ;
431
+ } else {
432
+ that . emit ( 'close' ) ;
433
+ }
434
+ } ) ;
435
+
436
+ this . fd = null ;
437
+ } ;
438
+
439
+ WriteStream . prototype . _final = function ( callback ) {
440
+ if ( typeof this . flush !== 'function' ) {
441
+ return callback ( ) ;
442
+ }
443
+
444
+ this . flush ( this . fd , callback ) ;
445
+ } ;
446
+
447
+ function closeOnOpen ( ) {
448
+ this . close ( ) ;
449
+ }
450
+
451
+ WriteStream . prototype . _write = function ( data , encoding , callback ) {
407
452
var self = this ;
408
453
409
454
// This is from node core but I have no idea how to get code coverage on it
@@ -430,15 +475,7 @@ function worker(data, encoding, callback) {
430
475
431
476
callback ( ) ;
432
477
}
433
- }
434
-
435
- function cleanup ( callback ) {
436
- if ( typeof this . flush !== 'function' ) {
437
- return callback ( ) ;
438
- }
439
-
440
- this . flush ( this . fd , callback ) ;
441
- }
478
+ } ;
442
479
443
480
module . exports = {
444
481
closeFd : closeFd ,
0 commit comments