From 48562fa9389946ae7ac5b977004fc0d47e5af947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Thu, 4 Mar 2010 22:06:06 +0100 Subject: [PATCH] Updated file streams Read streams now only support forceClose() Write streams support close() and forceClose() --- lib/fs.js | 22 ++++++++++++++++++++-- test/simple/test-file-write-stream.js | 3 +++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index de1bf94ce3..cf133158e8 100644 --- a/lib/fs.js +++ b/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) { if (bytesRead === 0) { self.emit('end'); - self.close(); + self.forceClose(); return; } @@ -415,6 +415,11 @@ var FileReadStream = exports.FileReadStream = function(path, options) { return; } + // do not emit events anymore after we declared the stream unreadable + if (!self.readable) { + return; + } + self.emit('data', data); read(); }); @@ -431,7 +436,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) { read(); }); - this.close = function() { + this.forceClose = function() { this.readable = false; fs.close(this.fd, function(err) { if (err) { @@ -544,6 +549,19 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { flush(); }; + this.forceClose = function() { + this.writeable = false; + fs.close(self.fd, function(err) { + if (err) { + self.emit('error', err); + return; + } + + self.emit('close'); + }); + }; + + flush(); }; FileWriteStream.prototype.__proto__ = process.EventEmitter.prototype; \ No newline at end of file diff --git a/test/simple/test-file-write-stream.js b/test/simple/test-file-write-stream.js index 669985e178..5f15bfe1c3 100644 --- a/test/simple/test-file-write-stream.js +++ b/test/simple/test-file-write-stream.js @@ -17,6 +17,9 @@ file callbacks.open++; assert.equal('number', typeof fd); }) + .addListener('error', function(err) { + throw err; + }) .addListener('drain', function() { callbacks.drain++; if (callbacks.drain == -1) {