(body: string)
| 112 | } |
| 113 | |
| 114 | export function parseSse(body: string): RuntimeEvent[] { |
| 115 | return body |
| 116 | .split(/\r?\n\r?\n/) |
| 117 | .map(block => block.trim()) |
| 118 | .filter(Boolean) |
| 119 | .map(block => { |
| 120 | let id = 0; |
| 121 | let type = "message"; |
| 122 | const data: string[] = []; |
| 123 | for (const line of block.split(/\r?\n/)) { |
| 124 | if (line.startsWith("id:")) id = Number(line.slice(3).trim()); |
| 125 | if (line.startsWith("event:")) type = line.slice(6).trim(); |
| 126 | if (line.startsWith("data:")) data.push(line.slice(5).trimStart()); |
| 127 | } |
| 128 | const payload = JSON.parse(data.join("\n")) as Record<string, unknown>; |
| 129 | const schemaVersion = typeof payload.schema_version === "number" ? payload.schema_version : 1; |
| 130 | return { id, type, schemaVersion, data: payload }; |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 135 | return typeof value === "object" && value !== null; |
no test coverage detected