refactor(unityfs): share compressed block decoding (#15490)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 14:56:03 +00:00
1 parent a260cac020
commit 54e6bbdd20
2 files changed
+17 -43

No files matched your search

+7 -6
View File
@@ -26,8 +26,7 @@ import {
UNITYFS_COMPRESSION_NONE,
} from "../domain/unityfsFormat.js";
import {
decodeUnityFsLz4,
decodeUnityFsLzma,
decodeUnityFsCompressedBlock,
readUnityFsBlocksInfo,
readUnityFsHeader,
UNITYFS_BLOCK_DECODED_BUDGET_BYTES,
@@ -396,10 +395,12 @@ export class UnityFsArtifactReader implements ArtifactReader {
this.#dataStart + block.compressedStart + block.compressedSize,
signal,
);
const bytes =
block.compression === UNITYFS_COMPRESSION_LZMA
? await decodeUnityFsLzma(encoded, block.uncompressedSize, signal)
: await decodeUnityFsLz4(encoded, block.uncompressedSize, signal);
const bytes = await decodeUnityFsCompressedBlock(
block.compression === UNITYFS_COMPRESSION_LZMA ? "LZMA" : "LZ4",
encoded,
block.uncompressedSize,
signal,
);
this.#assertOpen(signal);
this.#cachedBlock = { index, bytes };
return bytes;
+10 -37
View File
@@ -255,13 +255,15 @@ const decodeBlocksInfoTable = async (
compression === UNITYFS_COMPRESSION_LZ4 ||
compression === UNITYFS_COMPRESSION_LZ4HC
)
return decodeUnityFsLz4(
return decodeUnityFsCompressedBlock(
"LZ4",
compressed,
header.uncompressedBlocksInfoSize,
signal,
);
if (compression === UNITYFS_COMPRESSION_LZMA)
return decodeUnityFsLzma(
return decodeUnityFsCompressedBlock(
"LZMA",
compressed,
header.uncompressedBlocksInfoSize,
signal,
@@ -273,13 +275,15 @@ const decodeBlocksInfoTable = async (
};
/** Translate decoder failures once, without erasing cancellation or resource reasons. */
export const decodeUnityFsLz4 = async (
export const decodeUnityFsCompressedBlock = async (
codec: "LZ4" | "LZMA",
encoded: Buffer,
expected: number,
signal?: AbortSignal,
): Promise<Buffer> => {
const decode = codec === "LZ4" ? decodeLz4Block : decodeUnityLzmaBlock;
try {
return await decodeLz4Block(
return await decode(
encoded,
expected,
MAX_BLOCK_UNCOMPRESSED_BYTES,
@@ -290,44 +294,13 @@ export const decodeUnityFsLz4 = async (
if (cause instanceof BinaryResourceFailure)
throw new ArtifactReaderFailure(
"limit",
"UnityFS LZ4 exceeds the decoder memory budget",
`UnityFS ${codec} exceeds the decoder memory budget`,
{ cause, boundary: "memory" },
);
if (cause instanceof BinaryFormatFailure)
throw malformedArtifact(
"unityfs",
"UnityFS is malformed: LZ4 block does not decode to its declared size",
{ cause, field: "block" },
);
throw cause;
}
};
/** Translate LZMA decoder failures once, without erasing cancellation or resource reasons. */
export const decodeUnityFsLzma = async (
encoded: Buffer,
expected: number,
signal?: AbortSignal,
): Promise<Buffer> => {
try {
return await decodeUnityLzmaBlock(
encoded,
expected,
MAX_BLOCK_UNCOMPRESSED_BYTES,
signal,
);
} catch (cause: unknown) {
if (cause instanceof ArtifactReaderFailure) throw cause;
if (cause instanceof BinaryResourceFailure)
throw new ArtifactReaderFailure(
"limit",
"UnityFS LZMA exceeds the decoder memory budget",
{ cause, boundary: "memory" },
);
if (cause instanceof BinaryFormatFailure)
throw malformedArtifact(
"unityfs",
"UnityFS is malformed: LZMA block does not decode to its declared size",
`UnityFS is malformed: ${codec} block does not decode to its declared size`,
{ cause, field: "block" },
);
throw cause;