From 559b7d6d05ef08c46164266c9e09acfa176e3669 Mon Sep 17 00:00:00 2001 From: Kenn Ejima Date: Thu, 5 Dec 2024 02:36:32 +0900 Subject: [PATCH] Handle newlines with multiple consecutive data fields on eventStream (#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. --- src/server/event-stream.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/server/event-stream.ts b/src/server/event-stream.ts index 0597d67..75ef490 100644 --- a/src/server/event-stream.ts +++ b/src/server/event-stream.ts @@ -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);