|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#22313] A chunked upload completes with the file it was sent, or not at all — |
| 4 | +// over the HTTP doors of a REAL boot. |
| 5 | +// |
| 6 | +// The package suite (`service-storage/src/chunked-upload-integrity.test.ts`) |
| 7 | +// pins every rule at the route handlers. This file drives the four chunked |
| 8 | +// doors the way the SDK's `resumeUpload` drives them (`packages/client`): |
| 9 | +// progress first, then the chunks from `uploadedChunks` on, then a completion |
| 10 | +// listing only what that pass sent — and reads the stored object back through |
| 11 | +// the download door, because "200 with the declared size" is exactly what the |
| 12 | +// short file looked like. |
| 13 | +// |
| 14 | +// Not eligible for the shared showcase project: it boots its own storage root. |
| 15 | + |
| 16 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 17 | +import { mkdtempSync } from 'node:fs'; |
| 18 | +import { join } from 'node:path'; |
| 19 | +import { tmpdir } from 'node:os'; |
| 20 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 21 | +import { StorageServicePlugin } from '@objectstack/service-storage'; |
| 22 | +import { attachmentsFixtureStack, attachmentsFixtureSecurity } from './fixtures/attachments-fixture.js'; |
| 23 | + |
| 24 | +const MIB = 1024 * 1024; |
| 25 | +/** The chunk size the init door floors every upload at. */ |
| 26 | +const MIN_CHUNK = 5 * MIB; |
| 27 | + |
| 28 | +describe('[#22313] a chunked upload completes with the file it was sent, over a real boot', () => { |
| 29 | + let stack: VerifyStack; |
| 30 | + let token: string; |
| 31 | + |
| 32 | + const json = { 'Content-Type': 'application/json' }; |
| 33 | + const auth = () => ({ Authorization: `Bearer ${token}` }); |
| 34 | + |
| 35 | + const init = async (totalSize: number) => { |
| 36 | + const res = await stack.api('/storage/upload/chunked', { |
| 37 | + method: 'POST', |
| 38 | + headers: { ...json, ...auth() }, |
| 39 | + body: JSON.stringify({ filename: 'resume.bin', mimeType: 'application/octet-stream', totalSize }), |
| 40 | + }); |
| 41 | + expect(res.status, await res.clone().text()).toBe(200); |
| 42 | + return ((await res.json()) as any).data as { uploadId: string; resumeToken: string; fileId: string; totalChunks: number }; |
| 43 | + }; |
| 44 | + |
| 45 | + const putChunk = async (uploadId: string, resumeToken: string, chunkIndex: number, bytes: Uint8Array<ArrayBuffer>) => { |
| 46 | + const res = await stack.api(`/storage/upload/chunked/${uploadId}/chunk/${chunkIndex}`, { |
| 47 | + method: 'PUT', |
| 48 | + headers: { ...auth(), 'Content-Type': 'application/octet-stream', 'x-resume-token': resumeToken }, |
| 49 | + body: bytes, |
| 50 | + }); |
| 51 | + expect(res.status, await res.clone().text()).toBe(200); |
| 52 | + return ((await res.json()) as any).data.eTag as string; |
| 53 | + }; |
| 54 | + |
| 55 | + const progress = async (uploadId: string) => { |
| 56 | + const res = await stack.api(`/storage/upload/chunked/${uploadId}/progress`, { headers: auth() }); |
| 57 | + expect(res.status, await res.clone().text()).toBe(200); |
| 58 | + return ((await res.json()) as any).data as Record<string, unknown>; |
| 59 | + }; |
| 60 | + |
| 61 | + const complete = (uploadId: string, parts: Array<{ chunkIndex: number; eTag: string }>) => |
| 62 | + stack.api(`/storage/upload/chunked/${uploadId}/complete`, { |
| 63 | + method: 'POST', |
| 64 | + headers: { ...json, ...auth() }, |
| 65 | + body: JSON.stringify({ uploadId, parts }), |
| 66 | + }); |
| 67 | + |
| 68 | + /** The stored object, read back through the public download door. */ |
| 69 | + const download = async (fileId: string): Promise<Uint8Array> => { |
| 70 | + const res = await stack.api(`/storage/files/${fileId}/url`, { headers: auth() }); |
| 71 | + expect(res.status, await res.clone().text()).toBe(200); |
| 72 | + const url = String(((await res.json()) as any).data.url).replace(/^https?:\/\/[^/]+/, ''); |
| 73 | + const bytes = await stack.raw(url); |
| 74 | + expect(bytes.status).toBe(200); |
| 75 | + return new Uint8Array(await bytes.arrayBuffer()); |
| 76 | + }; |
| 77 | + |
| 78 | + const fill = (byte: number, n: number) => new Uint8Array(n).fill(byte); |
| 79 | + const concat = (a: Uint8Array, b: Uint8Array) => { |
| 80 | + const out = new Uint8Array(a.length + b.length); |
| 81 | + out.set(a, 0); |
| 82 | + out.set(b, a.length); |
| 83 | + return out; |
| 84 | + }; |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + const rootDir = mkdtempSync(join(tmpdir(), 'chunked-resume-dogfood-')); |
| 88 | + stack = await bootStack(attachmentsFixtureStack as never, { |
| 89 | + security: attachmentsFixtureSecurity(), |
| 90 | + // `bindToSettings:false` keeps the constructor rootDir; the size limit is |
| 91 | + // not what this file is about. |
| 92 | + extraPlugins: [new StorageServicePlugin({ adapter: 'local', local: { rootDir }, bindToSettings: false })], |
| 93 | + }); |
| 94 | + token = await stack.signIn(); |
| 95 | + }, 120_000); |
| 96 | + |
| 97 | + afterAll(async () => { |
| 98 | + await stack?.stop(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a RESUMED upload, driven as resumeUpload drives it, completes with the FULL file', async () => { |
| 102 | + const chunk0 = fill(0x61, MIN_CHUNK); |
| 103 | + const chunk1 = fill(0x62, 10); |
| 104 | + const { uploadId, resumeToken, fileId, totalChunks } = await init(MIN_CHUNK + 10); |
| 105 | + expect(totalChunks).toBe(2); |
| 106 | + |
| 107 | + // First pass: chunk 0 lands, then the client goes away. |
| 108 | + await putChunk(uploadId, resumeToken, 0, chunk0); |
| 109 | + |
| 110 | + // The resume: progress, the chunks from `uploadedChunks` on, and a |
| 111 | + // completion listing only the part this pass sent. |
| 112 | + const before = await progress(uploadId); |
| 113 | + expect(before.uploadedChunks).toBe(1); |
| 114 | + const eTag1 = await putChunk(uploadId, resumeToken, 1, chunk1); |
| 115 | + const res = await complete(uploadId, [{ chunkIndex: 1, eTag: eTag1 }]); |
| 116 | + |
| 117 | + expect(res.status, await res.clone().text()).toBe(200); |
| 118 | + expect(((await res.json()) as any).data.size).toBe(MIN_CHUNK + 10); |
| 119 | + const stored = await download(fileId); |
| 120 | + expect(stored.length, 'the stored object is the whole file, not the resumed chunk alone').toBe(MIN_CHUNK + 10); |
| 121 | + expect(Buffer.from(stored).equals(Buffer.from(concat(chunk0, chunk1)))).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it('REFUSED: an upload that does not hold a declared chunk is refused 409 RESOURCE_CONFLICT naming it', async () => { |
| 125 | + const { uploadId, resumeToken } = await init(MIN_CHUNK + 10); |
| 126 | + const eTag1 = await putChunk(uploadId, resumeToken, 1, fill(0x62, 10)); |
| 127 | + |
| 128 | + const res = await complete(uploadId, [{ chunkIndex: 1, eTag: eTag1 }]); |
| 129 | + |
| 130 | + expect(res.status, await res.clone().text()).toBe(409); |
| 131 | + const body = (await res.json()) as any; |
| 132 | + expect(body.success).toBe(false); |
| 133 | + expect(body.error.code).toBe('RESOURCE_CONFLICT'); |
| 134 | + expect(body.error.details.missingChunks).toEqual([0]); |
| 135 | + // Still resumable: nothing was assembled and the session is in flight. |
| 136 | + expect((await progress(uploadId)).status).toBe('in_progress'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('a RE-SENT chunk leaves the progress unchanged', async () => { |
| 140 | + const { uploadId, resumeToken } = await init(MIB); |
| 141 | + const chunk = fill(0x61, 600 * 1024); |
| 142 | + |
| 143 | + await putChunk(uploadId, resumeToken, 0, chunk); |
| 144 | + const once = await progress(uploadId); |
| 145 | + await putChunk(uploadId, resumeToken, 0, chunk); |
| 146 | + const twice = await progress(uploadId); |
| 147 | + |
| 148 | + expect(once).toMatchObject({ uploadedSize: 600 * 1024, uploadedChunks: 1, percentComplete: 59 }); |
| 149 | + expect({ uploadedSize: twice.uploadedSize, uploadedChunks: twice.uploadedChunks, percentComplete: twice.percentComplete }) |
| 150 | + .toEqual({ uploadedSize: once.uploadedSize, uploadedChunks: once.uploadedChunks, percentComplete: once.percentComplete }); |
| 151 | + }); |
| 152 | + |
| 153 | + it('CONTROL: a clean one-pass upload listing every part is unchanged', async () => { |
| 154 | + const chunk0 = fill(0x61, MIN_CHUNK); |
| 155 | + const chunk1 = fill(0x62, 10); |
| 156 | + const { uploadId, resumeToken, fileId } = await init(MIN_CHUNK + 10); |
| 157 | + const eTag0 = await putChunk(uploadId, resumeToken, 0, chunk0); |
| 158 | + const eTag1 = await putChunk(uploadId, resumeToken, 1, chunk1); |
| 159 | + |
| 160 | + const res = await complete(uploadId, [ |
| 161 | + { chunkIndex: 0, eTag: eTag0 }, |
| 162 | + { chunkIndex: 1, eTag: eTag1 }, |
| 163 | + ]); |
| 164 | + |
| 165 | + expect(res.status, await res.clone().text()).toBe(200); |
| 166 | + expect(((await res.json()) as any).data).toMatchObject({ fileId, size: MIN_CHUNK + 10 }); |
| 167 | + expect(Buffer.from(await download(fileId)).equals(Buffer.from(concat(chunk0, chunk1)))).toBe(true); |
| 168 | + }); |
| 169 | +}); |
0 commit comments