@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,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The room verbs over `RoomRegistry` (`actions/room_registry.ts`): join,
|
|
3
|
+
* leave, send, state, watch, and unwatch, as actions a consumer mounts on a
|
|
4
|
+
* WebSocket endpoint — the TS twin of the Rust spine's `fuz_actions::room`.
|
|
5
|
+
*
|
|
6
|
+
* `create_room_actions` returns the handlers over the specs of
|
|
7
|
+
* `actions/room_action_specs.ts`. Each is WebSocket-only — mount them on a
|
|
8
|
+
* `ws_endpoints` entry, never on an RPC endpoint (a room belongs to a socket;
|
|
9
|
+
* the Rust spine answers them `method_not_found` over HTTP, and a handler
|
|
10
|
+
* reached without a connection answers the same) — actor-required, and
|
|
11
|
+
* without side effects: the registry is memory, not the database, so no
|
|
12
|
+
* transaction opens for a relayed message. The registry does the work and the
|
|
13
|
+
* fan-out; this module parses, bounds, authorizes, and limits.
|
|
14
|
+
*
|
|
15
|
+
* Seats are keyed by connection and actor: under the dispatcher's `acting`
|
|
16
|
+
* selector, a leave, send, or state set acts on the acting actor's own seat,
|
|
17
|
+
* never another actor's on the same socket.
|
|
18
|
+
*
|
|
19
|
+
* ## The consumer's seams
|
|
20
|
+
*
|
|
21
|
+
* - **`RoomAuthorize`** decides each join and watch: `{allow: false}` answers
|
|
22
|
+
* `room_not_found`, the same as a room that doesn't exist, so a caller
|
|
23
|
+
* learns nothing of rooms it can't enter; `{allow: true, scope,
|
|
24
|
+
* max_members}` carries the `RoomScope` the seat or watch is admitted under,
|
|
25
|
+
* for eviction, and an optional member cap below the registry's. It gets the
|
|
26
|
+
* caller's ids and the database, not the request's role grants: authority is
|
|
27
|
+
* read through the database inside the reserve window (`RoomAuthorize`'s doc).
|
|
28
|
+
* - **Eviction** is the consumer calling `RoomRegistry.evict` after its own
|
|
29
|
+
* authority change commits — from the handler's `emit_after_commit` — never
|
|
30
|
+
* before.
|
|
31
|
+
* - **Rate limits.** `room_join` and `room_watch` carry the per-account class
|
|
32
|
+
* (`rate_limit: 'account'`, charged by the dispatcher): each runs the
|
|
33
|
+
* consumer's database authorizer, and one account may hold many sockets.
|
|
34
|
+
* **`RoomRateLimiter`** is per connection, outside the account budget: it
|
|
35
|
+
* bounds the count of joins, sends, and state sets, and the bytes of payloads
|
|
36
|
+
* and states as each frame carries them (JSON-escaped, `json_escaped_len`),
|
|
37
|
+
* because every room-wide byte lands in each member's outbound queue
|
|
38
|
+
* (`DEFAULT_WS_OUTBOUND_QUEUE_BYTES`) and a member reading slower than its
|
|
39
|
+
* room sends is closed for the overflow. A join stays on it too, since its
|
|
40
|
+
* state and the seats it moves fan out to every member; a watch fans out
|
|
41
|
+
* nothing, so it costs only its account charge.
|
|
42
|
+
* - **`join_extra`**, a hook whose value the join answers as `extra` (`null`
|
|
43
|
+
* without one) — for what a consumer mints per seat.
|
|
44
|
+
*
|
|
45
|
+
* ## A join, step by step
|
|
46
|
+
*
|
|
47
|
+
* The dispatcher's account charge and input parse; refuse a state over the
|
|
48
|
+
* registry's bound; charge the connection's limiter; `reserve_seat` (the join
|
|
49
|
+
* is now reachable by evictions); the authorizer; then the request's own
|
|
50
|
+
* signal — a client `cancel` of the join, or its socket closing, answers
|
|
51
|
+
* `request_cancelled` — and `admit_seat`. A join whose socket closes
|
|
52
|
+
* mid-authorizer keeps running here, unlike the Rust one, whose request future
|
|
53
|
+
* is dropped; its admit refuses (`connection_closed`, answered
|
|
54
|
+
* `request_cancelled`), so it seats nothing either way. A watch runs the same
|
|
55
|
+
* steps through the watch halves, without the connection's limiter.
|
|
56
|
+
*
|
|
57
|
+
* @module
|
|
58
|
+
*/
|
|
59
|
+
import type { Result } from '@fuzdev/fuz_util/result.ts';
|
|
60
|
+
import type { Uuid } from '../primitive_schemas.js';
|
|
61
|
+
import type { RequestActorContext } from '../auth/request_context.js';
|
|
62
|
+
import type { Db } from '../db/db.js';
|
|
63
|
+
import { type ThrownJsonrpcError } from '../http/jsonrpc_errors.js';
|
|
64
|
+
import { type RpcAction } from './action_types.js';
|
|
65
|
+
import { type MemberId, type RoomId } from './room_action_specs.js';
|
|
66
|
+
import type { RoomError, RoomRegistry, RoomRegistryLimits, RoomScope } from './room_registry.js';
|
|
67
|
+
/** Which access an authorizer decides: a seat (`join`), or occupancy only (`watch`). */
|
|
68
|
+
export type RoomAccess = 'join' | 'watch';
|
|
69
|
+
/**
|
|
70
|
+
* What `RoomAuthorize` decides on: who asks, for which room and access.
|
|
71
|
+
*
|
|
72
|
+
* It carries the caller's identity, never its authority. Each field is fixed
|
|
73
|
+
* for the request's life, so none can go stale between the dispatcher's
|
|
74
|
+
* resolve and the authorizer's read:
|
|
75
|
+
*
|
|
76
|
+
* - `actor_id` — the acting actor (after the `acting` selector), the key a
|
|
77
|
+
* policy reads its grants and memberships by;
|
|
78
|
+
* - `account_id` — the actor's account, which an actor never changes, for a
|
|
79
|
+
* policy keyed by account;
|
|
80
|
+
* - `room_id`, `access` — the request itself.
|
|
81
|
+
*
|
|
82
|
+
* Whether the actor or account is still live (not soft-deleted since the
|
|
83
|
+
* dispatcher resolved it) is the authorizer's to read with its grants, as any
|
|
84
|
+
* live-grant read does.
|
|
85
|
+
*/
|
|
86
|
+
export interface RoomAuthorizeInput {
|
|
87
|
+
readonly actor_id: Uuid;
|
|
88
|
+
readonly account_id: Uuid;
|
|
89
|
+
readonly room_id: RoomId;
|
|
90
|
+
readonly access: RoomAccess;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* An authorizer's answer: `{allow: false}` is answered `room_not_found`, as a
|
|
94
|
+
* missing room is; `{allow: true}` admits under `scope` (eviction's handle on
|
|
95
|
+
* it), and `max_members`, a non-negative integer, lowers the registry's cap
|
|
96
|
+
* for this join — a watch ignores it, and any other value is a bug the join
|
|
97
|
+
* throws on (`RoomRegistry.admit_seat`).
|
|
98
|
+
*/
|
|
99
|
+
export type RoomVerdict = {
|
|
100
|
+
readonly allow: false;
|
|
101
|
+
} | {
|
|
102
|
+
readonly allow: true;
|
|
103
|
+
readonly scope?: RoomScope | null;
|
|
104
|
+
readonly max_members?: number | null;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* The consumer's decision on each join and watch.
|
|
108
|
+
*
|
|
109
|
+
* Runs between the registry's reserve and admit, so an eviction landing while
|
|
110
|
+
* it reads still reaches the request (`RoomRegistry`'s two-phase join). It
|
|
111
|
+
* reads through `db`, the request's pool-level client (the specs have no side
|
|
112
|
+
* effects, so there is no transaction), under READ COMMITTED, and must only
|
|
113
|
+
* read. A room it doesn't know is `{allow: false}`.
|
|
114
|
+
*
|
|
115
|
+
* **Authority is read through `db`, inside the reserve window, never from the
|
|
116
|
+
* request.** The dispatcher loads the actor's role grants before the handler
|
|
117
|
+
* runs, so before the reserve: a consumer's eviction (its write committed, then
|
|
118
|
+
* `RoomRegistry.evict`) landing between that load and the reserve finds no
|
|
119
|
+
* join in flight to reach, and a verdict on those grants would seat the
|
|
120
|
+
* evicted actor. A read after the reserve sees the committed write, or the
|
|
121
|
+
* eviction reaches the join — the race closes either way. So the input carries
|
|
122
|
+
* the caller's ids (`RoomAuthorizeInput`), not its grants.
|
|
123
|
+
*
|
|
124
|
+
* A throw is a hard failure (a query that failed), answered as it is — a
|
|
125
|
+
* `ThrownJsonrpcError` with its code, anything else `internal_error`.
|
|
126
|
+
*/
|
|
127
|
+
export interface RoomAuthorize {
|
|
128
|
+
readonly authorize: (input: RoomAuthorizeInput, db: Db) => RoomVerdict | Promise<RoomVerdict>;
|
|
129
|
+
}
|
|
130
|
+
/** What `RoomJoinExtra` gets for an admitted seat. */
|
|
131
|
+
export interface RoomJoinExtraInput {
|
|
132
|
+
/**
|
|
133
|
+
* The request's context as the dispatcher resolved it, before the reserve:
|
|
134
|
+
* its `role_grants` may predate an eviction, so derive nothing from them
|
|
135
|
+
* that needs current authority (the seat itself was admitted on the
|
|
136
|
+
* authorizer's live read).
|
|
137
|
+
*/
|
|
138
|
+
readonly auth: RequestActorContext;
|
|
139
|
+
readonly room_id: RoomId;
|
|
140
|
+
readonly member_id: MemberId;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* A consumer's addition to every admitted join's answer, as `extra` — a JSON
|
|
144
|
+
* value; `undefined` answers `extra: null`.
|
|
145
|
+
*/
|
|
146
|
+
export type RoomJoinExtra = (input: RoomJoinExtraInput) => unknown;
|
|
147
|
+
/**
|
|
148
|
+
* A `RoomRateLimiter`'s budgets. Each is a bucket holding at most its maximum,
|
|
149
|
+
* refilled at that maximum per `window_ms`: a burst may spend it all at once,
|
|
150
|
+
* and the sustained rate is the maximum per window.
|
|
151
|
+
*/
|
|
152
|
+
export interface RoomRateLimitOptions {
|
|
153
|
+
/** Joins, sends, and state sets — each costs one. */
|
|
154
|
+
readonly max_requests: number;
|
|
155
|
+
/**
|
|
156
|
+
* Payload and state bytes as their frames carry them (`json_escaped_len`).
|
|
157
|
+
* At least a max-size payload's and state's worst case
|
|
158
|
+
* (`JSON_ESCAPE_MAX_EXPANSION` times the registry's bound), or
|
|
159
|
+
* `create_room_actions` refuses the pair.
|
|
160
|
+
*/
|
|
161
|
+
readonly max_bytes: number;
|
|
162
|
+
/** The refill window, in ms. */
|
|
163
|
+
readonly window_ms: number;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The default budgets — the Rust `DEFAULT_ROOM_RATE_LIMIT`.
|
|
167
|
+
*
|
|
168
|
+
* Room for a join into a full-mesh room (an offer and its candidates to every
|
|
169
|
+
* member) several times over. A full burst stays under half of a reader's
|
|
170
|
+
* default outbound queue (`DEFAULT_WS_OUTBOUND_QUEUE_BYTES`, 4 MiB), by
|
|
171
|
+
* construction: a charge is exactly the bytes the payload or state adds to its
|
|
172
|
+
* frame, and each charged request puts at most four frames in any other
|
|
173
|
+
* reader's queue (for a join, which moves at most one seat: the moved seat
|
|
174
|
+
* leaving, its `room_seat_lost`, the join's `room_member_joined`, and the
|
|
175
|
+
* uncharged leave that later ends the new seat), so one member's burst queues
|
|
176
|
+
* at most `max_bytes` plus four frames' fixed overhead per request.
|
|
177
|
+
*/
|
|
178
|
+
export declare const DEFAULT_ROOM_RATE_LIMIT: RoomRateLimitOptions;
|
|
179
|
+
/** A charge the budgets can't cover: whole seconds, at least one, until they would. */
|
|
180
|
+
export interface RoomRateLimited {
|
|
181
|
+
readonly retry_after: number;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The room verbs' limiter, one budget pair per connection
|
|
185
|
+
* (`RoomRateLimitOptions`) — the twin of the Rust `RoomRateLimiter`.
|
|
186
|
+
*
|
|
187
|
+
* A connection's buckets are forgotten once full again — a full bucket is what
|
|
188
|
+
* a fresh one is — so the map holds only connections that spent recently,
|
|
189
|
+
* swept as it grows.
|
|
190
|
+
*/
|
|
191
|
+
export declare class RoomRateLimiter {
|
|
192
|
+
#private;
|
|
193
|
+
readonly options: RoomRateLimitOptions;
|
|
194
|
+
/**
|
|
195
|
+
* @param options - the budgets
|
|
196
|
+
* @param now - the monotonic clock in ms; default `performance.now`, a test
|
|
197
|
+
* passes its own to drive the refill
|
|
198
|
+
* @throws Error when a budget is not a non-negative safe integer, or the
|
|
199
|
+
* window is not positive
|
|
200
|
+
*/
|
|
201
|
+
constructor(options?: RoomRateLimitOptions, now?: () => number);
|
|
202
|
+
/**
|
|
203
|
+
* Spend one request and `bytes` from `connection_id`'s budgets, both or
|
|
204
|
+
* neither.
|
|
205
|
+
*
|
|
206
|
+
* @returns ok, or the wait when either budget can't cover it — nothing is
|
|
207
|
+
* spent then
|
|
208
|
+
* @mutates this - spends the connection's buckets
|
|
209
|
+
*/
|
|
210
|
+
charge(connection_id: Uuid, bytes: number): Result<object, RoomRateLimited>;
|
|
211
|
+
/** Connections tracked — those whose buckets aren't full again. For tests. */
|
|
212
|
+
tracked(): number;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Why a limiter can't serve a registry: it would refuse a request the
|
|
216
|
+
* registry's bounds admit, forever. The twin of the Rust `RoomSpecSetError`.
|
|
217
|
+
*/
|
|
218
|
+
export type RoomLimitsError =
|
|
219
|
+
/** The limiter's `max_requests` is zero. */
|
|
220
|
+
{
|
|
221
|
+
readonly kind: 'no_request_budget';
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The limiter's `max_bytes` is below what one max-size payload or state can
|
|
225
|
+
* charge (its UTF-8 bound times `JSON_ESCAPE_MAX_EXPANSION`).
|
|
226
|
+
*/
|
|
227
|
+
| {
|
|
228
|
+
readonly kind: 'byte_budget_below_bound';
|
|
229
|
+
readonly bound: 'payload' | 'state';
|
|
230
|
+
readonly charge: number;
|
|
231
|
+
readonly max_bytes: number;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Check that a limiter can cover every request a registry admits.
|
|
235
|
+
*
|
|
236
|
+
* @returns `null` when it can, else why not
|
|
237
|
+
*/
|
|
238
|
+
export declare const check_room_limits: (registry: RoomRegistryLimits, limiter: RoomRateLimitOptions) => RoomLimitsError | null;
|
|
239
|
+
/** The capabilities the room verbs run on. */
|
|
240
|
+
export interface RoomActionsDeps {
|
|
241
|
+
/** The registry, over the connection registry of the WebSocket endpoint the verbs are mounted on. */
|
|
242
|
+
readonly registry: RoomRegistry;
|
|
243
|
+
readonly authorize: RoomAuthorize;
|
|
244
|
+
/**
|
|
245
|
+
* The per-connection limiter; `null` leaves the verbs unlimited — a test
|
|
246
|
+
* server's choice, not a deployment's.
|
|
247
|
+
*/
|
|
248
|
+
readonly limiter: RoomRateLimiter | null;
|
|
249
|
+
}
|
|
250
|
+
/** Options for `create_room_actions`. */
|
|
251
|
+
export interface RoomActionsOptions {
|
|
252
|
+
/** The join's `extra`; omitted answers `extra: null`. */
|
|
253
|
+
readonly join_extra?: RoomJoinExtra;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The answer for a registry refusal — the codes and reasons of the Rust
|
|
257
|
+
* `room_error`.
|
|
258
|
+
*
|
|
259
|
+
* @param error - the registry's refusal
|
|
260
|
+
* @returns the error to throw
|
|
261
|
+
*/
|
|
262
|
+
export declare const to_room_jsonrpc_error: (error: RoomError) => ThrownJsonrpcError;
|
|
263
|
+
/**
|
|
264
|
+
* Build the room verbs (module doc), ready for a WebSocket endpoint's action
|
|
265
|
+
* list — beside `room_notification_actions` (`actions/room_action_specs.ts`)
|
|
266
|
+
* for the surface to list what the registry pushes.
|
|
267
|
+
*
|
|
268
|
+
* @param deps - the registry, the authorizer, and the limiter
|
|
269
|
+
* @param options - the join's `extra`
|
|
270
|
+
* @returns the actions, in `all_room_action_specs` order
|
|
271
|
+
* @throws Error when the limiter can never cover a request the registry's
|
|
272
|
+
* bounds admit (`check_room_limits`): no request budget, or a byte budget
|
|
273
|
+
* below a max-size payload's or state's worst-case escaped charge
|
|
274
|
+
*/
|
|
275
|
+
export declare const create_room_actions: (deps: RoomActionsDeps, options?: RoomActionsOptions) => Array<RpcAction>;
|
|
276
|
+
//# sourceMappingURL=room_actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"room_actions.d.ts","sourceRoot":"","sources":["../../src/lib/actions/room_actions.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The room verbs over `RoomRegistry` (`actions/room_registry.ts`): join,
|
|
3
|
+
* leave, send, state, watch, and unwatch, as actions a consumer mounts on a
|
|
4
|
+
* WebSocket endpoint — the TS twin of the Rust spine's `fuz_actions::room`.
|
|
5
|
+
*
|
|
6
|
+
* `create_room_actions` returns the handlers over the specs of
|
|
7
|
+
* `actions/room_action_specs.ts`. Each is WebSocket-only — mount them on a
|
|
8
|
+
* `ws_endpoints` entry, never on an RPC endpoint (a room belongs to a socket;
|
|
9
|
+
* the Rust spine answers them `method_not_found` over HTTP, and a handler
|
|
10
|
+
* reached without a connection answers the same) — actor-required, and
|
|
11
|
+
* without side effects: the registry is memory, not the database, so no
|
|
12
|
+
* transaction opens for a relayed message. The registry does the work and the
|
|
13
|
+
* fan-out; this module parses, bounds, authorizes, and limits.
|
|
14
|
+
*
|
|
15
|
+
* Seats are keyed by connection and actor: under the dispatcher's `acting`
|
|
16
|
+
* selector, a leave, send, or state set acts on the acting actor's own seat,
|
|
17
|
+
* never another actor's on the same socket.
|
|
18
|
+
*
|
|
19
|
+
* ## The consumer's seams
|
|
20
|
+
*
|
|
21
|
+
* - **`RoomAuthorize`** decides each join and watch: `{allow: false}` answers
|
|
22
|
+
* `room_not_found`, the same as a room that doesn't exist, so a caller
|
|
23
|
+
* learns nothing of rooms it can't enter; `{allow: true, scope,
|
|
24
|
+
* max_members}` carries the `RoomScope` the seat or watch is admitted under,
|
|
25
|
+
* for eviction, and an optional member cap below the registry's. It gets the
|
|
26
|
+
* caller's ids and the database, not the request's role grants: authority is
|
|
27
|
+
* read through the database inside the reserve window (`RoomAuthorize`'s doc).
|
|
28
|
+
* - **Eviction** is the consumer calling `RoomRegistry.evict` after its own
|
|
29
|
+
* authority change commits — from the handler's `emit_after_commit` — never
|
|
30
|
+
* before.
|
|
31
|
+
* - **Rate limits.** `room_join` and `room_watch` carry the per-account class
|
|
32
|
+
* (`rate_limit: 'account'`, charged by the dispatcher): each runs the
|
|
33
|
+
* consumer's database authorizer, and one account may hold many sockets.
|
|
34
|
+
* **`RoomRateLimiter`** is per connection, outside the account budget: it
|
|
35
|
+
* bounds the count of joins, sends, and state sets, and the bytes of payloads
|
|
36
|
+
* and states as each frame carries them (JSON-escaped, `json_escaped_len`),
|
|
37
|
+
* because every room-wide byte lands in each member's outbound queue
|
|
38
|
+
* (`DEFAULT_WS_OUTBOUND_QUEUE_BYTES`) and a member reading slower than its
|
|
39
|
+
* room sends is closed for the overflow. A join stays on it too, since its
|
|
40
|
+
* state and the seats it moves fan out to every member; a watch fans out
|
|
41
|
+
* nothing, so it costs only its account charge.
|
|
42
|
+
* - **`join_extra`**, a hook whose value the join answers as `extra` (`null`
|
|
43
|
+
* without one) — for what a consumer mints per seat.
|
|
44
|
+
*
|
|
45
|
+
* ## A join, step by step
|
|
46
|
+
*
|
|
47
|
+
* The dispatcher's account charge and input parse; refuse a state over the
|
|
48
|
+
* registry's bound; charge the connection's limiter; `reserve_seat` (the join
|
|
49
|
+
* is now reachable by evictions); the authorizer; then the request's own
|
|
50
|
+
* signal — a client `cancel` of the join, or its socket closing, answers
|
|
51
|
+
* `request_cancelled` — and `admit_seat`. A join whose socket closes
|
|
52
|
+
* mid-authorizer keeps running here, unlike the Rust one, whose request future
|
|
53
|
+
* is dropped; its admit refuses (`connection_closed`, answered
|
|
54
|
+
* `request_cancelled`), so it seats nothing either way. A watch runs the same
|
|
55
|
+
* steps through the watch halves, without the connection's limiter.
|
|
56
|
+
*
|
|
57
|
+
* @module
|
|
58
|
+
*/
|
|
59
|
+
import { jsonrpc_errors } from '../http/jsonrpc_errors.js';
|
|
60
|
+
import { rpc_action } from './action_types.js';
|
|
61
|
+
import { ERROR_ROOM_FULL, ERROR_ROOM_JOIN_SUPERSEDED, ERROR_ROOM_MEMBER_NOT_FOUND, ERROR_ROOM_NOT_FOUND, ERROR_ROOM_NOT_JOINED, ERROR_ROOM_PAYLOAD_TOO_LARGE, ERROR_ROOM_SEAT_MOVED, ERROR_ROOM_STATE_TOO_LARGE, ERROR_ROOM_WATCH_LIMIT, JSON_ESCAPE_MAX_EXPANSION, json_escaped_len, room_join_action_spec, room_leave_action_spec, room_send_action_spec, room_state_set_action_spec, room_unwatch_action_spec, room_watch_action_spec } from './room_action_specs.js';
|
|
62
|
+
import { utf8_length } from './transports.js';
|
|
63
|
+
/**
|
|
64
|
+
* The default budgets — the Rust `DEFAULT_ROOM_RATE_LIMIT`.
|
|
65
|
+
*
|
|
66
|
+
* Room for a join into a full-mesh room (an offer and its candidates to every
|
|
67
|
+
* member) several times over. A full burst stays under half of a reader's
|
|
68
|
+
* default outbound queue (`DEFAULT_WS_OUTBOUND_QUEUE_BYTES`, 4 MiB), by
|
|
69
|
+
* construction: a charge is exactly the bytes the payload or state adds to its
|
|
70
|
+
* frame, and each charged request puts at most four frames in any other
|
|
71
|
+
* reader's queue (for a join, which moves at most one seat: the moved seat
|
|
72
|
+
* leaving, its `room_seat_lost`, the join's `room_member_joined`, and the
|
|
73
|
+
* uncharged leave that later ends the new seat), so one member's burst queues
|
|
74
|
+
* at most `max_bytes` plus four frames' fixed overhead per request.
|
|
75
|
+
*/
|
|
76
|
+
export const DEFAULT_ROOM_RATE_LIMIT = Object.freeze({
|
|
77
|
+
max_requests: 300,
|
|
78
|
+
max_bytes: 1024 * 1024,
|
|
79
|
+
window_ms: 10_000
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* The fewest connections the limiter tracks before it sweeps the full
|
|
83
|
+
* buckets; it sweeps again at twice what a sweep leaves.
|
|
84
|
+
*/
|
|
85
|
+
const LIMITER_SWEEP_FLOOR = 64;
|
|
86
|
+
/** Ms until a bucket holding `have` covers `need`, refilling at `max` per ms; a need past the bucket's whole size waits a window. */
|
|
87
|
+
const deficit_ms = (have, need, max, window) => {
|
|
88
|
+
if (have >= need)
|
|
89
|
+
return 0;
|
|
90
|
+
if (max === 0 || need > max * window)
|
|
91
|
+
return window;
|
|
92
|
+
return (need - have) / max;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* The room verbs' limiter, one budget pair per connection
|
|
96
|
+
* (`RoomRateLimitOptions`) — the twin of the Rust `RoomRateLimiter`.
|
|
97
|
+
*
|
|
98
|
+
* A connection's buckets are forgotten once full again — a full bucket is what
|
|
99
|
+
* a fresh one is — so the map holds only connections that spent recently,
|
|
100
|
+
* swept as it grows.
|
|
101
|
+
*/
|
|
102
|
+
export class RoomRateLimiter {
|
|
103
|
+
options;
|
|
104
|
+
#now;
|
|
105
|
+
#buckets = new Map();
|
|
106
|
+
#sweep_at = 0;
|
|
107
|
+
/**
|
|
108
|
+
* @param options - the budgets
|
|
109
|
+
* @param now - the monotonic clock in ms; default `performance.now`, a test
|
|
110
|
+
* passes its own to drive the refill
|
|
111
|
+
* @throws Error when a budget is not a non-negative safe integer, or the
|
|
112
|
+
* window is not positive
|
|
113
|
+
*/
|
|
114
|
+
constructor(options = DEFAULT_ROOM_RATE_LIMIT, now = () => performance.now()) {
|
|
115
|
+
const { max_requests, max_bytes, window_ms } = options;
|
|
116
|
+
for (const [name, value] of [
|
|
117
|
+
['max_requests', max_requests],
|
|
118
|
+
['max_bytes', max_bytes]
|
|
119
|
+
]) {
|
|
120
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
121
|
+
throw new Error(`RoomRateLimiter: ${name} must be a non-negative integer, got ${value}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!(window_ms > 0) || !Number.isFinite(window_ms)) {
|
|
125
|
+
throw new Error(`RoomRateLimiter: window_ms must be positive, got ${window_ms}`);
|
|
126
|
+
}
|
|
127
|
+
this.options = Object.freeze({ max_requests, max_bytes, window_ms });
|
|
128
|
+
this.#now = now;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Spend one request and `bytes` from `connection_id`'s budgets, both or
|
|
132
|
+
* neither.
|
|
133
|
+
*
|
|
134
|
+
* @returns ok, or the wait when either budget can't cover it — nothing is
|
|
135
|
+
* spent then
|
|
136
|
+
* @mutates this - spends the connection's buckets
|
|
137
|
+
*/
|
|
138
|
+
charge(connection_id, bytes) {
|
|
139
|
+
const now = this.#now();
|
|
140
|
+
const { max_requests, max_bytes, window_ms: window } = this.options;
|
|
141
|
+
const need_requests = window;
|
|
142
|
+
const need_bytes = bytes * window;
|
|
143
|
+
let buckets = this.#buckets.get(connection_id);
|
|
144
|
+
if (!buckets) {
|
|
145
|
+
buckets = this.#full(now);
|
|
146
|
+
this.#buckets.set(connection_id, buckets);
|
|
147
|
+
}
|
|
148
|
+
this.#refill(buckets, now);
|
|
149
|
+
if (buckets.requests >= need_requests && buckets.bytes >= need_bytes) {
|
|
150
|
+
buckets.requests -= need_requests;
|
|
151
|
+
buckets.bytes -= need_bytes;
|
|
152
|
+
this.#sweep(now);
|
|
153
|
+
return { ok: true };
|
|
154
|
+
}
|
|
155
|
+
const wait = Math.max(deficit_ms(buckets.requests, need_requests, max_requests, window), deficit_ms(buckets.bytes, need_bytes, max_bytes, window));
|
|
156
|
+
return { ok: false, retry_after: Math.max(Math.ceil(wait / 1000), 1) };
|
|
157
|
+
}
|
|
158
|
+
/** Connections tracked — those whose buckets aren't full again. For tests. */
|
|
159
|
+
tracked() {
|
|
160
|
+
return this.#buckets.size;
|
|
161
|
+
}
|
|
162
|
+
#full(now) {
|
|
163
|
+
const { max_requests, max_bytes, window_ms } = this.options;
|
|
164
|
+
return { requests: max_requests * window_ms, bytes: max_bytes * window_ms, at: now };
|
|
165
|
+
}
|
|
166
|
+
/** Credit what elapsed since the buckets were last read, up to full. */
|
|
167
|
+
#refill(buckets, now) {
|
|
168
|
+
const full = this.#full(now);
|
|
169
|
+
const elapsed = Math.max(now - buckets.at, 0);
|
|
170
|
+
buckets.requests = Math.min(buckets.requests + elapsed * this.options.max_requests, full.requests);
|
|
171
|
+
buckets.bytes = Math.min(buckets.bytes + elapsed * this.options.max_bytes, full.bytes);
|
|
172
|
+
buckets.at = now;
|
|
173
|
+
}
|
|
174
|
+
/** Forget the connections whose buckets are full again, once the map has grown past the last sweep's mark. */
|
|
175
|
+
#sweep(now) {
|
|
176
|
+
if (this.#buckets.size <= Math.max(this.#sweep_at, LIMITER_SWEEP_FLOOR))
|
|
177
|
+
return;
|
|
178
|
+
const full = this.#full(now);
|
|
179
|
+
for (const [connection_id, buckets] of this.#buckets) {
|
|
180
|
+
this.#refill(buckets, now);
|
|
181
|
+
if (buckets.requests >= full.requests && buckets.bytes >= full.bytes) {
|
|
182
|
+
this.#buckets.delete(connection_id);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
this.#sweep_at = this.#buckets.size * 2;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Check that a limiter can cover every request a registry admits.
|
|
190
|
+
*
|
|
191
|
+
* @returns `null` when it can, else why not
|
|
192
|
+
*/
|
|
193
|
+
export const check_room_limits = (registry, limiter) => {
|
|
194
|
+
if (limiter.max_requests === 0)
|
|
195
|
+
return { kind: 'no_request_budget' };
|
|
196
|
+
for (const [bound, bytes] of [
|
|
197
|
+
['payload', registry.max_payload_bytes],
|
|
198
|
+
['state', registry.max_state_bytes]
|
|
199
|
+
]) {
|
|
200
|
+
const charge = bytes * JSON_ESCAPE_MAX_EXPANSION;
|
|
201
|
+
if (charge > limiter.max_bytes) {
|
|
202
|
+
return { kind: 'byte_budget_below_bound', bound, charge, max_bytes: limiter.max_bytes };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
206
|
+
};
|
|
207
|
+
const format_room_limits_error = (error) => error.kind === 'no_request_budget'
|
|
208
|
+
? "the room limiter's max_requests is 0, so it refuses every join, send, and state set"
|
|
209
|
+
: `the room limiter's max_bytes (${error.max_bytes}) is below the worst-case charge of a ` +
|
|
210
|
+
`max-size ${error.bound} (${error.charge} bytes), which it would refuse forever`;
|
|
211
|
+
/** `room_not_found` — the answer to a denial, a missing room, and an eviction in flight alike. */
|
|
212
|
+
const room_not_found = () => jsonrpc_errors.not_found('room', { reason: ERROR_ROOM_NOT_FOUND });
|
|
213
|
+
const request_cancelled = () => jsonrpc_errors.request_cancelled('request cancelled');
|
|
214
|
+
/**
|
|
215
|
+
* The answer for a registry refusal — the codes and reasons of the Rust
|
|
216
|
+
* `room_error`.
|
|
217
|
+
*
|
|
218
|
+
* @param error - the registry's refusal
|
|
219
|
+
* @returns the error to throw
|
|
220
|
+
*/
|
|
221
|
+
export const to_room_jsonrpc_error = (error) => {
|
|
222
|
+
switch (error) {
|
|
223
|
+
case 'evicted':
|
|
224
|
+
return room_not_found();
|
|
225
|
+
// the socket is closing; nothing will read this
|
|
226
|
+
case 'connection_closed':
|
|
227
|
+
return request_cancelled();
|
|
228
|
+
case 'join_superseded':
|
|
229
|
+
return jsonrpc_errors.conflict('the join was superseded by a leave', {
|
|
230
|
+
reason: ERROR_ROOM_JOIN_SUPERSEDED
|
|
231
|
+
});
|
|
232
|
+
case 'seat_moved':
|
|
233
|
+
return jsonrpc_errors.conflict('the seat has moved', { reason: ERROR_ROOM_SEAT_MOVED });
|
|
234
|
+
case 'full':
|
|
235
|
+
return jsonrpc_errors.conflict('room is full', { reason: ERROR_ROOM_FULL });
|
|
236
|
+
case 'not_joined':
|
|
237
|
+
return jsonrpc_errors.conflict('not joined to the room', { reason: ERROR_ROOM_NOT_JOINED });
|
|
238
|
+
case 'member_not_found':
|
|
239
|
+
return jsonrpc_errors.not_found('room member', { reason: ERROR_ROOM_MEMBER_NOT_FOUND });
|
|
240
|
+
case 'payload_too_large':
|
|
241
|
+
return jsonrpc_errors.invalid_params('payload is too large', {
|
|
242
|
+
reason: ERROR_ROOM_PAYLOAD_TOO_LARGE
|
|
243
|
+
});
|
|
244
|
+
case 'state_too_large':
|
|
245
|
+
return jsonrpc_errors.invalid_params('state is too large', {
|
|
246
|
+
reason: ERROR_ROOM_STATE_TOO_LARGE
|
|
247
|
+
});
|
|
248
|
+
case 'watch_limit':
|
|
249
|
+
return jsonrpc_errors.conflict('too many watches', { reason: ERROR_ROOM_WATCH_LIMIT });
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Build the room verbs (module doc), ready for a WebSocket endpoint's action
|
|
254
|
+
* list — beside `room_notification_actions` (`actions/room_action_specs.ts`)
|
|
255
|
+
* for the surface to list what the registry pushes.
|
|
256
|
+
*
|
|
257
|
+
* @param deps - the registry, the authorizer, and the limiter
|
|
258
|
+
* @param options - the join's `extra`
|
|
259
|
+
* @returns the actions, in `all_room_action_specs` order
|
|
260
|
+
* @throws Error when the limiter can never cover a request the registry's
|
|
261
|
+
* bounds admit (`check_room_limits`): no request budget, or a byte budget
|
|
262
|
+
* below a max-size payload's or state's worst-case escaped charge
|
|
263
|
+
*/
|
|
264
|
+
export const create_room_actions = (deps, options = {}) => {
|
|
265
|
+
const { registry, authorize, limiter } = deps;
|
|
266
|
+
const { join_extra } = options;
|
|
267
|
+
if (limiter) {
|
|
268
|
+
const error = check_room_limits(registry.options, limiter.options);
|
|
269
|
+
if (error)
|
|
270
|
+
throw new Error(`create_room_actions: ${format_room_limits_error(error)}`);
|
|
271
|
+
}
|
|
272
|
+
/** The caller's socket; the verbs are WS-only, so its absence is a mount on the wrong endpoint. */
|
|
273
|
+
const require_connection = (connection_id, method) => {
|
|
274
|
+
if (connection_id === undefined)
|
|
275
|
+
throw jsonrpc_errors.method_not_found(method);
|
|
276
|
+
return connection_id;
|
|
277
|
+
};
|
|
278
|
+
/** Charge the connection's limiter one request and `bytes`, as escaped. */
|
|
279
|
+
const charge = (connection_id, bytes) => {
|
|
280
|
+
if (!limiter)
|
|
281
|
+
return;
|
|
282
|
+
const charged = limiter.charge(connection_id, bytes);
|
|
283
|
+
if (!charged.ok) {
|
|
284
|
+
throw jsonrpc_errors.rate_limited('rate limited', { retry_after: charged.retry_after });
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
return [
|
|
288
|
+
rpc_action(room_join_action_spec, async (input, ctx) => {
|
|
289
|
+
const connection_id = require_connection(ctx.connection_id, room_join_action_spec.method);
|
|
290
|
+
const { room_id } = input;
|
|
291
|
+
const state = input.state ?? null;
|
|
292
|
+
if (state !== null && utf8_length(state) > registry.options.max_state_bytes) {
|
|
293
|
+
throw to_room_jsonrpc_error('state_too_large');
|
|
294
|
+
}
|
|
295
|
+
charge(connection_id, state === null ? 0 : json_escaped_len(state));
|
|
296
|
+
const reserved = registry.reserve_seat({
|
|
297
|
+
connection_id,
|
|
298
|
+
actor_id: ctx.auth.actor.id,
|
|
299
|
+
room_id,
|
|
300
|
+
rejoin_of: input.rejoin_of ?? null
|
|
301
|
+
});
|
|
302
|
+
if (!reserved.ok)
|
|
303
|
+
throw to_room_jsonrpc_error(reserved.error);
|
|
304
|
+
const pending = reserved.value;
|
|
305
|
+
try {
|
|
306
|
+
const verdict = await authorize.authorize({
|
|
307
|
+
actor_id: ctx.auth.actor.id,
|
|
308
|
+
account_id: ctx.auth.account.id,
|
|
309
|
+
room_id,
|
|
310
|
+
access: 'join'
|
|
311
|
+
}, ctx.db);
|
|
312
|
+
if (!verdict.allow)
|
|
313
|
+
throw room_not_found();
|
|
314
|
+
// a client `cancel` of the join, or its socket closing, while the
|
|
315
|
+
// authorizer read
|
|
316
|
+
if (ctx.signal.aborted)
|
|
317
|
+
throw request_cancelled();
|
|
318
|
+
const joined = registry.admit_seat(pending, {
|
|
319
|
+
scope: verdict.scope ?? null,
|
|
320
|
+
max_members: verdict.max_members ?? null,
|
|
321
|
+
state
|
|
322
|
+
});
|
|
323
|
+
if (!joined.ok)
|
|
324
|
+
throw to_room_jsonrpc_error(joined.error);
|
|
325
|
+
// `extra` is always on the wire, `null` for a hook answering nothing, as in Rust
|
|
326
|
+
const extra = join_extra
|
|
327
|
+
? (join_extra({ auth: ctx.auth, room_id, member_id: joined.value.member_id }) ?? null)
|
|
328
|
+
: null;
|
|
329
|
+
return { ...joined.value, extra };
|
|
330
|
+
}
|
|
331
|
+
finally {
|
|
332
|
+
// a no-op once admitted; otherwise the join leaves nothing in flight
|
|
333
|
+
pending.withdraw();
|
|
334
|
+
}
|
|
335
|
+
}),
|
|
336
|
+
rpc_action(room_leave_action_spec, (input, ctx) => {
|
|
337
|
+
const connection_id = require_connection(ctx.connection_id, room_leave_action_spec.method);
|
|
338
|
+
return { left: registry.leave(connection_id, ctx.auth.actor.id, input.room_id) };
|
|
339
|
+
}),
|
|
340
|
+
rpc_action(room_send_action_spec, (input, ctx) => {
|
|
341
|
+
const connection_id = require_connection(ctx.connection_id, room_send_action_spec.method);
|
|
342
|
+
if (utf8_length(input.payload) > registry.options.max_payload_bytes) {
|
|
343
|
+
throw to_room_jsonrpc_error('payload_too_large');
|
|
344
|
+
}
|
|
345
|
+
charge(connection_id, json_escaped_len(input.payload));
|
|
346
|
+
const sent = registry.send(connection_id, ctx.auth.actor.id, input.room_id, input.to, input.payload);
|
|
347
|
+
if (!sent.ok)
|
|
348
|
+
throw to_room_jsonrpc_error(sent.error);
|
|
349
|
+
return null;
|
|
350
|
+
}),
|
|
351
|
+
rpc_action(room_state_set_action_spec, (input, ctx) => {
|
|
352
|
+
const connection_id = require_connection(ctx.connection_id, room_state_set_action_spec.method);
|
|
353
|
+
if (utf8_length(input.state) > registry.options.max_state_bytes) {
|
|
354
|
+
throw to_room_jsonrpc_error('state_too_large');
|
|
355
|
+
}
|
|
356
|
+
charge(connection_id, json_escaped_len(input.state));
|
|
357
|
+
const set = registry.set_state(connection_id, ctx.auth.actor.id, input.room_id, input.state);
|
|
358
|
+
if (!set.ok)
|
|
359
|
+
throw to_room_jsonrpc_error(set.error);
|
|
360
|
+
return { seq: set.value };
|
|
361
|
+
}),
|
|
362
|
+
rpc_action(room_watch_action_spec, async (input, ctx) => {
|
|
363
|
+
const connection_id = require_connection(ctx.connection_id, room_watch_action_spec.method);
|
|
364
|
+
const { room_id } = input;
|
|
365
|
+
const reserved = registry.reserve_watch(connection_id, ctx.auth.actor.id, room_id);
|
|
366
|
+
if (!reserved.ok)
|
|
367
|
+
throw to_room_jsonrpc_error(reserved.error);
|
|
368
|
+
const pending = reserved.value;
|
|
369
|
+
try {
|
|
370
|
+
const verdict = await authorize.authorize({
|
|
371
|
+
actor_id: ctx.auth.actor.id,
|
|
372
|
+
account_id: ctx.auth.account.id,
|
|
373
|
+
room_id,
|
|
374
|
+
access: 'watch'
|
|
375
|
+
}, ctx.db);
|
|
376
|
+
if (!verdict.allow)
|
|
377
|
+
throw room_not_found();
|
|
378
|
+
if (ctx.signal.aborted)
|
|
379
|
+
throw request_cancelled();
|
|
380
|
+
const watched = registry.admit_watch(pending, verdict.scope ?? null);
|
|
381
|
+
if (!watched.ok)
|
|
382
|
+
throw to_room_jsonrpc_error(watched.error);
|
|
383
|
+
return watched.value;
|
|
384
|
+
}
|
|
385
|
+
finally {
|
|
386
|
+
pending.withdraw();
|
|
387
|
+
}
|
|
388
|
+
}),
|
|
389
|
+
rpc_action(room_unwatch_action_spec, (input, ctx) => {
|
|
390
|
+
const connection_id = require_connection(ctx.connection_id, room_unwatch_action_spec.method);
|
|
391
|
+
return { unwatched: registry.unwatch(connection_id, input.room_id) };
|
|
392
|
+
})
|
|
393
|
+
];
|
|
394
|
+
};
|