pub struct SseParser { /* private fields */ }Expand description
Sans-IO incremental SSE parser.
Feed bytes with feed, drain completed events with
next_event, and call finish at end
of stream. Chunk boundaries never affect the output: any chunking of a
byte stream yields the same events as feeding it whole.
Time is linear in the bytes fed. Each byte is scanned once for a line
terminator and decoded once as part of its completed line, whatever the
chunk size. Memory is proportional to the longest block in flight unless
max_buffer caps it.
Implementations§
Source§impl SseParser
impl SseParser
Sourcepub fn with_last_event_id(id: impl Into<String>) -> Self
pub fn with_last_event_id(id: impl Into<String>) -> Self
A parser seeded with a last event ID string, for reconnection.
Events dispatched before the stream’s first id field report this ID.
Sourcepub fn max_buffer(self, limit: usize) -> Self
pub fn max_buffer(self, limit: usize) -> Self
Cap the bytes held for one block.
A block overflows when the raw bytes of one of its lines, before the
terminator, plus the bytes already in the block’s data and event
buffers exceed limit, or when those buffers exceed limit after a
line is decoded (invalid UTF-8 expands to the three-byte U+FFFD). The
checks run on every completed line and on the unterminated line after
every feed, so the outcome is the same for every chunking of the
stream, and after each feed the parser holds at most limit bytes
plus one chunk.
On overflow the parser drops the block and ignores every following
line up to the next blank line, where it resynchronizes. Fields
processed before the overflow keep their effect: an id or retry
seen earlier in the block stands, and the blank line still commits the
last event ID. overflows counts dropped blocks.
The default is unlimited.
Sourcepub fn overflows(&self) -> u64
pub fn overflows(&self) -> u64
Number of blocks dropped because they exceeded the
max_buffer cap.
Sourcepub fn feed(&mut self, bytes: &[u8])
pub fn feed(&mut self, bytes: &[u8])
Feed one chunk of bytes.
Every byte sequence is a valid event stream, so there is nothing to
reject. tests/no_panic.rs and the fuzz target feed arbitrary bytes
in arbitrary chunkings.
Feeding after finish is a contract violation: it
trips a debug assertion and is ignored in release builds.
Sourcepub fn next_event(&mut self) -> Option<Event>
pub fn next_event(&mut self) -> Option<Event>
Next completed event, in stream order, if any.
Sourcepub fn finish(&mut self)
pub fn finish(&mut self)
Signal end of stream.
A block with no closing blank line is discarded, per the WHATWG
algorithm. Events completed before the end stay available through
next_event. Calling this twice is harmless.
Sourcepub fn last_event_id(&self) -> &str
pub fn last_event_id(&self) -> &str
The last event ID string: the value a client sends back as
Last-Event-ID on reconnection.
Committed at every blank line, including blocks that dispatch no event.