Browse Source

fs: fix ReadStream / WriteStream missing callback

The (undocumented) callback argument to .destroy() was not called if the
stream was no longer readable / writable.
v0.8.7-release
Gil Pedersen 13 years ago
committed by Ben Noordhuis
parent
commit
f1fba8d1f5
  1. 10
      lib/fs.js
  2. 7
      test/simple/test-fs-read-stream.js

10
lib/fs.js

@ -1367,7 +1367,10 @@ ReadStream.prototype._emitData = function(d) {
ReadStream.prototype.destroy = function(cb) { ReadStream.prototype.destroy = function(cb) {
var self = this; var self = this;
if (!this.readable) return; if (!this.readable) {
if (cb) process.nextTick(function() { cb(null); });
return;
}
this.readable = false; this.readable = false;
function close() { function close() {
@ -1570,7 +1573,10 @@ WriteStream.prototype.end = function(data, encoding, cb) {
WriteStream.prototype.destroy = function(cb) { WriteStream.prototype.destroy = function(cb) {
var self = this; var self = this;
if (!this.writable) return; if (!this.writable) {
if (cb) process.nextTick(function() { cb(null); });
return;
}
this.writable = false; this.writable = false;
function close() { function close() {

7
test/simple/test-fs-read-stream.js

@ -86,6 +86,11 @@ var file2 = fs.createReadStream(fn);
file2.destroy(function(err) { file2.destroy(function(err) {
assert.ok(!err); assert.ok(!err);
callbacks.destroy++; callbacks.destroy++;
file2.destroy(function(err) {
assert.ok(!err);
callbacks.destroy++;
});
}); });
var file3 = fs.createReadStream(fn, {encoding: 'utf8'}); var file3 = fs.createReadStream(fn, {encoding: 'utf8'});
@ -107,7 +112,7 @@ file3.on('close', function() {
process.on('exit', function() { process.on('exit', function() {
assert.equal(1, callbacks.open); assert.equal(1, callbacks.open);
assert.equal(1, callbacks.end); assert.equal(1, callbacks.end);
assert.equal(1, callbacks.destroy); assert.equal(2, callbacks.destroy);
assert.equal(2, callbacks.close); assert.equal(2, callbacks.close);

Loading…
Cancel
Save