Skip to content

Commit

Permalink
Add "options" argument to req.range
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 1, 2016
1 parent 7fcf1d7 commit 5d642af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased
==========

* Add `options` argument to `req.range`
- Includes the `combine` option
* Fix Windows absolute path check using forward slashes
* Improve performance for `res.json`/`res.jsonp` in most cases
* deps: accepts@~1.3.3
Expand Down
9 changes: 7 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,23 @@ req.acceptsLanguage = deprecate.function(req.acceptsLanguages,
* range that is required (most commonly, "bytes"). Each array element is an object
* with a "start" and "end" property for the portion of the range.
*
* The "combine" option can be set to `true` and overlapping & adjacent ranges
* will be combined into a single range.
*
* NOTE: remember that ranges are inclusive, so for example "Range: users=0-3"
* should respond with 4 users when available, not 3.
*
* @param {number} size
* @param {object} [options]
* @param {boolean} [options.combine=false]
* @return {number|array}
* @public
*/

req.range = function range(size) {
req.range = function range(size, options) {
var range = this.get('Range');
if (!range) return;
return parseRange(size, range);
return parseRange(size, range, options);
};

/**
Expand Down
16 changes: 13 additions & 3 deletions test/req.range.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function req(ret) {
describe('req', function(){
describe('.range(size)', function(){
it('should return parsed ranges', function(){
var ret = [{ start: 0, end: 50 }, { start: 60, end: 100 }];
ret.type = 'bytes';
req('bytes=0-50,60-100').range(120).should.eql(ret);
var ranges = [{ start: 0, end: 50 }, { start: 51, end: 100 }]
ranges.type = 'bytes'
assert.deepEqual(req('bytes=0-50,51-100').range(120), ranges)
})

it('should cap to the given size', function(){
Expand All @@ -35,4 +35,14 @@ describe('req', function(){
assert(req('').range(120) === undefined);
})
})

describe('.range(size, options)', function(){
describe('with "combine: true" option', function(){
it('should return combined ranges', function(){
var ranges = [{ start: 0, end: 100 }]
ranges.type = 'bytes'
assert.deepEqual(req('bytes=0-50,51-100').range(120, { combine: true }), ranges)
})
})
})
})

0 comments on commit 5d642af

Please sign in to comment.