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
11 changes: 8 additions & 3 deletions lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,17 @@ export class Storage {
return Promise.all(this.mergeStreamPromises)
}

/**
* `false` when the upload does not exist, or was deleted while the part was streaming
* (the written part is then Orphaned Storage, ADR-0001).
*/
async uploadPart(uploadId: number, partIndex: number, stream: Readable) {
const upload = await this.db
.selectFrom('uploads')
.where('id', '=', uploadId)
.select(['folderName'])
.executeTakeFirst()
if (!upload) return
if (!upload) return false

await this.db
.updateTable('uploads')
Expand All @@ -395,14 +399,15 @@ export class Storage {

await this.adapter.uploadStream(`${upload.folderName}/parts/${partIndex}`, stream)

await this.db
const { numUpdatedRows } = await this.db
.updateTable('uploads')
.set({
lastPartUploadedAt: Date.now(),
finishedPartUploadCount: sql`${sql.ref('finishedPartUploadCount')} + 1`,
})
.where('id', '=', uploadId)
.execute()
.executeTakeFirst()
return numUpdatedRows > 0n
}

async completeUpload(reservation: Reservation) {
Expand Down
7 changes: 6 additions & 1 deletion routes/devstoreaccount1/upload/[uploadId].put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export default defineEventHandler(async (event) => {
// Not h3's `getRequestWebStream`: it enqueues every `data` event and never pauses the
// request, so a body arriving faster than storage consumes it is buffered in full.
const storage = await getStorage()
await storage.uploadPart(uploadId, chunkIndex, event.node.req)
const stored = await storage.uploadPart(uploadId, chunkIndex, event.node.req)
if (!stored)
throw createError({
statusCode: 404,
statusMessage: `Upload not found: ${uploadId}`,
})

// prevent random EOF error with in tonistiigi/go-actions-cache caused by missing request id
setHeader(event, 'x-ms-request-id', randomUUID())
Expand Down
44 changes: 44 additions & 0 deletions tests/upload-part.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { randomUUID } from 'node:crypto'
import { Readable } from 'node:stream'

import { afterEach, expect, test, vi } from 'vitest'
import { getDatabase } from '~/lib/db'
import { getStorage } from '~/lib/storage'

// `generateNumberId` never yields a negative id
const UNKNOWN_UPLOAD_ID = -1

afterEach(() => vi.restoreAllMocks())

test('a block PUT for an unknown upload answers 404', async () => {
const res = await fetch(`http://localhost:3000/devstoreaccount1/upload/${UNKNOWN_UPLOAD_ID}`, {
method: 'PUT',
body: 'payload',
})
expect(res.status).toBe(404)
})

test('a part for an unknown upload is not written', async () => {
const storage = await getStorage()
const uploadStream = vi.spyOn(storage.adapter, 'uploadStream')

expect(await storage.uploadPart(UNKNOWN_UPLOAD_ID, 0, Readable.from('payload'))).toBe(false)
expect(uploadStream).not.toHaveBeenCalled()
})

test('a part whose upload is deleted mid-stream reports failure', async () => {
const [storage, db] = await Promise.all([getStorage(), getDatabase()])
const upload = await storage.createUpload({
key: `upload-part-${randomUUID()}`,
version: 'v1',
scope: 'refs/heads/main',
repoId: '123',
})
// `cleanup:uploads` removing the row mid-part
vi.spyOn(storage.adapter, 'uploadStream').mockImplementation(async (_name, stream) => {
await db.deleteFrom('uploads').where('id', '=', upload.id).execute()
for await (const _chunk of stream);
})

expect(await storage.uploadPart(upload.id, 0, Readable.from('payload'))).toBe(false)
})
Loading