@celljs/ta-relay 4.19.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.
Files changed (82) hide show
  1. package/esm/common/index.d.ts +1 -0
  2. package/esm/common/index.js +1 -0
  3. package/esm/common/relay-account-client.d.ts +4 -0
  4. package/esm/common/relay-account-client.js +13 -3
  5. package/esm/common/relay-account.d.ts +17 -2
  6. package/esm/common/relay-account.js +15 -6
  7. package/esm/common/relay-accounts.d.ts +2 -0
  8. package/esm/common/relay-accounts.js +16 -2
  9. package/esm/common/relay-group-invites.d.ts +6 -2
  10. package/esm/common/relay-group-invites.js +6 -5
  11. package/esm/common/relay-hub-charter.d.ts +74 -0
  12. package/esm/common/relay-hub-charter.js +149 -0
  13. package/esm/common/relay-hub-epochs.d.ts +43 -0
  14. package/esm/common/relay-hub-epochs.js +92 -0
  15. package/esm/common/relay-hub-util.d.ts +39 -7
  16. package/esm/common/relay-hub-util.js +46 -15
  17. package/esm/common/relay-hub.d.ts +13 -0
  18. package/esm/common/relay-hub.js +81 -18
  19. package/esm/common/relay-hubs-admins.d.ts +27 -0
  20. package/esm/common/relay-hubs-admins.js +91 -0
  21. package/esm/common/relay-hubs-connection.d.ts +21 -63
  22. package/esm/common/relay-hubs-connection.js +103 -87
  23. package/esm/common/relay-hubs-identity.d.ts +29 -1
  24. package/esm/common/relay-hubs-identity.js +109 -1
  25. package/esm/common/relay-hubs-live.d.ts +58 -0
  26. package/esm/common/relay-hubs-live.js +27 -0
  27. package/esm/common/relay-hubs-protocol.d.ts +10 -0
  28. package/esm/common/relay-hubs-util.d.ts +9 -0
  29. package/esm/common/relay-hubs-util.js +23 -1
  30. package/esm/common/relay-hubs.d.ts +12 -15
  31. package/esm/common/relay-hubs.js +60 -57
  32. package/esm/common/relay-identity.d.ts +7 -0
  33. package/esm/common/relay-identity.js +8 -0
  34. package/esm/common/relay-protocol.d.ts +21 -3
  35. package/esm/common/relay-protocol.js +8 -1
  36. package/esm/common/relay-room-chat.d.ts +6 -1
  37. package/esm/common/relay-room-chat.js +1 -0
  38. package/esm/node/relay-server-accounts.d.ts +1 -1
  39. package/esm/node/relay-server-accounts.js +1 -1
  40. package/lib/common/index.d.ts +1 -0
  41. package/lib/common/index.js +1 -0
  42. package/lib/common/relay-account-client.d.ts +4 -0
  43. package/lib/common/relay-account-client.js +13 -3
  44. package/lib/common/relay-account.d.ts +17 -2
  45. package/lib/common/relay-account.js +15 -6
  46. package/lib/common/relay-accounts.d.ts +2 -0
  47. package/lib/common/relay-accounts.js +16 -2
  48. package/lib/common/relay-group-invites.d.ts +6 -2
  49. package/lib/common/relay-group-invites.js +6 -5
  50. package/lib/common/relay-hub-charter.d.ts +75 -0
  51. package/lib/common/relay-hub-charter.js +152 -0
  52. package/lib/common/relay-hub-epochs.d.ts +44 -0
  53. package/lib/common/relay-hub-epochs.js +95 -0
  54. package/lib/common/relay-hub-util.d.ts +39 -7
  55. package/lib/common/relay-hub-util.js +46 -15
  56. package/lib/common/relay-hub.d.ts +13 -0
  57. package/lib/common/relay-hub.js +81 -18
  58. package/lib/common/relay-hubs-admins.d.ts +28 -0
  59. package/lib/common/relay-hubs-admins.js +95 -0
  60. package/lib/common/relay-hubs-connection.d.ts +21 -63
  61. package/lib/common/relay-hubs-connection.js +109 -94
  62. package/lib/common/relay-hubs-identity.d.ts +29 -1
  63. package/lib/common/relay-hubs-identity.js +109 -1
  64. package/lib/common/relay-hubs-live.d.ts +59 -0
  65. package/lib/common/relay-hubs-live.js +31 -0
  66. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  67. package/lib/common/relay-hubs-util.d.ts +9 -0
  68. package/lib/common/relay-hubs-util.js +23 -1
  69. package/lib/common/relay-hubs.d.ts +12 -15
  70. package/lib/common/relay-hubs.js +66 -63
  71. package/lib/common/relay-identity.d.ts +7 -0
  72. package/lib/common/relay-identity.js +8 -0
  73. package/lib/common/relay-protocol.d.ts +21 -3
  74. package/lib/common/relay-protocol.js +9 -2
  75. package/lib/common/relay-room-chat.d.ts +6 -1
  76. package/lib/common/relay-room-chat.js +1 -0
  77. package/lib/node/relay-server-accounts.d.ts +1 -1
  78. package/lib/node/relay-server-accounts.js +1 -1
  79. package/package.json +3 -3
  80. package/viewer/join.html +6 -6
  81. package/viewer/page.html +12 -12
  82. package/viewer/room.html +15 -15
