@dokebi/websocket-hub 0.1.1 → 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.
@@ -1,12 +1,16 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { createConnection } from 'node:net';
3
- import { setTimeout as delay } from 'node:timers/promises';
3
+ import { ConsoleLogger } from '@dokebi/core';
4
4
  import { EventQueue } from './event-queue.js';
5
- import { array, epoch, identifier, matches, metadata, payload, query, record, records, sequence, } from './protocol.js';
5
+ import { errorCode, LogThrottle } from './log.js';
6
+ import { array, epoch, identifier, matches, metadata, PROTOCOL, payload, query, record, records, sequence, } from './protocol.js';
6
7
  import { HubError, } from './types.js';
7
8
  import { Wire } from './wire.js';
9
+ /** Codes of a leader that refused a broadcast because it is not (or no longer) the leader. */
10
+ const NOT_LEADER = new Set(['STALE_LEADER', 'STALE_PEER']);
8
11
  export class PeerClient {
9
12
  options;
13
+ logger;
10
14
  ownerEpoch = randomUUID();
11
15
  #session;
12
16
  /** Session of the last hello; the owner record in Redis carries the same value. */
@@ -19,26 +23,39 @@ export class PeerClient {
19
23
  #peers = new Map();
20
24
  #revision = 0;
21
25
  #broadcasts = 0;
22
- #acks = new Map();
23
- #ackBytes = 0;
24
- #ackTimer;
26
+ /** Protocol a leader named when it refused our hello (PROTOCOL_MISMATCH), for the log. */
27
+ #leaderProtocol;
25
28
  #beforeReady = [];
26
29
  #wire;
27
30
  #leader;
28
31
  #ready = false;
29
32
  #closing = false;
33
+ /** Saying bye: a drop now is this server leaving, not a fault (close() reports the result). */
34
+ #leaving = false;
35
+ /** The leader said it is stopping: the drop that follows is planned. */
36
+ #leaderLeaving = false;
30
37
  #next = 0;
31
38
  #pending = new Map();
32
39
  #tail = Promise.resolve();
33
40
  #queued = 0;
41
+ /**
42
+ * Roster changes waiting for one `set` (group commit). `set` carries the whole list, so the
43
+ * changes that arrive while one is queued or in flight are sent together as the final list:
44
+ * 500 sockets leaving took 500 sequential sets (about 1.8 s per worker, OPS-002) and every
45
+ * broadcast in that window waited the full deadline for connections that were gone.
46
+ */
47
+ #roster = [];
34
48
  #connecting;
35
49
  #readyWaiters = new Set();
36
- /** Told why the connection to the leader was dropped (logged by the hub node). */
37
- #onFail;
38
- constructor(options, onFail = () => { }) {
50
+ /** Failures and decisions on per-message paths, logged once per interval with a count. */
51
+ #throttle;
52
+ constructor(options,
53
+ /** The hub node passes the plugin logger; the console default keeps failures visible. */
54
+ logger = new ConsoleLogger()) {
39
55
  this.options = options;
56
+ this.logger = logger;
40
57
  this.events = new EventQueue(options.maxPending, options.queueBytes);
41
- this.#onFail = onFail;
58
+ this.#throttle = new LogThrottle(logger);
42
59
  }
43
60
  get ready() {
44
61
  return this.#ready && !this.#closing && !!this.#wire && !this.#wire.socket.destroyed;
@@ -50,13 +67,25 @@ export class PeerClient {
50
67
  return this.#owned;
51
68
  }
52
69
  #fail(error) {
53
- if (this.#ready)
54
- this.#onFail(error instanceof HubError ? error.code : 'UNKNOWN');
70
+ if (this.#ready) {
71
+ const code = error instanceof HubError ? error.code : errorCode(error, 'UNKNOWN');
72
+ // RECOVERING and CLOSED are this server's own decisions (reconnect, shutdown); any other
73
+ // cause (revision gap, stale leader, socket closed) is a failure.
74
+ if (code === 'RECOVERING' || code === 'CLOSED' || this.#leaving || this.#leaderLeaving)
75
+ this.logger.log('debug', 'Hub connection dropped', {
76
+ code,
77
+ leader: this.#leader?.serverId ?? null,
78
+ });
79
+ else
80
+ this.#throttle.report('error', 'Hub connection dropped', code, error, {
81
+ leader: this.#leader?.serverId ?? null,
82
+ });
83
+ }
55
84
  this.#ready = false;
56
85
  this.#wire?.close();
57
86
  for (const p of this.#pending.values()) {
58
87
  clearTimeout(p.timer);
59
- p.reject(error);
88
+ p.reject(p.outcome ? new HubError('OUTCOME_UNKNOWN', { cause: error }) : error);
60
89
  }
61
90
  this.#pending.clear();
62
91
  }
@@ -77,11 +106,18 @@ export class PeerClient {
77
106
  return this.#connecting;
78
107
  }
79
108
  async #connect(leader) {
109
+ const started = performance.now();
80
110
  this.#fail(new HubError('RECOVERING'));
81
111
  await this.#tail;
82
112
  this.#beforeReady = [];
83
113
  this.#leader = leader;
114
+ this.#leaderLeaving = false;
84
115
  const socket = createConnection({ host: leader.host, port: leader.port });
116
+ // Before the hello answer the caller owns the failure: startup retries it (debug, a leader
117
+ // that is still opening refuses peers) and recovery logs it as an error, with this socket
118
+ // error as the cause. Only a connection that was up is reported here at error.
119
+ let established = false;
120
+ let socketError;
85
121
  const wire = new Wire(socket, this.options.maxFrameBytes, this.options.queueBytes, (f) => {
86
122
  if (this.#wire !== wire)
87
123
  return;
@@ -93,7 +129,16 @@ export class PeerClient {
93
129
  }
94
130
  }, () => {
95
131
  if (this.#wire === wire)
96
- this.#fail(new HubError('DISCONNECTED'));
132
+ this.#fail(new HubError('DISCONNECTED', socketError === undefined ? undefined : { cause: socketError }));
133
+ }, (code, error) => {
134
+ if (established) {
135
+ this.#throttle.report('error', 'Hub peer connection failed', code, error, {
136
+ leader: leader.serverId,
137
+ });
138
+ return;
139
+ }
140
+ socketError = error;
141
+ this.#throttle.report('debug', 'Hub peer connection failed before it was established', code, error, { leader: leader.serverId });
97
142
  });
98
143
  this.#wire = wire;
99
144
  const timer = setTimeout(() => socket.destroy(new Error('Connect timeout')), this.options.leaseMs);
@@ -109,13 +154,31 @@ export class PeerClient {
109
154
  await this.#operation(async () => {
110
155
  const session = randomUUID();
111
156
  this.#session = session;
112
- const value = (await this.#rpc('hello', {
113
- owner: this.ownerEpoch,
114
- session,
115
- serverId: this.options.serverId,
116
- secret: this.options.secret,
117
- connections: [...this.#owned.values()],
118
- }));
157
+ this.#leaderProtocol = undefined;
158
+ let value;
159
+ try {
160
+ value = (await this.#rpc('hello', {
161
+ protocol: PROTOCOL,
162
+ owner: this.ownerEpoch,
163
+ session,
164
+ serverId: this.options.serverId,
165
+ secret: this.options.secret,
166
+ connections: [...this.#owned.values()],
167
+ }));
168
+ }
169
+ catch (error) {
170
+ if (error instanceof HubError && error.code === 'PROTOCOL_MISMATCH')
171
+ this.#protocolMismatch(leader, this.#leaderProtocol, error);
172
+ throw error;
173
+ }
174
+ // A 0.2 leader answers without a protocol: its broadcasts wait for ACKs this server no
175
+ // longer sends, so it is refused here (rolling restart, D-0057).
176
+ if (value.protocol !== PROTOCOL) {
177
+ const error = new HubError('PROTOCOL_MISMATCH');
178
+ this.#protocolMismatch(leader, value.protocol, error);
179
+ wire.close();
180
+ throw error;
181
+ }
119
182
  const peers = new Map();
120
183
  for (const row of array(value.owners, this.options.maxOwners)) {
121
184
  const pair = array(row, 2);
@@ -130,13 +193,28 @@ export class PeerClient {
130
193
  const previous = this.#peers;
131
194
  this.#peers = peers;
132
195
  this.#revision = sequence(value.revision);
196
+ let resynced = 0;
133
197
  for (const [owner, connections] of peers)
134
- if (JSON.stringify(previous.get(owner) ?? []) !== JSON.stringify(connections))
198
+ if (JSON.stringify(previous.get(owner) ?? []) !== JSON.stringify(connections)) {
199
+ resynced++;
135
200
  this.#resync(owner, connections);
201
+ }
136
202
  for (const owner of previous.keys())
137
- if (!peers.has(owner))
203
+ if (!peers.has(owner)) {
204
+ resynced++;
138
205
  this.#resync(owner, []);
206
+ }
207
+ this.logger.log('debug', 'Hub connected to leader', {
208
+ leader: leader.serverId,
209
+ epoch: leader.epoch,
210
+ servers: peers.size,
211
+ connections: this.#owned.size,
212
+ revision: this.#revision,
213
+ resynced,
214
+ ms: Math.round(performance.now() - started),
215
+ });
139
216
  this.#ready = true;
217
+ established = true;
140
218
  for (const resolve of this.#readyWaiters)
141
219
  resolve(true);
142
220
  this.#readyWaiters.clear();
@@ -146,6 +224,18 @@ export class PeerClient {
146
224
  this.#receive(frame);
147
225
  }, true);
148
226
  }
227
+ /**
228
+ * Servers of different versions cannot share a leader (D-0057). Logged at error with both
229
+ * versions, once per interval: the hub keeps retrying until every server runs the same version.
230
+ */
231
+ #protocolMismatch(leader, theirs, error) {
232
+ this.#throttle.report('error', 'Hub protocol differs from the leader; retrying', 'PROTOCOL_MISMATCH', error, {
233
+ protocol: PROTOCOL,
234
+ leaderProtocol: typeof theirs === 'number' ? theirs : null,
235
+ leader: leader.serverId,
236
+ fix: 'Run the same @dokebi/websocket-hub version on every server of the namespace',
237
+ });
238
+ }
149
239
  #resync(owner, connections) {
