refactor(highway): introduce ChunkSource for uploadHighwayHttp (phase 1)

Prep for streaming large media uploads from disk instead of buffering the
whole file in RAM. Add a `ChunkSource` abstraction — `BufferChunkSource`
(in-memory, byte-for-byte the old path) and `FileChunkSource` (disk-backed,
reads exact-length chunks via a FileHandle, loops on short reads, throws on
unexpected EOF) — and change `uploadHighwayHttp` to consume a `ChunkSource`
instead of a `Uint8Array`. The uploader now owns the source and closes it once
in `finally` (best-effort; never masks a primary upload error).

Pure structural refactor: no streaming is wired yet. All five call sites
(pipeline runPuts, group + private file upload, personal + group avatar) wrap
their bytes in `BufferChunkSource`, so behavior is unchanged. Adds a
byte-oracle test proving FileChunkSource ≡ BufferChunkSource across block
boundaries, plus short-read/EOF/empty-input coverage.
This commit is contained in:
DrSmoothl committed 2026-07-05 02:29:13 +08:00
1 parent c236bc86ee
commit 5324e2f014
11 files changed
+276 -83

No files matched your search

@@ -153,9 +153,12 @@ describe('runNtv2Upload — two images in one message (sequential)', () => {
// The PUT receives image-2's bytes, image-2's md5, and the
// image-2-derived extend (NOT image-1's data).
const [, , cmdId, putBytes, putMd5, putExtend] = vi.mocked(highway.uploadHighwayHttp).mock.calls[0]!;
const [, , cmdId, putSource, putMd5, putExtend] = vi.mocked(highway.uploadHighwayHttp).mock.calls[0]!;
expect(cmdId).toBe(1004);
expect(putBytes).toBe(image2Bytes);
// The PUT source is a ChunkSource wrapping image-2's bytes — verify it
// yields image-2 exactly (not image-1) through its public read().
expect(putSource.size).toBe(image2Bytes.length);
expect(Buffer.from(await putSource.read(0, putSource.size)).equals(Buffer.from(image2Bytes))).toBe(true);
expect(putMd5).toBe(image2Md5);
// Decode the extend bytes; uKey + fileUuid must come from image-2's