@@ -1,55 +1,9 @@
1
- import { type RelayHubAskBody, type RelayHubKeys, type RelayHubMember, type RelayHubRosterBody } from './relay-hub-util.js';
2
- import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus, RelayRoomValue } from './relay-hubs-protocol.js';
1
+ import { type RelayHubCharterBody } from './relay-hub-charter.js';
2
+ import { type RelayHubAskBody, type RelayHubMember } from './relay-hub-util.js';
3
+ import { type Live, type MessageBody } from './relay-hubs-live.js';
4
+ import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus } from './relay-hubs-protocol.js';
3
5
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
4
- import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest } from './relay-protocol.js';
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
- /** How long a call waits for the room to be connected, or for the relay to answer. */
19
- export declare const READY_MS = 15000;
20
- export declare const OPEN = 1;
21
- /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
22
- export declare class Live {
23
- record: RelayRoomRecord;
24
- socket?: WebSocket;
25
- keys?: RelayHubKeys;
26
- synced: boolean;
27
- online: Set<string>;
28
- roster?: RelayHubRosterBody;
29
- readonly state: Map<string, RelayRoomValue>;
30
- /** Asks to join, with the invitation each came with. */
31
- readonly asks: Map<string, {
32
- body: RelayHubAskBody;
33
- invite: string;
34
- }>;
35
- readonly done: Map<number, (result: {
36
- ok: boolean;
37
- v: number;
38
- error?: RelayHubFailure;
39
- }) => void>;
40
- readonly ready: Array<() => void>;
41
- attempts: number;
42
- /** Connect again at once, whatever the close said. */
43
- retry: boolean;
44
- timer?: ReturnType<typeof setTimeout>;
45
- pingTimer?: ReturnType<typeof setInterval>;
46
- awaitingPong: boolean;
47
- /** The look this Ta last sent the owner, for the roster to say: once, whatever the owner does with it. */
48
- toldLook?: string;
49
- /** Events are handled one after another: opening them is async. */
50
- chain: Promise<void>;
51
- constructor(record: RelayRoomRecord);
52
- }
6
+ import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
53
7
  /**
54
8
  * The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
55
9
  * connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
@@ -74,12 +28,15 @@ export declare abstract class RelayHubsConnection {
74
28
  protected ready(live: Live): Promise<void>;
75
29
  protected request(live: Live, request: RelayHubRequest): void;
76
30
  protected sendBody(live: Live, body: MessageBody, to?: string): Promise<void>;
77
- /** The roster with `change` made, its version one more, signed by the owner. */
78
- protected nextRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<{
79
- v: number;
80
- d: string;
81
- }>;
82
- protected writeRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<void>;
31
+ /** Whether this Ta lets in, takes out and invites: the owner, or an admin of the charter. */
32
+ protected manages(live: Live): boolean;
33
+ protected isAdmin(live: Live, member: string): boolean;
34
+ /**
35
+ * The roster with `change` made, its version one more, signed by this Ta (the owner, or an admin). The owner's
36
+ * carries `charter` when it changes it, else the charter of before, its admins still members; an admin's, the one it has.
37
+ */
38
+ protected nextRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[], charter?: RelayHubCharterBody, rekey?: boolean): Promise<RelayHubRoster>;
39
+ protected writeRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[], charter?: RelayHubCharterBody, rekey?: boolean): Promise<void>;
83
40
  protected connect(live: Live): void;
