Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions sse-decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package sse

import (
"bufio"
"bytes"
"io"
)
Expand Down Expand Up @@ -37,19 +38,16 @@ func (d *decoder) dispatchEvent(event Event, data string) {
}

func (d *decoder) decode(r io.Reader) ([]Event, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}

var currentEvent Event
dataBuffer := new(bytes.Buffer)
// TODO (and unit tests)
// Lines must be separated by either a U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair,
// a single U+000A LINE FEED (LF) character,
// or a single U+000D CARRIAGE RETURN (CR) character.
lines := bytes.Split(buf, []byte{'\n'})
for _, line := range lines {
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Bytes()

if len(line) == 0 {
// If the line is empty (a blank line). Dispatch the event.
d.dispatchEvent(currentEvent, dataBuffer.String())
Expand Down Expand Up @@ -113,5 +111,5 @@ func (d *decoder) decode(r io.Reader) ([]Event, error) {
// Once the end of the file is reached, the user agent must dispatch the event one final time.
d.dispatchEvent(currentEvent, dataBuffer.String())

return d.events, nil
return d.events, s.Err()
}