@@ -5,7 +5,7 @@ var util = require('util');
55var fs = require ( 'graceful-fs' ) ;
66var assign = require ( 'object.assign' ) ;
77var date = require ( 'value-or-function' ) . date ;
8- var FlushWriteStream = require ( 'flush-write- stream' ) ;
8+ var Writable = require ( 'readable- stream' ) . Writable ;
99
1010var constants = require ( './constants' ) ;
1111
@@ -359,7 +359,7 @@ function WriteStream(path, options, flush) {
359359
360360 options = options || { } ;
361361
362- FlushWriteStream . call ( this , options , worker , cleanup ) ;
362+ Writable . call ( this , options ) ;
363363
364364 this . flush = flush ;
365365 this . path = path ;
@@ -377,7 +377,7 @@ function WriteStream(path, options, flush) {
377377 this . once ( 'finish' , this . close ) ;
378378}
379379
380- util . inherits ( WriteStream , FlushWriteStream ) ;
380+ util . inherits ( WriteStream , Writable ) ;
381381
382382WriteStream . prototype . open = function ( ) {
383383 var self = this ;
@@ -398,12 +398,57 @@ WriteStream.prototype.open = function() {
398398
399399// Use our `end` method since it is patched for flush
400400WriteStream . 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 ;
405401
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 ) {
407452 var self = this ;
408453
409454 // 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) {
430475
431476 callback ( ) ;
432477 }
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+ } ;
442479
443480module . exports = {
444481 closeFd : closeFd ,
0 commit comments