Repository navigation
feat(sse): add server-side event encoding - #243
Merged
Merged
Conversation
Add encode_event() and encode_comment() so the module can produce text/event-stream responses as well as consume them. Multi-line data is split into one data: field per line, line breaks in event/id and NUL in id are rejected, and retry is validated as a non-negative integer. The helpers are pure functions with no network dependency, so they work with httpserver.StreamingResponse or any other streaming transport. Fixes #241
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
httpservercan already stream a response, but nothing in the project turnsapplication data into a valid
text/event-streambody. Every server-side SSEuser therefore hand-rolls the same formatting rules — and gets the edge cases
wrong: a
datavalue containing\nsilently truncates on clients thatdispatch per blank line, an
idwith a newline corrupts the stream, and anegative
retryis accepted by nobody.This adds the producer half of the protocol next to the existing consumer, in
the same single file, so the two sides are verified against each other.
What
encode_event(data="", *, event=None, id=None, retry=None)returns onecomplete block, including the terminating blank line that dispatches it.
Multi-line payloads produce one
data:field per line; CRLF and CR arenormalized. A line break in
event/id, NUL inid, or aretrythat isnot a non-negative integer raises
ValueError.encode_comment(text="")returns a comment block for heartbeats.httpserver.StreamingResponse, any other transport, or a directwrite().The helpers sit in the same module as
EventSource, which makes a round-triptest possible: whatever
encode_eventemits, the parser must reconstruct.Docs
docs/modules/sse.md(EN + ZH): title, feature list, a server-side encodingexample, API reference entries, comparison table.
Tests
12 new cases in
TestEncodeEventcover single-line and multi-line payloads,CRLF/CR normalization, every optional field, empty data, trailing newlines,
encoder → parser round-trip, and each rejection path.
pytest sse/test_sse_correctness.pypasses(71 tests).
Fixes #241