Files
ComfyUI_frontend/src/scripts/metadata/parser.ts
Johnpaul Chiwetelu 02e926471f fix: replace as-unknown-as casts with safer patterns (#9107)
## Summary

- Replace 83 `as unknown as` double casts with safer alternatives across
33 files
- Use `as Partial<X> as X` pattern where TypeScript allows it
- Create/reuse factory functions from `litegraphTestUtils.ts` for mock
objects
- Widen `getWorkflowDataFromFile` return type to include `ComfyMetadata`
directly
- Reduce total `as unknown as` count from ~153 to 71

The remaining 71 occurrences are genuinely necessary due to cross-schema
casts, generic variance, missing index signatures, Float64Array-to-tuple
conversions, and DOM type incompatibilities.

## Test plan

- [x] `pnpm typecheck` passes
- [x] `pnpm lint` passes
- [x] All affected unit tests pass

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-9107-fix-replace-as-unknown-as-casts-with-safer-patterns-3106d73d3650815cb5bcd613ad635bd7)
by [Unito](https://www.unito.io)
2026-02-22 20:46:12 -08:00

74 lines
2.4 KiB
TypeScript

import { getFromWebmFile } from '@/scripts/metadata/ebml'
import { getGltfBinaryMetadata } from '@/scripts/metadata/gltf'
import { getFromIsobmffFile } from '@/scripts/metadata/isobmff'
import { getDataFromJSON } from '@/scripts/metadata/json'
import { getMp3Metadata } from '@/scripts/metadata/mp3'
import { getOggMetadata } from '@/scripts/metadata/ogg'
import { getSvgMetadata } from '@/scripts/metadata/svg'
import type { ComfyMetadata } from '@/types/metadataTypes'
import {
getAvifMetadata,
getWebpMetadata,
getFlacMetadata,
getLatentMetadata,
getPngMetadata
} from '@/scripts/pnginfo'
export async function getWorkflowDataFromFile(
file: File
): Promise<ComfyMetadata | Record<string, string | object> | undefined> {
if (file.type === 'image/png') {
return await getPngMetadata(file)
}
if (file.type === 'image/avif') {
return await getAvifMetadata(file)
}
if (file.type === 'image/webp') {
const pngInfo = await getWebpMetadata(file)
// Support loading workflows from that webp custom node.
const workflow = pngInfo?.workflow || pngInfo?.Workflow
const prompt = pngInfo?.prompt || pngInfo?.Prompt
return { workflow, prompt }
}
if (file.type === 'audio/mpeg') {
return await getMp3Metadata(file)
}
if (file.type === 'audio/ogg') {
return await getOggMetadata(file)
}
if (file.type === 'audio/flac' || file.type === 'audio/x-flac') {
const pngInfo = await getFlacMetadata(file)
const workflow = pngInfo?.workflow || pngInfo?.Workflow
const prompt = pngInfo?.prompt || pngInfo?.Prompt
return { workflow, prompt }
}
if (file.type === 'video/webm') {
return await getFromWebmFile(file)
}
if (
file.name?.endsWith('.mp4') ||
file.name?.endsWith('.mov') ||
file.name?.endsWith('.m4v') ||
file.type === 'video/mp4' ||
file.type === 'video/quicktime' ||
file.type === 'video/x-m4v'
) {
return await getFromIsobmffFile(file)
}
if (file.type === 'image/svg+xml' || file.name?.endsWith('.svg')) {
return await getSvgMetadata(file)
}
if (file.type === 'model/gltf-binary' || file.name?.endsWith('.glb')) {
return await getGltfBinaryMetadata(file)
}
if (file.name?.endsWith('.latent') || file.name?.endsWith('.safetensors')) {
return await getLatentMetadata(file)
}
if (file.type === 'application/json' || file.name?.endsWith('.json')) {
return getDataFromJSON(file)
}
return
}