Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rely on Node.js internals #296

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: required
language: node_js
node_js:
- '9'
- '8'
- '6'
- '5'
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "9"

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
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