mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-23 22:25:05 +00:00
## Summary API and other legacy JSON generated by python `json.dumps` can contain `NaN` and `Infinity` which cannot be parsed with JS `JSON.parse`. This adds regex to replace these invalid tokens with `null`. ## Changes - **What**: - add regex replace on bare NaN/infinity tokens after JSON.parse fails - update call sites - tests ## Review Focus - The regex should only rewrite bare NaN/-Infinity/Infinity and not touch string values or other invalid tokens. - A small regex was chosen over JSON5 due to package size (30.3kB Minified, 9kB Minified + Gzipped) or a manual parser due to the unnecessarily complexity vs a single regex replace. - The happy path is run first, the safe parse is only executed if that failed, meaning no overhead the vast majority of the time and no possiblity of corrupting valid workflows due to a bug in the fallback parser - Multiple call sites had to be updated due to pre-existing architecture of the various parsers, an issue for unifying these is logged for future cleanup - New binary fixtures added for validating e2e import using real files ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-12217-fix-add-support-for-parsing-python-generated-json-with-NaN-infinite-35f6d73d365081889fc7f4af823f29c1) by [Unito](https://www.unito.io)
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import type {
|
|
ComfyApiWorkflow,
|
|
ComfyWorkflowJSON
|
|
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
|
import { parseJsonWithNonFinite } from '@/utils/jsonUtil'
|
|
|
|
export async function getMp3Metadata(file: File) {
|
|
const reader = new FileReader()
|
|
const read_process = new Promise<ArrayBuffer | null>((r) => {
|
|
reader.onload = (event) => r((event?.target?.result as ArrayBuffer) ?? null)
|
|
reader.onerror = () => r(null)
|
|
reader.onabort = () => r(null)
|
|
})
|
|
reader.readAsArrayBuffer(file)
|
|
const arrayBuffer = await read_process
|
|
if (!arrayBuffer) return { prompt: undefined, workflow: undefined }
|
|
//https://stackoverflow.com/questions/7302439/how-can-i-determine-that-a-particular-file-is-in-fact-an-mp3-file#7302482
|
|
const sig_bytes = new Uint8Array(arrayBuffer, 0, 3)
|
|
if (
|
|
(sig_bytes[0] != 0xff || sig_bytes[1] != 0xfb) &&
|
|
(sig_bytes[0] != 0x49 || sig_bytes[1] != 0x44 || sig_bytes[2] != 0x33)
|
|
)
|
|
console.error('Invalid file signature.')
|
|
let header = ''
|
|
while (header.length < arrayBuffer.byteLength) {
|
|
const page = String.fromCharCode(
|
|
...new Uint8Array(
|
|
arrayBuffer,
|
|
header.length,
|
|
Math.min(4096, arrayBuffer.byteLength - header.length)
|
|
)
|
|
)
|
|
header += page
|
|
if (page.match('\u00ff\u00fb')) break
|
|
}
|
|
let workflow: ComfyWorkflowJSON | undefined
|
|
let prompt: ComfyApiWorkflow | undefined
|
|
let prompt_s = header.match(/prompt\u0000(\{.*?\})\u0000/s)?.[1]
|
|
if (prompt_s) {
|
|
try {
|
|
prompt = parseJsonWithNonFinite<ComfyApiWorkflow>(prompt_s)
|
|
} catch (e) {
|
|
console.error('Failed to parse MP3 prompt metadata', e)
|
|
}
|
|
}
|
|
let workflow_s = header.match(/workflow\u0000(\{.*?\})\u0000/s)?.[1]
|
|
if (workflow_s) {
|
|
try {
|
|
workflow = parseJsonWithNonFinite<ComfyWorkflowJSON>(workflow_s)
|
|
} catch (e) {
|
|
console.error('Failed to parse MP3 workflow metadata', e)
|
|
}
|
|
}
|
|
return { prompt, workflow }
|
|
}
|