Browse Source

fs: remove undocumented .destroy callbacks

The destroy() method of fs.ReadStream and fs.WriteStream takes a callback.
It's a leftover from the node 0.1 days, undocumented and not part of the
streams API. Remove it.
v0.9.1-release
Ben Noordhuis 12 years ago
parent
commit
2f7e0f2da6
  1. 28
      lib/fs.js
  2. 16
      test/simple/test-fs-read-stream.js

28
lib/fs.js

@ -1364,25 +1364,19 @@ ReadStream.prototype._emitData = function(d) {
};
ReadStream.prototype.destroy = function(cb) {
ReadStream.prototype.destroy = function() {
var self = this;
if (!this.readable) {
if (cb) process.nextTick(function() { cb(null); });
return;
}
if (!this.readable) return;
this.readable = false;
function close() {
fs.close(self.fd, function(err) {
if (err) {
if (cb) cb(err);
self.emit('error', err);
return;
}
if (cb) cb(null);
} else {
self.emit('close');
}
});
}
@ -1570,25 +1564,19 @@ WriteStream.prototype.end = function(data, encoding, cb) {
this.flush();
};
WriteStream.prototype.destroy = function(cb) {
WriteStream.prototype.destroy = function() {
var self = this;
if (!this.writable) {
if (cb) process.nextTick(function() { cb(null); });
return;
}
if (!this.writable) return;
this.writable = false;
function close() {
fs.close(self.fd, function(err) {
if (err) {
if (cb) { cb(err); }
self.emit('error', err);
return;
}
if (cb) { cb(null); }
} else {
self.emit('close');
}
});
}

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

@ -34,7 +34,7 @@ var fs = require('fs');
var fn = path.join(common.fixturesDir, 'elipses.txt');
var rangeFile = path.join(common.fixturesDir, 'x.txt');
var callbacks = { open: 0, end: 0, close: 0, destroy: 0 };
var callbacks = { open: 0, end: 0, close: 0 };
var paused = false;
@ -82,17 +82,6 @@ file.on('close', function() {
//assert.equal(fs.readFileSync(fn), fileContent);
});
var file2 = fs.createReadStream(fn);
file2.destroy(function(err) {
assert.ok(!err);
callbacks.destroy++;
file2.destroy(function(err) {
assert.ok(!err);
callbacks.destroy++;
});
});
var file3 = fs.createReadStream(fn, {encoding: 'utf8'});
file3.length = 0;
file3.on('data', function(data) {
@ -112,10 +101,7 @@ file3.on('close', function() {
process.on('exit', function() {
assert.equal(1, callbacks.open);
assert.equal(1, callbacks.end);
assert.equal(2, callbacks.destroy);
assert.equal(2, callbacks.close);
assert.equal(30000, file.length);
assert.equal(10000, file3.length);
});

Loading…
Cancel
Save