@celljs/ta-relay 4.10.0 → 4.12.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 (118) hide show
  1. package/README.en.md +13 -1
  2. package/README.md +13 -1
  3. package/esm/common/index.d.ts +11 -0
  4. package/esm/common/index.js +11 -0
  5. package/esm/common/relay-blobs.d.ts +54 -0
  6. package/esm/common/relay-blobs.js +153 -0
  7. package/esm/common/relay-card.d.ts +23 -0
  8. package/esm/common/relay-card.js +30 -0
  9. package/esm/common/relay-content.d.ts +104 -0
  10. package/esm/common/relay-content.js +316 -0
  11. package/esm/common/relay-conversation.d.ts +59 -0
  12. package/esm/common/relay-conversation.js +9 -0
  13. package/esm/common/relay-faces.d.ts +32 -0
  14. package/esm/common/relay-faces.js +208 -0
  15. package/esm/common/relay-hub-util.d.ts +91 -0
  16. package/esm/common/relay-hub-util.js +164 -0
  17. package/esm/common/relay-hub.d.ts +125 -0
  18. package/esm/common/relay-hub.js +438 -0
  19. package/esm/common/relay-hubs-connection.d.ts +98 -0
  20. package/esm/common/relay-hubs-connection.js +505 -0
  21. package/esm/common/relay-hubs-protocol.d.ts +130 -0
  22. package/esm/common/relay-hubs-protocol.js +2 -0
  23. package/esm/common/relay-hubs.d.ts +61 -0
  24. package/esm/common/relay-hubs.js +351 -0
  25. package/esm/common/relay-identity.d.ts +46 -0
  26. package/esm/common/relay-identity.js +126 -0
  27. package/esm/common/relay-protocol.d.ts +211 -2
  28. package/esm/common/relay-protocol.js +53 -0
  29. package/esm/common/relay-room-chat.d.ts +109 -0
  30. package/esm/common/relay-room-chat.js +176 -0
  31. package/esm/common/relay-socket.d.ts +8 -0
  32. package/esm/common/relay-socket.js +23 -2
  33. package/esm/guest/guest-i18n.d.ts +50 -0
  34. package/esm/guest/guest-i18n.js +103 -0
  35. package/esm/guest/guest-style.d.ts +1 -0
  36. package/esm/guest/guest-style.js +85 -0
  37. package/esm/guest/guest.d.ts +1 -0
  38. package/esm/guest/guest.js +384 -0
  39. package/esm/node/index.d.ts +2 -0
  40. package/esm/node/index.js +2 -0
  41. package/esm/node/relay-host.d.ts +25 -3
  42. package/esm/node/relay-host.js +115 -4
  43. package/esm/node/relay-hub-files.d.ts +12 -0
  44. package/esm/node/relay-hub-files.js +39 -0
  45. package/esm/node/relay-server.d.ts +46 -1
  46. package/esm/node/relay-server.js +227 -1
  47. package/esm/node/relay-store.d.ts +72 -0
  48. package/esm/node/relay-store.js +123 -2
  49. package/esm/node/relay-viewer.d.ts +7 -0
  50. package/esm/node/relay-viewer.js +33 -0
  51. package/esm/viewer/viewer-i18n.d.ts +56 -0
  52. package/esm/viewer/viewer-i18n.js +117 -0
  53. package/esm/viewer/viewer-markdown.d.ts +7 -0
  54. package/esm/viewer/viewer-markdown.js +198 -0
  55. package/esm/viewer/viewer-style.d.ts +1 -0
  56. package/esm/viewer/viewer-style.js +72 -0
  57. package/esm/viewer/viewer.d.ts +8 -0
  58. package/esm/viewer/viewer.js +378 -0
  59. package/lib/common/index.d.ts +11 -0
  60. package/lib/common/index.js +11 -0
  61. package/lib/common/relay-blobs.d.ts +55 -0
  62. package/lib/common/relay-blobs.js +157 -0
  63. package/lib/common/relay-card.d.ts +24 -0
  64. package/lib/common/relay-card.js +33 -0
  65. package/lib/common/relay-content.d.ts +105 -0
  66. package/lib/common/relay-content.js +320 -0
  67. package/lib/common/relay-conversation.d.ts +60 -0
  68. package/lib/common/relay-conversation.js +12 -0
  69. package/lib/common/relay-faces.d.ts +33 -0
  70. package/lib/common/relay-faces.js +211 -0
  71. package/lib/common/relay-hub-util.d.ts +92 -0
  72. package/lib/common/relay-hub-util.js +167 -0
  73. package/lib/common/relay-hub.d.ts +126 -0
  74. package/lib/common/relay-hub.js +442 -0
  75. package/lib/common/relay-hubs-connection.d.ts +99 -0
  76. package/lib/common/relay-hubs-connection.js +510 -0
  77. package/lib/common/relay-hubs-protocol.d.ts +131 -0
  78. package/lib/common/relay-hubs-protocol.js +3 -0
  79. package/lib/common/relay-hubs.d.ts +62 -0
  80. package/lib/common/relay-hubs.js +355 -0
  81. package/lib/common/relay-identity.d.ts +47 -0
  82. package/lib/common/relay-identity.js +129 -0
  83. package/lib/common/relay-protocol.d.ts +211 -2
  84. package/lib/common/relay-protocol.js +54 -1
  85. package/lib/common/relay-room-chat.d.ts +110 -0
  86. package/lib/common/relay-room-chat.js +180 -0
  87. package/lib/common/relay-socket.d.ts +8 -0
  88. package/lib/common/relay-socket.js +23 -2
  89. package/lib/guest/guest-i18n.d.ts +51 -0
  90. package/lib/guest/guest-i18n.js +106 -0
  91. package/lib/guest/guest-style.d.ts +2 -0
  92. package/lib/guest/guest-style.js +88 -0
  93. package/lib/guest/guest.d.ts +2 -0
  94. package/lib/guest/guest.js +386 -0
  95. package/lib/node/index.d.ts +2 -0
  96. package/lib/node/index.js +2 -0
  97. package/lib/node/relay-host.d.ts +25 -3
  98. package/lib/node/relay-host.js +115 -4
  99. package/lib/node/relay-hub-files.d.ts +13 -0
  100. package/lib/node/relay-hub-files.js +43 -0
  101. package/lib/node/relay-server.d.ts +46 -1
  102. package/lib/node/relay-server.js +226 -0
  103. package/lib/node/relay-store.d.ts +72 -0
  104. package/lib/node/relay-store.js +121 -0
  105. package/lib/node/relay-viewer.d.ts +8 -0
  106. package/lib/node/relay-viewer.js +32 -0
  107. package/lib/viewer/viewer-i18n.d.ts +57 -0
  108. package/lib/viewer/viewer-i18n.js +120 -0
  109. package/lib/viewer/viewer-markdown.d.ts +8 -0
  110. package/lib/viewer/viewer-markdown.js +201 -0
  111. package/lib/viewer/viewer-style.d.ts +2 -0
  112. package/lib/viewer/viewer-style.js +75 -0
  113. package/lib/viewer/viewer.d.ts +9 -0
  114. package/lib/viewer/viewer.js +381 -0
  115. package/package.json +5 -4
  116. package/viewer/frame.html +1 -0
  117. package/viewer/page.html +80 -0
  118. package/viewer/room.html +94 -0
