diff --git a/src/adapters/browser-transfer/resumable-upload/resumable-upload-runtime.ts b/src/adapters/browser-transfer/resumable-upload/resumable-upload-runtime.ts index ac6cbb33..9599873d 100644 --- a/src/adapters/browser-transfer/resumable-upload/resumable-upload-runtime.ts +++ b/src/adapters/browser-transfer/resumable-upload/resumable-upload-runtime.ts @@ -879,6 +879,9 @@ async function transferMissingParts( { recovery: "RESELECT" }, ); } + // 1101/01-02. An accepted part is not sent, so it is not hashed either; + // the manifest above already checked the file against the checkpoint. + if (accepted.has(part.partNumber)) continue; const verified = await verifyUploadPartBytes({ bytes: partResult.value.bytes, part, @@ -886,7 +889,6 @@ async function transferMissingParts( signal: request.signal, }); if (!verified.ok) return verified; - if (accepted.has(part.partNumber)) continue; const attempts = { count: 0 }; const uploaded = await uploadPartWithRetry( dependencies, diff --git a/tests/unit/resumable-upload-runtime-recovery.test.ts b/tests/unit/resumable-upload-runtime-recovery.test.ts index 29a8a843..5f0f8f27 100644 --- a/tests/unit/resumable-upload-runtime-recovery.test.ts +++ b/tests/unit/resumable-upload-runtime-recovery.test.ts @@ -1321,6 +1321,93 @@ describe("resume after a policy change (01-08)", () => { }); }); +/** + * 1101/01-02 (2026-11-03 리뷰 2절). A resumed FILE_BYTE_SOURCE upload reads + * the whole stream again, but hashes only the parts it still has to send, + * as the RANGE_READER path already does. The manifest hashes every part once. + */ +describe("resumed stream upload hashing (1101/01-02)", () => { + const PARTS = Array.from({ length: 8 }, (_, index) => + Uint8Array.from([1, 2, 3, 4], (offset) => index * 4 + offset), + ); + const FILE = Uint8Array.from(PARTS.flatMap((part) => [...part])); + + const streamSource = () => + Object.freeze({ + kind: "FILE_BYTE_SOURCE" as const, + bytes: Object.freeze({ + byteLength: FILE.byteLength, + async *stream() { + for (let offset = 0; offset < FILE.byteLength; offset += 5) { + yield browserDataSuccess(FILE.slice(offset, offset + 5)); + } + }, + }), + }); + + /** Counts SHA-256 digests whose input is exactly one part's bytes. */ + function countingCrypto() { + let partDigests = 0; + const isPart = (data: Uint8Array) => + PARTS.some( + (part) => + part.byteLength === data.byteLength && + part.every((byte, index) => byte === data[index]), + ); + const counting = { + subtle: { + async digest(algorithm: AlgorithmIdentifier, data: Uint8Array) { + if (isPart(data)) partDigests += 1; + return await crypto.subtle.digest(algorithm, data); + }, + }, + } as unknown as Crypto; + return { crypto: counting, partDigests: () => partDigests }; + } + + it("hashes only the two parts the server has not accepted", async () => { + const harness = createControlHarness(); + const checkpoints = new MemoryCheckpointStore(); + const key = "upload_key_stream_resume_hash"; + const partial = await createResumableUploadRuntime({ + controlPlane: harness.control, + partExecutor: { + async uploadPart(input) { + return input.part.partNumber >= 7 + ? browserDataFailure("UNAVAILABLE", "UPLOAD_PART", { + recovery: "RESUME", + }) + : await executorFor(harness).uploadPart(input); + }, + }, + checkpoints, + mutationLock: noContentionLock, + crypto, + policy: runtimePolicy({ maxRetries: 0 }), + now: () => 1_000, + sleep: async () => {}, + }).upload({ ...request(key), source: streamSource() }); + expect(partial.ok).toBe(false); + expect(harness.accepted.size).toBe(6); + + const counted = countingCrypto(); + const resumed = await createResumableUploadRuntime({ + controlPlane: harness.control, + partExecutor: executorFor(harness), + checkpoints, + mutationLock: noContentionLock, + crypto: counted.crypto, + policy: runtimePolicy({ maxRetries: 0 }), + now: () => 1_000, + sleep: async () => {}, + }).upload({ ...request(key), source: streamSource() }); + + expect(resumed).toMatchObject({ ok: true, value: { state: "QUARANTINED" } }); + // 8 for the manifest, 2 for the parts sent. The old order made it 16. + expect(counted.partDigests()).toBe(10); + }); +}); + describe("upload part observation (01-10)", () => { it("records a part that succeeded on its second attempt as one UPLOAD_PART", async () => { const observations: BrowserDataObservation[] = [];