Skip to content
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function rangeParser (size, str, options) {
// split the range string
var arr = str.slice(index + 1).split(',')
var ranges = []
var code = -1

// add ranges type
ranges.type = str.slice(0, index)
Expand All @@ -46,7 +47,8 @@ function rangeParser (size, str, options) {
for (var i = 0; i < arr.length; i++) {
var indexOf = arr[i].indexOf('-')
if (indexOf === -1) {
return -2
code = -2
continue
}

var startStr = arr[i].slice(0, indexOf).trim()
Expand All @@ -67,11 +69,13 @@ function rangeParser (size, str, options) {
end = size - 1
}

// invalid format range
if (isNaN(start) || isNaN(end)) {
return -2
code = -2
continue
}

// invalid or unsatisifiable
// skip unsatisfiable ranges
if (start > end || start < 0) {
continue
}
Expand All @@ -84,8 +88,7 @@ function rangeParser (size, str, options) {
}

if (ranges.length < 1) {
// unsatisifiable
return -1
return code
}

return options && options.combine
Expand Down
56 changes: 55 additions & 1 deletion test/range-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var assert = require('assert')
var deepEqual = require('deep-equal')
var parse = require('..')
Expand All @@ -9,6 +8,15 @@ describe('parseRange(len, str)', function () {
/TypeError: argument str must be a string/)
})

it('should return -2 for completely empty header', function () {
assert.strictEqual(parse(200, ''), -2)
})

it('should return -2 for range missing dash', function () {
assert.strictEqual(parse(200, 'bytes=100200'), -2)
assert.strictEqual(parse(200, 'bytes=,100200'), -2)
})
Comment on lines 15 to 18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to get the coverage to 100%, it has nothing to do with my changes


it('should return -2 for invalid str', function () {
assert.strictEqual(parse(200, 'malformed'), -2)
})
Expand All @@ -28,6 +36,12 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes= - '), -2)
})

it('should return -2 for empty range value', function () {
assert.strictEqual(parse(200, 'bytes='), -2)
assert.strictEqual(parse(200, 'bytes=,'), -2)
assert.strictEqual(parse(200, 'bytes= , , '), -2)
})

it('should return -2 with multiple dashes in range', function () {
assert.strictEqual(parse(200, 'bytes=100-200-300'), -2)
})
Expand All @@ -41,6 +55,12 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes=100-15b0'), -2)
})

it('should return -2 when all multiple ranges have invalid format', function () {
assert.strictEqual(parse(200, 'bytes=y-v,x-'), -2)
assert.strictEqual(parse(200, 'bytes=abc-def,ghi-jkl'), -2)
assert.strictEqual(parse(200, 'bytes=x-,y-,z-'), -2)
})

it('should return -1 for unsatisfiable range', function () {
assert.strictEqual(parse(200, 'bytes=500-600'), -1)
})
Expand All @@ -55,6 +75,11 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes=500-999,1000-1499'), -1)
})

it('should return -1 when invalid format mixed with unsatisfiable ranges', function () {
assert.strictEqual(parse(200, 'bytes=500-600,900-'), -1)
assert.strictEqual(parse(200, 'bytes=900-,500-600'), -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto for this test, 500-600 is valid. I think this test is just invalid now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t it supposed to be out of range? And is that why it’s -1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a -1 this is already being tested above.

Copy link
Member

@blakeembrey blakeembrey Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your PR previously had it as -2. Misremembered, my edit changes it to a -2. I can go either way I guess, but I'd err to simplicity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test back, do you need invalid and unsatisfiable to return -1?

Copy link
Member Author

@bjohansebas bjohansebas Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should still return -1. There’s a valid range, but it exceeds the allowed bounds, so it should return -1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

})

it('should parse str', function () {
var range = parse(1000, 'bytes=0-499')
assert.strictEqual(range.type, 'bytes')
Expand Down Expand Up @@ -111,6 +136,28 @@ describe('parseRange(len, str)', function () {
deepEqual(range[0], { start: 999, end: 999 })
})

it('should ignore invalid format range when valid range exists', function () {
var range = parse(1000, 'bytes=100-200,x-')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 100, end: 200 })
})

it('should ignore invalid format ranges when some are valid', function () {
var range = parse(1000, 'bytes=x-,0-100,y-')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 0, end: 100 })
})

it('should ignore invalid format ranges at different positions', function () {
var range = parse(1000, 'bytes=0-50,abc-def,100-150')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 2)
deepEqual(range[0], { start: 0, end: 50 })
deepEqual(range[1], { start: 100, end: 150 })
})

it('should parse str with multiple ranges', function () {
var range = parse(1000, 'bytes=40-80,81-90,-1')
assert.strictEqual(range.type, 'bytes')
Expand Down Expand Up @@ -161,4 +208,11 @@ describe('parseRange(len, str)', function () {
deepEqual(range[2], { start: 0, end: 1 })
})
})

it('should ignore whitespace-only invalid ranges when valid present', function () {
var range = parse(1000, 'bytes= , 0-10')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 0, end: 10 })
})
})
Loading