MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / openSse

Function openSse

test/artifact-live-server.test.ts:35–57  ·  view source on GitHub ↗

Consume an SSE channel, counting `reload` events. Returns a stop() fn.

(url: string, onReload: () => void)

Source from the content-addressed store, hash-verified

33
34/** Consume an SSE channel, counting `reload` events. Returns a stop() fn. */
35async 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
59describe('live artifact server (end-to-end, in-process)', () => {
60 it('serves current version, hot-reloads on update and rollback', async () => {

Callers 1

Calls 1

abortMethod · 0.80

Tested by

no test coverage detected