refactor(zstd): share frame read request construction (#16912)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 11:21:08 +08:00
1 parent 27c695b6c6
commit 54ea50acd3
1 file changed
+13 -33
+13 -33
View File
@@ -79,6 +79,13 @@ export const zstandardFrameKind = (
return status === "zstd" || status === "skippable" ? status : undefined;
};
const frameRead = (
length: number,
inspect: boolean,
eof: ZstandardTruncationReason,
failureOffset: number,
): ZstandardFrameRead => ({ length, inspect, eof, failureOffset });
/**
* RFC 8878 §3.1 frame extent after its magic. The caller supplies inspected
* fields and skips opaque bytes, which remain the native decoder's concern.
@@ -92,20 +99,11 @@ export function* zstandardFrameReads(
Uint8Array
> {
let cursor = offset + ZSTANDARD_MAGIC_BYTES;
const header = (length: number, inspect: boolean): ZstandardFrameRead => ({
length,
inspect,
eof: "truncated_frame_header",
failureOffset: offset,
});
const header = (length: number, inspect: boolean) =>
frameRead(length, inspect, "truncated_frame_header", offset);
if (kind === "skippable") {
const userSize = yield header(4, true);
yield {
length: u32(userSize, 0),
inspect: false,
eof: "truncated_block",
failureOffset: cursor + 4,
};
yield frameRead(u32(userSize, 0), false, "truncated_block", cursor + 4);
return undefined;
}
@@ -123,35 +121,17 @@ export function* zstandardFrameReads(
cursor += 1 + headerRest;
for (;;) {
const block = u24(
yield {
length: 3,
inspect: true,
eof: "truncated_block",
failureOffset: cursor,
},
0,
);
const block = u24(yield frameRead(3, true, "truncated_block", cursor), 0);
const type = (block >>> 1) & 3;
if (type === 3) return { code: "reserved_block", offset: cursor };
cursor += 3;
// An RLE block stores one byte and repeats it the declared number of times.
const length = type === 1 ? 1 : block >>> 3;
yield {
length,
inspect: false,
eof: "truncated_block",
failureOffset: cursor,
};
yield frameRead(length, false, "truncated_block", cursor);
cursor += length;
if (block & 1) break;
}
if (descriptor & 0x04)
yield {
length: 4,
inspect: false,
eof: "truncated_checksum",
failureOffset: cursor,
};
yield frameRead(4, false, "truncated_checksum", cursor);
return undefined;
}