150
240
  try {
151
241
  this.events.push({
@@ -164,6 +254,13 @@ export class PeerClient {
164
254
  #receive(f) {
165
255
  if (f.epoch !== this.#leader?.epoch)
166
256
  throw new HubError('STALE_LEADER');
257
+ if (f.kind === 'leaving') {
258
+ this.#leaderLeaving = true;
259
+ this.logger.log('debug', 'Hub leader is stopping', {
260
+ leader: this.#leader?.serverId ?? null,
261
+ });
262
+ return;
263
+ }
167
264
  if (f.kind === 'reply') {
168
265
  const id = Number(f.id), pending = this.#pending.get(id);
169
266
  if (!pending)
@@ -172,8 +269,12 @@ export class PeerClient {
172
269
  clearTimeout(pending.timer);
173
270
  if (f.ok === true)
174
271
  pending.resolve(f.result);
175
- else
176
- pending.reject(new HubError(typeof f.code === 'string' ? f.code : 'PROTOCOL_ERROR'));
272
+ else {
273
+ const code = typeof f.code === 'string' ? f.code : 'PROTOCOL_ERROR';
274
+ if (code === 'PROTOCOL_MISMATCH')
275
+ this.#leaderProtocol = f.protocol;
276
+ pending.reject(new HubError(code));
277
+ }
177
278
  return;
178
279
  }
179
280
  if (f.kind === 'presence') {
@@ -192,6 +293,12 @@ export class PeerClient {
192
293
  this.#peers.set(owner, values);
193
294
  else
194
295
  this.#peers.delete(owner);
296
+ if (this.logger.isEnabled('debug'))
297
+ this.logger.log('debug', 'Hub presence changed', {
298
+ owner,
299
+ connections: values.length,
300
+ revision: rev,
301
+ });
195
302
  this.events.push({ kind: 'presence', revision: rev, ownerEpoch: owner, connections: values });
196
303
  return;
197
304
  }
@@ -203,12 +310,23 @@ export class PeerClient {
203
310
  id,
204
311
  connections,
205
312
  data: payload(f.data, this.options.maxPayloadBytes),
206
- expiresAt: sequence(f.expiresAt),
207
313
  };
208
314
  else if (f.kind === 'kick')
209
315
  event = { kind: 'kick', id, connections, code: Number(f.code), reason: identifier(f.reason) };
210
316
  else
211
317
  throw new HubError('PROTOCOL_ERROR');
318
+ // Broadcasts are at most once (D-0057) and producers no longer wait for ACKs, so a burst can
319
+ // outrun the gateway. A message that finds half the queue unread is dropped (warn, counted)
320
+ // instead of ending the event stream, which would stop this server's hub for good; the rest
321
+ // of the queue stays for presence and kicks, which cannot be dropped.
322
+ if (event.kind === 'message') {
323
+ if (!this.events.offer(event, 0.5))
324
+ this.#throttle.report('warn', 'Hub message dropped; event queue full', 'MESSAGE_DROPPED', undefined, {
325
+ id,
326
+ connections: connections.length,
327
+ });
328
+ return;
329
+ }
212
330
  try {
213
331
  this.events.push(event);
214
332
  }
@@ -218,7 +336,7 @@ export class PeerClient {
218
336
  throw error;
219
337
  }
220
338
  }
221
- #rpc(op, body = {}, timeout = this.options.leaseMs) {
339
+ #rpc(op, body = {}, timeout = this.options.leaseMs, outcome = false) {
222
340
  const wire = this.#wire;
223
341
  if (!wire || wire.socket.destroyed)
224
342
  return Promise.reject(new HubError('RECOVERING'));
@@ -232,7 +350,7 @@ export class PeerClient {
232
350
  this.#fail(new HubError('OUTCOME_UNKNOWN'));
233
351
  reject(new HubError('OUTCOME_UNKNOWN'));
234
352
  }, timeout);
235
- this.#pending.set(id, { resolve, reject, timer });
353
+ this.#pending.set(id, { resolve, reject, timer, outcome });
236
354
  try {
237
355
  wire.send({ id, op, epoch: this.#leader?.epoch, ...body });
238
356
  }
@@ -259,36 +377,99 @@ export class PeerClient {
259
377
  });
260
378
  return result;
261
379
  }
262
- async #replace(id, value) {
380
+ /**
381
+ * Queues one roster change. `apply` checks before it changes anything and throws a HubError to
382
+ * refuse only this change; the batch goes on. `offline` changes (a connection leaving) also
383
+ * work while recovering: they only have to leave the local roster.
384
+ */
385
+ #change(apply, offline = false) {
386
+ if (!offline && !this.ready)
387
+ return Promise.reject(new HubError('RECOVERING'));
388
+ if (this.#roster.length >= this.options.maxConnections + this.options.maxPending)
389
+ return Promise.reject(new HubError('BUSY'));
390
+ return new Promise((resolve, reject) => {
391
+ this.#roster.push({ apply, offline, resolve, reject });
392
+ // The first change of a batch queues the flush; later ones join it until it starts.
393
+ if (this.#roster.length === 1)
394
+ void this.#operation(() => this.#flushRoster(), true);
395
+ });
396
+ }
397
+ async #flushRoster() {
398
+ const batch = this.#roster;
399
+ this.#roster = [];
400
+ const started = performance.now();
401
+ const online = this.ready;
263
402
  const next = new Map(this.#owned);
264
- if (value)
265
- next.set(id, value);
266
- else
267
- next.delete(id);
268
- if (next.size > this.options.maxConnections)
269
- throw new HubError('CONNECTION_LIMIT');
270
- await this.#rpc('set', { connections: [...next.values()] });
403
+ const applied = [];
404
+ for (const change of batch) {
405
+ if (!online && !change.offline) {
406
+ change.reject(new HubError('RECOVERING'));
407
+ continue;
408
+ }
409
+ try {
410
+ change.apply(next);
411
+ applied.push(change);
412
+ }
413
+ catch (error) {
414
+ change.reject(error);
415
+ }
416
+ }
417
+ if (!applied.length)
418
+ return;
419
+ if (online)
420
+ try {
421
+ await this.#rpc('set', { connections: [...next.values()] });
422
+ }
423
+ catch (error) {
424
+ // Leaving connections leave locally anyway and the client reconnects to resync (as a
425
+ // single close did); every other change of the batch is refused with the cause.
426
+ const leaving = applied.filter((change) => change.offline);
427
+ for (const change of leaving)
428
+ change.apply(this.#owned);
429
+ for (const change of applied)
430
+ change.reject(error);
431
+ if (leaving.length)
432
+ this.disconnect();
433
+ return;
434
+ }
271
435
  this.#owned.clear();
272
- for (const [key, v] of next)
273
- this.#owned.set(key, v);
436
+ for (const [key, value] of next)
437
+ this.#owned.set(key, value);
438
+ for (const change of applied)
439
+ change.resolve();
440
+ if (this.logger.isEnabled('debug'))
441
+ this.logger.log('debug', 'Hub roster sent', {
442
+ changes: applied.length,
443
+ refused: batch.length - applied.length,
444
+ connections: next.size,
445
+ sent: online,
446
+ ms: Math.round(performance.now() - started),
447
+ });
274
448
  }
275
- register(input) {
276
- return this.#operation(async () => {
277
- const value = record(input, this.ownerEpoch);
278
- if (this.#owned.has(value.connection))
449
+ async register(input) {
450
+ const value = record(input, this.ownerEpoch);
451
+ await this.#change((next) => {
452
+ if (next.has(value.connection))
279
453
  throw new HubError('DUPLICATE_CONNECTION');
280
- await this.#replace(value.connection, value);
454
+ if (next.size >= this.options.maxConnections)
455
+ throw new HubError('CONNECTION_LIMIT');
456
+ next.set(value.connection, value);
457
+ });
458
+ {
281
459
  const token = Symbol();
282
460
  this.#tokens.set(value.connection, token);
283
461
  let closed = false;
462
+ let left = false;
284
463
  let closing;
285
- const change = (fn) => this.#operation(async () => {
286
- const current = this.#owned.get(value.connection);
464
+ const change = (fn) => this.#change((next) => {
465
+ const current = next.get(value.connection);
287
466
  if (closed || !current || this.#tokens.get(value.connection) !== token)
288
467
  throw new HubError('UNKNOWN_CONNECTION');
289
468
  const replacement = fn(current);
290
- await this.#replace(value.connection, replacement);
291
- if (!replacement) {
469
+ if (replacement)
470
+ next.set(value.connection, replacement);
471
+ else {
472
+ next.delete(value.connection);
292
473
  closed = true;
293
474
  this.#tokens.delete(value.connection);
294
475
  }
@@ -298,24 +479,19 @@ export class PeerClient {
298
479
  join: (room) => change((current) => record({ ...current, rooms: [...new Set([...current.rooms, identifier(room)])] }, this.ownerEpoch)),
299
480
  leave: (room) => change((current) => record({ ...current, rooms: current.rooms.filter((x) => x !== identifier(room)) }, this.ownerEpoch)),
300
481
  updateMetadata: (meta) => change((current) => record({ ...current, metadata: metadata(meta) }, this.ownerEpoch)),
301
- close: () => (closing ??= this.#operation(async () => {
302
- if (closed || this.#tokens.get(value.connection) !== token)
303
- return;
304
- closed = true;
305
- this.#tokens.delete(value.connection);
306
- this.#owned.delete(value.connection);
307
- if (this.ready) {
308
- try {
309
- await this.#rpc('set', { connections: [...this.#owned.values()] });
310
- }
311
- catch (error) {
312
- this.disconnect();
313
- throw error;
314
- }
482
+ close: () => (closing ??= this.#change((next) => {
483
+ // Applied to the batch's list, and again to the local list if the set failed.
484
+ if (!left) {
485
+ if (this.#tokens.get(value.connection) !== token)
486
+ return;
487
+ left = true;
488
+ closed = true;
489
+ this.#tokens.delete(value.connection);
315
490
  }
491
+ next.delete(value.connection);
316
492
  }, true)),
317
493
  });
318
- });
494
+ }
319
495
  }
320
496
  list(input) {
321
497
  if (!this.ready)
@@ -327,110 +503,67 @@ export class PeerClient {
327
503
  connections: Object.freeze([...this.#peers.values()].flat().filter((r) => matches(r, filter))),
328
504
  }));
329
505
  }
506
+ /**
507
+ * One request to the leader, answered once the leader has written the message frames
508
+ * (D-0057). Never resent: not connected is RECOVERING at once, a drop after sending is
509
+ * OUTCOME_UNKNOWN, and the caller decides whether to send again.
510
+ */
330
511
  broadcast(input, data) {
331
512
  if (!(data instanceof Uint8Array) || data.byteLength > this.options.maxPayloadBytes)
332
513
  return Promise.reject(new HubError('INVALID_ARGUMENT'));
333
- const filter = query(input), encoded = Buffer.from(data).toString('base64');
334
- if (this.#broadcasts >= this.options.maxPending)
514
+ let filter;
515
+ try {
516
+ filter = query(input);
517
+ }
518
+ catch (error) {
519
+ return Promise.reject(error);
520
+ }
521
+ if (this.#closing)
522
+ return Promise.reject(new HubError('CLOSED'));
523
+ if (!this.ready)
524
+ return Promise.reject(new HubError('RECOVERING'));
525
+ if (this.#broadcasts >= this.options.maxPending) {
526
+ this.#throttle.report('debug', 'Hub broadcast refused; too many in flight', 'BUSY', undefined, {
527
+ inFlight: this.#broadcasts,
528
+ });
335
529
  return Promise.reject(new HubError('BUSY'));
530
+ }
336
531
  this.#broadcasts++;
337
- const body = {
532
+ const sent = this.#rpc('broadcast', {
338
533
  query: filter,
339
- data: encoded,
340
- messageId: randomUUID(),
341
- expiresAt: Date.now() + this.options.deliveryTimeoutMs,
342
- };
343
- return (async () => {
344
- try {
345
- while (!this.#closing && Date.now() < body.expiresAt) {
346
- try {
347
- if (this.ready)
348
- return (await this.#rpc('broadcast', body, Math.max(1, body.expiresAt - Date.now())));
349
- }
350
- catch (error) {
351
- if (!(error instanceof HubError) ||
352
- ![
353
- 'RECOVERING',
354
- 'DISCONNECTED',
355
- 'STALE_LEADER',
356
- 'STALE_PEER',
357
- 'STORE_UNAVAILABLE',
358
- 'OUTCOME_UNKNOWN',
359
- 'BUSY',
360
- ].includes(error.code))
361
- throw error;
362
- }
363
- await delay(Math.min(100, Math.max(1, body.expiresAt - Date.now())));
364
- }
365
- throw new HubError(this.#closing ? 'CLOSED' : 'DELIVERY_TIMEOUT');
366
- }
367
- finally {
368
- this.#broadcasts--;
369
- }
370
- })();
371
- }
372
- acknowledge(id, connections) {
373
- epoch(id);
374
- if (this.#closing)
375
- return;
376
- if (!this.#acks.has(id) && this.#acks.size >= 256)
377
- return; // The durable sender retries unacknowledged deliveries.
378
- const selected = this.#acks.get(id) ?? new Set();
379
- for (const connection of array(connections, this.options.maxConnections).map(identifier)) {
380
- if (this.#owned.has(connection) &&
381
- !selected.has(connection) &&
382
- this.#ackBytes + Buffer.byteLength(connection) + 64 <= this.options.queueBytes) {
383
- selected.add(connection);
384
- this.#ackBytes += Buffer.byteLength(connection) + 64;
385
- }
386
- }
387
- this.#acks.set(id, selected);
388
- if (!this.#ackTimer)
389
- this.#ackTimer = setTimeout(() => {
390
- this.#ackTimer = undefined;
391
- const acks = this.#acks;
392
- this.#acks = new Map();
393
- this.#ackBytes = 0;
394
- if (!this.ready)
395
- return;
396
- // Split at the encoded frame boundary; oversize ACKs must not cause endless full-frame retries.
397
- let batch = [], bytes = 256;
398
- const flush = () => {
399
- if (batch.length)
400
- void this.#rpc('ack', { acks: batch }).catch(() => { });
401
- batch = [];
402
- bytes = 256;
403
- };
404
- for (const [id, connections] of acks) {
405
- let row = { id, connections: [] };
406
- for (const connection of connections) {
407
- const size = Buffer.byteLength(JSON.stringify(connection)) + 1;
408
- if (bytes + size + 128 > this.options.maxFrameBytes || batch.length >= 255) {
409
- if (row.connections.length)
410
- batch.push(row);
411
- flush();
412
- row = { id, connections: [] };
413
- }
414
- if (!row.connections.length)
415
- bytes += 96;
416
- row.connections.push(connection);
417
- bytes += size;
418
- }
419
- if (row.connections.length)
420
- batch.push(row);
421
- }
422
- flush();
423
- }, 10);
534
+ data: Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString('base64'),
535
+ }, this.options.leaseMs, true);
536
+ return sent
537
+ .then((result) => {
538
+ const value = result;
539
+ return Object.freeze({
540
+ id: epoch(value.id),
541
+ recipients: Object.freeze(array(value.recipients, this.options.maxTotalConnections).map((key) => {
542
+ if (typeof key !== 'string')
543
+ throw new HubError('PROTOCOL_ERROR');
544
+ return key;
545
+ })),
546
+ });
547
+ }, (error) => {
548
+ // Refused by a leader that is no longer one, or not written to the socket: nothing
549
+ // was sent, as when not connected.
550
+ const code = error instanceof HubError ? error.code : undefined;
551
+ if (code !== undefined &&
552
+ (NOT_LEADER.has(code) || code === 'DISCONNECTED' || code === 'BACKPRESSURE'))
553
+ throw new HubError('RECOVERING', { cause: error });
554
+ if (code === 'OUTCOME_UNKNOWN')
555
+ this.#throttle.report('debug', 'Hub broadcast outcome unknown; not resent', 'OUTCOME_UNKNOWN', error);
556
+ throw error;
557
+ })
558
+ .finally(() => {
559
+ this.#broadcasts--;
560
+ });
424
561
  }
425
562
  kick(input, code = 4403, reason = 'Disconnected by hub') {
426
563
  const filter = query(input);
427
564
  return this.#operation(async () => (await this.#rpc('kick', { query: filter, code, reason })));
428
565
  }
429
566
  async close() {
430
- if (this.#ackTimer)
431
- clearTimeout(this.#ackTimer);
432
- this.#acks.clear();
433
- this.#ackBytes = 0;
434
567
  if (this.#closing) {
435
568
  this.#fail(new HubError('CLOSED'));
436
569
  this.events.finish();
@@ -439,6 +572,7 @@ export class PeerClient {
439
572
  return;
440
573
  }
441
574
  await this.#tail;
575
+ this.#leaving = true;
442
576
  try {
443
577
  if (this.ready)
444
578
  await this.#rpc('bye');
@@ -449,6 +583,7 @@ export class PeerClient {
449
583
  this.events.finish();
450
584
  for (const resolve of [...this.#readyWaiters])
451
585
  resolve(false);
586
+ this.#throttle.flush();
452
587
  }
453
588
  }
454
589
  disconnect() {
@@ -1,4 +1,10 @@
1
1
  import { type HubQuery, type HubRecord } from './types.js';
2
+ /**
3
+ * Version of the frames hub servers exchange, sent in hello (D-0057). 2: broadcasts without
4
+ * client ACKs (no `accepted`, `ack` or resend). A leader refuses another version with
5
+ * PROTOCOL_MISMATCH; a 0.2 leader answers without it and is refused by the peer.
6
+ */
7
+ export declare const PROTOCOL = 2;
2
8
  export declare function encode(value: unknown, limit: number): Uint8Array;
3
9
  export declare function decode(data: Uint8Array, limit: number): Record<string, unknown>;
4
10
  export declare function identifier(value: unknown): string;
package/dist/protocol.js CHANGED
@@ -1,6 +1,12 @@
1
1
  import { types } from 'node:util';
2
2
  import { integer, keys, object } from './options.js';
3
3
  import { HubError } from './types.js';
4
+ /**
5
+ * Version of the frames hub servers exchange, sent in hello (D-0057). 2: broadcasts without
6
+ * client ACKs (no `accepted`, `ack` or resend). A leader refuses another version with
7
+ * PROTOCOL_MISMATCH; a 0.2 leader answers without it and is refused by the peer.
8
+ */
9
+ export const PROTOCOL = 2;
4
10
  const decoder = new TextDecoder('utf-8', { fatal: true });
5
11
  export function encode(value, limit) {
6
12
  try {