84
41
  protected hello(live: Live, socket: WebSocket): Promise<void>;
85
42
  protected closed(live: Live, code: number): void;
@@ -96,16 +53,17 @@ export declare abstract class RelayHubsConnection {
96
53
  protected heardMembers(_members: RelayPublicIdentity[]): void;
97
54
  /** A member asks the user to be friends, by the link of its room of friends. */
98
55
  protected offered(_live: Live, _from: RelayPublicIdentity, _link: string): void;
99
- protected readRoster(live: Live, sealed: string): Promise<void>;
100
- /**
101
- * 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,
102
- * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
103
- */
104
- protected keepLook(live: Live): Promise<void>;
56
+ protected readRoster(live: Live, roster: RelayHubRoster): Promise<void>;
57
+ /** The roster says this Ta as it is now: its look (RelayHubsIdentity). */
58
+ protected keepLook(_live: Live): Promise<void>;
59
+ /** The state of the room has this Ta's picture as it is now (RelayHubs). */
60
+ protected keepPicture(_live: Live): Promise<void>;
105
61
  protected guestOf(live: Live, member: string): boolean;
106
62
  protected signOf(live: Live): (member: string) => string | undefined;
107
63
  protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
108
64
  protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
65
+ /** A member set a key naming another: the owner clears it (RelayHubs). */
66
+ protected foreignSet(_live: Live, _key: string): void;
109
67
  /** Ids of those the user chose as friends. */
110
68
  protected friendIds(): Set<string>;
111
69
  protected readRooms(): RelayRoomRecord[];
@@ -1,34 +1,14 @@
1
1
  import { RelayFriendsUtil } from './relay-friends.js';
2
2
  import { RelayHubCloseCode } from './relay-hub.js';
3
+ import { RelayHubCharterUtil } from './relay-hub-charter.js';
4
+ import { RelayHubEpochs } from './relay-hub-epochs.js';
3
5
  import { RelayHubUtil } from './relay-hub-util.js';
6
+ import { MAX_RECONNECT_MS, OPEN, READY_MS, SKEW_MS } from './relay-hubs-live.js';
7
+ import { RelayHubsUtil } from './relay-hubs-util.js';
4
8
  import { RelayIdentityUtil } from './relay-identity.js';
5
9
  import { RelayPairingUtil } from './relay-pairing.js';
6
10
  import { RELAY_HUB_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, } from './relay-protocol.js';
7
11
  import { RELAY_CHAT_KEY } from './relay-room-chat.js';
8
- const MAX_RECONNECT_MS = 30000;
9
- const SKEW_MS = 5 * 60 * 1000;
10
- /** How long a call waits for the room to be connected, or for the relay to answer. */
11
- export const READY_MS = 15000;
12
- export const OPEN = 1;
13
- /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
14
- export class Live {
15
- constructor(record) {
16
- this.record = record;
17
- this.synced = false;
18
- this.online = new Set();
19
- this.state = new Map();
20
- /** Asks to join, with the invitation each came with. */
21
- this.asks = new Map();
22
- this.done = new Map();
23
- this.ready = [];
24
- this.attempts = 0;
25
- /** Connect again at once, whatever the close said. */
26
- this.retry = false;
27
- this.awaitingPong = false;
28
- /** Events are handled one after another: opening them is async. */
29
- this.chain = Promise.resolve();
30
- }
31
- }
32
12
  /**
33
13
  * The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
34
14
  * connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
@@ -102,22 +82,51 @@ export class RelayHubsConnection {
102
82
  async sendBody(live, body, to) {
103
83
  await this.ready(live);
104
84
  const identity = await this.identityOf(live);
105
- const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
85
+ const d = await RelayHubUtil.sealIn(live.keys, identity, live.record.member, body);
106
86
  this.request(live, { t: 'send', d, ...(to ? { to } : {}) });
107
87
  }
108
- /** The roster with `change` made, its version one more, signed by the owner. */
109
- async nextRoster(live, change) {
110
- var _a, _b;
111
- var _c, _d;
88
+ /** Whether this Ta lets in, takes out and invites: the owner, or an admin of the charter. */
89
+ manages(live) {
90
+ return live.record.own || this.isAdmin(live, live.record.member);
91
+ }
92
+ isAdmin(live, member) {
93
+ var _a;
94
+ return !!((_a = live.charter) === null || _a === void 0 ? void 0 : _a.admins.some(admin => admin.member === member));
95
+ }
96
+ /**
97
+ * The roster with `change` made, its version one more, signed by this Ta (the owner, or an admin). The owner's
98
+ * carries `charter` when it changes it, else the charter of before, its admins still members; an admin's, the one it has.
99
+ */
100
+ async nextRoster(live, change, charter, rekey = false) {
101
+ var _a, _b, _c, _d;
102
+ var _e, _f, _g;
112
103
  const identity = await this.identityOf(live);
113
- const body = { v: ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.v) !== null && _c !== void 0 ? _c : 0) + 1, members: change((_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : []) };
114
- const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
104
+ const members = change((_e = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _e !== void 0 ? _e : []);
105
+ let sealed = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.charter;
106
+ if (live.record.own) {
107
+ const base = charter !== null && charter !== void 0 ? charter : live.charter;
108
+ 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));
109
+ if (base && admins && (charter || admins.length !== base.admins.length)) {
110
+ 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 };
111
+ sealed = await RelayHubCharterUtil.seal(identity, next);
112
+ live.charter = next;
113
+ }
114
+ }
115
+ 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 } : {}) };
116
+ // A new epoch once one is taken out or leaves (docs/design/ta-link.md 8.7): taken on when the roster comes back.
117
+ const { record } = live;
118
+ const epochs = rekey ? RelayHubEpochs.next(record.epochs) : record.epochs;
119
+ const keys = rekey ? await RelayHubUtil.keys(record.key, epochs) : live.keys;
120
+ const d = await RelayHubUtil.sealIn(keys, identity, record.member, body);
121
+ const k = epochs && RelayHubEpochs.latest(epochs)
122
+ ? await RelayHubEpochs.seal(identity, record.member, record.id, epochs, members, live.sealedEpochs)
123
+ : undefined;
115
124
  live.roster = body;
