Poll Comfy history and the job snapshot so the browser still gets the video when the live stream is buffered or the websocket misses completion. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1002 B
TypeScript
43 lines
1002 B
TypeScript
export default defineEventHandler(async (event) => {
|
|
const id = getRouterParam(event, 'id')
|
|
const job = id ? getJob(id) : undefined
|
|
if (!job) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Job not found' })
|
|
}
|
|
|
|
setResponseHeaders(event, {
|
|
'Cache-Control': 'no-cache, no-store, no-transform',
|
|
'X-Accel-Buffering': 'no',
|
|
Connection: 'keep-alive'
|
|
})
|
|
|
|
const stream = createEventStream(event)
|
|
const send = async (payload: unknown) => {
|
|
await stream.push(JSON.stringify(payload))
|
|
}
|
|
|
|
await send(jobSnapshot(job))
|
|
for (const past of job.events) {
|
|
await send(past)
|
|
}
|
|
|
|
const unsubscribe = subscribeJob(job, (payload) => {
|
|
void send(payload).then(() => {
|
|
if (payload.type === 'complete' || payload.type === 'error') {
|
|
void stream.close()
|
|
}
|
|
})
|
|
})
|
|
|
|
const ping = setInterval(() => {
|
|
void send(jobSnapshot(job))
|
|
}, 1000)
|
|
|
|
stream.onClosed(() => {
|
|
clearInterval(ping)
|
|
unsubscribe()
|
|
})
|
|
|
|
return stream.send()
|
|
})
|