Public: anyone can read this; the crew can change it.
A room on an app's change stream reports who is present #16776
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15077-a-room-on-an-app-s-change-stream-reports → main 0 commits fork 2 threads · 2 unresolved
4 files
changed +176
−0
scripts/unreached-allow.json +1
scripts/unused-exports-allow.json +1
src/apps/room.ts added +100
| @@ -0,0 +1,100 @@ | ||
| 1 | /** | |
| 2 | * Who is in a room (#15077): presence kept as entries of the app's change | |
| 3 | * stream (`src/apps/changes.ts`, #14232). `join`, `beat` and `leave` append | |
| 4 | * `{"presence": "join"|"beat"|"leave", "member": id}` to the room; `present` | |
| 5 | * replays them, and a member silent for `SILENT_MS` is dropped. A room exists | |
| 6 | * once someone has joined it; every other call on a room with no entries is | |
| 7 | * refused as unknown. A beat sooner than `BEAT_EVERY_MS` after the member's | |
| 8 | * last entry is answered ok and not appended, so heartbeat load stays at one | |
| 9 | * entry per member per 5 s. Plain data in, plain data out; no route yet. | |
| 10 | */ | |
| 11 | ||
| 12 | import type { Changes } from "./changes.ts"; | |
| 13 | ||
| 14 | export const SILENT_MS = 30_000; | |
| 15 | export const BEAT_EVERY_MS = 5_000; | |
| 16 | ||
| 17 | const MEMBER = /^[0-9a-z][0-9a-z_.-]{0,39}$/i; | |
| 18 | ||
| 19 | export type Said = { readonly ok: true } | { readonly ok: false; readonly why: string }; | |
| 20 | export type Members = { readonly ok: true; readonly members: string[] } | { readonly ok: false; readonly why: string }; | |
| 21 | ||
| 22 | interface Seen { | |
| 23 | cursor: number; | |
| 24 | /** member -> time of their last entry, absent once they left */ | |
| 25 | last: Map<string, number>; | |
| 26 | } | |
| 27 | ||
| 28 | export interface Rooms { | |
| 29 | join(app: string, room: string, member: string): Said; | |
| 30 | beat(app: string, room: string, member: string): Said; | |
| 31 | leave(app: string, room: string, member: string): Said; | |
| 32 | /** Members present now, sorted. */ | |
| 33 | present(app: string, room: string): Members; | |
| 34 | } | |
| 35 | ||
| 36 | export function openRooms(changes: Changes, now: () => number = Date.now): Rooms { | |
| 37 | const seen = new Map<string, Seen>(); | |
| 38 | ||
| 39 | /** Fold new stream entries into what is known; false when the room has none. */ | |
| 40 | function catchUp(app: string, room: string): Seen | null { | |
| 41 | const key = `${app.toLowerCase()}\n${room}`; | |
| 42 | let s = seen.get(key); | |
| 43 | if (!s) { | |
| 44 | s = { cursor: 0, last: new Map() }; | |
| 45 | seen.set(key, s); | |
| 46 | } | |
| 47 | for (;;) { | |
| 48 | const rows = changes.since(app, room, s.cursor); | |
| 49 | for (const r of rows) { | |
| 50 | s.cursor = r.seq; | |
| 51 | let p: unknown; | |
| 52 | try { | |
| 53 | p = JSON.parse(r.json); | |
| 54 | } catch { | |
| 55 | continue; | |
| 56 | } | |
| 57 | const e = p as { presence?: unknown; member?: unknown }; | |
| 58 | if (typeof e?.member !== "string" || typeof e.presence !== "string") continue; | |
| 59 | if (e.presence === "leave") s.last.delete(e.member); | |
| 60 | else if (e.presence === "join" || e.presence === "beat") s.last.set(e.member, r.at); | |
| 61 | } | |
| 62 | if (rows.length === 0) break; | |
| 63 | } | |
| 64 | if (s.cursor === 0) { | |
| 65 | seen.delete(key); | |
| 66 | return null; | |
| 67 | } | |
| 68 | return s; | |
| 69 | } | |
| 70 | ||
| 71 | function write(app: string, room: string, member: string, kind: "join" | "beat" | "leave"): Said { | |
| 72 | if (typeof member !== "string" || !MEMBER.test(member)) return { ok: false, why: "bad member" }; | |
| 73 | const s = catchUp(app, room); | |
| 74 | if (!s && kind !== "join") { | |
| 75 | // An invalid app or room name reads as an empty stream too. | |
| 76 | return { ok: false, why: "unknown room" }; | |
| 77 | } | |
| 78 | const t = now(); | |
| 79 | if (kind === "beat") { | |
| 80 | const at = s!.last.get(member); | |
| 81 | if (at === undefined || t - at >= SILENT_MS) return { ok: false, why: "not in the room" }; | |
| 82 | if (t - at < BEAT_EVERY_MS) return { ok: true }; | |
| 83 | } | |
| 84 | const done = changes.append(app, room, JSON.stringify({ presence: kind, member })); | |
| 85 | return done.ok ? { ok: true } : { ok: false, why: done.why }; | |
| 86 | } | |
| 87 | ||
| 88 | return { | |
| 89 | join: (a, r, m) => write(a, r, m, "join"), | |
| 90 | beat: (a, r, m) => write(a, r, m, "beat"), | |
| 91 | leave: (a, r, m) => write(a, r, m, "leave"), | |
| 92 | present(app, room) { | |
| 93 | const s = catchUp(app, room); | |
| 94 | if (!s) return { ok: false, why: "unknown room" }; | |
| 95 | const t = now(); | |
| 96 | const members = [...s.last].filter(([, at]) => t - at < SILENT_MS).map(([m]) => m); | |
| 97 | return { ok: true, members: members.sort() }; | |
| 98 | }, | |
| 99 | }; | |
| 100 | } |
test/room.test.ts added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { openChanges } from "../src/apps/changes.ts"; | |
| 6 | import { BEAT_EVERY_MS, SILENT_MS, openRooms } from "../src/apps/room.ts"; | |
| 7 | ||
| 8 | const dir = () => mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "room-")); | |
| 9 | ||
| 10 | test("join, then a member silent for 30 s is dropped and a beat keeps one", () => { | |
| 11 | let t = 1000; | |
| 12 | const c = openChanges(dir(), () => t); | |
| 13 | const r = openRooms(c, () => t); | |
| 14 | expect(r.join("a", "lobby", "ann")).toEqual({ ok: true }); | |
| 15 | expect(r.join("a", "lobby", "bob")).toEqual({ ok: true }); | |
| 16 | expect(r.present("a", "lobby")).toEqual({ ok: true, members: ["ann", "bob"] }); | |
| 17 | t += 20_000; | |
| 18 | expect(r.beat("a", "lobby", "ann")).toEqual({ ok: true }); | |
| 19 | t += 20_000; // bob silent 40 s, ann 20 s | |
| 20 | expect(r.present("a", "lobby")).toEqual({ ok: true, members: ["ann"] }); | |
| 21 | t += SILENT_MS; | |
| 22 | expect(r.present("a", "lobby")).toEqual({ ok: true, members: [] }); | |
| 23 | expect(r.beat("a", "lobby", "ann")).toEqual({ ok: false, why: "not in the room" }); | |
| 24 | c.close(); | |
| 25 | }); | |
| 26 | ||
| 27 | test("leave removes a member at once; entries are in the stream", () => { | |
| 28 | const c = openChanges(dir(), () => 5); | |
| 29 | const r = openRooms(c, () => 5); | |
| 30 | r.join("a", "lobby", "ann"); | |
| 31 | r.leave("a", "lobby", "ann"); | |
| 32 | expect(r.present("a", "lobby")).toEqual({ ok: true, members: [] }); | |
| 33 | expect(c.since("a", "lobby", 0).map((x) => JSON.parse(x.json).presence)).toEqual(["join", "leave"]); | |
| 34 | c.close(); | |
| 35 | }); | |
| 36 | ||
| 37 | test("an unknown room is refused, and a bad member too", () => { | |
| 38 | const c = openChanges(dir()); | |
| 39 | const r = openRooms(c); | |
| 40 | expect(r.present("a", "nowhere")).toEqual({ ok: false, why: "unknown room" }); | |
| 41 | expect(r.beat("a", "nowhere", "ann")).toEqual({ ok: false, why: "unknown room" }); | |
| 42 | expect(r.leave("a", "nowhere", "ann")).toEqual({ ok: false, why: "unknown room" }); | |
| 43 | expect(r.join("a", "../x", "ann").ok).toBe(false); | |
| 44 | expect(r.join("a", "lobby", "bad member")).toEqual({ ok: false, why: "bad member" }); | |
| 45 | expect(c.since("a", "nowhere", 0)).toEqual([]); | |
| 46 | c.close(); | |
| 47 | }); | |
| 48 | ||
| 49 | test("heartbeats are capped at one per member per 5 s", () => { | |
| 50 | let t = 1000; | |
| 51 | const c = openChanges(dir(), () => t); | |
| 52 | const r = openRooms(c, () => t); | |
| 53 | r.join("a", "lobby", "ann"); | |
| 54 | for (let i = 0; i < 4; i++) { | |
| 55 | t += 1000; | |
| 56 | expect(r.beat("a", "lobby", "ann")).toEqual({ ok: true }); | |
| 57 | } | |
| 58 | expect(c.since("a", "lobby", 0)).toHaveLength(1); | |
| 59 | t += BEAT_EVERY_MS; | |
| 60 | r.beat("a", "lobby", "ann"); | |
| 61 | expect(c.since("a", "lobby", 0)).toHaveLength(2); | |
| 62 | c.close(); | |
| 63 | }); | |
| 64 | ||
| 65 | test("presence survives a restart by replaying the stream", () => { | |
| 66 | const d = dir(); | |
| 67 | const t = 1000; | |
| 68 | const one = openChanges(d, () => t); | |
| 69 | openRooms(one, () => t).join("a", "lobby", "ann"); | |
| 70 | one.close(); | |
| 71 | const two = openChanges(d, () => t); | |
| 72 | expect(openRooms(two, () => t).present("a", "lobby")).toEqual({ ok: true, members: ["ann"] }); | |
| 73 | two.close(); | |
| 74 | }); |
new above
offline
Low:
joinalways appends, so the one-entry-per-5-s cap only covers beats. Consider treating a join by a member who is already present like a beat.