Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@marshallswain
Copy link
Member

Fixes a bug in FetchClient.handleEventStream where SSE events split across TCP chunks would fail to parse.

Problem

TCP chunks don't align with SSE event boundaries. When streaming responses arrive, a single reader.read() call might return partial JSON:

  • Chunk 1: data: {"response":"Hel
  • Chunk 2: lo"}\n\ndata: {"response":" world"}\n\n

The previous implementation tried to parse each chunk independently, causing JSON.parse to fail on incomplete data.

Solution

Buffer incoming data and only parse complete SSE events (those terminated by \n\n):

let buffer = ''

while (true) {
  const { value, done } = await reader.read()
  if (done) break

  buffer += decoder.decode(value, { stream: true })

  const events = buffer.split('\n\n')
  buffer = events.pop() || '' // Keep incomplete event in buffer

  for (const event of events) {
    // Parse complete events
  }
}

Changes

  • fix: Buffer SSE stream chunks before parsing in FetchClient.handleEventStream
  • test: Add tests for chunked SSE handling (split events, multiple events per chunk, delimiter boundary splits, multi-byte UTF-8)
  • docs: Add "Streaming Responses (SSE)" section to REST client documentation

Testing

All existing tests pass plus 4 new tests:

  • handles SSE events split across chunks
  • handles multiple events in a single chunk
  • handles event split at delimiter boundary
  • `handles multi-byte UTF-8 characters split across chunks

The handleEventStream method was parsing each chunk independently,
but TCP chunks don't align with SSE event boundaries. This caused
JSON parse errors when events were split across chunks.

For example:
- Chunk 1: data: {"response":"Hel
- Chunk 2: lo"}\n\ndata: {"response":" world"}\n\n

The fix buffers incoming data and only parses complete events
(those ending with the SSE delimiter \n\n).
Tests verify handleEventStream correctly handles:
- Events split across TCP chunks
- Multiple events in a single chunk
- Events split at the \n\n delimiter boundary
- Multi-byte UTF-8 characters split across chunks
Documents how to consume streaming responses from services that
return async generators. Includes examples for AI/LLM streaming,
progress updates, and notes about automatic SSE buffering.
@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
feathers-v6 ccde4ca Commit Preview URL

Branch Preview URL
Jan 29 2026, 04:02 AM

@daffl daffl merged commit e3cc58c into v6 Jan 29, 2026
5 checks passed
@daffl daffl deleted the fix/sse-stream-buffering branch January 29, 2026 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.