@celljs/ta-relay 4.20.0 → 4.22.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 (91) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -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 +78 -0
  12. package/esm/common/relay-hub-charter.js +152 -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 +19 -63
  22. package/esm/common/relay-hubs-connection.js +100 -87
  23. package/esm/common/relay-hubs-identity.d.ts +7 -1
  24. package/esm/common/relay-hubs-identity.js +28 -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 +15 -15
  31. package/esm/common/relay-hubs.js +76 -57
  32. package/esm/common/relay-protocol.d.ts +14 -3
  33. package/esm/common/relay-protocol.js +1 -1
  34. package/esm/common/relay-room-app.d.ts +106 -0
  35. package/esm/common/relay-room-app.js +171 -0
  36. package/esm/common/relay-room-chat.d.ts +6 -1
  37. package/esm/common/relay-room-chat.js +1 -0
  38. package/esm/guest/guest-i18n.d.ts +3 -0
  39. package/esm/guest/guest-i18n.js +12 -0
  40. package/esm/guest/guest-style.d.ts +1 -1
  41. package/esm/guest/guest-style.js +11 -0
  42. package/esm/guest/guest.js +76 -6
  43. package/esm/node/relay-server-accounts.d.ts +1 -1
  44. package/esm/node/relay-server-accounts.js +1 -1
  45. package/lib/common/index.d.ts +2 -0
  46. package/lib/common/index.js +2 -0
  47. package/lib/common/relay-account-client.d.ts +4 -0
  48. package/lib/common/relay-account-client.js +13 -3
  49. package/lib/common/relay-account.d.ts +17 -2
  50. package/lib/common/relay-account.js +15 -6
  51. package/lib/common/relay-accounts.d.ts +2 -0
  52. package/lib/common/relay-accounts.js +16 -2
  53. package/lib/common/relay-group-invites.d.ts +6 -2
  54. package/lib/common/relay-group-invites.js +6 -5
  55. package/lib/common/relay-hub-charter.d.ts +79 -0
  56. package/lib/common/relay-hub-charter.js +155 -0
  57. package/lib/common/relay-hub-epochs.d.ts +44 -0
  58. package/lib/common/relay-hub-epochs.js +95 -0
  59. package/lib/common/relay-hub-util.d.ts +39 -7
  60. package/lib/common/relay-hub-util.js +46 -15
  61. package/lib/common/relay-hub.d.ts +13 -0
  62. package/lib/common/relay-hub.js +81 -18
  63. package/lib/common/relay-hubs-admins.d.ts +28 -0
  64. package/lib/common/relay-hubs-admins.js +95 -0
  65. package/lib/common/relay-hubs-connection.d.ts +19 -63
  66. package/lib/common/relay-hubs-connection.js +106 -94
  67. package/lib/common/relay-hubs-identity.d.ts +7 -1
  68. package/lib/common/relay-hubs-identity.js +28 -1
  69. package/lib/common/relay-hubs-live.d.ts +59 -0
  70. package/lib/common/relay-hubs-live.js +31 -0
  71. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  72. package/lib/common/relay-hubs-util.d.ts +9 -0
  73. package/lib/common/relay-hubs-util.js +23 -1
  74. package/lib/common/relay-hubs.d.ts +15 -15
  75. package/lib/common/relay-hubs.js +82 -63
  76. package/lib/common/relay-protocol.d.ts +14 -3
  77. package/lib/common/relay-protocol.js +1 -1
  78. package/lib/common/relay-room-app.d.ts +107 -0
  79. package/lib/common/relay-room-app.js +176 -0
  80. package/lib/common/relay-room-chat.d.ts +6 -1
  81. package/lib/common/relay-room-chat.js +1 -0
  82. package/lib/guest/guest-i18n.d.ts +3 -0
  83. package/lib/guest/guest-i18n.js +12 -0
  84. package/lib/guest/guest-style.d.ts +1 -1
  85. package/lib/guest/guest-style.js +11 -0
  86. package/lib/guest/guest.js +76 -6
  87. package/lib/node/relay-server-accounts.d.ts +1 -1
  88. package/lib/node/relay-server-accounts.js +1 -1
  89. package/package.json +3 -3
  90. package/viewer/join.html +17 -6
  91. package/viewer/room.html +26 -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,18 +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>;
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>;
100
59
  /** The state of the room has this Ta's picture as it is now (RelayHubs). */
