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

            Line data    Source code
       1            1 : async function decompressZlib(
       2              :   data: Uint8Array<ArrayBuffer>
       3              : ): Promise<Uint8Array<ArrayBuffer>> {
       4              :   const compressed = new Blob([data]).stream()
       5            1 :   const decompressed = compressed.pipeThrough(
       6              :     new DecompressionStream('deflate')
       7              :   )
       8            1 :   return new Uint8Array(await new Response(decompressed).arrayBuffer())
       9              : }
      10              : 
      11            7 : export async function getFromPngBuffer(
      12              :   buffer: ArrayBuffer
      13              : ): Promise<Record<string, string>> {
      14            7 :   const pngData = new Uint8Array(buffer)
      15            7 :   const dataView = new DataView(pngData.buffer)
      16              : 
      17            7 :   if (dataView.getUint32(0) !== 0x89504e47) {
      18            0 :     console.error('Not a valid PNG file')
      19            0 :     return {}
      20              :   }
      21              : 
      22            7 :   let offset = 8
      23            7 :   const txt_chunks: Record<string, string> = {}
      24              : 
      25            7 :   while (offset < pngData.length) {
      26           30 :     const length = dataView.getUint32(offset)
      27           30 :     const type = String.fromCharCode(...pngData.slice(offset + 4, offset + 8))
      28           30 :     const chunkEnd = offset + 8 + length
      29              : 
      30           30 :     if (type === 'IEND') break
      31              : 
      32           30 :     if (chunkEnd > pngData.length) {
      33            0 :       console.error(`PNG chunk "${type}" runs past the end of the file`)
      34            0 :       break
      35              :     }
      36              : 
      37           30 :     if (type === 'tEXt' || type === 'comf' || type === 'iTXt') {
      38            9 :       const keywordLength = pngData.subarray(offset + 8, chunkEnd).indexOf(0)
      39            9 :       if (keywordLength === -1) {
      40            0 :         console.error(`PNG chunk "${type}" has an unterminated keyword`)
      41            0 :         offset += 12 + length
      42            0 :         continue
      43              :       }
      44            9 :       const keyword_end = offset + 8 + keywordLength
      45            9 :       const keyword = String.fromCharCode(
      46              :         ...pngData.slice(offset + 8, keyword_end)
      47              :       )
      48              : 
      49            9 :       let textStart = keyword_end + 1
      50            9 :       let isCompressed = false
      51            9 :       let compressionMethod = 0
      52              : 
      53            9 :       if (type === 'iTXt') {
      54            4 :         isCompressed = pngData[textStart] === 1
      55            4 :         compressionMethod = pngData[textStart + 1]
      56            4 :         textStart += 2
      57              : 
      58            4 :         while (pngData[textStart] !== 0 && textStart < chunkEnd) {
      59            0 :           textStart++
      60              :         }
      61            4 :         if (textStart < chunkEnd) textStart++
      62              : 
      63            4 :         while (pngData[textStart] !== 0 && textStart < chunkEnd) {
      64            0 :           textStart++
      65              :         }
      66            4 :         if (textStart < chunkEnd) textStart++
      67              :       }
      68              : 
      69            9 :       let contentArraySegment = pngData.slice(textStart, chunkEnd)
      70              : 
      71            9 :       if (isCompressed) {
      72            1 :         if (compressionMethod === 0) {
      73            1 :           try {
      74            1 :             contentArraySegment = await decompressZlib(contentArraySegment)
      75              :           } catch (e) {
      76            1 :             console.error(`Failed to decompress iTXt chunk "${keyword}":`, e)
      77            1 :             offset += 12 + length
      78            1 :             continue
      79              :           }
      80              :         } else {
      81            0 :           console.warn(
      82              :             `Unsupported compression method ${compressionMethod} for iTXt chunk "${keyword}"`
      83              :           )
      84            0 :           offset += 12 + length
      85            0 :           continue
      86              :         }
      87              :       }
      88              : 
      89              :       const contentJson = new TextDecoder('utf-8').decode(contentArraySegment)
      90            8 :       txt_chunks[keyword] = contentJson
      91              :     }
      92              : 
      93           22 :     offset += 12 + length
      94              :   }
      95            7 :   return txt_chunks
      96              : }
      97              : 
      98            7 : export async function getFromPngFile(
      99              :   file: File
     100              : ): Promise<Record<string, string>> {
     101            7 :   return new Promise<Record<string, string>>((resolve, reject) => {
     102            7 :     const reader = new FileReader()
     103            7 :     reader.onload = async (event) => {
     104            7 :       const buffer = event.target?.result
     105            7 :       if (!(buffer instanceof ArrayBuffer)) {
     106            0 :         reject(new Error('Failed to read file as ArrayBuffer'))
     107            0 :         return
     108              :       }
     109            7 :       try {
     110            7 :         resolve(await getFromPngBuffer(buffer))
     111              :       } catch (error) {
     112            0 :         reject(error)
     113              :       }
     114              :     }
     115            7 :     reader.onerror = () => reject(reader.error)
     116            7 :     reader.onabort = () => reject(new Error('FileReader aborted'))
     117            7 :     reader.readAsArrayBuffer(file)
     118              :   })
     119              : }
        

Generated by: LCOV version 2.0-1