@celljs/ta-relay 4.20.0 → 4.21.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 +1 -0
- package/esm/common/index.js +1 -0
- package/esm/common/relay-account-client.d.ts +4 -0
- package/esm/common/relay-account-client.js +13 -3
- package/esm/common/relay-account.d.ts +17 -2
- package/esm/common/relay-account.js +15 -6
- package/esm/common/relay-accounts.d.ts +2 -0
- package/esm/common/relay-accounts.js +16 -2
- package/esm/common/relay-group-invites.d.ts +6 -2
- package/esm/common/relay-group-invites.js +6 -5
- package/esm/common/relay-hub-charter.d.ts +74 -0
- package/esm/common/relay-hub-charter.js +149 -0
- package/esm/common/relay-hub-epochs.d.ts +43 -0
- package/esm/common/relay-hub-epochs.js +92 -0
- package/esm/common/relay-hub-util.d.ts +39 -7
- package/esm/common/relay-hub-util.js +46 -15
- package/esm/common/relay-hub.d.ts +13 -0
- package/esm/common/relay-hub.js +81 -18
- package/esm/common/relay-hubs-admins.d.ts +27 -0
- package/esm/common/relay-hubs-admins.js +91 -0
- package/esm/common/relay-hubs-connection.d.ts +19 -63
- package/esm/common/relay-hubs-connection.js +100 -87
- package/esm/common/relay-hubs-identity.d.ts +7 -1
- package/esm/common/relay-hubs-identity.js +28 -1
- package/esm/common/relay-hubs-live.d.ts +58 -0
- package/esm/common/relay-hubs-live.js +27 -0
- package/esm/common/relay-hubs-protocol.d.ts +10 -0
- package/esm/common/relay-hubs-util.d.ts +9 -0
- package/esm/common/relay-hubs-util.js +23 -1
- package/esm/common/relay-hubs.d.ts +12 -15
- package/esm/common/relay-hubs.js +60 -57
- package/esm/common/relay-protocol.d.ts +14 -3
- package/esm/common/relay-protocol.js +1 -1
- package/esm/common/relay-room-chat.d.ts +6 -1
- package/esm/common/relay-room-chat.js +1 -0
- package/esm/node/relay-server-accounts.d.ts +1 -1
- package/esm/node/relay-server-accounts.js +1 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/relay-account-client.d.ts +4 -0
- package/lib/common/relay-account-client.js +13 -3
- package/lib/common/relay-account.d.ts +17 -2
- package/lib/common/relay-account.js +15 -6
- package/lib/common/relay-accounts.d.ts +2 -0
- package/lib/common/relay-accounts.js +16 -2
- package/lib/common/relay-group-invites.d.ts +6 -2
- package/lib/common/relay-group-invites.js +6 -5
- package/lib/common/relay-hub-charter.d.ts +75 -0
- package/lib/common/relay-hub-charter.js +152 -0
- package/lib/common/relay-hub-epochs.d.ts +44 -0
- package/lib/common/relay-hub-epochs.js +95 -0
- package/lib/common/relay-hub-util.d.ts +39 -7
- package/lib/common/relay-hub-util.js +46 -15
- package/lib/common/relay-hub.d.ts +13 -0
- package/lib/common/relay-hub.js +81 -18
- package/lib/common/relay-hubs-admins.d.ts +28 -0
- package/lib/common/relay-hubs-admins.js +95 -0
- package/lib/common/relay-hubs-connection.d.ts +19 -63
- package/lib/common/relay-hubs-connection.js +106 -94
- package/lib/common/relay-hubs-identity.d.ts +7 -1
- package/lib/common/relay-hubs-identity.js +28 -1
- package/lib/common/relay-hubs-live.d.ts +59 -0
- package/lib/common/relay-hubs-live.js +31 -0
- package/lib/common/relay-hubs-protocol.d.ts +10 -0
- package/lib/common/relay-hubs-util.d.ts +9 -0
- package/lib/common/relay-hubs-util.js +23 -1
- package/lib/common/relay-hubs.d.ts +12 -15
- package/lib/common/relay-hubs.js +66 -63
- package/lib/common/relay-protocol.d.ts +14 -3
- package/lib/common/relay-protocol.js +1 -1
- package/lib/common/relay-room-chat.d.ts +6 -1
- package/lib/common/relay-room-chat.js +1 -0
- package/lib/node/relay-server-accounts.d.ts +1 -1
- package/lib/node/relay-server-accounts.js +1 -1
- package/package.json +3 -3
- package/viewer/room.html +15 -15
|
@@ -1,38 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RelayHubsConnection =
|
|
3
|
+
exports.RelayHubsConnection = void 0;
|
|
4
4
|
const relay_friends_1 = require("./relay-friends");
|
|
5
5
|
const relay_hub_1 = require("./relay-hub");
|
|
6
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
7
|
+
const relay_hub_epochs_1 = require("./relay-hub-epochs");
|
|
6
8
|
const relay_hub_util_1 = require("./relay-hub-util");
|
|
9
|
+
const relay_hubs_live_1 = require("./relay-hubs-live");
|
|
10
|
+
const relay_hubs_util_1 = require("./relay-hubs-util");
|
|
7
11
|
const relay_identity_1 = require("./relay-identity");
|
|
8
12
|
const relay_pairing_1 = require("./relay-pairing");
|
|
9
13
|
const relay_protocol_1 = require("./relay-protocol");
|
|
10
14
|
const relay_room_chat_1 = require("./relay-room-chat");
|
|
11
|
-
const MAX_RECONNECT_MS = 30000;
|
|
12
|
-
const SKEW_MS = 5 * 60 * 1000;
|
|
13
|
-
/** How long a call waits for the room to be connected, or for the relay to answer. */
|
|
14
|
-
exports.READY_MS = 15000;
|
|
15
|
-
exports.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
|
-
exports.Live = Live;
|
|
36
15
|
/**
|
|
37
16
|
* The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
|
|
38
17
|
* connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
|
|
@@ -85,11 +64,11 @@ class RelayHubsConnection {
|
|
|
85
64
|
/** Resolves once the room is connected and has its state; rejects after a while. */
|
|
86
65
|
ready(live) {
|
|
87
66
|
var _a;
|
|
88
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) ===
|
|
67
|
+
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_live_1.OPEN && live.synced) {
|
|
89
68
|
return Promise.resolve();
|
|
90
69
|
}
|
|
91
70
|
return new Promise((resolve, reject) => {
|
|
92
|
-
const timer = setTimeout(() => reject(new Error('The room cannot be reached now.')),
|
|
71
|
+
const timer = setTimeout(() => reject(new Error('The room cannot be reached now.')), relay_hubs_live_1.READY_MS);
|
|
93
72
|
live.ready.push(() => {
|
|
94
73
|
clearTimeout(timer);
|
|
95
74
|
resolve();
|
|
@@ -98,7 +77,7 @@ class RelayHubsConnection {
|
|
|
98
77
|
}
|
|
99
78
|
request(live, request) {
|
|
100
79
|
var _a;
|
|
101
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !==
|
|
80
|
+
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== relay_hubs_live_1.OPEN) {
|
|
102
81
|
throw new Error('The room cannot be reached now.');
|
|
103
82
|
}
|
|
104
83
|
live.socket.send(JSON.stringify(request));
|
|
@@ -106,22 +85,51 @@ class RelayHubsConnection {
|
|
|
106
85
|
async sendBody(live, body, to) {
|
|
107
86
|
await this.ready(live);
|
|
108
87
|
const identity = await this.identityOf(live);
|
|
109
|
-
const d = await relay_hub_util_1.RelayHubUtil.
|
|
88
|
+
const d = await relay_hub_util_1.RelayHubUtil.sealIn(live.keys, identity, live.record.member, body);
|
|
110
89
|
this.request(live, { t: 'send', d, ...(to ? { to } : {}) });
|
|
111
90
|
}
|
|
112
|
-
/**
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
91
|
+
/** Whether this Ta lets in, takes out and invites: the owner, or an admin of the charter. */
|
|
92
|
+
manages(live) {
|
|
93
|
+
return live.record.own || this.isAdmin(live, live.record.member);
|
|
94
|
+
}
|
|
95
|
+
isAdmin(live, member) {
|
|
96
|
+
var _a;
|
|
97
|
+
return !!((_a = live.charter) === null || _a === void 0 ? void 0 : _a.admins.some(admin => admin.member === member));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The roster with `change` made, its version one more, signed by this Ta (the owner, or an admin). The owner's
|
|
101
|
+
* carries `charter` when it changes it, else the charter of before, its admins still members; an admin's, the one it has.
|
|
102
|
+
*/
|
|
103
|
+
async nextRoster(live, change, charter, rekey = false) {
|
|
104
|
+
var _a, _b, _c, _d;
|
|
105
|
+
var _e, _f, _g;
|
|
116
106
|
const identity = await this.identityOf(live);
|
|
117
|
-
const
|
|
118
|
-
|
|
107
|
+
const members = change((_e = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _e !== void 0 ? _e : []);
|
|
108
|
+
let sealed = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.charter;
|
|
109
|
+
if (live.record.own) {
|
|
110
|
+
const base = charter !== null && charter !== void 0 ? charter : live.charter;
|
|
111
|
+
const admins = base === null || base === void 0 ? void 0 : base.admins.filter(admin => members.some(member => member.member === admin.member && member.id === admin.id && !member.guest));
|
|
112
|
+
if (base && admins && (charter || admins.length !== base.admins.length)) {
|
|
113
|
+
const next = { ...base, admins, v: Math.max(base.v, (_f = (_c = live.charter) === null || _c === void 0 ? void 0 : _c.v) !== null && _f !== void 0 ? _f : 0) + 1 };
|
|
114
|
+
sealed = await relay_hub_charter_1.RelayHubCharterUtil.seal(identity, next);
|
|
115
|
+
live.charter = next;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const body = { v: ((_g = (_d = live.roster) === null || _d === void 0 ? void 0 : _d.v) !== null && _g !== void 0 ? _g : 0) + 1, members, ...(sealed ? { charter: sealed } : {}) };
|
|
119
|
+
// A new epoch once one is taken out or leaves (docs/design/ta-link.md 8.7): taken on when the roster comes back.
|
|
120
|
+
const { record } = live;
|
|
121
|
+
const epochs = rekey ? relay_hub_epochs_1.RelayHubEpochs.next(record.epochs) : record.epochs;
|
|
122
|
+
const keys = rekey ? await relay_hub_util_1.RelayHubUtil.keys(record.key, epochs) : live.keys;
|
|
123
|
+
const d = await relay_hub_util_1.RelayHubUtil.sealIn(keys, identity, record.member, body);
|
|
124
|
+
const k = epochs && relay_hub_epochs_1.RelayHubEpochs.latest(epochs)
|
|
125
|
+
? await relay_hub_epochs_1.RelayHubEpochs.seal(identity, record.member, record.id, epochs, members, live.sealedEpochs)
|
|
126
|
+
: undefined;
|
|
119
127
|
live.roster = body;
|
|
120
|
-
return { v: body.v, d };
|
|
128
|
+
return { v: body.v, d, ...(k ? { k } : {}) };
|
|
121
129
|
}
|
|
122
|
-
async writeRoster(live, change) {
|
|
130
|
+
async writeRoster(live, change, charter, rekey = false) {
|
|
123
131
|
await this.ready(live);
|
|
124
|
-
this.request(live, { t: 'roster', roster: await this.nextRoster(live, change) });
|
|
132
|
+
this.request(live, { t: 'roster', roster: await this.nextRoster(live, change, charter, rekey) });
|
|
125
133
|
}
|
|
126
134
|
connect(live) {
|
|
127
135
|
if (this.stopped) {
|
|
@@ -182,10 +190,10 @@ class RelayHubsConnection {
|
|
|
182
190
|
};
|
|
183
191
|
}
|
|
184
192
|
else {
|
|
185
|
-
(_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await relay_hub_util_1.RelayHubUtil.keys(record.key));
|
|
193
|
+
(_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await relay_hub_util_1.RelayHubUtil.keys(record.key, record.epochs));
|
|
186
194
|
request = { t: record.own && !record.made ? 'create' : 'auth', member: record.member, token: record.token };
|
|
187
195
|
}
|
|
188
|
-
if (socket.readyState ===
|
|
196
|
+
if (socket.readyState === relay_hubs_live_1.OPEN) {
|
|
189
197
|
socket.send(JSON.stringify(request));
|
|
190
198
|
}
|
|
191
199
|
}
|
|
@@ -211,7 +219,7 @@ class RelayHubsConnection {
|
|
|
211
219
|
if (record.status !== 'member' && record.status !== 'waiting') {
|
|
212
220
|
return;
|
|
213
221
|
}
|
|
214
|
-
const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 1000) * 2 ** live.attempts, MAX_RECONNECT_MS);
|
|
222
|
+
const delay = Math.min(((_a = this.options.reconnectDelayMs) !== null && _a !== void 0 ? _a : 1000) * 2 ** live.attempts, relay_hubs_live_1.MAX_RECONNECT_MS);
|
|
215
223
|
live.attempts++;
|
|
216
224
|
clearTimeout(live.timer);
|
|
217
225
|
live.timer = setTimeout(() => this.connect(live), delay);
|
|
@@ -243,7 +251,7 @@ class RelayHubsConnection {
|
|
|
243
251
|
live.online = new Set(event.online);
|
|
244
252
|
live.asks.clear();
|
|
245
253
|
if (event.roster) {
|
|
246
|
-
await this.readRoster(live, event.roster
|
|
254
|
+
await this.readRoster(live, event.roster);
|
|
247
255
|
}
|
|
248
256
|
for (const ask of (_b = event.asks) !== null && _b !== void 0 ? _b : []) {
|
|
249
257
|
await this.asked(live, ask.d, false);
|
|
@@ -282,7 +290,7 @@ class RelayHubsConnection {
|
|
|
282
290
|
this.emit({ type: 'rooms' });
|
|
283
291
|
return;
|
|
284
292
|
case 'roster':
|
|
285
|
-
await this.readRoster(live, event.roster
|
|
293
|
+
await this.readRoster(live, event.roster);
|
|
286
294
|
this.emit({ type: 'rooms' });
|
|
287
295
|
return;
|
|
288
296
|
case 'asked':
|
|
@@ -290,8 +298,9 @@ class RelayHubsConnection {
|
|
|
290
298
|
return;
|
|
291
299
|
case 'left':
|
|
292
300
|
live.online.delete(event.member);
|
|
293
|
-
|
|
294
|
-
|
|
301
|
+
// An admin's roster keeps every admin: one who left goes from it when the owner writes it.
|
|
302
|
+
if (record.own || (this.manages(live) && !this.isAdmin(live, event.member))) {
|
|
303
|
+
await this.writeRoster(live, members => members.filter(member => member.member !== event.member), undefined, true).catch(() => undefined);
|
|
295
304
|
}
|
|
296
305
|
this.emit({ type: 'rooms' });
|
|
297
306
|
return;
|
|
@@ -335,6 +344,7 @@ class RelayHubsConnection {
|
|
|
335
344
|
live.record = { ...live.record, status, ...(reason ? { reason } : {}) };
|
|
336
345
|
if (status !== 'member' && status !== 'waiting') {
|
|
337
346
|
delete live.record.key;
|
|
347
|
+
delete live.record.epochs;
|
|
338
348
|
delete live.record.invite;
|
|
339
349
|
this.disconnect(live);
|
|
340
350
|
}
|
|
@@ -351,9 +361,11 @@ class RelayHubsConnection {
|
|
|
351
361
|
this.setStatus(live, 'gone', 'invite');
|
|
352
362
|
return;
|
|
353
363
|
}
|
|
364
|
+
const epochs = relay_hub_epochs_1.RelayHubEpochs.kept(opened.body.epochs && typeof opened.body.epochs === 'object' ? opened.body.epochs : {});
|
|
354
365
|
live.record = {
|
|
355
366
|
...record,
|
|
356
367
|
key: opened.body.key,
|
|
368
|
+
...(Object.keys(epochs).length ? { epochs } : {}),
|
|
357
369
|
kind: relay_friends_1.RelayFriendsUtil.kindOf(String(opened.body.kind)),
|
|
358
370
|
title: String(opened.body.title).slice(0, 80),
|
|
359
371
|
...(opened.body.view ? { view: String(opened.body.view) } : {}),
|
|
@@ -364,7 +376,7 @@ class RelayHubsConnection {
|
|
|
364
376
|
};
|
|
365
377
|
delete live.record.invite;
|
|
366
378
|
delete live.record.asked;
|
|
367
|
-
live.keys = await relay_hub_util_1.RelayHubUtil.keys(opened.body.key);
|
|
379
|
+
live.keys = await relay_hub_util_1.RelayHubUtil.keys(opened.body.key, live.record.epochs);
|
|
368
380
|
this.save();
|
|
369
381
|
this.emit({ type: 'rooms' });
|
|
370
382
|
}
|
|
@@ -392,31 +404,47 @@ class RelayHubsConnection {
|
|
|
392
404
|
heardMembers(_members) { }
|
|
393
405
|
/** A member asks the user to be friends, by the link of its room of friends. */
|
|
394
406
|
offered(_live, _from, _link) { }
|
|
395
|
-
async readRoster(live,
|
|
407
|
+
async readRoster(live, roster) {
|
|
396
408
|
var _a;
|
|
397
|
-
|
|
398
|
-
const
|
|
409
|
+
var _b, _c, _d, _e;
|
|
410
|
+
const { record } = live;
|
|
411
|
+
const owner = record.owner;
|
|
412
|
+
const keys = (_b = live.keys) !== null && _b !== void 0 ? _b : (record.key ? await relay_hub_util_1.RelayHubUtil.keys(record.key, record.epochs) : undefined);
|
|
399
413
|
if (!keys || !owner.sign) {
|
|
400
414
|
return;
|
|
401
415
|
}
|
|
402
416
|
live.keys = keys;
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
});
|
|
409
|
-
if (!
|
|
417
|
+
// Sealed in an epoch this Ta lacks: its keys, sealed for it by the one who wrote the roster (docs/design/ta-link.md 8.7).
|
|
418
|
+
const epoch = relay_hub_epochs_1.RelayHubEpochs.epochOf(roster.d);
|
|
419
|
+
const taken = keys.ring.has(epoch) ? undefined : await relay_hub_epochs_1.RelayHubEpochs.open(await this.identityOf(live), record.member, record.id, epoch, roster.k);
|
|
420
|
+
const tried = taken ? await relay_hub_util_1.RelayHubUtil.keys(record.key, { ...record.epochs, ...taken.epochs }) : keys;
|
|
421
|
+
// Signed by the owner, or by an admin its charter names for a change of members alone.
|
|
422
|
+
const opened = await relay_hub_charter_1.RelayHubCharterUtil.openRoster(tried, roster.d, { id: owner.id, sign: owner.sign }, record.id);
|
|
423
|
+
if (!opened ||
|
|
424
|
+
opened.body.v < Math.max((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.v) !== null && _c !== void 0 ? _c : 0, (_d = record.rosterV) !== null && _d !== void 0 ? _d : 0) ||
|
|
425
|
+
(live.charter && opened.charter && opened.charter.v < live.charter.v) ||
|
|
426
|
+
(opened.byAdmin && !relay_hub_charter_1.RelayHubCharterUtil.adminMay(opened, live.roster && { members: live.roster.members, charter: live.charter })) ||
|
|
427
|
+
(taken && !relay_hub_epochs_1.RelayHubEpochs.sealedBy(taken.sealer, opened))) {
|
|
410
428
|
return;
|
|
411
429
|
}
|
|
412
|
-
const members =
|
|
413
|
-
|
|
414
|
-
|
|
430
|
+
const members = opened.body.members;
|
|
431
|
+
live.roster = opened.body;
|
|
432
|
+
live.charter = opened.charter;
|
|
433
|
+
if (taken || ((_e = record.rosterV) !== null && _e !== void 0 ? _e : 0) < opened.body.v) {
|
|
434
|
+
record.rosterV = opened.body.v;
|
|
435
|
+
if (taken) {
|
|
436
|
+
record.epochs = relay_hub_epochs_1.RelayHubEpochs.kept({ ...record.epochs, ...taken.epochs });
|
|
437
|
+
live.keys = tried;
|
|
438
|
+
}
|
|
439
|
+
this.save();
|
|
440
|
+
}
|
|
441
|
+
if (record.own && opened.byAdmin) {
|
|
442
|
+
// Signed again by the owner: a Ta of before checks the roster against the owner alone.
|
|
443
|
+
void this.writeRoster(live, same => same).catch(() => undefined);
|
|
415
444
|
}
|
|
416
|
-
live.roster = { v: signed.b.v, members };
|
|
417
445
|
const own = (await this.identityOf(live)).id;
|
|
418
446
|
this.heardMembers(members.filter(member => member.id !== own && !member.guest));
|
|
419
|
-
if (!members.some(member => member.member ===
|
|
447
|
+
if (!members.some(member => member.member === record.member) && !record.own && record.status === 'member') {
|
|
420
448
|
this.setStatus(live, 'gone', 'removed');
|
|
421
449
|
return;
|
|
422
450
|
}
|
|
@@ -424,34 +452,10 @@ class RelayHubsConnection {
|
|
|
424
452
|
void this.keepLook(live);
|
|
425
453
|
void this.keepPicture(live).catch(() => undefined);
|
|
426
454
|
}
|
|
455
|
+
/** The roster says this Ta as it is now: its look (RelayHubsIdentity). */
|
|
456
|
+
async keepLook(_live) { }
|
|
427
457
|
/** The state of the room has this Ta's picture as it is now (RelayHubs). */
|
|
428
458
|
async keepPicture(_live) { }
|
|
429
|
-
/**
|
|
430
|
-
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
431
|
-
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
432
|
-
*/
|
|
433
|
-
async keepLook(live) {
|
|
434
|
-
var _a, _b;
|
|
435
|
-
if (live.record.status !== 'member' || live.record.identity || !live.roster) {
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
const identity = await this.identity();
|
|
439
|
-
const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
|
|
440
|
-
const self = live.roster.members.find(member => member.member === live.record.member);
|
|
441
|
-
if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
if (live.record.own) {
|
|
445
|
-
await this.writeRoster(live, members => members.map(member => (member.member === self.member ? relay_identity_1.RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
448
|
-
const owner = live.roster.members.find(member => member.owner);
|
|
449
|
-
if (owner && live.toldLook !== look) {
|
|
450
|
-
await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
|
|
451
|
-
live.toldLook = look;
|
|
452
|
-
}, () => undefined);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
459
|
guestOf(live, member) {
|
|
456
460
|
var _a, _b;
|
|
457
461
|
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);
|
|
@@ -460,11 +464,12 @@ class RelayHubsConnection {
|
|
|
460
464
|
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; };
|
|
461
465
|
}
|
|
462
466
|
async readEntry(live, entry, tell) {
|
|
463
|
-
var _a
|
|
467
|
+
var _a;
|
|
468
|
+
var _b, _c, _d;
|
|
464
469
|
if (!live.keys) {
|
|
465
470
|
return;
|
|
466
471
|
}
|
|
467
|
-
const signed = await relay_hub_util_1.RelayHubUtil.
|
|
472
|
+
const signed = await relay_hub_util_1.RelayHubUtil.openIn(live.keys, entry.d, this.signOf(live));
|
|
468
473
|
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) {
|
|
469
474
|
return;
|
|
470
475
|
}
|
|
@@ -472,9 +477,14 @@ class RelayHubsConnection {
|
|
|
472
477
|
if (before && before.version >= entry.v) {
|
|
473
478
|
return;
|
|
474
479
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
480
|
+
const foreign = relay_hubs_util_1.RelayHubsUtil.foreign((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _b !== void 0 ? _b : [], signed.b.key, entry.by, signed.b.value);
|
|
481
|
+
if (foreign || (this.guestOf(live, entry.by) && !signed.b.key.startsWith(relay_room_chat_1.RELAY_CHAT_KEY))) {
|
|
482
|
+
// A guest only chats; a key naming a member is theirs alone (docs/design/ta-link.md 8.8): what else was set is
|
|
483
|
+
// not taken, its version kept, so that setting it again goes on that. The owner clears another's write there.
|
|
484
|
+
live.state.set(signed.b.key, { value: (_c = before === null || before === void 0 ? void 0 : before.value) !== null && _c !== void 0 ? _c : null, version: entry.v, by: (_d = before === null || before === void 0 ? void 0 : before.by) !== null && _d !== void 0 ? _d : entry.by });
|
|
485
|
+
if (foreign && live.record.own && signed.b.value !== null) {
|
|
486
|
+
this.foreignSet(live, signed.b.key);
|
|
487
|
+
}
|
|
478
488
|
return;
|
|
479
489
|
}
|
|
480
490
|
live.state.set(signed.b.key, { value: signed.b.value, version: entry.v, by: entry.by });
|
|
@@ -488,7 +498,7 @@ class RelayHubsConnection {
|
|
|
488
498
|
if (!live.keys) {
|
|
489
499
|
return;
|
|
490
500
|
}
|
|
491
|
-
const signed = await relay_hub_util_1.RelayHubUtil.
|
|
501
|
+
const signed = await relay_hub_util_1.RelayHubUtil.openIn(live.keys, sealed, this.signOf(live), { skewMs: relay_hubs_live_1.SKEW_MS });
|
|
492
502
|
if (!signed || signed.m !== from) {
|
|
493
503
|
return;
|
|
494
504
|
}
|
|
@@ -514,6 +524,8 @@ class RelayHubsConnection {
|
|
|
514
524
|
}
|
|
515
525
|
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 });
|
|
516
526
|
}
|
|
527
|
+
/** A member set a key naming another: the owner clears it (RelayHubs). */
|
|
528
|
+
foreignSet(_live, _key) { }
|
|
517
529
|
/** Ids of those the user chose as friends. */
|
|
518
530
|
friendIds() {
|
|
519
531
|
return new Set();
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type RelayFriends } from './relay-friends';
|
|
2
|
-
import {
|
|
2
|
+
import { RelayHubsConnection } from './relay-hubs-connection';
|
|
3
|
+
import { Live } from './relay-hubs-live';
|
|
3
4
|
import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol';
|
|
4
5
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
|
|
5
6
|
export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
|
|
@@ -20,6 +21,11 @@ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
|
|
|
20
21
|
* in the rooms this Ta is in now, and in those it connects to later.
|
|
21
22
|
*/
|
|
22
23
|
setPicture(picture: string | null): Promise<RelayIdentity>;
|
|
24
|
+
/**
|
|
25
|
+
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
26
|
+
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
27
|
+
*/
|
|
28
|
+
protected keepLook(live: Live): Promise<void>;
|
|
23
29
|
/**
|
|
24
30
|
* Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
|
|
25
31
|
* ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RelayHubsIdentity = void 0;
|
|
4
4
|
const relay_friends_1 = require("./relay-friends");
|
|
5
5
|
const relay_hubs_connection_1 = require("./relay-hubs-connection");
|
|
6
|
+
const relay_hubs_live_1 = require("./relay-hubs-live");
|
|
6
7
|
const relay_hubs_util_1 = require("./relay-hubs-util");
|
|
7
8
|
const relay_identity_1 = require("./relay-identity");
|
|
8
9
|
const relay_protocol_1 = require("./relay-protocol");
|
|
@@ -51,6 +52,32 @@ class RelayHubsIdentity extends relay_hubs_connection_1.RelayHubsConnection {
|
|
|
51
52
|
}
|
|
52
53
|
return identity;
|
|
53
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
57
|
+
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
58
|
+
*/
|
|
59
|
+
async keepLook(live) {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
if (live.record.status !== 'member' || live.record.identity || !live.roster) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const identity = await this.identity();
|
|
65
|
+
const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
|
|
66
|
+
const self = live.roster.members.find(member => member.member === live.record.member);
|
|
67
|
+
if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (live.record.own) {
|
|
71
|
+
await this.writeRoster(live, members => members.map(member => (member.member === self.member ? relay_identity_1.RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const owner = live.roster.members.find(member => member.owner);
|
|
75
|
+
if (owner && live.toldLook !== look) {
|
|
76
|
+
await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
|
|
77
|
+
live.toldLook = look;
|
|
78
|
+
}, () => undefined);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
54
81
|
/**
|
|
55
82
|
* Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
|
|
56
83
|
* ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
|
|
@@ -195,7 +222,7 @@ class RelayHubsIdentity extends relay_hubs_connection_1.RelayHubsConnection {
|
|
|
195
222
|
}
|
|
196
223
|
const live = this.live.get(record.id);
|
|
197
224
|
if (!live) {
|
|
198
|
-
const added = new
|
|
225
|
+
const added = new relay_hubs_live_1.Live({ ...record });
|
|
199
226
|
this.live.set(record.id, added);
|
|
200
227
|
if (record.status === 'member' || record.status === 'waiting') {
|
|
201
228
|
this.connect(added);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { RelayHubCharterBody } from './relay-hub-charter';
|
|
2
|
+
import type { RelayHubAskBody, RelayHubKeys, RelayHubRosterBody } from './relay-hub-util';
|
|
3
|
+
import type { RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol';
|
|
4
|
+
import type { RelayHubFailure } from './relay-protocol';
|
|
5
|
+
/** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
|
|
6
|
+
export interface MessageBody {
|
|
7
|
+
d?: unknown;
|
|
8
|
+
/** A member's new name, for the owner to put in the roster; with its look (RelayPublicIdentity.look), '' for none. */
|
|
9
|
+
name?: string;
|
|
10
|
+
look?: string;
|
|
11
|
+
/** Asks the one it is sent to to be friends: the link of the sender's room of friends (RelayFriends.offer). */
|
|
12
|
+
friend?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface StateBody {
|
|
15
|
+
key: string;
|
|
16
|
+
value: unknown;
|
|
17
|
+
}
|
|
18
|
+
export declare const MAX_RECONNECT_MS = 30000;
|
|
19
|
+
export declare const SKEW_MS: number;
|
|
20
|
+
/** How long a call waits for the room to be connected, or for the relay to answer. */
|
|
21
|
+
export declare const READY_MS = 15000;
|
|
22
|
+
export declare const OPEN = 1;
|
|
23
|
+
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
24
|
+
export declare class Live {
|
|
25
|
+
record: RelayRoomRecord;
|
|
26
|
+
socket?: WebSocket;
|
|
27
|
+
keys?: RelayHubKeys;
|
|
28
|
+
synced: boolean;
|
|
29
|
+
online: Set<string>;
|
|
30
|
+
roster?: RelayHubRosterBody;
|
|
31
|
+
/** The charter of the roster, opened: the admins and the rules (docs/design/ta-link.md 8). */
|
|
32
|
+
charter?: RelayHubCharterBody;
|
|
33
|
+
readonly state: Map<string, RelayRoomValue>;
|
|
34
|
+
/** Asks to join, with the invitation each came with. */
|
|
35
|
+
readonly asks: Map<string, {
|
|
36
|
+
body: RelayHubAskBody;
|
|
37
|
+
invite: string;
|
|
38
|
+
}>;
|
|
39
|
+
readonly done: Map<number, (result: {
|
|
40
|
+
ok: boolean;
|
|
41
|
+
v: number;
|
|
42
|
+
error?: RelayHubFailure;
|
|
43
|
+
}) => void>;
|
|
44
|
+
readonly ready: Array<() => void>;
|
|
45
|
+
attempts: number;
|
|
46
|
+
/** Connect again at once, whatever the close said. */
|
|
47
|
+
retry: boolean;
|
|
48
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
49
|
+
pingTimer?: ReturnType<typeof setInterval>;
|
|
50
|
+
awaitingPong: boolean;
|
|
51
|
+
/** The look this Ta last sent the owner, for the roster to say: once, whatever the owner does with it. */
|
|
52
|
+
toldLook?: string;
|
|
53
|
+
/** The keys of the latest epoch this Ta sealed for each member (RelayHubEpochs.seal). */
|
|
54
|
+
readonly sealedEpochs: Map<string, string>;
|
|
55
|
+
/** Events are handled one after another: opening them is async. */
|
|
56
|
+
chain: Promise<void>;
|
|
57
|
+
constructor(record: RelayRoomRecord);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=relay-hubs-live.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Live = exports.OPEN = exports.READY_MS = exports.SKEW_MS = exports.MAX_RECONNECT_MS = void 0;
|
|
4
|
+
exports.MAX_RECONNECT_MS = 30000;
|
|
5
|
+
exports.SKEW_MS = 5 * 60 * 1000;
|
|
6
|
+
/** How long a call waits for the room to be connected, or for the relay to answer. */
|
|
7
|
+
exports.READY_MS = 15000;
|
|
8
|
+
exports.OPEN = 1;
|
|
9
|
+
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
10
|
+
class Live {
|
|
11
|
+
constructor(record) {
|
|
12
|
+
this.record = record;
|
|
13
|
+
this.synced = false;
|
|
14
|
+
this.online = new Set();
|
|
15
|
+
this.state = new Map();
|
|
16
|
+
/** Asks to join, with the invitation each came with. */
|
|
17
|
+
this.asks = new Map();
|
|
18
|
+
this.done = new Map();
|
|
19
|
+
this.ready = [];
|
|
20
|
+
this.attempts = 0;
|
|
21
|
+
/** Connect again at once, whatever the close said. */
|
|
22
|
+
this.retry = false;
|
|
23
|
+
this.awaitingPong = false;
|
|
24
|
+
/** The keys of the latest epoch this Ta sealed for each member (RelayHubEpochs.seal). */
|
|
25
|
+
this.sealedEpochs = new Map();
|
|
26
|
+
/** Events are handled one after another: opening them is async. */
|
|
27
|
+
this.chain = Promise.resolve();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Live = Live;
|
|
31
|
+
//# sourceMappingURL=relay-hubs-live.js.map
|
|
@@ -14,6 +14,10 @@ export interface RelayRoomRecord {
|
|
|
14
14
|
token: string;
|
|
15
15
|
/** base64url, 32 bytes: the key of the room, once admitted. */
|
|
16
16
|
key?: string;
|
|
17
|
+
/** The keys of the epochs after the first, by number, the latest kept (docs/design/ta-link.md 8.7). */
|
|
18
|
+
epochs?: Record<string, string>;
|
|
19
|
+
/** The version of the latest roster taken: an older one, served again, is not. */
|
|
20
|
+
rosterV?: number;
|
|
17
21
|
/** The owner: all of it once admitted (its keys check the roster), its id and name from the link before. */
|
|
18
22
|
owner: Pick<RelayPublicIdentity, 'id' | 'name'> & Partial<RelayPublicIdentity>;
|
|
19
23
|
/** This Ta made the room. */
|
|
@@ -63,6 +67,8 @@ export interface RelayRoomMember {
|
|
|
63
67
|
/** The look of its double (RelayPublicIdentity.look). */
|
|
64
68
|
look?: string;
|
|
65
69
|
owner: boolean;
|
|
70
|
+
/** An admin the owner made (docs/design/ta-link.md 8). */
|
|
71
|
+
admin?: true;
|
|
66
72
|
online: boolean;
|
|
67
73
|
self: boolean;
|
|
68
74
|
/** In a browser, without Ta: it only chats (docs/design/ta-link.md 3.8). */
|
|
@@ -77,8 +83,12 @@ export interface RelayRoomInfo {
|
|
|
77
83
|
/** The relay it is on. */
|
|
78
84
|
relay: string;
|
|
79
85
|
owner: boolean;
|
|
86
|
+
/** This Ta is an admin of the room: it lets in, takes out and invites, as the owner. */
|
|
87
|
+
admin?: true;
|
|
80
88
|
/** Of a room this Ta made: the members it lets in at most. */
|
|
81
89
|
capacity?: number;
|
|
90
|
+
/** The rules its owner set, for all (RelayHubRules); none when it set none. */
|
|
91
|
+
rules?: Record<string, boolean>;
|
|
82
92
|
ownerName: string;
|
|
83
93
|
status: RelayRoomStatus;
|
|
84
94
|
reason?: RelayRoomRecord['reason'];
|
|
@@ -8,5 +8,14 @@ export declare namespace RelayHubsUtil {
|
|
|
8
8
|
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
9
9
|
*/
|
|
10
10
|
function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
|
|
11
|
+
/**
|
|
12
|
+
* Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
|
|
13
|
+
* a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
|
|
14
|
+
* clear it. Keys naming nobody (a game's) are anyone's.
|
|
15
|
+
*/
|
|
16
|
+
function foreign(members: readonly {
|
|
17
|
+
member: string;
|
|
18
|
+
owner?: boolean;
|
|
19
|
+
}[], key: string, by: string, value: unknown): boolean;
|
|
11
20
|
}
|
|
12
21
|
//# sourceMappingURL=relay-hubs-util.d.ts.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubsUtil = void 0;
|
|
4
|
+
const relay_hub_epochs_1 = require("./relay-hub-epochs");
|
|
4
5
|
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
5
6
|
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
6
7
|
var RelayHubsUtil;
|
|
@@ -37,7 +38,7 @@ var RelayHubsUtil;
|
|
|
37
38
|
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
38
39
|
*/
|
|
39
40
|
function merge(local, other) {
|
|
40
|
-
var _a, _b, _c, _d, _e, _f;
|
|
41
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
41
42
|
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
42
43
|
const behind = ahead === local ? other : local;
|
|
43
44
|
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
@@ -54,7 +55,12 @@ var RelayHubsUtil;
|
|
|
54
55
|
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
55
56
|
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
56
57
|
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
58
|
+
// The keys of the epochs either has (docs/design/ta-link.md 8.7), the latest kept.
|
|
59
|
+
const epochs = relay_hub_epochs_1.RelayHubEpochs.kept({ ...behind.epochs, ...ahead.epochs, ...local.epochs, ...other.epochs });
|
|
60
|
+
const rosterV = Math.max((_g = local.rosterV) !== null && _g !== void 0 ? _g : 0, (_h = other.rosterV) !== null && _h !== void 0 ? _h : 0);
|
|
57
61
|
delete merged.key;
|
|
62
|
+
delete merged.epochs;
|
|
63
|
+
delete merged.rosterV;
|
|
58
64
|
delete merged.invites;
|
|
59
65
|
delete merged.joinedAt;
|
|
60
66
|
delete merged.identity;
|
|
@@ -66,6 +72,8 @@ var RelayHubsUtil;
|
|
|
66
72
|
return {
|
|
67
73
|
...merged,
|
|
68
74
|
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
75
|
+
...(key && merged.status === 'member' && Object.keys(epochs).length ? { epochs } : {}),
|
|
76
|
+
...(rosterV ? { rosterV } : {}),
|
|
69
77
|
...(invites.length ? { invites } : {}),
|
|
70
78
|
...(local.made || other.made ? { made: true } : {}),
|
|
71
79
|
...(joinedAt ? { joinedAt } : {}),
|
|
@@ -73,5 +81,19 @@ var RelayHubsUtil;
|
|
|
73
81
|
};
|
|
74
82
|
}
|
|
75
83
|
RelayHubsUtil.merge = merge;
|
|
84
|
+
/**
|
|
85
|
+
* Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
|
|
86
|
+
* a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
|
|
87
|
+
* clear it. Keys naming nobody (a game's) are anyone's.
|
|
88
|
+
*/
|
|
89
|
+
function foreign(members, key, by, value) {
|
|
90
|
+
const parts = new Set(key.split(':'));
|
|
91
|
+
const named = members.find(member => parts.has(member.member));
|
|
92
|
+
if (!named || named.member === by) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return !(value === null && members.some(member => member.member === by && member.owner));
|
|
96
|
+
}
|
|
97
|
+
RelayHubsUtil.foreign = foreign;
|
|
76
98
|
})(RelayHubsUtil || (exports.RelayHubsUtil = RelayHubsUtil = {}));
|
|
77
99
|
//# sourceMappingURL=relay-hubs-util.js.map
|