Browse Source

Updated file streams

Read streams now only support forceClose()

Write streams support close() and forceClose()
v0.7.4-release
Felix Geisendörfer 15 years ago
parent
commit
48562fa938
  1. 22
      lib/fs.js
  2. 3
      test/simple/test-file-write-stream.js

22
lib/fs.js

@ -405,7 +405,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) { fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) {
if (bytesRead === 0) { if (bytesRead === 0) {
self.emit('end'); self.emit('end');
self.close(); self.forceClose();
return; return;
} }
@ -415,6 +415,11 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
return; return;
} }
// do not emit events anymore after we declared the stream unreadable
if (!self.readable) {
return;
}
self.emit('data', data); self.emit('data', data);
read(); read();
}); });
@ -431,7 +436,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
read(); read();
}); });
this.close = function() { this.forceClose = function() {
this.readable = false; this.readable = false;
fs.close(this.fd, function(err) { fs.close(this.fd, function(err) {
if (err) { if (err) {
@ -544,6 +549,19 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
flush(); flush();
}; };
this.forceClose = function() {
this.writeable = false;
fs.close(self.fd, function(err) {
if (err) {
self.emit('error', err);
return;
}
self.emit('close');
});
};
flush(); flush();
}; };
FileWriteStream.prototype.__proto__ = process.EventEmitter.prototype; FileWriteStream.prototype.__proto__ = process.EventEmitter.prototype;

3
test/simple/test-file-write-stream.js

@ -17,6 +17,9 @@ file
callbacks.open++; callbacks.open++;
assert.equal('number', typeof fd); assert.equal('number', typeof fd);
}) })
.addListener('error', function(err) {
throw err;
})
.addListener('drain', function() { .addListener('drain', function() {
callbacks.drain++; callbacks.drain++;
if (callbacks.drain == -1) { if (callbacks.drain == -1) {

Loading…
Cancel
Save