@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
@@ -0,0 +1,59 @@
1
+ import type { RelayHubCharterBody } from './relay-hub-charter';
2
+ import type { RelayHubAskBody, RelayHubKeys, RelayHubRosterBody } from './relay-hub-util';
3
+ import type { RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol';
4
+ import type { RelayHubFailure } from './relay-protocol';
5
+ /** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
6
+ export interface MessageBody {
7
+ d?: unknown;
8
+ /** A member's new name, for the owner to put in the roster; with its look (RelayPublicIdentity.look), '' for none. */
9
+ name?: string;
10
+ look?: string;
11
+ /** Asks the one it is sent to to be friends: the link of the sender's room of friends (RelayFriends.offer). */
12
+ friend?: string;
13
+ }
14
+ export interface StateBody {
15
+ key: string;
16
+ value: unknown;
17
+ }
18
+ export declare const MAX_RECONNECT_MS = 30000;
19
+ export declare const SKEW_MS: number;
20
+ /** How long a call waits for the room to be connected, or for the relay to answer. */
21
+ export declare const READY_MS = 15000;
22
+ export declare const OPEN = 1;
23
+ /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
24
+ export declare class Live {
25
+ record: RelayRoomRecord;
26
+ socket?: WebSocket;
27
+ keys?: RelayHubKeys;
28
+ synced: boolean;
29
+ online: Set<string>;
30
+ roster?: RelayHubRosterBody;
31
+ /** The charter of the roster, opened: the admins and the rules (docs/design/ta-link.md 8). */
32
+ charter?: RelayHubCharterBody;
33
+ readonly state: Map<string, RelayRoomValue>;
34
+ /** Asks to join, with the invitation each came with. */
35
+ readonly asks: Map<string, {
36
+ body: RelayHubAskBody;
37
+ invite: string;
38
+ }>;
39
+ readonly done: Map<number, (result: {
40
+ ok: boolean;
41
+ v: number;
42
+ error?: RelayHubFailure;
43
+ }) => void>;
44
+ readonly ready: Array<() => void>;
45
+ attempts: number;
46
+ /** Connect again at once, whatever the close said. */
47
+ retry: boolean;
48
+ timer?: ReturnType<typeof setTimeout>;
49
+ pingTimer?: ReturnType<typeof setInterval>;
50
+ awaitingPong: boolean;
51
+ /** The look this Ta last sent the owner, for the roster to say: once, whatever the owner does with it. */
52
+ toldLook?: string;
53
+ /** The keys of the latest epoch this Ta sealed for each member (RelayHubEpochs.seal). */
54
+ readonly sealedEpochs: Map<string, string>;
55
+ /** Events are handled one after another: opening them is async. */
56
+ chain: Promise<void>;
57
+ constructor(record: RelayRoomRecord);
58
+ }
59
+ //# sourceMappingURL=relay-hubs-live.d.ts.map
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Live = exports.OPEN = exports.READY_MS = exports.SKEW_MS = exports.MAX_RECONNECT_MS = void 0;
4
+ exports.MAX_RECONNECT_MS = 30000;
5
+ exports.SKEW_MS = 5 * 60 * 1000;
6
+ /** How long a call waits for the room to be connected, or for the relay to answer. */
7
+ exports.READY_MS = 15000;
8
+ exports.OPEN = 1;
9
+ /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
10
+ class Live {
11
+ constructor(record) {
12
+ this.record = record;
13
+ this.synced = false;
14
+ this.online = new Set();
15
+ this.state = new Map();
16
+ /** Asks to join, with the invitation each came with. */
17
+ this.asks = new Map();
18
+ this.done = new Map();
19
+ this.ready = [];
20
+ this.attempts = 0;
21
+ /** Connect again at once, whatever the close said. */
22
+ this.retry = false;
23
+ this.awaitingPong = false;
24
+ /** The keys of the latest epoch this Ta sealed for each member (RelayHubEpochs.seal). */
25
+ this.sealedEpochs = new Map();
26
+ /** Events are handled one after another: opening them is async. */
27
+ this.chain = Promise.resolve();
28
+ }
29
+ }
30
+ exports.Live = Live;
31
+ //# sourceMappingURL=relay-hubs-live.js.map
@@ -14,6 +14,10 @@ export interface RelayRoomRecord {
14
14
  token: string;
15
15
  /** base64url, 32 bytes: the key of the room, once admitted. */
16
16
  key?: string;
17
+ /** The keys of the epochs after the first, by number, the latest kept (docs/design/ta-link.md 8.7). */
18
+ epochs?: Record<string, string>;
19
+ /** The version of the latest roster taken: an older one, served again, is not. */
20
+ rosterV?: number;
17
21
  /** The owner: all of it once admitted (its keys check the roster), its id and name from the link before. */
18
22
  owner: Pick<RelayPublicIdentity, 'id' | 'name'> & Partial<RelayPublicIdentity>;
19
23
  /** This Ta made the room. */
@@ -63,6 +67,8 @@ export interface RelayRoomMember {
63
67
  /** The look of its double (RelayPublicIdentity.look). */
64
68
  look?: string;
65
69
  owner: boolean;
70
+ /** An admin the owner made (docs/design/ta-link.md 8). */
71
+ admin?: true;
66
72
  online: boolean;
67
73
  self: boolean;
68
74
  /** In a browser, without Ta: it only chats (docs/design/ta-link.md 3.8). */
@@ -77,8 +83,12 @@ export interface RelayRoomInfo {
77
83
  /** The relay it is on. */
78
84
  relay: string;
79
85
  owner: boolean;
86
+ /** This Ta is an admin of the room: it lets in, takes out and invites, as the owner. */
87
+ admin?: true;
80
88
  /** Of a room this Ta made: the members it lets in at most. */
81
89
  capacity?: number;
90
+ /** The rules its owner set, for all (RelayHubRules); none when it set none. */
91
+ rules?: Record<string, boolean>;
82
92
  ownerName: string;
83
93
  status: RelayRoomStatus;
84
94
  reason?: RelayRoomRecord['reason'];
@@ -8,5 +8,14 @@ export declare namespace RelayHubsUtil {
8
8
  * key and the owner's keys once either has them, the invitations of both, made once either made it.
9
9
  */
10
10
  function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
11
+ /**
12
+ * Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
13
+ * a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
14
+ * clear it. Keys naming nobody (a game's) are anyone's.
15
+ */
16
+ function foreign(members: readonly {
17
+ member: string;
18
+ owner?: boolean;
19
+ }[], key: string, by: string, value: unknown): boolean;
11
20
  }
12
21
  //# sourceMappingURL=relay-hubs-util.d.ts.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelayHubsUtil = void 0;
4
+ const relay_hub_epochs_1 = require("./relay-hub-epochs");
4
5
  /** How far a membership has come: one computer heard it end, it ended for both. */
5
6
  const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
6
7
  var RelayHubsUtil;
@@ -37,7 +38,7 @@ var RelayHubsUtil;
37
38
  * key and the owner's keys once either has them, the invitations of both, made once either made it.
38
39
  */
39
40
  function merge(local, other) {
40
- var _a, _b, _c, _d, _e, _f;
41
+ var _a, _b, _c, _d, _e, _f, _g, _h;
41
42
  const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
42
43
  const behind = ahead === local ? other : local;
43
44
  const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
@@ -54,7 +55,12 @@ var RelayHubsUtil;
54
55
  const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
55
56
  const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
56
57
  const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
58
+ // The keys of the epochs either has (docs/design/ta-link.md 8.7), the latest kept.
59
+ const epochs = relay_hub_epochs_1.RelayHubEpochs.kept({ ...behind.epochs, ...ahead.epochs, ...local.epochs, ...other.epochs });
60
+ const rosterV = Math.max((_g = local.rosterV) !== null && _g !== void 0 ? _g : 0, (_h = other.rosterV) !== null && _h !== void 0 ? _h : 0);
57
61
  delete merged.key;
62
+ delete merged.epochs;
63
+ delete merged.rosterV;
58
64
  delete merged.invites;
59
65
  delete merged.joinedAt;
60
66
  delete merged.identity;
@@ -66,6 +72,8 @@ var RelayHubsUtil;
66
72
  return {
67
73
  ...merged,
68
74
  ...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
75
+ ...(key && merged.status === 'member' && Object.keys(epochs).length ? { epochs } : {}),
76
+ ...(rosterV ? { rosterV } : {}),
69
77
  ...(invites.length ? { invites } : {}),
70
78
  ...(local.made || other.made ? { made: true } : {}),
71
79
  ...(joinedAt ? { joinedAt } : {}),
@@ -73,5 +81,19 @@ var RelayHubsUtil;
73
81
  };
74
82
  }
75
83
  RelayHubsUtil.merge = merge;
84
+ /**
85
+ * Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
86
+ * a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
87
+ * clear it. Keys naming nobody (a game's) are anyone's.
88
+ */
89
+ function foreign(members, key, by, value) {
90
+ const parts = new Set(key.split(':'));
91
+ const named = members.find(member => parts.has(member.member));
92
+ if (!named || named.member === by) {
93
+ return false;
94
+ }
95
+ return !(value === null && members.some(member => member.member === by && member.owner));
96
+ }
97
+ RelayHubsUtil.foreign = foreign;
76
98
  })(RelayHubsUtil || (exports.RelayHubsUtil = RelayHubsUtil = {}));
77
99
  //# sourceMappingURL=relay-hubs-util.js.map
@@ -2,9 +2,9 @@ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends';
2
2
  import { RelayFriends } from './relay-friends';
3
3
  import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites';
4
4
  import { type RelayHubAskBody } from './relay-hub-util';
5
- import { Live } from './relay-hubs-connection';
6
- import { RelayHubsIdentity } from './relay-hubs-identity';
7
- import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol';
5
+ import { RelayHubsAdmins } from './relay-hubs-admins';
6
+ import { Live } from './relay-hubs-live';
7
+ import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol';
8
8
  import { type RelayPublicIdentity } from './relay-identity';
9
9
  /**
10
10
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
@@ -12,7 +12,7 @@ import { type RelayPublicIdentity } from './relay-identity';
12
12
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
13
13
  * their own (RelayFriends).
14
14
  */
15
- export declare class RelayHubs extends RelayHubsIdentity {
15
+ export declare class RelayHubs extends RelayHubsAdmins {
16
16
  protected readonly book: RelayFriends;
17
17
  protected readonly groups: RelayGroupInvites;
18
18
  protected readonly sends: RelayFriendSends;
@@ -49,14 +49,7 @@ export declare class RelayHubs extends RelayHubsIdentity {
49
49
  requests(): Promise<RelayRoomRequest[]>;
50
50
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
51
51
  answer(roomId: string, askId: string, accept: boolean): Promise<void>;
52
- /**
53
- * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
54
- * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
55
- */
56
- configure(roomId: string, options: {
57
- members?: number;
58
- }): RelayRoomInfo;
59
- /** The owner takes a member out: the relay closes it at once. */
52
+ /** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
60
53
  remove(roomId: string, member: string): Promise<void>;
61
54
  /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
62
55
  leave(roomId: string): Promise<void>;
@@ -72,6 +65,12 @@ export declare class RelayHubs extends RelayHubsIdentity {
72
65
  ok: boolean;
73
66
  version: number;
74
67
  }>;
68
+ /**
69
+ * Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
70
+ * names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
71
+ * friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
72
+ */
73
+ static owns(record: Pick<RelayRoomRecord, 'member' | 'kind' | 'own'>, name: string): boolean;
75
74
  /** Friends of the user's other computer, with those of this Ta. */
76
75
  mergeFriends(friends: RelayFriend[]): number;
77
76
  /** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
@@ -109,15 +108,13 @@ export declare class RelayHubs extends RelayHubsIdentity {
109
108
  friendShares(): Promise<RelayFriendShare[]>;
110
109
  /** The user opened one (`keep`) or let it go. */
111
110
  answerFriendShare(id: string, keep: boolean): void;
112
- protected isFriends(live: Live): boolean;
113
111
  protected friendIds(): Set<string>;
114
112
  protected heardAsk(live: Live, ask: RelayHubAskBody): void;
113
+ protected foreignSet(live: Live, key: string): void;
115
114
  protected heardMembers(members: RelayPublicIdentity[]): void;
116
115
  protected offered(live: Live, from: RelayPublicIdentity, link: string): void;
117
116
  /** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
118
117
  protected after(event: RelayRoomEvent): void;
119
- protected owned(roomId: string): Live;
120
- protected member(roomId: string): Live;
121
118
  protected info(live: Live): RelayRoomInfo;
122
119
  }
123
120
  //# sourceMappingURL=relay-hubs.d.ts.map
@@ -4,9 +4,10 @@ exports.RelayHubs = void 0;
4
4
  const relay_friend_sends_1 = require("./relay-friend-sends");
5
5
  const relay_friends_1 = require("./relay-friends");
6
6
  const relay_group_invites_1 = require("./relay-group-invites");
7
+ const relay_hub_charter_1 = require("./relay-hub-charter");
7
8
  const relay_hub_util_1 = require("./relay-hub-util");
8
- const relay_hubs_connection_1 = require("./relay-hubs-connection");
9
- const relay_hubs_identity_1 = require("./relay-hubs-identity");
9
+ const relay_hubs_admins_1 = require("./relay-hubs-admins");
10
+ const relay_hubs_live_1 = require("./relay-hubs-live");
10
11
  const relay_identity_1 = require("./relay-identity");
11
12
  const relay_protocol_1 = require("./relay-protocol");
12
13
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -16,7 +17,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
16
17
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
17
18
  * their own (RelayFriends).
18
19
  */
19
- class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
20
+ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
20
21
  constructor() {
21
22
  super(...arguments);
22
23
  this.book = new relay_friends_1.RelayFriends({
@@ -43,6 +44,10 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
43
44
  identity: () => this.identity(),
44
45
  list: () => this.list(),
45
46
  record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
47
+ manages: room => {
48
+ const live = this.live.get(room);
49
+ return !!live && this.manages(live);
50
+ },
46
51
  save: () => this.save(),
47
52
  friends: () => this.friends(),
48
53
  friendRoom: () => this.friendRoom(),
@@ -73,7 +78,7 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
73
78
  this.book.start();
74
79
  }
75
80
  for (const record of this.readRooms()) {
76
- const live = new relay_hubs_connection_1.Live(record);
81
+ const live = new relay_hubs_live_1.Live(record);
77
82
  this.live.set(record.id, live);
78
83
  if (record.status === 'member' || record.status === 'waiting') {
79
84
  this.connect(live);
@@ -126,7 +131,7 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
126
131
  createdAt: now,
127
132
  joinedAt: now,
128
133
  };
129
- const live = new relay_hubs_connection_1.Live(record);
134
+ const live = new relay_hubs_live_1.Live(record);
130
135
  this.live.set(record.id, live);
131
136
  this.save();
132
137
  this.connect(live);
@@ -145,14 +150,15 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
145
150
  async invite(roomId, options = {}) {
146
151
  var _a;
147
152
  var _b, _c;
148
- const live = this.owned(roomId);
153
+ const live = this.managed(roomId);
149
154
  const identity = await this.identityOf(live);
150
155
  const linkOf = (invite) => ({
151
156
  link: relay_hub_util_1.RelayHubUtil.link({
152
157
  relay: live.record.relay,
153
158
  room: live.record.id,
154
159
  invite: invite.secret,
155
- owner: identity.id,
160
+ // The owner's, whoever invites: the one who asks checks the answer against it (an admin's by the charter).
161
+ owner: live.record.owner.id,
156
162
  title: live.record.title,
157
163
  by: identity.name,
158
164
  }),
@@ -203,7 +209,7 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
203
209
  if (known) {
204
210
  this.disconnect(known);
205
211
  }
206
- const live = new relay_hubs_connection_1.Live(record);
212
+ const live = new relay_hubs_live_1.Live(record);
207
213
  this.live.set(record.id, live);
208
214
  this.save();
209
215
  this.connect(live);
@@ -226,9 +232,9 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
226
232
  }
227
233
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
228
234
  async answer(roomId, askId, accept) {
229
- var _a;
230
- var _b, _c;
231
- const live = this.owned(roomId);
235
+ var _a, _b;
236
+ var _c;
237
+ const live = this.managed(roomId);
232
238
  const asked = live.asks.get(askId);
233
239
  if (!asked) {
234
240
  throw new Error('Nobody asks that now.');
@@ -241,17 +247,21 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
241
247
  const identity = await this.identityOf(live);
242
248
  const body = {
243
249
  key: live.record.key,
250
+ ...(live.record.epochs && Object.keys(live.record.epochs).length ? { epochs: live.record.epochs } : {}),
244
251
  kind: live.record.kind,
245
252
  title: live.record.title,
246
253
  ...(live.record.view ? { view: live.record.view } : {}),
247
254
  ...(live.record.unlisted ? { unlisted: true } : {}),
248
255
  };
249
256
  const ask = asked.body;
250
- const most = this.isFriends(live) ? relay_protocol_1.RELAY_FRIENDS_MAX + 1 : ((_b = live.record.capacity) !== null && _b !== void 0 ? _b : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS);
251
- if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
257
+ if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= this.capacityOf(live)) {
252
258
  throw new Error('The room is full.');
253
259
  }
254
- const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
260
+ const head = live.record.owner;
261
+ const proof = !live.record.own && ((_b = live.roster) === null || _b === void 0 ? void 0 : _b.charter) && head.sign && head.box
262
+ ? relay_hub_charter_1.RelayHubCharterUtil.answerProof({ ...head, sign: head.sign, box: head.box }, live.roster.charter)
263
+ : undefined;
264
+ const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
255
265
  const member = { ...relay_identity_1.RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
256
266
  const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
257
267
  this.request(live, { t: 'admit', ask: askId, answer, roster });
@@ -259,35 +269,20 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
259
269
  live.asks.delete(askId);
260
270
  this.emit({ type: 'rooms' });
261
271
  }
262
- /**
263
- * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
264
- * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
265
- */
266
- configure(roomId, options) {
267
- const live = this.owned(roomId);
268
- if (options.members !== undefined) {
269
- const members = Math.round(Number(options.members));
270
- if (!(members >= 2 && members <= relay_protocol_1.RELAY_ROOM_CAPACITY_MAX)) {
271
- throw new Error(`A room takes 2 to ${relay_protocol_1.RELAY_ROOM_CAPACITY_MAX} members.`);
272
- }
273
- if (members === relay_protocol_1.RELAY_ROOM_MAX_MEMBERS) {
274
- delete live.record.capacity;
275
- }
276
- else {
277
- live.record.capacity = members;
278
- }
279
- this.save();
280
- }
281
- return this.info(live);
282
- }
283
- /** The owner takes a member out: the relay closes it at once. */
272
+ /** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
284
273
  async remove(roomId, member) {
285
- const live = this.owned(roomId);
274
+ var _a, _b;
275
+ const live = this.managed(roomId);
286
276
  if (member === live.record.member) {
287
277
  throw new Error('The owner leaves by closing the room.');
288
278
  }
279
+ const owner = (_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(other => other.owner)) === null || _b === void 0 ? void 0 : _b.member;
280
+ if (!live.record.own && (member === owner || this.isAdmin(live, member))) {
281
+ throw new Error('Only the owner of the room does that.');
282
+ }
289
283
  await this.ready(live);
290
- const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member));
284
+ // A new key for those left: the one taken out holds those of before alone (docs/design/ta-link.md 8.7).
285
+ const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member), undefined, true);
291
286
  this.request(live, { t: 'remove', member, roster });
292
287
  }
293
288
  /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
@@ -299,7 +294,7 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
299
294
  }
300
295
  if (live.record.status === 'member') {
301
296
  await this.ready(live).catch(() => undefined);
302
- if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_connection_1.OPEN && live.synced) {
297
+ if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_live_1.OPEN && live.synced) {
303
298
  this.request(live, { t: live.record.own ? 'close' : 'leave' });
304
299
  }
305
300
  }
@@ -332,20 +327,27 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
332
327
  await this.ready(live);
333
328
  const identity = await this.identityOf(live);
334
329
  const keys = live.keys;
335
- const d = await relay_hub_util_1.RelayHubUtil.seal(keys.seal, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
330
+ const d = await relay_hub_util_1.RelayHubUtil.sealIn(keys, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
336
331
  const k = await relay_hub_util_1.RelayHubUtil.keyId(keys, name);
337
332
  const id = this.nextId++;
338
333
  const result = await new Promise((resolve, reject) => {
339
334
  const timer = setTimeout(() => {
340
335
  live.done.delete(id);
341
336
  reject(new Error('The relay did not answer.'));
342
- }, relay_hubs_connection_1.READY_MS);
337
+ }, relay_hubs_live_1.READY_MS);
343
338
  live.done.set(id, answer => {
344
339
  clearTimeout(timer);
345
340
  resolve(answer);
346
341
  });
347
342
  try {
348
- this.request(live, { t: 'set', id, k, d, ...(version === undefined ? {} : { v: version }) });
343
+ this.request(live, {
344
+ t: 'set',
345
+ id,
346
+ k,
347
+ d,
348
+ ...(version === undefined ? {} : { v: version }),
349
+ ...(RelayHubs.owns(live.record, name) ? { own: true } : {}),
350
+ });
349
351
  }
350
352
  catch (error) {
351
353
  clearTimeout(timer);
@@ -358,7 +360,9 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
358
360
  ? 'The value is too large.'
359
361
  : result.error === 'limit'
360
362
  ? 'The shared state of the room is full.'
361
- : 'The relay refused it.');
363
+ : result.error === 'owned'
364
+ ? 'Another member keeps this key: only they or the owner of the room change it.'
365
+ : 'The relay refused it.');
362
366
  }
363
367
  if (result.ok) {
364
368
  live.state.set(name, { value: value !== null && value !== void 0 ? value : null, version: result.v, by: live.record.member });
@@ -366,6 +370,14 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
366
370
  }
367
371
  return { ok: result.ok, version: result.v };
368
372
  }
373
+ /**
374
+ * Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
375
+ * names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
376
+ * friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
377
+ */
378
+ static owns(record, name) {
379
+ return name.split(':').includes(record.member) || (record.own && relay_friends_1.RelayFriendsUtil.isKind(record.kind));
380
+ }
369
381
  /** Friends of the user's other computer, with those of this Ta. */
370
382
  mergeFriends(friends) {
371
383
  return this.book.merge(friends);
@@ -430,17 +442,15 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
430
442
  this.sends.answer(id, keep);
431
443
  this.emit({ type: 'rooms' });
432
444
  }
433
- isFriends(live) {
434
- return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
435
- }
436
445
  friendIds() {
437
446
  return this.book.ids();
438
447
  }
439
448
  heardAsk(live, ask) {
449
+ var _a;
440
450
  if (this.isFriends(live)) {
441
451
  this.book.asked(ask);
442
452
  }
443
- else if (this.groups.admits(live.record, ask)) {
453
+ else if (!((_a = live.charter) === null || _a === void 0 ? void 0 : _a.rules.approve) && this.groups.admits(live.record, ask)) {
444
454
  // A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
445
455
  void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
446
456
  }
@@ -448,6 +458,10 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
448
458
  super.heardAsk(live, ask);
449
459
  }
450
460
  }
461
+ foreignSet(live, key) {
462
+ // Not awaited: the owner clears what one set under another's name, as soon as it is in the room.
463
+ void this.setState(live.record.id, key, null).catch(() => undefined);
464
+ }
451
465
  heardMembers(members) {
452
466
  if (!this.options.guest) {
453
467
  this.book.heard(members);
@@ -470,20 +484,6 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
470
484
  this.emit({ type: 'rooms' });
471
485
  }
472
486
  }
473
- owned(roomId) {
474
- const live = this.member(roomId);
475
- if (!live.record.own) {
476
- throw new Error('Only the owner of the room does that.');
477
- }
478
- return live;
479
- }
480
- member(roomId) {
481
- const live = this.live.get(roomId);
482
- if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
483
- throw new Error('Not in that room.');
484
- }
485
- return live;
486
- }
487
487
  info(live) {
488
488
  var _a, _b;
489
489
  var _c, _d;
@@ -494,7 +494,8 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
494
494
  name: member.name,
495
495
  ...(relay_identity_1.RelayIdentityUtil.isLook(member.look) ? { look: member.look } : {}),
496
496
  owner: !!member.owner,
497
- online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_connection_1.OPEN && live.synced : live.online.has(member.member),
497
+ ...(this.isAdmin(live, member.member) ? { admin: true } : {}),
498
+ online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_live_1.OPEN && live.synced : live.online.has(member.member),
498
499
  self: member.member === record.member,
499
500
  ...(member.guest ? { guest: true } : {}),
500
501
  }); });
@@ -506,11 +507,13 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
506
507
  ...(record.unlisted ? { unlisted: true } : {}),
507
508
  relay: record.relay,
508
509
  owner: record.own,
510
+ ...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
509
511
  ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS } : {}),
512
+ ...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
510
513
  ownerName: record.owner.name,
511
514
  status: record.status,
512
515
  ...(record.reason ? { reason: record.reason } : {}),
513
- connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_connection_1.OPEN && (live.synced || record.status === 'waiting'),
516
+ connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_live_1.OPEN && (live.synced || record.status === 'waiting'),
514
517
  members,
515
518
  createdAt: record.createdAt,
516
519
  ...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
@@ -20,6 +20,11 @@ export interface RelayIdentity extends RelayPublicIdentity {
20
20
  createdAt: number;
21
21
  /** The user's other computers are this Ta too: who answers in a room is agreed first (docs/design/ta-sync.md 9). */
22
22
  shared?: true;
23
+ /**
24
+ * The picture others see of the user (RelayIdentityUtil.isPicture), instead of the dinosaur of the look: kept in the
25
+ * state of the rooms this Ta is in (RELAY_PICTURE_KEY), never in what others are given of the identity.
26
+ */
27
+ picture?: string;
23
28
  }
24
29
  /** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
25
30
  export declare namespace RelayKeys {
@@ -44,6 +49,8 @@ export declare namespace RelayIdentityUtil {
44
49
  function publicOf(identity: RelayPublicIdentity): RelayPublicIdentity;
45
50
  /** Whether it is a look as a Ta gives it: a short word of letters, digits and dashes. */
46
51
  function isLook(look: unknown): look is string;
52
+ /** Whether it is a picture as a Ta keeps it: a data URL of a PNG, JPEG, WebP or SVG, small. */
53
+ function isPicture(picture: unknown): picture is string;
47
54
  /** `value` with `look`, or without any for one that is none. */
48
55
  function withLook<T extends {
49
56
  look?: string;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelayIdentityUtil = exports.RelayKeys = void 0;
4
4
  const relay_bytes_1 = require("./relay-bytes");
5
+ const relay_protocol_1 = require("./relay-protocol");
5
6
  const encoder = new TextEncoder();
6
7
  const decoder = new TextDecoder();
7
8
  const subtle = () => globalThis.crypto.subtle;
@@ -99,6 +100,13 @@ var RelayIdentityUtil;
99
100
  return typeof look === 'string' && /^[a-z][a-z0-9-]{0,31}$/.test(look);
100
101
  }
101
102
  RelayIdentityUtil.isLook = isLook;
103
+ /** Whether it is a picture as a Ta keeps it: a data URL of a PNG, JPEG, WebP or SVG, small. */
104
+ function isPicture(picture) {
105
+ return (typeof picture === 'string' &&
106
+ picture.length <= relay_protocol_1.RELAY_PICTURE_MAX_CHARS &&
107
+ /^data:image\/(png|jpeg|webp|svg\+xml);base64,[A-Za-z0-9+/]+={0,2}$/.test(picture));
108
+ }
109
+ RelayIdentityUtil.isPicture = isPicture;
102
110
  /** `value` with `look`, or without any for one that is none. */
103
111
  function withLook(value, look) {
104
112
  const { look: _, ...rest } = value;