101
60
  protected keepPicture(_live: Live): Promise<void>;
102
- /**
103
- * 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,
104
- * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
105
- */
106
- protected keepLook(live: Live): Promise<void>;
107
61
  protected guestOf(live: Live, member: string): boolean;
108
62
  protected signOf(live: Live): (member: string) => string | undefined;
109
63
  protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
110
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;
111
67
  /** Ids of those the user chose as friends. */
112
68
  protected friendIds(): Set<string>;
113
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,31 +401,47 @@ 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
  }
@@ -420,34 +449,10 @@ export class RelayHubsConnection {
420
449
  void this.keepLook(live);
421
450
  void this.keepPicture(live).catch(() => undefined);
422
451
  }
452
+ /** The roster says this Ta as it is now: its look (RelayHubsIdentity). */
453
+ async keepLook(_live) { }
423
454
  /** The state of the room has this Ta's picture as it is now (RelayHubs). */
424
455
  async keepPicture(_live) { }
425
- /**
426
- * 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,
427
- * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
428
- */
429
- async keepLook(live) {
430
- var _a, _b;
431
- if (live.record.status !== 'member' || live.record.identity || !live.roster) {
432
- return;
433
- }
434
- const identity = await this.identity();
435
- const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
436
- const self = live.roster.members.find(member => member.member === live.record.member);
437
- if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
438
- return;
439
- }
440
- if (live.record.own) {
441
- await this.writeRoster(live, members => members.map(member => (member.member === self.member ? RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
442
- return;
443
- }
444
- const owner = live.roster.members.find(member => member.owner);
445
- if (owner && live.toldLook !== look) {
446
- await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
447
- live.toldLook = look;
448
- }, () => undefined);
449
- }
450
- }
451
456
  guestOf(live, member) {
452
457
  var _a, _b;
453
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);
@@ -456,11 +461,12 @@ export class RelayHubsConnection {
456
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; };
457
462
  }
458
463
  async readEntry(live, entry, tell) {
459
- var _a, _b;
464
+ var _a;
465
+ var _b, _c, _d;
460
466
  if (!live.keys) {
461
467
  return;
462
468
  }
463
- 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));
464
470
  if (!signed || signed.m !== entry.by || typeof signed.b.key !== 'string' || (await RelayHubUtil.keyId(live.keys, signed.b.key)) !== entry.k) {
465
471
  return;
466
472
  }
@@ -468,9 +474,14 @@ export class RelayHubsConnection {
468
474
  if (before && before.version >= entry.v) {
469
475
  return;
470
476
  }
471
- if (this.guestOf(live, entry.by) && !signed.b.key.startsWith(RELAY_CHAT_KEY)) {
472
- // A guest only chats: what else it set is not taken, its version kept, so that setting it again goes on that.
473
- 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
+ }
474
485
  return;
475
486
  }
476
487
  live.state.set(signed.b.key, { value: signed.b.value, version: entry.v, by: entry.by });
@@ -484,7 +495,7 @@ export class RelayHubsConnection {
484
495
  if (!live.keys) {
485
496
  return;
486
497
  }
487
- 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 });
488
499
  if (!signed || signed.m !== from) {
489
500
  return;
490
501
  }
@@ -510,6 +521,8 @@ export class RelayHubsConnection {
510
521
  }
511
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 });
512
523
  }
524
+ /** A member set a key naming another: the owner clears it (RelayHubs). */
525
+ foreignSet(_live, _key) { }
513
526
  /** Ids of those the user chose as friends. */
