MCPcopy Create free account
hub / github.com/backnotprop/plannotator / readSSEEvents

Function readSSEEvents

packages/server/reference-watch.test.ts:15–46  ·  view source on GitHub ↗
(response: Response, count: number)

Source from the content-addressed store, hash-verified

13}
14
15async function readSSEEvents(response: Response, count: number): Promise<Array<{ type?: string; dirPath?: string }>> {
16 const reader = response.body?.getReader();
17 if (!reader) throw new Error("Missing response body");
18 const decoder = new TextDecoder();
19 const events: Array<{ type?: string; dirPath?: string }> = [];
20 let pending = "";
21
22 try {
23 while (events.length < count) {
24 let timeout: ReturnType<typeof setTimeout> | null = null;
25 const result = await Promise.race([
26 reader.read(),
27 new Promise<never>((_, reject) => {
28 timeout = setTimeout(() => reject(new Error("Timed out waiting for SSE event")), 1000);
29 }),
30 ]);
31 if (timeout) clearTimeout(timeout);
32 if (result.done) break;
33 pending += decoder.decode(result.value, { stream: true });
34 const blocks = pending.split("\n\n");
35 pending = blocks.pop() ?? "";
36 for (const block of blocks) {
37 const line = block.split("\n").find((item) => item.startsWith("data: "));
38 if (!line) continue;
39 events.push(JSON.parse(line.slice("data: ".length)));
40 }
41 }
42 return events;
43 } finally {
44 await reader.cancel();
45 }
46}
47
48afterEach(() => {
49 for (const dir of tempDirs.splice(0)) {

Callers 1

Calls 3

rejectFunction · 0.85
parseMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected