From d19702011a9725f7d1278fda75158ebe9c138d4c Mon Sep 17 00:00:00 2001 From: D Trombett Date: Tue, 12 Nov 2024 18:07:58 +0100 Subject: [PATCH] fix: range end is zero-indexed (#3826) * fix: range end is zero-indexed * tests: update tests for retry handler (cherry picked from commit 2b81fbc845cb0f7e8a621674a3c7aa5c59642daa) --- lib/handler/retry-handler.js | 6 +++--- test/interceptors/retry.js | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/handler/retry-handler.js b/lib/handler/retry-handler.js index e00e82faf08..5d1ccf00538 100644 --- a/lib/handler/retry-handler.js +++ b/lib/handler/retry-handler.js @@ -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') @@ -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' @@ -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)) diff --git a/test/interceptors/retry.js b/test/interceptors/retry.js index 6e6f997cafa..b510ddba6c0 100644 --- a/test/interceptors/retry.js +++ b/test/interceptors/retry.js @@ -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')