Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/shared/src/mcpResponsePayloads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>({
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', {
Expand Down
34 changes: 23 additions & 11 deletions packages/shared/src/mcpResponsePayloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>,
): AsyncGenerator<string> {
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);
}
}

Expand Down
Loading