Skip to content

Commit

Permalink
fix: range end is zero-indexed (nodejs#3826)
Browse files Browse the repository at this point in the history
* fix: range end is zero-indexed

* tests: update tests for retry handler
  • Loading branch information
DTrombett authored and flakey5 committed Nov 14, 2024
1 parent 3fd30d3 commit d639d91
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class RetryHandler {
return false
}

const { start, size, end = size } = contentRange
const { start, size, end = size - 1 } = contentRange

assert(this.start === start, 'content-range mismatch')
assert(this.end == null || this.end === end, 'content-range mismatch')
Expand All @@ -252,7 +252,7 @@ class RetryHandler {
)
}

const { start, size, end = size } = range
const { start, size, end = size - 1 } = range
assert(
start != null && Number.isFinite(start),
'content-range mismatch'
Expand All @@ -266,7 +266,7 @@ class RetryHandler {
// We make our best to checkpoint the body for further range headers
if (this.end == null) {
const contentLength = headers['content-length']
this.end = contentLength != null ? Number(contentLength) : null
this.end = contentLength != null ? Number(contentLength) - 1 : null
}

assert(Number.isFinite(this.start))
Expand Down
5 changes: 3 additions & 2 deletions test/interceptors/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,15 @@ test('Should handle 206 partial content', async t => {
const server = createServer((req, res) => {
if (x === 0) {
t.ok(true, 'pass')
res.setHeader('content-length', '6')
res.setHeader('etag', 'asd')
res.write('abc')
setTimeout(() => {
res.destroy()
}, 1e2)
} else if (x === 1) {
t.deepStrictEqual(req.headers.range, 'bytes=3-')
res.setHeader('content-range', 'bytes 3-6/6')
t.deepStrictEqual(req.headers.range, 'bytes=3-5')
res.setHeader('content-range', 'bytes 3-5/6')
res.setHeader('etag', 'asd')
res.statusCode = 206
res.end('def')
Expand Down

0 comments on commit d639d91

Please sign in to comment.