@celljs/ta-relay 4.12.0 → 4.13.1

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.en.md +26 -1
  2. package/README.md +26 -1
  3. package/esm/common/index.d.ts +3 -0
  4. package/esm/common/index.js +3 -0
  5. package/esm/common/relay-friends.d.ts +154 -0
  6. package/esm/common/relay-friends.js +482 -0
  7. package/esm/common/relay-hub-util.d.ts +9 -0
  8. package/esm/common/relay-hub-util.js +19 -2
  9. package/esm/common/relay-hubs-connection.d.ts +17 -5
  10. package/esm/common/relay-hubs-connection.js +40 -37
  11. package/esm/common/relay-hubs-protocol.d.ts +51 -7
  12. package/esm/common/relay-hubs.d.ts +67 -5
  13. package/esm/common/relay-hubs.js +305 -16
  14. package/esm/common/relay-identity.d.ts +2 -0
  15. package/esm/common/relay-info.d.ts +44 -0
  16. package/esm/common/relay-info.js +94 -0
  17. package/esm/common/relay-protocol.d.ts +7 -3
  18. package/esm/common/relay-protocol.js +7 -3
  19. package/esm/common/relay-room-chat.d.ts +6 -1
  20. package/esm/common/relay-room-chat.js +1 -0
  21. package/esm/common/relay-spot.d.ts +122 -0
  22. package/esm/common/relay-spot.js +179 -0
  23. package/esm/guest/guest-i18n.d.ts +2 -0
  24. package/esm/guest/guest-i18n.js +4 -0
  25. package/esm/guest/guest.js +9 -1
  26. package/esm/node/index.d.ts +2 -0
  27. package/esm/node/index.js +2 -0
  28. package/esm/node/relay-file-storage.d.ts +32 -0
  29. package/esm/node/relay-file-storage.js +101 -0
  30. package/esm/node/relay-host.d.ts +63 -28
  31. package/esm/node/relay-host.js +145 -52
  32. package/esm/node/relay-server.d.ts +37 -3
  33. package/esm/node/relay-server.js +193 -37
  34. package/esm/node/relay-store.d.ts +34 -6
  35. package/esm/node/relay-store.js +54 -22
  36. package/esm/node/relays-file.d.ts +139 -0
  37. package/esm/node/relays-file.js +373 -0
  38. package/lib/common/index.d.ts +3 -0
  39. package/lib/common/index.js +3 -0
  40. package/lib/common/relay-friends.d.ts +155 -0
  41. package/lib/common/relay-friends.js +486 -0
  42. package/lib/common/relay-hub-util.d.ts +9 -0
  43. package/lib/common/relay-hub-util.js +19 -2
  44. package/lib/common/relay-hubs-connection.d.ts +17 -5
  45. package/lib/common/relay-hubs-connection.js +40 -37
  46. package/lib/common/relay-hubs-protocol.d.ts +51 -7
  47. package/lib/common/relay-hubs.d.ts +67 -5
  48. package/lib/common/relay-hubs.js +306 -17
  49. package/lib/common/relay-identity.d.ts +2 -0
  50. package/lib/common/relay-info.d.ts +45 -0
  51. package/lib/common/relay-info.js +97 -0
  52. package/lib/common/relay-protocol.d.ts +7 -3
  53. package/lib/common/relay-protocol.js +8 -4
  54. package/lib/common/relay-room-chat.d.ts +6 -1
  55. package/lib/common/relay-room-chat.js +1 -0
  56. package/lib/common/relay-spot.d.ts +123 -0
  57. package/lib/common/relay-spot.js +183 -0
  58. package/lib/guest/guest-i18n.d.ts +2 -0
  59. package/lib/guest/guest-i18n.js +4 -0
  60. package/lib/guest/guest.js +9 -1
  61. package/lib/node/index.d.ts +2 -0
  62. package/lib/node/index.js +2 -0
  63. package/lib/node/relay-file-storage.d.ts +33 -0
  64. package/lib/node/relay-file-storage.js +105 -0
  65. package/lib/node/relay-host.d.ts +63 -28
  66. package/lib/node/relay-host.js +146 -53
  67. package/lib/node/relay-server.d.ts +37 -3
  68. package/lib/node/relay-server.js +193 -37
  69. package/lib/node/relay-store.d.ts +34 -6
  70. package/lib/node/relay-store.js +54 -22
  71. package/lib/node/relays-file.d.ts +140 -0
  72. package/lib/node/relays-file.js +378 -0
  73. package/package.json +2 -2
  74. package/viewer/room.html +10 -10
