@dokebi/websocket 0.1.3 → 0.3.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/gateway.js CHANGED
@@ -1,8 +1,67 @@
1
+ import { isUtf8 } from 'node:buffer';
1
2
  import { randomUUID } from 'node:crypto';
2
3
  import { setTimeout as delay, setImmediate as immediate } from 'node:timers/promises';
3
4
  import fastifyWebsocket from '@fastify/websocket';
4
5
  import { WebSocket } from 'ws';
6
+ import { errorCode, errorText, LogThrottle } from './log.js';
7
+ import { PLAIN_PROTOCOL, SEAL_PROTOCOL, } from './types.js';
5
8
  const TEXT = Object.freeze({ binary: false });
9
+ const BINARY = Object.freeze({ binary: true });
10
+ /** Close codes of sealed connections (D-0059 §4); the client does not reconnect after them. */
11
+ const BAD_FRAME = 4400;
12
+ const SEAL_REQUIRED = 4415;
13
+ /** Why a sealed frame was refused, as a bounded log group (codec reasons look like `replay`). */
14
+ const REASON = /^[a-z][a-z-]{0,31}$/;
15
+ /** Message types of `api.send`, as defineSocket handler types. */
16
+ const TYPE = /^[A-Za-z][A-Za-z0-9._:-]{0,63}$/;
17
+ /** Hub calls a `tenantPrefix` broadcast or kick runs at once. */
18
+ const FAN_OUT = 16;
19
+ const encoder = new TextEncoder();
20
+ function coded(message, code) {
21
+ return Object.assign(new Error(message), { code });
22
+ }
23
+ /** A host without its port, lower case; IPv6 literals keep their brackets (like the browser). */
24
+ function withoutPort(host) {
25
+ const raw = host.toLowerCase();
26
+ if (raw.startsWith('['))
27
+ return raw.slice(0, raw.indexOf(']') + 1);
28
+ const colon = raw.lastIndexOf(':');
29
+ return colon === -1 ? raw : raw.slice(0, colon);
30
+ }
31
+ /** A filter string: 1 to 256 bytes (the hub's identifier limit). */
32
+ function filterText(value) {
33
+ return typeof value === 'string' && value.length > 0 && Buffer.byteLength(value) <= 256;
34
+ }
35
+ /** Checks a server-side filter (D-0061): exactly one of tenant and tenantPrefix. */
36
+ function checkFilter(filter) {
37
+ const value = filter;
38
+ if (!value ||
39
+ typeof value !== 'object' ||
40
+ (value.tenant === undefined) === (value.tenantPrefix === undefined) ||
41
+ (value.tenant !== undefined && !filterText(value.tenant)) ||
42
+ (value.tenantPrefix !== undefined && !filterText(value.tenantPrefix)) ||
43
+ (value.room !== undefined && !filterText(value.room)) ||
44
+ (value.member !== undefined && !filterText(value.member)))
45
+ throw coded('A websocket filter is { tenant } or { tenantPrefix }, with room? and member?, strings of 1 to 256 bytes', 'INVALID_FILTER');
46
+ return value;
47
+ }
48
+ function connectionInfo(record) {
49
+ return Object.freeze({
50
+ tenant: record.tenant,
51
+ member: record.member,
52
+ connection: record.connection,
53
+ ownerEpoch: record.ownerEpoch,
54
+ ...(record.clientId === undefined ? {} : { clientId: record.clientId }),
55
+ rooms: record.rooms,
56
+ metadata: record.metadata,
57
+ });
58
+ }
59
+ function sealOf(route) {
60
+ const value = route.seal;
61
+ if (value !== undefined && typeof value !== 'boolean' && typeof value !== 'function')
62
+ throw new Error('Invalid websocket route');
63
+ return value;
64
+ }
6
65
  /** Allow-list entries must be exact http(s) origins such as `https://app.example.com`. */
