Skip to content

Commit 1a7737b

Browse files
Merge pull request #416 from ryanseys/fix-storage-range
Accept 0 for start and end of storage range requests
2 parents fe10aa5 + daa84fc commit 1a7737b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/storage/file.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ File.prototype.copy = function(destination, callback) {
235235
* with less reliability. You may also choose to skip validation completely,
236236
* however this is **not recommended**.
237237
* @param {number} options.start - A byte offset to begin the file's download
238-
* from. NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
239-
* `options.end = 999` represent the first 1000 bytes in a file or object.
240-
* NOTE: when specifying a byte range, data integrity is not available.
238+
* from. Default is 0. NOTE: Byte ranges are inclusive; that is,
239+
* `options.start = 0` and `options.end = 999` represent the first 1000
240+
* bytes in a file or object. NOTE: when specifying a byte range, data
241+
* integrity is not available.
241242
* @param {number} options.end - A byte offset to stop reading the file at.
242243
* NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
243244
* `options.end = 999` represent the first 1000 bytes in a file or object.
@@ -325,8 +326,10 @@ File.prototype.createReadStream = function(options) {
325326
};
326327

327328
if (rangeRequest) {
329+
var start = util.is(options.start, 'number') ? options.start : '0';
330+
var end = util.is(options.end, 'number') ? options.end : '';
328331
reqOpts.headers = {
329-
Range: 'bytes=' + [options.start || '', options.end || ''].join('-')
332+
Range: 'bytes=' + start + '-' + end
330333
};
331334
}
332335

test/storage/file.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,12 @@ describe('File', function() {
454454
file.createReadStream({ start: startOffset });
455455
});
456456

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

460460
request_Override = function(opts) {
461461
setImmediate(function () {
462-
assert.equal(opts.headers.Range, 'bytes=-' + endOffset);
462+
assert.equal(opts.headers.Range, 'bytes=0-' + endOffset);
463463
done();
464464
});
465465
return duplexify();
@@ -485,6 +485,23 @@ describe('File', function() {
485485
file.metadata = metadata;
486486
file.createReadStream({ start: startOffset, end: endOffset });
487487
});
488+
489+
it('should accept range start and end as 0', function(done) {
490+
var startOffset = 0;
491+
var endOffset = 0;
492+
493+
request_Override = function(opts) {
494+
setImmediate(function () {
495+
var expectedRange = 'bytes=0-0';
496+
assert.equal(opts.headers.Range, expectedRange);
497+
done();
498+
});
499+
return duplexify();
500+
};
501+
502+
file.metadata = metadata;
503+
file.createReadStream({ start: startOffset, end: endOffset });
504+
});
488505
});
489506

490507
describe('createWriteStream', function() {

0 commit comments

Comments
 (0)