116
- return { v: body.v, d };
125
+ return { v: body.v, d, ...(k ? { k } : {}) };
117
126
  }
118
- async writeRoster(live, change) {
127
+ async writeRoster(live, change, charter, rekey = false) {
119
128
  await this.ready(live);
120
- this.request(live, { t: 'roster', roster: await this.nextRoster(live, change) });
129
+ this.request(live, { t: 'roster', roster: await this.nextRoster(live, change, charter, rekey) });
121
130
  }
122
131
  connect(live) {
123
132
  if (this.stopped) {
@@ -178,7 +187,7 @@ export class RelayHubsConnection {
178
187
  };
179
188
  }
180
189
  else {
181
- (_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await RelayHubUtil.keys(record.key));
190
+ (_a = live.keys) !== null && _a !== void 0 ? _a : (live.keys = await RelayHubUtil.keys(record.key, record.epochs));
182
191
  request = { t: record.own && !record.made ? 'create' : 'auth', member: record.member, token: record.token };
183
192
  }
184
193
  if (socket.readyState === OPEN) {
@@ -239,7 +248,7 @@ export class RelayHubsConnection {
239
248
  live.online = new Set(event.online);
240
249
  live.asks.clear();
241
250
  if (event.roster) {
242
- await this.readRoster(live, event.roster.d);
251
+ await this.readRoster(live, event.roster);
243
252
  }
244
253
  for (const ask of (_b = event.asks) !== null && _b !== void 0 ? _b : []) {
245
254
  await this.asked(live, ask.d, false);
@@ -278,7 +287,7 @@ export class RelayHubsConnection {
278
287
  this.emit({ type: 'rooms' });
279
288
  return;
280
289
  case 'roster':
281
- await this.readRoster(live, event.roster.d);
290
+ await this.readRoster(live, event.roster);
282
291
  this.emit({ type: 'rooms' });
283
292
  return;
284
293
  case 'asked':
@@ -286,8 +295,9 @@ export class RelayHubsConnection {
286
295
  return;
287
296
  case 'left':
288
297
  live.online.delete(event.member);
289
- if (record.own) {
290
- await this.writeRoster(live, members => members.filter(member => member.member !== event.member)).catch(() => undefined);
298
+ // An admin's roster keeps every admin: one who left goes from it when the owner writes it.
299
+ if (record.own || (this.manages(live) && !this.isAdmin(live, event.member))) {
300
+ await this.writeRoster(live, members => members.filter(member => member.member !== event.member), undefined, true).catch(() => undefined);
291
301
  }
292
302
  this.emit({ type: 'rooms' });
293
303
  return;
@@ -331,6 +341,7 @@ export class RelayHubsConnection {
331
341
  live.record = { ...live.record, status, ...(reason ? { reason } : {}) };
332
342
  if (status !== 'member' && status !== 'waiting') {
333
343
  delete live.record.key;
344
+ delete live.record.epochs;
334
345
  delete live.record.invite;
335
346
  this.disconnect(live);
336
347
  }
@@ -347,9 +358,11 @@ export class RelayHubsConnection {
347
358
  this.setStatus(live, 'gone', 'invite');
348
359
  return;
349
360
  }
361
+ const epochs = RelayHubEpochs.kept(opened.body.epochs && typeof opened.body.epochs === 'object' ? opened.body.epochs : {});
350
362
  live.record = {
351
363
  ...record,
352
364
  key: opened.body.key,
365
+ ...(Object.keys(epochs).length ? { epochs } : {}),
353
366
  kind: RelayFriendsUtil.kindOf(String(opened.body.kind)),
354
367
  title: String(opened.body.title).slice(0, 80),
355
368
  ...(opened.body.view ? { view: String(opened.body.view) } : {}),
@@ -360,7 +373,7 @@ export class RelayHubsConnection {
360
373
  };
361
374
  delete live.record.invite;
362
375
  delete live.record.asked;
363
- live.keys = await RelayHubUtil.keys(opened.body.key);
376
+ live.keys = await RelayHubUtil.keys(opened.body.key, live.record.epochs);
364
377
  this.save();
365
378
  this.emit({ type: 'rooms' });
366
379
  }
@@ -388,63 +401,58 @@ export class RelayHubsConnection {
388
401
  heardMembers(_members) { }
389
402
  /** A member asks the user to be friends, by the link of its room of friends. */
390
403
  offered(_live, _from, _link) { }
391
- async readRoster(live, sealed) {
404
+ async readRoster(live, roster) {
392
405
  var _a;
393
- const owner = live.record.owner;
394
- const keys = (_a = live.keys) !== null && _a !== void 0 ? _a : (live.record.key ? await RelayHubUtil.keys(live.record.key) : undefined);
406
+ var _b, _c, _d, _e;
407
+ const { record } = live;
408
+ const owner = record.owner;
409
+ const keys = (_b = live.keys) !== null && _b !== void 0 ? _b : (record.key ? await RelayHubUtil.keys(record.key, record.epochs) : undefined);
395
410
  if (!keys || !owner.sign) {
396
411
  return;
397
412
  }
398
413
  live.keys = keys;
399
- // Signed by the owner alone: its member id is in the roster it signs.
400
- let ownerMember;
401
- const signed = await RelayHubUtil.open(keys.seal, sealed, member => {
402
- ownerMember = member;
403
- return owner.sign;
404
- });
405
- if (!signed || !Array.isArray(signed.b.members) || (live.roster && signed.b.v < live.roster.v)) {
414
+ // 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).
415
+ const epoch = RelayHubEpochs.epochOf(roster.d);
416
+ const taken = keys.ring.has(epoch) ? undefined : await RelayHubEpochs.open(await this.identityOf(live), record.member, record.id, epoch, roster.k);
417
+ const tried = taken ? await RelayHubUtil.keys(record.key, { ...record.epochs, ...taken.epochs }) : keys;
418
+ // Signed by the owner, or by an admin its charter names for a change of members alone.
419
+ const opened = await RelayHubCharterUtil.openRoster(tried, roster.d, { id: owner.id, sign: owner.sign }, record.id);
420
+ if (!opened ||
421
+ 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) ||
422
+ (live.charter && opened.charter && opened.charter.v < live.charter.v) ||
423
+ (opened.byAdmin && !RelayHubCharterUtil.adminMay(opened, live.roster && { members: live.roster.members, charter: live.charter })) ||
424
+ (taken && !RelayHubEpochs.sealedBy(taken.sealer, opened))) {
406
425
  return;
407
426
  }
408
- const members = signed.b.members.filter(member => member && typeof member.member === 'string');
409
- if (!members.some(member => member.owner && member.member === ownerMember && member.id === owner.id)) {
410
- return;
427
+ const members = opened.body.members;
428
+ live.roster = opened.body;
429
+ live.charter = opened.charter;
430
+ if (taken || ((_e = record.rosterV) !== null && _e !== void 0 ? _e : 0) < opened.body.v) {
431
+ record.rosterV = opened.body.v;
432
+ if (taken) {
433
+ record.epochs = RelayHubEpochs.kept({ ...record.epochs, ...taken.epochs });
434
+ live.keys = tried;
435
+ }
436
+ this.save();
437
+ }
438
+ if (record.own && opened.byAdmin) {
439
+ // Signed again by the owner: a Ta of before checks the roster against the owner alone.
440
+ void this.writeRoster(live, same => same).catch(() => undefined);
411
441
  }
412
- live.roster = { v: signed.b.v, members };
413
442
  const own = (await this.identityOf(live)).id;
414
443
  this.heardMembers(members.filter(member => member.id !== own && !member.guest));
415
- if (!members.some(member => member.member === live.record.member) && !live.record.own && live.record.status === 'member') {
444
+ if (!members.some(member => member.member === record.member) && !record.own && record.status === 'member') {
416
445
  this.setStatus(live, 'gone', 'removed');
417
446
  return;
418
447
  }
419
448
  // Not awaited: what it sends waits for the room to be ready, which may wait for this event.
420
449
  void this.keepLook(live);
450
+ void this.keepPicture(live).catch(() => undefined);
421
451
  }
422
- /**
423
- * 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,
424
- * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
425
- */
426
- async keepLook(live) {
427
- var _a, _b;
428
- if (live.record.status !== 'member' || live.record.identity || !live.roster) {
429
- return;
430
- }
431
- const identity = await this.identity();
432
- const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
433
- const self = live.roster.members.find(member => member.member === live.record.member);
434
- if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
435
- return;
436
- }
437
- if (live.record.own) {
438
- await this.writeRoster(live, members => members.map(member => (member.member === self.member ? RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
439
- return;
440
- }
441
- const owner = live.roster.members.find(member => member.owner);
442
- if (owner && live.toldLook !== look) {
443
- await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
444
- live.toldLook = look;
445
- }, () => undefined);
446
- }
447
- }
452
+ /** The roster says this Ta as it is now: its look (RelayHubsIdentity). */
453
+ async keepLook(_live) { }
454
+ /** The state of the room has this Ta's picture as it is now (RelayHubs). */
455
+ async keepPicture(_live) { }
448
456
  guestOf(live, member) {
449
457
  var _a, _b;
450
458
  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);
@@ -453,11 +461,12 @@ export class RelayHubsConnection {
453
461
  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; };
454
462
  }
455
463
  async readEntry(live, entry, tell) {
456
- var _a, _b;
464
+ var _a;
465
+ var _b, _c, _d;
457
466
  if (!live.keys) {
458
467
  return;
459
468
  }
460
- const signed = await RelayHubUtil.open(live.keys.seal, entry.d, this.signOf(live));
469
+ const signed = await RelayHubUtil.openIn(live.keys, entry.d, this.signOf(live));
461
470
  if (!signed || signed.m !== entry.by || typeof signed.b.key !== 'string' || (await RelayHubUtil.keyId(live.keys, signed.b.key)) !== entry.k) {
462
471
  return;
463
472
  }
@@ -465,9 +474,14 @@ export class RelayHubsConnection {
465
474
  if (before && before.version >= entry.v) {
466
475
  return;
467
476
  }
468
- if (this.guestOf(live, entry.by) && !signed.b.key.startsWith(RELAY_CHAT_KEY)) {
469
- // A guest only chats: what else it set is not taken, its version kept, so that setting it again goes on that.
470
- 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 });
477
+ const foreign = 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);
478
+ if (foreign || (this.guestOf(live, entry.by) && !signed.b.key.startsWith(RELAY_CHAT_KEY))) {
479
+ // A guest only chats; a key naming a member is theirs alone (docs/design/ta-link.md 8.8): what else was set is
480
+ // not taken, its version kept, so that setting it again goes on that. The owner clears another's write there.
481
+ 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 });
482
+ if (foreign && live.record.own && signed.b.value !== null) {
483
+ this.foreignSet(live, signed.b.key);
484
+ }
471
485
  return;
472
486
  }
473
487
  live.state.set(signed.b.key, { value: signed.b.value, version: entry.v, by: entry.by });
@@ -481,7 +495,7 @@ export class RelayHubsConnection {
481
495
  if (!live.keys) {
482
496
  return;
483
497
  }
484
- const signed = await RelayHubUtil.open(live.keys.seal, sealed, this.signOf(live), { skewMs: SKEW_MS });
498
+ const signed = await RelayHubUtil.openIn(live.keys, sealed, this.signOf(live), { skewMs: SKEW_MS });
485
499
  if (!signed || signed.m !== from) {
486
500
  return;
487
501
  }
@@ -507,6 +521,8 @@ export class RelayHubsConnection {
507
521
  }
508
522
  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 });
509
523
  }
524
+ /** A member set a key naming another: the owner clears it (RelayHubs). */
525
+ foreignSet(_live, _key) { }
510
526
  /** Ids of those the user chose as friends. */
511
527
  friendIds() {
512
528
  return new Set();
@@ -1,9 +1,14 @@
1
- import type { RelayFriends } from './relay-friends.js';
1
+ import { type RelayFriends } from './relay-friends.js';
2
2
  import { RelayHubsConnection } from './relay-hubs-connection.js';
3
+ import { Live } from './relay-hubs-live.js';
3
4
  import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol.js';
4
5
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
5
6
  export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
6
7
  protected abstract readonly book: RelayFriends;
8
+ abstract setState(roomId: string, key: string, value: unknown, version?: number): Promise<{
9
+ ok: boolean;
10
+ version: number;
11
+ }>;
7
12
  /** The name others see; the owners of rooms this Ta is in hear it. */
8
13
  setName(name: string): Promise<RelayPublicIdentity>;
9
14
  /**
@@ -11,6 +16,29 @@ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
11
16
  * look that is none. The owners of rooms this Ta is in hear it, as a name.
12
17
  */
13
18
  setLook(look: string | undefined): Promise<RelayPublicIdentity>;
19
+ /**
20
+ * The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
21
+ * in the rooms this Ta is in now, and in those it connects to later.
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>;
29
+ /**
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
31
+ * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
32
+ */
33
+ protected keepPicture(live: Live): Promise<void>;
34
+ /**
35
+ * The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
36
+ * what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
37
+ */
38
+ pictures(): Promise<{
39
+ self: string;
40
+ pictures: Record<string, string>;
41
+ }>;
14
42
  /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
15
43
  protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
16
44
  /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
@@ -1,6 +1,9 @@
1
- import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
1
+ import { RelayFriendsUtil } from './relay-friends.js';
2
+ import { RelayHubsConnection } from './relay-hubs-connection.js';
3
+ import { Live } from './relay-hubs-live.js';
2
4
  import { RelayHubsUtil } from './relay-hubs-util.js';
3
5
  import { RelayIdentityUtil } from './relay-identity.js';
6
+ import { RELAY_PICTURE_KEY } from './relay-protocol.js';
4
7
  export class RelayHubsIdentity extends RelayHubsConnection {
5
8
  /** The name others see; the owners of rooms this Ta is in hear it. */
6
9
  async setName(name) {
@@ -19,6 +22,111 @@ export class RelayHubsIdentity extends RelayHubsConnection {
19
22
  const next = RelayIdentityUtil.withLook(identity, look);
20
23
  return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
21
24
  }
25
+ /**
26
+ * The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
27
+ * in the rooms this Ta is in now, and in those it connects to later.
28
+ */
29
+ async setPicture(picture) {
30
+ var _a;
31
+ if (picture !== null && !RelayIdentityUtil.isPicture(picture)) {
32
+ throw new Error('A picture is a data URL of a PNG, JPEG, WebP or SVG, of 16 KB at most.');
33
+ }
34
+ const identity = { ...(await this.identity()) };
35
+ if (((_a = identity.picture) !== null && _a !== void 0 ? _a : null) !== picture) {
36
+ if (picture === null) {
37
+ delete identity.picture;
38
+ }
39
+ else {
40
+ identity.picture = picture;
41
+ }
42
+ this.options.store.write('identity', identity);
43
+ this.own = identity;
44
+ // The pages draw the user by it, and read it again.
45
+ this.emit({ type: 'rooms' });
46
+ }
47
+ for (const live of this.live.values()) {
48
+ void this.keepPicture(live).catch(() => undefined);
49
+ }
50
+ return identity;
51
+ }
52
+ /**
53
+ * 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,
54
+ * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
55
+ */
56
+ async keepLook(live) {
57
+ var _a, _b;
58
+ if (live.record.status !== 'member' || live.record.identity || !live.roster) {
59
+ return;
60
+ }
61
+ const identity = await this.identity();
62
+ const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
63
+ const self = live.roster.members.find(member => member.member === live.record.member);
64
+ if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
65
+ return;
66
+ }
67
+ if (live.record.own) {
68
+ await this.writeRoster(live, members => members.map(member => (member.member === self.member ? RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
69
+ return;
70
+ }
71
+ const owner = live.roster.members.find(member => member.owner);
72
+ if (owner && live.toldLook !== look) {
73
+ await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
74
+ live.toldLook = look;
75
+ }, () => undefined);
76
+ }
77
+ }
78
+ /**
79
+ * Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
80
+ * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
81
+ */
82
+ async keepPicture(live) {
83
+ var _a, _b, _c;
84
+ const { record } = live;
85
+ if (record.status !== 'member' || record.identity || (RelayFriendsUtil.isKind(record.kind) && !record.own)) {
86
+ return;
87
+ }
88
+ await this.ready(live);
89
+ const picture = (_a = (await this.identity()).picture) !== null && _a !== void 0 ? _a : null;
90
+ const key = RELAY_PICTURE_KEY + record.member;
91
+ const kept = live.state.get(key);
92
+ if (live.record.status !== 'member' || ((_b = kept === null || kept === void 0 ? void 0 : kept.value) !== null && _b !== void 0 ? _b : null) === picture) {
93
+ return;
94
+ }
95
+ await this.setState(record.id, key, picture, (_c = kept === null || kept === void 0 ? void 0 : kept.version) !== null && _c !== void 0 ? _c : 0);
96
+ }
97
+ /**
98
+ * The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
99
+ * what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
100
+ */
101
+ async pictures() {
102
+ const own = await this.identity();
103
+ const pictures = {};
104
+ const owned = new Set();
105
+ for (const live of this.live.values()) {
106
+ if (live.record.status !== 'member' || !live.roster) {
107
+ continue;
108
+ }
109
+ for (const member of live.roster.members) {
110
+ const kept = live.state.get(RELAY_PICTURE_KEY + member.member);
111
+ if (member.guest || (kept === null || kept === void 0 ? void 0 : kept.by) !== member.member || !RelayIdentityUtil.isPicture(kept.value) || owned.has(member.id)) {
112
+ continue;
113
+ }
114
+ if (!(member.id in pictures) || member.owner) {
115
+ pictures[member.id] = kept.value;
116
+ }
117
+ if (member.owner) {
118
+ owned.add(member.id);
119
+ }
120
+ }
121
+ }
122
+ if (own.picture) {
123
+ pictures[own.id] = own.picture;
124
+ }
125
+ else {
126
+ delete pictures[own.id];
127
+ }
128
+ return { self: own.id, pictures };
129
+ }
22
130
  /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
23
131
  async changeSelf(identity) {
24
132
  var _a;
@@ -0,0 +1,58 @@
1
+ import type { RelayHubCharterBody } from './relay-hub-charter.js';
2
+ import type { RelayHubAskBody, RelayHubKeys, RelayHubRosterBody } from './relay-hub-util.js';
3
+ import type { RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol.js';
4
+ import type { RelayHubFailure } from './relay-protocol.js';
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
+ }