Skip to content

Commit

Permalink
Fixed fs.ReadStream() start: 0 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tj authored and ry committed Sep 22, 2010
1 parent e227441 commit 893ebe7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ var ReadStream = fs.ReadStream = function(path, options) {
this[key] = options[key];
}

if (this.start || this.end) {
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.'));
Expand Down Expand Up @@ -671,7 +671,7 @@ ReadStream.prototype._read = function () {
allocNewPool();
}

if(this.start && this.firstRead) {
if (this.start !== undefined && this.firstRead) {
this.pos = this.start;
this.firstRead = false;
}
Expand All @@ -683,7 +683,7 @@ ReadStream.prototype._read = function () {
var toRead = Math.min(pool.length - pool.used, this.bufferSize);
var start = pool.used;

if(this.pos) {
if (this.pos !== undefined) {
toRead = Math.min(this.end - this.pos + 1, toRead);
}

Expand Down Expand Up @@ -721,7 +721,7 @@ ReadStream.prototype._read = function () {

fs.read(self.fd, pool, pool.used, toRead, this.pos, afterRead);

if(self.pos) {
if (self.pos !== undefined) {
self.pos += toRead;
}
pool.used += toRead;
Expand Down
11 changes: 11 additions & 0 deletions test/simple/test-fs-read-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,14 @@ try {
} catch(e) {
assert.equal(e.message, 'Both start and end are needed for range streaming.');
}

var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
stream.data = '';

stream.on('data', function(chunk){
stream.data += chunk;
});

stream.on('end', function(){
assert.equal('x', stream.data);
});

0 comments on commit 893ebe7

Please sign in to comment.