From b4af145e15207be0657ac42d74524ec568aa9202 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Fri, 23 Oct 2026 17:03:03 +0200 Subject: [PATCH 1/2] fix(download): ignore Range on an empty entry instead of answering 416 An empty entry has no satisfiable range. The filesystem and GCS adapters raise RangeNotSatisfiable for any Range on one, Azure for any start past 0, while RustFS (the S3 backend in CI) answers bytes=0- with an empty 200. The response therefore depended on the configured storage driver. The route now ignores Range when the entry is empty and sends an empty 200, which RFC 9110 section 14.2 allows. Claude-Session: https://claude.ai/code/session_01RdsMKiGYJmUC1RdLmCBsN2 (cherry picked from commit f053636f4631b8b0fe88dfbb4b96226a6f9493e8) --- routes/download/[locationId].ts | 8 ++++++++ tests/range-download.test.ts | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/routes/download/[locationId].ts b/routes/download/[locationId].ts index ddc4909..c31513c 100644 --- a/routes/download/[locationId].ts +++ b/routes/download/[locationId].ts @@ -44,6 +44,14 @@ export default defineEventHandler(async (event) => { download = await storage.download(locationId, range) } catch (err) { if (!(err instanceof RangeNotSatisfiableError)) throw err + // An empty entry has no satisfiable range. The filesystem and GCS adapters raise + // for any Range on one and Azure for any start past 0, while RustFS (S3) answers + // `bytes=0-` with an empty 200. Ignore Range on empty entries (RFC 9110 ยง14.2) so + // the response does not depend on the storage driver. + if (err.size === 0) { + setHeader(event, 'content-length', 0) + return send(event) + } setResponseStatus(event, 416, 'Range Not Satisfiable') if (err.size !== undefined) setHeader(event, 'content-range', `bytes */${err.size}`) return send(event) diff --git a/tests/range-download.test.ts b/tests/range-download.test.ts index d971a10..6dbe819 100644 --- a/tests/range-download.test.ts +++ b/tests/range-download.test.ts @@ -125,6 +125,40 @@ describe('proxy download route on unmerged entries', () => { }) }) +test.each(['bytes=0-', 'bytes=5-'])( + 'an empty merged entry answers %s with an empty 200', + async (range) => { + const storage = await getStorage() + const key = `range-empty-${crypto.randomUUID()}` + const upload = await storage.createUpload({ + key, + version: VERSION, + scope: SCOPE, + repoId: REPO_ID, + }) + await storage.uploadPart(upload.id, 0, Readable.from(Buffer.alloc(0))) + await storage.completeUpload({ key, version: VERSION, scope: SCOPE, repoId: REPO_ID }) + const matched = await storage.matchCacheEntry({ + keys: [key], + version: VERSION, + scopes: [SCOPE], + repoId: REPO_ID, + }) + const id = matched!.match.locationId + const first = await storage.download(id) + for await (const _ of first!.stream) { + /* drain */ + } + await storage.waitForOngoingMerges() + + const { res, body } = await get(id, range) + expect(res.status).toBe(200) + expect(res.headers.get('content-range')).toBeNull() + expect(res.headers.get('content-length')).toBe('0') + expect(body).toHaveLength(0) + }, +) + test('a re-save does not change the bytes behind a handed-out URL', async () => { const { id, key } = await createEntry() const resaved = await createEntry(key) From c34bfd5e1e4ebb50f30edf6a55e83979a69b5202 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Fri, 23 Oct 2026 17:03:03 +0200 Subject: [PATCH 2/2] test(download): the route destroys the storage stream when the client hangs up Guards the client-abort half of #282: with sendStream an aborted download kept reading the backend and held its reader lease until it expired. Claude-Session: https://claude.ai/code/session_01RdsMKiGYJmUC1RdLmCBsN2 (cherry picked from commit c8f7c937968acf2f3c0cb42023a584bc34b78b5b) --- tests/proxy-backpressure.test.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/proxy-backpressure.test.ts b/tests/proxy-backpressure.test.ts index 2fc5af7..9a7ff4f 100644 --- a/tests/proxy-backpressure.test.ts +++ b/tests/proxy-backpressure.test.ts @@ -147,4 +147,24 @@ describe('proxy routes apply backpressure', () => { .toBeLessThan(MAX_BUFFERED_BYTES) expect.soft(queued, 'bytes queued in the response').toBeLessThan(MAX_BUFFERED_BYTES) }, 60_000) + + test('download destroys the storage stream when the client hangs up', async () => { + // Never ends on its own, so only the route tearing it down can stop the read + // (and release the reader lease that `Storage.download` ties to `close`). + function* endless() { + while (true) yield Buffer.alloc(CHUNK_BYTES) + } + const source = Readable.from(endless(), { objectMode: false, highWaterMark: CHUNK_BYTES }) + const storage = await getStorage() + vi.spyOn(storage, 'download').mockResolvedValue({ stream: source }) + + const { port } = server.address() as AddressInfo + const req = http.get(`http://localhost:${port}/download/${randomUUID()}`) + req.on('error', () => undefined) + const [res] = (await once(req, 'response')) as [IncomingMessage] + expect(res.statusCode).toBe(200) + res.once('data', () => req.destroy()) + + await vi.waitFor(() => expect(source.destroyed).toBe(true), { timeout: 5000, interval: 50 }) + }, 20_000) })