Skip to content

Commit

Permalink
Handle newlines with multiple consecutive data fields on eventStream (#…
Browse files Browse the repository at this point in the history
…343)

Server-Sent Events handle multi-line data with multiple consecutive data
fields.

* [Spec](https://web.dev/articles/eventsource-basics#multi-line_data)
* [Client-side
code](https://github.com/mpetazzoni/sse.js/blob/febdf09b9d3434b9ab59207519f6ff9dd30d97b0/lib/sse.js#L234)

This PR adds support for newlines as expected.
  • Loading branch information
kenn authored Dec 4, 2024
1 parent 2266773 commit 559b7d6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/server/event-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function eventStream(

function send({ event = "message", data }: SendFunctionArgs) {
controller.enqueue(encoder.encode(`event: ${event}\n`));
controller.enqueue(encoder.encode(`data: ${data}\n\n`));
data.split("\n").forEach((line) => {
controller.enqueue(encoder.encode(`data: ${line}\n`));
})
controller.enqueue(encoder.encode("\n"));
}

let cleanup = init(send, close);
Expand Down

0 comments on commit 559b7d6

Please sign in to comment.