@celljs/ta-relay 4.16.0 → 4.18.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 (106) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-content.d.ts +13 -0
  4. package/esm/common/relay-content.js +42 -0
  5. package/esm/common/relay-conversation.d.ts +14 -0
  6. package/esm/common/relay-faces.d.ts +1 -1
  7. package/esm/common/relay-faces.js +56 -1
  8. package/esm/common/relay-files.d.ts +14 -3
  9. package/esm/common/relay-files.js +41 -16
  10. package/esm/common/relay-friend-sends.d.ts +64 -0
  11. package/esm/common/relay-friend-sends.js +126 -0
  12. package/esm/common/relay-group-invites.d.ts +76 -0
  13. package/esm/common/relay-group-invites.js +163 -0
  14. package/esm/common/relay-hub.js +2 -2
  15. package/esm/common/relay-hubs-identity.d.ts +36 -0
  16. package/esm/common/relay-hubs-identity.js +156 -0
  17. package/esm/common/relay-hubs-protocol.d.ts +7 -1
  18. package/esm/common/relay-hubs-util.d.ts +11 -0
  19. package/esm/common/relay-hubs-util.js +74 -0
  20. package/esm/common/relay-hubs.d.ts +36 -44
  21. package/esm/common/relay-hubs.js +95 -235
  22. package/esm/common/relay-open-in-ta.d.ts +11 -0
  23. package/esm/common/relay-open-in-ta.js +34 -0
  24. package/esm/common/relay-protocol.d.ts +11 -2
  25. package/esm/common/relay-protocol.js +18 -2
  26. package/esm/common/relay-spot.d.ts +15 -3
  27. package/esm/common/relay-spot.js +17 -5
  28. package/esm/guest/guest-i18n.d.ts +1 -0
  29. package/esm/guest/guest-i18n.js +45 -41
  30. package/esm/guest/guest-style.d.ts +1 -1
  31. package/esm/guest/guest-style.js +2 -0
  32. package/esm/guest/guest.js +7 -2
  33. package/esm/join/join-i18n.d.ts +1 -0
  34. package/esm/join/join-i18n.js +4 -0
  35. package/esm/join/join.js +4 -1
  36. package/esm/node/index.d.ts +1 -0
  37. package/esm/node/index.js +1 -0
  38. package/esm/node/relay-files-host.d.ts +2 -0
  39. package/esm/node/relay-files-host.js +24 -5
  40. package/esm/node/relay-memory-storage.d.ts +26 -0
  41. package/esm/node/relay-memory-storage.js +60 -0
  42. package/esm/node/relay-server.d.ts +14 -26
  43. package/esm/node/relay-server.js +33 -60
  44. package/esm/node/relay-store.js +4 -4
  45. package/esm/node/relays-file.d.ts +27 -0
  46. package/esm/node/relays-file.js +59 -4
  47. package/esm/viewer/viewer-i18n.d.ts +1 -0
  48. package/esm/viewer/viewer-i18n.js +4 -0
  49. package/esm/viewer/viewer-style.d.ts +1 -1
  50. package/esm/viewer/viewer-style.js +2 -0
  51. package/esm/viewer/viewer.js +36 -2
  52. package/lib/common/index.d.ts +2 -0
  53. package/lib/common/index.js +2 -0
  54. package/lib/common/relay-content.d.ts +13 -0
  55. package/lib/common/relay-content.js +43 -1
  56. package/lib/common/relay-conversation.d.ts +14 -0
  57. package/lib/common/relay-faces.d.ts +1 -1
  58. package/lib/common/relay-faces.js +56 -1
  59. package/lib/common/relay-files.d.ts +14 -3
  60. package/lib/common/relay-files.js +42 -17
  61. package/lib/common/relay-friend-sends.d.ts +65 -0
  62. package/lib/common/relay-friend-sends.js +130 -0
  63. package/lib/common/relay-group-invites.d.ts +77 -0
  64. package/lib/common/relay-group-invites.js +167 -0
  65. package/lib/common/relay-hub.js +1 -1
  66. package/lib/common/relay-hubs-identity.d.ts +37 -0
  67. package/lib/common/relay-hubs-identity.js +160 -0
  68. package/lib/common/relay-hubs-protocol.d.ts +7 -1
  69. package/lib/common/relay-hubs-util.d.ts +12 -0
  70. package/lib/common/relay-hubs-util.js +77 -0
  71. package/lib/common/relay-hubs.d.ts +36 -44
  72. package/lib/common/relay-hubs.js +94 -234
  73. package/lib/common/relay-open-in-ta.d.ts +12 -0
  74. package/lib/common/relay-open-in-ta.js +37 -0
  75. package/lib/common/relay-protocol.d.ts +11 -2
  76. package/lib/common/relay-protocol.js +20 -3
  77. package/lib/common/relay-spot.d.ts +15 -3
  78. package/lib/common/relay-spot.js +17 -5
  79. package/lib/guest/guest-i18n.d.ts +1 -0
  80. package/lib/guest/guest-i18n.js +45 -41
  81. package/lib/guest/guest-style.d.ts +1 -1
  82. package/lib/guest/guest-style.js +2 -0
  83. package/lib/guest/guest.js +7 -2
  84. package/lib/join/join-i18n.d.ts +1 -0
  85. package/lib/join/join-i18n.js +4 -0
  86. package/lib/join/join.js +4 -1
  87. package/lib/node/index.d.ts +1 -0
  88. package/lib/node/index.js +1 -0
  89. package/lib/node/relay-files-host.d.ts +2 -0
  90. package/lib/node/relay-files-host.js +23 -4
  91. package/lib/node/relay-memory-storage.d.ts +27 -0
  92. package/lib/node/relay-memory-storage.js +65 -0
  93. package/lib/node/relay-server.d.ts +14 -26
  94. package/lib/node/relay-server.js +34 -61
  95. package/lib/node/relay-store.js +3 -3
  96. package/lib/node/relays-file.d.ts +27 -0
  97. package/lib/node/relays-file.js +58 -3
  98. package/lib/viewer/viewer-i18n.d.ts +1 -0
  99. package/lib/viewer/viewer-i18n.js +4 -0
  100. package/lib/viewer/viewer-style.d.ts +1 -1
  101. package/lib/viewer/viewer-style.js +2 -0
  102. package/lib/viewer/viewer.js +35 -1
  103. package/package.json +3 -2
  104. package/viewer/join.html +12 -3
  105. package/viewer/page.html +18 -9
  106. package/viewer/room.html +21 -10