@@ -0,0 +1,61 @@
1
+ import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
2
+ import type { RelayFriend, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
3
+ import { type RelayPublicIdentity } from './relay-identity.js';
4
+ /**
5
+ * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
6
+ * and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
7
+ * the others; every message signed by its member and sealed with the key of the room.
8
+ */
9
+ export declare class RelayHubs extends RelayHubsConnection {
10
+ /** Connects to the rooms of rooms.json, and keeps connected. */
11
+ start(): void;
12
+ stop(): void;
13
+ onEvent(listener: (event: RelayRoomEvent) => void): () => void;
14
+ /** The name others see; the owners of rooms this Ta is in hear it. */
15
+ setName(name: string): Promise<RelayPublicIdentity>;
16
+ list(): RelayRoomInfo[];
17
+ room(id: string): RelayRoomInfo | undefined;
18
+ create(input: {
19
+ kind: string;
20
+ title: string;
21
+ view?: string;
22
+ }): Promise<RelayRoomInfo>;
23
+ /**
24
+ * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
25
+ * one for `days` (7 by default, 30 at most), the links of before no longer working.
26
+ */
27
+ invite(roomId: string, options?: {
28
+ days?: number;
29
+ renew?: boolean;
30
+ }): Promise<{
31
+ link: string;
32
+ expiresAt: number;
33
+ }>;
34
+ /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
35
+ join(text: string): Promise<RelayRoomInfo>;
36
+ /** Those asking to join the rooms of this Ta, the earliest first. */
37
+ requests(): Promise<RelayRoomRequest[]>;
38
+ /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
39
+ answer(roomId: string, askId: string, accept: boolean): Promise<void>;
40
+ /** The owner takes a member out: the relay closes it at once. */
41
+ remove(roomId: string, member: string): Promise<void>;
42
+ /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
43
+ leave(roomId: string): Promise<void>;
44
+ /** To every other member, or to one (`to`, its member id). */
45
+ send(roomId: string, data: unknown, to?: string): Promise<void>;
46
+ /** The shared state, by its names. */
47
+ getState(roomId: string, prefix?: string): Promise<Record<string, RelayRoomValue>>;
48
+ /**
49
+ * Sets a key of the shared state (`null` clears it); with `version`, only if it is still that version (0: not
50
+ * set), else `ok` is false and `version` says what it is now: read it again, and set it again.
51
+ */
52
+ setState(roomId: string, key: string, value: unknown, version?: number): Promise<{
53
+ ok: boolean;
54
+ version: number;
55
+ }>;
56
+ friends(): RelayFriend[];
57
+ removeFriend(id: string): boolean;
58
+ protected owned(roomId: string): Live;
59
+ protected member(roomId: string): Live;
60
+ protected info(live: Live): RelayRoomInfo;
61
+ }
@@ -0,0 +1,351 @@
1
+ import { RelayHubUtil } from './relay-hub-util.js';
2
+ import { Live, OPEN, READY_MS, RelayHubsConnection } from './relay-hubs-connection.js';
3
+ import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
4
+ const DAY_MS = 24 * 60 * 60 * 1000;
5
+ /**
6
+ * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
7
+ * and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
8
+ * the others; every message signed by its member and sealed with the key of the room.
9
+ */
10
+ export class RelayHubs extends RelayHubsConnection {
11
+ /** Connects to the rooms of rooms.json, and keeps connected. */
12
+ start() {
13
+ this.stopped = false;
14
+ for (const record of this.readRooms()) {
15
+ const live = new Live(record);
16
+ this.live.set(record.id, live);
17
+ if (record.status === 'member' || record.status === 'waiting') {
18
+ this.connect(live);
19
+ }
20
+ }
21
+ }
22
+ stop() {
23
+ this.stopped = true;
24
+ for (const live of this.live.values()) {
25
+ this.disconnect(live);
26
+ }
27
+ this.live.clear();
28
+ }
29
+ onEvent(listener) {
30
+ this.listeners.add(listener);
31
+ return () => this.listeners.delete(listener);
32
+ }
33
+ /** The name others see; the owners of rooms this Ta is in hear it. */
34
+ async setName(name) {
35
+ const trimmed = String(name).trim().slice(0, 40);
36
+ if (!trimmed) {
37
+ throw new Error('A name is needed.');
38
+ }
39
+ const identity = { ...(await this.identity()), name: trimmed };
40
+ this.options.store.write('identity', identity);
41
+ this.own = identity;
42
+ for (const live of this.live.values()) {
43
+ if (live.record.status !== 'member') {
44
+ continue;
45
+ }
46
+ if (live.record.own) {
47
+ await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? { ...member, name: trimmed } : member))).catch(() => undefined);
48
+ }
49
+ else if (live.roster) {
50
+ const owner = live.roster.members.find(member => member.owner);
51
+ if (owner) {
52
+ await this.sendBody(live, { name: trimmed }, owner.member).catch(() => undefined);
53
+ }
54
+ }
55
+ }
56
+ return RelayIdentityUtil.publicOf(identity);
57
+ }
58
+ list() {
59
+ return [...this.live.values()].map(live => this.info(live)).sort((a, b) => { var _a, _b; return ((_a = b.joinedAt) !== null && _a !== void 0 ? _a : b.createdAt) - ((_b = a.joinedAt) !== null && _b !== void 0 ? _b : a.createdAt); });
60
+ }
61
+ room(id) {
62
+ const live = this.live.get(id);
63
+ return live && this.info(live);
64
+ }
65
+ async create(input) {
66
+ var _a, _b;
67
+ const identity = await this.identity();
68
+ const kind = String((_a = input.kind) !== null && _a !== void 0 ? _a : '').trim();
69
+ const title = String((_b = input.title) !== null && _b !== void 0 ? _b : '')
70
+ .trim()
71
+ .slice(0, 80);
72
+ if (!kind || !title) {
73
+ throw new Error('A room needs a kind and a title.');
74
+ }
75
+ const now = Date.now();
76
+ const record = {
77
+ id: RelayHubUtil.randomId(),
78
+ relay: this.options.relay().replace(/\/+$/, ''),
79
+ kind,
80
+ title,
81
+ ...(input.view ? { view: String(input.view) } : {}),
82
+ member: RelayHubUtil.randomId(),
83
+ token: RelayHubUtil.randomKey(),
84
+ key: RelayHubUtil.randomKey(),
85
+ owner: RelayIdentityUtil.publicOf(identity),
86
+ own: true,
87
+ status: 'member',
88
+ createdAt: now,
89
+ joinedAt: now,
90
+ };
91
+ const live = new Live(record);
92
+ this.live.set(record.id, live);
93
+ this.save();
94
+ this.connect(live);
95
+ await this.ready(live);
96
+ if (!live.roster) {
97
+ const self = { ...RelayIdentityUtil.publicOf(identity), member: record.member, owner: true, at: now };
98
+ await this.writeRoster(live, () => [self]);
99
+ }
100
+ this.emit({ type: 'rooms' });
101
+ return this.info(live);
102
+ }
103
+ /**
104
+ * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
105
+ * one for `days` (7 by default, 30 at most), the links of before no longer working.
106
+ */
107
+ async invite(roomId, options = {}) {
108
+ var _a;
109
+ var _b, _c;
110
+ const live = this.owned(roomId);
111
+ const identity = await this.identity();
112
+ const linkOf = (invite) => ({
113
+ link: RelayHubUtil.link({
114
+ relay: live.record.relay,
115
+ room: live.record.id,
116
+ invite: invite.secret,
117
+ owner: identity.id,
118
+ title: live.record.title,
119
+ by: identity.name,
120
+ }),
121
+ expiresAt: invite.expiresAt,
122
+ });
123
+ const latest = (_a = live.record.invites) === null || _a === void 0 ? void 0 : _a.at(-1);
124
+ if (latest && !options.renew && latest.expiresAt - Date.now() > DAY_MS) {
125
+ return linkOf(latest);
126
+ }
127
+ const secret = RelayHubUtil.randomKey();
128
+ const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), 30) * DAY_MS;
129
+ await this.ready(live);
130
+ this.request(live, { t: 'invite', hash: await RelayKeys.hash(await RelayHubUtil.inviteProof(secret)), expiresAt });
131
+ // Asks sealed with an invitation of before are still read, while they wait.
132
+ live.record.invites = [...((_c = live.record.invites) !== null && _c !== void 0 ? _c : []), { secret, expiresAt }].slice(-3);
133
+ this.save();
134
+ return linkOf({ secret, expiresAt });
135
+ }
136
+ /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
137
+ async join(text) {
138
+ const link = RelayHubUtil.parse(text);
139
+ const identity = await this.identity();
140
+ const known = this.live.get(link.room);
141
+ if (known && (known.record.status === 'member' || known.record.status === 'waiting')) {
142
+ return this.info(known);
143
+ }
144
+ if (link.owner === identity.id) {
145
+ throw new Error('This is a room of your own.');
146
+ }
147
+ const record = {
148
+ id: link.room,
149
+ relay: link.relay,
150
+ kind: '',
151
+ title: link.title,
152
+ member: RelayHubUtil.randomId(),
153
+ token: RelayHubUtil.randomKey(),
154
+ owner: { id: link.owner, name: link.by },
155
+ own: false,
156
+ status: 'waiting',
157
+ invite: link.invite,
158
+ createdAt: Date.now(),
159
+ };
160
+ if (known) {
161
+ this.disconnect(known);
162
+ }
163
+ const live = new Live(record);
164
+ this.live.set(record.id, live);
165
+ this.save();
166
+ this.connect(live);
167
+ this.emit({ type: 'rooms' });
168
+ return this.info(live);
169
+ }
170
+ /** Those asking to join the rooms of this Ta, the earliest first. */
171
+ async requests() {
172
+ const friends = this.friendIds();
173
+ const requests = [];
174
+ for (const live of this.live.values()) {
175
+ for (const ask of live.asks.values()) {
176
+ requests.push(this.requestOf(live, ask.body, friends));
177
+ }
178
+ }
179
+ return requests.sort((a, b) => a.at - b.at);
180
+ }
181
+ /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
182
+ async answer(roomId, askId, accept) {
183
+ const live = this.owned(roomId);
184
+ const asked = live.asks.get(askId);
185
+ if (!asked) {
186
+ throw new Error('Nobody asks that now.');
187
+ }
188
+ await this.ready(live);
189
+ if (!accept) {
190
+ this.request(live, { t: 'refuse', ask: askId });
191
+ }
192
+ else {
193
+ const identity = await this.identity();
194
+ const body = {
195
+ key: live.record.key,
196
+ kind: live.record.kind,
197
+ title: live.record.title,
198
+ ...(live.record.view ? { view: live.record.view } : {}),
199
+ };
200
+ const ask = asked.body;
201
+ const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
202
+ const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
203
+ const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
204
+ this.request(live, { t: 'admit', ask: askId, answer, roster });
205
+ // A guest of a browser is no Ta friend: its keys are of that browser alone.
206
+ if (!member.guest) {
207
+ this.meet(live.record.title, [member]);
208
+ }
209
+ }
210
+ live.asks.delete(askId);
211
+ this.emit({ type: 'rooms' });
212
+ }
213
+ /** The owner takes a member out: the relay closes it at once. */
214
+ async remove(roomId, member) {
215
+ const live = this.owned(roomId);
216
+ if (member === live.record.member) {
217
+ throw new Error('The owner leaves by closing the room.');
218
+ }
219
+ await this.ready(live);
220
+ const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member));
221
+ this.request(live, { t: 'remove', member, roster });
222
+ }
223
+ /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
224
+ async leave(roomId) {
225
+ var _a;
226
+ const live = this.live.get(roomId);
227
+ if (!live) {
228
+ return;
229
+ }
230
+ if (live.record.status === 'member') {
231
+ await this.ready(live).catch(() => undefined);
232
+ if (((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced) {
233
+ this.request(live, { t: live.record.own ? 'close' : 'leave' });
234
+ }
235
+ }
236
+ this.disconnect(live);
237
+ this.live.delete(roomId);
238
+ this.save();
239
+ this.emit({ type: 'rooms' });
240
+ }
241
+ /** To every other member, or to one (`to`, its member id). */
242
+ async send(roomId, data, to) {
243
+ await this.sendBody(this.member(roomId), { d: data }, to);
244
+ }
245
+ /** The shared state, by its names. */
246
+ async getState(roomId, prefix) {
247
+ const live = this.member(roomId);
248
+ await this.ready(live).catch(() => undefined);
249
+ const entries = [...live.state];
250
+ return Object.fromEntries(prefix ? entries.filter(([key]) => key.startsWith(prefix)) : entries);
251
+ }
252
+ /**
253
+ * Sets a key of the shared state (`null` clears it); with `version`, only if it is still that version (0: not
254
+ * set), else `ok` is false and `version` says what it is now: read it again, and set it again.
255
+ */
256
+ async setState(roomId, key, value, version) {
257
+ const live = this.member(roomId);
258
+ const name = String(key);
259
+ if (!name || name.length > 200) {
260
+ throw new Error('A key of 1 to 200 characters is needed.');
261
+ }
262
+ await this.ready(live);
263
+ const identity = await this.identity();
264
+ const keys = live.keys;
265
+ const d = await RelayHubUtil.seal(keys.seal, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
266
+ const k = await RelayHubUtil.keyId(keys, name);
267
+ const id = this.nextId++;
268
+ const result = await new Promise((resolve, reject) => {
269
+ const timer = setTimeout(() => {
270
+ live.done.delete(id);
271
+ reject(new Error('The relay did not answer.'));
272
+ }, READY_MS);
273
+ live.done.set(id, answer => {
274
+ clearTimeout(timer);
275
+ resolve(answer);
276
+ });
277
+ try {
278
+ this.request(live, { t: 'set', id, k, d, ...(version === undefined ? {} : { v: version }) });
279
+ }
280
+ catch (error) {
281
+ clearTimeout(timer);
282
+ live.done.delete(id);
283
+ reject(error);
284
+ }
285
+ });
286
+ if (result.error) {
287
+ throw new Error(result.error === 'large'
288
+ ? 'The value is too large.'
289
+ : result.error === 'limit'
290
+ ? 'The shared state of the room is full.'
291
+ : 'The relay refused it.');
292
+ }
293
+ if (result.ok) {
294
+ live.state.set(name, { value: value !== null && value !== void 0 ? value : null, version: result.v, by: live.record.member });
295
+ this.emit({ type: 'state', room: roomId, key: name, value: value !== null && value !== void 0 ? value : null, version: result.v, by: live.record.member });
296
+ }
297
+ return { ok: result.ok, version: result.v };
298
+ }
299
+ friends() {
300
+ return this.readFriends().sort((a, b) => b.seenAt - a.seenAt);
301
+ }
302
+ removeFriend(id) {
303
+ const friends = this.readFriends();
304
+ const left = friends.filter(friend => friend.id !== id);
305
+ this.options.store.write('friends', { friends: left });
306
+ return left.length !== friends.length;
307
+ }
308
+ owned(roomId) {
309
+ const live = this.member(roomId);
310
+ if (!live.record.own) {
311
+ throw new Error('Only the owner of the room does that.');
312
+ }
313
+ return live;
314
+ }
315
+ member(roomId) {
316
+ const live = this.live.get(roomId);
317
+ if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
318
+ throw new Error('Not in that room.');
319
+ }
320
+ return live;
321
+ }
322
+ info(live) {
323
+ var _a, _b;
324
+ var _c;
325
+ const { record } = live;
326
+ const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
327
+ member: member.member,
328
+ id: member.id,
329
+ name: member.name,
330
+ owner: !!member.owner,
331
+ online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
332
+ self: member.member === record.member,
333
+ ...(member.guest ? { guest: true } : {}),
334
+ }); });
335
+ return {
336
+ id: record.id,
337
+ kind: record.kind,
338
+ title: record.title,
339
+ ...(record.view ? { view: record.view } : {}),
340
+ owner: record.own,
341
+ ownerName: record.owner.name,
342
+ status: record.status,
343
+ ...(record.reason ? { reason: record.reason } : {}),
344
+ connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN && (live.synced || record.status === 'waiting'),
345
+ members,
346
+ createdAt: record.createdAt,
347
+ ...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
348
+ };
349
+ }
350
+ }
351
+ //# sourceMappingURL=relay-hubs.js.map
@@ -0,0 +1,46 @@
1
+ /** What others know of a Ta (docs/design/ta-link.md 3.1): its two public keys, raw and base64url, and the name it gives. */
2
+ export interface RelayPublicIdentity {
3
+ /** RelayIdentityUtil.id of the two keys. */
4
+ id: string;
5
+ name: string;
6
+ /** ECDSA P-256: what it signs with. */
7
+ sign: string;
8
+ /** ECDH P-256: what keys for it are agreed with. */
9
+ box: string;
10
+ }
11
+ /** A Ta's own identity, its private keys as JWK; in `identity.json` of the agent directory. */
12
+ export interface RelayIdentity extends RelayPublicIdentity {
13
+ signKey: JsonWebKey;
14
+ boxKey: JsonWebKey;
15
+ createdAt: number;
16
+ }
17
+ /** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
18
+ export declare namespace RelayKeys {
19
+ function hkdf(key: Uint8Array, salt: Uint8Array, info: string): Promise<Uint8Array>;
20
+ function sha256(bytes: Uint8Array | string): Promise<Uint8Array>;
21
+ /** base64url of SHA-256 of the text: what the relay keeps of a token. */
22
+ function hash(text: string): Promise<string>;
23
+ function hmac(key: Uint8Array, text: string): Promise<string>;
24
+ function aesKey(key: Uint8Array): Promise<CryptoKey>;
25
+ /** base64url of a random IV and AES-256-GCM of the text. */
26
+ function seal(key: CryptoKey, text: string): Promise<string>;
27
+ /** Throws when it was not sealed with this key. */
28
+ function open(key: CryptoKey, sealed: string): Promise<string>;
29
+ }
30
+ /** The identity of a Ta (docs/design/ta-link.md 3.1): made once, then what it signs with and agrees keys with. */
31
+ export declare namespace RelayIdentityUtil {
32
+ function create(name: string, now?: number): Promise<RelayIdentity>;
33
+ /** The first 16 bytes of SHA-256 of both public keys, base64url (22 characters): the same keys, the same id. */
34
+ function id(sign: string, box: string): Promise<string>;
35
+ /** Whether the keys are what the id says, and well formed. */
36
+ function check(identity: RelayPublicIdentity): Promise<boolean>;
37
+ function publicOf(identity: RelayPublicIdentity): RelayPublicIdentity;
38
+ function sign(identity: RelayIdentity, text: string): Promise<string>;
39
+ /** False for a signature that is not of this text by this key, or not a signature at all. */
40
+ function verify(signPublic: string, text: string, signature: string): Promise<boolean>;
41
+ /**
42
+ * A key only two identities can make, each with its own private key and the other's public one: HKDF of their
43
+ * ECDH, with `salt` and `info` saying what it is for.
44
+ */
45
+ function agree(identity: RelayIdentity, otherBox: string, salt: string, info: string): Promise<Uint8Array>;
46
+ }
@@ -0,0 +1,126 @@
1
+ import { RelayBytes } from './relay-bytes.js';
2
+ const encoder = new TextEncoder();
3
+ const decoder = new TextDecoder();
4
+ const subtle = () => globalThis.crypto.subtle;
5
+ const SIGN = { name: 'ECDSA', namedCurve: 'P-256' };
6
+ const BOX = { name: 'ECDH', namedCurve: 'P-256' };
7
+ const SIGNATURE = { name: 'ECDSA', hash: 'SHA-256' };
8
+ /** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
9
+ export var RelayKeys;
10
+ (function (RelayKeys) {
11
+ async function hkdf(key, salt, info) {
12
+ const secret = await subtle().importKey('raw', key, 'HKDF', false, ['deriveBits']);
13
+ const bits = await subtle().deriveBits({ name: 'HKDF', hash: 'SHA-256', salt: salt, info: encoder.encode(info) }, secret, 256);
14
+ return new Uint8Array(bits);
15
+ }
16
+ RelayKeys.hkdf = hkdf;
17
+ async function sha256(bytes) {
18
+ const data = typeof bytes === 'string' ? encoder.encode(bytes) : bytes;
19
+ return new Uint8Array(await subtle().digest('SHA-256', data));
20
+ }
21
+ RelayKeys.sha256 = sha256;
22
+ /** base64url of SHA-256 of the text: what the relay keeps of a token. */
23
+ async function hash(text) {
24
+ return RelayBytes.toBase64(await sha256(text));
25
+ }
26
+ RelayKeys.hash = hash;
27
+ async function hmac(key, text) {
28
+ const imported = await subtle().importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
29
+ return RelayBytes.toBase64(new Uint8Array(await subtle().sign('HMAC', imported, encoder.encode(text))));
30
+ }
31
+ RelayKeys.hmac = hmac;
32
+ function aesKey(key) {
33
+ return subtle().importKey('raw', key, 'AES-GCM', false, ['encrypt', 'decrypt']);
34
+ }
35
+ RelayKeys.aesKey = aesKey;
36
+ /** base64url of a random IV and AES-256-GCM of the text. */
37
+ async function seal(key, text) {
38
+ const iv = RelayBytes.random(12);
39
+ const sealed = await subtle().encrypt({ name: 'AES-GCM', iv: iv }, key, encoder.encode(text));
40
+ return RelayBytes.toBase64(RelayBytes.concat(iv, new Uint8Array(sealed)));
41
+ }
42
+ RelayKeys.seal = seal;
43
+ /** Throws when it was not sealed with this key. */
44
+ async function open(key, sealed) {
45
+ const bytes = RelayBytes.fromBase64(sealed);
46
+ const opened = await subtle().decrypt({ name: 'AES-GCM', iv: bytes.subarray(0, 12) }, key, bytes.subarray(12));
47
+ return decoder.decode(opened);
48
+ }
49
+ RelayKeys.open = open;
50
+ })(RelayKeys || (RelayKeys = {}));
51
+ /** The identity of a Ta (docs/design/ta-link.md 3.1): made once, then what it signs with and agrees keys with. */
52
+ export var RelayIdentityUtil;
53
+ (function (RelayIdentityUtil) {
54
+ async function create(name, now = Date.now()) {
55
+ const sign = (await subtle().generateKey(SIGN, true, ['sign', 'verify']));
56
+ const box = (await subtle().generateKey(BOX, true, ['deriveBits']));
57
+ const raw = async (key) => RelayBytes.toBase64(new Uint8Array(await subtle().exportKey('raw', key)));
58
+ const [signPublic, boxPublic] = await Promise.all([raw(sign.publicKey), raw(box.publicKey)]);
59
+ return {
60
+ id: await id(signPublic, boxPublic),
61
+ name,
62
+ sign: signPublic,
63
+ box: boxPublic,
64
+ signKey: await subtle().exportKey('jwk', sign.privateKey),
65
+ boxKey: await subtle().exportKey('jwk', box.privateKey),
66
+ createdAt: now,
67
+ };
68
+ }
69
+ RelayIdentityUtil.create = create;
70
+ /** The first 16 bytes of SHA-256 of both public keys, base64url (22 characters): the same keys, the same id. */
71
+ async function id(sign, box) {
72
+ const digest = await RelayKeys.sha256(RelayBytes.concat(RelayBytes.fromBase64(sign), RelayBytes.fromBase64(box)));
73
+ return RelayBytes.toBase64(digest.subarray(0, 16));
74
+ }
75
+ RelayIdentityUtil.id = id;
76
+ /** Whether the keys are what the id says, and well formed. */
77
+ async function check(identity) {
78
+ try {
79
+ if (identity.id !== (await id(identity.sign, identity.box))) {
80
+ return false;
81
+ }
82
+ await Promise.all([importSign(identity.sign), importBox(identity.box)]);
83
+ return true;
84
+ }
85
+ catch (_a) {
86
+ return false;
87
+ }
88
+ }
89
+ RelayIdentityUtil.check = check;
90
+ function publicOf(identity) {
91
+ return { id: identity.id, name: identity.name, sign: identity.sign, box: identity.box };
92
+ }
93
+ RelayIdentityUtil.publicOf = publicOf;
94
+ async function sign(identity, text) {
95
+ const key = await subtle().importKey('jwk', identity.signKey, SIGN, false, ['sign']);
96
+ return RelayBytes.toBase64(new Uint8Array(await subtle().sign(SIGNATURE, key, encoder.encode(text))));
97
+ }
98
+ RelayIdentityUtil.sign = sign;
99
+ /** False for a signature that is not of this text by this key, or not a signature at all. */
100
+ async function verify(signPublic, text, signature) {
101
+ try {
102
+ return await subtle().verify(SIGNATURE, await importSign(signPublic), RelayBytes.fromBase64(signature), encoder.encode(text));
103
+ }
104
+ catch (_a) {
105
+ return false;
106
+ }
107
+ }
108
+ RelayIdentityUtil.verify = verify;
109
+ /**
110
+ * A key only two identities can make, each with its own private key and the other's public one: HKDF of their
111
+ * ECDH, with `salt` and `info` saying what it is for.
112
+ */
113
+ async function agree(identity, otherBox, salt, info) {
114
+ const own = await subtle().importKey('jwk', identity.boxKey, BOX, false, ['deriveBits']);
115
+ const shared = await subtle().deriveBits({ name: 'ECDH', public: await importBox(otherBox) }, own, 256);
116
+ return RelayKeys.hkdf(new Uint8Array(shared), encoder.encode(salt), info);
117
+ }
118
+ RelayIdentityUtil.agree = agree;
119
+ function importSign(raw) {
120
+ return subtle().importKey('raw', RelayBytes.fromBase64(raw), SIGN, false, ['verify']);
121
+ }
122
+ function importBox(raw) {
123
+ return subtle().importKey('raw', RelayBytes.fromBase64(raw), BOX, false, []);
124
+ }
125
+ })(RelayIdentityUtil || (RelayIdentityUtil = {}));
126
+ //# sourceMappingURL=relay-identity.js.map