@rapidmx/meet-plugin 0.15.0 → 0.16.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.
Files changed (74) hide show
  1. package/README.md +35 -55
  2. package/apps/meet/[token].tsx +341 -341
  3. package/apps/meet/_CallControls.tsx +508 -483
  4. package/apps/meet/_CallView.tsx +1060 -924
  5. package/apps/meet/_ChatPanel.tsx +194 -0
  6. package/apps/meet/_ColorWheel.tsx +136 -0
  7. package/apps/meet/_MeetLobby.tsx +224 -224
  8. package/apps/meet/_ParticipantTile.tsx +243 -242
  9. package/apps/meet/_Whiteboard.tsx +577 -0
  10. package/apps/meet/_icons.tsx +135 -90
  11. package/apps/meet/_meetApi.ts +213 -212
  12. package/apps/shared/collab/CollabSession.ts +675 -0
  13. package/apps/shared/collab/WhiteboardModel.ts +216 -0
  14. package/apps/shared/collab/chatMarkdown.tsx +375 -0
  15. package/apps/shared/collab/color.ts +67 -0
  16. package/apps/shared/collab/protocol.ts +321 -0
  17. package/apps/shared/collab/whiteboardRender.ts +311 -0
  18. package/apps/shared/media/filters/VideoFilterProcessor.ts +408 -408
  19. package/apps/shared/media/filters/mlModels.ts +174 -174
  20. package/apps/shared/relay/RelayClient.ts +2 -1
  21. package/apps/shared/relay/RelayTransport.ts +2 -1
  22. package/apps/shared/relay/relayEnv.ts +1 -1
  23. package/apps/shared/signaling/SignalingClient.ts +335 -0
  24. package/apps/shared/webrtc/MeshConnectionManager.ts +971 -971
  25. package/apps/shared/webrtc/types.ts +335 -316
  26. package/dist/apps/meet/[token].js +1 -1
  27. package/dist/apps/meet/_CallControls.d.ts +12 -4
  28. package/dist/apps/meet/_CallControls.js +5 -5
  29. package/dist/apps/meet/_CallView.d.ts +19 -4
  30. package/dist/apps/meet/_CallView.js +100 -11
  31. package/dist/apps/meet/_ChatPanel.d.ts +24 -0
  32. package/dist/apps/meet/_ChatPanel.js +116 -0
  33. package/dist/apps/meet/_ColorWheel.d.ts +14 -0
  34. package/dist/apps/meet/_ColorWheel.js +82 -0
  35. package/dist/apps/meet/_ParticipantTile.d.ts +2 -1
  36. package/dist/apps/meet/_ParticipantTile.js +1 -1
  37. package/dist/apps/meet/_Whiteboard.d.ts +64 -0
  38. package/dist/apps/meet/_Whiteboard.js +312 -0
  39. package/dist/apps/meet/_icons.d.ts +12 -0
  40. package/dist/apps/meet/_icons.js +12 -0
  41. package/dist/apps/meet/_meetApi.d.ts +8 -7
  42. package/dist/apps/meet/_meetApi.js +2 -2
  43. package/dist/apps/shared/collab/CollabSession.d.ts +208 -0
  44. package/dist/apps/shared/collab/CollabSession.js +559 -0
  45. package/dist/apps/shared/collab/WhiteboardModel.d.ts +54 -0
  46. package/dist/apps/shared/collab/WhiteboardModel.js +197 -0
  47. package/dist/apps/shared/collab/chatMarkdown.d.ts +65 -0
  48. package/dist/apps/shared/collab/chatMarkdown.js +311 -0
  49. package/dist/apps/shared/collab/color.d.ts +18 -0
  50. package/dist/apps/shared/collab/color.js +59 -0
  51. package/dist/apps/shared/collab/protocol.d.ts +157 -0
  52. package/dist/apps/shared/collab/protocol.js +222 -0
  53. package/dist/apps/shared/collab/whiteboardRender.d.ts +31 -0
  54. package/dist/apps/shared/collab/whiteboardRender.js +268 -0
  55. package/dist/apps/shared/relay/RelayClient.js +2 -1
  56. package/dist/apps/shared/relay/RelayTransport.d.ts +2 -1
  57. package/dist/apps/shared/relay/RelayTransport.js +2 -1
  58. package/dist/apps/shared/relay/relayEnv.d.ts +1 -1
  59. package/dist/apps/shared/signaling/SignalingClient.d.ts +95 -0
  60. package/dist/apps/shared/signaling/SignalingClient.js +278 -0
  61. package/dist/apps/shared/webrtc/MeshConnectionManager.d.ts +7 -7
  62. package/dist/apps/shared/webrtc/MeshConnectionManager.js +8 -8
  63. package/dist/apps/shared/webrtc/types.d.ts +38 -24
  64. package/dist/apps/shared/webrtc/types.js +3 -4
  65. package/dist/lib/routes/BaseVideoMeetingRoute.js +171 -69
  66. package/dist/lib/routes/BaseVideoMeetingRoute.js.map +1 -1
  67. package/dist/lib/util/SignalingHub.js +339 -0
  68. package/dist/lib/util/SignalingHub.js.map +1 -0
  69. package/dist/types/routes/BaseVideoMeetingRoute.d.ts +68 -46
  70. package/dist/types/util/SignalingHub.d.ts +112 -0
  71. package/package.json +208 -208
  72. package/apps/shared/push/GuestSignalingClient.ts +0 -359
  73. package/dist/apps/shared/push/GuestSignalingClient.d.ts +0 -98
  74. package/dist/apps/shared/push/GuestSignalingClient.js +0 -312
