Add SseDecoder to decode Server-Sent Events from any readable stream - #52
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new README examples/signature include PHP syntax and formatting errors that conflict with the project’s declared PHP compatibility and should be corrected before merge.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR introduces a reusable SseDecoder that decodes the text/event-stream (SSE wire protocol) from any ReadableStreamInterface, and refactors EventSource to use this decoder internally while preserving reconnection behavior.
Changes:
- Add new
Clue\React\EventSource\SseDecoderstream decoder and a dedicated test suite for SSE parsing behavior. - Refactor
EventSourceparsing to delegate SSE framing/parsing toSseDecoder, and extend reconnection tests forLast-Event-IDandretry. - Document
SseDecoderusage in the README and add an explicit runtime dependency onreact/stream.
File summaries
| File | Description |
|---|---|
src/SseDecoder.php |
New decoder that buffers and parses SSE frames from any readable stream. |
src/EventSource.php |
Uses SseDecoder internally and persists id/retry across reconnects. |
tests/SseDecoderTest.php |
New unit tests covering SSE decoding, buffering, and close/error semantics. |
tests/EventSourceTest.php |
Adds reconnection regression tests for Last-Event-ID and retry persistence. |
README.md |
Adds SseDecoder documentation and examples. |
composer.json |
Adds explicit react/stream dependency required by SseDecoder. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This changeset adds a new
SseDecoderclass that decodes the Server-Sent Events (SSE) wire protocol from any readable byte stream.Unlike the
EventSourceclass which implements the higher-level HTML5 EventSource API (HTTPGETrequest, automatic reconnection,readyStateandLast-Event-IDhandling), this class only decodes thetext/event-streamwire protocol. This makes it reusable whenever you already have a readable stream of SSE data, such as a streaming HTTP response to a custom request. This is common for LLM streaming APIs that return SSE over an HTTPPOSTrequest:The second commit updates
EventSourceto use this class internally without changing its behavior. All existingEventSourcetests continue to pass unchanged and the test suite ensures 100% code coverage across all supported environments, so this should be safe. Limiting the buffer size for the parser remains out of scope, see #34.Builds on top of #36 and #41
Refs #34