Skip to content

fix(server): encode undefined subscription values in SSE stream - #8146

Merged
KATT merged 2 commits into
trpc:mainfrom
tony1233200:fix-8143-sse-undefined-yield
Nov 23, 2026
Merged

KATT merged 2 commits into
trpc:mainfrom
tony1233200:fix-8143-sse-undefined-yield

Conversation

@tony1233200

@tony1233200 tony1233200 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Why

What

  • Producer writes an empty data: line instead of the literal text undefined when a chunk serializes to undefined.
  • Consumer reads an empty data: line back as undefined instead of calling JSON.parse('').
  • No change for transformer users: superjson already encodes undefined.

Links

None

Alternatives considered

  • Skipping undefined yields: drops values the subscriber expects.

Testing

  • Regression check: packages/tests/server/regression/issue-8143-sse-undefined-yield.test.ts fails without this change ("undefined" is not valid JSON) and passes with it.

AI model

  • Muse Spark

Start Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • Bug Fixes
    • Server-sent event subscriptions now correctly transmit undefined values, preserving the order of streamed results and allowing subscriptions to complete without errors.

A subscription yielding `undefined` over httpSubscriptionLink stalled:
the producer wrote the literal text `data: undefined` and the client
threw parsing it. Write an empty `data:` line instead and read it
back as `undefined`.

Closes trpc#8143
@tony1233200
tony1233200 requested a review from a team as a code owner November 21, 2026 17:43
@vercel

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
examples-todomvc Ready Ready Preview Nov 23, 2026 1:08pm UTC
next-prisma-starter Ready Ready Preview Nov 23, 2026 1:08pm UTC
trpc-next-app-dir Ready Ready Preview Nov 23, 2026 1:08pm UTC
www Ready Ready Preview Nov 23, 2026 1:08pm UTC

Request Review

@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown

@tony1233200 is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The SSE producer encodes undefined as an empty payload. The consumer maps empty message payloads to undefined and continues to parse and deserialize nonempty payloads. A regression test checks delivery of 1, undefined, and 3.

Changes: SSE undefined value handling

Layer / File(s) Summary
SSE undefined value round trip
packages/server/src/unstable-core-do-not-import/stream/sse.ts, packages/tests/server/regression/issue-8143-sse-undefined-yield.test.ts
The producer converts serialized undefined to an empty payload. The consumer maps empty message payloads to undefined and continues to parse and deserialize nonempty payloads. The regression test checks ordered data delivery, no error callback, and exactly one completion or error callback.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: katt, nick-lucas

Merge Risk: Minimal · up to d04e7

This fixes subscriptions over httpSubscriptionLink without a transformer so that a yielded undefined is delivered as undefined and the stream continues. The change is small, covered by a regression test, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(server): encode undefined subscription values in SSE stream" matches the main change. The raw summary says sseStreamProducer now encodes serialized undefined values as an empty string…
Linked Issues check Passed [#8143] Objective: a subscription that yields undefined over httpSubscriptionLink (SSE, no transformer) must deliver onData(undefined) and keep the stream open. I read `packages/server/src/unsta…
Out of Scope Changes check Passed The whole-PR diff against merge base 0813ceb changes sse.ts and adds the regression test packages/tests/server/regression/issue-8143-sse-undefined-yield.test.ts. The sse.ts changes are limited t…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "fix(server): encode undefined subscription values in SSE stream" matches the main change. The raw summary says sseStreamProducer now encodes serialized undefined values as an empty string, and the consumer reads an empty payload as undefined without JSON.parse. The added regression test for issue #8143 covers the same behavior. The title is specific and describes the primary fix.

Full details: Linked Issues check

Explanation

[#8143] Objective: a subscription that yields undefined over httpSubscriptionLink (SSE, no transformer) must deliver onData(undefined) and keep the stream open. I read packages/server/src/unstable-core-do-not-import/stream/sse.ts at the reviewed head d04e797. The producer writes JSON.stringify(serialize(chunk.data)) ?? '', so an undefined value produces an empty data: line instead of data: undefined. The consumer's 'message' listener maps msg.data === '' to undefined and skips JSON.parse, then enqueues a data result. Non-empty payloads still go through deserialize(JSON.parse(...)). The restore-id and return events use other event names and are not affected by the 'message' listener. The empty string cannot collide with real output, because JSON.stringify('') returns "". The change since the previous reviewed head (ce58908) affects the regression test, per the commit message 'test: fail with the parse error instead of a timeout'. The incremental diff could not be read because changed blobs are missing from the checkout. The test source was not read. The test summary states it waits for one completion or error callback and asserts no error and ordered delivery of 1, undefined, and 3, which matches the objective. The source code path the objective depends on is unchanged from the prior assessment at the head. The accepted tradeoff (old client with new server still fails) is listed in the issue as a known option.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against merge base 0813ceb changes sse.ts and adds the regression test packages/tests/server/regression/issue-8143-sse-undefined-yield.test.ts. The sse.ts changes are limited to the producer encoding line and the consumer empty-payload branch, both of which implement #8143. The test exercises the same behavior. The latest commit changes only the test's failure reporting, which supports the same objective. No unrelated files or refactors appear in the supplied summaries. The incremental diff could not be read, so the test-only change since the previous review is assessed from its commit message and the test summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: trpc/trpc
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 0813ceb
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2a1846fc-11a8-4121-ac15-5f0f9b8b56bd
Commits

Reviewing files that changed from the base of the PR and between ce58908 and d04e797.

Files selected for processing: 1

  • packages/tests/server/regression/issue-8143-sse-undefined-yield.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@trpc-bot trpc-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test fails on main and passes here. I changed it to wait for the stream to end, so it fails with the parse error instead of a timeout.

@KATT
KATT added this pull request to the merge queue Nov 23, 2026
@KATT
KATT removed this pull request from the merge queue due to a manual request Nov 23, 2026
@KATT
KATT added this pull request to the merge queue Nov 23, 2026
Merged via the queue into trpc:main with commit 8edb20a Nov 23, 2026
32 of 37 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 24, 2026

This branch was successfully deployed

4 active deployments
Preview – www — d04e7970 Deployed Nov 23, 2026 by vercel[bot]
Preview – trpc-next-app-dir — d04e7970 Deployed Nov 23, 2026 by vercel[bot]
Preview – next-prisma-starter — d04e7970 Deployed Nov 23, 2026 by vercel[bot]
Preview – examples-todomvc — d04e7970 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: a subscription that yields undefined ends with "undefined is not valid JSON" over httpSubscriptionLink (SSE)

3 participants