@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/esm/common/relay-hubs.js
CHANGED
|
@@ -1,49 +1,13 @@
|
|
|
1
|
-
import { RelayHubCloseCode } from './relay-hub.js';
|
|
2
1
|
import { RelayHubUtil } from './relay-hub-util.js';
|
|
2
|
+
import { Live, OPEN, READY_MS, RelayHubsConnection } from './relay-hubs-connection.js';
|
|
3
3
|
import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
|
|
4
|
-
import { RelayPairingUtil } from './relay-pairing.js';
|
|
5
|
-
import { RELAY_HUB_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, } from './relay-protocol.js';
|
|
6
|
-
import { RELAY_CHAT_KEY } from './relay-room-chat.js';
|
|
7
|
-
const MAX_RECONNECT_MS = 30000;
|
|
8
|
-
const SKEW_MS = 5 * 60 * 1000;
|
|
9
4
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
10
|
-
const READY_MS = 15000;
|
|
11
|
-
const MAX_FRIEND_ROOMS = 8;
|
|
12
|
-
const OPEN = 1;
|
|
13
|
-
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
14
|
-
class Live {
|
|
15
|
-
constructor(record) {
|
|
16
|
-
this.record = record;
|
|
17
|
-
this.synced = false;
|
|
18
|
-
this.online = new Set();
|
|
19
|
-
this.state = new Map();
|
|
20
|
-
/** Asks to join, with the invitation each came with. */
|
|
21
|
-
this.asks = new Map();
|
|
22
|
-
this.done = new Map();
|
|
23
|
-
this.ready = [];
|
|
24
|
-
this.attempts = 0;
|
|
25
|
-
/** Connect again at once, whatever the close said. */
|
|
26
|
-
this.retry = false;
|
|
27
|
-
this.awaitingPong = false;
|
|
28
|
-
/** Events are handled one after another: opening them is async. */
|
|
29
|
-
this.chain = Promise.resolve();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
5
|
/**
|
|
33
6
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
34
7
|
* and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
|
|
35
8
|
* the others; every message signed by its member and sealed with the key of the room.
|
|
36
9
|
*/
|
|
37
|
-
export class RelayHubs {
|
|
38
|
-
constructor(options) {
|
|
39
|
-
this.options = options;
|
|
40
|
-
this.live = new Map();
|
|
41
|
-
this.listeners = new Set();
|
|
42
|
-
/** Asks the user was told of while this runs: those the owner finds waiting when it connects are told once. */
|
|
43
|
-
this.told = new Set();
|
|
44
|
-
this.stopped = true;
|
|
45
|
-
this.nextId = 1;
|
|
46
|
-
}
|
|
10
|
+
export class RelayHubs extends RelayHubsConnection {
|
|
47
11
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
48
12
|
start() {
|
|
49
13
|
this.stopped = false;
|
|
@@ -66,22 +30,6 @@ export class RelayHubs {
|
|
|
66
30
|
this.listeners.add(listener);
|
|
67
31
|
return () => this.listeners.delete(listener);
|
|
68
32
|
}
|
|
69
|
-
/** This Ta's identity, made the first time. */
|
|
70
|
-
async identity() {
|
|
71
|
-
var _a, _b;
|
|
72
|
-
if (this.own) {
|
|
73
|
-
return this.own;
|
|
74
|
-
}
|
|
75
|
-
const kept = this.options.store.read('identity');
|
|
76
|
-
if (kept && (await RelayIdentityUtil.check(kept))) {
|
|
77
|
-
this.own = kept;
|
|
78
|
-
return kept;
|
|
79
|
-
}
|
|
80
|
-
const made = await RelayIdentityUtil.create(((_b = (_a = this.options).name) === null || _b === void 0 ? void 0 : _b.call(_a)) || 'Ta');
|
|
81
|
-
this.options.store.write('identity', made);
|
|
82
|
-
this.own = made;
|
|
83
|
-
return made;
|
|
84
|
-
}
|
|
85
33
|
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
86
34
|
async setName(name) {
|
|
87
35
|
const trimmed = String(name).trim().slice(0, 40);
|
|
@@ -399,446 +347,5 @@ export class RelayHubs {
|
|
|
399
347
|
...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
|
|
400
348
|
};
|
|
401
349
|
}
|
|
402
|
-
requestOf(live, ask, friends) {
|
|
403
|
-
return {
|
|
404
|
-
room: live.record.id,
|
|
405
|
-
title: live.record.title,
|
|
406
|
-
ask: ask.member,
|
|
407
|
-
id: ask.id,
|
|
408
|
-
name: ask.name,
|
|
409
|
-
friend: friends.has(ask.id),
|
|
410
|
-
...(ask.guest ? { guest: true } : {}),
|
|
411
|
-
at: ask.at,
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
/** Resolves once the room is connected and has its state; rejects after a while. */
|
|
415
|
-
ready(live) {
|
|
416
|
-
var _a;
|
|
417
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced) {
|
|
418
|
-
return Promise.resolve();
|
|
419
|
-
}
|
|
420
|
-
return new Promise((resolve, reject) => {
|
|
421
|
-
const timer = setTimeout(() => reject(new Error('The room cannot be reached now.')), READY_MS);
|
|
422
|
-
live.ready.push(() => {
|
|
423
|
-
clearTimeout(timer);
|
|
424
|
-
resolve();
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
request(live, request) {
|
|
429
|
-
var _a;
|
|
430
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== OPEN) {
|
|
431
|
-
throw new Error('The room cannot be reached now.');
|
|
432
|
-
}
|
|
433
|
-
live.socket.send(JSON.stringify(request));
|
|
434
|
-
}
|
|
435
|
-
async sendBody(live, body, to) {
|
|
436
|
-
await this.ready(live);
|
|
437
|
-
const identity = await this.identity();
|
|
438
|
-
const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
|
|
439
|
-
this.request(live, { t: 'send', d, ...(to ? { to } : {}) });
|
|
440
|
-
}
|
|
441
|
-
/** The roster with `change` made, its version one more, signed by the owner. */
|
|
442
|
-
async nextRoster(live, change) {
|
|
443
|
-
var _a, _b;
|
|
444
|
-
var _c, _d;
|
|
445
|
-
const identity = await this.identity();
|
|
446
|
-
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 : []) };
|
|
447
|
-
const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
|
|
448
|
-
live.roster = body;
|
|
449
|
-
return { v: body.v, d };
|
|
450
|
-
}
|
|
451
|
-
async writeRoster(live, change) {
|
|
452
|
-
await this.ready(live);
|
|
453
|
-
this.request(live, { t: 'roster', roster: await this.nextRoster(live, change) });
|
|
454
|
-
}
|
|
455
|
-
connect(live) {
|
|
456
|
-
if (this.stopped) {
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
const { record } = live;
|
|
460
|
-
const socket = new WebSocket(RelayPairingUtil.websocketUrl(record.relay, RELAY_HUB_PATH + record.id));
|
|
461
|
-
live.socket = socket;
|
|
462
|
-
live.synced = false;
|
|
463
|
-
socket.onopen = () => {
|
|
464
|
-
var _a;
|
|
465
|
-
live.attempts = 0;
|
|
466
|
-
void this.hello(live, socket);
|
|
467
|
-
live.pingTimer = setInterval(() => {
|
|
468
|
-
if (live.awaitingPong) {
|
|
469
|
-
socket.close(4408, 'The relay did not answer.');
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
live.awaitingPong = true;
|
|
473
|
-
socket.send(RELAY_PING);
|
|
474
|
-
}, (_a = this.options.pingMs) !== null && _a !== void 0 ? _a : RELAY_PING_MS);
|
|
475
|
-
};
|
|
476
|
-
socket.onmessage = message => {
|
|
477
|
-
const text = String(message.data);
|
|
478
|
-
if (text === RELAY_PONG) {
|
|
479
|
-
live.awaitingPong = false;
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
live.chain = live.chain.then(() => this.receive(live, text)).catch(() => undefined);
|
|
483
|
-
};
|
|
484
|
-
socket.onerror = () => undefined;
|
|
485
|
-
socket.onclose = event => {
|
|
486
|
-
if (live.socket !== socket) {
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
clearInterval(live.pingTimer);
|
|
490
|
-
live.socket = undefined;
|
|
491
|
-
live.synced = false;
|
|
492
|
-
live.awaitingPong = false;
|
|
493
|
-
live.online.clear();
|
|
494
|
-
void live.chain.then(() => this.closed(live, event.code));
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
async hello(live, socket) {
|
|
498
|
-
var _a;
|
|
499
|
-
const { record } = live;
|
|
500
|
-
let request;
|
|
501
|
-
if (record.status === 'waiting') {
|
|
502
|
-
request =
|
|
503
|
-
record.asked || !record.invite
|
|
504
|
-
? { t: 'wait', ask: record.member, token: record.token }
|
|
505
|
-
: {
|
|
506
|
-
t: 'ask',
|
|
507
|
-
ask: record.member,
|
|
508
|
-
token: record.token,
|
|
509
|
-
proof: await RelayHubUtil.inviteProof(record.invite),
|
|
510
|
-
d: await RelayHubUtil.sealAsk(record.invite, await this.identity(), record.member, Date.now(), !!this.options.guest),
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
else {
|
|
514
|
-
(_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await RelayHubUtil.keys(record.key));
|
|
515
|
-
request = { t: record.own && !record.made ? 'create' : 'auth', member: record.member, token: record.token };
|
|
516
|
-
}
|
|
517
|
-
if (socket.readyState === OPEN) {
|
|
518
|
-
socket.send(JSON.stringify(request));
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
closed(live, code) {
|
|
522
|
-
var _a;
|
|
523
|
-
for (const resolve of live.done.values()) {
|
|
524
|
-
resolve({ ok: false, v: 0, error: 'gone' });
|
|
525
|
-
}
|
|
526
|
-
live.done.clear();
|
|
527
|
-
const { record } = live;
|
|
528
|
-
this.emit({ type: 'rooms' });
|
|
529
|
-
if (this.stopped || this.live.get(record.id) !== live) {
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
if ((code === RelayHubCloseCode.ANSWERED || live.retry) && (record.status === 'member' || record.status === 'waiting')) {
|
|
533
|
-
live.retry = false;
|
|
534
|
-
this.connect(live);
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
if (code === RelayHubCloseCode.REFUSED || code === RelayHubCloseCode.GONE || code === RelayHubCloseCode.REMOVED || code === RelayHubCloseCode.INVALID) {
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
if (record.status !== 'member' && record.status !== 'waiting') {
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 1000) * 2 ** live.attempts, MAX_RECONNECT_MS);
|
|
544
|
-
live.attempts++;
|
|
545
|
-
clearTimeout(live.timer);
|
|
546
|
-
live.timer = setTimeout(() => this.connect(live), delay);
|
|
547
|
-
}
|
|
548
|
-
disconnect(live) {
|
|
549
|
-
clearTimeout(live.timer);
|
|
550
|
-
clearInterval(live.pingTimer);
|
|
551
|
-
const socket = live.socket;
|
|
552
|
-
live.socket = undefined;
|
|
553
|
-
socket === null || socket === void 0 ? void 0 : socket.close(1000, 'Bye.');
|
|
554
|
-
}
|
|
555
|
-
async receive(live, text) {
|
|
556
|
-
var _a;
|
|
557
|
-
var _b;
|
|
558
|
-
let event;
|
|
559
|
-
try {
|
|
560
|
-
event = JSON.parse(text);
|
|
561
|
-
}
|
|
562
|
-
catch (_c) {
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
const { record } = live;
|
|
566
|
-
switch (event.t) {
|
|
567
|
-
case 'welcome': {
|
|
568
|
-
if (record.own && !record.made) {
|
|
569
|
-
record.made = true;
|
|
570
|
-
this.save();
|
|
571
|
-
}
|
|
572
|
-
live.online = new Set(event.online);
|
|
573
|
-
live.asks.clear();
|
|
574
|
-
if (event.roster) {
|
|
575
|
-
await this.readRoster(live, event.roster.d);
|
|
576
|
-
}
|
|
577
|
-
for (const ask of (_b = event.asks) !== null && _b !== void 0 ? _b : []) {
|
|
578
|
-
await this.asked(live, ask.d, false);
|
|
579
|
-
}
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
case 'entries':
|
|
583
|
-
for (const entry of event.entries) {
|
|
584
|
-
await this.readEntry(live, entry, false);
|
|
585
|
-
}
|
|
586
|
-
return;
|
|
587
|
-
case 'synced':
|
|
588
|
-
live.synced = true;
|
|
589
|
-
for (const resolve of live.ready.splice(0)) {
|
|
590
|
-
resolve();
|
|
591
|
-
}
|
|
592
|
-
this.emit({ type: 'rooms' });
|
|
593
|
-
return;
|
|
594
|
-
case 'state':
|
|
595
|
-
await this.readEntry(live, event.entry, true);
|
|
596
|
-
return;
|
|
597
|
-
case 'msg':
|
|
598
|
-
await this.readMessage(live, event.from, event.d);
|
|
599
|
-
return;
|
|
600
|
-
case 'done':
|
|
601
|
-
(_a = live.done.get(event.id)) === null || _a === void 0 ? void 0 : _a({ ok: event.ok, v: event.v, error: event.error });
|
|
602
|
-
live.done.delete(event.id);
|
|
603
|
-
return;
|
|
604
|
-
case 'presence':
|
|
605
|
-
if (event.online) {
|
|
606
|
-
live.online.add(event.member);
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
live.online.delete(event.member);
|
|
610
|
-
}
|
|
611
|
-
this.emit({ type: 'rooms' });
|
|
612
|
-
return;
|
|
613
|
-
case 'roster':
|
|
614
|
-
await this.readRoster(live, event.roster.d);
|
|
615
|
-
this.emit({ type: 'rooms' });
|
|
616
|
-
return;
|
|
617
|
-
case 'asked':
|
|
618
|
-
await this.asked(live, event.ask.d, true);
|
|
619
|
-
return;
|
|
620
|
-
case 'left':
|
|
621
|
-
live.online.delete(event.member);
|
|
622
|
-
if (record.own) {
|
|
623
|
-
await this.writeRoster(live, members => members.filter(member => member.member !== event.member)).catch(() => undefined);
|
|
624
|
-
}
|
|
625
|
-
this.emit({ type: 'rooms' });
|
|
626
|
-
return;
|
|
627
|
-
case 'waiting':
|
|
628
|
-
if (!record.asked) {
|
|
629
|
-
record.asked = true;
|
|
630
|
-
this.save();
|
|
631
|
-
}
|
|
632
|
-
this.emit({ type: 'rooms' });
|
|
633
|
-
return;
|
|
634
|
-
case 'admitted':
|
|
635
|
-
await this.admitted(live, event.answer);
|
|
636
|
-
return;
|
|
637
|
-
case 'refused':
|
|
638
|
-
this.setStatus(live, 'refused');
|
|
639
|
-
return;
|
|
640
|
-
case 'error':
|
|
641
|
-
this.failed(live, event.code);
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
failed(live, code) {
|
|
646
|
-
const { record } = live;
|
|
647
|
-
if (code === 'gone') {
|
|
648
|
-
this.setStatus(live, 'gone', record.status === 'waiting' ? 'invite' : 'closed');
|
|
649
|
-
}
|
|
650
|
-
else if (code === 'auth' && record.status === 'member') {
|
|
651
|
-
this.setStatus(live, 'gone', 'removed');
|
|
652
|
-
}
|
|
653
|
-
else if (code === 'invite' || (code === 'exists' && record.status === 'waiting')) {
|
|
654
|
-
this.setStatus(live, 'gone', 'invite');
|
|
655
|
-
}
|
|
656
|
-
else if (code === 'exists' && record.own && !record.made) {
|
|
657
|
-
// Made before, its first answer not heard: it is this Ta's, to connect to as a member.
|
|
658
|
-
record.made = true;
|
|
659
|
-
live.retry = true;
|
|
660
|
-
this.save();
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
setStatus(live, status, reason) {
|
|
664
|
-
live.record = { ...live.record, status, ...(reason ? { reason } : {}) };
|
|
665
|
-
if (status !== 'member' && status !== 'waiting') {
|
|
666
|
-
delete live.record.key;
|
|
667
|
-
delete live.record.invite;
|
|
668
|
-
this.disconnect(live);
|
|
669
|
-
}
|
|
670
|
-
this.save();
|
|
671
|
-
this.emit({ type: 'rooms' });
|
|
672
|
-
}
|
|
673
|
-
async admitted(live, answer) {
|
|
674
|
-
const { record } = live;
|
|
675
|
-
const opened = record.invite
|
|
676
|
-
? await RelayHubUtil.openAnswer(await this.identity(), record.id, record.member, record.owner.id, answer, record.invite)
|
|
677
|
-
: undefined;
|
|
678
|
-
if (!opened) {
|
|
679
|
-
// Not signed by the owner the link named: whoever answered, it is not that room.
|
|
680
|
-
this.setStatus(live, 'gone', 'invite');
|
|
681
|
-
return;
|
|
682
|
-
}
|
|
683
|
-
live.record = {
|
|
684
|
-
...record,
|
|
685
|
-
key: opened.body.key,
|
|
686
|
-
kind: String(opened.body.kind),
|
|
687
|
-
title: String(opened.body.title).slice(0, 80),
|
|
688
|
-
...(opened.body.view ? { view: String(opened.body.view) } : {}),
|
|
689
|
-
owner: opened.owner,
|
|
690
|
-
status: 'member',
|
|
691
|
-
joinedAt: Date.now(),
|
|
692
|
-
};
|
|
693
|
-
delete live.record.invite;
|
|
694
|
-
delete live.record.asked;
|
|
695
|
-
live.keys = await RelayHubUtil.keys(opened.body.key);
|
|
696
|
-
this.save();
|
|
697
|
-
this.emit({ type: 'rooms' });
|
|
698
|
-
}
|
|
699
|
-
/** An ask to join: told as a request the first time this Ta hears it; `fresh` when it came now, not with the welcome. */
|
|
700
|
-
async asked(live, sealed, fresh) {
|
|
701
|
-
var _a;
|
|
702
|
-
for (const invite of [...((_a = live.record.invites) !== null && _a !== void 0 ? _a : [])].reverse()) {
|
|
703
|
-
const ask = await RelayHubUtil.openAsk(invite.secret, sealed);
|
|
704
|
-
if (ask) {
|
|
705
|
-
live.asks.set(ask.member, { body: ask, invite: invite.secret });
|
|
706
|
-
if (fresh || !this.told.has(ask.member)) {
|
|
707
|
-
this.told.add(ask.member);
|
|
708
|
-
this.emit({ type: 'request', request: this.requestOf(live, ask, this.friendIds()) });
|
|
709
|
-
this.emit({ type: 'rooms' });
|
|
710
|
-
}
|
|
711
|
-
return;
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
async readRoster(live, sealed) {
|
|
716
|
-
var _a;
|
|
717
|
-
const owner = live.record.owner;
|
|
718
|
-
const keys = (_a = live.keys) !== null && _a !== void 0 ? _a : (live.record.key ? await RelayHubUtil.keys(live.record.key) : undefined);
|
|
719
|
-
if (!keys || !owner.sign) {
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
live.keys = keys;
|
|
723
|
-
// Signed by the owner alone: its member id is in the roster it signs.
|
|
724
|
-
let ownerMember;
|
|
725
|
-
const signed = await RelayHubUtil.open(keys.seal, sealed, member => {
|
|
726
|
-
ownerMember = member;
|
|
727
|
-
return owner.sign;
|
|
728
|
-
});
|
|
729
|
-
if (!signed || !Array.isArray(signed.b.members) || (live.roster && signed.b.v < live.roster.v)) {
|
|
730
|
-
return;
|
|
731
|
-
}
|
|
732
|
-
const members = signed.b.members.filter(member => member && typeof member.member === 'string');
|
|
733
|
-
if (!members.some(member => member.owner && member.member === ownerMember && member.id === owner.id)) {
|
|
734
|
-
return;
|
|
735
|
-
}
|
|
736
|
-
live.roster = { v: signed.b.v, members };
|
|
737
|
-
const own = (await this.identity()).id;
|
|
738
|
-
this.meet(live.record.title, members.filter(member => member.id !== own && !member.guest));
|
|
739
|
-
if (!members.some(member => member.member === live.record.member) && !live.record.own && live.record.status === 'member') {
|
|
740
|
-
this.setStatus(live, 'gone', 'removed');
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
guestOf(live, member) {
|
|
744
|
-
var _a, _b;
|
|
745
|
-
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);
|
|
746
|
-
}
|
|
747
|
-
signOf(live) {
|
|
748
|
-
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; };
|
|
749
|
-
}
|
|
750
|
-
async readEntry(live, entry, tell) {
|
|
751
|
-
var _a, _b;
|
|
752
|
-
if (!live.keys) {
|
|
753
|
-
return;
|
|
754
|
-
}
|
|
755
|
-
const signed = await RelayHubUtil.open(live.keys.seal, entry.d, this.signOf(live));
|
|
756
|
-
if (!signed || signed.m !== entry.by || typeof signed.b.key !== 'string' || (await RelayHubUtil.keyId(live.keys, signed.b.key)) !== entry.k) {
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
const before = live.state.get(signed.b.key);
|
|
760
|
-
if (before && before.version >= entry.v) {
|
|
761
|
-
return;
|
|
762
|
-
}
|
|
763
|
-
if (this.guestOf(live, entry.by) && !signed.b.key.startsWith(RELAY_CHAT_KEY)) {
|
|
764
|
-
// A guest only chats: what else it set is not taken, its version kept, so that setting it again goes on that.
|
|
765
|
-
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 });
|
|
766
|
-
return;
|
|
767
|
-
}
|
|
768
|
-
live.state.set(signed.b.key, { value: signed.b.value, version: entry.v, by: entry.by });
|
|
769
|
-
if (tell) {
|
|
770
|
-
this.emit({ type: 'state', room: live.record.id, key: signed.b.key, value: signed.b.value, version: entry.v, by: entry.by });
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
async readMessage(live, from, sealed) {
|
|
774
|
-
var _a;
|
|
775
|
-
var _b;
|
|
776
|
-
if (!live.keys) {
|
|
777
|
-
return;
|
|
778
|
-
}
|
|
779
|
-
const signed = await RelayHubUtil.open(live.keys.seal, sealed, this.signOf(live), { skewMs: SKEW_MS });
|
|
780
|
-
if (!signed || signed.m !== from) {
|
|
781
|
-
return;
|
|
782
|
-
}
|
|
783
|
-
const sender = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(member => member.member === from);
|
|
784
|
-
if ((sender === null || sender === void 0 ? void 0 : sender.guest) && typeof signed.b.name !== 'string') {
|
|
785
|
-
// A guest only chats, in the state: the messages of games are not its.
|
|
786
|
-
return;
|
|
787
|
-
}
|
|
788
|
-
if (typeof signed.b.name === 'string') {
|
|
789
|
-
if (live.record.own && sender) {
|
|
790
|
-
const name = signed.b.name.trim().slice(0, 40);
|
|
791
|
-
await this.writeRoster(live, members => members.map(member => (member.member === from ? { ...member, name } : member))).catch(() => undefined);
|
|
792
|
-
}
|
|
793
|
-
return;
|
|
794
|
-
}
|
|
795
|
-
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 });
|
|
796
|
-
}
|
|
797
|
-
/** Those of a room are friends: kept with their keys and the rooms shared. */
|
|
798
|
-
meet(title, members) {
|
|
799
|
-
if (!members.length) {
|
|
800
|
-
return;
|
|
801
|
-
}
|
|
802
|
-
const friends = this.readFriends();
|
|
803
|
-
const now = Date.now();
|
|
804
|
-
for (const member of members) {
|
|
805
|
-
const known = friends.find(friend => friend.id === member.id);
|
|
806
|
-
if (known) {
|
|
807
|
-
known.name = member.name;
|
|
808
|
-
known.seenAt = now;
|
|
809
|
-
known.rooms = [...known.rooms.filter(room => room !== title), title].slice(-MAX_FRIEND_ROOMS);
|
|
810
|
-
}
|
|
811
|
-
else {
|
|
812
|
-
friends.push({ ...RelayIdentityUtil.publicOf(member), rooms: [title], metAt: now, seenAt: now });
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
this.options.store.write('friends', { friends });
|
|
816
|
-
}
|
|
817
|
-
friendIds() {
|
|
818
|
-
return new Set(this.readFriends().map(friend => friend.id));
|
|
819
|
-
}
|
|
820
|
-
readFriends() {
|
|
821
|
-
var _a;
|
|
822
|
-
const friends = (_a = this.options.store.read('friends')) === null || _a === void 0 ? void 0 : _a.friends;
|
|
823
|
-
return Array.isArray(friends) ? friends : [];
|
|
824
|
-
}
|
|
825
|
-
readRooms() {
|
|
826
|
-
var _a;
|
|
827
|
-
const rooms = (_a = this.options.store.read('rooms')) === null || _a === void 0 ? void 0 : _a.rooms;
|
|
828
|
-
return Array.isArray(rooms) ? rooms : [];
|
|
829
|
-
}
|
|
830
|
-
save() {
|
|
831
|
-
this.options.store.write('rooms', { rooms: [...this.live.values()].map(live => live.record) });
|
|
832
|
-
}
|
|
833
|
-
emit(event) {
|
|
834
|
-
for (const listener of this.listeners) {
|
|
835
|
-
try {
|
|
836
|
-
listener(event);
|
|
837
|
-
}
|
|
838
|
-
catch (_a) {
|
|
839
|
-
// A listener's failure is its own.
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
350
|
}
|
|
844
351
|
//# 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;
|
|
@@ -25,14 +25,28 @@ export var RelayRoomChatUtil;
|
|
|
25
25
|
const lines = [];
|
|
26
26
|
for (const line of value) {
|
|
27
27
|
if (line && typeof line.id === 'string' && typeof line.text === 'string' && typeof line.at === 'number') {
|
|
28
|
-
lines.push({ id: line.id, from: member, name: typeof line.name === 'string' ? line.name : '', text: line.text, at: line.at });
|
|
28
|
+
lines.push({ id: line.id, from: member, name: typeof line.name === 'string' ? line.name : '', text: line.text, at: line.at, ...extra(line) });
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
return lines;
|
|
32
32
|
}
|
|
33
33
|
RelayRoomChatUtil.lines = lines;
|
|
34
|
+
/** What a stored line adds, as far as it is well formed. */
|
|
35
|
+
function extra(line) {
|
|
36
|
+
const to = Array.isArray(line.to) ? line.to.filter((each) => typeof each === 'string').slice(0, 16) : [];
|
|
37
|
+
const agent = line.agent;
|
|
38
|
+
return {
|
|
39
|
+
...(line.double === true ? { double: true } : {}),
|
|
40
|
+
...(agent && typeof agent.id === 'string' && agent.id && typeof agent.name === 'string' && line.double !== true
|
|
41
|
+
? { agent: { id: agent.id.slice(0, 64), name: agent.name.slice(0, 64) } }
|
|
42
|
+
: {}),
|
|
43
|
+
...(to.length ? { to } : {}),
|
|
44
|
+
...(typeof line.re === 'string' ? { re: line.re } : {}),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
RelayRoomChatUtil.extra = extra;
|
|
34
48
|
function stored(lines) {
|
|
35
|
-
return lines.map(({
|
|
49
|
+
return lines.map(({ from: _from, ...line }) => line);
|
|
36
50
|
}
|
|
37
51
|
RelayRoomChatUtil.stored = stored;
|
|
38
52
|
function bytes(lines) {
|
|
@@ -86,20 +100,30 @@ export class RelayRoomChat {
|
|
|
86
100
|
this.changed();
|
|
87
101
|
return true;
|
|
88
102
|
}
|
|
89
|
-
/**
|
|
90
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Says `text` as `self` (its member id and name), with what `extra` adds (its double says it, to whose doubles); set
|
|
105
|
+
* again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
|
|
106
|
+
*/
|
|
107
|
+
async say(text, self, extra = {}) {
|
|
91
108
|
var _a;
|
|
92
109
|
var _b, _c;
|
|
93
110
|
const said = text.trim().slice(0, RELAY_CHAT_TEXT_MAX);
|
|
94
111
|
if (!said) {
|
|
95
|
-
return;
|
|
112
|
+
return undefined;
|
|
96
113
|
}
|
|
97
114
|
for (let tries = 0; tries < TRIES; tries++) {
|
|
98
115
|
const latest = this.latest(self.member);
|
|
99
116
|
// After what it said last, even within the same millisecond: the lines of a member keep their order.
|
|
100
117
|
const last = latest === null || latest === void 0 ? void 0 : latest.lines[latest.lines.length - 1];
|
|
101
118
|
const at = Math.max(Date.now(), ((_b = last === null || last === void 0 ? void 0 : last.at) !== null && _b !== void 0 ? _b : 0) + 1);
|
|
102
|
-
const line = {
|
|
119
|
+
const line = {
|
|
120
|
+
id: RelayRoomChatUtil.id(),
|
|
121
|
+
from: self.member,
|
|
122
|
+
name: self.name,
|
|
123
|
+
text: said,
|
|
124
|
+
at,
|
|
125
|
+
...RelayRoomChatUtil.extra(extra),
|
|
126
|
+
};
|
|
103
127
|
const fits = latest && RelayRoomChatUtil.bytes(RelayRoomChatUtil.stored([...latest.lines, line])) <= RELAY_CHAT_PART_BYTES;
|
|
104
128
|
const part = !latest ? 0 : fits ? latest.part : latest.part + 1;
|
|
105
129
|
const lines = fits && latest ? [...latest.lines, line] : [line];
|
|
@@ -112,7 +136,7 @@ export class RelayRoomChat {
|
|
|
112
136
|
if (part >= RELAY_CHAT_PARTS) {
|
|
113
137
|
void this.clear(RelayRoomChatUtil.key(self.member, part - RELAY_CHAT_PARTS));
|
|
114
138
|
}
|
|
115
|
-
return;
|
|
139
|
+
return line;
|
|
116
140
|
}
|
|
117
141
|
await this.load();
|
|
118
142
|
}
|
package/esm/guest/guest-i18n.js
CHANGED
|
@@ -26,6 +26,9 @@ const zh = {
|
|
|
26
26
|
owner: '房主',
|
|
27
27
|
you: '我',
|
|
28
28
|
guest: '网页访客',
|
|
29
|
+
doubleOf: '{name}的分身',
|
|
30
|
+
double: '分身',
|
|
31
|
+
agentOf: '{name}的智能体',
|
|
29
32
|
rename: '修改我的名字',
|
|
30
33
|
leave: '退出房间',
|
|
31
34
|
leaveAsk: '退出「{title}」?退出后要有新的邀请才能再进来。',
|
|
@@ -65,6 +68,9 @@ const en = {
|
|
|
65
68
|
owner: 'Owner',
|
|
66
69
|
you: 'You',
|
|
67
70
|
guest: 'Web guest',
|
|
71
|
+
doubleOf: "{name}'s double",
|
|
72
|
+
double: 'Double',
|
|
73
|
+
agentOf: "{name}'s agent",
|
|
68
74
|
rename: 'Change my name',
|
|
69
75
|
leave: 'Leave the room',
|
|
70
76
|
leaveAsk: 'Leave “{title}”? You come back only by a new invitation.',
|