diff --git a/src/artifacts/gpuAsset/GpuAssetLayouts.ts b/src/artifacts/gpuAsset/GpuAssetLayouts.ts index 93502765ec7..5a543f61756 100644 --- a/src/artifacts/gpuAsset/GpuAssetLayouts.ts +++ b/src/artifacts/gpuAsset/GpuAssetLayouts.ts @@ -341,8 +341,7 @@ export const glbLayout = async ( const end = Math.min(declaredLength, fileSize); let jsonStatus: GlbMetadata["json_status"] = "invalid"; let json: Readonly> = {}; - let binOffset: number | undefined; - let binLength = 0; + let bin: GlbMetadata["chunks"][number] | undefined; let regionsExamined: GpuAssetLayout["regionsExamined"] = "complete"; let at = 12; @@ -363,39 +362,31 @@ export const glbLayout = async ( throw malformed("glb", "the first GLB chunk is not JSON", at); if (length % 4 !== 0) throw malformed("glb", "GLB chunk length is not 4-byte aligned", at); - if (at + 8 + length > declaredLength) + const offset = at + 8; + if (offset + length > declaredLength) throw malformed("glb", "GLB chunk crosses the declared GLB length", at); - const typeName = - type === GLB_JSON - ? "JSON" - : type === GLB_BIN - ? "BIN" - : type.toString(16).padStart(8, "0"); - chunks.push({ type: typeName, offset: at + 8, length }); + const kind = + type === GLB_JSON ? "JSON" : type === GLB_BIN ? "BIN" : undefined; + const record = { + type: kind ?? type.toString(16).padStart(8, "0"), + offset, + length, + }; + chunks.push(record); regions.push({ - path: - type === GLB_JSON - ? `chunks/${String(index)}.json` - : `chunks/${String(index)}.bin`, + path: `chunks/${String(index)}.${kind === "JSON" ? "json" : "bin"}`, role: - type === GLB_JSON - ? "json-chunk" - : type === GLB_BIN - ? "bin-chunk" - : "chunk", + kind === "JSON" ? "json-chunk" : kind === "BIN" ? "bin-chunk" : "chunk", index, - offset: at + 8, + offset, length, }); - if (index === 0 && at + 8 + length <= fileSize) { - const parsed = await readGlbJsonChunk(read, at + 8, length); + if (index === 0 && offset + length <= fileSize) { + const parsed = await readGlbJsonChunk(read, offset, length); jsonStatus = parsed.status; json = parsed.json; } - if (type === GLB_BIN && binOffset === undefined) { - binOffset = at + 8; - binLength = length; - } + if (kind === "BIN") bin ??= record; at += 8 + length; } // A chunk table that stops short of a physically present declared end @@ -429,7 +420,9 @@ export const glbLayout = async ( const bufferLength = indexOf(asRecord(buffers[0])?.["byteLength"]); // The BIN chunk can include alignment padding past the embedded buffer. const embeddedByteLimit = - bufferLength === undefined ? undefined : Math.min(bufferLength, binLength); + bufferLength === undefined + ? undefined + : Math.min(bufferLength, bin?.length ?? 0); if (images.omitted > 0) limitations.push( `The GLB declares ${String(images.omitted)} image(s) past the ${String(MAX_GLB_IMAGES)}-image listing allowance; their bytes stay inside the BIN chunk region.`, @@ -469,7 +462,7 @@ export const glbLayout = async ( view["byteOffset"] === undefined ? 0 : indexOf(view["byteOffset"]); const length = indexOf(view["byteLength"]); if ( - binOffset === undefined || + bin === undefined || bufferIndex !== 0 || embeddedByteLimit === undefined || start === undefined || @@ -488,7 +481,7 @@ export const glbLayout = async ( path: `images/${String(index)}.${extension}`, role: "image", index, - offset: binOffset + start, + offset: bin.offset + start, length, }); });