Fix/node sse client#21
Conversation
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughReplaced fetch-based SSE handling with the Changes
Sequence Diagram(s)sequenceDiagram
participant Bot
participant Client
participant EventSource as SSEClient
participant Server
participant Dispatcher as EventDispatch
Bot->>Client: start/connect(cfg.url)
Client->>SSEClient: new EventSource(url, {fetch: with Authorization})
SSEClient-->>Client: onopen
Client->>Client: init connectTime, reset reconnectCount, register bot
SSEClient-->>Client: milky_event (data)
Client->>Dispatcher: parse JSON, dispatch event
note right of Dispatcher: Dispatcher handles incoming events
SSEClient-->>Client: onerror
Client->>Client: log error, maybe unregister bot
Client->>Client: reconnect() -> close old SSEClient, create new EventSource
SSEClient->>Server: HTTP SSE stream
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Code Review
This pull request migrates the SSE connection logic from @microsoft/fetch-event-source to the eventsource library and updates URL construction to handle trailing slashes more consistently. Review feedback identifies a critical issue where the eventsource constructor is incorrectly passed a fetch option, and suggests improvements for error handling during JSON parsing and more effective logging of connection errors.
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/connection/ServerSentEvents.ts (1)
84-91:⚠️ Potential issue | 🟠 MajorCancel pending reconnect backoff in
clear().
stop()callsclear(), but#reconnectTimersurvives. If a disconnect had already scheduled a retry, the timeout still fires after shutdown and creates a newEventSourceon a bot that was meant to stay offline.Suggested fix
clear () { + if (this.#reconnectTimer) { + clearTimeout(this.#reconnectTimer) + this.#reconnectTimer = null + } if (this.#IntervalTime) { clearInterval(this.#IntervalTime) this.#IntervalTime = null } if (this.#client) { this.#client.close() this.#client = null } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/connection/ServerSentEvents.ts` around lines 84 - 91, The clear() method currently cancels the interval and closes the client but does not cancel a pending reconnect, so add logic in clear() to clear and nullify the reconnect timeout (this.#reconnectTimer) to prevent a scheduled reconnect from creating a new EventSource after stop() is called; ensure that whichever method schedules the reconnect uses the same this.#reconnectTimer field so clearing it here prevents the retry from firing.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/connection/ServerSentEvents.ts`:
- Around line 34-36: The listener registered in
this.#client.addEventListener('milky_event', ...) currently calls
JSON.parse(event.data) and EventDispatch(data, this.bot) directly, so any thrown
error will escape the handler and bypass reconnect/error handling; wrap the
parse+dispatch in a try/catch inside that listener, catch any parsing or
dispatch errors, log the error (including the raw event.data or a safe summary)
using your existing logger (e.g., this.bot or process logger) and return/ignore
the bad frame so the transport stays alive; ensure you reference
JSON.parse(event.data) and EventDispatch(...) when adding the try/catch so only
this frame is suppressed and normal processing continues.
In `@src/core/bot.ts`:
- Around line 29-33: Normalize cfg.url once before building derived endpoints so
you don't append /event or /api twice: add a helper (e.g., normalizeBaseUrl)
that parses cfg.url, rejects URLs already pointing to /api or /event, ensures a
trailing slash, and returns the normalized base URL string; then use that
normalized value when constructing the event address (new URL('event',
normalizedBase).toString()) and when instantiating Client (new
Client(normalizedBase, this)) so both the event and API clients reuse the same
validated base URL.
---
Outside diff comments:
In `@src/connection/ServerSentEvents.ts`:
- Around line 84-91: The clear() method currently cancels the interval and
closes the client but does not cancel a pending reconnect, so add logic in
clear() to clear and nullify the reconnect timeout (this.#reconnectTimer) to
prevent a scheduled reconnect from creating a new EventSource after stop() is
called; ensure that whichever method schedules the reconnect uses the same
this.#reconnectTimer field so clearing it here prevents the retry from firing.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 15c7d400-fd09-43ab-963a-4e3eea1a133a
📒 Files selected for processing (3)
src/connection/ServerSentEvents.tssrc/core/Client.tssrc/core/bot.ts
修复支持路径前缀 , SSE连接错误
Summary by CodeRabbit
Bug Fixes
Enhancements
Chores