Skip to content
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

fix(go): handle missing delimiter case during stream parse method. #5492

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions fern/pages/changelogs/go-sdk/2024-12-27.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.35.1
**`(fix):`** Fixes an issue where the delimiter length was included during stream data read, even when the delimiter was missing, leading to a `bufio.ErrAdvanceTooFar` error

17 changes: 10 additions & 7 deletions generators/go/internal/generator/sdk/core/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,21 @@ func (s *scannerStreamReader) ReadFromStream() ([]byte, error) {
}

func (s *scannerStreamReader) parse(bytes []byte) (int, []byte, error) {
var start int
var startIndex int
if s.options != nil && s.options.prefix != "" {
if i := strings.Index(string(bytes), s.options.prefix); i >= 0 {
start = i + len(s.options.prefix)
startIndex = i + len(s.options.prefix)
}
}
data := bytes[start:]
if i := strings.Index(string(data), s.options.delimiter); i >= 0 {
data = data[:i+len(s.options.delimiter)]
data := bytes[startIndex:]
delimIndex := strings.Index(string(data), s.options.delimiter)
if delimIndex < 0 {
return startIndex + len(data), data, nil
}
n := start + len(data) + len(s.options.delimiter)
return n, data, nil
endIndex := delimIndex + len(s.options.delimiter)
parsedData := data[:endIndex]
n := startIndex + endIndex
return n, parsedData, nil
}

func (s *scannerStreamReader) isTerminated(bytes []byte) bool {
Expand Down
7 changes: 7 additions & 0 deletions generators/go/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- version: 0.35.1
changelogEntry:
- type: fix
summary: >-
Fixes an issue where the delimiter length was included during stream data read, even when
the delimiter was missing, leading to a `bufio.ErrAdvanceTooFar` error
irVersion: 53
- version: 0.35.0
changelogEntry:
- type: feat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,14 @@ export class Stream<T> implements AsyncIterable<T> {
buf += this.decodeChunk(chunk);

let terminatorIndex: number;
// Parse the chunk into as many messages as possible
while ((terminatorIndex = buf.indexOf(this.messageTerminator)) >= 0) {
// Extract the line from the buffer
let line = buf.slice(0, terminatorIndex + 1);
buf = buf.slice(terminatorIndex + this.messageTerminator.length);

// Skip empty lines
if (!line.trim()) {
continue;
}

// Skip the chunk until the prefix is found
if (!prefixSeen && this.prefix != null) {
const prefixIndex = line.indexOf(this.prefix);
if (prefixIndex === -1) {
Expand All @@ -85,12 +81,9 @@ export class Stream<T> implements AsyncIterable<T> {
line = line.slice(prefixIndex + this.prefix.length);
}

// If the stream terminator is present, return
if (this.streamTerminator != null && line.includes(this.streamTerminator)) {
return;
}

// Otherwise, yield message from the prefix to the terminator
const message = await this.parse(JSON.parse(line));
yield message;
prefixSeen = false;
Expand Down
1 change: 1 addition & 0 deletions seed/go-sdk/grpc-proto-exhaustive/core/request_option.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-sdk/grpc-proto/core/request_option.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-sdk/pagination/.mock/definition/users.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions seed/go-sdk/server-sent-event-examples/core/stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions seed/go-sdk/server-sent-events/core/stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions seed/go-sdk/streaming/core/stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading