@celljs/ta-relay 4.12.0 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.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';
2
2
  import { type RelayFilesFrame } from '../common/relay-files';
3
3
  import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol';
4
4
  import { RelayFilesHost } from './relay-files-host';
5
- import type { RelayContentShare, RelayShare, RelayStore } from './relay-store';
5
+ import { type RelayContentShare, type RelayShare, RelayStore } from './relay-store';
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,5 +110,36 @@ 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 {};
110
145
  //# sourceMappingURL=relay-host.d.ts.map
@@ -1,12 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayHost = void 0;
3
+ exports.RelayHost = exports.RelayHostUtil = void 0;
4
4
  const relay_bytes_1 = require("../common/relay-bytes");
5
5
  const relay_cipher_1 = require("../common/relay-cipher");
6
6
  const relay_files_1 = require("../common/relay-files");
7
7
  const relay_pairing_1 = require("../common/relay-pairing");
8
8
  const relay_protocol_1 = require("../common/relay-protocol");
9
9
  const relay_files_host_1 = require("./relay-files-host");
10
+ const relay_store_1 = require("./relay-store");
11
+ var RelayHostUtil;
12
+ (function (RelayHostUtil) {
13
+ /** The WebSocket of ta serve for a device: with its id, so that ta serve makes its calls as the one it belongs to. */
14
+ function localUrl(base, deviceId) {
15
+ if (!deviceId) {
16
+ return base;
17
+ }
18
+ const url = new URL(base);
19
+ url.searchParams.set('device', deviceId);
20
+ return url.toString();
21
+ }
22
+ RelayHostUtil.localUrl = localUrl;
23
+ })(RelayHostUtil || (exports.RelayHostUtil = RelayHostUtil = {}));
10
24
  /** Wrong keys (passwords) for a share before it is refused a while, and for how long. */
11
25
  const MAX_WRONG_KEYS = 10;
12
26
  const WRONG_KEY_WINDOW_MS = 10 * 60 * 1000;
@@ -89,6 +103,10 @@ class RelayChannel {
89
103
  this.early.push(data);
90
104
  }
91
105
  }
106
+ /** A paired device on ta serve or its files, not one holding a link. */
107
+ get device() {
108
+ return this.mode === 'rpc' || this.mode === 'files';
109
+ }
92
110
  /** From the relay: the device is gone. */
