Repository navigation
Migrate from Node.js/Express to Cloudflare Workers - #8
Merged
Merged
Conversation
The Express/Socket.IO server (index.js, node-persist, rotating logs) is replaced by a Worker with one Durable Object per session: - src/index.ts routes /api/session (create or resume via the rs-session-id cookie, SVG QR code), /ws/:session (WebSocket join) and everything else to the static assets in static/, with CORS on all of it and a www -> apex redirect. - src/session-room.ts is the SessionRoom Durable Object. It holds the host, remotes and observer as hibernatable WebSockets, relays control and application events between them, remembers each remote's settings for the connection info, replaces an older host or observer when a newer one connects, drops sockets that stop sending heartbeats (alarm every 15 s while connected) and deletes the session an hour after the last activity. Heartbeats are answered by the runtime through setWebSocketAutoResponse, so an idle session costs nothing. - The wire format is plain JSON over WebSocket instead of Socket.IO; PROTOCOL.md describes it. The socket client lives in the RemoteSlide-Controller submodule (rs-socket.js) so the website, the bookmarklet and the Chrome extension share it. Website changes: index.html moves from views/ into static/ as the SPA fallback, the controllers join the session through RemoteSlideSocket once they know the session id (the "init start" round trip is gone), "_forward"/"_get" become plain events, and the latency display uses the socket's own heartbeat. The bookmarklet loader no longer juggles window.io; it loads attrchange from this site (the rawgit CDN it used has been dead since 2019), then rs-socket.js and the page controller. The inline chrome.webstore.install call is removed (Chrome dropped inline installs in 2018), the _gaq calls are guarded (no analytics script is loaded, so settings.save() threw), and the bookmarklet's jQuery is bumped to 3.7.1. Tests run inside workerd via @cloudflare/vitest-pool-workers and cover the session API, routing, presence, relaying, host replacement, heartbeats and expiry. CI runs them on every push and PR; the deploy workflow publishes master with wrangler (needs the CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID secrets). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GfhNPpQzAKM2dMhYp66gLk
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
This PR migrates the Remote Slide signalling server from a Node.js/Express application to Cloudflare Workers, leveraging Durable Objects for session management and WebSocket support.
Key Changes
Replaced Express server with Cloudflare Workers: Removed the legacy
index.js(404 lines of Node.js/Express code) and replaced it with a modern TypeScript-based Workers implementation (src/index.ts)Implemented Durable Objects for session management: Created
src/session-room.ts(408 lines) to handle per-session state, WebSocket connections, and client lifecycle management using Cloudflare's Durable Objects APIFormalized wire protocol: Added
src/protocol.tswith shared protocol definitions andPROTOCOL.mddocumentation for the JSON-based WebSocket communication between website, injected controller, and serverAdded comprehensive test suite: Created
test/session.test.ts(300 lines) andtest/helpers.tswith test utilities for validating session creation, WebSocket communication, and client lifecycleUpdated build configuration:
wrangler.jsoncfor Cloudflare Workers configurationtsconfig.jsonandvitest.config.tsfor TypeScript and testing setuppackage.jsonto version 2.0.0 with Workers-specific dependenciesAdded CI/CD workflows: Created GitHub Actions workflows for automated testing (
ci.yml) and deployment (deploy.yml)Updated client-side code: Modified Angular controllers to work with the new WebSocket-based connection model instead of Socket.IO
Notable Implementation Details
src/qr.tsfor SVG-based QR code generation suitable for Workers environmenthttps://claude.ai/code/session_01GfhNPpQzAKM2dMhYp66gLk