fix(ui): распознавать CRLF и комментарии в потоке SSE

This commit is contained in:
Denozordec committed 2026-09-28 11:38:11 +07:00
1 parent f50a29f116
commit ed491c313f
8 files changed
+38 -15

No files matched your search

+12 -2
View File
@@ -10,8 +10,14 @@ export async function installOpenLiveStream(page: Page, path: string, sample: un
if (url.pathname !== path) return originalFetch(input, init)
const stream = new ReadableStream<Uint8Array>({
start(controller) {
const send = () => controller.enqueue(new TextEncoder().encode('event: sample\ndata: ' + JSON.stringify(sample) + '\n\n'))
Object.assign(window, { sendLiveSample: send })
const encode = (value: string) => new TextEncoder().encode(value)
const send = () => {
// A comment may precede an event; CRLF delimiters may cross chunks.
controller.enqueue(encode(': heartbeat\r\nevent: sample\r\ndata: ' + JSON.stringify(sample) + '\r\n\r'))
controller.enqueue(encode('\n'))
}
const heartbeat = () => controller.enqueue(encode(': heartbeat\r\n\r\n'))
Object.assign(window, { sendLiveSample: send, sendLiveHeartbeat: heartbeat })
send()
},
})
@@ -23,3 +29,7 @@ export async function installOpenLiveStream(page: Page, path: string, sample: un
export async function resumeLiveStream(page: Page) {
await page.evaluate(() => (window as unknown as { sendLiveSample: () => void }).sendLiveSample())
}
export async function sendLiveHeartbeat(page: Page) {
await page.evaluate(() => (window as unknown as { sendLiveHeartbeat: () => void }).sendLiveHeartbeat())
}