From 301f53c2aa998fae3bd43d41b2730560a06d7601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Tue, 5 Apr 2011 23:37:40 +0200 Subject: [PATCH] Allow omission of end option for range reads Problem: Sometimes it is useful to read a file from a certain position to it's end. The current implementation was already perfectly capable of this, but decided to throw an error when the user tried to omit the end option. The only way to do this, was to pass {end: Infinity}. Solution: Automatically assume {end: Infinity} when omitted, and remove the previous exception thrown. Also updated the docs. closes #801. --- doc/api/fs.markdown | 2 +- lib/fs.js | 11 ++++++----- test/simple/test-fs-read-stream.js | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index 6d7cd78364..506883bbfe 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -367,7 +367,7 @@ Returns a new ReadStream object (See `Readable Stream`). `options` can include `start` and `end` values to read a range of bytes from the file instead of the entire file. Both `start` and `end` are inclusive and -start at 0. When used, both the limits must be specified always. +start at 0. An example to read the last 10 bytes of a file which is 100 bytes long: diff --git a/lib/fs.js b/lib/fs.js index 0f2318d584..ab169fb9ab 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -799,11 +799,12 @@ var ReadStream = fs.ReadStream = function(path, options) { if (this.encoding) this.setEncoding(this.encoding); - if (this.start !== undefined || this.end !== undefined) { - if (this.start === undefined || this.end === undefined) { - this.emit('error', new Error('Both start and end are needed ' + - 'for range streaming.')); - } else if (this.start > this.end) { + if (this.start !== undefined) { + if (this.end === undefined) { + this.end = Infinity; + } + + if (this.start > this.end) { this.emit('error', new Error('start must be <= end')); } else { this._firstRead = true; diff --git a/test/simple/test-fs-read-stream.js b/test/simple/test-fs-read-stream.js index 188c95d52d..5362389fde 100644 --- a/test/simple/test-fs-read-stream.js +++ b/test/simple/test-fs-read-stream.js @@ -121,19 +121,19 @@ file4.addListener('end', function(data) { assert.equal(contentRead, 'yz'); }); -try { - fs.createReadStream(rangeFile, {start: 10, end: 2}); - assert.fail('Creating a ReadStream with incorrect range limits must throw.'); -} catch (e) { - assert.equal(e.message, 'start must be <= end'); -} +var file5 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1}); +file5.data = ''; +file5.addListener('data', function(data) { + file5.data += data.toString('utf-8'); +}); +file5.addListener('end', function() { + assert.equal(file5.data, 'yz\n'); +}); -try { - fs.createReadStream(rangeFile, {start: 2}); - assert.fail('Creating a ReadStream with a only one range limits must throw.'); -} catch (e) { - assert.equal(e.message, 'Both start and end are needed for range streaming.'); -} + +assert.throws(function() { + fs.createReadStream(rangeFile, {start: 10, end: 2}); +}, /start must be <= end/); var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 }); stream.data = '';