Skip to content

relay: migrate from Fastify to Bun.serve() native - #4

Merged
YairEtzion merged 1 commit into
mainfrom
relay/bun-migration
Mar 31, 2026
Merged

relay: migrate from Fastify to Bun.serve() native#4
YairEtzion merged 1 commit into
mainfrom
relay/bun-migration

Conversation

@YairEtzion

Copy link
Copy Markdown
Contributor

Summary

  • Replace Fastify v5 + @fastify/websocket + ws with Bun's native Bun.serve() HTTP and WebSocket server
  • Drop 62 transitive dependencies from the relay package (fastify, @fastify/websocket, @fastify/rate-limit, ws, and all their transitive deps)
  • Update Dockerfile.relay to use oven/bun:1.3-slim as runtime (Node.js kept for build stage only)
  • Switch relay tests from vitest to bun:test since the server requires Bun runtime

Why

  • Anthropic acquired Bun (Dec 2025); Claude Code ships as a Bun executable
  • The relay is a thin WebSocket message router — Fastify added framework overhead with no benefit
  • Bun's native WebSocket is faster than the ws library polyfill
  • Smaller Docker image, faster cold starts on Cloud Run

What changed

File Change
packages/relay/src/server.ts Full rewrite: Fastify → Bun.serve() with native WebSocket handlers
packages/relay/src/session.ts Types updated for ServerWebSocket<WebSocketData>
packages/relay/src/start.ts Simplified (Bun.serve returns synchronously)
packages/relay/src/index.ts Added WebSocketData type export
packages/relay/src/__tests__/ vitest → bun:test
packages/relay/package.json Removed 5 deps, added @types/bun, test script uses bun test
Dockerfile.relay Node.js build stage + oven/bun:1.3-slim runtime stage
pnpm-lock.yaml -409 lines (62 packages removed)

Test plan

  • bun test — all 5 relay tests pass (handshake integration + SAS computation)
  • Local Docker build and run — health endpoint + WebSocket verified
  • Deployed to Cloud Run (amesh-relay) — health + WebSocket verified
  • Custom domain relay.authmesh.dev — confirmed working
  • pnpm build — all packages build successfully

Replace Fastify v5 + @fastify/websocket + ws with Bun's native HTTP and
WebSocket server. Drops 62 transitive dependencies from the relay package.

- Rewrite server.ts to use Bun.serve() with native WebSocket handlers
- Update session.ts types for Bun's ServerWebSocket<T>
- Switch tests from vitest to bun:test (relay must run under Bun runtime)
- Update Dockerfile.relay: Node.js build stage + oven/bun:1.3-slim runtime
- Remove fastify, @fastify/websocket, @fastify/rate-limit, ws, @types/ws
- Add @types/bun for type definitions

Tested: local Docker, Cloud Run deployment, health + WebSocket verified.
@YairEtzion
YairEtzion merged commit 900cc96 into main Mar 31, 2026
0 of 2 checks passed
@YairEtzion
YairEtzion deleted the relay/bun-migration branch April 2, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant