Repository navigation
Expand file tree
/
Copy pathstateStream.ts
More file actions
46 lines (46 loc) · 1.46 KB
/
Copy pathstateStream.ts
File metadata and controls
46 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/** Global notifications contain only a version, authenticated without URL tokens. */
export async function watchState(
token: string,
signal: AbortSignal,
changed: (version: string) => void,
) {
const response = await fetch("/api/state/stream", {
headers: { Authorization: `Bearer ${token}`, Accept: "text/event-stream" },
credentials: "omit",
cache: "no-store",
redirect: "error",
signal,
});
if (
!response.ok ||
!response.body ||
!response.headers.get("Content-Type")?.startsWith("text/event-stream")
) {
throw new Error("State notifications unavailable");
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (!signal.aborted) {
const result = await reader.read();
if (result.done) break;
buffer += decoder.decode(result.value, { stream: true });
if (buffer.length > 8192) throw new Error("State notification too large");
let end: number;
while ((end = buffer.indexOf("\n\n")) >= 0) {
const frame = buffer.slice(0, end);
buffer = buffer.slice(end + 2);
const line = frame
.split("\n")
.find((value) => value.startsWith("data: "));
if (!line) continue;
const payload = JSON.parse(line.slice(6));
if (typeof payload.version === "string") changed(payload.version);
}
}
} finally {
await reader.cancel().catch(() => {});
reader.releaseLock();
}
}