diff --git a/lib/file-operations.js b/lib/file-operations.js index 29c6c726..b0aa31ba 100644 --- a/lib/file-operations.js +++ b/lib/file-operations.js @@ -5,7 +5,7 @@ var util = require('util'); var fs = require('graceful-fs'); var assign = require('object.assign'); var date = require('value-or-function').date; -var FlushWriteStream = require('flush-write-stream'); +var Writable = require('readable-stream').Writable; var constants = require('./constants'); @@ -359,7 +359,7 @@ function WriteStream(path, options, flush) { options = options || {}; - FlushWriteStream.call(this, options, worker, cleanup); + Writable.call(this, options); this.flush = flush; this.path = path; @@ -377,7 +377,7 @@ function WriteStream(path, options, flush) { this.once('finish', this.close); } -util.inherits(WriteStream, FlushWriteStream); +util.inherits(WriteStream, Writable); WriteStream.prototype.open = function() { var self = this; @@ -398,12 +398,57 @@ WriteStream.prototype.open = function() { // Use our `end` method since it is patched for flush WriteStream.prototype.destroySoon = WriteStream.prototype.end; -// Use node's `fs.WriteStream` methods -WriteStream.prototype._destroy = fs.WriteStream.prototype._destroy; -WriteStream.prototype.destroy = fs.WriteStream.prototype.destroy; -WriteStream.prototype.close = fs.WriteStream.prototype.close; -function worker(data, encoding, callback) { +WriteStream.prototype._destroy = function(err, cb) { + this.close(function(err2) { + cb(err || err2); + }); +}; + +WriteStream.prototype.close = function(cb) { + var that = this; + + if (cb) { + this.once('close', cb); + } + + if (this.closed || typeof this.fd !== 'number') { + if (typeof this.fd !== 'number') { + this.once('open', closeOnOpen); + return; + } + + return process.nextTick(function() { + that.emit('close'); + }); + } + + this.closed = true; + + fs.close(this.fd, function(er) { + if (er) { + that.emit('error', er); + } else { + that.emit('close'); + } + }); + + this.fd = null; +}; + +WriteStream.prototype._final = function(callback) { + if (typeof this.flush !== 'function') { + return callback(); + } + + this.flush(this.fd, callback); +}; + +function closeOnOpen() { + this.close(); +} + +WriteStream.prototype._write = function(data, encoding, callback) { var self = this; // 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) { callback(); } -} - -function cleanup(callback) { - if (typeof this.flush !== 'function') { - return callback(); - } - - this.flush(this.fd, callback); -} +}; module.exports = { closeFd: closeFd, diff --git a/package.json b/package.json index 7a0da2c4..3e27e57f 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "coveralls": "npm run cover && istanbul-coveralls" }, "dependencies": { - "flush-write-stream": "^1.0.0", "fs-mkdirp-stream": "^1.0.0", "glob-stream": "^6.1.0", "graceful-fs": "^4.0.0", @@ -35,6 +34,7 @@ "lead": "^1.0.0", "object.assign": "^4.0.4", "pumpify": "^1.3.5", + "readable-stream": "^2.3.3", "remove-bom-buffer": "^3.0.0", "remove-bom-stream": "^1.2.0", "resolve-options": "^1.1.0",