Skip to content

Commit 167d8ff

Browse files
mcollinaphated
authored andcommitted
Fix: Avoid node internals for our WriteStream (fixes #295)
1 parent ff51196 commit 167d8ff

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

lib/file-operations.js

+54-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var util = require('util');
55
var fs = require('graceful-fs');
66
var assign = require('object.assign');
77
var date = require('value-or-function').date;
8-
var FlushWriteStream = require('flush-write-stream');
8+
var Writable = require('readable-stream').Writable;
99

1010
var 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

382382
WriteStream.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
400400
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;
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

443480
module.exports = {
444481
closeFd: closeFd,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"coveralls": "npm run cover && istanbul-coveralls"
2727
},
2828
"dependencies": {
29-
"flush-write-stream": "^1.0.0",
3029
"fs-mkdirp-stream": "^1.0.0",
3130
"glob-stream": "^6.1.0",
3231
"graceful-fs": "^4.0.0",
@@ -35,6 +34,7 @@
3534
"lead": "^1.0.0",
3635
"object.assign": "^4.0.4",
3736
"pumpify": "^1.3.5",
37+
"readable-stream": "^2.3.3",
3838
"remove-bom-buffer": "^3.0.0",
3939
"remove-bom-stream": "^1.2.0",
4040
"resolve-options": "^1.1.0",

0 commit comments

Comments
 (0)