refactor: extract SessionWebSocketManager from SessionDO#74
Merged
ColeMurray merged 2 commits intomainfrom Feb 7, 2026
Merged
Conversation
Centralizes all Cloudflare WebSocket API usage (ctx.acceptWebSocket, ctx.getWebSockets, ctx.getTags, ctx.setWebSocketAutoResponse) into a single testable module. This replaces 5+ inline tag-parsing sites with a single classify() method and consolidates hibernation recovery, auth timeout enforcement, and broadcast filtering. The manager is a registry for ClientInfo, not a factory — the DO builds ClientInfo with domain knowledge (GitHub avatars, name fallback) and stores it via setClient/getClient. For hibernation recovery, the manager returns raw DB mappings; the DO converts them to ClientInfo. Key changes: - New SessionWebSocketManager interface + SessionWebSocketManagerImpl - Atomic acceptAndSetSandboxSocket (accept + replace + set) - getSandboxSocket reads expected sandbox ID from repository internally - forEachClientSocket supports "all_clients" and "authenticated_only" - 46 unit tests with FakeCtx and mock repository - Net reduction of ~150 lines in durable-object.ts Phase 1: all broadcasts use "all_clients" mode (behavior-preserving). Phase 2 (follow-up) will switch to "authenticated_only" with DB-backed auth check for post-hibernation correctness.
Terraform Validation Results
Pushed by: @ColeMurray, Action: |
Greptile OverviewGreptile SummaryExtracted WebSocket management logic from Key improvements:
Testing:
Minor observation:
|
…ribe setWebSocketAutoResponse is a DO-global setting. Calling it on every subscribe was harmless but redundant. Move to ensureInitialized() so it runs once per DO instance lifecycle.
Terraform Validation Results
Pushed by: @ColeMurray, Action: |
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.
Summary
ctx.acceptWebSocket,ctx.getWebSockets,ctx.getTags,ctx.setWebSocketAutoResponse) fromSessionDOinto a newSessionWebSocketManagermoduleclassify()method and consolidates hibernation recovery, auth timeout enforcement, and broadcast filtering into one testable unitClientInfo, not a factory — the DO buildsClientInfowith domain knowledge (avatar URLs, name fallback) and stores it viasetClient/getClientdurable-object.tsDesign decisions
acceptAndSetSandboxSocket()combines accept + replace + set to prevent misuse of a split sequencegetSandboxSocket()reads the expected sandbox ID from the repository internally — callers cannot accidentally omit validationforEachClientSocket()supports"all_clients"and"authenticated_only"modes; the latter checks both in-memory state and persisted DB mappings for post-hibernation correctness"all_clients"mode (behavior-preserving). A follow-up will switch to"authenticated_only"with prerequisite testslifecycleManagerto ensure the logger has session contextTest plan
all_clientsandauthenticated_onlymodes)