|
1 | | -import { beforeAll, describe, it, expect } from 'vitest' |
| 1 | +import { beforeAll, describe, it, expect, vi } from 'vitest' |
2 | 2 | import { feathers } from '../index.js' |
3 | 3 | import { clientTests } from '../../fixtures/client.js' |
4 | 4 | import { NotAcceptable, NotFound, MethodNotAllowed, BadRequest } from '../errors.js' |
5 | 5 |
|
6 | 6 | import { getApp, createTestServer, TestServiceTypes, verify } from '../../fixtures/index.js' |
7 | | -import { fetchClient } from './index.js' |
| 7 | +import { fetchClient, FetchClient } from './index.js' |
8 | 8 |
|
9 | 9 | describe('fetch REST connector', function () { |
10 | 10 | const port = 8888 |
@@ -211,3 +211,135 @@ describe('fetch REST connector', function () { |
211 | 211 |
|
212 | 212 | clientTests(app, 'todos') |
213 | 213 | }) |
| 214 | + |
| 215 | +describe('FetchClient.handleEventStream', () => { |
| 216 | + /** |
| 217 | + * Creates a mock Response with a ReadableStream that emits chunks |
| 218 | + * simulating TCP fragmentation of SSE data |
| 219 | + */ |
| 220 | + function createChunkedSSEResponse(chunks: string[]): Response { |
| 221 | + const encoder = new TextEncoder() |
| 222 | + let chunkIndex = 0 |
| 223 | + |
| 224 | + const stream = new ReadableStream({ |
| 225 | + pull(controller) { |
| 226 | + if (chunkIndex < chunks.length) { |
| 227 | + controller.enqueue(encoder.encode(chunks[chunkIndex])) |
| 228 | + chunkIndex++ |
| 229 | + } else { |
| 230 | + controller.close() |
| 231 | + } |
| 232 | + } |
| 233 | + }) |
| 234 | + |
| 235 | + return new Response(stream, { |
| 236 | + headers: { 'content-type': 'text/event-stream' } |
| 237 | + }) |
| 238 | + } |
| 239 | + |
| 240 | + it('handles SSE events split across chunks', async () => { |
| 241 | + // Simulate TCP fragmentation where JSON is split mid-object |
| 242 | + const chunks = ['data: {"message":"Hel', 'lo"}\n\ndata: {"message":" wor', 'ld"}\n\n'] |
| 243 | + |
| 244 | + const client = new FetchClient({ |
| 245 | + name: 'test', |
| 246 | + baseUrl: 'http://localhost', |
| 247 | + connection: fetch, |
| 248 | + stringify: (q) => '' |
| 249 | + }) |
| 250 | + |
| 251 | + const response = createChunkedSSEResponse(chunks) |
| 252 | + const messages: any[] = [] |
| 253 | + |
| 254 | + for await (const data of client.handleEventStream(response)) { |
| 255 | + messages.push(data) |
| 256 | + } |
| 257 | + |
| 258 | + expect(messages).toHaveLength(2) |
| 259 | + expect(messages[0]).toEqual({ message: 'Hello' }) |
| 260 | + expect(messages[1]).toEqual({ message: ' world' }) |
| 261 | + }) |
| 262 | + |
| 263 | + it('handles multiple events in a single chunk', async () => { |
| 264 | + const chunks = ['data: {"a":1}\n\ndata: {"b":2}\n\ndata: {"c":3}\n\n'] |
| 265 | + |
| 266 | + const client = new FetchClient({ |
| 267 | + name: 'test', |
| 268 | + baseUrl: 'http://localhost', |
| 269 | + connection: fetch, |
| 270 | + stringify: (q) => '' |
| 271 | + }) |
| 272 | + |
| 273 | + const response = createChunkedSSEResponse(chunks) |
| 274 | + const messages: any[] = [] |
| 275 | + |
| 276 | + for await (const data of client.handleEventStream(response)) { |
| 277 | + messages.push(data) |
| 278 | + } |
| 279 | + |
| 280 | + expect(messages).toHaveLength(3) |
| 281 | + expect(messages[0]).toEqual({ a: 1 }) |
| 282 | + expect(messages[1]).toEqual({ b: 2 }) |
| 283 | + expect(messages[2]).toEqual({ c: 3 }) |
| 284 | + }) |
| 285 | + |
| 286 | + it('handles event split at delimiter boundary', async () => { |
| 287 | + // Split right at the \n\n boundary |
| 288 | + const chunks = ['data: {"first":true}\n', '\ndata: {"second":true}\n\n'] |
| 289 | + |
| 290 | + const client = new FetchClient({ |
| 291 | + name: 'test', |
| 292 | + baseUrl: 'http://localhost', |
| 293 | + connection: fetch, |
| 294 | + stringify: (q) => '' |
| 295 | + }) |
| 296 | + |
| 297 | + const response = createChunkedSSEResponse(chunks) |
| 298 | + const messages: any[] = [] |
| 299 | + |
| 300 | + for await (const data of client.handleEventStream(response)) { |
| 301 | + messages.push(data) |
| 302 | + } |
| 303 | + |
| 304 | + expect(messages).toHaveLength(2) |
| 305 | + expect(messages[0]).toEqual({ first: true }) |
| 306 | + expect(messages[1]).toEqual({ second: true }) |
| 307 | + }) |
| 308 | + |
| 309 | + it('handles multi-byte UTF-8 characters split across chunks', async () => { |
| 310 | + // UTF-8 encoding of emoji can be split across chunks |
| 311 | + const fullMessage = 'data: {"emoji":"🎉"}\n\n' |
| 312 | + const bytes = new TextEncoder().encode(fullMessage) |
| 313 | + // Split in the middle of the emoji (which is 4 bytes in UTF-8) |
| 314 | + const chunk1 = bytes.slice(0, 18) // cuts into the emoji |
| 315 | + const chunk2 = bytes.slice(18) |
| 316 | + |
| 317 | + const stream = new ReadableStream({ |
| 318 | + start(controller) { |
| 319 | + controller.enqueue(chunk1) |
| 320 | + controller.enqueue(chunk2) |
| 321 | + controller.close() |
| 322 | + } |
| 323 | + }) |
| 324 | + |
| 325 | + const response = new Response(stream, { |
| 326 | + headers: { 'content-type': 'text/event-stream' } |
| 327 | + }) |
| 328 | + |
| 329 | + const client = new FetchClient({ |
| 330 | + name: 'test', |
| 331 | + baseUrl: 'http://localhost', |
| 332 | + connection: fetch, |
| 333 | + stringify: (q) => '' |
| 334 | + }) |
| 335 | + |
| 336 | + const messages: any[] = [] |
| 337 | + |
| 338 | + for await (const data of client.handleEventStream(response)) { |
| 339 | + messages.push(data) |
| 340 | + } |
| 341 | + |
| 342 | + expect(messages).toHaveLength(1) |
| 343 | + expect(messages[0]).toEqual({ emoji: '🎉' }) |
| 344 | + }) |
| 345 | +}) |
0 commit comments