@@ -1,312 +0,0 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz. All rights reserved.
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- /**
6
- * A standalone `SignalingChannel` (`../webrtc/types.js`) over `@rapidrest/service-core`'s `/push` WebSocket -
7
- * built from scratch rather than reusing `@rapidmx/web-client`'s `PushClient`/`getPushClient()` (`lib/mail/pushClient.js`).
8
- *
9
- * ## Why not `PushClient`
10
- *
11
- * `PushClient` was investigated first, as this plugin's Phase 2 `.claude/NOTES.md` entry required. Its `connect()`
12
- * opens a bare `new WebSocket(url)` and relies entirely on the browser automatically attaching this deployment's
13
- * `jwt` `HttpOnly` cookie to the upgrade request - it never sends a token of its own (see its own doc comment:
14
- * "a script can't read that cookie, so there is no token to send"). That fits the webmail tab's own authenticated
15
- * session perfectly, but this plugin's anonymous guest has no such cookie at all - only the bearer `token`
16
- * `BaseVideoMeetingRoute.join()` minted, handed to this client explicitly. `PushClient`'s API has no parameter
17
- * anywhere to supply that token, and its shared-singleton design (`getPushClient()`, one client per tab) is itself
18
- * the wrong shape here too: a guest's signaling identity must never be confused with (or silently share a socket
19
- * with) whatever webmail session, if any, happens to already be open in the same browser. So this module
20
- * implements the same wire protocol independently instead - see `../../.claude/NOTES.md` and
21
- * `BasePushRoute`/`JWTStrategy` (`@rapidrest/service-core`) for the protocol this matches:
22
- *
23
- * - `wss://<origin>/push` - authenticated at the WebSocket upgrade. A real browser `WebSocket` cannot attach a
24
- * custom `Authorization` header to that upgrade request at all (a WHATWG API limitation, not something any
25
- * client-side code can work around) - `JWTStrategy.getAuthToken()`'s only other source is the same `jwt` cookie
26
- * `PushClient` already relies on. When `token` is supplied (the common anonymous/guest case -
27
- * `VideoMeetingJoinResult.authenticated` is `false`), this client sets that cookie itself, to `token`, immediately
28
- * before connecting (`applyAuthCookie()`). When `token` is omitted (the caller already authenticated as a real
29
- * RapidMX identity in `join()` - `authenticated: true` - see the FIXED LIMITATION below), this client writes no
30
- * cookie at all and simply lets the browser attach whatever real `jwt` session cookie already exists for this
31
- * origin, exactly as `PushClient` does for an ordinary webmail session.
32
- * - Right after connecting, the server sends `{ id: 0, type: "SUBSCRIBED", data: [...] }` (channels the socket
33
- * already holds - the caller's own uid channel, never this meeting's). `connect()` then explicitly `SUBSCRIBE`s to
34
- * the one meeting channel and waits for the matching `{ id, type: "SUBSCRIBED", data }` reply, resolving once
35
- * `channel` is actually in that reply's `data` (rejecting otherwise - an unauthorized/expired token, or the
36
- * meeting's ACL grant somehow missing).
37
- * - Publishing (`send()`) is **not** a WebSocket message at all - `BasePushRoute.send()` is `POST /push/:id`, a
38
- * perfectly ordinary authenticated HTTP endpoint. When `token` is supplied it's sent there as a normal
39
- * `Authorization: Bearer` header (no browser limitation applies to a plain `fetch()`); when it's omitted, the
40
- * request carries no `Authorization` header at all and instead relies on `fetch()`'s own default same-origin
41
- * credentials mode, which already attaches the browser's real `jwt` cookie to a same-origin request with zero
42
- * extra code - `JWTStrategy.getAuthToken()`'s cookie fallback authenticates it exactly as it would any other
43
- * same-origin authenticated call. Either way `send()` also attaches the CSRF double-submit header
44
- * (`withCsrfHeader()`) - required server-side only for the cookie case, but harmless to send for the bearer one
45
- * too - see `send()`'s own doc comment. Fire-and-forget either way, matching every other push publish in this
46
- * codebase - a failed send is logged, never thrown, since a caller (`MeshConnectionManager`) has no meaningful
47
- * per-message retry of its own.
48
- * - An incoming meeting message arrives wrapped `{ type: "MESSAGE", channel, data: <the posted body> }` - this
49
- * client unwraps it and delivers `data` (expected to be a `SignalMessage`) to `onMessage()` listeners.
50
- *
51
- * ## FIXED: the browser-session-collision limitation this module used to carry
52
- *
53
- * Phase 2 originally always wrote the guest `token` as the `jwt` cookie before connecting, unconditionally. If the
54
- * visiting browser already held a *real*, `HttpOnly` `jwt` session cookie for this exact origin (a logged-in
55
- * RapidMX user, e.g. with webmail open), that write was silently blocked by the browser's own "script cannot
56
- * override an `HttpOnly` cookie of the same name" protection - the WebSocket then authenticated as that real
57
- * session instead of the intended guest identity, and since the meeting's `AccessControlList` grant only named the
58
- * guest uid, the real session's `SUBSCRIBE` was simply refused. Net effect: a logged-in user could never actually
59
- * join a call at all (safe - never a cross-identity leak - but broken).
60
- *
61
- * The fix is in `BaseVideoMeetingRoute.join()` (backend): when the caller already presents a valid session for a
62
- * real RapidMX identity, it grants *that* identity's own uid on the meeting's channel directly and returns
63
- * `authenticated: true` with no guest `token` at all - see `VideoMeetingJoinResult`'s doc comment. This module's
64
- * own half of the fix is simply to stop writing a cookie (and stop sending a bearer header) whenever `token` is
65
- * absent, as described above - the browser's real cookie was always going to win that write anyway, so the
66
- * correct behavior is to not fight it and let it authenticate normally instead.
67
- */
68
- import { apiOrigin, withCsrfHeader } from "@rapidmx/web-client/lib/util/api.js";
69
- import { pushUrl } from "@rapidmx/web-client/lib/mail/pushClient.js";
70
- const SOCKET_OPEN = 1;
71
- function defaultSocketFactory() {
72
- return typeof WebSocket === "undefined" ? undefined : (url) => new WebSocket(url);
73
- }
74
- /** The cookie name `JWTStrategyOptions.cookieName` defaults to (and this deployment always uses - see
75
- * `BaseVideoMeetingRoute`'s guest JWT doc comment, signed with the same `auth` config every other token is). */
76
- const AUTH_COOKIE_NAME = "jwt";
77
- /** The first reconnect waits about this long, doubling on each further failure, matching `PushClient`'s own
78
- * constants (kept independent rather than imported, since this module deliberately doesn't depend on
79
- * `pushClient.ts` beyond its `pushUrl()` helper - see this module's doc comment on why it isn't `PushClient` itself). */
80
- export const SIGNALING_BACKOFF_BASE_MS = 1000;
81
- export const SIGNALING_BACKOFF_MAX_MS = 30000;
82
- export class GuestSignalingClient {
83
- constructor(options) {
84
- this.options = options;
85
- this.closed = false;
86
- this.nextRequestId = 1;
87
- this.attempt = 0;
88
- this.cookieApplied = false;
89
- this.listeners = new Set();
90
- }
91
- /** Connects, authenticates (see this module's doc comment) and subscribes to the meeting's own channel.
92
- * Resolves once genuinely ready to send/receive; rejects if the channel is refused (an invalid/expired token,
93
- * or - now fixed rather than merely possible, see the FIXED LIMITATION section above - a stale grant) or there
94
- * is nothing to connect with at all (no `WebSocket`, e.g. a non-HTTPS context in some browsers - the lobby's
95
- * own device-support checks already cover the more common case of no `mediaDevices`, but this is an
96
- * independent capability). Calling this a second time on an already-closed client rejects immediately. */
97
- connect() {
98
- if (this.closed) {
99
- return Promise.reject(new Error("This signaling client has already been closed."));
100
- }
101
- return new Promise((resolve, reject) => {
102
- this.pendingConnect = { resolve, reject };
103
- this.openSocket();
104
- });
105
- }
106
- /** Closes the socket, stops reconnecting and clears the guest auth cookie this client applied (see
107
- * `applyAuthCookie()`) - idempotent. */
108
- close() {
109
- if (this.closed) {
110
- return;
111
- }
112
- this.closed = true;
113
- clearTimeout(this.timer);
114
- this.timer = undefined;
115
- const socket = this.socket;
116
- this.socket = undefined;
117
- if (socket) {
118
- socket.onopen = socket.onmessage = socket.onclose = socket.onerror = null;
119
- try {
120
- socket.close(1000, "closing");
121
- }
122
- catch {
123
- // Already gone.
124
- }
125
- }
126
- this.clearAuthCookie();
127
- this.listeners.clear();
128
- this.pendingConnect?.reject(new Error("Closed before the signaling channel connected."));
129
- this.pendingConnect = undefined;
130
- }
131
- onMessage(handler) {
132
- this.listeners.add(handler);
133
- return () => this.listeners.delete(handler);
134
- }
135
- /** Publishes over `POST /push/:id`, with `token` as a bearer credential when present - see this module's doc
136
- * comment on why this, unlike subscribing, needs no cookie workaround at all, and on the omitted-`token` case
137
- * (an already-authenticated real caller), which relies on the browser's own real `jwt` cookie instead - and,
138
- * because that's a *cookie*-authenticated request, also needs the matching CSRF header
139
- * (`@rapidmx/web-client`'s `withCsrfHeader()`, the same double-submit echo `apiFetch()` applies to every other
140
- * mutating call this server answers): without it, `RouteUtils.checkCsrf()` refuses the request with 403
141
- * `"This request is missing a valid CSRF token."` before it ever reaches `BasePushRoute.send()`'s own ACL
142
- * check - every `hello`/offer/answer/ICE candidate/`bye` a participant on that path sends 403s and is
143
- * silently dropped (`send()` is fire-and-forget), which looks exactly like "stuck connecting, then no audio or
144
- * video" from everyone else's side. `withCsrfHeader()` is harmless to also add on the bearer-token (guest)
145
- * branch - the server only enforces the check for `req.auth.source === "cookie"` - so it's applied
146
- * unconditionally here rather than only for the cookie case.
147
- * Fire-and-forget: a failure is swallowed (there is nothing a signaling message's own sender can usefully do
148
- * about a dropped publish beyond what the mesh's own `hello` re-announcement/renegotiation already tolerates -
149
- * see `MeshConnectionManager`'s doc comment on fire-and-forget delivery). */
150
- send(message) {
151
- const fetchImpl = this.options.fetchImpl ?? (typeof fetch === "undefined" ? undefined : fetch);
152
- if (!fetchImpl) {
153
- return;
154
- }
155
- const origin = apiOrigin() || (typeof window !== "undefined" ? window.location.origin : "");
156
- const headers = { "Content-Type": "application/json" };
157
- if (this.options.token) {
158
- headers.Authorization = `Bearer ${this.options.token}`;
159
- }
160
- void fetchImpl(`${origin}/push/${encodeURIComponent(this.options.channel)}`, {
161
- method: "POST",
162
- headers: withCsrfHeader(headers, "POST"),
163
- body: JSON.stringify(message),
164
- // Only a `bye` needs to survive the page unloading - every other message is sent while the page (and
165
- // the rest of this call's signaling) is very much alive. `keepalive` is a real-world liability to set
166
- // unconditionally: some restrictive networks' inspecting proxies single out a `keepalive` fetch (it
167
- // tells the browser the request may outlive the page - the same signal `navigator.sendBeacon()` and an
168
- // `unload` handler's own fetch give off) for extra scrutiny or drop it outright, well short of Chrome's
169
- // documented 64 KiB-per-request/several-MB-total keepalive quota. A participant behind such a network
170
- // was seen stuck signaling forever - every `hello`/offer/answer/ICE candidate sent with `keepalive` and
171
- // silently going nowhere (`send()` is fire-and-forget) - while an identical join from an unrestricted
172
- // network connected normally.
173
- keepalive: message.kind === "bye",
174
- }).catch(() => {
175
- // Best-effort - see this method's doc comment.
176
- });
177
- }
178
- openSocket() {
179
- const url = (this.options.url ?? pushUrl)();
180
- const createSocket = this.options.createSocket ?? defaultSocketFactory();
181
- if (!url || !createSocket) {
182
- this.failConnect(new Error("This browser (or this page's connection) doesn't support the signaling channel."));
183
- return;
184
- }
185
- this.applyAuthCookie();
186
- let socket;
187
- try {
188
- socket = createSocket(url);
189
- }
190
- catch {
191
- this.scheduleReconnect();
192
- return;
193
- }
194
- this.socket = socket;
195
- socket.onmessage = (event) => this.handleFrame(event.data);
196
- socket.onclose = () => {
197
- // Guards against a stale socket's belated close event: `close()` always sets `this.socket = undefined`
198
- // synchronously (before this handler could possibly be detached asynchronously), so once this client
199
- // is closed, `this.socket` can never equal `socket` again and this always returns here first.
200
- if (this.socket !== socket) {
201
- return;
202
- }
203
- this.socket = undefined;
204
- this.scheduleReconnect();
205
- };
206
- socket.onerror = () => undefined;
207
- }
208
- scheduleReconnect() {
209
- const ceiling = Math.min(SIGNALING_BACKOFF_MAX_MS, SIGNALING_BACKOFF_BASE_MS * 2 ** Math.min(this.attempt, 10));
210
- const delay = ceiling / 2 + ((this.options.random ?? Math.random)() * ceiling) / 2;
211
- this.attempt += 1;
212
- this.timer = setTimeout(() => {
213
- this.timer = undefined;
214
- this.openSocket();
215
- }, delay);
216
- }
217
- handleFrame(raw) {
218
- if (typeof raw !== "string") {
219
- return;
220
- }
221
- let frame;
222
- try {
223
- const parsed = JSON.parse(raw);
224
- if (!parsed || typeof parsed !== "object") {
225
- return;
226
- }
227
- frame = parsed;
228
- }
229
- catch {
230
- return;
231
- }
232
- if (frame.type === "SUBSCRIBED") {
233
- this.handleSubscribed(frame);
234
- return;
235
- }
236
- if (frame.type === "MESSAGE" && frame.channel === this.options.channel) {
237
- const data = frame.data;
238
- if (data && typeof data === "object" && data.type === "video-meeting-signal") {
239
- this.emit(data);
240
- }
241
- }
242
- }
243
- handleSubscribed(frame) {
244
- const channels = Array.isArray(frame.data) ? frame.data.filter((c) => typeof c === "string") : [];
245
- if (frame.id === 0) {
246
- // The connect-time greeting: now actually subscribe to the one channel this client cares about.
247
- const id = this.nextRequestId++;
248
- this.send0({ id, type: "SUBSCRIBE", data: [this.options.channel] });
249
- return;
250
- }
251
- if (channels.includes(this.options.channel)) {
252
- this.attempt = 0;
253
- this.pendingConnect?.resolve();
254
- this.pendingConnect = undefined;
255
- }
256
- else if (this.pendingConnect) {
257
- this.failConnect(new Error("This meeting's signaling channel refused the subscription."));
258
- }
259
- }
260
- send0(message) {
261
- const socket = this.socket;
262
- if (!socket || socket.readyState !== SOCKET_OPEN) {
263
- return;
264
- }
265
- try {
266
- socket.send(JSON.stringify(message));
267
- }
268
- catch {
269
- // The close that follows a dead socket reconnects and re-subscribes.
270
- }
271
- }
272
- failConnect(err) {
273
- this.pendingConnect?.reject(err);
274
- this.pendingConnect = undefined;
275
- }
276
- emit(message) {
277
- for (const listener of [...this.listeners]) {
278
- listener(message);
279
- }
280
- }
281
- /** Sets the `jwt` cookie to `token` for this origin, so the browser attaches it (as `PushClient`'s own doc
282
- * comment describes for a real session) to the WebSocket upgrade this client is about to open - see this
283
- * module's doc comment for the full reasoning. A no-op when `token` is omitted (the caller already
284
- * authenticated as a real RapidMX identity - see the FIXED LIMITATION section above): there is then no guest
285
- * token to apply, and writing nothing leaves the browser's own real `jwt` session cookie, whatever it is,
286
- * untouched and free to authenticate the connection normally. */
287
- applyAuthCookie() {
288
- if (!this.options.token) {
289
- return;
290
- }
291
- const doc = this.options.documentRef ?? (typeof document === "undefined" ? undefined : document);
292
- if (!doc) {
293
- return;
294
- }
295
- this.cookieApplied = true;
296
- const secure = (doc.location ?? (typeof location === "undefined" ? undefined : location))?.protocol === "https:" ? "; Secure" : "";
297
- doc.cookie = `${AUTH_COOKIE_NAME}=${encodeURIComponent(this.options.token)}; path=/; SameSite=Lax${secure}`;
298
- }
299
- /** Expires the cookie `applyAuthCookie()` set - a no-op if that write was itself silently blocked, or never
300
- * attempted at all because `token` was omitted (see `applyAuthCookie()`'s own doc comment), either way leaving
301
- * whatever real session cookie was already there untouched. `cookieApplied` is only ever set once
302
- * `applyAuthCookie()` has already confirmed both a `token` to write and a `doc` to write it to, and neither
303
- * `this.options.token`, `this.options.documentRef` nor the real global `document` can change mid-call, so
304
- * resolving it here is guaranteed to succeed too - no second `!doc` guard needed. */
305
- clearAuthCookie() {
306
- if (!this.cookieApplied) {
307
- return;
308
- }
309
- const doc = this.options.documentRef ?? (typeof document === "undefined" ? undefined : document);
310
- doc.cookie = `${AUTH_COOKIE_NAME}=; path=/; Max-Age=0`;
311
- }
312
- }