Browse Source

Fixed fs.ReadStream() start: 0 bug

v0.7.4-release
Tj Holowaychuk 14 years ago
committed by Ryan Dahl
parent
commit
893ebe7230
  1. 8
      lib/fs.js
  2. 11
      test/simple/test-fs-read-stream.js

8
lib/fs.js

@ -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.'));
@ -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;
}
@ -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);
}
@ -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;

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

@ -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);
});
Loading…
Cancel
Save