514
527
  friendIds() {
515
528
  return new Set();
@@ -1,5 +1,6 @@
1
1
  import { type RelayFriends } from './relay-friends.js';
2
- import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
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 {
@@ -20,6 +21,11 @@ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
20
21
  * in the rooms this Ta is in now, and in those it connects to later.
21
22
  */
22
23
  setPicture(picture: string | null): Promise<RelayIdentity>;
24
+ /**
25
+ * The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
26
+ * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
27
+ */
28
+ protected keepLook(live: Live): Promise<void>;
23
29
  /**
24
30
  * Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
25
31
  * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
@@ -1,5 +1,6 @@
1
1
  import { RelayFriendsUtil } from './relay-friends.js';
2
- import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
2
+ import { RelayHubsConnection } from './relay-hubs-connection.js';
3
+ import { Live } from './relay-hubs-live.js';
3
4
  import { RelayHubsUtil } from './relay-hubs-util.js';
4
5
  import { RelayIdentityUtil } from './relay-identity.js';
5
6
  import { RELAY_PICTURE_KEY } from './relay-protocol.js';
@@ -48,6 +49,32 @@ export class RelayHubsIdentity extends RelayHubsConnection {
48
49
  }
49
50
  return identity;
50
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
+ }
51
78
  /**
52
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
53
80
  * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
@@ -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
+ }
@@ -0,0 +1,27 @@
1
+ export const MAX_RECONNECT_MS = 30000;
2
+ export const SKEW_MS = 5 * 60 * 1000;
3
+ /** How long a call waits for the room to be connected, or for the relay to answer. */
4
+ export const READY_MS = 15000;
5
+ export const OPEN = 1;
6
+ /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
7
+ export class Live {
8
+ constructor(record) {
9
+ this.record = record;
10
+ this.synced = false;
11
+ this.online = new Set();
12
+ this.state = new Map();
13
+ /** Asks to join, with the invitation each came with. */
14
+ this.asks = new Map();
15
+ this.done = new Map();
16
+ this.ready = [];
17
+ this.attempts = 0;
18
+ /** Connect again at once, whatever the close said. */
19
+ this.retry = false;
20
+ this.awaitingPong = false;
21
+ /** The keys of the latest epoch this Ta sealed for each member (RelayHubEpochs.seal). */
22
+ this.sealedEpochs = new Map();
23
+ /** Events are handled one after another: opening them is async. */
24
+ this.chain = Promise.resolve();
25
+ }
26
+ }
27
+ //# sourceMappingURL=relay-hubs-live.js.map
@@ -14,6 +14,10 @@ export interface RelayRoomRecord {
14
14
  token: string;
15
15
  /** base64url, 32 bytes: the key of the room, once admitted. */
16
16
  key?: string;
17
+ /** The keys of the epochs after the first, by number, the latest kept (docs/design/ta-link.md 8.7). */
18
+ epochs?: Record<string, string>;
19
+ /** The version of the latest roster taken: an older one, served again, is not. */
20
+ rosterV?: number;
17
21
  /** The owner: all of it once admitted (its keys check the roster), its id and name from the link before. */
18
22
  owner: Pick<RelayPublicIdentity, 'id' | 'name'> & Partial<RelayPublicIdentity>;
19
23
  /** This Ta made the room. */
@@ -63,6 +67,8 @@ export interface RelayRoomMember {
63
67
  /** The look of its double (RelayPublicIdentity.look). */
64
68
  look?: string;
65
69
  owner: boolean;
70
+ /** An admin the owner made (docs/design/ta-link.md 8). */
71
+ admin?: true;
66
72
  online: boolean;
67
73
  self: boolean;
68
74
  /** In a browser, without Ta: it only chats (docs/design/ta-link.md 3.8). */
@@ -77,8 +83,12 @@ export interface RelayRoomInfo {
77
83
  /** The relay it is on. */
78
84
  relay: string;
79
85
  owner: boolean;
86
+ /** This Ta is an admin of the room: it lets in, takes out and invites, as the owner. */
87
+ admin?: true;
80
88
  /** Of a room this Ta made: the members it lets in at most. */
81
89
  capacity?: number;
90
+ /** The rules its owner set, for all (RelayHubRules); none when it set none. */
91
+ rules?: Record<string, boolean>;
82
92
  ownerName: string;
83
93
  status: RelayRoomStatus;
84
94
  reason?: RelayRoomRecord['reason'];
@@ -8,4 +8,13 @@ export declare namespace RelayHubsUtil {
8
8
  * key and the owner's keys once either has them, the invitations of both, made once either made it.
9
9
  */
10
10
  function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
11
+ /**
12
+ * Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
13
+ * a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
14
+ * clear it. Keys naming nobody (a game's) are anyone's.
15
+ */
16
+ function foreign(members: readonly {
17
+ member: string;
18
+ owner?: boolean;
19
+ }[], key: string, by: string, value: unknown): boolean;
11
20
  }