Next blocks until a complete frame is read and returns it. It returns io.EOF when the stream ends; a frame that is not terminated by a blank line before the end is discarded. Spec: a frame without an event field, or with an empty one, is returned with the event type "message", which is the default
()
| 54 | // Spec: a frame without an event field, or with an empty one, is returned with the event type "message", which is |
| 55 | // the default type of a dispatched event (9.2.6, dispatch step 5). |
| 56 | func (r *FrameReader) Next() (Frame, error) { |
| 57 | // Spec: the data and event type buffers of 9.2.6. The data buffer holds the values joined with LF rather than |
| 58 | // each followed by one, so hasData stands in for the buffer being non-empty and no trailing LF has to be removed. |
| 59 | var data strings.Builder |
| 60 | var hasData bool |
| 61 | var event string |
| 62 | |
| 63 | for r.scanner.Scan() { |
| 64 | line := r.scanner.Text() |
| 65 | if !r.started { |
| 66 | r.started = true |
| 67 | // Spec: the stream may start with a UTF-8 byte order mark (9.2.5), and decoding strips it (9.2.6), so it |
| 68 | // is not read as part of the first field name. Our API does not send one. |
| 69 | line = strings.TrimPrefix(line, "\ufeff") |
| 70 | } |
| 71 | |
| 72 | // Spec: a blank line dispatches the event (9.2.6). With an empty data buffer nothing is dispatched, and the |
| 73 | // event type buffer is reset so it does not leak into the next frame (dispatch step 2). |
| 74 | if line == "" { |
| 75 | if !hasData { |
| 76 | event = "" |
| 77 | continue |
| 78 | } |
| 79 | // Spec: the type defaults to "message" unless the event type buffer is set (9.2.6, dispatch steps 4 and 5). |
| 80 | if event == "" { |
| 81 | event = "message" |
| 82 | } |
| 83 | return Frame{ID: r.lastID, Event: event, Data: data.String()}, nil |
| 84 | } |
| 85 | |
| 86 | // Spec: a line starting with a colon is ignored (9.2.6). Reporting it is our addition, used for heartbeats. |
| 87 | if comment, ok := strings.CutPrefix(line, ":"); ok { |
| 88 | if r.OnComment != nil { |
| 89 | r.OnComment(strings.TrimPrefix(comment, " ")) |
| 90 | } |
| 91 | continue |
| 92 | } |
| 93 | |
| 94 | // Spec: the field name is everything before the first colon, or the whole line when there is none, in which |
| 95 | // case the value is empty. One leading space is removed from the value (9.2.6). |
| 96 | field, value, _ := strings.Cut(line, ":") |
| 97 | value = strings.TrimPrefix(value, " ") |
| 98 | |
| 99 | // Spec: field names are compared literally, and unknown fields are ignored (9.2.6, process the field). |
| 100 | switch field { |
| 101 | case "event": |
| 102 | event = value |
| 103 | case "data": |
| 104 | if hasData { |
| 105 | data.WriteByte('\n') |
| 106 | } |
| 107 | data.WriteString(value) |
| 108 | hasData = true |
| 109 | case "id": |
| 110 | // Spec: an ID containing NULL is ignored. The last event ID buffer is never reset between frames, so |
| 111 | // later frames without an id field keep this value, and an empty id field clears it (9.2.6). |
| 112 | if !strings.ContainsRune(value, 0) { |
| 113 | r.lastID = value |