-
Notifications
You must be signed in to change notification settings - Fork 36
feat: use bufio scanner to decode events #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||||||||
| package sse | ||||||||||||
|
|
||||||||||||
| import ( | ||||||||||||
| "bufio" | ||||||||||||
| "bytes" | ||||||||||||
| "io" | ||||||||||||
| ) | ||||||||||||
|
|
@@ -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) | ||||||||||||
|
||||||||||||
| s := bufio.NewScanner(r) | |
| s := bufio.NewScanner(r) | |
| // Increase buffer size to support large SSE events (default is 64KB) | |
| const maxTokenSize = 1024 * 1024 // 1MB | |
| s.Buffer(make([]byte, 0, 64*1024), maxTokenSize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The buffer size of bufio.Scanner is 64 kB, which might not be enough for some inputs. If anything, I'd suggest using bufio.Reader and its ReadString or ReadBytes methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TODO comment on line 44 mentions that SSE lines can be separated by CRLF, LF, or CR, but there are no tests verifying CRLF handling. Since
bufio.Scannerhandles CRLF differently than the previousbytes.Splitapproach (it strips the\r), consider adding test cases with CRLF line endings to verify the decoder works correctly with Windows-style line endings.Example test case: