Skip to content

Commit

Permalink
Merge pull request #416 from ryanseys/fix-storage-range
Browse files Browse the repository at this point in the history
Accept 0 for start and end of storage range requests
  • Loading branch information
stephenplusplus committed Mar 3, 2015
2 parents fe10aa5 + daa84fc commit 1a7737b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ File.prototype.copy = function(destination, callback) {
* with less reliability. You may also choose to skip validation completely,
* however this is **not recommended**.
* @param {number} options.start - A byte offset to begin the file's download
* from. NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
* `options.end = 999` represent the first 1000 bytes in a file or object.
* NOTE: when specifying a byte range, data integrity is not available.
* from. Default is 0. NOTE: Byte ranges are inclusive; that is,
* `options.start = 0` and `options.end = 999` represent the first 1000
* bytes in a file or object. NOTE: when specifying a byte range, data
* integrity is not available.
* @param {number} options.end - A byte offset to stop reading the file at.
* NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
* `options.end = 999` represent the first 1000 bytes in a file or object.
Expand Down Expand Up @@ -325,8 +326,10 @@ File.prototype.createReadStream = function(options) {
};

if (rangeRequest) {
var start = util.is(options.start, 'number') ? options.start : '0';
var end = util.is(options.end, 'number') ? options.end : '';
reqOpts.headers = {
Range: 'bytes=' + [options.start || '', options.end || ''].join('-')
Range: 'bytes=' + start + '-' + end
};
}

Expand Down
21 changes: 19 additions & 2 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ describe('File', function() {
file.createReadStream({ start: startOffset });
});

it('should accept an end range', function(done) {
it('should accept an end range and set start to 0', function(done) {
var endOffset = 100;

request_Override = function(opts) {
setImmediate(function () {
assert.equal(opts.headers.Range, 'bytes=-' + endOffset);
assert.equal(opts.headers.Range, 'bytes=0-' + endOffset);
done();
});
return duplexify();
Expand All @@ -485,6 +485,23 @@ describe('File', function() {
file.metadata = metadata;
file.createReadStream({ start: startOffset, end: endOffset });
});

it('should accept range start and end as 0', function(done) {
var startOffset = 0;
var endOffset = 0;

request_Override = function(opts) {
setImmediate(function () {
var expectedRange = 'bytes=0-0';
assert.equal(opts.headers.Range, expectedRange);
done();
});
return duplexify();
};

file.metadata = metadata;
file.createReadStream({ start: startOffset, end: endOffset });
});
});

describe('createWriteStream', function() {
Expand Down

0 comments on commit 1a7737b

Please sign in to comment.