From e9d024af7784624f291c2f68241d190efcc57cac Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Mon, 30 Nov 2026 16:28:35 +0800 Subject: [PATCH] fix(glb): preserve JSON chunk read failures (#15181) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: Vincent Koc --- ...GpuAssetArtifactReader.readFailure.test.ts | 63 +++++++++++++++++++ src/artifacts/gpuAsset/GpuAssetLayouts.ts | 5 +- 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/artifacts/gpuAsset/GpuAssetArtifactReader.readFailure.test.ts diff --git a/src/artifacts/gpuAsset/GpuAssetArtifactReader.readFailure.test.ts b/src/artifacts/gpuAsset/GpuAssetArtifactReader.readFailure.test.ts new file mode 100644 index 00000000000..dbc2138acec --- /dev/null +++ b/src/artifacts/gpuAsset/GpuAssetArtifactReader.readFailure.test.ts @@ -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(); + } + }, +); diff --git a/src/artifacts/gpuAsset/GpuAssetLayouts.ts b/src/artifacts/gpuAsset/GpuAssetLayouts.ts index e510dc079b9..64169e0de9f 100644 --- a/src/artifacts/gpuAsset/GpuAssetLayouts.ts +++ b/src/artifacts/gpuAsset/GpuAssetLayouts.ts @@ -276,13 +276,12 @@ const readGlbJsonChunk = async ( readonly json: Readonly>; }> => { 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: {} }