@std/sse.ms#
Import each required symbol with import Name from "@std/sse.ms".
SseDecoder Record#
Incremental decoder state. Create it with decoder, then pass chunks to feed and end with finish.
Only retry is meant to be read; the other fields are internal state.
type SseDecoder:
limits: SseLimits
retry: Optional[Int]
last_id: Optional[String]
line: BytesBuilder
line_bytes: Int
event_bytes: Int
carriage: Bool
started: Bool
open_block: Bool
event_type: String
data: List[String]
failure: Optional[SseError]
closed: BoolSseDecoder.carriage#
Internal: true when the previous byte was CR, so an immediately following LF is part of the same ending.
carriage: BoolSseDecoder.closed#
Internal: true after finish.
closed: BoolSseDecoder.data#
Internal: the pending data: lines.
data: List[String]SseDecoder.event_bytes#
Internal: bytes counted against the event budget.
event_bytes: IntSseDecoder.event_type#
Internal: the pending event: value, empty when unset.
event_type: StringSseDecoder.failure#
Internal: the first failure, repeated by later calls.
failure: Optional[SseError]SseDecoder.last_id#
Internal: the last event ID, which persists across events.
last_id: Optional[String]SseDecoder.limits#
Limits fixed when the decoder was created.
limits: SseLimitsSseDecoder.line#
Internal: bytes of the line being assembled.
line: BytesBuilderSseDecoder.line_bytes#
Internal: length of the line being assembled.
line_bytes: IntSseDecoder.open_block#
Internal: true while field lines have arrived since the last blank line.
open_block: BoolSseDecoder.retry#
The latest valid retry: value in milliseconds, or none if the stream sent none.
retry: Optional[Int]SseDecoder.started#
Internal: true once the first line has been assembled and the byte order mark has been considered.
started: BoolSseError Alternative#
Errors returned for invalid input, malformed or oversized event streams, and failed reads.
type SseError:
case InvalidInput
case InvalidData
case LimitExceeded
case Truncated
case ReadFailed(cause: IoError)impl Error for SseError:
fn message() -> String:SseError.InvalidData#
A line is not valid UTF-8 text.
case InvalidDataSseError.InvalidInput#
A supplied limit or chunk size is not positive, or the decoder was used after finish.
case InvalidInputSseError.LimitExceeded#
A line or an event exceeds a supplied byte budget.
case LimitExceededSseError.ReadFailed#
The underlying stream failed to read; the cause keeps its original I/O case.
case ReadFailed(cause: IoError)SseError.Truncated#
The stream ended inside a line or before the blank line that completes an event.
case TruncatedSseError.ReadFailed.cause#
The error returned by the borrowed stream.
cause: IoErrorSseError.Error.message#
Returns a fixed diagnostic that does not echo stream contents.
fn message() -> String:SseEvent Record#
One dispatched event, with the message type used when no event: field was sent.
type SseEvent:
event: String
data: String
id: Optional[String]SseEvent.data#
Every data: line of the event joined with LF; a bare data: line gives an empty line.
data: StringSseEvent.event#
The event type from the last event: field, or message.
event: StringSseEvent.id#
The last event ID in effect when the event was dispatched, or none if the stream never set one.
id: Optional[String]SseLimits Record#
Optional byte budgets for one line and for one event.
type SseLimits:
max_line_bytes: Optional[Int] = none
max_event_bytes: Optional[Int] = noneSseLimits.max_event_bytes#
Largest total of all field lines in one event, excluding line endings and comments; none adds no budget.
max_event_bytes: Optional[Int] = noneSseLimits.max_line_bytes#
Largest line in bytes, excluding its line ending; none adds no budget.
max_line_bytes: Optional[Int] = noneSseReader Record#
Pulls events from a borrowed ReadStream. The reader never closes the stream: its owner closes it, and closing it makes a blocked read fail with ReadFailed.
type SseReader:
input: ReadStream
decoder: SseDecoder
ready: List[SseEvent]
next: Int
chunk_bytes: Optional[Int]
finished: BoolSseReader.chunk_bytes#
Internal: requested read size, none for the stream default.
chunk_bytes: Optional[Int]SseReader.decoder#
The decoder, whose retry is readable between events.
decoder: SseDecoderSseReader.finished#
Internal: true after a clean end of stream.
finished: BoolSseReader.input#
The borrowed stream, such as an HTTP response body.
input: ReadStreamSseReader.next#
Internal: index of the next ready event.
next: IntSseReader.ready#
Internal: events decoded but not yet returned.
ready: List[SseEvent]borrow_body Function#
Views an HTTP response body as a ReadStream that the response keeps owning.
Closing the view does nothing, so close the body itself, usually with with.
fn borrow_body(body: ResponseBody) -> ReadStream:decoder Function#
Creates a decoder. Supplied limits must be positive.
fn decoder(limits: SseLimits = SseLimits()) -> Result[SseDecoder, SseError]:feed Function#
Decodes one chunk and returns the events it completed, in order. Chunks may split any line, line ending or UTF-8 scalar. An error is final for the decoder.
fn feed(decoder: SseDecoder, chunk: Bytes) -> Result[List[SseEvent], SseError]:finish Function#
Ends the stream. A stream with no partial line and no unfinished event ends cleanly; otherwise the result is Truncated and the unfinished event is never dispatched.
fn finish(decoder: SseDecoder) -> Result[Unit, SseError]:next_event Function#
Returns the next event, or none after a clean end of stream. A read failure, malformed line, exceeded limit or truncated event is an error.
fn next_event(reader: SseReader) -> Result[Optional[SseEvent], SseError]:reader Function#
Wraps a stream in an event reader. Supplied limits and chunk size must be positive.
fn reader(input: ReadStream, limits: SseLimits = SseLimits(), chunk_bytes: Optional[Int] = none) -> Result[SseReader, SseError]: