@quidgest/chatbot 0.5.8 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +5 -5
- package/dist/index.mjs +285 -281
- package/package.json +1 -1
- package/src/composables/__tests__/useSSE.spec.ts +96 -0
- package/src/composables/useSSE.ts +7 -1
package/package.json
CHANGED
|
@@ -129,4 +129,100 @@ describe('useSSE', () => {
|
|
|
129
129
|
expect(consoleWarnSpy).toHaveBeenCalledWith('Unknown event type: unknown_event')
|
|
130
130
|
consoleWarnSpy.mockRestore()
|
|
131
131
|
})
|
|
132
|
+
|
|
133
|
+
// Tests for handling events split across chunks (bug reproduction)
|
|
134
|
+
describe('Events split across multiple chunks', () => {
|
|
135
|
+
it('should handle event split in the middle of data field', async () => {
|
|
136
|
+
// Simulate network splitting an event across two chunks
|
|
137
|
+
const stream = createMockStream(['event: message\ndata: {"val', 'ue":"hello"}\n\n'])
|
|
138
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
139
|
+
|
|
140
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
141
|
+
|
|
142
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('hello')
|
|
143
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('should handle multiple events split across multiple chunks', async () => {
|
|
147
|
+
// Simulate the real scenario from the bug report
|
|
148
|
+
const stream = createMockStream([
|
|
149
|
+
'event: message\ndata: {"value":"The"}\n\nevent: message\ndata: {"val',
|
|
150
|
+
'ue":" capital"}\n\nevent: message\ndata: {"value":" of"}\n\n',
|
|
151
|
+
'event: message\ndata: {"value":" France"}\n\n'
|
|
152
|
+
])
|
|
153
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
154
|
+
|
|
155
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
156
|
+
|
|
157
|
+
// Should receive all 4 messages
|
|
158
|
+
expect(handlers.onMessage).toHaveBeenCalledTimes(4)
|
|
159
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(1, 'The')
|
|
160
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(2, ' capital')
|
|
161
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(3, ' of')
|
|
162
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(4, ' France')
|
|
163
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('should handle event split between event name and data', async () => {
|
|
167
|
+
const stream = createMockStream([
|
|
168
|
+
'event: message\n',
|
|
169
|
+
'data: {"value":"split event"}\n\n'
|
|
170
|
+
])
|
|
171
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
172
|
+
|
|
173
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
174
|
+
|
|
175
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('split event')
|
|
176
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('should handle event split right after event separator', async () => {
|
|
180
|
+
const stream = createMockStream([
|
|
181
|
+
'event: message\ndata: {"value":"first"}\n\n',
|
|
182
|
+
'event: message\ndata: {"value":"second"}\n\n'
|
|
183
|
+
])
|
|
184
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
185
|
+
|
|
186
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
187
|
+
|
|
188
|
+
expect(handlers.onMessage).toHaveBeenCalledTimes(2)
|
|
189
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(1, 'first')
|
|
190
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(2, 'second')
|
|
191
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('should handle complex JSON split across chunks', async () => {
|
|
195
|
+
const stream = createMockStream([
|
|
196
|
+
'event: field_metadata\ndata: {"foo":"bar","nested":{"key',
|
|
197
|
+
'":"value"}}\n\n'
|
|
198
|
+
])
|
|
199
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
200
|
+
|
|
201
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
202
|
+
|
|
203
|
+
expect(handlers.onFieldMetadata).toHaveBeenCalledWith({
|
|
204
|
+
foo: 'bar',
|
|
205
|
+
nested: { key: 'value' }
|
|
206
|
+
})
|
|
207
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('should handle event split in very small chunks', async () => {
|
|
211
|
+
// Extreme case: each character in a separate chunk
|
|
212
|
+
const stream = createMockStream([
|
|
213
|
+
'event: ',
|
|
214
|
+
'message\n',
|
|
215
|
+
'data: ',
|
|
216
|
+
'{"value":',
|
|
217
|
+
'"test"}',
|
|
218
|
+
'\n\n'
|
|
219
|
+
])
|
|
220
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
221
|
+
|
|
222
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
223
|
+
|
|
224
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('test')
|
|
225
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
226
|
+
})
|
|
227
|
+
})
|
|
132
228
|
})
|
|
@@ -30,6 +30,7 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
30
30
|
|
|
31
31
|
const reader = stream.getReader()
|
|
32
32
|
const decoder = new TextDecoder()
|
|
33
|
+
let buffer = '' // Buffer to accumulate incomplete events across chunks
|
|
33
34
|
|
|
34
35
|
while (true) {
|
|
35
36
|
const { done, value } = await reader.read()
|
|
@@ -39,7 +40,12 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
const chunk = decoder.decode(value, { stream: true })
|
|
42
|
-
|
|
43
|
+
buffer += chunk // Append chunk to buffer
|
|
44
|
+
const events = buffer.split(/\n\n+/)
|
|
45
|
+
|
|
46
|
+
// Keep the last element in buffer (might be incomplete)
|
|
47
|
+
// Process all complete events (all except the last one)
|
|
48
|
+
buffer = events.pop() || ''
|
|
43
49
|
|
|
44
50
|
for (const eventBlock of events) {
|
|
45
51
|
const lines = eventBlock.trim().split('\n')
|