Skip to content

Conversation

@BSteffaniak
Copy link
Contributor

@BSteffaniak BSteffaniak commented Dec 23, 2025

Fixes an issue where SSE messages containing newlines in the data field would be incorrectly parsed by the client.

Per the SSE spec, multi-line data must be sent as multiple data: lines that the EventSource parser concatenates. Previously, MSW sent the data as a single data: field, which caused messages containing \n\n to be truncated (since \n\n terminates an SSE message).

Before:
data:line1\n\nline2
Client receives: "line1" (truncated)

After:
data:line1
data:
data:line2

Client receives: "line1\n\nline2" (correct)

The fix handles all SSE line terminators per spec: LF (\n), CR (\r), and CRLF (\r\n).

Let me know your thoughts. Thanks!

@kettanaito kettanaito changed the title fix(sse): handle multi-line data in SSE messages fix(sse): handle multi-line messages Dec 26, 2025
kettanaito
kettanaito previously approved these changes Dec 26, 2025
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @BSteffaniak. Thank you for fixing this! And an extra thanks for those test cases.

I've pushed a change related to how the message data is processed, making that processing conditional (no need to fallback to '' to then split via RegExp into an empty array).

Could you please share a link to the paragraph section that outlines how line terminators have to be handled in the SSE spec? We could leave it in the implementation for future references. Thanks.

@BSteffaniak
Copy link
Contributor Author

Here are some references in the spec:

https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation

here's also some MDN docs around it too (a bit easier to read):

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#data

@BSteffaniak
Copy link
Contributor Author

@kettanaito
Copy link
Member

Thanks! This is great.

@kettanaito kettanaito merged commit 1266f61 into mswjs:main Dec 26, 2025
18 checks passed
@kettanaito
Copy link
Member

Released: v2.12.5 🎉

This has been released in v2.12.5.

Get these changes by running the following command:

npm i msw@latest

Predictable release automation by Release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants