fix(realtime): feed SSE reader chunks to the parser in maxChunkBytes slices (04-05)
The browser picks the read size, so a chunk over 256KB ended the stream as EVENT_TOO_LARGE even when every event was normal size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1fe9bfdc2d
commit
2138896472
@@ -23,6 +23,7 @@ import {
|
||||
} from "../result.ts";
|
||||
import {
|
||||
createIncrementalSseParser,
|
||||
SSE_PARSER_CEILINGS,
|
||||
type ParsedSseEvent,
|
||||
type SseParserItem,
|
||||
type SseParserLimits,
|
||||
@@ -131,6 +132,9 @@ export function createFetchSseConnection(
|
||||
// Validate immutable parser policy at factory construction, before network
|
||||
// side effects. A fresh parser is still created for every physical attempt.
|
||||
createIncrementalSseParser(dependencies.parserLimits);
|
||||
const maxChunkBytes =
|
||||
dependencies.parserLimits?.maxChunkBytes ??
|
||||
SSE_PARSER_CEILINGS.maxChunkBytes;
|
||||
|
||||
let closed = false;
|
||||
let active = false;
|
||||
@@ -416,19 +420,30 @@ export function createFetchSseConnection(
|
||||
await cancelReader(reader, clock, controller);
|
||||
return failed("MALFORMED_EVENT", "DECODE", false);
|
||||
}
|
||||
// Items past the parser's per-call cap arrive through drain().
|
||||
// The browser picks the read size, so a chunk larger than the
|
||||
// parser's per-push cap is fed in slices. Items past the per-call item
|
||||
// cap arrive through drain().
|
||||
const chunk = readResult.value.value;
|
||||
for (
|
||||
let parsed = parser.push(readResult.value.value);
|
||||
;
|
||||
parsed = parser.drain()
|
||||
let offset = 0;
|
||||
offset < chunk.byteLength;
|
||||
offset += maxChunkBytes
|
||||
) {
|
||||
if (!parsed.ok) {
|
||||
await cancelReader(reader, clock, controller);
|
||||
return parsed;
|
||||
for (
|
||||
let parsed = parser.push(
|
||||
chunk.subarray(offset, offset + maxChunkBytes),
|
||||
);
|
||||
;
|
||||
parsed = parser.drain()
|
||||
) {
|
||||
if (!parsed.ok) {
|
||||
await cancelReader(reader, clock, controller);
|
||||
return parsed;
|
||||
}
|
||||
if (parsed.value.length === 0) break;
|
||||
const dispatchFailure = await handleParserItems(parsed.value);
|
||||
if (dispatchFailure) return dispatchFailure;
|
||||
}
|
||||
if (parsed.value.length === 0) break;
|
||||
const dispatchFailure = await handleParserItems(parsed.value);
|
||||
if (dispatchFailure) return dispatchFailure;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -549,6 +549,31 @@ describe("fetch SSE connection", () => {
|
||||
expect(received[299]).toBe("cursor-299");
|
||||
});
|
||||
|
||||
it("dispatches every normal event from a 1MB reader chunk", async () => {
|
||||
// The browser, not the server, picks the chunk size.
|
||||
const chunk = Array.from(
|
||||
{ length: 2_000 },
|
||||
(_, index) =>
|
||||
`id: cursor-${index}\ndata: ${"é".repeat(250)}\n\n`,
|
||||
).join("");
|
||||
expect(encoder.encode(chunk).byteLength).toBeGreaterThan(1_000_000);
|
||||
const runtime = connection(
|
||||
(async () => eventStream([chunk])) as typeof fetch,
|
||||
);
|
||||
const received: string[] = [];
|
||||
await expect(
|
||||
runtime.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event) => {
|
||||
received.push(event.id ?? "");
|
||||
return realtimeSuccess(SSE_CONTINUE);
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: true, value: { kind: "EOF" } });
|
||||
expect(received).toHaveLength(2_000);
|
||||
expect(received[1_999]).toBe("cursor-1999");
|
||||
});
|
||||
|
||||
it("rejects arbitrary endpoints and mode-incoherent cursors before fetch", async () => {
|
||||
expect(() =>
|
||||
createFetchSseConnection({
|
||||
|
||||
Reference in New Issue
Block a user