Documentation
¶
Overview ¶
Package sse reads Server-Sent Events streams.
It is a reader only — chunk consumes SSE, it never serves it. The server side of this contract lives in sandbox-provisioner; the two are separate modules, so the framing is implemented on both sides rather than shared.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrFrameTooLarge = errors.New("sse: frame too large")
ErrFrameTooLarge is returned when a single line exceeds the configured maximum. It is an explicit error rather than a silent stop: truncating a stream because a frame was unexpectedly large is a failure that otherwise reads as success. This is why bufio.Scanner, whose 64KiB limit fails exactly that way, is deliberately not used here.
Functions ¶
func ParseCursor ¶
ParseCursor splits the opaque per-stream cursor carried in a frame's id. An empty string means "from the beginning" and is not an error, so a first connection and a resume share one code path.
func Scan ¶
Scan reads frames from r until EOF, calling fn for each. It returns the last non-empty id seen, so an interrupted reader knows where to resume from.
A partial trailing frame — one with no terminating blank line — is discarded rather than delivered, so a truncated stream never surfaces as a short but plausible-looking event. maxFrame caps a single line.
Terminators may be "\n" or "\r\n". A lone "\r" is not treated as one: no producer we read emits it, and payloads are base64 or JSON so they cannot contain one. Were it ever to appear it stays inside the payload rather than splitting a line, so framing is undisturbed and the payload fails to decode loudly instead of corrupting silently.
Types ¶
type Frame ¶
type Frame struct {
// Event is the "event:" field. Empty for an unnamed frame or a comment.
Event string
// ID is the "id:" field — an opaque cursor. Never interpret it; store it and
// echo it back as Last-Event-ID to resume.
ID string
// Data is the payload with the single optional space after the colon
// stripped.
Data []byte
// Comment is true for a ":" comment frame, i.e. a heartbeat.
Comment bool
}
Frame is one decoded SSE frame.