fix(realtime): hand SSE items past the per-chunk cap to drain() (Q-6.4-5)
A single reader chunk with more than maxItemsPerChunk (256) items used to fail the stream with non-retryable QUEUE_OVERFLOW, killing normal replay bursts even though maxChunkBytes already bounds memory. The parser now stops at the cap, keeps the remaining decoded text, and returns it through a new additive drain() call; fetch-sse-connection drains after every push before reading again. Pushing or finishing with undrained text still fails closed (QUEUE_OVERFLOW), so buffered text stays bounded by one chunk. A lone CR followed by another line now re-checks the cap before continuing. COMMENT and empty item lists reuse frozen constants. The old parser test that expected QUEUE_OVERFLOW for 3 comments with a cap of 2 encoded the bug and is replaced. RED: new parser drain test and a 300-event single-chunk connection test failed with QUEUE_OVERFLOW before the change. Gates: check:architecture, check:types:app/test, lint, check:adapter-inventory, test:unit, test:component all pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a8c5eabcd9
commit
cc906f08b4
@@ -526,6 +526,29 @@ describe("fetch SSE connection", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("dispatches a single chunk holding more events than the per-chunk item cap", async () => {
|
||||
const chunk = Array.from(
|
||||
{ length: 300 },
|
||||
(_, index) =>
|
||||
`id: cursor-${index}\ndata: ${"x".repeat(300)}\n\n`,
|
||||
).join("");
|
||||
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(300);
|
||||
expect(received[299]).toBe("cursor-299");
|
||||
});
|
||||
|
||||
it("rejects arbitrary endpoints and mode-incoherent cursors before fetch", async () => {
|
||||
expect(() =>
|
||||
createFetchSseConnection({
|
||||
|
||||
@@ -159,18 +159,6 @@ describe("incremental SSE parser", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const excessiveBatch = createIncrementalSseParser({
|
||||
maxItemsPerChunk: 2,
|
||||
});
|
||||
expect(excessiveBatch.push(encoder.encode(":\n:\n:\n"))).toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "QUEUE_OVERFLOW",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
|
||||
const oversizedChunk = createIncrementalSseParser({
|
||||
maxChunkBytes: 65_536,
|
||||
});
|
||||
@@ -186,6 +174,52 @@ describe("incremental SSE parser", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("hands items past the per-chunk cap to drain() instead of failing", () => {
|
||||
const parser = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
const comment = { kind: "COMMENT" };
|
||||
expect(pushText(parser, ":\r:\r\n:\ndata: a\r")).toEqual([comment]);
|
||||
// The retained text resumes between CR and LF.
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [comment] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [comment] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [] });
|
||||
expect(pushText(parser, "\n\n")).toEqual([
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "a",
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
]);
|
||||
|
||||
// A lone CR followed by another complete line still honours the cap.
|
||||
const loneCarriageReturn = createIncrementalSseParser({
|
||||
maxItemsPerChunk: 1,
|
||||
});
|
||||
expect(pushText(loneCarriageReturn, ":\r:\n")).toEqual([comment]);
|
||||
expect(loneCarriageReturn.drain()).toEqual({
|
||||
ok: true,
|
||||
value: [comment],
|
||||
});
|
||||
|
||||
// Undrained items are a caller bug: fail closed instead of buffering.
|
||||
const undrained = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
expect(pushText(undrained, ":\n:\n")).toEqual([comment]);
|
||||
const overflow = {
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "QUEUE_OVERFLOW",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
};
|
||||
expect(undrained.push(encoder.encode(":\n"))).toEqual(overflow);
|
||||
const unfinished = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
expect(pushText(unfinished, ":\n:\n")).toEqual([comment]);
|
||||
expect(unfinished.finish()).toEqual(overflow);
|
||||
});
|
||||
|
||||
it("parses the same items whether bytes arrive at once or one by one", () => {
|
||||
const stream = encoder.encode(
|
||||
"event: a\r\ndata: 한글\rdata: 😀 é\r\n\r\n" +
|
||||
|
||||
Reference in New Issue
Block a user