Skip to content

Commit da9af8b

Browse files
authored
fix: decoding chunks in http event stream (#1356)
Related: https://discordapp.com/channels/1308966753044398161/1308971636539658260/1460548346702987358 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed event parser to properly handle events split across multiple data chunks, ensuring incomplete message fragments are retained until complete event assembly. * **Tests** * Restructured decoder tests to validate mixed chunk scenarios and event parsing across multiple feeds. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 95c582a commit da9af8b

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

packages/standard-server/src/event-iterator/decoder.test.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,24 @@ describe('decodeEventMessage', () => {
8484
})
8585

8686
describe('eventDecoder', () => {
87-
it('on success', () => {
87+
it('on success with mixed chunks', () => {
8888
const onEvent = vi.fn()
8989

9090
const decoder = new EventDecoder({ onEvent })
9191

92-
decoder.feed('event: message\ndata: hello1\ndata: world\n\n')
93-
decoder.feed('event: message\ndata: hello2\ndata: world\nid: 123\nretry: 10000\n\n')
94-
decoder.feed('event: message\ndata: hello3\ndata: world\nid: 123\nretry: 10000\n\n')
95-
96-
decoder.feed('event: done\n')
97-
decoder.feed('data: hello4\n')
92+
decoder.feed('event: message\n')
93+
decoder.feed('data: hello1\n')
9894
decoder.feed('data: world\n\n')
95+
96+
decoder.feed('event: message\ndata: hello2\ndata: world\n\n')
97+
// NOTE: a chunk contain 1,5 events is important test, carefully when modify
98+
decoder.feed('event: message\ndata: hello3\ndata: world\n\nevent: message\ndata: hello4\n')
99+
decoder.feed('data: world\nid: 123\nretry: 10000\n\nevent: done\ndata: hello5\ndata: world\nid: 123\nretry: 10000\n')
100+
decoder.feed('\n')
101+
99102
decoder.end()
100103

101-
expect(onEvent).toHaveBeenCalledTimes(4)
104+
expect(onEvent).toHaveBeenCalledTimes(5)
102105
expect(onEvent).toHaveBeenNthCalledWith(1, {
103106
data: 'hello1\nworld',
104107
event: 'message',
@@ -109,22 +112,29 @@ describe('eventDecoder', () => {
109112
expect(onEvent).toHaveBeenNthCalledWith(2, {
110113
data: 'hello2\nworld',
111114
event: 'message',
112-
id: '123',
113-
retry: 10000,
115+
id: undefined,
116+
retry: undefined,
114117
comments: [],
115118
})
116119
expect(onEvent).toHaveBeenNthCalledWith(3, {
117120
data: 'hello3\nworld',
118121
event: 'message',
119-
id: '123',
120-
retry: 10000,
122+
id: undefined,
123+
retry: undefined,
121124
comments: [],
122125
})
123126
expect(onEvent).toHaveBeenNthCalledWith(4, {
124127
data: 'hello4\nworld',
128+
event: 'message',
129+
id: '123',
130+
retry: 10000,
131+
comments: [],
132+
})
133+
expect(onEvent).toHaveBeenNthCalledWith(5, {
134+
data: 'hello5\nworld',
125135
event: 'done',
126-
id: undefined,
127-
retry: undefined,
136+
id: '123',
137+
retry: 10000,
128138
comments: [],
129139
})
130140
})

packages/standard-server/src/event-iterator/decoder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ export class EventDecoder {
7878
this.options.onEvent(message)
7979
}
8080
}
81-
82-
this.incomplete = ''
8381
}
8482

8583
end(): void {

0 commit comments

Comments
 (0)