How I Stopped Passcode Brute-Force From Freezing Live Keystrokes in Carrel

Leader ●2
calendar_today ago • schedule1 min read
— Originally published at theayush.pages.dev

When I started building Carrel, a real-time collaborative CodeMirror 6 workspace, I thought auth was the easy part: hash room passcodes with scrypt, verify on join, done.

Then I tested what happens under brute-force traffic. scrypt is slow on purpose. Run it synchronously and it blocks Node's event loop; run dozens of async scrypt calls uncapped and they saturate libuv's threadpool. Either way, anyone hammering wrong passcodes starts stuttering live keystrokes for everyone typing in the room.

Here is how I ended up structuring the server and protocol across the 3-package TypeScript monorepo (@carrel/shared, @carrel/server, @carrel/client):

1. Bounded scrypt + single-use WS tickets

In server/src/throttle.ts, POST /api/rooms/:id/ticket runs async scrypt inside a 4-slot concurrency limiter paired with per-IP progressive lockouts (and ignores X-Forwarded-For unless TRUST_PROXY is explicitly enabled). Once verified, the client gets a short-lived, single-use nonce-bound HMAC ticket to upgrade /ws, which gets burned immediately on upgrade so replay attempts fail.

2. One WebSocket with a 1-byte frame header

Instead of pushing everything through JSON messages or opening parallel sockets, shared/src/frames.ts prefixes every binary WebSocket payload with one routing byte:

[0x00] -> raw y-protocols/sync CRDT updates (Y.Doc persisted via SQLite WAL)
[0x01] -> coalesced awareness updates (remote carets, selections, 80ms lerp)
[0x02] -> typed JSON control events (role changes, kicks, host handover)

CodeMirror 6 renders remote carets with an 80ms interpolation curve (CarrelProvider.ts) and turns interpolation off automatically when prefers-reduced-motion is set.

3. Not losing room admin on a 2-second Wi-Fi drop

If a host refreshes a tab or drops Wi-Fi briefly, promoting someone else on the exact close event creates constant role churn. In server/src/host.ts, a HOST_GRACE_MS (5000ms) timer marks the seat hostPending first. Only if that timer expires does seniority election promote the next peer by min(joinedAt) (tie-broken by min(clientId)), while SENIORITY_WINDOW_MS (30s) restores standing on quick reconnects. Multi-tab promotion and creator-key reclamation are verified in Playwright E2E specs (phase5.test.ts).

Full architecture notes and failure-mode diagrams: https://theayush.pages.dev/projects/carrel

Code on GitHub: https://github.com/theayushagarwal/carrel

Want more #react & #typescript deep dives?

Join 6,097+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

The Zero-Net-Loss Fleet & The Mercenary Squad: A Live AI Economy

DEVPlank - Aug 4

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

Merancang Backend Bisnis ISP: API Pelanggan, Paket Internet, Invoice, dan Tiket Support

Masbadar - Mar 13

What Security Teams Want From AI Agents Before They Let Them Act Alone

Tom Smithverified - Nov 18
chevron_left
641 Points • 2 Badges
2Posts
0Comments
5Connections
17-year-old Systems & Agent Engineer studying B.Tech CSE at VIT Vellore (Class of 2030). I build dis... Show more

Commenters (This Week)

6 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!