fix(glb): preserve JSON chunk read failures (#15181)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
This commit is contained in:
authored and GitHub committed 2026-11-30 16:28:35 +08:00
1 parent 91380cff6a
commit e9d024af77
2 files changed
+65 -3

No files matched your search

@@ -0,0 +1,63 @@
import { open } from "node:fs/promises";
import { expect, it, vi } from "vitest";
import { createTestTempFile } from "../../../tests/fixtures/temporaryDirectory.js";
import { GpuAssetArtifactReader } from "./GpuAssetArtifactReader.js";
import { glbFixture } from "./gpuAsset.fixture.js";
it.each(["io", "read-coverage"] as const)(
"propagates a GLB JSON %s failure without publishing invalid JSON facts",
async (failure) => {
const path = await createTestTempFile(
"rea-glb-read-",
"asset.glb",
glbFixture(),
);
const handle = await open(path, "r");
const reader = new GpuAssetArtifactReader(path, "glb", {
handle,
initial: await handle.stat(),
});
const nativeRead = handle.read.bind(handle);
const io = Object.assign(new Error("GLB JSON read failed"), {
code: "EIO",
});
let injected = false;
const reads = vi.spyOn(handle, "read").mockImplementation(((
...args: unknown[]
) => {
if (!injected && args[3] === 20) {
injected = true;
return failure === "io"
? Promise.reject(io)
: Promise.resolve({ bytesRead: 0, buffer: args[0] });
}
return Reflect.apply(nativeRead, handle, args);
}) as typeof handle.read);
const inventory = async () => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
return entries;
};
try {
if (failure === "io") await expect(inventory()).rejects.toBe(io);
else
await expect(inventory()).rejects.toMatchObject({
reason: "integrity",
integrityCheck: "read-coverage",
});
expect(injected).toBe(true);
expect(reader.formatMetadata()).toBeUndefined();
// The failed preparation leaves the reader usable for the same unchanged file.
await expect(inventory()).resolves.not.toHaveLength(0);
expect(reader.formatMetadata()).toMatchObject({
kind: "glb",
json_status: "parsed",
asset_version: "2.0",
});
} finally {
reads.mockRestore();
await reader.close();
await handle.close();
}
},
);
+2 -3
View File
@@ -276,13 +276,12 @@ const readGlbJsonChunk = async (
readonly json: Readonly<Record<string, unknown>>;
}> => {
if (length > GLB_JSON_PARSE_BYTES) return { status: "limit", json: {} };
const bytes = await read(offset, length);
try {
// glTF JSON is UTF-8; counts follow the last of duplicate members,
// as JavaScript glTF loaders do. Only a JSON object is a glTF root.
const parsed = asRecord(
parseLastWinsJson(
decodeStrictUtf8(await read(offset, length), "strip-signature"),
),
parseLastWinsJson(decodeStrictUtf8(bytes, "strip-signature")),
);
return parsed === undefined
? { status: "invalid", json: {} }