Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions routes/download/[locationId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/proxy-backpressure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
34 changes: 34 additions & 0 deletions tests/range-download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading