Skip to content

feat(sse): server-side event encoding #241

Closed

Description

@Oaklight

What

Add server-side formatting to sse (currently client-only).

  • encode_event(data="", *, event=None, id=None, retry=None) -> str — one complete SSE block, terminated by a blank line.
    • Multi-line data becomes one data: line per line; CRLF and lone CR are normalized to LF.
    • Empty data still emits data: so the block dispatches.
    • id must not contain NUL; retry must be a non-negative integer.
  • A comment/heartbeat helper (encode_comment) if it can be made unambiguous.
  • Optional SSEEvent.encode() convenience that delegates to encode_event.
  • Unit tests: field formatting, multi-line data, empty data, validation errors, round-trip through EventSource.
  • Docs (EN/ZH): a server-side example using httpserver.StreamingResponse with content_type="text/event-stream".
  • Bump sse 0.3.4 → 0.4.0; refresh manifest.json.

Why

httpserver has StreamingResponse but no SSE formatter. Users hand-roll the wire format and get the details wrong: multi-line data:, exactly one leading space after the colon, the blank line that dispatches, NUL in id, non-integer retry.

Acceptance

  • encode_event matches the WHATWG field syntax and round-trips through EventSource.
  • The encoder path imports neither httpserver nor httpclient (pure stdlib).
  • Tests and docs pass; manifest and version updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modulesIndividual zero-dependency modulesenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions