SSE: WHATWG Server-Sent Events parser#10
Merged
Merged
Conversation
WHATWG event-stream parser (Event + Parse over io.Reader as iter.Seq2); reconnecting connection deferred. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
Implements the
ssepackage: a WHATWG-conformant Server-Sent Events(text/event-stream) parser that yields events from any
io.Readeras arange-over-func iterator.
What's included
sse.Event—Type(defaults to "message"),Data(multi-line joined with\n, trailing newline trimmed),ID(sticky across events per spec), andRetry.sse.Parse(r io.Reader) iter.Seq2[Event, error]— runs the WHATWGevent-stream interpretation algorithm:
data/event/id/retryfieldsaccumulate and an event dispatches on a blank line. LF and CRLF line endings,
comment lines (
:), one-leading-space stripping, NUL-in-idignoring, andpartial-event-discard at EOF are all handled. The scanner buffer is capped at
1 MiB; an over-long line surfaces
bufio.ErrTooLong, and read errors aredelivered as the iterator's error value.
Behavior changes
None — new package only (
ssewas a placeholder).Deferred (not in this PR)
retryhonoring, andreconnect/backoff. The parser is fully usable on its own; reconnection is a
resilience layer for a follow-up.
Test plan
go build ./...go vet ./...gofmt -l .(clean)go test -race ./...(all packages pass)retry (valid/non-numeric/non-sticky), comments and blank-only input, CRLF,
leading-space stripping, no-colon field line, partial-event discard at EOF,
mid-stream read error, over-long line, and early break.
🤖 Generated with Claude Code