LCOV - code coverage report
Current view: top level - src/scripts/metadata - gltf.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 22.0 % 59 13
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   ComfyApiWorkflow,
       3              :   ComfyWorkflowJSON
       4              : } from '@comfyorg/graph-model/workflowSchema'
       5              : import { ASCII, ComfyMetadataTags, GltfSizeBytes } from '@/types/metadataTypes'
       6              : import type {
       7              :   ComfyMetadata,
       8              :   GltfChunkHeader,
       9              :   GltfHeader
      10              : } from '@/types/metadataTypes'
      11              : import { readFileAsArrayBuffer } from '@/utils/fileUtil'
      12              : import { parseJsonWithNonFinite } from '@/utils/jsonUtil'
      13              : 
      14         2356 : const MAX_READ_BYTES = 1 << 20
      15              : 
      16         2356 : const isJsonChunk = (chunk: GltfChunkHeader | null): boolean =>
      17              :   !!chunk && chunk.chunkTypeIdentifier === ASCII.JSON
      18              : 
      19         2356 : const isValidChunkRange = (
      20              :   start: number,
      21              :   length: number,
      22              :   bufferSize: number
      23              : ): boolean => start + length <= bufferSize
      24              : 
      25         2356 : const byteArrayToString = (bytes: Uint8Array): string =>
      26              :   new TextDecoder().decode(bytes)
      27              : 
      28         2356 : const parseGltfBinaryHeader = (dataView: DataView): GltfHeader | null => {
      29            0 :   if (dataView.byteLength < GltfSizeBytes.HEADER) return null
      30              : 
      31            0 :   const magicNumber = dataView.getUint32(0, true)
      32            0 :   if (magicNumber !== ASCII.GLTF) return null
      33              : 
      34            0 :   return {
      35              :     magicNumber,
      36              :     gltfFormatVersion: dataView.getUint32(4, true),
      37              :     totalLengthBytes: dataView.getUint32(8, true)
      38              :   }
      39              : }
      40              : 
      41         2356 : const parseChunkHeaderAtOffset = (
      42              :   dataView: DataView,
      43              :   offset: number
      44              : ): GltfChunkHeader | null => {
      45            0 :   if (offset + GltfSizeBytes.CHUNK_HEADER > dataView.byteLength) return null
      46              : 
      47            0 :   return {
      48              :     chunkLengthBytes: dataView.getUint32(offset, true),
      49              :     chunkTypeIdentifier: dataView.getUint32(offset + 4, true)
      50              :   }
      51              : }
      52              : 
      53         2356 : const extractJsonChunk = (
      54              :   buffer: ArrayBuffer
      55              : ): { start: number; length: number } | null => {
      56            0 :   const dataView = new DataView(buffer)
      57              : 
      58              :   const header = parseGltfBinaryHeader(dataView)
      59            0 :   if (!header) return null
      60              : 
      61            0 :   const chunkOffset = GltfSizeBytes.HEADER
      62            0 :   const firstChunk = parseChunkHeaderAtOffset(dataView, chunkOffset)
      63            0 :   if (!firstChunk || !isJsonChunk(firstChunk)) return null
      64              : 
      65            0 :   const jsonStart = chunkOffset + GltfSizeBytes.CHUNK_HEADER
      66              :   const isValid = isValidChunkRange(
      67              :     jsonStart,
      68              :     firstChunk.chunkLengthBytes,
      69              :     dataView.byteLength
      70              :   )
      71            0 :   if (!isValid) return null
      72              : 
      73            0 :   return { start: jsonStart, length: firstChunk.chunkLengthBytes }
      74              : }
      75              : 
      76         2356 : const extractJsonChunkData = (buffer: ArrayBuffer): Uint8Array | null => {
      77            0 :   const chunkLocation = extractJsonChunk(buffer)
      78            0 :   if (!chunkLocation) return null
      79              : 
      80            0 :   return new Uint8Array(buffer, chunkLocation.start, chunkLocation.length)
      81              : }
      82              : 
      83         2356 : const parseJson = (text: string): unknown => {
      84            0 :   try {
      85            0 :     return parseJsonWithNonFinite(text)
      86              :   } catch {
      87            0 :     return null
      88              :   }
      89              : }
      90              : 
      91         2356 : const isJsonObject = (value: unknown): value is Record<string, unknown> =>
      92              :   typeof value === 'object' && value !== null
      93              : 
      94         2356 : const parseMetadataValue = (
      95              :   value: unknown
      96              : ): ComfyWorkflowJSON | ComfyApiWorkflow | undefined => {
      97            0 :   const parsed = typeof value === 'string' ? parseJson(value) : value
      98            0 :   return isJsonObject(parsed)
      99              :     ? (parsed as ComfyWorkflowJSON | ComfyApiWorkflow)
     100              :     : undefined
     101              : }
     102              : 
     103         2356 : const extractComfyMetadata = (
     104              :   jsonData: Record<string, unknown>
     105              : ): ComfyMetadata => {
     106            0 :   const metadata: ComfyMetadata = {}
     107              : 
     108            0 :   const { asset } = jsonData
     109            0 :   if (!isJsonObject(asset) || !isJsonObject(asset.extras)) return metadata
     110              : 
     111            0 :   const { extras } = asset
     112              : 
     113            0 :   if (extras.workflow) {
     114            0 :     const parsedValue = parseMetadataValue(extras.workflow)
     115            0 :     if (parsedValue) {
     116            0 :       metadata[ComfyMetadataTags.WORKFLOW.toLowerCase()] = parsedValue
     117              :     }
     118              :   }
     119              : 
     120            0 :   if (extras.prompt) {
     121            0 :     const parsedValue = parseMetadataValue(extras.prompt)
     122            0 :     if (parsedValue) {
     123            0 :       metadata[ComfyMetadataTags.PROMPT.toLowerCase()] = parsedValue
     124              :     }
     125              :   }
     126              : 
     127            0 :   return metadata
     128              : }
     129              : 
     130         2356 : const processGltfFileBuffer = (buffer: ArrayBuffer): ComfyMetadata => {
     131            0 :   const jsonChunk = extractJsonChunkData(buffer)
     132            0 :   if (!jsonChunk) return {}
     133              : 
     134            0 :   const parsedJson = parseJson(byteArrayToString(jsonChunk))
     135            0 :   if (!isJsonObject(parsedJson)) return {}
     136              : 
     137            0 :   return extractComfyMetadata(parsedJson)
     138              : }
     139              : 
     140              : /**
     141              :  * Extract ComfyUI metadata from a GLTF binary file (GLB)
     142              :  */
     143            0 : export async function getGltfBinaryMetadata(
     144              :   file: File
     145              : ): Promise<ComfyMetadata> {
     146            0 :   const buffer = await readFileAsArrayBuffer(file, MAX_READ_BYTES)
     147            0 :   if (!buffer) return {}
     148              : 
     149            0 :   try {
     150            0 :     return processGltfFileBuffer(buffer)
     151              :   } catch {
     152            0 :     return {}
     153              :   }
     154              : }
        

Generated by: LCOV version 2.0-1