@fuzdev/fuz_app 0.178.0 → 0.180.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/CLAUDE.md +106 -0
- package/dist/actions/connection_registry.d.ts +18 -0
- package/dist/actions/connection_registry.d.ts.map +1 -1
- package/dist/actions/connection_registry.js +23 -0
- package/dist/actions/room_action_specs.d.ts +733 -0
- package/dist/actions/room_action_specs.d.ts.map +1 -0
- package/dist/actions/room_action_specs.js +506 -0
- package/dist/actions/room_actions.d.ts +276 -0
- package/dist/actions/room_actions.d.ts.map +1 -0
- package/dist/actions/room_actions.js +394 -0
- package/dist/actions/room_client.svelte.d.ts +399 -0
- package/dist/actions/room_client.svelte.d.ts.map +1 -0
- package/dist/actions/room_client.svelte.js +942 -0
- package/dist/actions/room_registry.d.ts +454 -0
- package/dist/actions/room_registry.d.ts.map +1 -0
- package/dist/actions/room_registry.js +927 -0
- package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
- package/dist/auth/role_grant_queries.d.ts +17 -0
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +30 -0
- package/dist/rtc/rtc_mesh.svelte.d.ts +102 -0
- package/dist/rtc/rtc_mesh.svelte.d.ts.map +1 -0
- package/dist/rtc/rtc_mesh.svelte.js +232 -0
- package/dist/rtc/rtc_peer.svelte.d.ts +180 -0
- package/dist/rtc/rtc_peer.svelte.d.ts.map +1 -0
- package/dist/rtc/rtc_peer.svelte.js +397 -0
- package/dist/rtc/rtc_signal.d.ts +74 -0
- package/dist/rtc/rtc_signal.d.ts.map +1 -0
- package/dist/rtc/rtc_signal.js +87 -0
- package/dist/testing/CLAUDE.md +67 -13
- package/dist/testing/cross_backend/action_manifest.d.ts +34 -2
- package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
- package/dist/testing/cross_backend/action_manifest.js +33 -6
- package/dist/testing/cross_backend/capabilities.d.ts +22 -4
- package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
- package/dist/testing/cross_backend/capabilities.js +4 -1
- package/dist/testing/cross_backend/full_spine_mount.d.ts +31 -6
- package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
- package/dist/testing/cross_backend/full_spine_mount.js +45 -13
- package/dist/testing/cross_backend/rooms_ws.d.ts +20 -0
- package/dist/testing/cross_backend/rooms_ws.d.ts.map +1 -0
- package/dist/testing/cross_backend/rooms_ws.js +534 -0
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +4 -1
- package/dist/testing/cross_backend/spine_suites.d.ts +5 -2
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +24 -5
- package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.js +2 -1
- package/dist/testing/cross_backend/test_room_authorize.d.ts +47 -0
- package/dist/testing/cross_backend/test_room_authorize.d.ts.map +1 -0
- package/dist/testing/cross_backend/test_room_authorize.js +58 -0
- package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_backdoor.js +10 -4
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +85 -1
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +57 -3
- package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/ts_spine_backend_config.js +5 -2
- package/dist/ui/AccountSessions.svelte.d.ts.map +1 -1
- package/dist/ui/AdminAccounts.svelte.d.ts.map +1 -1
- package/dist/ui/AdminAuditLog.svelte.d.ts.map +1 -1
- package/dist/ui/AdminInvites.svelte.d.ts.map +1 -1
- package/dist/ui/AdminOverview.svelte.d.ts.map +1 -1
- package/dist/ui/AdminRoleGrantHistory.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSessions.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSettings.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSurface.svelte.d.ts.map +1 -1
- package/dist/ui/BootstrapForm.svelte.d.ts.map +1 -1
- package/dist/ui/ConfirmButton.svelte.d.ts.map +1 -1
- package/dist/ui/Datatable.svelte.d.ts.map +1 -1
- package/dist/ui/LoginForm.svelte.d.ts.map +1 -1
- package/dist/ui/LogoutButton.svelte.d.ts.map +1 -1
- package/dist/ui/OpenSignupToggle.svelte.d.ts.map +1 -1
- package/dist/ui/PopoverButton.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferForm.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferHistory.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferInbox.svelte.d.ts.map +1 -1
- package/dist/ui/SignupForm.svelte.d.ts.map +1 -1
- package/dist/ui/SurfaceExplorer.svelte.d.ts.map +1 -1
- package/package.json +19 -19
|
@@ -0,0 +1,927 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rooms over the WebSocket connection registry: members seated in a room by
|
|
3
|
+
* key, relayed messages, per-member state, and occupancy watches. The TS twin
|
|
4
|
+
* of the Rust spine's `fuz_realtime::RoomRegistry`.
|
|
5
|
+
*
|
|
6
|
+
* A room is a key (`RoomId`) the consumer chooses — e.g. a voice room keyed
|
|
7
|
+
* by a space id — holding **seats** and **watches**. A seat is one actor's
|
|
8
|
+
* membership on one connection, with a random `MemberId` that clients address
|
|
9
|
+
* each other by (a connection id never reaches the wire) and a small
|
|
10
|
+
* client-encoded state string. Seats are keyed by connection and actor: a
|
|
11
|
+
* connection whose requests act as different actors of one account (the
|
|
12
|
+
* dispatcher's `acting` selector) holds a seat per actor, and a leave, send,
|
|
13
|
+
* or state set touches only the acting actor's own seat; an event skips the
|
|
14
|
+
* seat that caused it, not its connection. A watch sees the members without
|
|
15
|
+
* joining; it is one per connection and room, whichever actor last asked for
|
|
16
|
+
* it. The registry relays opaque payloads between members and knows nothing of
|
|
17
|
+
* what they carry. Rooms live in memory and die with the process: a restart
|
|
18
|
+
* drops every room and clients rejoin.
|
|
19
|
+
*
|
|
20
|
+
* The verbs over it — the specs, the authorizer hook, the per-connection
|
|
21
|
+
* limiter — are `actions/room_action_specs.ts` and `actions/room_actions.ts`;
|
|
22
|
+
* this module holds the state and the fan-out.
|
|
23
|
+
*
|
|
24
|
+
* ## One step, one order
|
|
25
|
+
*
|
|
26
|
+
* The Rust registry holds rooms, seats, watches, joins in flight, and the
|
|
27
|
+
* per-connection and per-actor indexes under one lock, since exclusivity and
|
|
28
|
+
* eviction span rooms. Here every method runs to completion without an
|
|
29
|
+
* `await`, so JavaScript's single thread gives the same guarantee: no check is
|
|
30
|
+
* ever separated from the mutation it guards. Every notification is queued
|
|
31
|
+
* inside that step through `ConnectionRegistry.send_to`, which never waits, so
|
|
32
|
+
* each room's events carry a `seq` that orders them: one counter per room,
|
|
33
|
+
* bumped by every event in the room. A room with no seats and no watches is
|
|
34
|
+
* deleted; a new one starts its `seq` again at zero.
|
|
35
|
+
*
|
|
36
|
+
* `seq` orders a room's events, and lets a client drop the events at or below
|
|
37
|
+
* the snapshot a join or watch answered; it is not contiguous per client (an
|
|
38
|
+
* addressed message bumps it, and only the addressee sees that one), and it
|
|
39
|
+
* restarts when a room is deleted and created again. Event order across rooms
|
|
40
|
+
* is unspecified; within a room it is the `seq`.
|
|
41
|
+
*
|
|
42
|
+
* Nothing here waits: a recipient that cannot take a frame is closed for the
|
|
43
|
+
* overflow (`WS_CLOSE_OUTBOUND_OVERFLOW`) by the connection registry, and its
|
|
44
|
+
* seats are then released like any other disconnect.
|
|
45
|
+
*
|
|
46
|
+
* ## Two-phase join
|
|
47
|
+
*
|
|
48
|
+
* A join is `reserve_seat`, the consumer's authorizer, then `admit_seat`. The
|
|
49
|
+
* reserve puts the join **in flight** before the authorizer reads anything,
|
|
50
|
+
* refused when the socket has already closed, and returns a `PendingSeat`;
|
|
51
|
+
* `admit_seat` always consumes it, and a caller that never reaches the admit
|
|
52
|
+
* calls `withdraw` (in a `finally` — the Rust guard's drop). While in flight
|
|
53
|
+
* the join is reachable: an eviction that names its actor or room refuses it,
|
|
54
|
+
* one that names a scope marks it, so the admit refuses when the verdict's
|
|
55
|
+
* scope is that one, and the connection's own `leave` of the room refuses it
|
|
56
|
+
* as superseded. That closes the race of an eviction against an authorizer: a
|
|
57
|
+
* consumer evicts after its write commits, and its authorizer reads under READ
|
|
58
|
+
* COMMITTED, so the eviction either precedes the reserve — then its commit
|
|
59
|
+
* precedes the authorizer's read, which denies — or follows it and reaches the
|
|
60
|
+
* join in flight.
|
|
61
|
+
*
|
|
62
|
+
* The argument holds only for authority the authorizer **reads inside that
|
|
63
|
+
* window**, after the reserve, from the database. Anything loaded before the
|
|
64
|
+
* reserve — the request's own role grants, which the dispatcher reads before
|
|
65
|
+
* the handler runs — may predate an eviction that also precedes the reserve
|
|
66
|
+
* and so found nothing in flight to reach: the stale read would admit an
|
|
67
|
+
* evicted actor. The authorizer hook (`RoomAuthorize`) therefore gets the
|
|
68
|
+
* caller's ids and the database, never the request's grants. Watches run the
|
|
69
|
+
* same two phases (`reserve_watch`, `admit_watch`).
|
|
70
|
+
*
|
|
71
|
+
* Unlike the Rust join, whose request future is dropped when its socket
|
|
72
|
+
* closes, a TS join keeps running after the close: the admit refuses it, since
|
|
73
|
+
* the close marks every join in flight on the connection and the admit reads
|
|
74
|
+
* the socket's signal.
|
|
75
|
+
*
|
|
76
|
+
* The admit seats the member unless it was refused meanwhile, its socket
|
|
77
|
+
* closed, or the room is full. A refused join displaces nothing.
|
|
78
|
+
*
|
|
79
|
+
* ## Exclusivity and the rejoin guard
|
|
80
|
+
*
|
|
81
|
+
* With `exclusive` an actor holds one seat across the registry: the newest
|
|
82
|
+
* **admitted** join wins (two sockets racing settle by which admits last, not
|
|
83
|
+
* which sent first), and the seat it displaces hears `room_seat_lost` with
|
|
84
|
+
* reason `moved` and `moved_to`, the new seat's room. Either way a connection
|
|
85
|
+
* holds one seat per room for each actor; joining again as that actor moves its
|
|
86
|
+
* old seat the same way.
|
|
87
|
+
*
|
|
88
|
+
* A seat whose socket died never hears that it moved. Its client, on
|
|
89
|
+
* reconnecting, rejoins naming the seat it held (`rejoin_of`), and must not
|
|
90
|
+
* take back the seat its actor holds now. So an exclusive registry keeps a
|
|
91
|
+
* short per-actor **tombstone** of seats that ended without a leave: each seat
|
|
92
|
+
* moved away, and each released by its disconnect, which counts as moved once
|
|
93
|
+
* the actor is seated again by any other join. A rejoin naming a moved seat is
|
|
94
|
+
* refused (`seat_moved`) instead of seated; one naming a seat released by its
|
|
95
|
+
* disconnect and not moved since is seated, as is any other. Tombstones last
|
|
96
|
+
* `ROOM_REJOIN_TOMBSTONE_TTL_MS`, at most `ROOM_REJOIN_TOMBSTONES_PER_ACTOR`
|
|
97
|
+
* per actor; past either bound a rejoin is seated like a fresh join.
|
|
98
|
+
*
|
|
99
|
+
* ## Disconnect
|
|
100
|
+
*
|
|
101
|
+
* The first seat or watch a connection reserves adds **one** listener to the
|
|
102
|
+
* connection's socket signal (`ConnectionRegistry.socket_signal` — the signal
|
|
103
|
+
* every close path aborts), and the connection keeps it for its life, however
|
|
104
|
+
* many joins and leaves it makes. When the signal aborts, the registry
|
|
105
|
+
* releases every seat (`room_member_left` with reason `disconnected`), watch,
|
|
106
|
+
* and join in flight the connection holds — on the next microtask, as the Rust
|
|
107
|
+
* waiter runs as its own task, so a close made inside a fan-out (an overflow)
|
|
108
|
+
* never re-enters the registry mid-step.
|
|
109
|
+
*
|
|
110
|
+
* ## Eviction
|
|
111
|
+
*
|
|
112
|
+
* `evict` is the consumer's call when its own authority changes, made after
|
|
113
|
+
* the change commits: by actor, by actor within a scope, by room, or by scope
|
|
114
|
+
* (`RoomEvictTarget`), covering seats, watches, and joins in flight alike, so
|
|
115
|
+
* an evicted actor keeps neither a seat nor a watch. Its socket still hears the
|
|
116
|
+
* room while another actor of the same account is seated there or holds the
|
|
117
|
+
* watch, on that actor's own authority.
|
|
118
|
+
*
|
|
119
|
+
* ## Bounds
|
|
120
|
+
*
|
|
121
|
+
* Seats are bounded by the rooms' cap (and by one per actor when exclusive),
|
|
122
|
+
* watches per connection, joins in flight by the connection's in-flight
|
|
123
|
+
* request cap, listeners at one per connection, and tombstones as above.
|
|
124
|
+
*
|
|
125
|
+
* @module
|
|
126
|
+
*/
|
|
127
|
+
import { create_uuid } from '../primitive_schemas.js';
|
|
128
|
+
import { DEFAULT_ROOM_MAX_MEMBERS, DEFAULT_ROOM_MAX_PAYLOAD_BYTES, DEFAULT_ROOM_MAX_STATE_BYTES, DEFAULT_ROOM_MAX_WATCHES_PER_CONNECTION, room_member_joined_notification_spec, room_member_left_notification_spec, room_member_state_notification_spec, room_message_notification_spec, room_seat_lost_notification_spec, room_watch_ended_notification_spec } from './room_action_specs.js';
|
|
129
|
+
import { utf8_length } from './transports.js';
|
|
130
|
+
/**
|
|
131
|
+
* How long an exclusive registry remembers a seat that ended without a leave,
|
|
132
|
+
* for the rejoin guard (module doc). Twin of the Rust
|
|
133
|
+
* `ROOM_REJOIN_TOMBSTONE_TTL` (15 minutes).
|
|
134
|
+
*/
|
|
135
|
+
export const ROOM_REJOIN_TOMBSTONE_TTL_MS = 15 * 60 * 1000;
|
|
136
|
+
/** The most tombstones an exclusive registry keeps per actor; past it the oldest goes. */
|
|
137
|
+
export const ROOM_REJOIN_TOMBSTONES_PER_ACTOR = 8;
|
|
138
|
+
/**
|
|
139
|
+
* The most scope evictions one join or watch in flight records; past it the
|
|
140
|
+
* join or watch is refused outright, whatever its verdict's scope.
|
|
141
|
+
*
|
|
142
|
+
* A client sees what any eviction of its request in flight answers: the join
|
|
143
|
+
* or watch is refused as if the room were missing (`room_not_found` from the
|
|
144
|
+
* verbs). It trips only when more distinct scopes than this are evicted during
|
|
145
|
+
* one authorizer read. Both twins hold the same bound.
|
|
146
|
+
*/
|
|
147
|
+
export const MAX_POISONED_SCOPES = 16;
|
|
148
|
+
/**
|
|
149
|
+
* The fewest actors the tombstone map holds before it sweeps expired entries;
|
|
150
|
+
* it sweeps again at twice what a sweep leaves.
|
|
151
|
+
*/
|
|
152
|
+
const TOMBSTONE_SWEEP_FLOOR = 64;
|
|
153
|
+
const pending_handles = new WeakMap();
|
|
154
|
+
/** Release a pending seat or watch, once. */
|
|
155
|
+
const withdraw_pending = (pending) => {
|
|
156
|
+
const handle = pending_handles.get(pending);
|
|
157
|
+
const forget = handle?.forget;
|
|
158
|
+
if (!handle || !forget)
|
|
159
|
+
return;
|
|
160
|
+
handle.forget = null;
|
|
161
|
+
forget();
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* A join in flight, between `RoomRegistry.reserve_seat` and
|
|
165
|
+
* `RoomRegistry.admit_seat`. The admit consumes it; a caller that never
|
|
166
|
+
* reaches the admit — a refusal, a cancelled request, a throwing authorizer —
|
|
167
|
+
* calls `withdraw`, which removes it (in a `finally`, as the Rust guard's drop
|
|
168
|
+
* does). Withdrawing an admitted or withdrawn seat does nothing.
|
|
169
|
+
*/
|
|
170
|
+
export class PendingSeat {
|
|
171
|
+
/** The member id the seat will have once admitted. */
|
|
172
|
+
member_id;
|
|
173
|
+
/** The room the join is for. */
|
|
174
|
+
room_id;
|
|
175
|
+
constructor(member_id, room_id) {
|
|
176
|
+
this.member_id = member_id;
|
|
177
|
+
this.room_id = room_id;
|
|
178
|
+
}
|
|
179
|
+
/** Withdraw the join, if it is still in flight. */
|
|
180
|
+
withdraw() {
|
|
181
|
+
withdraw_pending(this);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* A watch in flight, between `RoomRegistry.reserve_watch` and
|
|
186
|
+
* `RoomRegistry.admit_watch` — the watch half of `PendingSeat`.
|
|
187
|
+
*/
|
|
188
|
+
export class PendingWatch {
|
|
189
|
+
/** The room the watch is for. */
|
|
190
|
+
room_id;
|
|
191
|
+
constructor(room_id) {
|
|
192
|
+
this.room_id = room_id;
|
|
193
|
+
}
|
|
194
|
+
/** Withdraw the watch, if it is still in flight. */
|
|
195
|
+
withdraw() {
|
|
196
|
+
withdraw_pending(this);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** `actor_id`'s seat in the room on `conn`, or `null`. */
|
|
200
|
+
const conn_seat = (conn, room_id, actor_id) => conn.seats.get(room_id)?.get(actor_id) ?? null;
|
|
201
|
+
const conn_set_seat = (conn, room_id, actor_id, member_id) => {
|
|
202
|
+
let by_actor = conn.seats.get(room_id);
|
|
203
|
+
if (!by_actor) {
|
|
204
|
+
by_actor = new Map();
|
|
205
|
+
conn.seats.set(room_id, by_actor);
|
|
206
|
+
}
|
|
207
|
+
by_actor.set(actor_id, member_id);
|
|
208
|
+
};
|
|
209
|
+
/** Forget `actor_id`'s seat in the room on `conn`, if it is still `member_id`. */
|
|
210
|
+
const conn_clear_seat = (conn, room_id, actor_id, member_id) => {
|
|
211
|
+
const by_actor = conn.seats.get(room_id);
|
|
212
|
+
if (!by_actor)
|
|
213
|
+
return;
|
|
214
|
+
if (by_actor.get(actor_id) === member_id)
|
|
215
|
+
by_actor.delete(actor_id);
|
|
216
|
+
if (by_actor.size === 0)
|
|
217
|
+
conn.seats.delete(room_id);
|
|
218
|
+
};
|
|
219
|
+
const ok = (value) => ({ ok: true, value });
|
|
220
|
+
const refuse = (error) => ({ ok: false, error });
|
|
221
|
+
const members_of = (room) => room.seats.map((seat) => ({
|
|
222
|
+
member_id: seat.member_id,
|
|
223
|
+
actor_id: seat.actor_id,
|
|
224
|
+
state: seat.state
|
|
225
|
+
}));
|
|
226
|
+
/**
|
|
227
|
+
* The connection of every seat but `except`'s. A connection holding another
|
|
228
|
+
* seat beside `except`'s (another actor of the account) is included.
|
|
229
|
+
*/
|
|
230
|
+
const member_connections = (room, except) => {
|
|
231
|
+
const ids = new Set();
|
|
232
|
+
for (const seat of room.seats) {
|
|
233
|
+
if (seat.member_id !== except)
|
|
234
|
+
ids.add(seat.connection_id);
|
|
235
|
+
}
|
|
236
|
+
return ids;
|
|
237
|
+
};
|
|
238
|
+
/** The connections a member event goes to: every member's but the seat `except`'s, and every watcher's. */
|
|
239
|
+
const recipients_of = (room, except) => {
|
|
240
|
+
const ids = member_connections(room, except);
|
|
241
|
+
for (const watcher of room.watchers)
|
|
242
|
+
ids.add(watcher.connection_id);
|
|
243
|
+
return ids;
|
|
244
|
+
};
|
|
245
|
+
/** Whether an eviction `target` names a seat or watch of `actor_id`, admitted under `scope`, in `room_id`. */
|
|
246
|
+
const target_matches = (target, actor_id, scope, room_id) => {
|
|
247
|
+
switch (target.kind) {
|
|
248
|
+
case 'actor':
|
|
249
|
+
return actor_id === target.actor_id;
|
|
250
|
+
case 'actor_in_scope':
|
|
251
|
+
return actor_id === target.actor_id && scope === target.scope;
|
|
252
|
+
case 'room':
|
|
253
|
+
return room_id === target.room_id;
|
|
254
|
+
case 'scope':
|
|
255
|
+
return scope === target.scope;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
const poison = (pending, scope) => {
|
|
259
|
+
if (pending.poisoned.includes(scope))
|
|
260
|
+
return;
|
|
261
|
+
if (pending.poisoned.length >= MAX_POISONED_SCOPES) {
|
|
262
|
+
pending.refused = 'evicted';
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
pending.poisoned.push(scope);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
/** A positive integer option, or its default; anything else throws. */
|
|
269
|
+
const resolve_count = (name, value, fallback, min) => {
|
|
270
|
+
const resolved = value ?? fallback;
|
|
271
|
+
if (!Number.isSafeInteger(resolved) || resolved < min) {
|
|
272
|
+
throw new Error(`RoomRegistry: ${name} must be an integer of at least ${min}, got ${resolved}`);
|
|
273
|
+
}
|
|
274
|
+
return resolved;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* The room registry: see the module doc.
|
|
278
|
+
*
|
|
279
|
+
* Build one over the connection registry its WebSocket endpoint uses
|
|
280
|
+
* (`new RoomRegistry(connections, options)`); a connection id from any other
|
|
281
|
+
* registry names no socket here, so its reserves refuse as
|
|
282
|
+
* `connection_closed`. Two WS endpoints sharing one connection registry share
|
|
283
|
+
* one room registry.
|
|
284
|
+
*/
|
|
285
|
+
export class RoomRegistry {
|
|
286
|
+
/** The registry's resolved bounds and rules. */
|
|
287
|
+
options;
|
|
288
|
+
#connections;
|
|
289
|
+
#now;
|
|
290
|
+
#rooms = new Map();
|
|
291
|
+
#by_connection = new Map();
|
|
292
|
+
/** Joins and watches in flight, by ticket. */
|
|
293
|
+
#pending = new Map();
|
|
294
|
+
#last_ticket = 0;
|
|
295
|
+
/** Each actor's one seat, on an exclusive registry. */
|
|
296
|
+
#seat_of_actor = new Map();
|
|
297
|
+
/** Seats ended without a leave, per actor, on an exclusive registry. */
|
|
298
|
+
#tombstones = new Map();
|
|
299
|
+
/** The actor count past which the tombstones sweep. */
|
|
300
|
+
#tombstone_sweep_at = 0;
|
|
301
|
+
/**
|
|
302
|
+
* @throws Error when a bound is not an integer in range (`max_members_per_room`
|
|
303
|
+
* at least 1, the byte and watch bounds at least 0)
|
|
304
|
+
*/
|
|
305
|
+
constructor(connections, options = {}) {
|
|
306
|
+
this.#connections = connections;
|
|
307
|
+
this.#now = options.now ?? (() => performance.now());
|
|
308
|
+
this.options = Object.freeze({
|
|
309
|
+
exclusive: options.exclusive ?? false,
|
|
310
|
+
max_members_per_room: resolve_count('max_members_per_room', options.max_members_per_room, DEFAULT_ROOM_MAX_MEMBERS, 1),
|
|
311
|
+
max_payload_bytes: resolve_count('max_payload_bytes', options.max_payload_bytes, DEFAULT_ROOM_MAX_PAYLOAD_BYTES, 0),
|
|
312
|
+
max_state_bytes: resolve_count('max_state_bytes', options.max_state_bytes, DEFAULT_ROOM_MAX_STATE_BYTES, 0),
|
|
313
|
+
max_watches_per_connection: resolve_count('max_watches_per_connection', options.max_watches_per_connection, DEFAULT_ROOM_MAX_WATCHES_PER_CONNECTION, 0),
|
|
314
|
+
topology: options.topology ?? 'mesh'
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Phase one of a join: put it in flight before the authorizer runs (module
|
|
319
|
+
* doc, "Two-phase join").
|
|
320
|
+
*
|
|
321
|
+
* @returns the join in flight, or `connection_closed` when the connection
|
|
322
|
+
* is gone or closing, or `seat_moved` when the request is a rejoin of a
|
|
323
|
+
* seat that has moved (exclusive registries)
|
|
324
|
+
* @mutates this - records the join in flight, and on the connection's first
|
|
325
|
+
* reserve adds its one disconnect listener
|
|
326
|
+
*/
|
|
327
|
+
reserve_seat(request) {
|
|
328
|
+
const { connection_id, actor_id, room_id } = request;
|
|
329
|
+
const rejoin_of = request.rejoin_of ?? null;
|
|
330
|
+
if (this.options.exclusive &&
|
|
331
|
+
rejoin_of !== null &&
|
|
332
|
+
this.#rejoin_moved(actor_id, rejoin_of, this.#now())) {
|
|
333
|
+
return refuse('seat_moved');
|
|
334
|
+
}
|
|
335
|
+
const conn = this.#connection_entry(connection_id);
|
|
336
|
+
if (!conn)
|
|
337
|
+
return refuse('connection_closed');
|
|
338
|
+
const member_id = create_uuid();
|
|
339
|
+
return ok(this.#reserve(conn, { type: 'seat', member_id, rejoin_of }, connection_id, actor_id, new PendingSeat(member_id, room_id)));
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Phase two of a join: seat the member (module doc). Consumes `pending`,
|
|
343
|
+
* whatever it answers.
|
|
344
|
+
*
|
|
345
|
+
* Moves the actor's own seat in the room on this connection, and on an
|
|
346
|
+
* exclusive registry the actor's seat anywhere, to this one: each hears
|
|
347
|
+
* `room_seat_lost` with reason `moved`, its room `room_member_left` with
|
|
348
|
+
* reason `moved`. Then the room hears `room_member_joined`.
|
|
349
|
+
*
|
|
350
|
+
* @returns the seat and the room, or `state_too_large`; `evicted`,
|
|
351
|
+
* `join_superseded`, or `connection_closed` when the join was refused in
|
|
352
|
+
* flight or its socket closed; `seat_moved` for a rejoin of a moved seat;
|
|
353
|
+
* `full` when the room seats its cap (the lower of the registry's and
|
|
354
|
+
* `admit.max_members`) after the moves this join makes
|
|
355
|
+
* @throws Error when `admit.max_members` is not a non-negative integer (a
|
|
356
|
+
* consumer's authorizer bug), checked once the join passes its other
|
|
357
|
+
* refusals; `pending` is consumed all the same
|
|
358
|
+
* @mutates this - seats the member, moving what the join displaces
|
|
359
|
+
*/
|
|
360
|
+
admit_seat(pending, admit = {}) {
|
|
361
|
+
const ticket = this.#disarm(pending);
|
|
362
|
+
if (ticket === null)
|
|
363
|
+
return refuse('evicted');
|
|
364
|
+
const member_state = admit.state ?? null;
|
|
365
|
+
if (member_state !== null && utf8_length(member_state) > this.options.max_state_bytes) {
|
|
366
|
+
this.#take_pending(ticket);
|
|
367
|
+
return refuse('state_too_large');
|
|
368
|
+
}
|
|
369
|
+
const scope = admit.scope ?? null;
|
|
370
|
+
const exclusive = this.options.exclusive;
|
|
371
|
+
const entry = this.#take_pending(ticket);
|
|
372
|
+
if (!entry || entry.kind.type !== 'seat')
|
|
373
|
+
return refuse('evicted');
|
|
374
|
+
const { member_id, rejoin_of } = entry.kind;
|
|
375
|
+
if (entry.refused !== null)
|
|
376
|
+
return refuse(entry.refused);
|
|
377
|
+
const { connection_id, actor_id, room_id } = entry;
|
|
378
|
+
const conn = this.#by_connection.get(connection_id);
|
|
379
|
+
if (!conn || conn.signal.aborted)
|
|
380
|
+
return refuse('connection_closed');
|
|
381
|
+
const own_seat = conn_seat(conn, room_id, actor_id);
|
|
382
|
+
if (scope !== null && entry.poisoned.includes(scope))
|
|
383
|
+
return refuse('evicted');
|
|
384
|
+
const now = this.#now();
|
|
385
|
+
if (exclusive && rejoin_of !== null && this.#rejoin_moved(actor_id, rejoin_of, now)) {
|
|
386
|
+
return refuse('seat_moved');
|
|
387
|
+
}
|
|
388
|
+
// the seats this join moves: the actor's own on this connection in the
|
|
389
|
+
// room, and on an exclusive registry the actor's, wherever it is
|
|
390
|
+
const displaced = [];
|
|
391
|
+
if (own_seat !== null)
|
|
392
|
+
displaced.push({ room_id, member_id: own_seat });
|
|
393
|
+
const actor_seat = exclusive ? this.#seat_of_actor.get(actor_id) : undefined;
|
|
394
|
+
if (actor_seat && !displaced.some((d) => d.member_id === actor_seat.member_id)) {
|
|
395
|
+
displaced.push({ room_id: actor_seat.room_id, member_id: actor_seat.member_id });
|
|
396
|
+
}
|
|
397
|
+
const registry_cap = this.options.max_members_per_room;
|
|
398
|
+
const cap_in = admit.max_members ?? null;
|
|
399
|
+
if (cap_in !== null && !(Number.isSafeInteger(cap_in) && cap_in >= 0)) {
|
|
400
|
+
throw new Error(`RoomRegistry: max_members must be an integer of at least 0, got ${cap_in}`);
|
|
401
|
+
}
|
|
402
|
+
const cap = cap_in === null ? registry_cap : Math.min(cap_in, registry_cap);
|
|
403
|
+
const seated = this.#rooms.get(room_id)?.seats.length ?? 0;
|
|
404
|
+
const freed = displaced.filter((d) => d.room_id === room_id).length;
|
|
405
|
+
if (Math.max(seated - freed, 0) >= cap)
|
|
406
|
+
return refuse('full');
|
|
407
|
+
for (const d of displaced) {
|
|
408
|
+
const removed = this.#remove(d.room_id, [d.member_id], [], { kind: 'moved', to: room_id });
|
|
409
|
+
if (exclusive) {
|
|
410
|
+
for (const seat of removed)
|
|
411
|
+
this.#push_tombstone(seat.actor_id, seat.member_id, now, true);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const room = this.#room_entry(room_id);
|
|
415
|
+
room.seats.push({ member_id, connection_id, actor_id, scope, state: member_state });
|
|
416
|
+
const seq = ++room.seq;
|
|
417
|
+
this.#send(recipients_of(room, member_id), room_member_joined_notification_spec.method, {
|
|
418
|
+
room_id,
|
|
419
|
+
seq,
|
|
420
|
+
member: { member_id, actor_id, state: member_state }
|
|
421
|
+
});
|
|
422
|
+
const members = members_of(room);
|
|
423
|
+
conn_set_seat(conn, room_id, actor_id, member_id);
|
|
424
|
+
if (exclusive) {
|
|
425
|
+
this.#seat_of_actor.set(actor_id, { room_id, member_id });
|
|
426
|
+
this.#settle_tombstones(actor_id, rejoin_of, now);
|
|
427
|
+
}
|
|
428
|
+
return ok({ room_id, member_id, seq, members, topology: this.options.topology });
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* End `actor_id`'s seat in the room on the connection (`room_member_left`
|
|
432
|
+
* with reason `left`), and refuse that actor's joins of the room still in
|
|
433
|
+
* flight on it (`join_superseded`). Another actor's seat on the same
|
|
434
|
+
* connection is untouched. Idempotent.
|
|
435
|
+
*
|
|
436
|
+
* @returns `true` when it ended or refused anything
|
|
437
|
+
* @mutates this - removes the seat, marks the joins in flight
|
|
438
|
+
*/
|
|
439
|
+
leave(connection_id, actor_id, room_id) {
|
|
440
|
+
const conn = this.#by_connection.get(connection_id);
|
|
441
|
+
if (!conn)
|
|
442
|
+
return false;
|
|
443
|
+
const seat = conn_seat(conn, room_id, actor_id);
|
|
444
|
+
let left = false;
|
|
445
|
+
for (const ticket of conn.pending) {
|
|
446
|
+
const pending = this.#pending.get(ticket);
|
|
447
|
+
if (pending &&
|
|
448
|
+
pending.room_id === room_id &&
|
|
449
|
+
pending.actor_id === actor_id &&
|
|
450
|
+
pending.kind.type === 'seat' &&
|
|
451
|
+
pending.refused === null) {
|
|
452
|
+
pending.refused = 'join_superseded';
|
|
453
|
+
left = true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (seat !== null) {
|
|
457
|
+
this.#remove(room_id, [seat], [], { kind: 'left' });
|
|
458
|
+
left = true;
|
|
459
|
+
}
|
|
460
|
+
return left;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Relay `payload` from `actor_id`'s seat in the room on the connection to
|
|
464
|
+
* the member `to`, or with `null` to every other member (`room_message`).
|
|
465
|
+
* Watchers get no messages.
|
|
466
|
+
*
|
|
467
|
+
* @returns the message's `seq`, or `payload_too_large`, `not_joined`, or
|
|
468
|
+
* `member_not_found` when `to` names no member of the room — or names the
|
|
469
|
+
* sender's own seat: a seat never addresses itself
|
|
470
|
+
*/
|
|
471
|
+
send(connection_id, actor_id, room_id, to, payload) {
|
|
472
|
+
if (utf8_length(payload) > this.options.max_payload_bytes)
|
|
473
|
+
return refuse('payload_too_large');
|
|
474
|
+
const conn = this.#by_connection.get(connection_id);
|
|
475
|
+
const from = conn ? conn_seat(conn, room_id, actor_id) : null;
|
|
476
|
+
if (from === null)
|
|
477
|
+
return refuse('not_joined');
|
|
478
|
+
const room = this.#rooms.get(room_id);
|
|
479
|
+
if (!room)
|
|
480
|
+
return refuse('not_joined');
|
|
481
|
+
let recipients;
|
|
482
|
+
if (to === null) {
|
|
483
|
+
recipients = member_connections(room, from);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
if (to === from)
|
|
487
|
+
return refuse('member_not_found');
|
|
488
|
+
const addressee = room.seats.find((seat) => seat.member_id === to);
|
|
489
|
+
if (!addressee)
|
|
490
|
+
return refuse('member_not_found');
|
|
491
|
+
recipients = new Set([addressee.connection_id]);
|
|
492
|
+
}
|
|
493
|
+
const seq = ++room.seq;
|
|
494
|
+
this.#send(recipients, room_message_notification_spec.method, {
|
|
495
|
+
room_id,
|
|
496
|
+
seq,
|
|
497
|
+
from,
|
|
498
|
+
to,
|
|
499
|
+
payload
|
|
500
|
+
});
|
|
501
|
+
return ok(seq);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Set `actor_id`'s member state in the room on the connection, telling the
|
|
505
|
+
* other members and the watchers (`room_member_state`).
|
|
506
|
+
*
|
|
507
|
+
* @returns its `seq`, or `state_too_large` or `not_joined`
|
|
508
|
+
*/
|
|
509
|
+
set_state(connection_id, actor_id, room_id, member_state) {
|
|
510
|
+
if (utf8_length(member_state) > this.options.max_state_bytes)
|
|
511
|
+
return refuse('state_too_large');
|
|
512
|
+
const conn = this.#by_connection.get(connection_id);
|
|
513
|
+
const member_id = conn ? conn_seat(conn, room_id, actor_id) : null;
|
|
514
|
+
if (member_id === null)
|
|
515
|
+
return refuse('not_joined');
|
|
516
|
+
const room = this.#rooms.get(room_id);
|
|
517
|
+
if (!room)
|
|
518
|
+
return refuse('not_joined');
|
|
519
|
+
const seat = room.seats.find((s) => s.member_id === member_id);
|
|
520
|
+
if (!seat)
|
|
521
|
+
return refuse('not_joined');
|
|
522
|
+
seat.state = member_state;
|
|
523
|
+
const seq = ++room.seq;
|
|
524
|
+
this.#send(recipients_of(room, member_id), room_member_state_notification_spec.method, {
|
|
525
|
+
room_id,
|
|
526
|
+
seq,
|
|
527
|
+
member_id,
|
|
528
|
+
state: member_state
|
|
529
|
+
});
|
|
530
|
+
return ok(seq);
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Phase one of a watch, as `reserve_seat` is of a join.
|
|
534
|
+
*
|
|
535
|
+
* @returns the watch in flight, or `connection_closed`, or `watch_limit`
|
|
536
|
+
* when the connection watches its maximum of other rooms
|
|
537
|
+
* @mutates this - records the watch in flight, and on the connection's
|
|
538
|
+
* first reserve adds its one disconnect listener
|
|
539
|
+
*/
|
|
540
|
+
reserve_watch(connection_id, actor_id, room_id) {
|
|
541
|
+
const conn = this.#connection_entry(connection_id);
|
|
542
|
+
if (!conn)
|
|
543
|
+
return refuse('connection_closed');
|
|
544
|
+
if (!conn.watches.has(room_id) &&
|
|
545
|
+
conn.watches.size >= this.options.max_watches_per_connection) {
|
|
546
|
+
return refuse('watch_limit');
|
|
547
|
+
}
|
|
548
|
+
return ok(this.#reserve(conn, { type: 'watch' }, connection_id, actor_id, new PendingWatch(room_id)));
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Phase two of a watch: from now on the connection hears the room's member
|
|
552
|
+
* events. Watching a room already watched answers its occupancy again and
|
|
553
|
+
* takes the new scope and actor. Consumes `pending`, whatever it answers.
|
|
554
|
+
*
|
|
555
|
+
* @returns the room's occupancy, or `evicted` or `connection_closed` when
|
|
556
|
+
* the watch was refused in flight or its socket closed, or `watch_limit`
|
|
557
|
+
* @mutates this - records the watch
|
|
558
|
+
*/
|
|
559
|
+
admit_watch(pending, scope = null) {
|
|
560
|
+
const ticket = this.#disarm(pending);
|
|
561
|
+
if (ticket === null)
|
|
562
|
+
return refuse('evicted');
|
|
563
|
+
const entry = this.#take_pending(ticket);
|
|
564
|
+
if (!entry || entry.kind.type !== 'watch')
|
|
565
|
+
return refuse('evicted');
|
|
566
|
+
if (entry.refused !== null)
|
|
567
|
+
return refuse(entry.refused);
|
|
568
|
+
if (scope !== null && entry.poisoned.includes(scope))
|
|
569
|
+
return refuse('evicted');
|
|
570
|
+
const { connection_id, room_id } = entry;
|
|
571
|
+
const conn = this.#by_connection.get(connection_id);
|
|
572
|
+
if (!conn || conn.signal.aborted)
|
|
573
|
+
return refuse('connection_closed');
|
|
574
|
+
if (!conn.watches.has(room_id) &&
|
|
575
|
+
conn.watches.size >= this.options.max_watches_per_connection) {
|
|
576
|
+
return refuse('watch_limit');
|
|
577
|
+
}
|
|
578
|
+
conn.watches.add(room_id);
|
|
579
|
+
const room = this.#room_entry(room_id);
|
|
580
|
+
const watcher = room.watchers.find((w) => w.connection_id === connection_id);
|
|
581
|
+
if (watcher) {
|
|
582
|
+
watcher.actor_id = entry.actor_id;
|
|
583
|
+
watcher.scope = scope;
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
room.watchers.push({ connection_id, actor_id: entry.actor_id, scope });
|
|
587
|
+
}
|
|
588
|
+
return ok({ room_id, seq: room.seq, members: members_of(room) });
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Stop the connection's watch of the room. Idempotent. A watch still in
|
|
592
|
+
* flight is untouched — a client stops one by cancelling its request.
|
|
593
|
+
*
|
|
594
|
+
* @returns `true` when it was watching
|
|
595
|
+
*/
|
|
596
|
+
unwatch(connection_id, room_id) {
|
|
597
|
+
const watching = this.#by_connection.get(connection_id)?.watches.has(room_id) ?? false;
|
|
598
|
+
if (watching) {
|
|
599
|
+
this.#remove(room_id, [], [connection_id], { kind: 'left' });
|
|
600
|
+
}
|
|
601
|
+
return watching;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Remove the seats and watches `target` names, and refuse its joins and
|
|
605
|
+
* watches in flight (module doc, "Eviction"). Each removed seat hears
|
|
606
|
+
* `room_seat_lost`, its room `room_member_left`, each removed watch
|
|
607
|
+
* `room_watch_ended`, all with `reason`.
|
|
608
|
+
*
|
|
609
|
+
* A join in flight has no scope until its verdict, so a scope target marks
|
|
610
|
+
* it instead, and its admit refuses when its verdict's scope is that one; a
|
|
611
|
+
* join marked by more scopes than `MAX_POISONED_SCOPES` is refused outright.
|
|
612
|
+
*
|
|
613
|
+
* @returns what it removed and reached
|
|
614
|
+
*/
|
|
615
|
+
evict(target, reason) {
|
|
616
|
+
const evicted = { seats: 0, watches: 0, pending: 0 };
|
|
617
|
+
for (const pending of this.#pending.values()) {
|
|
618
|
+
if (pending.refused !== null)
|
|
619
|
+
continue;
|
|
620
|
+
switch (target.kind) {
|
|
621
|
+
case 'actor':
|
|
622
|
+
if (pending.actor_id !== target.actor_id)
|
|
623
|
+
continue;
|
|
624
|
+
pending.refused = 'evicted';
|
|
625
|
+
break;
|
|
626
|
+
case 'room':
|
|
627
|
+
if (pending.room_id !== target.room_id)
|
|
628
|
+
continue;
|
|
629
|
+
pending.refused = 'evicted';
|
|
630
|
+
break;
|
|
631
|
+
case 'actor_in_scope':
|
|
632
|
+
if (pending.actor_id !== target.actor_id)
|
|
633
|
+
continue;
|
|
634
|
+
poison(pending, target.scope);
|
|
635
|
+
break;
|
|
636
|
+
case 'scope':
|
|
637
|
+
poison(pending, target.scope);
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
evicted.pending++;
|
|
641
|
+
}
|
|
642
|
+
const room_ids = target.kind === 'room' ? [target.room_id] : [...this.#rooms.keys()];
|
|
643
|
+
for (const room_id of room_ids) {
|
|
644
|
+
const room = this.#rooms.get(room_id);
|
|
645
|
+
if (!room)
|
|
646
|
+
continue;
|
|
647
|
+
const seats = room.seats
|
|
648
|
+
.filter((seat) => target_matches(target, seat.actor_id, seat.scope, room_id))
|
|
649
|
+
.map((seat) => seat.member_id);
|
|
650
|
+
const watchers = room.watchers
|
|
651
|
+
.filter((watcher) => target_matches(target, watcher.actor_id, watcher.scope, room_id))
|
|
652
|
+
.map((watcher) => watcher.connection_id);
|
|
653
|
+
if (seats.length === 0 && watchers.length === 0)
|
|
654
|
+
continue;
|
|
655
|
+
evicted.seats += seats.length;
|
|
656
|
+
evicted.watches += watchers.length;
|
|
657
|
+
this.#remove(room_id, seats, watchers, { kind: 'evicted', reason });
|
|
658
|
+
}
|
|
659
|
+
return evicted;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* The room's members as of its current `seq`.
|
|
663
|
+
*
|
|
664
|
+
* @returns the occupancy, or `null` when no room by that id exists (no seats and no watches)
|
|
665
|
+
*/
|
|
666
|
+
occupancy(room_id) {
|
|
667
|
+
const room = this.#rooms.get(room_id);
|
|
668
|
+
if (!room)
|
|
669
|
+
return null;
|
|
670
|
+
return { room_id, seq: room.seq, members: members_of(room) };
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Drop every room, seat, watch, and tombstone, telling no one, and refuse
|
|
674
|
+
* every join and watch in flight (`evicted`). For a test server's reset
|
|
675
|
+
* between cases; the connections' disconnect listeners stay.
|
|
676
|
+
*
|
|
677
|
+
* @mutates this - empties the registry
|
|
678
|
+
*/
|
|
679
|
+
clear() {
|
|
680
|
+
this.#rooms.clear();
|
|
681
|
+
this.#seat_of_actor.clear();
|
|
682
|
+
this.#tombstones.clear();
|
|
683
|
+
for (const pending of this.#pending.values()) {
|
|
684
|
+
if (pending.refused === null)
|
|
685
|
+
pending.refused = 'evicted';
|
|
686
|
+
}
|
|
687
|
+
for (const conn of this.#by_connection.values()) {
|
|
688
|
+
conn.seats.clear();
|
|
689
|
+
conn.watches.clear();
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Connections the registry tracks — each that reserved a seat or watch and
|
|
694
|
+
* has not closed — each holding the one disconnect listener. For tests and
|
|
695
|
+
* telemetry.
|
|
696
|
+
*/
|
|
697
|
+
connection_count() {
|
|
698
|
+
return this.#by_connection.size;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* The connection's entry, created — with its disconnect listener — on its
|
|
702
|
+
* first reserve; `null` when the connection is gone or closing.
|
|
703
|
+
*/
|
|
704
|
+
#connection_entry(connection_id) {
|
|
705
|
+
const existing = this.#by_connection.get(connection_id);
|
|
706
|
+
if (existing)
|
|
707
|
+
return existing.signal.aborted ? null : existing;
|
|
708
|
+
const signal = this.#connections.socket_signal(connection_id);
|
|
709
|
+
if (!signal || signal.aborted)
|
|
710
|
+
return null;
|
|
711
|
+
// the release runs on the next microtask, as the Rust waiter runs as its
|
|
712
|
+
// own task: a close made inside a fan-out (an overflow) aborts this signal
|
|
713
|
+
// mid-step, and must not re-enter the registry there
|
|
714
|
+
signal.addEventListener('abort', () => queueMicrotask(() => this.#release_connection(connection_id)), { once: true });
|
|
715
|
+
const conn = {
|
|
716
|
+
signal,
|
|
717
|
+
seats: new Map(),
|
|
718
|
+
watches: new Set(),
|
|
719
|
+
pending: new Set()
|
|
720
|
+
};
|
|
721
|
+
this.#by_connection.set(connection_id, conn);
|
|
722
|
+
return conn;
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Release everything a closed connection holds: its seats
|
|
726
|
+
* (`room_member_left` with reason `disconnected`), watches, and joins and
|
|
727
|
+
* watches in flight.
|
|
728
|
+
*/
|
|
729
|
+
#release_connection(connection_id) {
|
|
730
|
+
const conn = this.#by_connection.get(connection_id);
|
|
731
|
+
if (!conn)
|
|
732
|
+
return;
|
|
733
|
+
this.#by_connection.delete(connection_id);
|
|
734
|
+
for (const ticket of conn.pending) {
|
|
735
|
+
const pending = this.#pending.get(ticket);
|
|
736
|
+
if (pending && pending.refused === null)
|
|
737
|
+
pending.refused = 'connection_closed';
|
|
738
|
+
}
|
|
739
|
+
for (const room_id of conn.watches) {
|
|
740
|
+
this.#remove(room_id, [], [connection_id], { kind: 'disconnected' });
|
|
741
|
+
}
|
|
742
|
+
const now = this.#now();
|
|
743
|
+
for (const [room_id, by_actor] of conn.seats) {
|
|
744
|
+
for (const member_id of by_actor.values()) {
|
|
745
|
+
const removed = this.#remove(room_id, [member_id], [], { kind: 'disconnected' });
|
|
746
|
+
if (this.options.exclusive) {
|
|
747
|
+
for (const seat of removed) {
|
|
748
|
+
this.#push_tombstone(seat.actor_id, seat.member_id, now, false);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/** Put a join or watch in flight on `conn`, and hand `pending` its ticket. */
|
|
755
|
+
#reserve(conn, kind, connection_id, actor_id, pending) {
|
|
756
|
+
const ticket = ++this.#last_ticket;
|
|
757
|
+
conn.pending.add(ticket);
|
|
758
|
+
this.#pending.set(ticket, {
|
|
759
|
+
kind,
|
|
760
|
+
connection_id,
|
|
761
|
+
actor_id,
|
|
762
|
+
room_id: pending.room_id,
|
|
763
|
+
refused: null,
|
|
764
|
+
poisoned: []
|
|
765
|
+
});
|
|
766
|
+
pending_handles.set(pending, {
|
|
767
|
+
registry: this,
|
|
768
|
+
ticket,
|
|
769
|
+
forget: () => this.#take_pending(ticket)
|
|
770
|
+
});
|
|
771
|
+
return pending;
|
|
772
|
+
}
|
|
773
|
+
/** The room by id, created empty when there is none. */
|
|
774
|
+
#room_entry(room_id) {
|
|
775
|
+
let room = this.#rooms.get(room_id);
|
|
776
|
+
if (!room) {
|
|
777
|
+
room = { seq: 0, seats: [], watchers: [] };
|
|
778
|
+
this.#rooms.set(room_id, room);
|
|
779
|
+
}
|
|
780
|
+
return room;
|
|
781
|
+
}
|
|
782
|
+
/** Take a pending seat's or watch's ticket for its admit, or `null` when it is withdrawn, spent, or another registry's. */
|
|
783
|
+
#disarm(pending) {
|
|
784
|
+
const handle = pending_handles.get(pending);
|
|
785
|
+
if (!handle)
|
|
786
|
+
return null;
|
|
787
|
+
if (handle.registry !== this) {
|
|
788
|
+
// another registry's: release it there, as a dropped Rust guard would
|
|
789
|
+
withdraw_pending(pending);
|
|
790
|
+
return null;
|
|
791
|
+
}
|
|
792
|
+
if (!handle.forget)
|
|
793
|
+
return null;
|
|
794
|
+
handle.forget = null;
|
|
795
|
+
return handle.ticket;
|
|
796
|
+
}
|
|
797
|
+
#take_pending(ticket) {
|
|
798
|
+
const pending = this.#pending.get(ticket);
|
|
799
|
+
if (!pending)
|
|
800
|
+
return null;
|
|
801
|
+
this.#pending.delete(ticket);
|
|
802
|
+
this.#by_connection.get(pending.connection_id)?.pending.delete(ticket);
|
|
803
|
+
return pending;
|
|
804
|
+
}
|
|
805
|
+
/** Queue one notification for each connection, never waiting. */
|
|
806
|
+
#send(ids, method, params) {
|
|
807
|
+
if (ids.size === 0)
|
|
808
|
+
return;
|
|
809
|
+
const frame = JSON.stringify({ jsonrpc: '2.0', method, params });
|
|
810
|
+
for (const id of ids)
|
|
811
|
+
this.#connections.send_to(id, frame);
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Remove seats and watchers from one room, then tell everyone what `why`
|
|
815
|
+
* says (`Departure`); delete the room if it is left empty.
|
|
816
|
+
*
|
|
817
|
+
* @returns the removed seats
|
|
818
|
+
*/
|
|
819
|
+
#remove(room_id, seats, watchers, why) {
|
|
820
|
+
const left = why.kind === 'evicted' ? why.reason : why.kind;
|
|
821
|
+
const seat_lost = why.kind === 'moved'
|
|
822
|
+
? { reason: 'moved', moved_to: why.to }
|
|
823
|
+
: why.kind === 'evicted'
|
|
824
|
+
? { reason: why.reason, moved_to: null }
|
|
825
|
+
: null;
|
|
826
|
+
const watch_ended = why.kind === 'evicted' ? why.reason : null;
|
|
827
|
+
const room = this.#rooms.get(room_id);
|
|
828
|
+
if (!room)
|
|
829
|
+
return [];
|
|
830
|
+
const removed_seats = room.seats.filter((seat) => seats.includes(seat.member_id));
|
|
831
|
+
room.seats = room.seats.filter((seat) => !seats.includes(seat.member_id));
|
|
832
|
+
const removed_watchers = room.watchers.filter((w) => watchers.includes(w.connection_id));
|
|
833
|
+
room.watchers = room.watchers.filter((w) => !watchers.includes(w.connection_id));
|
|
834
|
+
// the room's remaining members and watchers hear each departure
|
|
835
|
+
const recipients = recipients_of(room, null);
|
|
836
|
+
for (const seat of removed_seats) {
|
|
837
|
+
const seq = ++room.seq;
|
|
838
|
+
this.#send(recipients, room_member_left_notification_spec.method, {
|
|
839
|
+
room_id,
|
|
840
|
+
seq,
|
|
841
|
+
member_id: seat.member_id,
|
|
842
|
+
reason: left
|
|
843
|
+
});
|
|
844
|
+
if (seat_lost) {
|
|
845
|
+
this.#send(new Set([seat.connection_id]), room_seat_lost_notification_spec.method, {
|
|
846
|
+
room_id,
|
|
847
|
+
member_id: seat.member_id,
|
|
848
|
+
reason: seat_lost.reason,
|
|
849
|
+
moved_to: seat_lost.moved_to
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
if (watch_ended !== null) {
|
|
854
|
+
for (const watcher of removed_watchers) {
|
|
855
|
+
this.#send(new Set([watcher.connection_id]), room_watch_ended_notification_spec.method, {
|
|
856
|
+
room_id,
|
|
857
|
+
reason: watch_ended
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
if (room.seats.length === 0 && room.watchers.length === 0)
|
|
862
|
+
this.#rooms.delete(room_id);
|
|
863
|
+
for (const seat of removed_seats) {
|
|
864
|
+
const conn = this.#by_connection.get(seat.connection_id);
|
|
865
|
+
if (conn)
|
|
866
|
+
conn_clear_seat(conn, room_id, seat.actor_id, seat.member_id);
|
|
867
|
+
const actor_seat = this.#seat_of_actor.get(seat.actor_id);
|
|
868
|
+
if (actor_seat?.room_id === room_id && actor_seat.member_id === seat.member_id) {
|
|
869
|
+
this.#seat_of_actor.delete(seat.actor_id);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
for (const watcher of removed_watchers) {
|
|
873
|
+
this.#by_connection.get(watcher.connection_id)?.watches.delete(room_id);
|
|
874
|
+
}
|
|
875
|
+
return removed_seats;
|
|
876
|
+
}
|
|
877
|
+
/** Whether `actor_id`'s seat `member_id` has moved since it ended. */
|
|
878
|
+
#rejoin_moved(actor_id, member_id, now) {
|
|
879
|
+
const tombstones = this.#live_tombstones(actor_id, now);
|
|
880
|
+
return tombstones?.some((t) => t.member_id === member_id && t.moved) ?? false;
|
|
881
|
+
}
|
|
882
|
+
/** Remember a seat of `actor_id` that ended without a leave. */
|
|
883
|
+
#push_tombstone(actor_id, member_id, now, moved) {
|
|
884
|
+
const tombstones = (this.#tombstones.get(actor_id) ?? []).filter((t) => !tombstone_expired(t, now));
|
|
885
|
+
tombstones.push({ member_id, at: now, moved });
|
|
886
|
+
while (tombstones.length > ROOM_REJOIN_TOMBSTONES_PER_ACTOR)
|
|
887
|
+
tombstones.shift();
|
|
888
|
+
this.#tombstones.set(actor_id, tombstones);
|
|
889
|
+
if (this.#tombstones.size > Math.max(this.#tombstone_sweep_at, TOMBSTONE_SWEEP_FLOOR)) {
|
|
890
|
+
for (const [actor, list] of this.#tombstones) {
|
|
891
|
+
const live = list.filter((t) => !tombstone_expired(t, now));
|
|
892
|
+
if (live.length === 0)
|
|
893
|
+
this.#tombstones.delete(actor);
|
|
894
|
+
else
|
|
895
|
+
this.#tombstones.set(actor, live);
|
|
896
|
+
}
|
|
897
|
+
this.#tombstone_sweep_at = this.#tombstones.size * 2;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* `actor_id` was just seated (by a rejoin of `rejoin_of`, or fresh): the
|
|
902
|
+
* rejoined seat's tombstone is spent, and every other seat it lost to a
|
|
903
|
+
* disconnect now counts as moved.
|
|
904
|
+
*/
|
|
905
|
+
#settle_tombstones(actor_id, rejoin_of, now) {
|
|
906
|
+
const tombstones = this.#live_tombstones(actor_id, now);
|
|
907
|
+
if (!tombstones)
|
|
908
|
+
return;
|
|
909
|
+
const kept = tombstones.filter((t) => t.member_id !== rejoin_of);
|
|
910
|
+
for (const t of kept)
|
|
911
|
+
t.moved = true;
|
|
912
|
+
if (kept.length === 0)
|
|
913
|
+
this.#tombstones.delete(actor_id);
|
|
914
|
+
else
|
|
915
|
+
this.#tombstones.set(actor_id, kept);
|
|
916
|
+
}
|
|
917
|
+
/** `actor_id`'s unexpired tombstones, pruning the expired ones. */
|
|
918
|
+
#live_tombstones(actor_id, now) {
|
|
919
|
+
const tombstones = this.#tombstones.get(actor_id);
|
|
920
|
+
if (!tombstones)
|
|
921
|
+
return null;
|
|
922
|
+
const live = tombstones.filter((t) => !tombstone_expired(t, now));
|
|
923
|
+
this.#tombstones.set(actor_id, live);
|
|
924
|
+
return live;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
const tombstone_expired = (tombstone, now) => now - tombstone.at >= ROOM_REJOIN_TOMBSTONE_TTL_MS;
|