fix(standard-server): make event iterator encoder/decoder SSE spec compliant - #1742
Conversation
…mpliant Port the event stream encoder/decoder logic from the standardserver repo: - Decoder recognizes LF, CR, and CRLF line endings and message delimiters, including delimiters split across chunk boundaries - Decoder strips only a single U+0020 space after the colon per spec (previously any single whitespace character, e.g. tabs) - Decoder buffers chunks in an array joined lazily and only re-scans the last 3 characters for delimiters straddling a boundary - Encoder splits data on \r and \r\n in addition to \n - Encoder assertions reject carriage returns in id/event/comment, preventing field injection into the stream No public API changes: all exported names, signatures, and the EventMessage shape are unchanged.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/experimental-pino
@orpc/experimental-publisher
@orpc/experimental-publisher-durable-object
@orpc/experimental-ratelimit
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fastify
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR updates @orpc/standard-server’s event-iterator SSE encoder/decoder to be compliant with the WHATWG Server-Sent Events event-stream parsing rules, particularly around CR/CRLF handling, chunk-boundary correctness, and field-value whitespace semantics.
Changes:
- Tighten encoder validation to reject carriage returns (in addition to newlines) in
id/event/commentvalues and encodedatawith correct CR/CRLF normalization. - Rewrite the decoder to correctly interpret LF/CR/CRLF line endings and reliably detect message delimiters across arbitrary chunk boundaries.
- Expand unit tests to cover CR/CRLF, delimiter splits at all positions, chunk-size invariance, and encode→decode round-trips.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/standard-server/src/event-iterator/meta.test.ts | Updates assertions to reflect new CR/CRLF-aware validation error messages. |
| packages/standard-server/src/event-iterator/encoder.ts | Adds CR/CRLF detection in field validation and makes data encoding SSE-spec compliant for CR/CRLF inputs. |
| packages/standard-server/src/event-iterator/encoder.test.ts | Adds coverage for CR/CRLF in data encoding, comment validation, and encode→decode round-trips. |
| packages/standard-server/src/event-iterator/decoder.ts | Implements spec-compliant line ending handling and robust delimiter detection across chunk boundaries. |
| packages/standard-server/src/event-iterator/decoder.test.ts | Adds thorough tests for CR/CRLF support, delimiter split edge-cases, and chunk-size invariance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — ports the SSE encoder/decoder logic from the standardserver reference implementation into @orpc/standard-server's event-iterator module, with no public API changes.
- SSE-compliant line ending and delimiter handling —
decodeEventMessageandEventDecoder.feednow recognize CR, LF, and CRLF line endings and message delimiters, including delimiters straddling chunk boundaries. - Space-stripping spec fix — only a single U+0020 SPACE is stripped after the colon; tabs and other whitespace are preserved per the SSE spec.
encodeEventDatahandles CR-containing data — splits on\rand\r\nin addition to\n.- Stronger assertion coverage —
assertEventId/assertEventName/assertEventCommentreject carriage returns. - Comprehensive test coverage — chunk-size-invariance, every delimiter split at every position, CRLF split between chunks, 64 KB large-message, and encode→decode round-trip tests ported from the reference implementation.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Summary
Applies the event stream encoder/decoder from the standardserver repo to
@orpc/standard-server'sevent-iterator.What this fixes
\ror\r\nnow encodes to valid SSE output.Compatibility
No breaking changes: all exported names, signatures, and shapes are unchanged. The only observable differences are the spec fixes above and the assertion error messages now mentioning carriage returns.