diff --git a/packages/shared/src/mcpResponsePayloads.test.ts b/packages/shared/src/mcpResponsePayloads.test.ts index d3366d671586e..d77eadcd34c50 100644 --- a/packages/shared/src/mcpResponsePayloads.test.ts +++ b/packages/shared/src/mcpResponsePayloads.test.ts @@ -71,6 +71,30 @@ it.effect("reads SSE line endings across byte-chunk boundaries without splitting }), ); +it.effect("reads a multi-megabyte SSE event delivered in small chunks", () => + Effect.gen(function* () { + const text = "x".repeat(8 * 1024 * 1024); + const bytes = new TextEncoder().encode( + `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`, + ); + let offset = 0; + const response = new Response( + new ReadableStream({ + pull(controller) { + if (offset === bytes.length) return controller.close(); + controller.enqueue(bytes.subarray(offset, offset + 256)); + offset = Math.min(offset + 256, bytes.length); + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ); + const payloads = yield* Stream.runCollect( + responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), + ); + expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]); + }), +); + it.effect("picks the SSE reader from the media type alone, whatever its case", () => Effect.gen(function* () { const response = new Response('data: {"id":1}\n\n', { diff --git a/packages/shared/src/mcpResponsePayloads.ts b/packages/shared/src/mcpResponsePayloads.ts index 8c9a38cecf18d..0b4b99e7cecf5 100644 --- a/packages/shared/src/mcpResponsePayloads.ts +++ b/packages/shared/src/mcpResponsePayloads.ts @@ -3,28 +3,40 @@ import * as Effect from "effect/Effect"; import * as Stream from "effect/Stream"; const SSE_EVENT_SEPARATOR = /(?:\r\n|\r(?!\n)|\n){2}/u; +// A separator is at most four characters, so one that a new chunk completes starts within the +// last three characters already read. Scanning only those plus the chunk keeps parsing linear. +const SEPARATOR_OVERLAP = 3; + +function sseEventData(rawEvent: string): string { + return rawEvent + .split(/\r\n|\r|\n/u) + .filter((line) => line.startsWith("data:")) + .map((line) => line.slice("data:".length).trimStart()) + .join("\n"); +} async function* sseDataLines( reader: ReadableStreamDefaultReader, ): AsyncGenerator { const decoder = new TextDecoder(); - let buffered = ""; + let eventParts: string[] = []; + let unscanned = ""; while (true) { const { done, value } = await reader.read(); if (done) return; - buffered += decoder.decode(value, { stream: true }); - let separator = SSE_EVENT_SEPARATOR.exec(buffered); + let text = unscanned + decoder.decode(value, { stream: true }); + let separator = SSE_EVENT_SEPARATOR.exec(text); while (separator !== null) { - const rawEvent = buffered.slice(0, separator.index); - buffered = buffered.slice(separator.index + separator[0].length); - const data = rawEvent - .split(/\r\n|\r|\n/u) - .filter((line) => line.startsWith("data:")) - .map((line) => line.slice("data:".length).trimStart()) - .join("\n"); + eventParts.push(text.slice(0, separator.index)); + const data = sseEventData(eventParts.join("")); + eventParts = []; + text = text.slice(separator.index + separator[0].length); if (data.length > 0) yield data; - separator = SSE_EVENT_SEPARATOR.exec(buffered); + separator = SSE_EVENT_SEPARATOR.exec(text); } + const scannedLength = Math.max(0, text.length - SEPARATOR_OVERLAP); + eventParts.push(text.slice(0, scannedLength)); + unscanned = text.slice(scannedLength); } }