@celljs/ta-relay 4.11.0 → 4.12.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/esm/common/index.d.ts +2 -0
- package/esm/common/index.js +2 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-hubs-connection.d.ts +98 -0
- package/esm/common/relay-hubs-connection.js +505 -0
- package/esm/common/relay-hubs-protocol.d.ts +130 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +4 -214
- package/esm/common/relay-hubs.js +2 -495
- package/esm/common/relay-room-chat.d.ts +31 -2
- package/esm/common/relay-room-chat.js +31 -7
- package/esm/guest/guest-i18n.d.ts +3 -0
- package/esm/guest/guest-i18n.js +6 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -4
- package/esm/guest/guest.js +31 -18
- package/esm/node/relay-hub-files.d.ts +1 -1
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.js +2 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-hubs-connection.d.ts +99 -0
- package/lib/common/relay-hubs-connection.js +510 -0
- package/lib/common/relay-hubs-protocol.d.ts +131 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +4 -214
- package/lib/common/relay-hubs.js +9 -502
- package/lib/common/relay-room-chat.d.ts +31 -2
- package/lib/common/relay-room-chat.js +31 -7
- package/lib/guest/guest-i18n.d.ts +3 -0
- package/lib/guest/guest-i18n.js +6 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -4
- package/lib/guest/guest.js +31 -18
- package/lib/node/relay-hub-files.d.ts +1 -1
- package/package.json +2 -2
- package/viewer/room.html +21 -14
package/lib/common/relay-hubs.js
CHANGED
|
@@ -1,57 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubs = void 0;
|
|
4
|
-
const relay_hub_1 = require("./relay-hub");
|
|
5
4
|
const relay_hub_util_1 = require("./relay-hub-util");
|
|
5
|
+
const relay_hubs_connection_1 = require("./relay-hubs-connection");
|
|
6
6
|
const relay_identity_1 = require("./relay-identity");
|
|
7
|
-
const relay_pairing_1 = require("./relay-pairing");
|
|
8
|
-
const relay_protocol_1 = require("./relay-protocol");
|
|
9
|
-
const relay_room_chat_1 = require("./relay-room-chat");
|
|
10
|
-
const MAX_RECONNECT_MS = 30000;
|
|
11
|
-
const SKEW_MS = 5 * 60 * 1000;
|
|
12
7
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
13
|
-
const READY_MS = 15000;
|
|
14
|
-
const MAX_FRIEND_ROOMS = 8;
|
|
15
|
-
const OPEN = 1;
|
|
16
|
-
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
17
|
-
class Live {
|
|
18
|
-
constructor(record) {
|
|
19
|
-
this.record = record;
|
|
20
|
-
this.synced = false;
|
|
21
|
-
this.online = new Set();
|
|
22
|
-
this.state = new Map();
|
|
23
|
-
/** Asks to join, with the invitation each came with. */
|
|
24
|
-
this.asks = new Map();
|
|
25
|
-
this.done = new Map();
|
|
26
|
-
this.ready = [];
|
|
27
|
-
this.attempts = 0;
|
|
28
|
-
/** Connect again at once, whatever the close said. */
|
|
29
|
-
this.retry = false;
|
|
30
|
-
this.awaitingPong = false;
|
|
31
|
-
/** Events are handled one after another: opening them is async. */
|
|
32
|
-
this.chain = Promise.resolve();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
8
|
/**
|
|
36
9
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
37
10
|
* and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
|
|
38
11
|
* the others; every message signed by its member and sealed with the key of the room.
|
|
39
12
|
*/
|
|
40
|
-
class RelayHubs {
|
|
41
|
-
constructor(options) {
|
|
42
|
-
this.options = options;
|
|
43
|
-
this.live = new Map();
|
|
44
|
-
this.listeners = new Set();
|
|
45
|
-
/** Asks the user was told of while this runs: those the owner finds waiting when it connects are told once. */
|
|
46
|
-
this.told = new Set();
|
|
47
|
-
this.stopped = true;
|
|
48
|
-
this.nextId = 1;
|
|
49
|
-
}
|
|
13
|
+
class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
|
|
50
14
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
51
15
|
start() {
|
|
52
16
|
this.stopped = false;
|
|
53
17
|
for (const record of this.readRooms()) {
|
|
54
|
-
const live = new Live(record);
|
|
18
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
55
19
|
this.live.set(record.id, live);
|
|
56
20
|
if (record.status === 'member' || record.status === 'waiting') {
|
|
57
21
|
this.connect(live);
|
|
@@ -69,22 +33,6 @@ class RelayHubs {
|
|
|
69
33
|
this.listeners.add(listener);
|
|
70
34
|
return () => this.listeners.delete(listener);
|
|
71
35
|
}
|
|
72
|
-
/** This Ta's identity, made the first time. */
|
|
73
|
-
async identity() {
|
|
74
|
-
var _a, _b;
|
|
75
|
-
if (this.own) {
|
|
76
|
-
return this.own;
|
|
77
|
-
}
|
|
78
|
-
const kept = this.options.store.read('identity');
|
|
79
|
-
if (kept && (await relay_identity_1.RelayIdentityUtil.check(kept))) {
|
|
80
|
-
this.own = kept;
|
|
81
|
-
return kept;
|
|
82
|
-
}
|
|
83
|
-
const made = await relay_identity_1.RelayIdentityUtil.create(((_b = (_a = this.options).name) === null || _b === void 0 ? void 0 : _b.call(_a)) || 'Ta');
|
|
84
|
-
this.options.store.write('identity', made);
|
|
85
|
-
this.own = made;
|
|
86
|
-
return made;
|
|
87
|
-
}
|
|
88
36
|
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
89
37
|
async setName(name) {
|
|
90
38
|
const trimmed = String(name).trim().slice(0, 40);
|
|
@@ -143,7 +91,7 @@ class RelayHubs {
|
|
|
143
91
|
createdAt: now,
|
|
144
92
|
joinedAt: now,
|
|
145
93
|
};
|
|
146
|
-
const live = new Live(record);
|
|
94
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
147
95
|
this.live.set(record.id, live);
|
|
148
96
|
this.save();
|
|
149
97
|
this.connect(live);
|
|
@@ -215,7 +163,7 @@ class RelayHubs {
|
|
|
215
163
|
if (known) {
|
|
216
164
|
this.disconnect(known);
|
|
217
165
|
}
|
|
218
|
-
const live = new Live(record);
|
|
166
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
219
167
|
this.live.set(record.id, live);
|
|
220
168
|
this.save();
|
|
221
169
|
this.connect(live);
|
|
@@ -284,7 +232,7 @@ class RelayHubs {
|
|
|
284
232
|
}
|
|
285
233
|
if (live.record.status === 'member') {
|
|
286
234
|
await this.ready(live).catch(() => undefined);
|
|
287
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced) {
|
|
235
|
+
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_connection_1.OPEN && live.synced) {
|
|
288
236
|
this.request(live, { t: live.record.own ? 'close' : 'leave' });
|
|
289
237
|
}
|
|
290
238
|
}
|
|
@@ -324,7 +272,7 @@ class RelayHubs {
|
|
|
324
272
|
const timer = setTimeout(() => {
|
|
325
273
|
live.done.delete(id);
|
|
326
274
|
reject(new Error('The relay did not answer.'));
|
|
327
|
-
}, READY_MS);
|
|
275
|
+
}, relay_hubs_connection_1.READY_MS);
|
|
328
276
|
live.done.set(id, answer => {
|
|
329
277
|
clearTimeout(timer);
|
|
330
278
|
resolve(answer);
|
|
@@ -383,7 +331,7 @@ class RelayHubs {
|
|
|
383
331
|
id: member.id,
|
|
384
332
|
name: member.name,
|
|
385
333
|
owner: !!member.owner,
|
|
386
|
-
online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
|
|
334
|
+
online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_connection_1.OPEN && live.synced : live.online.has(member.member),
|
|
387
335
|
self: member.member === record.member,
|
|
388
336
|
...(member.guest ? { guest: true } : {}),
|
|
389
337
|
}); });
|
|
@@ -396,453 +344,12 @@ class RelayHubs {
|
|
|
396
344
|
ownerName: record.owner.name,
|
|
397
345
|
status: record.status,
|
|
398
346
|
...(record.reason ? { reason: record.reason } : {}),
|
|
399
|
-
connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN && (live.synced || record.status === 'waiting'),
|
|
347
|
+
connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_connection_1.OPEN && (live.synced || record.status === 'waiting'),
|
|
400
348
|
members,
|
|
401
349
|
createdAt: record.createdAt,
|
|
402
350
|
...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
|
|
403
351
|
};
|
|
404
352
|
}
|
|
405
|
-
requestOf(live, ask, friends) {
|
|
406
|
-
return {
|
|
407
|
-
room: live.record.id,
|
|
408
|
-
title: live.record.title,
|
|
409
|
-
ask: ask.member,
|
|
410
|
-
id: ask.id,
|
|
411
|
-
name: ask.name,
|
|
412
|
-
friend: friends.has(ask.id),
|
|
413
|
-
...(ask.guest ? { guest: true } : {}),
|
|
414
|
-
at: ask.at,
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
/** Resolves once the room is connected and has its state; rejects after a while. */
|
|
418
|
-
ready(live) {
|
|
419
|
-
var _a;
|
|
420
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced) {
|
|
421
|
-
return Promise.resolve();
|
|
422
|
-
}
|
|
423
|
-
return new Promise((resolve, reject) => {
|
|
424
|
-
const timer = setTimeout(() => reject(new Error('The room cannot be reached now.')), READY_MS);
|
|
425
|
-
live.ready.push(() => {
|
|
426
|
-
clearTimeout(timer);
|
|
427
|
-
resolve();
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
request(live, request) {
|
|
432
|
-
var _a;
|
|
433
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== OPEN) {
|
|
434
|
-
throw new Error('The room cannot be reached now.');
|
|
435
|
-
}
|
|
436
|
-
live.socket.send(JSON.stringify(request));
|
|
437
|
-
}
|
|
438
|
-
async sendBody(live, body, to) {
|
|
439
|
-
await this.ready(live);
|
|
440
|
-
const identity = await this.identity();
|
|
441
|
-
const d = await relay_hub_util_1.RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
|
|
442
|
-
this.request(live, { t: 'send', d, ...(to ? { to } : {}) });
|
|
443
|
-
}
|
|
444
|
-
/** The roster with `change` made, its version one more, signed by the owner. */
|
|
445
|
-
async nextRoster(live, change) {
|
|
446
|
-
var _a, _b;
|
|
447
|
-
var _c, _d;
|
|
448
|
-
const identity = await this.identity();
|
|
449
|
-
const body = { v: ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.v) !== null && _c !== void 0 ? _c : 0) + 1, members: change((_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : []) };
|
|
450
|
-
const d = await relay_hub_util_1.RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
|
|
451
|
-
live.roster = body;
|
|
452
|
-
return { v: body.v, d };
|
|
453
|
-
}
|
|
454
|
-
async writeRoster(live, change) {
|
|
455
|
-
await this.ready(live);
|
|
456
|
-
this.request(live, { t: 'roster', roster: await this.nextRoster(live, change) });
|
|
457
|
-
}
|
|
458
|
-
connect(live) {
|
|
459
|
-
if (this.stopped) {
|
|
460
|
-
return;
|
|
461
|
-
}
|
|
462
|
-
const { record } = live;
|
|
463
|
-
const socket = new WebSocket(relay_pairing_1.RelayPairingUtil.websocketUrl(record.relay, relay_protocol_1.RELAY_HUB_PATH + record.id));
|
|
464
|
-
live.socket = socket;
|
|
465
|
-
live.synced = false;
|
|
466
|
-
socket.onopen = () => {
|
|
467
|
-
var _a;
|
|
468
|
-
live.attempts = 0;
|
|
469
|
-
void this.hello(live, socket);
|
|
470
|
-
live.pingTimer = setInterval(() => {
|
|
471
|
-
if (live.awaitingPong) {
|
|
472
|
-
socket.close(4408, 'The relay did not answer.');
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
live.awaitingPong = true;
|
|
476
|
-
socket.send(relay_protocol_1.RELAY_PING);
|
|
477
|
-
}, (_a = this.options.pingMs) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_PING_MS);
|
|
478
|
-
};
|
|
479
|
-
socket.onmessage = message => {
|
|
480
|
-
const text = String(message.data);
|
|
481
|
-
if (text === relay_protocol_1.RELAY_PONG) {
|
|
482
|
-
live.awaitingPong = false;
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
live.chain = live.chain.then(() => this.receive(live, text)).catch(() => undefined);
|
|
486
|
-
};
|
|
487
|
-
socket.onerror = () => undefined;
|
|
488
|
-
socket.onclose = event => {
|
|
489
|
-
if (live.socket !== socket) {
|
|
490
|
-
return;
|
|
491
|
-
}
|
|
492
|
-
clearInterval(live.pingTimer);
|
|
493
|
-
live.socket = undefined;
|
|
494
|
-
live.synced = false;
|
|
495
|
-
live.awaitingPong = false;
|
|
496
|
-
live.online.clear();
|
|
497
|
-
void live.chain.then(() => this.closed(live, event.code));
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
async hello(live, socket) {
|
|
501
|
-
var _a;
|
|
502
|
-
const { record } = live;
|
|
503
|
-
let request;
|
|
504
|
-
if (record.status === 'waiting') {
|
|
505
|
-
request =
|
|
506
|
-
record.asked || !record.invite
|
|
507
|
-
? { t: 'wait', ask: record.member, token: record.token }
|
|
508
|
-
: {
|
|
509
|
-
t: 'ask',
|
|
510
|
-
ask: record.member,
|
|
511
|
-
token: record.token,
|
|
512
|
-
proof: await relay_hub_util_1.RelayHubUtil.inviteProof(record.invite),
|
|
513
|
-
d: await relay_hub_util_1.RelayHubUtil.sealAsk(record.invite, await this.identity(), record.member, Date.now(), !!this.options.guest),
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
else {
|
|
517
|
-
(_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await relay_hub_util_1.RelayHubUtil.keys(record.key));
|
|
518
|
-
request = { t: record.own && !record.made ? 'create' : 'auth', member: record.member, token: record.token };
|
|
519
|
-
}
|
|
520
|
-
if (socket.readyState === OPEN) {
|
|
521
|
-
socket.send(JSON.stringify(request));
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
closed(live, code) {
|
|
525
|
-
var _a;
|
|
526
|
-
for (const resolve of live.done.values()) {
|
|
527
|
-
resolve({ ok: false, v: 0, error: 'gone' });
|
|
528
|
-
}
|
|
529
|
-
live.done.clear();
|
|
530
|
-
const { record } = live;
|
|
531
|
-
this.emit({ type: 'rooms' });
|
|
532
|
-
if (this.stopped || this.live.get(record.id) !== live) {
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
if ((code === relay_hub_1.RelayHubCloseCode.ANSWERED || live.retry) && (record.status === 'member' || record.status === 'waiting')) {
|
|
536
|
-
live.retry = false;
|
|
537
|
-
this.connect(live);
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
if (code === relay_hub_1.RelayHubCloseCode.REFUSED || code === relay_hub_1.RelayHubCloseCode.GONE || code === relay_hub_1.RelayHubCloseCode.REMOVED || code === relay_hub_1.RelayHubCloseCode.INVALID) {
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
if (record.status !== 'member' && record.status !== 'waiting') {
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
|
-
const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 1000) * 2 ** live.attempts, MAX_RECONNECT_MS);
|
|
547
|
-
live.attempts++;
|
|
548
|
-
clearTimeout(live.timer);
|
|
549
|
-
live.timer = setTimeout(() => this.connect(live), delay);
|
|
550
|
-
}
|
|
551
|
-
disconnect(live) {
|
|
552
|
-
clearTimeout(live.timer);
|
|
553
|
-
clearInterval(live.pingTimer);
|
|
554
|
-
const socket = live.socket;
|
|
555
|
-
live.socket = undefined;
|
|
556
|
-
socket === null || socket === void 0 ? void 0 : socket.close(1000, 'Bye.');
|
|
557
|
-
}
|
|
558
|
-
async receive(live, text) {
|
|
559
|
-
var _a;
|
|
560
|
-
var _b;
|
|
561
|
-
let event;
|
|
562
|
-
try {
|
|
563
|
-
event = JSON.parse(text);
|
|
564
|
-
}
|
|
565
|
-
catch (_c) {
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
const { record } = live;
|
|
569
|
-
switch (event.t) {
|
|
570
|
-
case 'welcome': {
|
|
571
|
-
if (record.own && !record.made) {
|
|
572
|
-
record.made = true;
|
|
573
|
-
this.save();
|
|
574
|
-
}
|
|
575
|
-
live.online = new Set(event.online);
|
|
576
|
-
live.asks.clear();
|
|
577
|
-
if (event.roster) {
|
|
578
|
-
await this.readRoster(live, event.roster.d);
|
|
579
|
-
}
|
|
580
|
-
for (const ask of (_b = event.asks) !== null && _b !== void 0 ? _b : []) {
|
|
581
|
-
await this.asked(live, ask.d, false);
|
|
582
|
-
}
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
case 'entries':
|
|
586
|
-
for (const entry of event.entries) {
|
|
587
|
-
await this.readEntry(live, entry, false);
|
|
588
|
-
}
|
|
589
|
-
return;
|
|
590
|
-
case 'synced':
|
|
591
|
-
live.synced = true;
|
|
592
|
-
for (const resolve of live.ready.splice(0)) {
|
|
593
|
-
resolve();
|
|
594
|
-
}
|
|
595
|
-
this.emit({ type: 'rooms' });
|
|
596
|
-
return;
|
|
597
|
-
case 'state':
|
|
598
|
-
await this.readEntry(live, event.entry, true);
|
|
599
|
-
return;
|
|
600
|
-
case 'msg':
|
|
601
|
-
await this.readMessage(live, event.from, event.d);
|
|
602
|
-
return;
|
|
603
|
-
case 'done':
|
|
604
|
-
(_a = live.done.get(event.id)) === null || _a === void 0 ? void 0 : _a({ ok: event.ok, v: event.v, error: event.error });
|
|
605
|
-
live.done.delete(event.id);
|
|
606
|
-
return;
|
|
607
|
-
case 'presence':
|
|
608
|
-
if (event.online) {
|
|
609
|
-
live.online.add(event.member);
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
live.online.delete(event.member);
|
|
613
|
-
}
|
|
614
|
-
this.emit({ type: 'rooms' });
|
|
615
|
-
return;
|
|
616
|
-
case 'roster':
|
|
617
|
-
await this.readRoster(live, event.roster.d);
|
|
618
|
-
this.emit({ type: 'rooms' });
|
|
619
|
-
return;
|
|
620
|
-
case 'asked':
|
|
621
|
-
await this.asked(live, event.ask.d, true);
|
|
622
|
-
return;
|
|
623
|
-
case 'left':
|
|
624
|
-
live.online.delete(event.member);
|
|
625
|
-
if (record.own) {
|
|
626
|
-
await this.writeRoster(live, members => members.filter(member => member.member !== event.member)).catch(() => undefined);
|
|
627
|
-
}
|
|
628
|
-
this.emit({ type: 'rooms' });
|
|
629
|
-
return;
|
|
630
|
-
case 'waiting':
|
|
631
|
-
if (!record.asked) {
|
|
632
|
-
record.asked = true;
|
|
633
|
-
this.save();
|
|
634
|
-
}
|
|
635
|
-
this.emit({ type: 'rooms' });
|
|
636
|
-
return;
|
|
637
|
-
case 'admitted':
|
|
638
|
-
await this.admitted(live, event.answer);
|
|
639
|
-
return;
|
|
640
|
-
case 'refused':
|
|
641
|
-
this.setStatus(live, 'refused');
|
|
642
|
-
return;
|
|
643
|
-
case 'error':
|
|
644
|
-
this.failed(live, event.code);
|
|
645
|
-
return;
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
failed(live, code) {
|
|
649
|
-
const { record } = live;
|
|
650
|
-
if (code === 'gone') {
|
|
651
|
-
this.setStatus(live, 'gone', record.status === 'waiting' ? 'invite' : 'closed');
|
|
652
|
-
}
|
|
653
|
-
else if (code === 'auth' && record.status === 'member') {
|
|
654
|
-
this.setStatus(live, 'gone', 'removed');
|
|
655
|
-
}
|
|
656
|
-
else if (code === 'invite' || (code === 'exists' && record.status === 'waiting')) {
|
|
657
|
-
this.setStatus(live, 'gone', 'invite');
|
|
658
|
-
}
|
|
659
|
-
else if (code === 'exists' && record.own && !record.made) {
|
|
660
|
-
// Made before, its first answer not heard: it is this Ta's, to connect to as a member.
|
|
661
|
-
record.made = true;
|
|
662
|
-
live.retry = true;
|
|
663
|
-
this.save();
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
setStatus(live, status, reason) {
|
|
667
|
-
live.record = { ...live.record, status, ...(reason ? { reason } : {}) };
|
|
668
|
-
if (status !== 'member' && status !== 'waiting') {
|
|
669
|
-
delete live.record.key;
|
|
670
|
-
delete live.record.invite;
|
|
671
|
-
this.disconnect(live);
|
|
672
|
-
}
|
|
673
|
-
this.save();
|
|
674
|
-
this.emit({ type: 'rooms' });
|
|
675
|
-
}
|
|
676
|
-
async admitted(live, answer) {
|
|
677
|
-
const { record } = live;
|
|
678
|
-
const opened = record.invite
|
|
679
|
-
? await relay_hub_util_1.RelayHubUtil.openAnswer(await this.identity(), record.id, record.member, record.owner.id, answer, record.invite)
|
|
680
|
-
: undefined;
|
|
681
|
-
if (!opened) {
|
|
682
|
-
// Not signed by the owner the link named: whoever answered, it is not that room.
|
|
683
|
-
this.setStatus(live, 'gone', 'invite');
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
live.record = {
|
|
687
|
-
...record,
|
|
688
|
-
key: opened.body.key,
|
|
689
|
-
kind: String(opened.body.kind),
|
|
690
|
-
title: String(opened.body.title).slice(0, 80),
|
|
691
|
-
...(opened.body.view ? { view: String(opened.body.view) } : {}),
|
|
692
|
-
owner: opened.owner,
|
|
693
|
-
status: 'member',
|
|
694
|
-
joinedAt: Date.now(),
|
|
695
|
-
};
|
|
696
|
-
delete live.record.invite;
|
|
697
|
-
delete live.record.asked;
|
|
698
|
-
live.keys = await relay_hub_util_1.RelayHubUtil.keys(opened.body.key);
|
|
699
|
-
this.save();
|
|
700
|
-
this.emit({ type: 'rooms' });
|
|
701
|
-
}
|
|
702
|
-
/** An ask to join: told as a request the first time this Ta hears it; `fresh` when it came now, not with the welcome. */
|
|
703
|
-
async asked(live, sealed, fresh) {
|
|
704
|
-
var _a;
|
|
705
|
-
for (const invite of [...((_a = live.record.invites) !== null && _a !== void 0 ? _a : [])].reverse()) {
|
|
706
|
-
const ask = await relay_hub_util_1.RelayHubUtil.openAsk(invite.secret, sealed);
|
|
707
|
-
if (ask) {
|
|
708
|
-
live.asks.set(ask.member, { body: ask, invite: invite.secret });
|
|
709
|
-
if (fresh || !this.told.has(ask.member)) {
|
|
710
|
-
this.told.add(ask.member);
|
|
711
|
-
this.emit({ type: 'request', request: this.requestOf(live, ask, this.friendIds()) });
|
|
712
|
-
this.emit({ type: 'rooms' });
|
|
713
|
-
}
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
async readRoster(live, sealed) {
|
|
719
|
-
var _a;
|
|
720
|
-
const owner = live.record.owner;
|
|
721
|
-
const keys = (_a = live.keys) !== null && _a !== void 0 ? _a : (live.record.key ? await relay_hub_util_1.RelayHubUtil.keys(live.record.key) : undefined);
|
|
722
|
-
if (!keys || !owner.sign) {
|
|
723
|
-
return;
|
|
724
|
-
}
|
|
725
|
-
live.keys = keys;
|
|
726
|
-
// Signed by the owner alone: its member id is in the roster it signs.
|
|
727
|
-
let ownerMember;
|
|
728
|
-
const signed = await relay_hub_util_1.RelayHubUtil.open(keys.seal, sealed, member => {
|
|
729
|
-
ownerMember = member;
|
|
730
|
-
return owner.sign;
|
|
731
|
-
});
|
|
732
|
-
if (!signed || !Array.isArray(signed.b.members) || (live.roster && signed.b.v < live.roster.v)) {
|
|
733
|
-
return;
|
|
734
|
-
}
|
|
735
|
-
const members = signed.b.members.filter(member => member && typeof member.member === 'string');
|
|
736
|
-
if (!members.some(member => member.owner && member.member === ownerMember && member.id === owner.id)) {
|
|
737
|
-
return;
|
|
738
|
-
}
|
|
739
|
-
live.roster = { v: signed.b.v, members };
|
|
740
|
-
const own = (await this.identity()).id;
|
|
741
|
-
this.meet(live.record.title, members.filter(member => member.id !== own && !member.guest));
|
|
742
|
-
if (!members.some(member => member.member === live.record.member) && !live.record.own && live.record.status === 'member') {
|
|
743
|
-
this.setStatus(live, 'gone', 'removed');
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
guestOf(live, member) {
|
|
747
|
-
var _a, _b;
|
|
748
|
-
return !!((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(other => other.member === member)) === null || _b === void 0 ? void 0 : _b.guest);
|
|
749
|
-
}
|
|
750
|
-
signOf(live) {
|
|
751
|
-
return member => { var _a, _b; return (_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(other => other.member === member)) === null || _b === void 0 ? void 0 : _b.sign; };
|
|
752
|
-
}
|
|
753
|
-
async readEntry(live, entry, tell) {
|
|
754
|
-
var _a, _b;
|
|
755
|
-
if (!live.keys) {
|
|
756
|
-
return;
|
|
757
|
-
}
|
|
758
|
-
const signed = await relay_hub_util_1.RelayHubUtil.open(live.keys.seal, entry.d, this.signOf(live));
|
|
759
|
-
if (!signed || signed.m !== entry.by || typeof signed.b.key !== 'string' || (await relay_hub_util_1.RelayHubUtil.keyId(live.keys, signed.b.key)) !== entry.k) {
|
|
760
|
-
return;
|
|
761
|
-
}
|
|
762
|
-
const before = live.state.get(signed.b.key);
|
|
763
|
-
if (before && before.version >= entry.v) {
|
|
764
|
-
return;
|
|
765
|
-
}
|
|
766
|
-
if (this.guestOf(live, entry.by) && !signed.b.key.startsWith(relay_room_chat_1.RELAY_CHAT_KEY)) {
|
|
767
|
-
// A guest only chats: what else it set is not taken, its version kept, so that setting it again goes on that.
|
|
768
|
-
live.state.set(signed.b.key, { value: (_a = before === null || before === void 0 ? void 0 : before.value) !== null && _a !== void 0 ? _a : null, version: entry.v, by: (_b = before === null || before === void 0 ? void 0 : before.by) !== null && _b !== void 0 ? _b : entry.by });
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
live.state.set(signed.b.key, { value: signed.b.value, version: entry.v, by: entry.by });
|
|
772
|
-
if (tell) {
|
|
773
|
-
this.emit({ type: 'state', room: live.record.id, key: signed.b.key, value: signed.b.value, version: entry.v, by: entry.by });
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
async readMessage(live, from, sealed) {
|
|
777
|
-
var _a;
|
|
778
|
-
var _b;
|
|
779
|
-
if (!live.keys) {
|
|
780
|
-
return;
|
|
781
|
-
}
|
|
782
|
-
const signed = await relay_hub_util_1.RelayHubUtil.open(live.keys.seal, sealed, this.signOf(live), { skewMs: SKEW_MS });
|
|
783
|
-
if (!signed || signed.m !== from) {
|
|
784
|
-
return;
|
|
785
|
-
}
|
|
786
|
-
const sender = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(member => member.member === from);
|
|
787
|
-
if ((sender === null || sender === void 0 ? void 0 : sender.guest) && typeof signed.b.name !== 'string') {
|
|
788
|
-
// A guest only chats, in the state: the messages of games are not its.
|
|
789
|
-
return;
|
|
790
|
-
}
|
|
791
|
-
if (typeof signed.b.name === 'string') {
|
|
792
|
-
if (live.record.own && sender) {
|
|
793
|
-
const name = signed.b.name.trim().slice(0, 40);
|
|
794
|
-
await this.writeRoster(live, members => members.map(member => (member.member === from ? { ...member, name } : member))).catch(() => undefined);
|
|
795
|
-
}
|
|
796
|
-
return;
|
|
797
|
-
}
|
|
798
|
-
this.emit({ type: 'message', room: live.record.id, from, name: (_b = sender === null || sender === void 0 ? void 0 : sender.name) !== null && _b !== void 0 ? _b : '', data: signed.b.d });
|
|
799
|
-
}
|
|
800
|
-
/** Those of a room are friends: kept with their keys and the rooms shared. */
|
|
801
|
-
meet(title, members) {
|
|
802
|
-
if (!members.length) {
|
|
803
|
-
return;
|
|
804
|
-
}
|
|
805
|
-
const friends = this.readFriends();
|
|
806
|
-
const now = Date.now();
|
|
807
|
-
for (const member of members) {
|
|
808
|
-
const known = friends.find(friend => friend.id === member.id);
|
|
809
|
-
if (known) {
|
|
810
|
-
known.name = member.name;
|
|
811
|
-
known.seenAt = now;
|
|
812
|
-
known.rooms = [...known.rooms.filter(room => room !== title), title].slice(-MAX_FRIEND_ROOMS);
|
|
813
|
-
}
|
|
814
|
-
else {
|
|
815
|
-
friends.push({ ...relay_identity_1.RelayIdentityUtil.publicOf(member), rooms: [title], metAt: now, seenAt: now });
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
this.options.store.write('friends', { friends });
|
|
819
|
-
}
|
|
820
|
-
friendIds() {
|
|
821
|
-
return new Set(this.readFriends().map(friend => friend.id));
|
|
822
|
-
}
|
|
823
|
-
readFriends() {
|
|
824
|
-
var _a;
|
|
825
|
-
const friends = (_a = this.options.store.read('friends')) === null || _a === void 0 ? void 0 : _a.friends;
|
|
826
|
-
return Array.isArray(friends) ? friends : [];
|
|
827
|
-
}
|
|
828
|
-
readRooms() {
|
|
829
|
-
var _a;
|
|
830
|
-
const rooms = (_a = this.options.store.read('rooms')) === null || _a === void 0 ? void 0 : _a.rooms;
|
|
831
|
-
return Array.isArray(rooms) ? rooms : [];
|
|
832
|
-
}
|
|
833
|
-
save() {
|
|
834
|
-
this.options.store.write('rooms', { rooms: [...this.live.values()].map(live => live.record) });
|
|
835
|
-
}
|
|
836
|
-
emit(event) {
|
|
837
|
-
for (const listener of this.listeners) {
|
|
838
|
-
try {
|
|
839
|
-
listener(event);
|
|
840
|
-
}
|
|
841
|
-
catch (_a) {
|
|
842
|
-
// A listener's failure is its own.
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
353
|
}
|
|
847
354
|
exports.RelayHubs = RelayHubs;
|
|
848
355
|
//# sourceMappingURL=relay-hubs.js.map
|
|
@@ -21,7 +21,31 @@ export interface RelayChatLine {
|
|
|
21
21
|
name: string;
|
|
22
22
|
text: string;
|
|
23
23
|
at: number;
|
|
24
|
+
/**
|
|
25
|
+
* Said by the double of the member, its Ta's agent, not by the member (docs/design/ta-link.md 4): as an answer to
|
|
26
|
+
* one who asked it, or for the member who sent it to say something.
|
|
27
|
+
*/
|
|
28
|
+
double?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Said by an agent of the member's Ta (one of ta-channels) that the member invited to the room: its id there and its
|
|
31
|
+
* name. Not with `double`.
|
|
32
|
+
*/
|
|
33
|
+
agent?: RelayChatAgent;
|
|
34
|
+
/**
|
|
35
|
+
* Whom it asks: members' doubles by their member ids, each one's Ta answering if the member lets it; agents in the room
|
|
36
|
+
* by `<member>/<agent id>`, their member's Ta answering.
|
|
37
|
+
*/
|
|
38
|
+
to?: string[];
|
|
39
|
+
/** The id of the line it answers. */
|
|
40
|
+
re?: string;
|
|
24
41
|
}
|
|
42
|
+
/** An agent of a member's Ta, in a room (RelayChatLine.agent). */
|
|
43
|
+
export interface RelayChatAgent {
|
|
44
|
+
id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
}
|
|
47
|
+
/** What a line adds to its text: who said it and to whom (RelayRoomChat.say). */
|
|
48
|
+
export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 're'>;
|
|
25
49
|
/** What is kept of a line: `from` is the key's. */
|
|
26
50
|
type StoredLine = Omit<RelayChatLine, 'from'>;
|
|
27
51
|
/** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
|
|
@@ -39,6 +63,8 @@ export declare namespace RelayRoomChatUtil {
|
|
|
39
63
|
} | undefined;
|
|
40
64
|
/** The lines of a part, if its value is one and was written by the member of its key. */
|
|
41
65
|
function lines(member: string, value: unknown, by: string): RelayChatLine[] | undefined;
|
|
66
|
+
/** What a stored line adds, as far as it is well formed. */
|
|
67
|
+
function extra(line: Record<string, unknown>): RelayChatLineExtra;
|
|
42
68
|
function stored(lines: RelayChatLine[]): StoredLine[];
|
|
43
69
|
function bytes(lines: StoredLine[]): number;
|
|
44
70
|
function id(): string;
|
|
@@ -67,11 +93,14 @@ export declare class RelayRoomChat {
|
|
|
67
93
|
load(): Promise<void>;
|
|
68
94
|
/** A key of the room set, as its event tells: whether it was of the chat. */
|
|
69
95
|
heard(key: string, value: RelayRoomChatValue): boolean;
|
|
70
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Says `text` as `self` (its member id and name), with what `extra` adds (its double says it, to whose doubles); set
|
|
98
|
+
* again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
|
|
99
|
+
*/
|
|
71
100
|
say(text: string, self: {
|
|
72
101
|
member: string;
|
|
73
102
|
name: string;
|
|
74
|
-
}): Promise<
|
|
103
|
+
}, extra?: RelayChatLineExtra): Promise<RelayChatLine | undefined>;
|
|
75
104
|
protected clear(key: string): Promise<void>;
|
|
76
105
|
protected latest(member: string): Part | undefined;
|
|
77
106
|
protected put(key: string, value: RelayRoomChatValue): void;
|