Maintenance: Extract shared parseEvent() helper from channel transports#34328
Maintenance: Extract shared parseEvent() helper from channel transports#34328mixelburg wants to merge 1 commit into
Conversation
…ransports
The conditional telejson deserialization expression was duplicated
across all three channel transports, with inconsistent parse options:
- websocket: parse(data) — no options
- postmessage: parse(data, global.CHANNEL_OPTIONS || {})
- server-channel: parse(data, {}) — empty options
Extract parseEvent() to code/core/src/channels/parse-event.ts.
All transports now use CHANNEL_OPTIONS (browser) or {} (server/node),
making behaviour consistent across the board and easier to update.
Fixes storybookjs#34291
📝 WalkthroughWalkthroughA new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 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.
🧹 Nitpick comments (1)
code/core/src/channels/websocket/index.ts (1)
96-101: Consider adding fallback for consistency with parseEvent.The
sendNowmethod spreadsglobal.CHANNEL_OPTIONSwithout the|| {}fallback used inparseEvent. While spreadingundefinedis technically safe (it's a no-op), the inconsistency could be confusing for future maintainers.♻️ Optional: Add fallback for consistency
private sendNow(event: any) { const data = stringify(event, { maxDepth: 15, - ...global.CHANNEL_OPTIONS, + ...(global.CHANNEL_OPTIONS || {}), }); this.socket.send(data); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@code/core/src/channels/websocket/index.ts` around lines 96 - 101, The sendNow method spreads global.CHANNEL_OPTIONS directly which is inconsistent with parseEvent's use of a fallback; update sendNow (method sendNow in this file) to spread (global.CHANNEL_OPTIONS || {}) when calling stringify so it behaves the same as parseEvent and avoids confusion for future maintainers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@code/core/src/channels/websocket/index.ts`:
- Around line 96-101: The sendNow method spreads global.CHANNEL_OPTIONS directly
which is inconsistent with parseEvent's use of a fallback; update sendNow
(method sendNow in this file) to spread (global.CHANNEL_OPTIONS || {}) when
calling stringify so it behaves the same as parseEvent and avoids confusion for
future maintainers.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0a8e8ce5-2b48-4f31-97b9-3704a49ff5a0
📒 Files selected for processing (4)
code/core/src/channels/parse-event.tscode/core/src/channels/postmessage/index.tscode/core/src/channels/websocket/index.tscode/core/src/core-server/utils/get-server-channel.ts
| import { stringify } from 'telejson'; | ||
| import WebSocket, { WebSocketServer } from 'ws'; | ||
|
|
||
| import { parseEvent } from '../../channels/parse-event'; |
There was a problem hiding this comment.
relative references to sub package exports like storybook/internal/channels are not allowed
|
Closing. See reason here: #34453 (comment) |
What does this PR do?
Resolves the duplicated telejson deserialization pattern across all three channel transport implementations.
Problem
The conditional
typeof data === 'string' && isJSON(data) ? parse(data, ...) : dataexpression was copied in three places with subtly differentparseoptions:parsechannels/websocket/index.tschannels/postmessage/index.tsglobal.CHANNEL_OPTIONS || {}core-server/utils/get-server-channel.ts{}(empty)The websocket transport was silently ignoring
CHANNEL_OPTIONS, which could cause deserialization mismatches when custom options are configured.Fix
Extract a
parseEvent(data)helper tocode/core/src/channels/parse-event.tsthat always usesglobal.CHANNEL_OPTIONS || {}. All three transport implementations now call the shared helper, ensuring consistent behaviour and making future option changes a single-file edit.Closes #34291
Summary by CodeRabbit