@@ -1,8 +1,11 @@
1
+ import { RelayFriendSends } from './relay-friend-sends.js';
1
2
  import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
3
+ import { RelayGroupInvites } from './relay-group-invites.js';
2
4
  import { RelayHubUtil } from './relay-hub-util.js';
3
- import { Live, OPEN, READY_MS, RelayHubsConnection } from './relay-hubs-connection.js';
5
+ import { Live, OPEN, READY_MS } from './relay-hubs-connection.js';
6
+ import { RelayHubsIdentity } from './relay-hubs-identity.js';
4
7
  import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
5
- import { RELAY_FRIENDS_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
8
+ import { RELAY_FRIENDS_MAX, RELAY_INVITE_MAX_DAYS, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
6
9
  const DAY_MS = 24 * 60 * 60 * 1000;
7
10
  /**
8
11
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
@@ -10,7 +13,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
10
13
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
11
14
  * their own (RelayFriends).
12
15
  */
13
- export class RelayHubs extends RelayHubsConnection {
16
+ export class RelayHubs extends RelayHubsIdentity {
14
17
  constructor() {
15
18
  super(...arguments);
16
19
  this.book = new RelayFriends({
@@ -33,6 +36,27 @@ export class RelayHubs extends RelayHubsConnection {
33
36
  setState: (room, key, value) => this.setState(room, key, value),
34
37
  offer: (room, member, link) => this.sendBody(this.member(room), { friend: link }, member),
35
38
  }, this.options.store, event => this.notify(event));
39
+ this.groups = new RelayGroupInvites({
40
+ identity: () => this.identity(),
41
+ list: () => this.list(),
42
+ record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
43
+ save: () => this.save(),
44
+ friends: () => this.friends(),
45
+ friendRoom: () => this.friendRoom(),
46
+ invite: (room, options) => this.invite(room, options),
47
+ join: link => this.join(link),
48
+ getState: (room, prefix) => this.getState(room, prefix),
49
+ setState: (room, key, value) => this.setState(room, key, value),
50
+ }, this.options.store);
51
+ this.sends = new RelayFriendSends({
52
+ identity: () => this.identity(),
53
+ list: () => this.list(),
54
+ record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
55
+ friends: () => this.friends(),
56
+ friendRoom: () => this.friendRoom(),
57
+ getState: (room, prefix) => this.getState(room, prefix),
58
+ setState: (room, key, value) => this.setState(room, key, value),
59
+ }, this.options.store);
36
60
  }
37
61
  /** Connects to the rooms of rooms.json, and keeps connected. */
38
62
  start() {
@@ -65,48 +89,6 @@ export class RelayHubs extends RelayHubsConnection {
65
89
  this.listeners.add(listener);
66
90
  return () => this.listeners.delete(listener);
67
91
  }
68
- /** The name others see; the owners of rooms this Ta is in hear it. */
69
- async setName(name) {
70
- const trimmed = String(name).trim().slice(0, 40);
71
- if (!trimmed) {
72
- throw new Error('A name is needed.');
73
- }
74
- return this.changeSelf({ ...(await this.identity()), name: trimmed });
75
- }
76
- /**
77
- * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
78
- * look that is none. The owners of rooms this Ta is in hear it, as a name.
79
- */
80
- async setLook(look) {
81
- const identity = await this.identity();
82
- const next = RelayIdentityUtil.withLook(identity, look);
83
- return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
84
- }
85
- /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
86
- async changeSelf(identity) {
87
- var _a;
88
- this.options.store.write('identity', identity);
89
- this.own = identity;
90
- for (const live of this.live.values()) {
91
- // A room joined with another identity keeps that one's name.
92
- if (live.record.status !== 'member' || live.record.identity) {
93
- continue;
94
- }
95
- if (live.record.own) {
96
- await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
97
- }
98
- else if (live.roster) {
99
- const owner = live.roster.members.find(member => member.owner);
100
- if (owner) {
101
- const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
102
- await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
103
- live.toldLook = look;
104
- }, () => undefined);
105
- }
106
- }
107
- }
108
- return RelayIdentityUtil.publicOf(identity);
109
- }
110
92
  list() {
111
93
  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); });
112
94
  }
@@ -155,7 +137,7 @@ export class RelayHubs extends RelayHubsConnection {
155
137
  }
156
138
  /**
157
139
  * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
158
- * one for `days` (7 by default, 30 at most), the links of before no longer working.
140
+ * one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
159
141
  */
160
142
  async invite(roomId, options = {}) {
161
143
  var _a;
@@ -178,7 +160,7 @@ export class RelayHubs extends RelayHubsConnection {
178
160
  return linkOf(latest);
179
161
  }
180
162
  const secret = RelayHubUtil.randomKey();
181
- const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), 30) * DAY_MS;
163
+ const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), RELAY_INVITE_MAX_DAYS) * DAY_MS;
182
164
  await this.ready(live);
