From 2119a1a23850ac8b3a0bef9da0e45540ace87897 Mon Sep 17 00:00:00 2001 From: Louis Haftmann <30736553+LouisHaftmann@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:09:09 +0200 Subject: [PATCH 1/2] fix(upload): 404 for a block whose upload no longer exists Closes #279 Co-Authored-By: Claude Opus 5.5 --- lib/storage.ts | 8 ++-- .../devstoreaccount1/upload/[uploadId].put.ts | 3 +- tests/upload-part.test.ts | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 tests/upload-part.test.ts diff --git a/lib/storage.ts b/lib/storage.ts index 0c909ac..002433c 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -377,13 +377,14 @@ export class Storage { return Promise.all(this.mergeStreamPromises) } + /** `false` when the upload does not exist or was deleted while the part was streaming. */ 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') @@ -395,14 +396,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) { diff --git a/routes/devstoreaccount1/upload/[uploadId].put.ts b/routes/devstoreaccount1/upload/[uploadId].put.ts index ebe3ac9..5c2db5d 100644 --- a/routes/devstoreaccount1/upload/[uploadId].put.ts +++ b/routes/devstoreaccount1/upload/[uploadId].put.ts @@ -38,7 +38,8 @@ 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) + if (!(await storage.uploadPart(uploadId, chunkIndex, event.node.req))) + throw createError({ statusCode: 404, message: `Upload ${uploadId} not found` }) // prevent random EOF error with in tonistiigi/go-actions-cache caused by missing request id setHeader(event, 'x-ms-request-id', randomUUID()) diff --git a/tests/upload-part.test.ts b/tests/upload-part.test.ts new file mode 100644 index 0000000..f95823e --- /dev/null +++ b/tests/upload-part.test.ts @@ -0,0 +1,42 @@ +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' + +// one past the 10-digit range `generateNumberId` draws from +const UNKNOWN_UPLOAD_ID = 10_000_000_000 + +afterEach(() => vi.restoreAllMocks()) + +test('a block for an unknown upload is answered with 404 and not stored', async () => { + const storage = await getStorage() + const uploadStream = vi.spyOn(storage.adapter, 'uploadStream') + + const res = await fetch(`http://localhost:3000/devstoreaccount1/upload/${UNKNOWN_UPLOAD_ID}`, { + method: 'PUT', + body: 'payload', + }) + expect(res.status).toBe(404) + + expect(await storage.uploadPart(UNKNOWN_UPLOAD_ID, 0, Readable.from('payload'))).toBe(false) + expect(uploadStream).not.toHaveBeenCalled() +}) + +test('a block whose upload is deleted while it streams is reported as not stored', 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-block + 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) +}) From 9da54698fb81760494a5a0a48ecb7db471d7feab Mon Sep 17 00:00:00 2001 From: Louis Haftmann <30736553+LouisHaftmann@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:11:01 +0200 Subject: [PATCH 2/2] test(upload): split unknown-upload checks, use an id that cannot exist Co-Authored-By: Claude Opus 5.5 --- lib/storage.ts | 5 ++++- .../devstoreaccount1/upload/[uploadId].put.ts | 8 ++++++-- tests/upload-part.test.ts | 18 ++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/storage.ts b/lib/storage.ts index 002433c..d5078a2 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -377,7 +377,10 @@ export class Storage { return Promise.all(this.mergeStreamPromises) } - /** `false` when the upload does not exist or was deleted while the part was streaming. */ + /** + * `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') diff --git a/routes/devstoreaccount1/upload/[uploadId].put.ts b/routes/devstoreaccount1/upload/[uploadId].put.ts index 5c2db5d..9eb7fdd 100644 --- a/routes/devstoreaccount1/upload/[uploadId].put.ts +++ b/routes/devstoreaccount1/upload/[uploadId].put.ts @@ -38,8 +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() - if (!(await storage.uploadPart(uploadId, chunkIndex, event.node.req))) - throw createError({ statusCode: 404, message: `Upload ${uploadId} not found` }) + 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()) diff --git a/tests/upload-part.test.ts b/tests/upload-part.test.ts index f95823e..29bdefe 100644 --- a/tests/upload-part.test.ts +++ b/tests/upload-part.test.ts @@ -5,26 +5,28 @@ import { afterEach, expect, test, vi } from 'vitest' import { getDatabase } from '~/lib/db' import { getStorage } from '~/lib/storage' -// one past the 10-digit range `generateNumberId` draws from -const UNKNOWN_UPLOAD_ID = 10_000_000_000 +// `generateNumberId` never yields a negative id +const UNKNOWN_UPLOAD_ID = -1 afterEach(() => vi.restoreAllMocks()) -test('a block for an unknown upload is answered with 404 and not stored', async () => { - const storage = await getStorage() - const uploadStream = vi.spyOn(storage.adapter, 'uploadStream') - +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 block whose upload is deleted while it streams is reported as not stored', async () => { +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()}`, @@ -32,7 +34,7 @@ test('a block whose upload is deleted while it streams is reported as not stored scope: 'refs/heads/main', repoId: '123', }) - // `cleanup:uploads` removing the row mid-block + // `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);