From 274ae6908875b15935e3ea9e1f6d968ab7e007ac Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Wed, 4 Nov 2026 20:57:48 +0900 Subject: [PATCH] =?UTF-8?q?perf(browser-transfer):=20=EC=9D=B4=EC=96=B4=20?= =?UTF-8?q?=EC=98=AC=EB=A6=AC=EB=8A=94=20=EC=8A=A4=ED=8A=B8=EB=A6=BC=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=EB=8A=94=20=EB=B3=B4=EB=82=BC=20par?= =?UTF-8?q?t=EB=A7=8C=20=ED=95=B4=EC=8B=9C=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: FILE_BYTE_SOURCE로 이어 올리면 runtime이 서버가 이미 받은 part까지 SHA-256으로 다시 검증한다. 검증(verifyUploadPartBytes)이 수락 확인보다 앞에 있기 때문이다. 8 part 중 6개가 수락된 뒤 이어 올리면 part 해시가 16번 돈다. RANGE_READER 경로는 같은 상황에서 10번이다(manifest 8 + 보낼 part 2). 고친 방법: 스트림 경로에서 수락된 part는 검증 전에 건너뛴다. 보내지 않는 part의 바이트는 검증할 필요가 없다. 파일이 checkpoint와 같은지는 그 앞의 manifest 계산이 이미 확인한다(다른 파일이면 STALE_RESULT). part의 offset·길이 확인은 그대로 모든 part에 한다. 위험·한계: 스트림은 여전히 처음부터 끝까지 읽는다. 읽기를 건너뛰는 것은 스트림 모양상 할 수 없다. 검증: - 새 테스트(8 part 중 6개 수락 뒤 FILE 재개, part 크기 digest 수): 고치기 전 16, 고친 뒤 10 - 영역 단위 테스트 15파일 275건 통과 - check:types(test), eslint(바꾼 파일) 통과 Refs: docs/reviews/2026-11-03-maturity-review/01-adapters-browser-transfer.md 1101/01-02 --- .../resumable-upload-runtime.ts | 4 +- .../resumable-upload-runtime-recovery.test.ts | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) 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[] = [];