183
165
  this.request(live, { t: 'invite', hash: await RelayKeys.hash(await RelayHubUtil.inviteProof(secret)), expiresAt });
184
166
  // Asks sealed with an invitation of before are still read, while they wait.
@@ -186,15 +168,17 @@ export class RelayHubs extends RelayHubsConnection {
186
168
  this.save();
187
169
  return linkOf({ secret, expiresAt });
188
170
  }
189
- /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
171
+ /**
172
+ * Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
173
+ * A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
174
+ */
190
175
  async join(text) {
191
176
  const link = RelayHubUtil.parse(text);
192
- const identity = await this.identity();
193
177
  const known = this.live.get(link.room);
194
178
  if (known && (known.record.status === 'member' || known.record.status === 'waiting')) {
195
179
  return this.info(known);
196
180
  }
197
- if (link.owner === identity.id) {
181
+ if (known === null || known === void 0 ? void 0 : known.record.own) {
198
182
  throw new Error('This is a room of your own.');
199
183
  }
200
184
  const friends = RelayFriendsUtil.isKind(link.kind);
@@ -240,7 +224,7 @@ export class RelayHubs extends RelayHubsConnection {
240
224
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
241
225
  async answer(roomId, askId, accept) {
242
226
  var _a;
243
- var _b;
227
+ var _b, _c;
244
228
  const live = this.owned(roomId);
245
229
  const asked = live.asks.get(askId);
246
230
  if (!asked) {
@@ -260,8 +244,8 @@ export class RelayHubs extends RelayHubsConnection {
260
244
  ...(live.record.unlisted ? { unlisted: true } : {}),
261
245
  };
262
246
  const ask = asked.body;
263
- const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : RELAY_ROOM_MAX_MEMBERS;
264
- if (((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _b !== void 0 ? _b : 0) >= most) {
247
+ const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : ((_b = live.record.capacity) !== null && _b !== void 0 ? _b : RELAY_ROOM_MAX_MEMBERS);
248
+ if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
265
249
  throw new Error('The room is full.');
266
250
  }
267
251
  const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
@@ -272,6 +256,27 @@ export class RelayHubs extends RelayHubsConnection {
272
256
  live.asks.delete(askId);
273
257
  this.emit({ type: 'rooms' });
274
258
  }
259
+ /**
260
+ * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
261
+ * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
262
+ */
263
+ configure(roomId, options) {
264
+ const live = this.owned(roomId);
265
+ if (options.members !== undefined) {
266
+ const members = Math.round(Number(options.members));
267
+ if (!(members >= 2 && members <= RELAY_ROOM_CAPACITY_MAX)) {
268
+ throw new Error(`A room takes 2 to ${RELAY_ROOM_CAPACITY_MAX} members.`);
269
+ }
270
+ if (members === RELAY_ROOM_MAX_MEMBERS) {
271
+ delete live.record.capacity;
272
+ }
273
+ else {
274
+ live.record.capacity = members;
275
+ }
276
+ this.save();
277
+ }
278
+ return this.info(live);
279
+ }
275
280
  /** The owner takes a member out: the relay closes it at once. */
276
281
  async remove(roomId, member) {
277
282
  const live = this.owned(roomId);
@@ -358,114 +363,6 @@ export class RelayHubs extends RelayHubsConnection {
358
363
  }
359
364
  return { ok: result.ok, version: result.v };
360
365
  }
361
- /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
362
- async exportIdentity() {
363
- return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
364
- }
365
- /**
366
- * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
367
- * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
368
- * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
369
- */
370
- async importIdentity(bundle, options = {}) {
371
- const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
372
- if (!RelayHubsUtil.isPrivate(incoming) || !(await RelayIdentityUtil.check(incoming))) {
373
- throw new Error('Not an identity of Ta.');
374
- }
375
- const own = await this.identity();
376
- const switched = incoming.id !== own.id;
377
- if (switched) {
378
- for (const live of this.live.values()) {
379
- if (!live.record.identity) {
380
- live.record = { ...live.record, identity: own };
381
- }
382
- }
383
- }
384
- const shared = options.shared || own.shared;
385
- const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
386
- if (!shared) {
387
- delete next.shared;
388
- }
389
- this.own = next;
390
- this.options.store.write('identity', next);
391
- let rooms = 0;
392
- for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
393
- if (this.upsert(record)) {
394
- rooms++;
395
- }
396
- }
397
- const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
398
- this.save();
399
- this.emit({ type: 'rooms' });
400
- return { switched, rooms, friends };
401
- }
402
- /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
403
- async setShared(shared) {
404
- const identity = { ...(await this.identity()) };
405
- if (!!identity.shared === shared) {
406
- return;
407
- }
408
- if (shared) {
409
- identity.shared = true;
410
- }
411
- else {
412
- delete identity.shared;
413
- }
414
- this.own = identity;
415
- this.options.store.write('identity', identity);
416
- }
417
- /**
418
- * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
419
- * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
420
- * as another member.
421
- */
422
- upsert(record) {
423
- if (!RelayHubsUtil.isRecord(record)) {
424
- return false;
425
- }
426
- const live = this.live.get(record.id);
427
- if (!live) {
428
- const added = new Live({ ...record });
429
- this.live.set(record.id, added);
430
- if (record.status === 'member' || record.status === 'waiting') {
431
- this.connect(added);
432
- }
433
- this.save();
434
- this.emit({ type: 'rooms' });
435
- return true;
436
- }
437
- if (live.record.member !== record.member) {
438
- return false;
439
- }
440
- const merged = RelayHubsUtil.merge(live.record, record);
441
- if (JSON.stringify(merged) === JSON.stringify(live.record)) {
442
- return false;
443
- }
444
- const was = live.record.status;
445
- live.record = merged;
446
- if (merged.status !== 'member' && merged.status !== 'waiting') {
447
- this.disconnect(live);
448
- }
449
- else if (was !== merged.status || !live.socket) {
450
- this.disconnect(live);
451
- this.connect(live);
452
- }
453
- this.save();
454
- this.emit({ type: 'rooms' });
455
- return true;
456
- }
457
- /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
458
- forget(roomId, member) {
459
- const live = this.live.get(roomId);
460
- if (!live || live.record.member !== member) {
461
- return false;
462
- }
463
- this.disconnect(live);
464
- this.live.delete(roomId);
465
- this.save();
466
- this.emit({ type: 'rooms' });
467
- return true;
468
- }
469
366
  /** Friends of the user's other computer, with those of this Ta. */
470
367
  mergeFriends(friends) {
471
368
  return this.book.merge(friends);
@@ -505,6 +402,31 @@ export class RelayHubs extends RelayHubsConnection {
505
402
  removeFriend(id) {
506
403
  return this.book.remove(id);
507
404
  }
405
+ /** Invites friends into a group chat of the user's own: they join on their word, let in at once. How many were. */
406
+ inviteFriends(roomId, friends) {
407
+ return this.groups.invite(roomId, friends);
408
+ }
409
+ /** The group chats friends invited the user into. */
410
+ groupInvites() {
411
+ return this.groups.list();
412
+ }
413
+ /** The user's answer to an invitation into a group chat. */
414
+ answerGroupInvite(roomId, accept) {
415
+ return this.groups.answer(roomId, accept);
416
+ }
417
+ /** Sends a link to friends from the share sheet: how many it went to. */
418
+ sendToFriends(friends, item) {
419
+ return this.sends.send(friends, item);
420
+ }
421
+ /** What friends sent the user. */
422
+ friendShares() {
423
+ return this.sends.list();
424
+ }
425
+ /** The user opened one (`keep`) or let it go. */
426
+ answerFriendShare(id, keep) {
427
+ this.sends.answer(id, keep);
428
+ this.emit({ type: 'rooms' });
429
+ }
508
430
  isFriends(live) {
509
431
  return live.record.own && live.record.kind === RELAY_FRIENDS_KIND;
510
432
  }
@@ -515,6 +437,10 @@ export class RelayHubs extends RelayHubsConnection {
515
437
  if (this.isFriends(live)) {
516
438
  this.book.asked(ask);
517
439
  }
440
+ else if (this.groups.admits(live.record, ask)) {
441
+ // A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
442
+ void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
443
+ }
518
444
  else {
519
445
  super.heardAsk(live, ask);
520
446
  }
@@ -531,9 +457,15 @@ export class RelayHubs extends RelayHubsConnection {
531
457
  }
532
458
  /** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
533
459
  after(event) {
460
+ var _a;
534
461
  if (!this.options.guest && (event.type === 'rooms' || (event.type === 'state' && event.key.startsWith('back:')))) {
535
462
  this.book.changed();
536
463
  }
464
+ // A friend invited the user into a group chat, or took it back: the pages read the rooms again.
465
+ const record = (_a = this.live.get(event.type === 'state' ? event.room : '')) === null || _a === void 0 ? void 0 : _a.record;
466
+ if (!this.options.guest && event.type === 'state' && (RelayGroupInvites.heard(record, event.key) || RelayFriendSends.heard(record, event.key))) {
467
+ this.emit({ type: 'rooms' });
468
+ }
537
469
  }
538
470
  owned(roomId) {
539
471
  const live = this.member(roomId);
@@ -551,7 +483,7 @@ export class RelayHubs extends RelayHubsConnection {
551
483
  }
552
484
  info(live) {
553
485
  var _a, _b;
554
- var _c;
486
+ var _c, _d;
555
487
  const { record } = live;
556
488
  const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
557
489
  member: member.member,
@@ -571,6 +503,7 @@ export class RelayHubs extends RelayHubsConnection {
571
503
  ...(record.unlisted ? { unlisted: true } : {}),
572
504
  relay: record.relay,
573
505
  owner: record.own,
506
+ ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : RELAY_ROOM_MAX_MEMBERS } : {}),
574
507
  ownerName: record.owner.name,
575
508
  status: record.status,
576
509
  ...(record.reason ? { reason: record.reason } : {}),
@@ -581,77 +514,4 @@ export class RelayHubs extends RelayHubsConnection {
581
514
  };
582
515
  }
583
516
  }
584
- /** How far a membership has come: one computer heard it end, it ended for both. */
585
- const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
586
- export var RelayHubsUtil;
587
- (function (RelayHubsUtil) {
588
- function isPrivate(identity) {
589
- const value = identity;
590
- return (!!value &&
591
- typeof value.id === 'string' &&
592
- typeof value.name === 'string' &&
593
- typeof value.sign === 'string' &&
594
- typeof value.box === 'string' &&
595
- !!value.signKey &&
596
- typeof value.signKey === 'object' &&
597
- !!value.boxKey &&
598
- typeof value.boxKey === 'object');
599
- }
600
- RelayHubsUtil.isPrivate = isPrivate;
601
- function isRecord(record) {
602
- const value = record;
603
- return (!!value &&
604
- typeof value.id === 'string' &&
605
- typeof value.relay === 'string' &&
606
- typeof value.member === 'string' &&
607
- typeof value.token === 'string' &&
608
- typeof value.status === 'string' &&
609
- value.status in STATUS_RANK &&
610
- !!value.owner &&
611
- typeof value.owner.id === 'string' &&
612
- (value.identity === undefined || isPrivate(value.identity)));
613
- }
614
- RelayHubsUtil.isRecord = isRecord;
615
- /**
616
- * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
617
- * key and the owner's keys once either has them, the invitations of both, made once either made it.
618
- */
619
- function merge(local, other) {
620
- var _a, _b, _c, _d, _e, _f;
621
- const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
622
- const behind = ahead === local ? other : local;
623
- const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
624
- .filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
625
- .sort((a, b) => a.expiresAt - b.expiresAt)
626
- .slice(-3);
627
- const merged = {
628
- ...behind,
629
- ...ahead,
630
- kind: ahead.kind || behind.kind,
631
- owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
632
- createdAt: Math.min(local.createdAt, other.createdAt),
633
- };
634
- const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
635
- const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
636
- const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
637
- delete merged.key;
638
- delete merged.invites;
639
- delete merged.joinedAt;
640
- delete merged.identity;
641
- delete merged.made;
642
- if (merged.status !== 'waiting') {
643
- delete merged.invite;
644
- delete merged.asked;
645
- }
646
- return {
647
- ...merged,
648
- ...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
649
- ...(invites.length ? { invites } : {}),
650
- ...(local.made || other.made ? { made: true } : {}),
651
- ...(joinedAt ? { joinedAt } : {}),
652
- ...(identity ? { identity } : {}),
653
- };
654
- }
655
- RelayHubsUtil.merge = merge;
656
- })(RelayHubsUtil || (RelayHubsUtil = {}));
657
517
  //# sourceMappingURL=relay-hubs.js.map
@@ -0,0 +1,11 @@
1
+ /**
2
+ * A link of Ta handed to the Ta app by the system (docs/design/ta-links.md 5): `ta://open?link=<the link>`, which the
3
+ * pages of the relay offer as "Open in Ta" and the app takes as its own (a scheme of its own on the computer and the
4
+ * phone). The link inside opens as any link of Ta, asking first wherever it would do something. Pure, for the tests.
5
+ */
6
+ export declare namespace RelayOpenInTa {
7
+ const SCHEME = "ta:";
8
+ function link(href: string): string;
9
+ /** The link of Ta inside, or undefined for what is not one (another `ta://`, a page of the web, one too long). */
10
+ function inner(text: string): string | undefined;
11
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * A link of Ta handed to the Ta app by the system (docs/design/ta-links.md 5): `ta://open?link=<the link>`, which the
3
+ * pages of the relay offer as "Open in Ta" and the app takes as its own (a scheme of its own on the computer and the
4
+ * phone). The link inside opens as any link of Ta, asking first wherever it would do something. Pure, for the tests.
5
+ */
6
+ export var RelayOpenInTa;
7
+ (function (RelayOpenInTa) {
8
+ RelayOpenInTa.SCHEME = 'ta:';
9
+ const MAX = 4096;
10
+ function link(href) {
11
+ return `ta://open?link=${encodeURIComponent(href)}`;
12
+ }
13
+ RelayOpenInTa.link = link;
14
+ /** The link of Ta inside, or undefined for what is not one (another `ta://`, a page of the web, one too long). */
15
+ function inner(text) {
16
+ const trimmed = text.trim();
17
+ if (trimmed.length > MAX * 3 || !/^ta:\/\/open\?/i.test(trimmed)) {
18
+ return undefined;
19
+ }
20
+ const found = /[?&]link=([^&#]+)/.exec(trimmed);
21
+ if (!found) {
22
+ return undefined;
23
+ }
24
+ try {
25
+ const link = decodeURIComponent(found[1]).trim();
26
+ return /^https?:\/\/\S+$/i.test(link) && link.length <= MAX ? link : undefined;
27
+ }
28
+ catch (_a) {
29
+ return undefined;
30
+ }
31
+ }
32
+ RelayOpenInTa.inner = inner;
33
+ })(RelayOpenInTa || (RelayOpenInTa = {}));
34
+ //# sourceMappingURL=relay-open-in-ta.js.map
@@ -1,5 +1,10 @@
1
1
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
2
2
  export declare const RELAY_DEFAULT_URL = "https://relay.cellbang.com";
3
+ /**
4
+ * The relay of this deployment (docs/design/ta-deploy.md 3): TA_RELAY_URL, which a distribution of Ta sets for its own
5
+ * people, else the official one. New pairings, rooms and links go there unless relays.json says otherwise.
6
+ */
7
+ export declare function relayDefaultUrl(env?: Record<string, string | undefined> | undefined): string;
3
8
  /** What the relay serves: hosts connect to the first, devices to the second. */
4
9
  export declare const RELAY_HOST_PATH = "/v1/host/";
5
10
  export declare const RELAY_CONNECT_PATH = "/v1/connect/";
@@ -35,9 +40,9 @@ export declare const RELAY_VIEWER_HEADERS: {
35
40
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
36
41
  export declare const RELAY_BLOB_PATH = "/v1/blob/";
37
42
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
38
- export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p></body>";
43
+ export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p><p><a id=\"open\" style=\"display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600\">Open in Ta \u00B7 \u5728 Ta \u4E2D\u6253\u5F00</a></p>\n<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script></body>";
39
44
  /** The page a link of an extension opens in a browser. */
40
- export declare const RELAY_SHARE_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>\n<p>\u6709\u4EBA\u5206\u4EAB\u4E86\u4E00\u4E2A Ta \u6269\u5C55\u3002\u5728 Ta \u5E94\u7528\u91CC\u626B\u7801\uFF0C\u6216\u628A\u8FD9\u4E2A\u94FE\u63A5\u7C98\u8D34\u5230\u201C\u5FEB\u6377\u6253\u5F00\u201D\uFF0C\u5C31\u80FD\u6253\u5F00\u5B83\u3002</p></body>";
45
+ export declare const RELAY_SHARE_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>\n<p>\u6709\u4EBA\u5206\u4EAB\u4E86\u4E00\u4E2A Ta \u6269\u5C55\u3002\u5728 Ta \u5E94\u7528\u91CC\u626B\u7801\uFF0C\u6216\u628A\u8FD9\u4E2A\u94FE\u63A5\u7C98\u8D34\u5230\u201C\u5FEB\u6377\u6253\u5F00\u201D\uFF0C\u5C31\u80FD\u6253\u5F00\u5B83\u3002</p><p><a id=\"open\" style=\"display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600\">Open in Ta \u00B7 \u5728 Ta \u4E2D\u6253\u5F00</a></p>\n<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script></body>";
41
46
  /**
42
47
  * A package handed on through the relay (RelayShare): the host, asked on a channel by the key of the share,
43
48
  * sends what the package is, then its bytes in chunks small enough for any relay, then the end; sealed as the
@@ -170,6 +175,10 @@ export declare const RELAY_ROOM_PATH = "/r";
170
175
  export declare const RELAY_HUB_MAX_MEMBERS = 101;
171
176
  /** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
172
177
  export declare const RELAY_ROOM_MAX_MEMBERS = 16;
178
+ /** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
179
+ export declare const RELAY_ROOM_CAPACITY_MAX = 100;
180
+ /** How long a link that invites works at most, in days. */
181
+ export declare const RELAY_INVITE_MAX_DAYS = 365;
173
182
  /** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
174
183
  export declare const RELAY_FRIENDS_MAX = 100;
175
184
  /** Characters of a sealed message or value of the state. */
@@ -1,5 +1,14 @@
1
1
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
2
2
  export const RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
3
+ /**
4
+ * The relay of this deployment (docs/design/ta-deploy.md 3): TA_RELAY_URL, which a distribution of Ta sets for its own
5
+ * people, else the official one. New pairings, rooms and links go there unless relays.json says otherwise.
6
+ */
7
+ export function relayDefaultUrl(env) {
8
+ var _a, _b;
9
+ if (env === void 0) { env = (_a = globalThis.process) === null || _a === void 0 ? void 0 : _a.env; }
10
+ return ((_b = env === null || env === void 0 ? void 0 : env.TA_RELAY_URL) === null || _b === void 0 ? void 0 : _b.trim().replace(/\/+$/, '')) || RELAY_DEFAULT_URL;
11
+ }
3
12
  /** What the relay serves: hosts connect to the first, devices to the second. */
4
13
  export const RELAY_HOST_PATH = '/v1/host/';
5
14
  export const RELAY_CONNECT_PATH = '/v1/connect/';
@@ -35,16 +44,19 @@ export const RELAY_VIEWER_HEADERS = {
35
44
  };
36
45
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
37
46
  export const RELAY_BLOB_PATH = '/v1/blob/';
47
+ /** Open in Ta on the plain pages of the relay: the link of the page, its fragment too, to the Ta app (RelayOpenInTa). */
48
+ const OPEN_IN_TA = `<p><a id="open" style="display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600">Open in Ta · 在 Ta 中打开</a></p>
49
+ <script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
38
50
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
39
51
  export const RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
40
52
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
41
53
  <h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
42
- <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p></body>`;
54
+ <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p>${OPEN_IN_TA}</body>`;
43
55
  /** The page a link of an extension opens in a browser. */
44
56
  export const RELAY_SHARE_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
45
57
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
46
58
  <h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>
47
- <p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p></body>`;
59
+ <p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p>${OPEN_IN_TA}</body>`;
48
60
  /** The largest package that is handed on: a gzipped tar of a package of the user, its code and a few assets. */
49
61
  export const RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
50
62
  /** The bytes of a chunk, base64 in a sealed message: well under the 1 MiB of a message of Cloudflare. */
@@ -96,6 +108,10 @@ export const RELAY_ROOM_PATH = '/r';
96
108
  export const RELAY_HUB_MAX_MEMBERS = 101;
97
109
  /** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
98
110
  export const RELAY_ROOM_MAX_MEMBERS = 16;
111
+ /** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
112
+ export const RELAY_ROOM_CAPACITY_MAX = 100;
113
+ /** How long a link that invites works at most, in days. */
114
+ export const RELAY_INVITE_MAX_DAYS = 365;
99
115
  /** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
100
116
  export const RELAY_FRIENDS_MAX = 100;
101
117
  /** Characters of a sealed message or value of the state. */
@@ -22,7 +22,9 @@ export declare enum RelaySpotCloseCode {
22
22
  LATE = 4410,
23
23
  FULL = 4429,
24
24
  /** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
25
- TRIES = 4428
25
+ TRIES = 4428,
26
+ /** Opened already, by one whose key this one does not hold: a new code is needed. Counted as a failure. */
27
+ TAKEN = 4409
26
28
  }
27
29
  /**
28
30
  * The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
@@ -44,12 +46,19 @@ export interface RelaySpotWho {
44
46
  /** Its id of before, for what was sealed for it just before it changed. */
45
47
  was?: string;
46
48
  opened: number;
49
+ /** The key of the spot, given to who opened it alone: with it, it comes back in (RelaySpotIn `open`). */
50
+ k?: string;
47
51
  }
48
52
  /** What one sends to its spot. */
49
53
  export type RelaySpotIn =
50
- /** First: opens it, or comes in if it is open. */
54
+ /**
55
+ * First: opens it. A spot open already lets in only who holds its key (`k`, of its `ready`), back after its connection
56
+ * dropped, the time to come in over or not; anyone else is refused and counted as a code tried (RelaySpotTries), or
57
+ * `open` would join any code untried.
58
+ */
51
59
  {
52
60
  t: 'open';
61
+ k?: string;
53
62
  }
54
63
  /** First: comes in; refused when nobody opened it. */
55
64
  | {
@@ -73,9 +82,12 @@ export type RelaySpotIn =
73
82
  t: 'bye';
74
83
  };
75
84
  /** What the relay sends to one in a spot. */
76
- export type RelaySpotOut = {
85
+ export type RelaySpotOut =
86
+ /** `k`: the key of the spot, to who opened it. */
87
+ {
77
88
  t: 'ready';
78
89
  opened: number;
90
+ k?: string;
79
91
  } | {
80
92
  t: 'here';
81
93
  b: {