7
66
  function parseOrigins(value) {
8
67
  if (value === undefined)
@@ -17,7 +76,9 @@ function parseOrigins(value) {
17
76
  try {
18
77
  url = typeof item === 'string' ? new URL(item) : undefined;
19
78
  }
20
- catch { }
79
+ catch (error) {
80
+ throw new Error('Invalid websocket route', { cause: error });
81
+ }
21
82
  if (!url || (url.protocol !== 'http:' && url.protocol !== 'https:') || url.origin !== item)
22
83
  throw new Error('Invalid websocket route');
23
84
  result.add(url.origin);
@@ -66,6 +127,7 @@ export class Gateway {
66
127
  web;
67
128
  hub;
68
129
  settings;
130
+ codecs;
69
131
  api;
70
132
  #routes = [];
71
133
  #clients = new Map();
@@ -76,20 +138,29 @@ export class Gateway {
76
138
  #sends = 0;
77
139
  #sendsIdle;
78
140
  #identities = new WeakMap();
141
+ /** `api.onPresence` listeners (D-0061). */
142
+ #listeners = new Set();
143
+ /** Upgrades decided as sealed in preValidation, read by handleProtocols and the wsHandler. */
144
+ #sealed = new WeakSet();
79
145
  #frozen = false;
80
146
  #closing = false;
81
147
  #loop;
82
148
  #timer;
83
- constructor(context, web, hub, settings) {
149
+ /** Failures on per-message and per-socket paths, logged once per interval with a count. */
150
+ #throttle;
151
+ constructor(context, web, hub, settings, codecs = undefined) {
84
152
  this.context = context;
85
153
  this.web = web;
86
154
  this.hub = hub;
87
155
  this.settings = settings;
156
+ this.codecs = codecs;
88
157
  const self = this;
158
+ this.#throttle = new LogThrottle(context.logger);
89
159
  this.api = Object.freeze({
90
160
  get connections() {
91
161
  return self.#clients.size;
92
162
  },
163
+ sealing: codecs !== undefined,
93
164
  route: (route) => {
94
165
  if (this.#frozen || context.app.state !== 'starting')
95
166
  throw new Error('Register websocket routes during setup');
@@ -98,8 +169,43 @@ export class Gateway {
98
169
  typeof route.message !== 'function')
99
170
  throw new Error('Invalid websocket route');
100
171
  const origins = parseOrigins(route.origins);
172
+ const seal = sealOf(route);
173
+ // Fail closed: a route that may seal must not quietly run in plain text.
174
+ if (seal && !codecs)
175
+ throw coded(`WebSocket route ${route.path} is sealed; register the dokebi-seal plugin`, 'SEAL_PROVIDER_MISSING');
101
176
  this.#routes.push({ route: { ...route }, origins });
102
177
  },
178
+ list: (filter) => this.#list(filter),
179
+ broadcast: (filter, data) => this.#broadcast(filter, data),
180
+ send: (filter, type, data) => {
181
+ if (typeof type !== 'string' || !TYPE.test(type))
182
+ return Promise.reject(coded('Message types match ' + TYPE.source, 'INVALID_TYPE'));
183
+ let text;
184
+ try {
185
+ text = JSON.stringify(data === undefined ? { type } : { type, data });
186
+ }
187
+ catch (error) {
188
+ return Promise.reject(Object.assign(new Error('Message data is not JSON', { cause: error }), {
189
+ code: 'INVALID_DATA',
190
+ }));
191
+ }
192
+ return this.#broadcast(filter, text);
193
+ },
194
+ kick: (filter, code, reason) => this.#kick(filter, code, reason),
195
+ onPresence: (listener) => {
196
+ if (typeof listener !== 'function')
197
+ throw new TypeError('Invalid presence listener');
198
+ this.#listeners.add(listener);
199
+ context.logger.log('debug', 'WebSocket presence listener added', {
200
+ listeners: this.#listeners.size,
201
+ });
202
+ return () => {
203
+ if (this.#listeners.delete(listener))
204
+ context.logger.log('debug', 'WebSocket presence listener removed', {
205
+ listeners: this.#listeners.size,
206
+ });
207
+ };
208
+ },
103
209
  });
104
210
  // Registered without encapsulation (skip-override) so @fastify/websocket's hooks run for
105
211
  // every upgrade, including ones that match no websocket route: its onResponse hook then
@@ -107,8 +213,22 @@ export class Gateway {
107
213
  const gateway = async (scope) => {
108
214
  this.#frozen = true;
109
215
  await scope.register(fastifyWebsocket, {
110
- options: { maxPayload: settings.maxPayload, perMessageDeflate: false },
111
- errorHandler: (_error, socket) => {
216
+ options: {
217
+ maxPayload: settings.maxPayload,
218
+ perMessageDeflate: false,
219
+ // D-0059 §4: a client with a seal bridge offers `dokebi.seal` and `dokebi.plain`.
220
+ // A sealed upgrade selects `dokebi.seal` only (never plain; not offered: 4415 after the
221
+ // upgrade). A plain upgrade selects `dokebi.plain` when offered (browsers fail a handshake
222
+ // that selects none of the offered protocols), otherwise nothing, as for old clients.
223
+ handleProtocols: (protocols, request) => {
224
+ if (this.#sealed.has(request))
225
+ return protocols.has(SEAL_PROTOCOL) ? SEAL_PROTOCOL : false;
226
+ return protocols.has(PLAIN_PROTOCOL) ? PLAIN_PROTOCOL : false;
227
+ },
228
+ },
229
+ // A wsHandler that threw: the client gets 1011, the cause goes to the log (D-0047).
230
+ errorHandler: (error, socket) => {
231
+ this.#throttle.report('error', 'WebSocket handler failed', 'SOCKET_FAILURE', error);
112
232
  socket.close(1011, 'Socket failure');
113
233
  },
114
234
  });
@@ -140,6 +260,20 @@ export class Gateway {
140
260
  if (!identity)
141
261
  return reply.code(401).send({ error: 'UNAUTHORIZED' });
142
262
  this.#identities.set(request, identity);
263
+ const seal = route.seal;
264
+ if (seal === undefined || seal === false)
265
+ return;
266
+ const sealed = seal === true || seal(request) === true;
267
+ if (sealed)
268
+ this.#sealed.add(request.raw);
269
+ if (this.context.logger.isEnabled('debug'))
270
+ this.context.logger.log('debug', 'WebSocket seal decision', {
271
+ path: route.path,
272
+ sealed,
273
+ offered: String(request.headers['sec-websocket-protocol'] ?? '')
274
+ .split(',')
275
+ .some((value) => value.trim() === SEAL_PROTOCOL),
276
+ });
143
277
  },
144
278
  handler: (_request, reply) => reply.code(426).send({ error: 'WEBSOCKET_REQUIRED' }),
145
279
  wsHandler: (socket, request) => this.#attach(socket, request, route),
@@ -154,10 +288,31 @@ export class Gateway {
154
288
  void task.then(() => this.#tasks.delete(task), () => this.#tasks.delete(task));
155
289
  return task;
156
290
  }
157
- #send(socket, data) {
158
- const bytes = typeof data === 'string' ? Buffer.byteLength(data) : data.byteLength;
291
+ #send(value, data) {
292
+ const socket = value.socket;
159
293
  if (socket.readyState !== WebSocket.OPEN)
160
294
  return Promise.reject(Object.assign(new Error('Socket closed'), { code: 'SOCKET_CLOSED' }));
295
+ if (value.codec) {
296
+ // Sealed connections carry text only: UTF-8 bytes are sent as text, other bytes refused.
297
+ let text;
298
+ if (typeof data === 'string')
299
+ text = data;
300
+ else {
301
+ const buffer = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
302
+ if (!isUtf8(buffer))
303
+ return Promise.reject(coded('Binary data cannot be sent on a sealed socket', 'SEAL_BINARY_UNSUPPORTED'));
304
+ text = buffer.toString();
305
+ }
306
+ try {
307
+ data = value.codec.encode(text);
308
+ }
309
+ catch (error) {
310
+ return Promise.reject(Object.assign(new Error('Sealing a frame failed', { cause: error }), {
311
+ code: 'SEAL_ENCODE_FAILED',
312
+ }));
313
+ }
314
+ }
315
+ const bytes = typeof data === 'string' ? Buffer.byteLength(data) : data.byteLength;
161
316
  if (socket.bufferedAmount + bytes > this.settings.maxBufferedBytes) {
162
317
  this.#slow(socket);
163
318
  return Promise.reject(Object.assign(new Error('Socket backpressure'), { code: 'SLOW_CONSUMER' }));
@@ -175,17 +330,20 @@ export class Gateway {
175
330
  socket.close(1013, 'Slow consumer');
176
331
  }
177
332
  /** Queues one broadcast frame without waiting; a slow socket never delays the others. */
178
- #deliver(socket, frame) {
333
+ #deliver(socket, frame, kind) {
179
334
  if (socket.readyState !== WebSocket.OPEN)
180
335
  return;
181
- if (socket.bufferedAmount + frame.byteLength > this.settings.maxBufferedBytes) {
336
+ const bytes = typeof frame === 'string' ? Buffer.byteLength(frame) : frame.byteLength;
337
+ if (socket.bufferedAmount + bytes > this.settings.maxBufferedBytes) {
182
338
  this.#slow(socket);
183
339
  return;
184
340
  }
185
341
  this.#sends++;
186
- socket.send(frame, TEXT, (error) => {
187
- if (error)
342
+ socket.send(frame, kind, (error) => {
343
+ if (error) {
344
+ this.#throttle.report('error', 'WebSocket broadcast write failed; socket terminated', 'BROADCAST_WRITE_FAILED', error);
188
345
  socket.terminate();
346
+ }
189
347
  if (--this.#sends === 0 && this.#sendsIdle) {
190
348
  this.#sendsIdle.resolve();
191
349
  this.#sendsIdle = undefined;
@@ -221,14 +379,52 @@ export class Gateway {
221
379
  return;
222
380
  }
223
381
  }
382
+ const sealed = this.#sealed.has(request.raw);
383
+ // A sealed host never talks plain text: a client that cannot seal is told so (no reconnect).
384
+ if (sealed && socket.protocol !== SEAL_PROTOCOL) {
385
+ this.context.logger.log('debug', 'WebSocket seal required; closed', {
386
+ path: route.path,
387
+ code: 'SEAL_REQUIRED',
388
+ });
389
+ socket.close(SEAL_REQUIRED, 'Seal required');
390
+ return;
391
+ }
224
392
  const identity = this.#identities.get(request);
225
393
  if (!identity || this.#closing || this.#clients.size >= this.settings.maxConnections) {
226
394
  socket.close(1013, 'Unavailable');
227
395
  return;
228
396
  }
229
397
  const id = randomUUID();
398
+ let codec;
399
+ if (sealed) {
400
+ let hello;
401
+ try {
402
+ codec = this.codecs.forConnection({
403
+ host: withoutPort(this.web.requestHost(request)),
404
+ path: request.url.split('?', 1)[0] ?? route.path,
405
+ userAgent: String(request.headers['user-agent'] ?? ''),
406
+ nonces: this.hub.nonces(),
407
+ driftMs: this.settings.sealDriftMs,
408
+ });
409
+ hello = codec.hello();
410
+ }
411
+ catch (error) {
412
+ this.#throttle.report('error', 'WebSocket seal codec failed; closed', 'SEAL_CODEC_FAILED', error, { connection: id });
413
+ socket.close(1011, 'Socket failure');
414
+ return;
415
+ }
416
+ // The first frame of a sealed connection: the client takes its clock offset from it and
417
+ // reports 'open' only after it (D-0059 §4).
418
+ socket.send(hello, TEXT, (error) => {
419
+ if (error)
420
+ this.#throttle.report('warn', 'WebSocket seal hello not sent', 'SEAL_HELLO_FAILED', error, { connection: id });
421
+ });
422
+ if (this.context.logger.isEnabled('debug'))
423
+ this.context.logger.log('debug', 'WebSocket seal hello sent', { connection: id });
424
+ }
230
425
  const value = {
231
426
  socket,
427
+ codec,
232
428
  route,
233
429
  session: undefined,
234
430
  tail: Promise.resolve(),
@@ -247,6 +443,7 @@ export class Gateway {
247
443
  member: identity.member,
248
444
  clientId: identity.clientId ?? null,
249
445
  path: route.path,
446
+ sealed: codec !== undefined,
250
447
  remote: `${request.socket.remoteAddress ?? '?'}:${request.socket.remotePort ?? '?'}`,
251
448
  userAgent: String(request.headers['user-agent'] ?? '').slice(0, 160),
252
449
  // Browser context of the upgrade: a prerendered or prefetched page says so here.
@@ -257,33 +454,62 @@ export class Gateway {
257
454
  const session = Object.freeze({
258
455
  id,
259
456
  identity,
260
- send: (data) => this.#send(socket, data),
457
+ send: (data) => this.#send(value, data),
261
458
  broadcast: async (data, target) => {
262
459
  return this.hub.broadcast({ ...target, tenant: identity.tenant }, data);
263
460
  },
264
461
  join: async (room) => {
265
- if (!route.authorizeRoom || !(await route.authorizeRoom(identity, room)))
462
+ if (!route.authorizeRoom || !(await route.authorizeRoom(identity, room))) {
463
+ this.context.logger.log('debug', 'WebSocket room refused', { connection: id });
266
464
  throw new Error('Room access denied');
465
+ }
267
466
  await value.registration?.join(room);
467
+ this.context.logger.log('debug', 'WebSocket room joined', {
468
+ connection: id,
469
+ room: room.slice(0, 64),
470
+ });
268
471
  },
269
472
  leave: async (room) => {
270
473
  await value.registration?.leave(room);
474
+ this.context.logger.log('debug', 'WebSocket room left', {
475
+ connection: id,
476
+ room: room.slice(0, 64),
477
+ });
271
478
  },
272
479
  close: (code = 1000, reason = '') => {
273
480
  socket.close(code, reason);
274
481
  },
482
+ // What defineSocket answered with an error frame (D-0047): the client sees the code only,
483
+ // the operator gets the cause. Handler types are a bounded set (the route's handlers), so
484
+ // repeats are counted per type and reply code; client errors are counted per code only.
485
+ reportError: (error, details) => {
486
+ if (details.source === 'handler') {
487
+ this.#throttle.report('error', 'WebSocket handler failed.', 'SOCKET_HANDLER_FAILED', error === undefined ? 'NonError: undefined' : error, // `throw undefined` still shows
488
+ { type: details.type ?? null, reply: details.code, connection: id }, `${details.type ?? ''}\u0000${details.code}`);
489
+ return;
490
+ }
491
+ this.#throttle.report('debug', 'WebSocket client message refused', details.code, error, {
492
+ type: details.type?.slice(0, 64) ?? null,
493
+ connection: id,
494
+ });
495
+ },
275
496
  });
276
497
  value.session = session;
277
- const registration = { ...identity, connection: id };
498
+ // `namespace` belongs to @dokebi/app (D-0061); the hub registers known fields only.
499
+ const { namespace: _namespace, ...fields } = identity;
500
+ const registration = { ...fields, connection: id };
278
501
  value.tail = this.#watch(this.hub.register(registration).then((handle) => {
279
502
  value.registration = handle;
503
+ if (this.context.logger.isEnabled('debug'))
504
+ this.context.logger.log('debug', 'WebSocket registered with hub', {
505
+ connection: id,
506
+ ms: Math.round(performance.now() - opened),
507
+ });
280
508
  }));
281
509
  const registered = value.tail;
282
510
  void value.tail.catch((error) => {
283
- this.context.logger.log('warn', 'WebSocket registration failed', {
284
- connection: id,
285
- code: error instanceof Error ? error.message : 'UNKNOWN',
286
- });
511
+ // Converted to a close code (4409 or 1013) for the client; logged here for the operator.
512
+ this.#throttle.report('warn', 'WebSocket registration failed', errorCode(error, 'REGISTRATION_FAILED'), error, { connection: id });
287
513
  value.failed = true;
288
514
  // The same (tenant, member, clientId) is already connected (D-0025): a client error, not
289
515
  // a hub outage. A client that retries with the same clientId is refused again.
@@ -297,72 +523,54 @@ export class Gateway {
297
523
  socket.on('pong', () => {
298
524
  value.alive = true;
299
525
  });
300
- socket.on('error', () => { });
526
+ // ws emits 'close' after 'error'; the close cleans up, the error is reported here.
527
+ socket.on('error', (error) => {
528
+ this.#throttle.report('error', 'WebSocket socket error', 'SOCKET_ERROR', error, {
529
+ reason: errorCode(error, 'UNKNOWN'),
530
+ });
531
+ });
301
532
  // Attach synchronously, before awaiting Redis/TCP registration, so early frames are queued.
302
- socket.on('message', (raw) => {
533
+ socket.on('message', (raw, isBinary) => {
303
534
  value.seen = performance.now();
304
- // ACKs bypass the business queue (including drain), which may itself await a broadcast.
305
535
  const bytes = Array.isArray(raw)
306
536
  ? Buffer.concat(raw)
307
537
  : Buffer.from(raw instanceof ArrayBuffer ? new Uint8Array(raw) : raw);
308
- if (bytes[0] === 123) {
309
- let control;
310
- try {
311
- control = JSON.parse(bytes.toString());
312
- }
313
- catch {
314
- /* The route owns non-control payload parsing. */
315
- }
316
- // The page is alive (idleTimeoutMs): nothing else to do.
317
- if (control?.$dokebi === 'ping-v1')
538
+ if (codec) {
539
+ // Sealed: the frame is opened in this connection's order chain, then handled like a
540
+ // plain one (control frames, then the route). Nothing is read before it opens.
541
+ if (this.#closing)
318
542
  return;
319
- if (control?.$dokebi === 'ack-v1') {
543
+ this.#enqueue(value, id, async () => {
544
+ if (value.failed)
545
+ return; // refused earlier (4400) or not registered: take nothing
546
+ let text;
320
547
  try {
321
- if (typeof control.id !== 'string')
322
- throw new Error('Invalid ACK');
323
- this.hub.acknowledge(control.id, [id]);
548
+ if (isBinary)
549
+ throw Object.assign(new Error('Binary frame'), { reason: 'binary' });
550
+ text = await codec.decode(bytes.toString());
324
551
  }
325
- catch {
326
- socket.close(1008, 'Invalid ACK');
552
+ catch (error) {
553
+ this.#refuse(value, id, error);
554
+ return;
327
555
  }
328
- return;
329
- }
556
+ if (text.charCodeAt(0) === 123 && this.#control(text, id))
557
+ return;
558
+ if (value.failed || !value.registration)
559
+ return;
560
+ return route.message(Buffer.from(text), session);
561
+ });
562
+ return;
330
563
  }
331
- if (this.#closing)
564
+ // Control frames are answered here, outside the route's message queue.
565
+ if (bytes[0] === 123 && this.#control(bytes.toString(), id))
332
566
  return;
333
- if (++value.pending > this.settings.maxPendingMessages) {
334
- value.pending--;
335
- socket.close(1013, 'Too many messages');
567
+ if (this.#closing)
336
568
  return;
337
- }
338
- const data = Array.isArray(raw)
339
- ? Buffer.concat(raw)
340
- : raw instanceof ArrayBuffer
341
- ? Buffer.from(raw)
342
- : Buffer.from(raw);
343
- const work = value.tail.then(() => {
569
+ this.#enqueue(value, id, () => {
344
570
  if (value.failed || !value.registration)
345
571
  return;
346
- return route.message(data, session);
347
- });
348
- value.tail = work
349
- .then(() => { }, (error) => {
350
- value.failed = true;
351
- // A reply that cannot be written because the socket is closing (slow consumer, the
352
- // client left) is not a handler failure; it was logged where the socket closed.
353
- const code = error?.code;
354
- if (code === 'SLOW_CONSUMER' || code === 'SOCKET_CLOSED')
355
- return;
356
- this.context.logger.log('error', 'WebSocket message handler failed', {
357
- connection: id,
358
- ...(typeof code === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(code) ? { code } : {}),
359
- });
360
- socket.close(1011, 'Message failed');
361
- })
362
- .finally(() => {
363
- value.pending--;
572
+ return route.message(bytes, session);
364
573
  });
365
- this.#watch(value.tail);
366
574
  });
367
575
  socket.once('close', (code, reason) => {
368
576
  this.context.logger.log('debug', 'WebSocket closed', {
@@ -380,13 +588,84 @@ export class Gateway {
380
588
  .finally(() => {
381
589
  this.#clients.delete(id);
382
590
  });
383
- void this.#watch(cleanup).catch(() => {
384
- this.context.logger.log('warn', 'WebSocket presence cleanup deferred to hub recovery', {
385
- connection: id,
386
- });
591
+ void this.#watch(cleanup).catch((error) => {
592
+ // The hub drops this connection when it recovers (owner snapshot or expiry).
593
+ this.#throttle.report('error', 'WebSocket presence cleanup deferred to hub recovery', 'PRESENCE_CLEANUP_DEFERRED', error, { connection: id });
387
594
  });
388
595
  });
389
596
  }
597
+ /**
598
+ * Answers a `$dokebi` control frame (ping-v1, ack-v1); false when `text` is not one, so the
599
+ * route gets it.
600
+ */
601
+ #control(text, id) {
602
+ let control;
603
+ try {
604
+ control = JSON.parse(text);
605
+ }
606
+ catch {
607
+ /* The route owns non-control payload parsing. */
608
+ }
609
+ // The page is alive (idleTimeoutMs): nothing else to do.
610
+ if (control?.$dokebi === 'ping-v1')
611
+ return true;
612
+ // A page loaded before D-0057 still acknowledges broadcasts: nothing to do, and no
613
+ // reason to close it. Counted at debug until it reloads.
614
+ if (control?.$dokebi === 'ack-v1') {
615
+ this.#throttle.report('debug', 'WebSocket ACK from an old client ignored', 'ACK_IGNORED', undefined, { connection: id });
616
+ return true;
617
+ }
618
+ return false;
619
+ }
620
+ /**
621
+ * A sealed frame that did not open (plain text, binary, drift, replay, bad seal): 4400 and the
622
+ * connection takes nothing more. The reason goes to the log only, never to the client: telling
623
+ * `replay` from `drift` would let an attacker probe the defence (D-0059 §4).
624
+ */
625
+ #refuse(value, id, error) {
626
+ const given = error?.reason;
627
+ const reason = typeof given === 'string' && REASON.test(given) ? given : 'unknown';
628
+ value.failed = true;
629
+ this.#throttle.report('warn', 'WebSocket sealed frame refused; closed', 'SEAL_FRAME_REFUSED', error, { connection: id, reason }, reason);
630
+ value.socket.close(BAD_FRAME, 'Bad frame');
631
+ }
632
+ /** Runs `work` after this connection's earlier messages and presence calls, in order. */
633
+ #enqueue(value, id, work) {
634
+ const socket = value.socket;
635
+ if (++value.pending > this.settings.maxPendingMessages) {
636
+ value.pending--;
637
+ // The server closing a client is worth seeing (like SLOW_CONSUMER): usually a handler
638
+ // that waits long (a slow database call) while the client keeps sending.
639
+ this.#throttle.report('warn', 'WebSocket message queue full; closed', 'TOO_MANY_MESSAGES', undefined, {
640
+ connection: id,
641
+ pending: value.pending,
642
+ });
643
+ socket.close(1013, 'Too many messages');
644
+ return;
645
+ }
646
+ value.tail = value.tail
647
+ .then(work)
648
+ .then(() => { }, (error) => {
649
+ value.failed = true;
650
+ // A reply that cannot be written because the socket is closing is not a handler
651
+ // failure. A slow consumer was logged where it was closed; a client that left is
652
+ // counted here as warn, once per interval: the client is gone, nothing on this
653
+ // server failed (OPS-002 soak: thousands of these hid the real cause at error).
654
+ const code = error?.code;
655
+ if (code === 'SLOW_CONSUMER')
656
+ return;
657
+ if (code === 'SOCKET_CLOSED') {
658
+ this.#throttle.report('warn', 'WebSocket reply dropped; socket closed', 'REPLY_DROPPED', error, { connection: id });
659
+ return;
660
+ }
661
+ this.#throttle.report('error', 'WebSocket message handler failed', errorCode(error, 'HANDLER_FAILED'), error, { connection: id });
662
+ socket.close(1011, 'Message failed');
663
+ })
664
+ .finally(() => {
665
+ value.pending--;
666
+ });
667
+ this.#watch(value.tail);
668
+ }
390
669
  /** Turns one server's new connection list into joins and leaves per tenant, for local routes. */
391
670
  #presence(owner, connections) {
392
671
  const before = this.#owners.get(owner) ?? [];
@@ -417,6 +696,22 @@ export class Gateway {
417
696
  of(record.tenant).left.push(peer(record));
418
697
  if (!changes.size)
419
698
  return;
699
+ if (this.context.logger.isEnabled('debug')) {
700
+ let joined = 0, left = 0;
701
+ for (const change of changes.values()) {
702
+ joined += change.joined.length;
703
+ left += change.left.length;
704
+ }
705
+ this.context.logger.log('debug', 'WebSocket presence changed', {
706
+ owner,
707
+ tenants: changes.size,
708
+ joined,
709
+ left,
710
+ listeners: this.#listeners.size,
711
+ });
712
+ }
713
+ if (this.#listeners.size)
714
+ this.#notify(changes);
420
715
  for (const value of this.#clients.values()) {
421
716
  const { route, session } = value;
422
717
  const change = session && changes.get(session.identity.tenant);
@@ -436,14 +731,155 @@ export class Gateway {
436
731
  // In order with this connection's messages, like a message handler.
437
732
  value.tail = value.tail
438
733
  .then(() => route.presence?.(presence, session))
439
- .then(() => { }, () => {
440
- this.context.logger.log('warn', 'WebSocket presence handler failed', {
441
- connection: session.id,
442
- });
734
+ .then(() => { }, (error) => {
735
+ const code = error?.code;
736
+ if (code === 'SLOW_CONSUMER')
737
+ return; // Logged where the socket was closed.
738
+ this.#throttle.report(code === 'SOCKET_CLOSED' ? 'warn' : 'error', code === 'SOCKET_CLOSED'
739
+ ? 'WebSocket presence dropped; socket closed'
740
+ : 'WebSocket presence handler failed', code === 'SOCKET_CLOSED' ? 'REPLY_DROPPED' : 'PRESENCE_HANDLER_FAILED', error, { connection: session.id });
443
741
  });
444
742
  this.#watch(value.tail);
445
743
  }
446
744
  }
745
+ /** Records of the presence mirror (every server, as the hub reported them) that match. */
746
+ #mirror(filter) {
747
+ const result = [];
748
+ for (const records of this.#owners.values())
749
+ for (const record of records)
750
+ if ((filter.tenant === undefined
751
+ ? record.tenant.startsWith(filter.tenantPrefix)
752
+ : record.tenant === filter.tenant) &&
753
+ (filter.room === undefined || record.rooms.includes(filter.room)) &&
754
+ (filter.member === undefined || record.member === filter.member))
755
+ result.push(record);
756
+ return result;
757
+ }
758
+ /** One hub query per tenant the filter names: its own, or each known one under the prefix. */
759
+ #queries(filter) {
760
+ const narrow = {
761
+ ...(filter.room === undefined ? {} : { room: filter.room }),
762
+ ...(filter.member === undefined ? {} : { member: filter.member }),
763
+ };
764
+ if (filter.tenant !== undefined)
765
+ return [{ tenant: filter.tenant, ...narrow }];
766
+ const tenants = new Set(this.#mirror(filter).map((record) => record.tenant));
767
+ return [...tenants].map((tenant) => ({ tenant, ...narrow }));
768
+ }
769
+ /** Runs `call` for every query, FAN_OUT at a time; the results and failures in query order. */
770
+ async #fanOut(queries, call) {
771
+ const results = [];
772
+ const failures = [];
773
+ for (let index = 0; index < queries.length; index += FAN_OUT) {
774
+ const settled = await Promise.allSettled(queries.slice(index, index + FAN_OUT).map((query) => call(query)));
775
+ for (const outcome of settled)
776
+ if (outcome.status === 'fulfilled')
777
+ results.push(outcome.value);
778
+ else
779
+ failures.push(outcome.reason);
780
+ }
781
+ return { results, failures };
782
+ }
783
+ async #list(input) {
784
+ const filter = checkFilter(input);
785
+ const started = performance.now();
786
+ let records;
787
+ if (filter.tenant !== undefined) {
788
+ const [query] = this.#queries(filter);
789
+ records = (await this.hub.list(query)).connections;
790
+ }
791
+ else
792
+ records = this.#mirror(filter);
793
+ if (this.context.logger.isEnabled('debug'))
794
+ this.context.logger.log('debug', 'WebSocket connections listed', {
795
+ source: filter.tenant === undefined ? 'presence' : 'hub',
796
+ tenant: filter.tenant?.slice(0, 64) ?? null,
797
+ tenantPrefix: filter.tenantPrefix?.slice(0, 64) ?? null,
798
+ connections: records.length,
799
+ ms: Math.round((performance.now() - started) * 1000) / 1000,
800
+ });
801
+ return Object.freeze(records.map(connectionInfo));
802
+ }
803
+ async #broadcast(input, data) {
804
+ const filter = checkFilter(input);
805
+ if (typeof data !== 'string' && !(data instanceof Uint8Array))
806
+ throw coded('Broadcast data is a string or a Uint8Array', 'INVALID_DATA');
807
+ const bytes = typeof data === 'string' ? encoder.encode(data) : data;
808
+ const queries = this.#queries(filter);
809
+ // A single tenant keeps the hub's own error (RECOVERING, BUSY, OUTCOME_UNKNOWN).
810
+ if (filter.tenant !== undefined) {
811
+ const receipt = await this.hub.broadcast(queries[0], bytes);
812
+ if (this.context.logger.isEnabled('debug'))
813
+ this.context.logger.log('debug', 'WebSocket server broadcast sent', {
814
+ id: receipt.id,
815
+ tenants: 1,
816
+ recipients: receipt.recipients.length,
817
+ bytes: bytes.byteLength,
818
+ });
819
+ return Object.freeze({ ids: Object.freeze([receipt.id]), recipients: receipt.recipients });
820
+ }
821
+ const { results, failures } = await this.#fanOut(queries, (query) => this.hub.broadcast(query, bytes));
822
+ const receipt = Object.freeze({
823
+ ids: Object.freeze(results.map((result) => result.id)),
824
+ recipients: Object.freeze(results.flatMap((result) => result.recipients)),
825
+ });
826
+ if (this.context.logger.isEnabled('debug'))
827
+ this.context.logger.log('debug', 'WebSocket server broadcast sent', {
828
+ tenantPrefix: filter.tenantPrefix?.slice(0, 64) ?? null,
829
+ tenants: queries.length,
830
+ failed: failures.length,
831
+ recipients: receipt.recipients.length,
832
+ bytes: bytes.byteLength,
833
+ });
834
+ if (failures.length)
835
+ throw Object.assign(new Error(`WebSocket broadcast failed for ${failures.length} of ${queries.length} tenants`, {
836
+ cause: new AggregateError(failures, 'Hub broadcast failures'),
837
+ }), { code: errorCode(failures[0], 'BROADCAST_FAILED'), receipt });
838
+ return receipt;
839
+ }
840
+ async #kick(input, code, reason) {
841
+ const filter = checkFilter(input);
842
+ const queries = this.#queries(filter);
843
+ const { results, failures } = await this.#fanOut(queries, (query) => this.hub.kick(query, code, reason));
844
+ const receipt = Object.freeze({
845
+ ids: Object.freeze(results.map((result) => result.id)),
846
+ });
847
+ this.context.logger.log('debug', 'WebSocket server kick sent', {
848
+ tenant: filter.tenant?.slice(0, 64) ?? null,
849
+ tenantPrefix: filter.tenantPrefix?.slice(0, 64) ?? null,
850
+ tenants: queries.length,
851
+ failed: failures.length,
852
+ code: code ?? null,
853
+ });
854
+ if (failures.length === 1 && queries.length === 1)
855
+ throw failures[0];
856
+ if (failures.length)
857
+ throw Object.assign(new Error(`WebSocket kick failed for ${failures.length} of ${queries.length} tenants`, {
858
+ cause: new AggregateError(failures, 'Hub kick failures'),
859
+ }), { code: errorCode(failures[0], 'KICK_FAILED'), receipt });
860
+ return receipt;
861
+ }
862
+ /** Hands each tenant's change to the `api.onPresence` listeners; their failures are logged. */
863
+ #notify(changes) {
864
+ for (const [tenant, change] of changes) {
865
+ const presence = Object.freeze({
866
+ tenant,
867
+ joined: Object.freeze([...change.joined]),
868
+ left: Object.freeze([...change.left]),
869
+ });
870
+ for (const listener of this.#listeners) {
871
+ const failed = (error) => this.#throttle.report('error', 'WebSocket presence listener failed', 'PRESENCE_LISTENER_FAILED', error);
872
+ try {
873
+ const result = listener(presence);
874
+ if (result && typeof result.then === 'function')
875
+ result.then(undefined, failed);
876
+ }
877
+ catch (error) {
878
+ failed(error);
879
+ }
880
+ }
881
+ }
882
+ }
447
883
  start() {
448
884
  const events = this.hub.events();
449
885
  this.#loop = (async () => {
@@ -453,39 +889,94 @@ export class Gateway {
453
889
  continue;
454
890
  }
455
891
  if (event.kind === 'kick') {
892
+ this.context.logger.log('debug', 'WebSocket kick', {
893
+ id: event.id,
894
+ connections: event.connections.length,
895
+ code: event.code,
896
+ });
456
897
  for (const id of event.connections)
457
898
  this.#clients.get(id)?.socket.close(event.code, event.reason);
458
899
  continue;
459
900
  }
460
- // Encoded once and shared by every recipient; ws keeps per-socket order in its queue.
461
- const frame = Buffer.from(JSON.stringify({
462
- $dokebi: 'delivery-v1',
463
- id: event.id,
464
- expiresAt: event.expiresAt,
465
- data: Buffer.from(event.data).toString('base64'),
466
- }));
901
+ // The broadcast bytes as they are (D-0057): a text frame when they are UTF-8 (the
902
+ // `{ type, data }` JSON of defineSocket), a binary frame otherwise. One buffer shared
903
+ // by every plain recipient; ws keeps per-socket order in its queue. Sealed sockets get
904
+ // their own sealed copy of the text (each key and nonce is per connection and frame).
905
+ const frame = Buffer.from(event.data.buffer, event.data.byteOffset, event.data.byteLength);
906
+ const kind = isUtf8(frame) ? TEXT : BINARY;
907
+ let local = 0;
908
+ let sealed = 0;
909
+ let dropped = 0;
910
+ let text;
467
911
  for (const id of event.connections) {
468
912
  const value = this.#clients.get(id);
469
- if (value)
470
- this.#deliver(value.socket, frame);
913
+ if (!value)
914
+ continue;
915
+ local++;
916
+ if (!value.codec) {
917
+ this.#deliver(value.socket, frame, kind);
918
+ continue;
919
+ }
920
+ // A sealed connection carries text only: a binary broadcast is not sent to it.
921
+ if (kind === BINARY) {
922
+ dropped++;
923
+ continue;
924
+ }
925
+ text ??= frame.toString();
926
+ let sealedFrame;
927
+ try {
928
+ sealedFrame = value.codec.encode(text);
929
+ }
930
+ catch (error) {
931
+ this.#throttle.report('error', 'WebSocket broadcast not sealed; socket closed', 'SEAL_ENCODE_FAILED', error, { connection: id });
932
+ value.socket.close(1011, 'Socket failure');
933
+ continue;
934
+ }
935
+ sealed++;
936
+ this.#deliver(value.socket, sealedFrame, TEXT);
471
937
  }
938
+ if (dropped)
939
+ this.#throttle.report('warn', 'WebSocket binary broadcast not sent to sealed sockets', 'SEAL_BINARY_UNSUPPORTED', undefined, { id: event.id, dropped });
940
+ if (this.context.logger.isEnabled('debug'))
941
+ this.context.logger.log('debug', 'WebSocket broadcast delivered', {
942
+ id: event.id,
943
+ connections: event.connections.length,
944
+ local,
945
+ sealed,
946
+ binary: kind === BINARY,
947
+ });
472
948
  // Queued events would otherwise be fanned out in one tick, before any socket can flush,
473
949
  // and a burst could exceed maxBufferedBytes on healthy sockets. One turn lets I/O run.
474
950
  if (this.#sends)
475
951
  await immediate();
476
952
  }
477
- })().catch(() => {
953
+ })().catch((error) => {
954
+ // The hub ended its event stream (overflow, closed): every socket gets 1013 and reconnects.
955
+ this.context.logger.log('error', 'WebSocket hub event stream failed; sockets closed', {
956
+ code: 'HUB_EVENTS_FAILED',
957
+ error: errorText(error),
958
+ open: this.#clients.size,
959
+ });
478
960
  for (const value of this.#clients.values())
479
961
  value.socket.close(1013, 'Hub unavailable');
480
962
  });
963
+ this.context.logger.log('debug', 'WebSocket gateway started', {
964
+ routes: this.#routes.length,
965
+ heartbeatMs: this.settings.heartbeatMs,
966
+ idleTimeoutMs: this.settings.idleTimeoutMs,
967
+ });
481
968
  this.#timer = setInterval(() => {
482
969
  const idleBefore = performance.now() - this.settings.idleTimeoutMs;
483
- for (const value of this.#clients.values()) {
970
+ for (const [id, value] of this.#clients) {
484
971
  if (this.settings.idleTimeoutMs && value.seen < idleBefore) {
972
+ this.context.logger.log('debug', 'WebSocket idle closed', { connection: id });
485
973
  value.socket.close(4408, 'Idle');
486
974
  continue;
487
975
  }
488
976
  if (!value.alive) {
977
+ this.context.logger.log('debug', 'WebSocket heartbeat missed; terminated', {
978
+ connection: id,
979
+ });
489
980
  value.socket.terminate();
490
981
  continue;
491
982
  }
@@ -497,7 +988,12 @@ export class Gateway {
497
988
  }
498
989
  /** Stops waiting as soon as `signal` (the core drain deadline) aborts; sockets are then cut. */
499
990
  async drain(signal) {
991
+ const started = performance.now();
500
992
  this.#closing = true;
993
+ this.context.logger.log('debug', 'WebSocket drain started', {
994
+ open: this.#clients.size,
995
+ tasks: this.#tasks.size,
996
+ });
501
997
  if (this.#timer)
502
998
  clearInterval(this.#timer);
503
999
  const stop = aborted(signal);
@@ -509,10 +1005,20 @@ export class Gateway {
509
1005
  const deadline = performance.now() + this.settings.closeTimeoutMs;
510
1006
  while (this.#clients.size && performance.now() < deadline && !signal.aborted)
511
1007
  await Promise.race([delay(10), stop]);
1008
+ if (this.#clients.size)
1009
+ this.context.logger.log('debug', 'WebSocket drain terminating sockets', {
1010
+ open: this.#clients.size,
1011
+ aborted: signal.aborted,
1012
+ });
512
1013
  for (const value of this.#clients.values())
513
1014
  value.socket.terminate();
514
1015
  await this.hub.events().return?.();
515
1016
  await Promise.race([this.#loop, stop]);
516
1017
  await this.#idle(signal, stop, true);
1018
+ this.#throttle.flush();
1019
+ this.context.logger.log('debug', 'WebSocket drain finished', {
1020
+ ms: Math.round(performance.now() - started),
1021
+ aborted: signal.aborted,
1022
+ });
517
1023
  }
518
1024
  }