@celljs/ta-relay 4.11.0 → 4.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +26 -1
- package/README.md +26 -1
- package/esm/common/index.d.ts +5 -0
- package/esm/common/index.js +5 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-friends.d.ts +154 -0
- package/esm/common/relay-friends.js +482 -0
- package/esm/common/relay-hub-util.d.ts +9 -0
- package/esm/common/relay-hub-util.js +19 -2
- package/esm/common/relay-hubs-connection.d.ts +110 -0
- package/esm/common/relay-hubs-connection.js +508 -0
- package/esm/common/relay-hubs-protocol.d.ts +174 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +68 -216
- package/esm/common/relay-hubs.js +303 -507
- package/esm/common/relay-identity.d.ts +2 -0
- package/esm/common/relay-info.d.ts +44 -0
- package/esm/common/relay-info.js +94 -0
- package/esm/common/relay-protocol.d.ts +7 -3
- package/esm/common/relay-protocol.js +7 -3
- package/esm/common/relay-room-chat.d.ts +36 -2
- package/esm/common/relay-room-chat.js +32 -7
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +5 -0
- package/esm/guest/guest-i18n.js +10 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -4
- package/esm/guest/guest.js +39 -18
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-file-storage.d.ts +32 -0
- package/esm/node/relay-file-storage.js +101 -0
- package/esm/node/relay-host.d.ts +63 -28
- package/esm/node/relay-host.js +145 -52
- package/esm/node/relay-hub-files.d.ts +1 -1
- package/esm/node/relay-server.d.ts +37 -3
- package/esm/node/relay-server.js +193 -37
- package/esm/node/relay-store.d.ts +34 -6
- package/esm/node/relay-store.js +54 -22
- package/esm/node/relays-file.d.ts +139 -0
- package/esm/node/relays-file.js +373 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +5 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-friends.d.ts +155 -0
- package/lib/common/relay-friends.js +486 -0
- package/lib/common/relay-hub-util.d.ts +9 -0
- package/lib/common/relay-hub-util.js +19 -2
- package/lib/common/relay-hubs-connection.d.ts +111 -0
- package/lib/common/relay-hubs-connection.js +513 -0
- package/lib/common/relay-hubs-protocol.d.ts +175 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +68 -216
- package/lib/common/relay-hubs.js +311 -515
- package/lib/common/relay-identity.d.ts +2 -0
- package/lib/common/relay-info.d.ts +45 -0
- package/lib/common/relay-info.js +97 -0
- package/lib/common/relay-protocol.d.ts +7 -3
- package/lib/common/relay-protocol.js +8 -4
- package/lib/common/relay-room-chat.d.ts +36 -2
- package/lib/common/relay-room-chat.js +32 -7
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +5 -0
- package/lib/guest/guest-i18n.js +10 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -4
- package/lib/guest/guest.js +39 -18
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-file-storage.d.ts +33 -0
- package/lib/node/relay-file-storage.js +105 -0
- package/lib/node/relay-host.d.ts +63 -28
- package/lib/node/relay-host.js +146 -53
- package/lib/node/relay-hub-files.d.ts +1 -1
- package/lib/node/relay-server.d.ts +37 -3
- package/lib/node/relay-server.js +193 -37
- package/lib/node/relay-store.d.ts +34 -6
- package/lib/node/relay-store.js +54 -22
- package/lib/node/relays-file.d.ts +140 -0
- package/lib/node/relays-file.js +378 -0
- package/package.json +2 -2
- package/viewer/room.html +21 -14
package/lib/common/relay-hubs.js
CHANGED
|
@@ -1,57 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RelayHubs = void 0;
|
|
4
|
-
const
|
|
3
|
+
exports.RelayHubsUtil = exports.RelayHubs = void 0;
|
|
4
|
+
const relay_friends_1 = require("./relay-friends");
|
|
5
5
|
const relay_hub_util_1 = require("./relay-hub-util");
|
|
6
|
+
const relay_hubs_connection_1 = require("./relay-hubs-connection");
|
|
6
7
|
const relay_identity_1 = require("./relay-identity");
|
|
7
|
-
const relay_pairing_1 = require("./relay-pairing");
|
|
8
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
9
|
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
10
|
/**
|
|
36
11
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
37
12
|
* and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
|
|
38
|
-
* the others; every message signed by its member and sealed with the key of the room.
|
|
13
|
+
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
14
|
+
* their own (RelayFriends).
|
|
39
15
|
*/
|
|
40
|
-
class RelayHubs {
|
|
41
|
-
constructor(
|
|
42
|
-
|
|
43
|
-
this.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
16
|
+
class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.book = new relay_friends_1.RelayFriends({
|
|
20
|
+
identity: () => this.identity(),
|
|
21
|
+
list: () => this.list(),
|
|
22
|
+
ownerOf: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record.owner.id; },
|
|
23
|
+
memberOf: (room, member) => {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
const found = (_b = (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.roster) === null || _b === void 0 ? void 0 : _b.members.find(each => each.member === member);
|
|
26
|
+
return found && relay_identity_1.RelayIdentityUtil.publicOf(found);
|
|
27
|
+
},
|
|
28
|
+
asksOf: room => { var _a; var _b; return [...((_b = (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.asks.values()) !== null && _b !== void 0 ? _b : [])].map(ask => ask.body); },
|
|
29
|
+
create: input => this.create(input),
|
|
30
|
+
invite: (room, options) => this.invite(room, options),
|
|
31
|
+
join: link => this.join(link),
|
|
32
|
+
answer: (room, ask, accept) => this.answer(room, ask, accept),
|
|
33
|
+
remove: (room, member) => this.remove(room, member),
|
|
34
|
+
leave: room => this.leave(room),
|
|
35
|
+
getState: (room, prefix) => this.getState(room, prefix),
|
|
36
|
+
setState: (room, key, value) => this.setState(room, key, value),
|
|
37
|
+
offer: (room, member, link) => this.sendBody(this.member(room), { friend: link }, member),
|
|
38
|
+
}, this.options.store, event => this.notify(event));
|
|
49
39
|
}
|
|
50
40
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
51
41
|
start() {
|
|
52
42
|
this.stopped = false;
|
|
43
|
+
if (!this.options.guest) {
|
|
44
|
+
const migrated = relay_friends_1.RelayFriendsUtil.migrate(this.readRooms(), this.options.store.read('friends'));
|
|
45
|
+
if (migrated) {
|
|
46
|
+
this.options.store.write('rooms', { rooms: migrated.records });
|
|
47
|
+
this.options.store.write('friends', migrated.book);
|
|
48
|
+
}
|
|
49
|
+
this.book.start();
|
|
50
|
+
}
|
|
53
51
|
for (const record of this.readRooms()) {
|
|
54
|
-
const live = new Live(record);
|
|
52
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
55
53
|
this.live.set(record.id, live);
|
|
56
54
|
if (record.status === 'member' || record.status === 'waiting') {
|
|
57
55
|
this.connect(live);
|
|
@@ -60,6 +58,7 @@ class RelayHubs {
|
|
|
60
58
|
}
|
|
61
59
|
stop() {
|
|
62
60
|
this.stopped = true;
|
|
61
|
+
this.book.stop();
|
|
63
62
|
for (const live of this.live.values()) {
|
|
64
63
|
this.disconnect(live);
|
|
65
64
|
}
|
|
@@ -69,22 +68,6 @@ class RelayHubs {
|
|
|
69
68
|
this.listeners.add(listener);
|
|
70
69
|
return () => this.listeners.delete(listener);
|
|
71
70
|
}
|
|
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
71
|
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
89
72
|
async setName(name) {
|
|
90
73
|
const trimmed = String(name).trim().slice(0, 40);
|
|
@@ -95,7 +78,8 @@ class RelayHubs {
|
|
|
95
78
|
this.options.store.write('identity', identity);
|
|
96
79
|
this.own = identity;
|
|
97
80
|
for (const live of this.live.values()) {
|
|
98
|
-
|
|
81
|
+
// A room joined with another identity keeps that one's name.
|
|
82
|
+
if (live.record.status !== 'member' || live.record.identity) {
|
|
99
83
|
continue;
|
|
100
84
|
}
|
|
101
85
|
if (live.record.own) {
|
|
@@ -130,10 +114,11 @@ class RelayHubs {
|
|
|
130
114
|
const now = Date.now();
|
|
131
115
|
const record = {
|
|
132
116
|
id: relay_hub_util_1.RelayHubUtil.randomId(),
|
|
133
|
-
relay: this.options.relay().replace(/\/+$/, ''),
|
|
117
|
+
relay: this.options.relay(typeof input.use === 'string' && input.use ? input.use : undefined).replace(/\/+$/, ''),
|
|
134
118
|
kind,
|
|
135
119
|
title,
|
|
136
120
|
...(input.view ? { view: String(input.view) } : {}),
|
|
121
|
+
...(input.unlisted === true ? { unlisted: true } : {}),
|
|
137
122
|
member: relay_hub_util_1.RelayHubUtil.randomId(),
|
|
138
123
|
token: relay_hub_util_1.RelayHubUtil.randomKey(),
|
|
139
124
|
key: relay_hub_util_1.RelayHubUtil.randomKey(),
|
|
@@ -143,7 +128,7 @@ class RelayHubs {
|
|
|
143
128
|
createdAt: now,
|
|
144
129
|
joinedAt: now,
|
|
145
130
|
};
|
|
146
|
-
const live = new Live(record);
|
|
131
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
147
132
|
this.live.set(record.id, live);
|
|
148
133
|
this.save();
|
|
149
134
|
this.connect(live);
|
|
@@ -163,7 +148,7 @@ class RelayHubs {
|
|
|
163
148
|
var _a;
|
|
164
149
|
var _b, _c;
|
|
165
150
|
const live = this.owned(roomId);
|
|
166
|
-
const identity = await this.
|
|
151
|
+
const identity = await this.identityOf(live);
|
|
167
152
|
const linkOf = (invite) => ({
|
|
168
153
|
link: relay_hub_util_1.RelayHubUtil.link({
|
|
169
154
|
relay: live.record.relay,
|
|
@@ -199,10 +184,13 @@ class RelayHubs {
|
|
|
199
184
|
if (link.owner === identity.id) {
|
|
200
185
|
throw new Error('This is a room of your own.');
|
|
201
186
|
}
|
|
187
|
+
const friends = relay_friends_1.RelayFriendsUtil.isKind(link.kind);
|
|
202
188
|
const record = {
|
|
203
189
|
id: link.room,
|
|
204
190
|
relay: link.relay,
|
|
205
|
-
|
|
191
|
+
// A room of friends is known as one from its link: its Ta keeps it, whatever the owner says.
|
|
192
|
+
kind: friends ? relay_friends_1.RELAY_FRIENDS_KIND : '',
|
|
193
|
+
...(friends ? { unlisted: true } : {}),
|
|
206
194
|
title: link.title,
|
|
207
195
|
member: relay_hub_util_1.RelayHubUtil.randomId(),
|
|
208
196
|
token: relay_hub_util_1.RelayHubUtil.randomKey(),
|
|
@@ -215,7 +203,7 @@ class RelayHubs {
|
|
|
215
203
|
if (known) {
|
|
216
204
|
this.disconnect(known);
|
|
217
205
|
}
|
|
218
|
-
const live = new Live(record);
|
|
206
|
+
const live = new relay_hubs_connection_1.Live(record);
|
|
219
207
|
this.live.set(record.id, live);
|
|
220
208
|
this.save();
|
|
221
209
|
this.connect(live);
|
|
@@ -227,6 +215,9 @@ class RelayHubs {
|
|
|
227
215
|
const friends = this.friendIds();
|
|
228
216
|
const requests = [];
|
|
229
217
|
for (const live of this.live.values()) {
|
|
218
|
+
if (this.isFriends(live)) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
230
221
|
for (const ask of live.asks.values()) {
|
|
231
222
|
requests.push(this.requestOf(live, ask.body, friends));
|
|
232
223
|
}
|
|
@@ -235,6 +226,8 @@ class RelayHubs {
|
|
|
235
226
|
}
|
|
236
227
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
237
228
|
async answer(roomId, askId, accept) {
|
|
229
|
+
var _a;
|
|
230
|
+
var _b;
|
|
238
231
|
const live = this.owned(roomId);
|
|
239
232
|
const asked = live.asks.get(askId);
|
|
240
233
|
if (!asked) {
|
|
@@ -245,22 +238,23 @@ class RelayHubs {
|
|
|
245
238
|
this.request(live, { t: 'refuse', ask: askId });
|
|
246
239
|
}
|
|
247
240
|
else {
|
|
248
|
-
const identity = await this.
|
|
241
|
+
const identity = await this.identityOf(live);
|
|
249
242
|
const body = {
|
|
250
243
|
key: live.record.key,
|
|
251
244
|
kind: live.record.kind,
|
|
252
245
|
title: live.record.title,
|
|
253
246
|
...(live.record.view ? { view: live.record.view } : {}),
|
|
247
|
+
...(live.record.unlisted ? { unlisted: true } : {}),
|
|
254
248
|
};
|
|
255
249
|
const ask = asked.body;
|
|
250
|
+
const most = this.isFriends(live) ? relay_protocol_1.RELAY_FRIENDS_MAX + 1 : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS;
|
|
251
|
+
if (((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _b !== void 0 ? _b : 0) >= most) {
|
|
252
|
+
throw new Error('The room is full.');
|
|
253
|
+
}
|
|
256
254
|
const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
|
|
257
255
|
const member = { ...relay_identity_1.RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
|
|
258
256
|
const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
|
|
259
257
|
this.request(live, { t: 'admit', ask: askId, answer, roster });
|
|
260
|
-
// A guest of a browser is no Ta friend: its keys are of that browser alone.
|
|
261
|
-
if (!member.guest) {
|
|
262
|
-
this.meet(live.record.title, [member]);
|
|
263
|
-
}
|
|
264
258
|
}
|
|
265
259
|
live.asks.delete(askId);
|
|
266
260
|
this.emit({ type: 'rooms' });
|
|
@@ -284,7 +278,7 @@ class RelayHubs {
|
|
|
284
278
|
}
|
|
285
279
|
if (live.record.status === 'member') {
|
|
286
280
|
await this.ready(live).catch(() => undefined);
|
|
287
|
-
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced) {
|
|
281
|
+
if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_connection_1.OPEN && live.synced) {
|
|
288
282
|
this.request(live, { t: live.record.own ? 'close' : 'leave' });
|
|
289
283
|
}
|
|
290
284
|
}
|
|
@@ -315,7 +309,7 @@ class RelayHubs {
|
|
|
315
309
|
throw new Error('A key of 1 to 200 characters is needed.');
|
|
316
310
|
}
|
|
317
311
|
await this.ready(live);
|
|
318
|
-
const identity = await this.
|
|
312
|
+
const identity = await this.identityOf(live);
|
|
319
313
|
const keys = live.keys;
|
|
320
314
|
const d = await relay_hub_util_1.RelayHubUtil.seal(keys.seal, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
|
|
321
315
|
const k = await relay_hub_util_1.RelayHubUtil.keyId(keys, name);
|
|
@@ -324,7 +318,7 @@ class RelayHubs {
|
|
|
324
318
|
const timer = setTimeout(() => {
|
|
325
319
|
live.done.delete(id);
|
|
326
320
|
reject(new Error('The relay did not answer.'));
|
|
327
|
-
}, READY_MS);
|
|
321
|
+
}, relay_hubs_connection_1.READY_MS);
|
|
328
322
|
live.done.set(id, answer => {
|
|
329
323
|
clearTimeout(timer);
|
|
330
324
|
resolve(answer);
|
|
@@ -351,14 +345,182 @@ class RelayHubs {
|
|
|
351
345
|
}
|
|
352
346
|
return { ok: result.ok, version: result.v };
|
|
353
347
|
}
|
|
348
|
+
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
349
|
+
async exportIdentity() {
|
|
350
|
+
return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
354
|
+
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
355
|
+
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
356
|
+
*/
|
|
357
|
+
async importIdentity(bundle, options = {}) {
|
|
358
|
+
const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
|
|
359
|
+
if (!RelayHubsUtil.isPrivate(incoming) || !(await relay_identity_1.RelayIdentityUtil.check(incoming))) {
|
|
360
|
+
throw new Error('Not an identity of Ta.');
|
|
361
|
+
}
|
|
362
|
+
const own = await this.identity();
|
|
363
|
+
const switched = incoming.id !== own.id;
|
|
364
|
+
if (switched) {
|
|
365
|
+
for (const live of this.live.values()) {
|
|
366
|
+
if (!live.record.identity) {
|
|
367
|
+
live.record = { ...live.record, identity: own };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const shared = options.shared || own.shared;
|
|
372
|
+
const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
|
|
373
|
+
if (!shared) {
|
|
374
|
+
delete next.shared;
|
|
375
|
+
}
|
|
376
|
+
this.own = next;
|
|
377
|
+
this.options.store.write('identity', next);
|
|
378
|
+
let rooms = 0;
|
|
379
|
+
for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
|
|
380
|
+
if (this.upsert(record)) {
|
|
381
|
+
rooms++;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
|
|
385
|
+
this.save();
|
|
386
|
+
this.emit({ type: 'rooms' });
|
|
387
|
+
return { switched, rooms, friends };
|
|
388
|
+
}
|
|
389
|
+
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
390
|
+
async setShared(shared) {
|
|
391
|
+
const identity = { ...(await this.identity()) };
|
|
392
|
+
if (!!identity.shared === shared) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (shared) {
|
|
396
|
+
identity.shared = true;
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
delete identity.shared;
|
|
400
|
+
}
|
|
401
|
+
this.own = identity;
|
|
402
|
+
this.options.store.write('identity', identity);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
406
|
+
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
407
|
+
* as another member.
|
|
408
|
+
*/
|
|
409
|
+
upsert(record) {
|
|
410
|
+
if (!RelayHubsUtil.isRecord(record)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
const live = this.live.get(record.id);
|
|
414
|
+
if (!live) {
|
|
415
|
+
const added = new relay_hubs_connection_1.Live({ ...record });
|
|
416
|
+
this.live.set(record.id, added);
|
|
417
|
+
if (record.status === 'member' || record.status === 'waiting') {
|
|
418
|
+
this.connect(added);
|
|
419
|
+
}
|
|
420
|
+
this.save();
|
|
421
|
+
this.emit({ type: 'rooms' });
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
if (live.record.member !== record.member) {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
const merged = RelayHubsUtil.merge(live.record, record);
|
|
428
|
+
if (JSON.stringify(merged) === JSON.stringify(live.record)) {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
const was = live.record.status;
|
|
432
|
+
live.record = merged;
|
|
433
|
+
if (merged.status !== 'member' && merged.status !== 'waiting') {
|
|
434
|
+
this.disconnect(live);
|
|
435
|
+
}
|
|
436
|
+
else if (was !== merged.status || !live.socket) {
|
|
437
|
+
this.disconnect(live);
|
|
438
|
+
this.connect(live);
|
|
439
|
+
}
|
|
440
|
+
this.save();
|
|
441
|
+
this.emit({ type: 'rooms' });
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
445
|
+
forget(roomId, member) {
|
|
446
|
+
const live = this.live.get(roomId);
|
|
447
|
+
if (!live || live.record.member !== member) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
this.disconnect(live);
|
|
451
|
+
this.live.delete(roomId);
|
|
452
|
+
this.save();
|
|
453
|
+
this.emit({ type: 'rooms' });
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
/** Friends of the user's other computer, with those of this Ta. */
|
|
457
|
+
mergeFriends(friends) {
|
|
458
|
+
return this.book.merge(friends);
|
|
459
|
+
}
|
|
460
|
+
/** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
|
|
354
461
|
friends() {
|
|
355
|
-
return this.
|
|
462
|
+
return this.book.friends();
|
|
463
|
+
}
|
|
464
|
+
/** The friends and those being added, for the pages. */
|
|
465
|
+
listFriends() {
|
|
466
|
+
return this.book.list();
|
|
467
|
+
}
|
|
468
|
+
/** The room of friends of this Ta, made the first time: other features keep there what they give friends. */
|
|
469
|
+
friendRoom() {
|
|
470
|
+
return this.book.room();
|
|
471
|
+
}
|
|
472
|
+
/** The link (and code) that asks to be the user's friend; with `renew`, a new one, the one before no longer working. */
|
|
473
|
+
friendLink(renew = false) {
|
|
474
|
+
return this.book.link(renew);
|
|
475
|
+
}
|
|
476
|
+
/** Asks the one of a link to be friends, on the user's word. */
|
|
477
|
+
addFriend(link) {
|
|
478
|
+
return this.book.add(link);
|
|
479
|
+
}
|
|
480
|
+
/** Asks a member of a room the user is in to be friends. */
|
|
481
|
+
offerFriend(roomId, member) {
|
|
482
|
+
return this.book.offer(roomId, member);
|
|
356
483
|
}
|
|
484
|
+
friendRequests() {
|
|
485
|
+
return this.book.requests();
|
|
486
|
+
}
|
|
487
|
+
/** The user's answer to one asking to be friends. */
|
|
488
|
+
answerFriend(id, accept) {
|
|
489
|
+
return this.book.answer(id, accept);
|
|
490
|
+
}
|
|
491
|
+
/** No longer friends, on both sides. */
|
|
357
492
|
removeFriend(id) {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
return
|
|
493
|
+
return this.book.remove(id);
|
|
494
|
+
}
|
|
495
|
+
isFriends(live) {
|
|
496
|
+
return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
|
|
497
|
+
}
|
|
498
|
+
friendIds() {
|
|
499
|
+
return this.book.ids();
|
|
500
|
+
}
|
|
501
|
+
heardAsk(live, ask) {
|
|
502
|
+
if (this.isFriends(live)) {
|
|
503
|
+
this.book.asked(ask);
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
super.heardAsk(live, ask);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
heardMembers(members) {
|
|
510
|
+
if (!this.options.guest) {
|
|
511
|
+
this.book.heard(members);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
offered(live, from, link) {
|
|
515
|
+
if (!this.options.guest) {
|
|
516
|
+
this.book.offered(from, link, live.record.title);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
|
|
520
|
+
after(event) {
|
|
521
|
+
if (!this.options.guest && (event.type === 'rooms' || (event.type === 'state' && event.key.startsWith('back:')))) {
|
|
522
|
+
this.book.changed();
|
|
523
|
+
}
|
|
362
524
|
}
|
|
363
525
|
owned(roomId) {
|
|
364
526
|
const live = this.member(roomId);
|
|
@@ -383,7 +545,7 @@ class RelayHubs {
|
|
|
383
545
|
id: member.id,
|
|
384
546
|
name: member.name,
|
|
385
547
|
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),
|
|
548
|
+
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
549
|
self: member.member === record.member,
|
|
388
550
|
...(member.guest ? { guest: true } : {}),
|
|
389
551
|
}); });
|
|
@@ -392,457 +554,91 @@ class RelayHubs {
|
|
|
392
554
|
kind: record.kind,
|
|
393
555
|
title: record.title,
|
|
394
556
|
...(record.view ? { view: record.view } : {}),
|
|
557
|
+
...(record.unlisted ? { unlisted: true } : {}),
|
|
558
|
+
relay: record.relay,
|
|
395
559
|
owner: record.own,
|
|
396
560
|
ownerName: record.owner.name,
|
|
397
561
|
status: record.status,
|
|
398
562
|
...(record.reason ? { reason: record.reason } : {}),
|
|
399
|
-
connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN && (live.synced || record.status === 'waiting'),
|
|
563
|
+
connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_connection_1.OPEN && (live.synced || record.status === 'waiting'),
|
|
400
564
|
members,
|
|
401
565
|
createdAt: record.createdAt,
|
|
402
566
|
...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
|
|
403
567
|
};
|
|
404
568
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
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));
|
|
569
|
+
}
|
|
570
|
+
exports.RelayHubs = RelayHubs;
|
|
571
|
+
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
572
|
+
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
573
|
+
var RelayHubsUtil;
|
|
574
|
+
(function (RelayHubsUtil) {
|
|
575
|
+
function isPrivate(identity) {
|
|
576
|
+
const value = identity;
|
|
577
|
+
return (!!value &&
|
|
578
|
+
typeof value.id === 'string' &&
|
|
579
|
+
typeof value.name === 'string' &&
|
|
580
|
+
typeof value.sign === 'string' &&
|
|
581
|
+
typeof value.box === 'string' &&
|
|
582
|
+
!!value.signKey &&
|
|
583
|
+
typeof value.signKey === 'object' &&
|
|
584
|
+
!!value.boxKey &&
|
|
585
|
+
typeof value.boxKey === 'object');
|
|
586
|
+
}
|
|
587
|
+
RelayHubsUtil.isPrivate = isPrivate;
|
|
588
|
+
function isRecord(record) {
|
|
589
|
+
const value = record;
|
|
590
|
+
return (!!value &&
|
|
591
|
+
typeof value.id === 'string' &&
|
|
592
|
+
typeof value.relay === 'string' &&
|
|
593
|
+
typeof value.member === 'string' &&
|
|
594
|
+
typeof value.token === 'string' &&
|
|
595
|
+
typeof value.status === 'string' &&
|
|
596
|
+
value.status in STATUS_RANK &&
|
|
597
|
+
!!value.owner &&
|
|
598
|
+
typeof value.owner.id === 'string' &&
|
|
599
|
+
(value.identity === undefined || isPrivate(value.identity)));
|
|
600
|
+
}
|
|
601
|
+
RelayHubsUtil.isRecord = isRecord;
|
|
602
|
+
/**
|
|
603
|
+
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
604
|
+
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
605
|
+
*/
|
|
606
|
+
function merge(local, other) {
|
|
607
|
+
var _a, _b, _c, _d, _e, _f;
|
|
608
|
+
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
609
|
+
const behind = ahead === local ? other : local;
|
|
610
|
+
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
611
|
+
.filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
|
|
612
|
+
.sort((a, b) => a.expiresAt - b.expiresAt)
|
|
613
|
+
.slice(-3);
|
|
614
|
+
const merged = {
|
|
615
|
+
...behind,
|
|
616
|
+
...ahead,
|
|
617
|
+
kind: ahead.kind || behind.kind,
|
|
618
|
+
owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
|
|
619
|
+
createdAt: Math.min(local.createdAt, other.createdAt),
|
|
498
620
|
};
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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
|
-
};
|
|
621
|
+
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
622
|
+
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
623
|
+
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
624
|
+
delete merged.key;
|
|
625
|
+
delete merged.invites;
|
|
626
|
+
delete merged.joinedAt;
|
|
627
|
+
delete merged.identity;
|
|
628
|
+
delete merged.made;
|
|
629
|
+
if (merged.status !== 'waiting') {
|
|
630
|
+
delete merged.invite;
|
|
631
|
+
delete merged.asked;
|
|
515
632
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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(),
|
|
633
|
+
return {
|
|
634
|
+
...merged,
|
|
635
|
+
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
636
|
+
...(invites.length ? { invites } : {}),
|
|
637
|
+
...(local.made || other.made ? { made: true } : {}),
|
|
638
|
+
...(joinedAt ? { joinedAt } : {}),
|
|
639
|
+
...(identity ? { identity } : {}),
|
|
695
640
|
};
|
|
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
641
|
}
|
|
820
|
-
|
|
821
|
-
|
|
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
|
-
}
|
|
847
|
-
exports.RelayHubs = RelayHubs;
|
|
642
|
+
RelayHubsUtil.merge = merge;
|
|
643
|
+
})(RelayHubsUtil || (exports.RelayHubsUtil = RelayHubsUtil = {}));
|
|
848
644
|
//# sourceMappingURL=relay-hubs.js.map
|