93
111
  closed() {
94
112
  var _a, _b;
@@ -113,9 +131,13 @@ class RelayChannel {
113
131
  return;
114
132
  }
115
133
  const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
116
- const device = valid ? this.options.store.device(hello.device) : undefined;
117
- const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
118
- const content = valid && !device && !share && this.options.shareContent ? this.options.store.content(hello.device) : undefined;
134
+ // Each comes only through the relay it was paired or shared on: one of another relay is not known here.
135
+ const config = this.options.store.read();
136
+ const here = (item) => (item && relay_store_1.RelayStore.relayOf(item, config) === this.host.relay ? item : undefined);
137
+ const device = valid ? here(this.options.store.device(hello.device)) : undefined;
138
+ this.deviceId = device === null || device === void 0 ? void 0 : device.id;
139
+ const share = valid && !device && this.options.sharePackage ? here(this.options.store.share(hello.device)) : undefined;
140
+ const content = valid && !device && !share && this.options.shareContent ? here(this.options.store.content(hello.device)) : undefined;
119
141
  if (content && this.host.blocked(content.id)) {
120
142
  this.answer({ t: 'refused', reason: 'busy' });
121
143
  this.close();
@@ -217,7 +239,7 @@ class RelayChannel {
217
239
  this.close();
218
240
  }
219
241
  connectLocal(cipher) {
220
- const local = new WebSocket(this.options.localUrl);
242
+ const local = new WebSocket(RelayHostUtil.localUrl(this.options.localUrl, this.deviceId));
221
243
  this.local = local;
222
244
  local.onopen = () => {
223
245
  for (const data of this.early.splice(0)) {
@@ -239,68 +261,52 @@ class RelayChannel {
239
261
  }
240
262
  }
241
263
  /**
242
- * ta serve at the relay: one WebSocket out to it, over which the paired devices come in, each on a
243
- * channel of its own. A device that knows its key gets a connection to ta serve; nothing else does, and
244
- * the relay sees only what the two seal.
264
+ * ta serve at one relay: one WebSocket out to it, over which the devices and links of that relay come in, each on a
265
+ * channel of its own.
245
266
  */
246
- class RelayHost {
247
- constructor(options) {
248
- this.options = options;
267
+ class RelayUplink {
268
+ constructor(
269
+ /** The relay, without a trailing slash. */
270
+ relay, target, owner) {
271
+ this.relay = relay;
272
+ this.target = target;
273
+ this.owner = owner;
249
274
  this.stopped = false;
250
275
  this.attempts = 0;
251
276
  this.channels = new Map();
252
- /** Wrong keys of each share of content, of late. */
253
- this.wrong = new Map();
254
- }
255
- /** @internal Whether a share of content had too many wrong keys of late. */
256
- blocked(contentId, now = Date.now()) {
257
- const wrong = this.wrong.get(contentId);
258
- return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
259
- }
260
- /** @internal */
261
- wrongKey(contentId, now = Date.now()) {
262
- const wrong = this.wrong.get(contentId);
263
- this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
264
277
  }
265
- /** Whether the connection to the relay is open. */
266
278
  get online() {
267
279
  var _a;
268
280
  return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN;
269
281
  }
270
- /** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
271
- start() {
272
- this.stopped = false;
273
- this.refresh();
274
- }
275
- /** relay.json may have changed: connect if it now has devices, or leave if it has none. */
276
- refresh() {
277
- const { url, host, devices } = this.options.store.read();
278
- const shared = (!!this.options.sharePackage && this.options.store.shares().length > 0) ||
279
- (!!this.options.shareContent && this.options.store.contents().some(content => content.access !== 'self'));
280
- const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
281
- if (!wanted) {
282
- this.disconnect();
283
- return;
282
+ get devicesOpen() {
283
+ let open = 0;
284
+ for (const channel of this.channels.values()) {
285
+ open += channel.device ? 1 : 0;
284
286
  }
285
- const target = `${relay_pairing_1.RelayPairingUtil.websocketUrl(url, relay_protocol_1.RELAY_HOST_PATH + host.id)}?secret=${encodeURIComponent(host.secret)}`;
286
- if (this.socket && this.target === target && this.socket.readyState <= WebSocket.OPEN) {
287
- return;
287
+ return open;
288
+ }
289
+ start() {
290
+ if (!this.socket || this.socket.readyState > WebSocket.OPEN) {
291
+ this.connect();
288
292
  }
289
- this.disconnect();
290
- this.connect(target);
291
293
  }
292
294
  stop() {
293
295
  this.stopped = true;
294
296
  this.disconnect();
295
297
  }
296
- /** @internal */
298
+ blocked(contentId) {
299
+ return this.owner.blocked(contentId);
300
+ }
301
+ wrongKey(contentId) {
302
+ this.owner.wrongKey(contentId);
303
+ }
297
304
  send(message) {
298
305
  var _a;
299
306
  if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
300
307
  this.socket.send(typeof message === 'string' ? message : JSON.stringify(message));
301
308
  }
302
309
  }
303
- /** @internal */
304
310
  closeChannel(id) {
305
311
  const channel = this.channels.get(id);
306
312
  if (channel) {
@@ -309,10 +315,12 @@ class RelayHost {
309
315
  this.send({ t: 'close', ch: id });
310
316
  }
311
317
  }
312
- connect(target) {
313
- const socket = new WebSocket(target);
318
+ connect() {
319
+ if (this.stopped) {
320
+ return;
321
+ }
322
+ const socket = new WebSocket(this.target);
314
323
  this.socket = socket;
315
- this.target = target;
316
324
  socket.onopen = () => {
317
325
  this.attempts = 0;
318
326
  this.beat(socket);
@@ -343,7 +351,7 @@ class RelayHost {
343
351
  beat(socket) {
344
352
  var _a, _b;
345
353
  var _c;
346
- const every = (_c = this.options.pingMs) !== null && _c !== void 0 ? _c : relay_protocol_1.RELAY_PING_MS;
354
+ const every = (_c = this.owner.options.pingMs) !== null && _c !== void 0 ? _c : relay_protocol_1.RELAY_PING_MS;
347
355
  let last = Date.now();
348
356
  let ponged = false;
349
357
  let awaiting = false;
@@ -364,7 +372,7 @@ class RelayHost {
364
372
  }
365
373
  if (slept || (awaiting && ponged)) {
366
374
  this.disconnect();
367
- this.refresh();
375
+ this.connect();
368
376
  return;
369
377
  }
370
378
  awaiting = true;
@@ -385,7 +393,7 @@ class RelayHost {
385
393
  return;
386
394
  }
387
395
  if (message.t === 'open') {
388
- this.channels.set(message.ch, new RelayChannel(message.ch, this, this.options));
396
+ this.channels.set(message.ch, new RelayChannel(message.ch, this, this.owner.options));
389
397
  }
390
398
  else if (message.t === 'data') {
391
399
  void ((_a = this.channels.get(message.ch)) === null || _a === void 0 ? void 0 : _a.receive(message.d));
@@ -400,9 +408,9 @@ class RelayHost {
400
408
  if (this.stopped) {
401
409
  return;
402
410
  }
403
- const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 500) * 2 ** this.attempts++, MAX_RECONNECT_DELAY_MS);
411
+ const delay = Math.min(((_a = this.owner.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 500) * 2 ** this.attempts++, MAX_RECONNECT_DELAY_MS);
404
412
  clearTimeout(this.timer);
405
- this.timer = setTimeout(() => this.refresh(), delay);
413
+ this.timer = setTimeout(() => this.connect(), delay);
406
414
  }
407
415
  disconnect() {
408
416
  clearTimeout(this.timer);
@@ -419,5 +427,90 @@ class RelayHost {
419
427
  this.channels.clear();
420
428
  }
421
429
  }
430
+ /**
431
+ * ta serve at its relays: one WebSocket out to each relay its devices and links are on (relay.json), over which they
432
+ * come in, each on a channel of its own. A device that knows its key gets a connection to ta serve; nothing else
433
+ * does, and a relay sees only what the two seal.
434
+ */
435
+ class RelayHost {
436
+ constructor(options) {
437
+ this.options = options;
438
+ this.stopped = false;
439
+ this.uplinks = new Map();
440
+ /** Wrong keys of each share of content, of late. */
441
+ this.wrong = new Map();
442
+ }
443
+ /** @internal Whether a share of content had too many wrong keys of late. */
444
+ blocked(contentId, now = Date.now()) {
445
+ const wrong = this.wrong.get(contentId);
446
+ return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
447
+ }
448
+ /** @internal */
449
+ wrongKey(contentId, now = Date.now()) {
450
+ const wrong = this.wrong.get(contentId);
451
+ this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
452
+ }
453
+ /** Whether the connection to every relay it should be at is open; false when it is at none. */
454
+ get online() {
455
+ return this.uplinks.size > 0 && [...this.uplinks.values()].every(uplink => uplink.online);
456
+ }
457
+ /** Whether the connection to this relay is open. */
458
+ onlineAt(relay) {
459
+ var _a;
460
+ var _b;
461
+ return (_b = (_a = this.uplinks.get(relay.replace(/\/+$/, ''))) === null || _a === void 0 ? void 0 : _a.online) !== null && _b !== void 0 ? _b : false;
462
+ }
463
+ /** The paired devices connected now, each connection once: those holding a link are not counted. */
464
+ get devicesOpen() {
465
+ let open = 0;
466
+ for (const uplink of this.uplinks.values()) {
467
+ open += uplink.devicesOpen;
468
+ }
469
+ return open;
470
+ }
471
+ /** Connects to the relays relay.json needs, and keeps connected; checks again on {@link refresh}. */
472
+ start() {
473
+ this.stopped = false;
474
+ this.refresh();
475
+ }
476
+ /** relay.json may have changed: connect to a relay that now has devices or links, leave one that has none. */
477
+ refresh() {
478
+ const { store, sharePackage, shareContent } = this.options;
479
+ const config = store.read();
480
+ const wanted = new Map();
481
+ if (!this.stopped && config.host) {
482
+ // The devices, and the links this ta serve hands on: what is shared for oneself alone needs no relay.
483
+ const used = [
484
+ ...config.devices,
485
+ ...(sharePackage ? store.shares() : []),
486
+ ...(shareContent ? store.contents().filter(content => content.access !== 'self') : []),
487
+ ];
488
+ for (const relay of new Set(used.map(item => relay_store_1.RelayStore.relayOf(item, config)))) {
489
+ wanted.set(relay, `${relay_pairing_1.RelayPairingUtil.websocketUrl(relay, relay_protocol_1.RELAY_HOST_PATH + config.host.id)}?secret=${encodeURIComponent(config.host.secret)}`);
490
+ }
491
+ }
492
+ for (const [relay, uplink] of this.uplinks) {
493
+ if (!wanted.has(relay)) {
494
+ uplink.stop();
495
+ this.uplinks.delete(relay);
496
+ }
497
+ }
498
+ for (const [relay, target] of wanted) {
499
+ let uplink = this.uplinks.get(relay);
500
+ if (!uplink) {
501
+ uplink = new RelayUplink(relay, target, this);
502
+ this.uplinks.set(relay, uplink);
503
+ }
504
+ uplink.start();
505
+ }
506
+ }
507
+ stop() {
508
+ this.stopped = true;
509
+ for (const uplink of this.uplinks.values()) {
510
+ uplink.stop();
511
+ }
512
+ this.uplinks.clear();
513
+ }
514
+ }
422
515
  exports.RelayHost = RelayHost;
423
516
  //# 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';
5
5
  import { RelayHub, type RelayHubStorage, type RelayHubWho } from '../common/relay-hub';
6
6
  import { RelayRoom } from '../common/relay-room';
7
+ import { RelaySpot, type RelaySpotSocket, type RelaySpotWho } from '../common/relay-spot';
8
+ import { FileRelayStorage } from './relay-file-storage';
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
  }