@@ -2,7 +2,7 @@ import { RelayCipher } from '../common/relay-cipher.js';
2
2
  import { type RelayFilesFrame } from '../common/relay-files.js';
3
3
  import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol.js';
4
4
  import { RelayFilesHost } from './relay-files-host.js';
5
- import type { RelayContentShare, RelayShare, RelayStore } from './relay-store.js';
5
+ import { type RelayContentShare, type RelayShare, RelayStore } from './relay-store.js';
6
6
  export interface RelayHostOptions {
7
7
  store: RelayStore;
8
8
  /** The WebSocket of this ta serve, with its token: every device gets a connection to it of its own. */
@@ -16,6 +16,10 @@ export interface RelayHostOptions {
16
16
  /** Hands on the content shared in the store (RelayStore.contents) to whoever has its link; without it, such links are refused. */
17
17
  shareContent?: boolean;
18
18
  }
19
+ export declare namespace RelayHostUtil {
20
+ /** The WebSocket of ta serve for a device: with its id, so that ta serve makes its calls as the one it belongs to. */
21
+ function localUrl(base: string, deviceId: string | undefined): string;
22
+ }
19
23
  /**
20
24
  * A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
21
25
  * for files (RelayFiles, RelayFilesHost), the file it asks for. What a channel is, it says in its first
@@ -23,7 +27,7 @@ export interface RelayHostOptions {
23
27
  */
24
28
  declare class RelayChannel {
25
29
  readonly id: string;
26
- protected readonly host: RelayHost;
30
+ protected readonly host: RelayUplink;
27
31
  protected readonly options: RelayHostOptions;
28
32
  protected cipher?: RelayCipher;
29
33
  protected local?: WebSocket;
@@ -34,13 +38,17 @@ declare class RelayChannel {
34
38
  /** Content shared with Ta friends: a cipher with the key of each, till the proof of one opens. */
35
39
  protected candidates?: RelayCipher[];
36
40
  protected files?: RelayFilesHost;
41
+ /** The paired device on this channel: ta serve is told which, to know whose calls they are. */
42
+ protected deviceId?: string;
37
43
  protected readonly early: string[];
38
44
  /** Agreed in the hello of a viewer of content, set once it proved. */
39
45
  protected compress: boolean;
40
46
  protected proofTimer?: ReturnType<typeof setTimeout>;
41
47
  protected readonly timer: NodeJS.Timeout;
42
- constructor(id: string, host: RelayHost, options: RelayHostOptions);
48
+ constructor(id: string, host: RelayUplink, options: RelayHostOptions);
43
49
  receive(text: string): Promise<void>;
50
+ /** A paired device on ta serve or its files, not one holding a link. */
51
+ get device(): boolean;
44
52
  /** From the relay: the device is gone. */
45
53
  closed(): void;
46
54
  /** A frame of the files of the workspace, sealed as the rest. */
@@ -56,43 +64,39 @@ declare class RelayChannel {
56
64
  protected answer(welcome: RelayWelcome): void;
57
65
  protected close(): void;
58
66
  }
67
+ /** What a channel needs of the connection it came over. */
68
+ interface RelayUplinkOwner {
69
+ readonly options: RelayHostOptions;
70
+ blocked(contentId: string): boolean;
71
+ wrongKey(contentId: string): void;
72
+ }
59
73
  /**
60
- * ta serve at the relay: one WebSocket out to it, over which the paired devices come in, each on a
61
- * channel of its own. A device that knows its key gets a connection to ta serve; nothing else does, and
62
- * the relay sees only what the two seal.
74
+ * ta serve at one relay: one WebSocket out to it, over which the devices and links of that relay come in, each on a
75
+ * channel of its own.
63
76
  */
64
- export declare class RelayHost {
65
- protected readonly options: RelayHostOptions;
77
+ declare class RelayUplink {
78
+ /** The relay, without a trailing slash. */
79
+ readonly relay: string;
80
+ protected readonly target: string;
81
+ protected readonly owner: RelayUplinkOwner;
66
82
  protected socket?: WebSocket;
67
- /** Where {@link socket} goes: WebSocket.url is normalised. */
68
- protected target?: string;
69
83
  protected stopped: boolean;
70
84
  protected attempts: number;
71
85
  protected timer?: ReturnType<typeof setTimeout>;
72
86
  protected pingTimer?: ReturnType<typeof setInterval>;
73
- protected readonly channels: Map<string, RelayChannel>;
74
- /** Wrong keys of each share of content, of late. */
75
- protected readonly wrong: Map<string, {
76
- count: number;
77
- since: number;
78
- }>;
79
- constructor(options: RelayHostOptions);
80
- /** @internal Whether a share of content had too many wrong keys of late. */
81
- blocked(contentId: string, now?: number): boolean;
82
- /** @internal */
83
- wrongKey(contentId: string, now?: number): void;
84
- /** Whether the connection to the relay is open. */
87
+ readonly channels: Map<string, RelayChannel>;
88
+ constructor(
89
+ /** The relay, without a trailing slash. */
90
+ relay: string, target: string, owner: RelayUplinkOwner);
85
91
  get online(): boolean;
86
- /** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
92
+ get devicesOpen(): number;
87
93
  start(): void;
88
- /** relay.json may have changed: connect if it now has devices, or leave if it has none. */
89
- refresh(): void;
90
94
  stop(): void;
91
- /** @internal */
95
+ blocked(contentId: string): boolean;
96
+ wrongKey(contentId: string): void;
92
97
  send(message: RelayHostMessage | typeof RELAY_PING): void;
93
- /** @internal */
94
98
  closeChannel(id: string): void;
95
- protected connect(target: string): void;
99
+ protected connect(): void;
96
100
  /**
97
101
  * The heartbeat of the connection to the relay. Without it a connection gone without a word (through
98
102
  * a proxy, a network that changed, a computer that slept) would look open for good, its devices told
@@ -106,4 +110,35 @@ export declare class RelayHost {
106
110
  protected disconnect(): void;
107
111
  protected closeChannels(): void;
108
112
  }
113
+ /**
114
+ * ta serve at its relays: one WebSocket out to each relay its devices and links are on (relay.json), over which they
115
+ * come in, each on a channel of its own. A device that knows its key gets a connection to ta serve; nothing else
116
+ * does, and a relay sees only what the two seal.
117
+ */
118
+ export declare class RelayHost implements RelayUplinkOwner {
119
+ readonly options: RelayHostOptions;
120
+ protected stopped: boolean;
121
+ protected readonly uplinks: Map<string, RelayUplink>;
122
+ /** Wrong keys of each share of content, of late. */
123
+ protected readonly wrong: Map<string, {
124
+ count: number;
125
+ since: number;
126
+ }>;
127
+ constructor(options: RelayHostOptions);
128
+ /** @internal Whether a share of content had too many wrong keys of late. */
129
+ blocked(contentId: string, now?: number): boolean;
130
+ /** @internal */
131
+ wrongKey(contentId: string, now?: number): void;
132
+ /** Whether the connection to every relay it should be at is open; false when it is at none. */
133
+ get online(): boolean;
134
+ /** Whether the connection to this relay is open. */
135
+ onlineAt(relay: string): boolean;
136
+ /** The paired devices connected now, each connection once: those holding a link are not counted. */
137
+ get devicesOpen(): number;
138
+ /** Connects to the relays relay.json needs, and keeps connected; checks again on {@link refresh}. */
139
+ start(): void;
140
+ /** relay.json may have changed: connect to a relay that now has devices or links, leave one that has none. */
141
+ refresh(): void;
142
+ stop(): void;
143
+ }
109
144
  export {};
@@ -4,6 +4,20 @@ import { RelayFilesUtil } from '../common/relay-files.js';
4
4
  import { RelayPairingUtil } from '../common/relay-pairing.js';
5
5
  import { RELAY_HOST_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, RELAY_PROOF, RELAY_SHARE_CHUNK_BYTES, RELAY_SHARE_MAX_BYTES, RelayCloseCode, } from '../common/relay-protocol.js';
6
6
  import { RelayFilesHost } from './relay-files-host.js';
7
+ import { RelayStore } from './relay-store.js';
8
+ export var RelayHostUtil;
9
+ (function (RelayHostUtil) {
10
+ /** The WebSocket of ta serve for a device: with its id, so that ta serve makes its calls as the one it belongs to. */
11
+ function localUrl(base, deviceId) {
12
+ if (!deviceId) {
13
+ return base;
14
+ }
15
+ const url = new URL(base);
16
+ url.searchParams.set('device', deviceId);
17
+ return url.toString();
18
+ }
19
+ RelayHostUtil.localUrl = localUrl;
20
+ })(RelayHostUtil || (RelayHostUtil = {}));
7
21
  /** Wrong keys (passwords) for a share before it is refused a while, and for how long. */
8
22
  const MAX_WRONG_KEYS = 10;
9
23
  const WRONG_KEY_WINDOW_MS = 10 * 60 * 1000;
@@ -86,6 +100,10 @@ class RelayChannel {
86
100
  this.early.push(data);
87
101
  }
88
102
  }
103
+ /** A paired device on ta serve or its files, not one holding a link. */
104
+ get device() {
105
+ return this.mode === 'rpc' || this.mode === 'files';
106
+ }
89
107
  /** From the relay: the device is gone. */
90
108
  closed() {
91
109
  var _a, _b;
@@ -110,9 +128,13 @@ class RelayChannel {
110
128
  return;
111
129
  }
112
130
  const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
113
- const device = valid ? this.options.store.device(hello.device) : undefined;
114
- const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
115
- const content = valid && !device && !share && this.options.shareContent ? this.options.store.content(hello.device) : undefined;
131
+ // Each comes only through the relay it was paired or shared on: one of another relay is not known here.
132
+ const config = this.options.store.read();
133
+ const here = (item) => (item && RelayStore.relayOf(item, config) === this.host.relay ? item : undefined);
134
+ const device = valid ? here(this.options.store.device(hello.device)) : undefined;
135
+ this.deviceId = device === null || device === void 0 ? void 0 : device.id;
136
+ const share = valid && !device && this.options.sharePackage ? here(this.options.store.share(hello.device)) : undefined;
137
+ const content = valid && !device && !share && this.options.shareContent ? here(this.options.store.content(hello.device)) : undefined;
116
138
  if (content && this.host.blocked(content.id)) {
117
139
  this.answer({ t: 'refused', reason: 'busy' });
118
140
  this.close();
@@ -214,7 +236,7 @@ class RelayChannel {
214
236
  this.close();
215
237
  }
216
238
  connectLocal(cipher) {
217
- const local = new WebSocket(this.options.localUrl);
239
+ const local = new WebSocket(RelayHostUtil.localUrl(this.options.localUrl, this.deviceId));
218
240
  this.local = local;
219
241
  local.onopen = () => {
220
242
  for (const data of this.early.splice(0)) {
@@ -236,68 +258,52 @@ class RelayChannel {
236
258
  }
237
259
  }
238
260
  /**
239
- * ta serve at the relay: one WebSocket out to it, over which the paired devices come in, each on a
240
- * channel of its own. A device that knows its key gets a connection to ta serve; nothing else does, and
241
- * the relay sees only what the two seal.
261
+ * ta serve at one relay: one WebSocket out to it, over which the devices and links of that relay come in, each on a
262
+ * channel of its own.
242
263
  */
243
- export class RelayHost {
244
- constructor(options) {
245
- this.options = options;
264
+ class RelayUplink {
265
+ constructor(
266
+ /** The relay, without a trailing slash. */
267
+ relay, target, owner) {
268
+ this.relay = relay;
269
+ this.target = target;
270
+ this.owner = owner;
246
271
  this.stopped = false;
247
272
  this.attempts = 0;
248
273
  this.channels = new Map();
249
- /** Wrong keys of each share of content, of late. */
250
- this.wrong = new Map();
251
- }
252
- /** @internal Whether a share of content had too many wrong keys of late. */
253
- blocked(contentId, now = Date.now()) {
254
- const wrong = this.wrong.get(contentId);
255
- return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
256
- }
257
- /** @internal */
258
- wrongKey(contentId, now = Date.now()) {
259
- const wrong = this.wrong.get(contentId);
260
- this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
261
274
  }
262
- /** Whether the connection to the relay is open. */
263
275
  get online() {
264
276
  var _a;
265
277
  return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN;
266
278
  }
267
- /** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
268
- start() {
269
- this.stopped = false;
270
- this.refresh();
271
- }
272
- /** relay.json may have changed: connect if it now has devices, or leave if it has none. */
273
- refresh() {
274
- const { url, host, devices } = this.options.store.read();
275
- const shared = (!!this.options.sharePackage && this.options.store.shares().length > 0) ||
276
- (!!this.options.shareContent && this.options.store.contents().some(content => content.access !== 'self'));
277
- const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
278
- if (!wanted) {
279
- this.disconnect();
280
- return;
279
+ get devicesOpen() {
280
+ let open = 0;
281
+ for (const channel of this.channels.values()) {
282
+ open += channel.device ? 1 : 0;
281
283
  }
282
- const target = `${RelayPairingUtil.websocketUrl(url, RELAY_HOST_PATH + host.id)}?secret=${encodeURIComponent(host.secret)}`;
283
- if (this.socket && this.target === target && this.socket.readyState <= WebSocket.OPEN) {
284
- return;
284
+ return open;
285
+ }
286
+ start() {
287
+ if (!this.socket || this.socket.readyState > WebSocket.OPEN) {
288
+ this.connect();
285
289
  }
286
- this.disconnect();
287
- this.connect(target);
288
290
  }
289
291
  stop() {
290
292
  this.stopped = true;
291
293
  this.disconnect();
292
294
  }
293
- /** @internal */
295
+ blocked(contentId) {
296
+ return this.owner.blocked(contentId);
297
+ }
298
+ wrongKey(contentId) {
299
+ this.owner.wrongKey(contentId);
300
+ }
294
301
  send(message) {
295
302
  var _a;
296
303
  if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
297
304
  this.socket.send(typeof message === 'string' ? message : JSON.stringify(message));
298
305
  }
299
306
  }
300
- /** @internal */
301
307
  closeChannel(id) {
302
308
  const channel = this.channels.get(id);
303
309
  if (channel) {
@@ -306,10 +312,12 @@ export class RelayHost {
306
312
  this.send({ t: 'close', ch: id });
307
313
  }
308
314
  }
309
- connect(target) {
310
- const socket = new WebSocket(target);
315
+ connect() {
316
+ if (this.stopped) {
317
+ return;
318
+ }
319
+ const socket = new WebSocket(this.target);
311
320
  this.socket = socket;
312
- this.target = target;
313
321
  socket.onopen = () => {
314
322
  this.attempts = 0;
315
323
  this.beat(socket);
@@ -340,7 +348,7 @@ export class RelayHost {
340
348
  beat(socket) {
341
349
  var _a, _b;
342
350
  var _c;
343
- const every = (_c = this.options.pingMs) !== null && _c !== void 0 ? _c : RELAY_PING_MS;
351
+ const every = (_c = this.owner.options.pingMs) !== null && _c !== void 0 ? _c : RELAY_PING_MS;
344
352
  let last = Date.now();
345
353
  let ponged = false;
346
354
  let awaiting = false;
@@ -361,7 +369,7 @@ export class RelayHost {
361
369
  }
362
370
  if (slept || (awaiting && ponged)) {
363
371
  this.disconnect();
364
- this.refresh();
372
+ this.connect();
365
373
  return;
366
374
  }
367
375
  awaiting = true;
@@ -382,7 +390,7 @@ export class RelayHost {
382
390
  return;
383
391
  }
384
392
  if (message.t === 'open') {
385
- this.channels.set(message.ch, new RelayChannel(message.ch, this, this.options));
393
+ this.channels.set(message.ch, new RelayChannel(message.ch, this, this.owner.options));
386
394
  }
387
395
  else if (message.t === 'data') {
388
396
  void ((_a = this.channels.get(message.ch)) === null || _a === void 0 ? void 0 : _a.receive(message.d));
@@ -397,9 +405,9 @@ export class RelayHost {
397
405
  if (this.stopped) {
398
406
  return;
399
407
  }
400
- const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 500) * 2 ** this.attempts++, MAX_RECONNECT_DELAY_MS);
408
+ const delay = Math.min(((_a = this.owner.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 500) * 2 ** this.attempts++, MAX_RECONNECT_DELAY_MS);
401
409
  clearTimeout(this.timer);
402
- this.timer = setTimeout(() => this.refresh(), delay);
410
+ this.timer = setTimeout(() => this.connect(), delay);
403
411
  }
404
412
  disconnect() {
405
413
  clearTimeout(this.timer);
@@ -416,4 +424,89 @@ export class RelayHost {
416
424
  this.channels.clear();
417
425
  }
418
426
  }
427
+ /**
428
+ * ta serve at its relays: one WebSocket out to each relay its devices and links are on (relay.json), over which they
429
+ * come in, each on a channel of its own. A device that knows its key gets a connection to ta serve; nothing else
430
+ * does, and a relay sees only what the two seal.
431
+ */
432
+ export class RelayHost {
433
+ constructor(options) {
434
+ this.options = options;
435
+ this.stopped = false;
436
+ this.uplinks = new Map();
437
+ /** Wrong keys of each share of content, of late. */
438
+ this.wrong = new Map();
439
+ }
440
+ /** @internal Whether a share of content had too many wrong keys of late. */
441
+ blocked(contentId, now = Date.now()) {
442
+ const wrong = this.wrong.get(contentId);
443
+ return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
444
+ }
445
+ /** @internal */
446
+ wrongKey(contentId, now = Date.now()) {
447
+ const wrong = this.wrong.get(contentId);
448
+ this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
449
+ }
450
+ /** Whether the connection to every relay it should be at is open; false when it is at none. */
451
+ get online() {
452
+ return this.uplinks.size > 0 && [...this.uplinks.values()].every(uplink => uplink.online);
453
+ }
454
+ /** Whether the connection to this relay is open. */
455
+ onlineAt(relay) {
456
+ var _a;
457
+ var _b;
458
+ return (_b = (_a = this.uplinks.get(relay.replace(/\/+$/, ''))) === null || _a === void 0 ? void 0 : _a.online) !== null && _b !== void 0 ? _b : false;
459
+ }
460
+ /** The paired devices connected now, each connection once: those holding a link are not counted. */
461
+ get devicesOpen() {
462
+ let open = 0;
463
+ for (const uplink of this.uplinks.values()) {
464
+ open += uplink.devicesOpen;
465
+ }
466
+ return open;
467
+ }
468
+ /** Connects to the relays relay.json needs, and keeps connected; checks again on {@link refresh}. */
469
+ start() {
470
+ this.stopped = false;
471
+ this.refresh();
472
+ }
473
+ /** relay.json may have changed: connect to a relay that now has devices or links, leave one that has none. */
474
+ refresh() {
475
+ const { store, sharePackage, shareContent } = this.options;
476
+ const config = store.read();
477
+ const wanted = new Map();
478
+ if (!this.stopped && config.host) {
479
+ // The devices, and the links this ta serve hands on: what is shared for oneself alone needs no relay.
480
+ const used = [
481
+ ...config.devices,
482
+ ...(sharePackage ? store.shares() : []),
483
+ ...(shareContent ? store.contents().filter(content => content.access !== 'self') : []),
484
+ ];
485
+ for (const relay of new Set(used.map(item => RelayStore.relayOf(item, config)))) {
486
+ wanted.set(relay, `${RelayPairingUtil.websocketUrl(relay, RELAY_HOST_PATH + config.host.id)}?secret=${encodeURIComponent(config.host.secret)}`);
487
+ }
488
+ }
489
+ for (const [relay, uplink] of this.uplinks) {
490
+ if (!wanted.has(relay)) {
491
+ uplink.stop();
492
+ this.uplinks.delete(relay);
493
+ }
494
+ }
495
+ for (const [relay, target] of wanted) {
496
+ let uplink = this.uplinks.get(relay);
497
+ if (!uplink) {
498
+ uplink = new RelayUplink(relay, target, this);
499
+ this.uplinks.set(relay, uplink);
500
+ }
501
+ uplink.start();
502
+ }
503
+ }
504
+ stop() {
505
+ this.stopped = true;
506
+ for (const uplink of this.uplinks.values()) {
507
+ uplink.stop();
508
+ }
509
+ this.uplinks.clear();
510
+ }
511
+ }
419
512
  //# sourceMappingURL=relay-host.js.map
@@ -4,9 +4,16 @@ import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-co
4
4
  import { type RelayBlobStorage, RelayBlobs } from '../common/relay-blobs.js';
5
5
  import { RelayHub, type RelayHubStorage, type RelayHubWho } from '../common/relay-hub.js';
6
6
  import { RelayRoom } from '../common/relay-room.js';
7
+ import { RelaySpot, type RelaySpotSocket, type RelaySpotWho } from '../common/relay-spot.js';
8
+ import { FileRelayStorage } from './relay-file-storage.js';
7
9
  export interface RelayServerOptions {
8
10
  host?: string;
9
11
  port?: number;
12
+ /**
13
+ * Where rooms, sealed copies and which host holds which id are kept, so that they stay when the relay restarts
14
+ * (`ta relay --data <dir>`); without it, in memory for as long as it runs.
15
+ */
16
+ data?: string;
10
17
  }
11
18
  interface Room {
12
19
  secretHash: string;
@@ -40,7 +47,7 @@ declare class MemoryHubStorage implements RelayHubStorage {
40
47
  }
41
48
  interface Hub {
42
49
  hub: RelayHub;
43
- storage: MemoryHubStorage;
50
+ storage: MemoryHubStorage | FileRelayStorage;
44
51
  sockets: Map<WebSocketConnection, {
45
52
  who?: RelayHubWho;
46
53
  }>;
@@ -54,24 +61,51 @@ export declare class RelayServer {
54
61
  protected readonly rooms: Map<string, Room>;
55
62
  protected readonly blobs: Map<string, {
56
63
  blobs: RelayBlobs;
57
- storage: MemoryBlobStorage;
64
+ storage: MemoryBlobStorage | FileRelayStorage;
58
65
  pending: number;
59
66
  }>;
67
+ /** With `data`: the hash of the secret of each host id, kept across restarts. */
68
+ protected readonly claims: Map<string, string>;
60
69
  protected readonly hubs: Map<string, Hub>;
70
+ /** Spots of codes (RelaySpot): who is connected, nothing kept. */
71
+ protected readonly spots: Map<string, {
72
+ spot: RelaySpot;
73
+ sockets: Map<WebSocketConnection, {
74
+ who?: RelaySpotWho;
75
+ }>;
76
+ socketOf(connection: WebSocketConnection): RelaySpotSocket;
77
+ }>;
78
+ /** Codes nobody opened, tried lately, by address (RelaySpotTries). */
79
+ protected readonly spotTries: Map<string, number[]>;
80
+ /** The address each connection to a spot came from. */
81
+ protected readonly spotAddresses: WeakMap<WebSocketConnection, string>;
61
82
  protected server?: Server;
62
83
  constructor(options?: RelayServerOptions);
63
84
  listen(): Promise<string>;
64
85
  close(): Promise<void>;
65
86
  /** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
66
87
  protected blob(id: string, request: IncomingMessage, response: ServerResponse): Promise<void>;
88
+ protected blobCopy(id: string): {
89
+ blobs: RelayBlobs;
90
+ storage: MemoryBlobStorage | FileRelayStorage;
91
+ pending: number;
92
+ };
93
+ protected claimsFile(): string;
94
+ protected saveClaims(): void;
67
95
  /** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
68
96
  protected static body(request: IncomingMessage, max: number): Promise<Buffer<ArrayBuffer> | undefined>;
69
97
  protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
70
- /** The first host to come with an id keeps it, for as long as the relay runs. */
98
+ /** The first host to come with an id keeps it, for as long as the relay runs, or for good with `data`. */
71
99
  protected openHost(id: string, secret: string, connection: WebSocketConnection): void;
72
100
  protected openClient(id: string, connection: WebSocketConnection): void;
101
+ /** A connection to a spot of a code (RelaySpot): in memory, gone with the last one. */
102
+ protected openSpot(id: string, connection: WebSocketConnection, address: string): void;
73
103
  /** A connection to the hub of a room (RelayHub), kept in memory for as long as the relay runs. */
74
104
  protected openHub(id: string, connection: WebSocketConnection): void;
105
+ /** The hub of a room: in memory, or read from `data` when it was kept. */
106
+ protected hubOf(id: string): Hub;
107
+ /** A room nobody holds any more, its storage empty: forgotten. */
108
+ protected dropHub(id: string, hub: Hub | undefined): void;
75
109
  protected createRoom(id: string, secretHash: string): Room;
76
110
  protected static same(a: string, b: string): boolean;
77
111
  }