Consume an SSE channel, counting `reload` events. Returns a stop() fn.
(url: string, onReload: () => void)
| 33 | |
| 34 | /** Consume an SSE channel, counting `reload` events. Returns a stop() fn. */ |
| 35 | async function openSse(url: string, onReload: () => void): Promise<() => void> { |
| 36 | const ac = new AbortController(); |
| 37 | const res = await fetch(url, { headers: { accept: 'text/event-stream' }, signal: ac.signal }); |
| 38 | const reader = (res.body as ReadableStream<Uint8Array>).getReader(); |
| 39 | const dec = new TextDecoder(); |
| 40 | void (async () => { |
| 41 | let buf = ''; |
| 42 | try { |
| 43 | for (;;) { |
| 44 | const { done, value } = await reader.read(); |
| 45 | if (done) break; |
| 46 | buf += dec.decode(value, { stream: true }); |
| 47 | let idx: number; |
| 48 | while ((idx = buf.indexOf('\n\n')) >= 0) { |
| 49 | const block = buf.slice(0, idx); |
| 50 | buf = buf.slice(idx + 2); |
| 51 | if (/^event:\s*reload/m.test(block)) onReload(); |
| 52 | } |
| 53 | } |
| 54 | } catch { /* aborted */ } |
| 55 | })(); |
| 56 | return () => ac.abort(); |
| 57 | } |
| 58 | |
| 59 | describe('live artifact server (end-to-end, in-process)', () => { |
| 60 | it('serves current version, hot-reloads on update and rollback', async () => { |
no test coverage detected