Skip to content

Commit

Permalink
Fix: Avoid node internals for our WriteStream (fixes #295)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored and phated committed Jan 23, 2018
1 parent ff51196 commit 167d8ff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
71 changes: 54 additions & 17 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 167d8ff

Please sign in to comment.