Skip to content
Open
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
45 changes: 45 additions & 0 deletions packages/fetch/src/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,51 @@ describe("streamSse", () => {
expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]);
});

it.each([
{
name: "a leading ping",
chunks: [': ping\n\ndata: {"foo":"bar"}\n\ndata: {"baz":42}\n\n'],
},
{
name: "an interleaved ping",
chunks: ['data: {"foo":"bar"}\n\n: ping\n\ndata: {"baz":42}\n\n'],
},
{
name: "a ping split across chunks",
chunks: [": pi", 'ng\n\ndata: {"foo":"bar"}\n\ndata: {"baz":42}\n\n'],
},
{
name: "consecutive pings",
chunks: [
': ping\n\n: ping\n\ndata: {"foo":"bar"}\n\ndata: {"baz":42}\n\n',
],
},
{
name: "a trailing ping",
chunks: ['data: {"foo":"bar"}\n\ndata: {"baz":42}\n\n: ping\n\n'],
},
{
name: "another comment",
chunks: [': heartbeat\n\ndata: {"foo":"bar"}\n\ndata: {"baz":42}\n\n'],
},
])("keeps draining data after $name", async ({ chunks }) => {
const body = new ReadableStream<Uint8Array>({
start(controller) {
const encoder = new TextEncoder();
for (const chunk of chunks) {
controller.enqueue(encoder.encode(chunk));
}
controller.close();
},
});
const results = [];
for await (const data of streamSse(new Response(body))) {
results.push(data);
}

expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]);
});

it("throws on malformed JSON", async () => {
const sseLines = ['data: {"foo": "bar"', "data:[DONE]"];
const response = createMockResponse(sseLines);
Expand Down
3 changes: 0 additions & 3 deletions packages/fetch/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ function parseSseLine(line: string): { done: boolean; data: any } {
if (line.startsWith("data:")) {
return { done: false, data: parseDataLine(line) };
}
if (line.startsWith(": ping")) {
return { done: true, data: undefined };
}
return { done: false, data: undefined };
}

Expand Down
Loading