Skip to content

Conversation

@qiao
Copy link

@qiao qiao commented Jan 3, 2026

Context

I was trying to use Upstash SDK with Redis PubSub commands on Cloudflare Workers and was not able to receive the messages. After digging into the Upstash SDK source code and network packets I was able to find that the worker runtime was splitting the SSE message into multiple chunks in the middle of the line (which should be permitted), but the Upstash SDK was not properly handle this situation, causing the message to be silently dropped.

Problem

The streaming parser in the Upstash SDK processed each network chunk independently, splitting on newlines without buffering partial lines. This was incorrect because the stream reader is not guaranteed to emit chunks aligned with newlines (as in the Cloudflare case above). Chunks can be generated at arbitrary byte boundaries, including mid-line. When a message arrived split across multiple chunks, the parser attempted to process incomplete data, resulting in JSON parse errors and lost messages.

Example scenario:

A subscription receives the following message split across two chunks:

Chunk 1: data: {"id":"msg1","co
Chunk 2: ntent":"hello"}\n

Previous behavior:

  • Attempts to parse {"id":"msg1","co → incomplete message, dropped.
  • Attempts to parse ntent":"hello"} → incomplete message, dropped
  • Message lost

Fix

Implemented line buffering to accumulate partial data between chunks:

  1. Added a buffer variable to store incomplete lines
  2. Modified the decoder to use { stream: true } for proper streaming text decoding (see TextDecoder.decode() documentation)
  3. Updated line processing logic:
    • Append each chunk to the buffer
    • Split on \n to extract complete lines
    • Process only complete lines
    • Retain the last incomplete line in the buffer for the next chunk

The stream: true option indicates that additional data will follow in subsequent calls to decode(), which is essential for correctly handling multi-byte characters that may be split across chunk boundaries.

CahidArda added a commit that referenced this pull request Jan 7, 2026
borrowed from #1403
@CahidArda
Copy link
Contributor

Hi @qiao,

Thanks for the report and the fix. We will continue under #1404 for the fix.

Thanks a lot!

@CahidArda CahidArda closed this Jan 8, 2026
CahidArda added a commit that referenced this pull request Jan 8, 2026
* feat: support chunked messages

* fix: function test

* fix: add test

borrowed from #1403

---------

Co-authored-by: CahidArda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants