@celljs/ta-relay 4.11.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.
- package/README.en.md +26 -1
- package/README.md +26 -1
- package/esm/common/index.d.ts +5 -0
- package/esm/common/index.js +5 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-friends.d.ts +154 -0
- package/esm/common/relay-friends.js +482 -0
- package/esm/common/relay-hub-util.d.ts +9 -0
- package/esm/common/relay-hub-util.js +19 -2
- package/esm/common/relay-hubs-connection.d.ts +110 -0
- package/esm/common/relay-hubs-connection.js +508 -0
- package/esm/common/relay-hubs-protocol.d.ts +174 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +68 -216
- package/esm/common/relay-hubs.js +303 -507
- package/esm/common/relay-identity.d.ts +2 -0
- package/esm/common/relay-info.d.ts +44 -0
- package/esm/common/relay-info.js +94 -0
- package/esm/common/relay-protocol.d.ts +7 -3
- package/esm/common/relay-protocol.js +7 -3
- package/esm/common/relay-room-chat.d.ts +36 -2
- package/esm/common/relay-room-chat.js +32 -7
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +5 -0
- package/esm/guest/guest-i18n.js +10 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -4
- package/esm/guest/guest.js +39 -18
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-file-storage.d.ts +32 -0
- package/esm/node/relay-file-storage.js +101 -0
- package/esm/node/relay-host.d.ts +63 -28
- package/esm/node/relay-host.js +145 -52
- package/esm/node/relay-hub-files.d.ts +1 -1
- package/esm/node/relay-server.d.ts +37 -3
- package/esm/node/relay-server.js +193 -37
- package/esm/node/relay-store.d.ts +34 -6
- package/esm/node/relay-store.js +54 -22
- package/esm/node/relays-file.d.ts +139 -0
- package/esm/node/relays-file.js +373 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +5 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-friends.d.ts +155 -0
- package/lib/common/relay-friends.js +486 -0
- package/lib/common/relay-hub-util.d.ts +9 -0
- package/lib/common/relay-hub-util.js +19 -2
- package/lib/common/relay-hubs-connection.d.ts +111 -0
- package/lib/common/relay-hubs-connection.js +513 -0
- package/lib/common/relay-hubs-protocol.d.ts +175 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +68 -216
- package/lib/common/relay-hubs.js +311 -515
- package/lib/common/relay-identity.d.ts +2 -0
- package/lib/common/relay-info.d.ts +45 -0
- package/lib/common/relay-info.js +97 -0
- package/lib/common/relay-protocol.d.ts +7 -3
- package/lib/common/relay-protocol.js +8 -4
- package/lib/common/relay-room-chat.d.ts +36 -2
- package/lib/common/relay-room-chat.js +32 -7
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +5 -0
- package/lib/guest/guest-i18n.js +10 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -4
- package/lib/guest/guest.js +39 -18
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-file-storage.d.ts +33 -0
- package/lib/node/relay-file-storage.js +105 -0
- package/lib/node/relay-host.d.ts +63 -28
- package/lib/node/relay-host.js +146 -53
- package/lib/node/relay-hub-files.d.ts +1 -1
- package/lib/node/relay-server.d.ts +37 -3
- package/lib/node/relay-server.js +193 -37
- package/lib/node/relay-store.d.ts +34 -6
- package/lib/node/relay-store.js +54 -22
- package/lib/node/relays-file.d.ts +140 -0
- package/lib/node/relays-file.js +378 -0
- package/package.json +2 -2
- package/viewer/room.html +21 -14
package/lib/node/relay-host.js
CHANGED
|
@@ -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
|
-
|
|
117
|
-
const
|
|
118
|
-
const
|
|
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
|
|
243
|
-
* channel of its own.
|
|
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
|
|
247
|
-
constructor(
|
|
248
|
-
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
this.
|
|
273
|
-
|
|
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
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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
|
-
|
|
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(
|
|
313
|
-
|
|
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.
|
|
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.
|
|
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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs';
|
|
1
|
+
import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs-protocol';
|
|
2
2
|
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
3
3
|
export declare class RelayHubFiles implements RelayHubsStore {
|
|
4
4
|
readonly agentDir: string;
|
|
@@ -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
|
}
|