Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
sse: accept CRLF and CR event framing
Raine Virta1fb7f9b11993
diff --git a/src/translate/sse.ts b/src/translate/sse.tsindex 3099bff..5e92b47 100644--- a/src/translate/sse.ts+++ b/src/translate/sse.ts@@ -7,6 +7,8 @@ export function encodeSseEvent(event: string, data: unknown): string {return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`}+const BOUNDARY = /\r\n\r\n|\n\n|\r\r/+export async function* parseSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseEvent> {const reader = body.getReader()const decoder = new TextDecoder()@@ -16,10 +18,10 @@ export async function* parseSseStream(body: ReadableStream<Uint8Array>): AsyncGeconst { value, done } = await reader.read()if (done) breakbuf += decoder.decode(value, { stream: true })- let idx: number- while ((idx = buf.indexOf("\n\n")) !== -1) {- const raw = buf.slice(0, idx)- buf = buf.slice(idx + 2)+ let match: RegExpExecArray | null+ while ((match = BOUNDARY.exec(buf)) !== null) {+ const raw = buf.slice(0, match.index)+ buf = buf.slice(match.index + match[0].length)const evt = parseEventBlock(raw)if (evt) yield evt}@@ -36,7 +38,8 @@ export async function* parseSseStream(body: ReadableStream<Uint8Array>): AsyncGefunction parseEventBlock(raw: string): SseEvent | undefined {let event: string | undefinedconst dataLines: string[] = []- for (const line of raw.split("\n")) {+ // Per SSE spec, lines are terminated by CR, LF, or CRLF.+ for (const line of raw.split(/\r\n|\n|\r/)) {if (!line || line.startsWith(":")) continueconst colon = line.indexOf(":")const field = colon === -1 ? line : line.slice(0, colon)