@celljs/ta-relay 4.12.0 → 4.13.1
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 +3 -0
- package/esm/common/index.js +3 -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 +17 -5
- package/esm/common/relay-hubs-connection.js +40 -37
- package/esm/common/relay-hubs-protocol.d.ts +51 -7
- package/esm/common/relay-hubs.d.ts +67 -5
- package/esm/common/relay-hubs.js +305 -16
- 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 +6 -1
- package/esm/common/relay-room-chat.js +1 -0
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +2 -0
- package/esm/guest/guest-i18n.js +4 -0
- package/esm/guest/guest.js +9 -1
- 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-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 +3 -0
- package/lib/common/index.js +3 -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 +17 -5
- package/lib/common/relay-hubs-connection.js +40 -37
- package/lib/common/relay-hubs-protocol.d.ts +51 -7
- package/lib/common/relay-hubs.d.ts +67 -5
- package/lib/common/relay-hubs.js +306 -17
- 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 +6 -1
- package/lib/common/relay-room-chat.js +1 -0
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +2 -0
- package/lib/guest/guest-i18n.js +4 -0
- package/lib/guest/guest.js +9 -1
- 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-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 +10 -10
package/esm/common/relay-hubs.js
CHANGED
|
@@ -1,16 +1,50 @@
|
|
|
1
|
+
import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
|
|
1
2
|
import { RelayHubUtil } from './relay-hub-util.js';
|
|
2
3
|
import { Live, OPEN, READY_MS, RelayHubsConnection } from './relay-hubs-connection.js';
|
|
3
4
|
import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
|
|
5
|
+
import { RELAY_FRIENDS_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
4
6
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
5
7
|
/**
|
|
6
8
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
7
9
|
* and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
|
|
8
|
-
* the others; every message signed by its member and sealed with the key of the room.
|
|
10
|
+
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
11
|
+
* their own (RelayFriends).
|
|
9
12
|
*/
|
|
10
13
|
export class RelayHubs extends RelayHubsConnection {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
this.book = new RelayFriends({
|
|
17
|
+
identity: () => this.identity(),
|
|
18
|
+
list: () => this.list(),
|
|
19
|
+
ownerOf: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record.owner.id; },
|
|
20
|
+
memberOf: (room, member) => {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
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);
|
|
23
|
+
return found && RelayIdentityUtil.publicOf(found);
|
|
24
|
+
},
|
|
25
|
+
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); },
|
|
26
|
+
create: input => this.create(input),
|
|
27
|
+
invite: (room, options) => this.invite(room, options),
|
|
28
|
+
join: link => this.join(link),
|
|
29
|
+
answer: (room, ask, accept) => this.answer(room, ask, accept),
|
|
30
|
+
remove: (room, member) => this.remove(room, member),
|
|
31
|
+
leave: room => this.leave(room),
|
|
32
|
+
getState: (room, prefix) => this.getState(room, prefix),
|
|
33
|
+
setState: (room, key, value) => this.setState(room, key, value),
|
|
34
|
+
offer: (room, member, link) => this.sendBody(this.member(room), { friend: link }, member),
|
|
35
|
+
}, this.options.store, event => this.notify(event));
|
|
36
|
+
}
|
|
11
37
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
12
38
|
start() {
|
|
13
39
|
this.stopped = false;
|
|
40
|
+
if (!this.options.guest) {
|
|
41
|
+
const migrated = RelayFriendsUtil.migrate(this.readRooms(), this.options.store.read('friends'));
|
|
42
|
+
if (migrated) {
|
|
43
|
+
this.options.store.write('rooms', { rooms: migrated.records });
|
|
44
|
+
this.options.store.write('friends', migrated.book);
|
|
45
|
+
}
|
|
46
|
+
this.book.start();
|
|
47
|
+
}
|
|
14
48
|
for (const record of this.readRooms()) {
|
|
15
49
|
const live = new Live(record);
|
|
16
50
|
this.live.set(record.id, live);
|
|
@@ -21,6 +55,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
21
55
|
}
|
|
22
56
|
stop() {
|
|
23
57
|
this.stopped = true;
|
|
58
|
+
this.book.stop();
|
|
24
59
|
for (const live of this.live.values()) {
|
|
25
60
|
this.disconnect(live);
|
|
26
61
|
}
|
|
@@ -40,7 +75,8 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
40
75
|
this.options.store.write('identity', identity);
|
|
41
76
|
this.own = identity;
|
|
42
77
|
for (const live of this.live.values()) {
|
|
43
|
-
|
|
78
|
+
// A room joined with another identity keeps that one's name.
|
|
79
|
+
if (live.record.status !== 'member' || live.record.identity) {
|
|
44
80
|
continue;
|
|
45
81
|
}
|
|
46
82
|
if (live.record.own) {
|
|
@@ -75,10 +111,11 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
75
111
|
const now = Date.now();
|
|
76
112
|
const record = {
|
|
77
113
|
id: RelayHubUtil.randomId(),
|
|
78
|
-
relay: this.options.relay().replace(/\/+$/, ''),
|
|
114
|
+
relay: this.options.relay(typeof input.use === 'string' && input.use ? input.use : undefined).replace(/\/+$/, ''),
|
|
79
115
|
kind,
|
|
80
116
|
title,
|
|
81
117
|
...(input.view ? { view: String(input.view) } : {}),
|
|
118
|
+
...(input.unlisted === true ? { unlisted: true } : {}),
|
|
82
119
|
member: RelayHubUtil.randomId(),
|
|
83
120
|
token: RelayHubUtil.randomKey(),
|
|
84
121
|
key: RelayHubUtil.randomKey(),
|
|
@@ -108,7 +145,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
108
145
|
var _a;
|
|
109
146
|
var _b, _c;
|
|
110
147
|
const live = this.owned(roomId);
|
|
111
|
-
const identity = await this.
|
|
148
|
+
const identity = await this.identityOf(live);
|
|
112
149
|
const linkOf = (invite) => ({
|
|
113
150
|
link: RelayHubUtil.link({
|
|
114
151
|
relay: live.record.relay,
|
|
@@ -144,10 +181,13 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
144
181
|
if (link.owner === identity.id) {
|
|
145
182
|
throw new Error('This is a room of your own.');
|
|
146
183
|
}
|
|
184
|
+
const friends = RelayFriendsUtil.isKind(link.kind);
|
|
147
185
|
const record = {
|
|
148
186
|
id: link.room,
|
|
149
187
|
relay: link.relay,
|
|
150
|
-
|
|
188
|
+
// A room of friends is known as one from its link: its Ta keeps it, whatever the owner says.
|
|
189
|
+
kind: friends ? RELAY_FRIENDS_KIND : '',
|
|
190
|
+
...(friends ? { unlisted: true } : {}),
|
|
151
191
|
title: link.title,
|
|
152
192
|
member: RelayHubUtil.randomId(),
|
|
153
193
|
token: RelayHubUtil.randomKey(),
|
|
@@ -172,6 +212,9 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
172
212
|
const friends = this.friendIds();
|
|
173
213
|
const requests = [];
|
|
174
214
|
for (const live of this.live.values()) {
|
|
215
|
+
if (this.isFriends(live)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
175
218
|
for (const ask of live.asks.values()) {
|
|
176
219
|
requests.push(this.requestOf(live, ask.body, friends));
|
|
177
220
|
}
|
|
@@ -180,6 +223,8 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
180
223
|
}
|
|
181
224
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
182
225
|
async answer(roomId, askId, accept) {
|
|
226
|
+
var _a;
|
|
227
|
+
var _b;
|
|
183
228
|
const live = this.owned(roomId);
|
|
184
229
|
const asked = live.asks.get(askId);
|
|
185
230
|
if (!asked) {
|
|
@@ -190,22 +235,23 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
190
235
|
this.request(live, { t: 'refuse', ask: askId });
|
|
191
236
|
}
|
|
192
237
|
else {
|
|
193
|
-
const identity = await this.
|
|
238
|
+
const identity = await this.identityOf(live);
|
|
194
239
|
const body = {
|
|
195
240
|
key: live.record.key,
|
|
196
241
|
kind: live.record.kind,
|
|
197
242
|
title: live.record.title,
|
|
198
243
|
...(live.record.view ? { view: live.record.view } : {}),
|
|
244
|
+
...(live.record.unlisted ? { unlisted: true } : {}),
|
|
199
245
|
};
|
|
200
246
|
const ask = asked.body;
|
|
247
|
+
const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : RELAY_ROOM_MAX_MEMBERS;
|
|
248
|
+
if (((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _b !== void 0 ? _b : 0) >= most) {
|
|
249
|
+
throw new Error('The room is full.');
|
|
250
|
+
}
|
|
201
251
|
const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
|
|
202
252
|
const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
|
|
203
253
|
const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
|
|
204
254
|
this.request(live, { t: 'admit', ask: askId, answer, roster });
|
|
205
|
-
// A guest of a browser is no Ta friend: its keys are of that browser alone.
|
|
206
|
-
if (!member.guest) {
|
|
207
|
-
this.meet(live.record.title, [member]);
|
|
208
|
-
}
|
|
209
255
|
}
|
|
210
256
|
live.asks.delete(askId);
|
|
211
257
|
this.emit({ type: 'rooms' });
|
|
@@ -260,7 +306,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
260
306
|
throw new Error('A key of 1 to 200 characters is needed.');
|
|
261
307
|
}
|
|
262
308
|
await this.ready(live);
|
|
263
|
-
const identity = await this.
|
|
309
|
+
const identity = await this.identityOf(live);
|
|
264
310
|
const keys = live.keys;
|
|
265
311
|
const d = await RelayHubUtil.seal(keys.seal, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
|
|
266
312
|
const k = await RelayHubUtil.keyId(keys, name);
|
|
@@ -296,14 +342,182 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
296
342
|
}
|
|
297
343
|
return { ok: result.ok, version: result.v };
|
|
298
344
|
}
|
|
345
|
+
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
346
|
+
async exportIdentity() {
|
|
347
|
+
return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
351
|
+
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
352
|
+
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
353
|
+
*/
|
|
354
|
+
async importIdentity(bundle, options = {}) {
|
|
355
|
+
const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
|
|
356
|
+
if (!RelayHubsUtil.isPrivate(incoming) || !(await RelayIdentityUtil.check(incoming))) {
|
|
357
|
+
throw new Error('Not an identity of Ta.');
|
|
358
|
+
}
|
|
359
|
+
const own = await this.identity();
|
|
360
|
+
const switched = incoming.id !== own.id;
|
|
361
|
+
if (switched) {
|
|
362
|
+
for (const live of this.live.values()) {
|
|
363
|
+
if (!live.record.identity) {
|
|
364
|
+
live.record = { ...live.record, identity: own };
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const shared = options.shared || own.shared;
|
|
369
|
+
const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
|
|
370
|
+
if (!shared) {
|
|
371
|
+
delete next.shared;
|
|
372
|
+
}
|
|
373
|
+
this.own = next;
|
|
374
|
+
this.options.store.write('identity', next);
|
|
375
|
+
let rooms = 0;
|
|
376
|
+
for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
|
|
377
|
+
if (this.upsert(record)) {
|
|
378
|
+
rooms++;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
|
|
382
|
+
this.save();
|
|
383
|
+
this.emit({ type: 'rooms' });
|
|
384
|
+
return { switched, rooms, friends };
|
|
385
|
+
}
|
|
386
|
+
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
387
|
+
async setShared(shared) {
|
|
388
|
+
const identity = { ...(await this.identity()) };
|
|
389
|
+
if (!!identity.shared === shared) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (shared) {
|
|
393
|
+
identity.shared = true;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
delete identity.shared;
|
|
397
|
+
}
|
|
398
|
+
this.own = identity;
|
|
399
|
+
this.options.store.write('identity', identity);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
403
|
+
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
404
|
+
* as another member.
|
|
405
|
+
*/
|
|
406
|
+
upsert(record) {
|
|
407
|
+
if (!RelayHubsUtil.isRecord(record)) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
const live = this.live.get(record.id);
|
|
411
|
+
if (!live) {
|
|
412
|
+
const added = new Live({ ...record });
|
|
413
|
+
this.live.set(record.id, added);
|
|
414
|
+
if (record.status === 'member' || record.status === 'waiting') {
|
|
415
|
+
this.connect(added);
|
|
416
|
+
}
|
|
417
|
+
this.save();
|
|
418
|
+
this.emit({ type: 'rooms' });
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
if (live.record.member !== record.member) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
const merged = RelayHubsUtil.merge(live.record, record);
|
|
425
|
+
if (JSON.stringify(merged) === JSON.stringify(live.record)) {
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
const was = live.record.status;
|
|
429
|
+
live.record = merged;
|
|
430
|
+
if (merged.status !== 'member' && merged.status !== 'waiting') {
|
|
431
|
+
this.disconnect(live);
|
|
432
|
+
}
|
|
433
|
+
else if (was !== merged.status || !live.socket) {
|
|
434
|
+
this.disconnect(live);
|
|
435
|
+
this.connect(live);
|
|
436
|
+
}
|
|
437
|
+
this.save();
|
|
438
|
+
this.emit({ type: 'rooms' });
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
442
|
+
forget(roomId, member) {
|
|
443
|
+
const live = this.live.get(roomId);
|
|
444
|
+
if (!live || live.record.member !== member) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
this.disconnect(live);
|
|
448
|
+
this.live.delete(roomId);
|
|
449
|
+
this.save();
|
|
450
|
+
this.emit({ type: 'rooms' });
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
/** Friends of the user's other computer, with those of this Ta. */
|
|
454
|
+
mergeFriends(friends) {
|
|
455
|
+
return this.book.merge(friends);
|
|
456
|
+
}
|
|
457
|
+
/** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
|
|
299
458
|
friends() {
|
|
300
|
-
return this.
|
|
459
|
+
return this.book.friends();
|
|
460
|
+
}
|
|
461
|
+
/** The friends and those being added, for the pages. */
|
|
462
|
+
listFriends() {
|
|
463
|
+
return this.book.list();
|
|
464
|
+
}
|
|
465
|
+
/** The room of friends of this Ta, made the first time: other features keep there what they give friends. */
|
|
466
|
+
friendRoom() {
|
|
467
|
+
return this.book.room();
|
|
468
|
+
}
|
|
469
|
+
/** The link (and code) that asks to be the user's friend; with `renew`, a new one, the one before no longer working. */
|
|
470
|
+
friendLink(renew = false) {
|
|
471
|
+
return this.book.link(renew);
|
|
301
472
|
}
|
|
473
|
+
/** Asks the one of a link to be friends, on the user's word. */
|
|
474
|
+
addFriend(link) {
|
|
475
|
+
return this.book.add(link);
|
|
476
|
+
}
|
|
477
|
+
/** Asks a member of a room the user is in to be friends. */
|
|
478
|
+
offerFriend(roomId, member) {
|
|
479
|
+
return this.book.offer(roomId, member);
|
|
480
|
+
}
|
|
481
|
+
friendRequests() {
|
|
482
|
+
return this.book.requests();
|
|
483
|
+
}
|
|
484
|
+
/** The user's answer to one asking to be friends. */
|
|
485
|
+
answerFriend(id, accept) {
|
|
486
|
+
return this.book.answer(id, accept);
|
|
487
|
+
}
|
|
488
|
+
/** No longer friends, on both sides. */
|
|
302
489
|
removeFriend(id) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
return
|
|
490
|
+
return this.book.remove(id);
|
|
491
|
+
}
|
|
492
|
+
isFriends(live) {
|
|
493
|
+
return live.record.own && live.record.kind === RELAY_FRIENDS_KIND;
|
|
494
|
+
}
|
|
495
|
+
friendIds() {
|
|
496
|
+
return this.book.ids();
|
|
497
|
+
}
|
|
498
|
+
heardAsk(live, ask) {
|
|
499
|
+
if (this.isFriends(live)) {
|
|
500
|
+
this.book.asked(ask);
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
super.heardAsk(live, ask);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
heardMembers(members) {
|
|
507
|
+
if (!this.options.guest) {
|
|
508
|
+
this.book.heard(members);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
offered(live, from, link) {
|
|
512
|
+
if (!this.options.guest) {
|
|
513
|
+
this.book.offered(from, link, live.record.title);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
|
|
517
|
+
after(event) {
|
|
518
|
+
if (!this.options.guest && (event.type === 'rooms' || (event.type === 'state' && event.key.startsWith('back:')))) {
|
|
519
|
+
this.book.changed();
|
|
520
|
+
}
|
|
307
521
|
}
|
|
308
522
|
owned(roomId) {
|
|
309
523
|
const live = this.member(roomId);
|
|
@@ -337,6 +551,8 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
337
551
|
kind: record.kind,
|
|
338
552
|
title: record.title,
|
|
339
553
|
...(record.view ? { view: record.view } : {}),
|
|
554
|
+
...(record.unlisted ? { unlisted: true } : {}),
|
|
555
|
+
relay: record.relay,
|
|
340
556
|
owner: record.own,
|
|
341
557
|
ownerName: record.owner.name,
|
|
342
558
|
status: record.status,
|
|
@@ -348,4 +564,77 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
348
564
|
};
|
|
349
565
|
}
|
|
350
566
|
}
|
|
567
|
+
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
568
|
+
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
569
|
+
export var RelayHubsUtil;
|
|
570
|
+
(function (RelayHubsUtil) {
|
|
571
|
+
function isPrivate(identity) {
|
|
572
|
+
const value = identity;
|
|
573
|
+
return (!!value &&
|
|
574
|
+
typeof value.id === 'string' &&
|
|
575
|
+
typeof value.name === 'string' &&
|
|
576
|
+
typeof value.sign === 'string' &&
|
|
577
|
+
typeof value.box === 'string' &&
|
|
578
|
+
!!value.signKey &&
|
|
579
|
+
typeof value.signKey === 'object' &&
|
|
580
|
+
!!value.boxKey &&
|
|
581
|
+
typeof value.boxKey === 'object');
|
|
582
|
+
}
|
|
583
|
+
RelayHubsUtil.isPrivate = isPrivate;
|
|
584
|
+
function isRecord(record) {
|
|
585
|
+
const value = record;
|
|
586
|
+
return (!!value &&
|
|
587
|
+
typeof value.id === 'string' &&
|
|
588
|
+
typeof value.relay === 'string' &&
|
|
589
|
+
typeof value.member === 'string' &&
|
|
590
|
+
typeof value.token === 'string' &&
|
|
591
|
+
typeof value.status === 'string' &&
|
|
592
|
+
value.status in STATUS_RANK &&
|
|
593
|
+
!!value.owner &&
|
|
594
|
+
typeof value.owner.id === 'string' &&
|
|
595
|
+
(value.identity === undefined || isPrivate(value.identity)));
|
|
596
|
+
}
|
|
597
|
+
RelayHubsUtil.isRecord = isRecord;
|
|
598
|
+
/**
|
|
599
|
+
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
600
|
+
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
601
|
+
*/
|
|
602
|
+
function merge(local, other) {
|
|
603
|
+
var _a, _b, _c, _d, _e, _f;
|
|
604
|
+
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
605
|
+
const behind = ahead === local ? other : local;
|
|
606
|
+
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
607
|
+
.filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
|
|
608
|
+
.sort((a, b) => a.expiresAt - b.expiresAt)
|
|
609
|
+
.slice(-3);
|
|
610
|
+
const merged = {
|
|
611
|
+
...behind,
|
|
612
|
+
...ahead,
|
|
613
|
+
kind: ahead.kind || behind.kind,
|
|
614
|
+
owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
|
|
615
|
+
createdAt: Math.min(local.createdAt, other.createdAt),
|
|
616
|
+
};
|
|
617
|
+
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
618
|
+
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
619
|
+
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
620
|
+
delete merged.key;
|
|
621
|
+
delete merged.invites;
|
|
622
|
+
delete merged.joinedAt;
|
|
623
|
+
delete merged.identity;
|
|
624
|
+
delete merged.made;
|
|
625
|
+
if (merged.status !== 'waiting') {
|
|
626
|
+
delete merged.invite;
|
|
627
|
+
delete merged.asked;
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
...merged,
|
|
631
|
+
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
632
|
+
...(invites.length ? { invites } : {}),
|
|
633
|
+
...(local.made || other.made ? { made: true } : {}),
|
|
634
|
+
...(joinedAt ? { joinedAt } : {}),
|
|
635
|
+
...(identity ? { identity } : {}),
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
RelayHubsUtil.merge = merge;
|
|
639
|
+
})(RelayHubsUtil || (RelayHubsUtil = {}));
|
|
351
640
|
//# sourceMappingURL=relay-hubs.js.map
|
|
@@ -13,6 +13,8 @@ export interface RelayIdentity extends RelayPublicIdentity {
|
|
|
13
13
|
signKey: JsonWebKey;
|
|
14
14
|
boxKey: JsonWebKey;
|
|
15
15
|
createdAt: number;
|
|
16
|
+
/** The user's other computers are this Ta too: who answers in a room is agreed first (docs/design/ta-sync.md 9). */
|
|
17
|
+
shared?: true;
|
|
16
18
|
}
|
|
17
19
|
/** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
|
|
18
20
|
export declare namespace RelayKeys {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** What a relay says of itself (docs/design/ta-relays.md 3.1): GET RELAY_INFO_PATH. */
|
|
2
|
+
export declare const RELAY_INFO_PATH = "/v1/info";
|
|
3
|
+
/** What a relay can do: forward to a host, hold rooms, keep sealed copies, serve the pages of links. */
|
|
4
|
+
export type RelayCapability = 'tunnel' | 'hub' | 'store' | 'pages';
|
|
5
|
+
export interface RelayLimits {
|
|
6
|
+
hubMembers: number;
|
|
7
|
+
/** Characters of a sealed message or value of a room. */
|
|
8
|
+
hubValueBytes: number;
|
|
9
|
+
hubKeys: number;
|
|
10
|
+
hubStateBytes: number;
|
|
11
|
+
hubIdleDays: number;
|
|
12
|
+
/** Bytes of a sealed copy, and its days at most. */
|
|
13
|
+
storeValueBytes: number;
|
|
14
|
+
storeDays: number;
|
|
15
|
+
}
|
|
16
|
+
export interface RelayInfo {
|
|
17
|
+
v: 1;
|
|
18
|
+
capabilities: RelayCapability[];
|
|
19
|
+
limits: RelayLimits;
|
|
20
|
+
/** Whether rooms and copies stay when the relay restarts. */
|
|
21
|
+
persistent: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** A relay that answers no RELAY_INFO_PATH, made before it: all it can do, with the limits of then. */
|
|
24
|
+
export declare const RELAY_LEGACY_INFO: RelayInfo;
|
|
25
|
+
/** What a probe of a relay found. */
|
|
26
|
+
export interface RelayProbe {
|
|
27
|
+
online: boolean;
|
|
28
|
+
info?: RelayInfo;
|
|
29
|
+
/** It answers, but not RELAY_INFO_PATH: one made before it. */
|
|
30
|
+
legacy?: boolean;
|
|
31
|
+
/** Milliseconds to its answer. */
|
|
32
|
+
ms?: number;
|
|
33
|
+
error?: string;
|
|
34
|
+
}
|
|
35
|
+
export declare namespace RelayInfoUtil {
|
|
36
|
+
function of(persistent: boolean): RelayInfo;
|
|
37
|
+
/** The answer of RELAY_INFO_PATH: JSON, readable by any page (it says nothing secret). */
|
|
38
|
+
function response(info: RelayInfo): Response;
|
|
39
|
+
/** The address of a relay as it is kept: http(s), its origin and path, no trailing slash. Throws on what is not one. */
|
|
40
|
+
function normalize(url: string): string;
|
|
41
|
+
/** Asks a relay what it is; never throws. */
|
|
42
|
+
function probe(url: string, timeoutMs?: number): Promise<RelayProbe>;
|
|
43
|
+
function parse(value: unknown): RelayInfo | undefined;
|
|
44
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { RELAY_BLOB_MAX_DAYS, RELAY_CONTENT_MAX_BYTES, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, } from './relay-protocol.js';
|
|
2
|
+
/** What a relay says of itself (docs/design/ta-relays.md 3.1): GET RELAY_INFO_PATH. */
|
|
3
|
+
export const RELAY_INFO_PATH = '/v1/info';
|
|
4
|
+
/** A relay that answers no RELAY_INFO_PATH, made before it: all it can do, with the limits of then. */
|
|
5
|
+
export const RELAY_LEGACY_INFO = {
|
|
6
|
+
v: 1,
|
|
7
|
+
capabilities: ['tunnel', 'hub', 'store', 'pages'],
|
|
8
|
+
limits: {
|
|
9
|
+
hubMembers: RELAY_HUB_MAX_MEMBERS,
|
|
10
|
+
hubValueBytes: RELAY_HUB_MAX_VALUE,
|
|
11
|
+
hubKeys: RELAY_HUB_MAX_KEYS,
|
|
12
|
+
hubStateBytes: RELAY_HUB_MAX_STATE,
|
|
13
|
+
hubIdleDays: RELAY_HUB_IDLE_DAYS,
|
|
14
|
+
storeValueBytes: RELAY_CONTENT_MAX_BYTES,
|
|
15
|
+
storeDays: RELAY_BLOB_MAX_DAYS,
|
|
16
|
+
},
|
|
17
|
+
persistent: true,
|
|
18
|
+
};
|
|
19
|
+
export var RelayInfoUtil;
|
|
20
|
+
(function (RelayInfoUtil) {
|
|
21
|
+
function of(persistent) {
|
|
22
|
+
return { ...RELAY_LEGACY_INFO, persistent };
|
|
23
|
+
}
|
|
24
|
+
RelayInfoUtil.of = of;
|
|
25
|
+
/** The answer of RELAY_INFO_PATH: JSON, readable by any page (it says nothing secret). */
|
|
26
|
+
function response(info) {
|
|
27
|
+
return new Response(JSON.stringify(info), {
|
|
28
|
+
headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*', 'cache-control': 'no-store' },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
RelayInfoUtil.response = response;
|
|
32
|
+
/** The address of a relay as it is kept: http(s), its origin and path, no trailing slash. Throws on what is not one. */
|
|
33
|
+
function normalize(url) {
|
|
34
|
+
const text = String(url !== null && url !== void 0 ? url : '').trim();
|
|
35
|
+
const parsed = new URL(text);
|
|
36
|
+
if (!/^https?:$/.test(parsed.protocol) || parsed.search || parsed.hash || parsed.username || parsed.password) {
|
|
37
|
+
throw new Error(`${text} is not the address of a relay.`);
|
|
38
|
+
}
|
|
39
|
+
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, '');
|
|
40
|
+
}
|
|
41
|
+
RelayInfoUtil.normalize = normalize;
|
|
42
|
+
/** Asks a relay what it is; never throws. */
|
|
43
|
+
async function probe(url, timeoutMs = 6000) {
|
|
44
|
+
const started = Date.now();
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(`${normalize(url)}${RELAY_INFO_PATH}`, { signal: controller.signal, redirect: 'manual' });
|
|
49
|
+
const ms = Date.now() - started;
|
|
50
|
+
if (response.ok) {
|
|
51
|
+
const info = parse(await response.json().catch(() => undefined));
|
|
52
|
+
return info ? { online: true, info, ms } : { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms };
|
|
53
|
+
}
|
|
54
|
+
if (response.status === 404 || response.status === 426) {
|
|
55
|
+
// A relay of before answers its root and nothing of this path: it is one if its root says so.
|
|
56
|
+
const root = await fetch(`${normalize(url)}/`, { signal: controller.signal }).then(answer => answer.text(), () => '');
|
|
57
|
+
return root.startsWith('Ta relay')
|
|
58
|
+
? { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms }
|
|
59
|
+
: { online: false, ms, error: `It answers, but is not a relay of Ta (${response.status}).` };
|
|
60
|
+
}
|
|
61
|
+
return { online: false, ms, error: `It answered ${response.status}.` };
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
const aborted = (error === null || error === void 0 ? void 0 : error.name) === 'AbortError';
|
|
65
|
+
return { online: false, error: aborted ? 'No answer in time.' : (error === null || error === void 0 ? void 0 : error.message) || String(error) };
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
RelayInfoUtil.probe = probe;
|
|
72
|
+
function parse(value) {
|
|
73
|
+
const info = value;
|
|
74
|
+
if ((info === null || info === void 0 ? void 0 : info.v) !== 1 || !Array.isArray(info.capabilities) || !info.limits || typeof info.limits !== 'object') {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const known = ['tunnel', 'hub', 'store', 'pages'];
|
|
78
|
+
const limits = { ...RELAY_LEGACY_INFO.limits };
|
|
79
|
+
for (const key of Object.keys(limits)) {
|
|
80
|
+
const given = info.limits[key];
|
|
81
|
+
if (typeof given === 'number' && given > 0) {
|
|
82
|
+
limits[key] = given;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
v: 1,
|
|
87
|
+
capabilities: info.capabilities.filter((capability) => known.includes(capability)),
|
|
88
|
+
limits,
|
|
89
|
+
persistent: info.persistent !== false,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
RelayInfoUtil.parse = parse;
|
|
93
|
+
})(RelayInfoUtil || (RelayInfoUtil = {}));
|
|
94
|
+
//# sourceMappingURL=relay-info.js.map
|
|
@@ -166,11 +166,15 @@ export declare const RELAY_PROOF = "ta-relay ready";
|
|
|
166
166
|
export declare const RELAY_HUB_PATH = "/v1/hub/";
|
|
167
167
|
/** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
|
|
168
168
|
export declare const RELAY_ROOM_PATH = "/r";
|
|
169
|
-
/** Members of a
|
|
170
|
-
export declare const RELAY_HUB_MAX_MEMBERS =
|
|
169
|
+
/** Members of a hub at most, its owner among them: a room of friends (its owner and RELAY_FRIENDS_MAX friends). */
|
|
170
|
+
export declare const RELAY_HUB_MAX_MEMBERS = 101;
|
|
171
|
+
/** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
|
|
172
|
+
export declare const RELAY_ROOM_MAX_MEMBERS = 16;
|
|
173
|
+
/** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
|
|
174
|
+
export declare const RELAY_FRIENDS_MAX = 100;
|
|
171
175
|
/** Characters of a sealed message or value of the state. */
|
|
172
176
|
export declare const RELAY_HUB_MAX_VALUE: number;
|
|
173
|
-
export declare const RELAY_HUB_MAX_KEYS =
|
|
177
|
+
export declare const RELAY_HUB_MAX_KEYS = 4096;
|
|
174
178
|
/** Characters of all of the state, sealed. */
|
|
175
179
|
export declare const RELAY_HUB_MAX_STATE: number;
|
|
176
180
|
/** Asks to join waiting for the owner, and for how long. */
|
|
@@ -91,11 +91,15 @@ export const RELAY_PROOF = 'ta-relay ready';
|
|
|
91
91
|
export const RELAY_HUB_PATH = '/v1/hub/';
|
|
92
92
|
/** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
|
|
93
93
|
export const RELAY_ROOM_PATH = '/r';
|
|
94
|
-
/** Members of a
|
|
95
|
-
export const RELAY_HUB_MAX_MEMBERS =
|
|
94
|
+
/** Members of a hub at most, its owner among them: a room of friends (its owner and RELAY_FRIENDS_MAX friends). */
|
|
95
|
+
export const RELAY_HUB_MAX_MEMBERS = 101;
|
|
96
|
+
/** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
|
|
97
|
+
export const RELAY_ROOM_MAX_MEMBERS = 16;
|
|
98
|
+
/** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
|
|
99
|
+
export const RELAY_FRIENDS_MAX = 100;
|
|
96
100
|
/** Characters of a sealed message or value of the state. */
|
|
97
101
|
export const RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
98
|
-
export const RELAY_HUB_MAX_KEYS =
|
|
102
|
+
export const RELAY_HUB_MAX_KEYS = 4096;
|
|
99
103
|
/** Characters of all of the state, sealed. */
|
|
100
104
|
export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
101
105
|
/** Asks to join waiting for the owner, and for how long. */
|
|
@@ -38,6 +38,11 @@ export interface RelayChatLine {
|
|
|
38
38
|
to?: string[];
|
|
39
39
|
/** The id of the line it answers. */
|
|
40
40
|
re?: string;
|
|
41
|
+
/**
|
|
42
|
+
* A task handed to the doubles it asks (`to`), not a question (docs/design/ta-link.md 6): each one's Ta works on it on
|
|
43
|
+
* its own, says how it goes under `tasks:<member>` of the state, and answers with `re` when it ends.
|
|
44
|
+
*/
|
|
45
|
+
task?: boolean;
|
|
41
46
|
}
|
|
42
47
|
/** An agent of a member's Ta, in a room (RelayChatLine.agent). */
|
|
43
48
|
export interface RelayChatAgent {
|
|
@@ -45,7 +50,7 @@ export interface RelayChatAgent {
|
|
|
45
50
|
name: string;
|
|
46
51
|
}
|
|
47
52
|
/** 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'>;
|
|
53
|
+
export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 're' | 'task'>;
|
|
49
54
|
/** What is kept of a line: `from` is the key's. */
|
|
50
55
|
type StoredLine = Omit<RelayChatLine, 'from'>;
|
|
51
56
|
/** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
|