/// Server-sent events, as a client reads them (the HTML standard's /// `text/event-stream`, section 9.2.6): fed with a body as it arrives /// (`fetch.request_streaming` and `fetch.next`), in pieces of any size. /// /// let parser = sse.parser () /// let (parser, events) = sse.feed parser piece // [Event(name: "delta", data: "Bon", id: "")] /// /// The rules: /// - lines end with CRLF, LF or CR, even across pieces; /// - a blank line dispatches an event if any `data` came (an event with /// no `data` field is dropped); /// - `event` names it (`message` by default); /// - each `data` line adds a line to its data; /// - `id` sets the last event id, which every later event carries (an /// `id` holding a NUL is ignored); /// - `retry` (digits only) sets [`retry`]; /// - lines starting with `:` are comments, and unknown fields are /// ignored; /// - after a field's colon, one space is dropped. A line without a colon /// is a field with an empty value. /// /// The text is decoded as UTF-8, invalid bytes replaced (U+FFFD), and a /// byte order mark at the start is dropped. An event not ended by a blank /// line when the body ends is not dispatched. /// /// Reconnecting (with `Last-Event-ID`) is the caller's choice: this module /// only parses. import std/bytes import std/list import std/string /// An event: its name, its data (lines joined with LF), and the last /// event id ("" when none was set). @derive Eq Show Debug pub type Event { Event(name: String, data: String, id: String) } /// A parser's state between pieces. pub opaque type Parser { Parser( pending: Bytes , after_cr: Bool , started: Bool , name: String , lines: List , id: String , retry: Option ) } /// A parser at the start of a stream. pub fn parser :: -> Parser { Parser(bytes.empty (), False, False, "", [], "", None) } /// The reconnection time the stream asked for, in milliseconds, if any. pub fn retry :: p: Parser -> Option { p.retry } /// The parser after `piece`, and the events it completed, in order. pub fn feed :: p: Parser piece: Bytes -> (Parser, List) { let n = bytes.length piece if n == 0 { (p, []) } else { // A CR that ended the last piece and an LF that starts this one are // one line ending. let piece = if p.after_cr && bytes.read_u8 piece 0 == 10 { bytes.slice piece 1 n } else { piece } let data = bytes.concat p.pending piece let p = Parser(..p, pending: bytes.empty (), after_cr: False) if p.started { lines p data 0 0 [] } else if bytes.length data < 3 && is_bom_prefix data { (Parser(..p, pending: data), []) } else { let data = if bytes.length data >= 3 && is_bom_prefix data { bytes.slice data 3 (bytes.length data) } else { data } lines (Parser(..p, started: True)) data 0 0 [] } } } /// Whether `data` starts like UTF-8's byte order mark (EF BB BF). fn is_bom_prefix :: data: Bytes -> Bool { let bom = [239, 187, 191] let n = if bytes.length data < 3 { bytes.length data } else { 3 } list.all (list.zip (list.take bom n) (list.range 0 (n - 1))) fn p { bytes.read_u8 data p.1 == p.0 } } /// Splits `data` into lines from `start`, `at` the next byte to look at; /// what follows the last line ending waits for the next piece. fn lines :: p: Parser data: Bytes start: Int at: Int events: List -> (Parser, List) { let n = bytes.length data if at >= n { (Parser(..p, pending: bytes.slice data start n), list.reverse events) } else { let b = bytes.read_u8 data at if b == 10 || b == 13 { let (p, events) = line p (bytes.slice data start at) events if b == 10 { lines p data (at + 1) (at + 1) events } else if at + 1 >= n { (Parser(..p, after_cr: True), list.reverse events) } else { let next = if bytes.read_u8 data (at + 1) == 10 { at + 2 } else { at + 1 } lines p data next next events } } else { lines p data start (at + 1) events } } } /// One line: a blank one dispatches, a comment is skipped, anything else /// is a field. fn line :: p: Parser text: Bytes events: List -> (Parser, List) { let n = bytes.length text if n == 0 { dispatch p events } else if bytes.read_u8 text 0 == 58 { (p, events) } else { let (field, value) = match colon text 0 { Some(i) -> (bytes.slice text 0 i, bytes.slice text (i + 1) n) , None -> (text, bytes.empty ()) } let value = if bytes.length value > 0 && bytes.read_u8 value 0 == 32 { bytes.slice value 1 (bytes.length value) } else { value } (apply p (bytes.to_string_lossy field) value, events) } } fn colon :: text: Bytes at: Int -> Option { if at >= bytes.length text { None } else if bytes.read_u8 text at == 58 { Some(at) } else { colon text (at + 1) } } fn apply :: p: Parser field: String value: Bytes -> Parser { let text = bytes.to_string_lossy value match field { "event" -> Parser(..p, name: text) , "data" -> Parser(..p, lines: [text, ..p.lines]) , "id" -> if has_byte value 0 { p } else { Parser(..p, id: text) } , "retry" -> if is_digits value { match string.to_int text { Ok(ms) -> Parser(..p, retry: Some(ms)), Error(_) -> p } } else { p } , _ -> p } } fn has_byte :: data: Bytes b: Int -> Bool { list.any (bytes.to_list data) fn x { x == b } } fn is_digits :: data: Bytes -> Bool { bytes.length data > 0 && list.all (bytes.to_list data) fn x { x >= 48 && x <= 57 } } /// The event the buffered fields make, if any `data` came; the name and /// the data start again, the id stays. fn dispatch :: p: Parser events: List -> (Parser, List) { let reset = Parser(..p, name: "", lines: []) match p.lines { [] -> (reset, events) , _ -> { let name = if p.name == "" { "message" } else { p.name } let event = Event(name, string.join (list.reverse p.lines) "\n", p.id) (reset, [event, ..events]) } } }