@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,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayHubsUtil = exports.RelayHubs = void 0;
3
+ exports.RelayHubs = void 0;
4
+ const relay_friend_sends_1 = require("./relay-friend-sends");
4
5
  const relay_friends_1 = require("./relay-friends");
6
+ const relay_group_invites_1 = require("./relay-group-invites");
5
7
  const relay_hub_util_1 = require("./relay-hub-util");
6
8
  const relay_hubs_connection_1 = require("./relay-hubs-connection");
9
+ const relay_hubs_identity_1 = require("./relay-hubs-identity");
7
10
  const relay_identity_1 = require("./relay-identity");
8
11
  const relay_protocol_1 = require("./relay-protocol");
9
12
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -13,7 +16,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
13
16
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
14
17
  * their own (RelayFriends).
15
18
  */
16
- class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
19
+ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
17
20
  constructor() {
18
21
  super(...arguments);
19
22
  this.book = new relay_friends_1.RelayFriends({
@@ -36,6 +39,27 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
36
39
  setState: (room, key, value) => this.setState(room, key, value),
37
40
  offer: (room, member, link) => this.sendBody(this.member(room), { friend: link }, member),
38
41
  }, this.options.store, event => this.notify(event));
42
+ this.groups = new relay_group_invites_1.RelayGroupInvites({
43
+ identity: () => this.identity(),
44
+ list: () => this.list(),
45
+ record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
46
+ save: () => this.save(),
47
+ friends: () => this.friends(),
48
+ friendRoom: () => this.friendRoom(),
49
+ invite: (room, options) => this.invite(room, options),
50
+ join: link => this.join(link),
51
+ getState: (room, prefix) => this.getState(room, prefix),
52
+ setState: (room, key, value) => this.setState(room, key, value),
53
+ }, this.options.store);
54
+ this.sends = new relay_friend_sends_1.RelayFriendSends({
55
+ identity: () => this.identity(),
56
+ list: () => this.list(),
57
+ record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
58
+ friends: () => this.friends(),
59
+ friendRoom: () => this.friendRoom(),
60
+ getState: (room, prefix) => this.getState(room, prefix),
61
+ setState: (room, key, value) => this.setState(room, key, value),
62
+ }, this.options.store);
39
63
  }
40
64
  /** Connects to the rooms of rooms.json, and keeps connected. */
41
65
  start() {
@@ -68,48 +92,6 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
68
92
  this.listeners.add(listener);
69
93
  return () => this.listeners.delete(listener);
70
94
  }
71
- /** The name others see; the owners of rooms this Ta is in hear it. */
72
- async setName(name) {
73
- const trimmed = String(name).trim().slice(0, 40);
74
- if (!trimmed) {
75
- throw new Error('A name is needed.');
76
- }
77
- return this.changeSelf({ ...(await this.identity()), name: trimmed });
78
- }
79
- /**
80
- * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
81
- * look that is none. The owners of rooms this Ta is in hear it, as a name.
82
- */
83
- async setLook(look) {
84
- const identity = await this.identity();
85
- const next = relay_identity_1.RelayIdentityUtil.withLook(identity, look);
86
- return next.look === identity.look ? relay_identity_1.RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
87
- }
88
- /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
89
- async changeSelf(identity) {
90
- var _a;
91
- this.options.store.write('identity', identity);
92
- this.own = identity;
93
- for (const live of this.live.values()) {
94
- // A room joined with another identity keeps that one's name.
95
- if (live.record.status !== 'member' || live.record.identity) {
96
- continue;
97
- }
98
- if (live.record.own) {
99
- await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? relay_identity_1.RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
100
- }
101
- else if (live.roster) {
102
- const owner = live.roster.members.find(member => member.owner);
103
- if (owner) {
104
- const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
105
- await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
106
- live.toldLook = look;
107
- }, () => undefined);
108
- }
109
- }
110
- }
111
- return relay_identity_1.RelayIdentityUtil.publicOf(identity);
112
- }
113
95
  list() {
114
96
  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); });
115
97
  }
@@ -158,7 +140,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
158
140
  }
159
141
  /**
160
142
  * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
161
- * one for `days` (7 by default, 30 at most), the links of before no longer working.
143
+ * one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
162
144
  */
163
145
  async invite(roomId, options = {}) {
164
146
  var _a;
@@ -181,7 +163,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
181
163
  return linkOf(latest);
182
164
  }
183
165
  const secret = relay_hub_util_1.RelayHubUtil.randomKey();
184
- const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), 30) * DAY_MS;
166
+ const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), relay_protocol_1.RELAY_INVITE_MAX_DAYS) * DAY_MS;
185
167
  await this.ready(live);
186
168
  this.request(live, { t: 'invite', hash: await relay_identity_1.RelayKeys.hash(await relay_hub_util_1.RelayHubUtil.inviteProof(secret)), expiresAt });
187
169
  // Asks sealed with an invitation of before are still read, while they wait.
@@ -189,15 +171,17 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
189
171
  this.save();
190
172
  return linkOf({ secret, expiresAt });
191
173
  }
192
- /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
174
+ /**
175
+ * Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
176
+ * A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
177
+ */
193
178
  async join(text) {
194
179
  const link = relay_hub_util_1.RelayHubUtil.parse(text);
195
- const identity = await this.identity();
196
180
  const known = this.live.get(link.room);
197
181
  if (known && (known.record.status === 'member' || known.record.status === 'waiting')) {
198
182
  return this.info(known);
199
183
  }
200
- if (link.owner === identity.id) {
184
+ if (known === null || known === void 0 ? void 0 : known.record.own) {
201
185
  throw new Error('This is a room of your own.');
202
186
  }
203
187
  const friends = relay_friends_1.RelayFriendsUtil.isKind(link.kind);
@@ -243,7 +227,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
243
227
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
244
228
  async answer(roomId, askId, accept) {
245
229
  var _a;
246
- var _b;
230
+ var _b, _c;
247
231
  const live = this.owned(roomId);
248
232
  const asked = live.asks.get(askId);
249
233
  if (!asked) {
@@ -263,8 +247,8 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
263
247
  ...(live.record.unlisted ? { unlisted: true } : {}),
264
248
  };
265
249
  const ask = asked.body;
266
- const most = this.isFriends(live) ? relay_protocol_1.RELAY_FRIENDS_MAX + 1 : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS;
267
- if (((_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _b !== void 0 ? _b : 0) >= most) {
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) {
268
252
  throw new Error('The room is full.');
269
253
  }
270
254
  const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
@@ -275,6 +259,27 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
275
259
  live.asks.delete(askId);
276
260
  this.emit({ type: 'rooms' });
277
261
  }
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
+ }
278
283
  /** The owner takes a member out: the relay closes it at once. */
279
284
  async remove(roomId, member) {
280
285
  const live = this.owned(roomId);
@@ -361,114 +366,6 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
361
366
  }
362
367
  return { ok: result.ok, version: result.v };
363
368
  }
364
- /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
365
- async exportIdentity() {
366
- return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
367
- }
368
- /**
369
- * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
370
- * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
371
- * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
372
- */
373
- async importIdentity(bundle, options = {}) {
374
- const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
375
- if (!RelayHubsUtil.isPrivate(incoming) || !(await relay_identity_1.RelayIdentityUtil.check(incoming))) {
376
- throw new Error('Not an identity of Ta.');
377
- }
378
- const own = await this.identity();
379
- const switched = incoming.id !== own.id;
380
- if (switched) {
381
- for (const live of this.live.values()) {
382
- if (!live.record.identity) {
383
- live.record = { ...live.record, identity: own };
384
- }
385
- }
386
- }
387
- const shared = options.shared || own.shared;
388
- const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
389
- if (!shared) {
390
- delete next.shared;
391
- }
392
- this.own = next;
393
- this.options.store.write('identity', next);
394
- let rooms = 0;
395
- for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
396
- if (this.upsert(record)) {
397
- rooms++;
398
- }
399
- }
400
- const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
401
- this.save();
402
- this.emit({ type: 'rooms' });
403
- return { switched, rooms, friends };
404
- }
405
- /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
406
- async setShared(shared) {
407
- const identity = { ...(await this.identity()) };
408
- if (!!identity.shared === shared) {
409
- return;
410
- }
411
- if (shared) {
412
- identity.shared = true;
413
- }
414
- else {
415
- delete identity.shared;
416
- }
417
- this.own = identity;
418
- this.options.store.write('identity', identity);
419
- }
420
- /**
421
- * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
422
- * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
423
- * as another member.
424
- */
425
- upsert(record) {
426
- if (!RelayHubsUtil.isRecord(record)) {
427
- return false;
428
- }
429
- const live = this.live.get(record.id);
430
- if (!live) {
431
- const added = new relay_hubs_connection_1.Live({ ...record });
432
- this.live.set(record.id, added);
433
- if (record.status === 'member' || record.status === 'waiting') {
434
- this.connect(added);
435
- }
436
- this.save();
437
- this.emit({ type: 'rooms' });
438
- return true;
439
- }
440
- if (live.record.member !== record.member) {
441
- return false;
442
- }
443
- const merged = RelayHubsUtil.merge(live.record, record);
444
- if (JSON.stringify(merged) === JSON.stringify(live.record)) {
445
- return false;
446
- }
447
- const was = live.record.status;
448
- live.record = merged;
449
- if (merged.status !== 'member' && merged.status !== 'waiting') {
450
- this.disconnect(live);
451
- }
452
- else if (was !== merged.status || !live.socket) {
453
- this.disconnect(live);
454
- this.connect(live);
455
- }
456
- this.save();
457
- this.emit({ type: 'rooms' });
458
- return true;
459
- }
460
- /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
461
- forget(roomId, member) {
462
- const live = this.live.get(roomId);
463
- if (!live || live.record.member !== member) {
464
- return false;
465
- }
466
- this.disconnect(live);
467
- this.live.delete(roomId);
468
- this.save();
469
- this.emit({ type: 'rooms' });
470
- return true;
471
- }
472
369
  /** Friends of the user's other computer, with those of this Ta. */
473
370
  mergeFriends(friends) {
474
371
  return this.book.merge(friends);
@@ -508,6 +405,31 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
508
405
  removeFriend(id) {
509
406
  return this.book.remove(id);
510
407
  }
408
+ /** Invites friends into a group chat of the user's own: they join on their word, let in at once. How many were. */
409
+ inviteFriends(roomId, friends) {
410
+ return this.groups.invite(roomId, friends);
411
+ }
412
+ /** The group chats friends invited the user into. */
413
+ groupInvites() {
414
+ return this.groups.list();
415
+ }
416
+ /** The user's answer to an invitation into a group chat. */
417
+ answerGroupInvite(roomId, accept) {
418
+ return this.groups.answer(roomId, accept);
419
+ }
420
+ /** Sends a link to friends from the share sheet: how many it went to. */
421
+ sendToFriends(friends, item) {
422
+ return this.sends.send(friends, item);
423
+ }
424
+ /** What friends sent the user. */
425
+ friendShares() {
426
+ return this.sends.list();
427
+ }
428
+ /** The user opened one (`keep`) or let it go. */
429
+ answerFriendShare(id, keep) {
430
+ this.sends.answer(id, keep);
431
+ this.emit({ type: 'rooms' });
432
+ }
511
433
  isFriends(live) {
512
434
  return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
513
435
  }
@@ -518,6 +440,10 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
518
440
  if (this.isFriends(live)) {
519
441
  this.book.asked(ask);
520
442
  }
443
+ else if (this.groups.admits(live.record, ask)) {
444
+ // A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
445
+ void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
446
+ }
521
447
  else {
522
448
  super.heardAsk(live, ask);
523
449
  }
@@ -534,9 +460,15 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
534
460
  }
535
461
  /** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
536
462
  after(event) {
463
+ var _a;
537
464
  if (!this.options.guest && (event.type === 'rooms' || (event.type === 'state' && event.key.startsWith('back:')))) {
538
465
  this.book.changed();
539
466
  }
467
+ // A friend invited the user into a group chat, or took it back: the pages read the rooms again.
468
+ const record = (_a = this.live.get(event.type === 'state' ? event.room : '')) === null || _a === void 0 ? void 0 : _a.record;
469
+ if (!this.options.guest && event.type === 'state' && (relay_group_invites_1.RelayGroupInvites.heard(record, event.key) || relay_friend_sends_1.RelayFriendSends.heard(record, event.key))) {
470
+ this.emit({ type: 'rooms' });
471
+ }
540
472
  }
541
473
  owned(roomId) {
542
474
  const live = this.member(roomId);
@@ -554,7 +486,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
554
486
  }
555
487
  info(live) {
556
488
  var _a, _b;
557
- var _c;
489
+ var _c, _d;
558
490
  const { record } = live;
559
491
  const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
560
492
  member: member.member,
@@ -574,6 +506,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
574
506
  ...(record.unlisted ? { unlisted: true } : {}),
575
507
  relay: record.relay,
576
508
  owner: record.own,
509
+ ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS } : {}),
577
510
  ownerName: record.owner.name,
578
511
  status: record.status,
579
512
  ...(record.reason ? { reason: record.reason } : {}),
@@ -585,77 +518,4 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
585
518
  }
586
519
  }
587
520
  exports.RelayHubs = RelayHubs;
588
- /** How far a membership has come: one computer heard it end, it ended for both. */
589
- const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
590
- var RelayHubsUtil;
591
- (function (RelayHubsUtil) {
592
- function isPrivate(identity) {
593
- const value = identity;
594
- return (!!value &&
595
- typeof value.id === 'string' &&
596
- typeof value.name === 'string' &&
597
- typeof value.sign === 'string' &&
598
- typeof value.box === 'string' &&
599
- !!value.signKey &&
600
- typeof value.signKey === 'object' &&
601
- !!value.boxKey &&
602
- typeof value.boxKey === 'object');
603
- }
604
- RelayHubsUtil.isPrivate = isPrivate;
605
- function isRecord(record) {
606
- const value = record;
607
- return (!!value &&
608
- typeof value.id === 'string' &&
609
- typeof value.relay === 'string' &&
610
- typeof value.member === 'string' &&
611
- typeof value.token === 'string' &&
612
- typeof value.status === 'string' &&
613
- value.status in STATUS_RANK &&
614
- !!value.owner &&
615
- typeof value.owner.id === 'string' &&
616
- (value.identity === undefined || isPrivate(value.identity)));
617
- }
618
- RelayHubsUtil.isRecord = isRecord;
619
- /**
620
- * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
621
- * key and the owner's keys once either has them, the invitations of both, made once either made it.
622
- */
623
- function merge(local, other) {
624
- var _a, _b, _c, _d, _e, _f;
625
- const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
626
- const behind = ahead === local ? other : local;
627
- const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
628
- .filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
629
- .sort((a, b) => a.expiresAt - b.expiresAt)
630
- .slice(-3);
631
- const merged = {
632
- ...behind,
633
- ...ahead,
634
- kind: ahead.kind || behind.kind,
635
- owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
636
- createdAt: Math.min(local.createdAt, other.createdAt),
637
- };
638
- const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
639
- const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
640
- const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
641
- delete merged.key;
642
- delete merged.invites;
643
- delete merged.joinedAt;
644
- delete merged.identity;
645
- delete merged.made;
646
- if (merged.status !== 'waiting') {
647
- delete merged.invite;
648
- delete merged.asked;
649
- }
650
- return {
651
- ...merged,
652
- ...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
653
- ...(invites.length ? { invites } : {}),
654
- ...(local.made || other.made ? { made: true } : {}),
655
- ...(joinedAt ? { joinedAt } : {}),
656
- ...(identity ? { identity } : {}),
657
- };
658
- }
659
- RelayHubsUtil.merge = merge;
660
- })(RelayHubsUtil || (exports.RelayHubsUtil = RelayHubsUtil = {}));
661
521
  //# sourceMappingURL=relay-hubs.js.map
@@ -0,0 +1,12 @@
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
+ }
12
+ //# sourceMappingURL=relay-open-in-ta.d.ts.map
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayOpenInTa = void 0;
4
+ /**
5
+ * 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
6
+ * 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
7
+ * phone). The link inside opens as any link of Ta, asking first wherever it would do something. Pure, for the tests.
8
+ */
9
+ var RelayOpenInTa;
10
+ (function (RelayOpenInTa) {
11
+ RelayOpenInTa.SCHEME = 'ta:';
12
+ const MAX = 4096;
13
+ function link(href) {
14
+ return `ta://open?link=${encodeURIComponent(href)}`;
15
+ }
16
+ RelayOpenInTa.link = link;
17
+ /** The link of Ta inside, or undefined for what is not one (another `ta://`, a page of the web, one too long). */
18
+ function inner(text) {
19
+ const trimmed = text.trim();
20
+ if (trimmed.length > MAX * 3 || !/^ta:\/\/open\?/i.test(trimmed)) {
21
+ return undefined;
22
+ }
23
+ const found = /[?&]link=([^&#]+)/.exec(trimmed);
24
+ if (!found) {
25
+ return undefined;
26
+ }
27
+ try {
28
+ const link = decodeURIComponent(found[1]).trim();
29
+ return /^https?:\/\/\S+$/i.test(link) && link.length <= MAX ? link : undefined;
30
+ }
31
+ catch (_a) {
32
+ return undefined;
33
+ }
34
+ }
35
+ RelayOpenInTa.inner = inner;
36
+ })(RelayOpenInTa || (exports.RelayOpenInTa = RelayOpenInTa = {}));
37
+ //# 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,8 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RELAY_HUB_RATE = exports.RELAY_HUB_IDLE_DAYS = exports.RELAY_HUB_ASK_DAYS = exports.RELAY_HUB_MAX_ASKS = exports.RELAY_HUB_MAX_STATE = exports.RELAY_HUB_MAX_KEYS = exports.RELAY_HUB_MAX_VALUE = exports.RELAY_FRIENDS_MAX = exports.RELAY_ROOM_MAX_MEMBERS = exports.RELAY_HUB_MAX_MEMBERS = exports.RELAY_ROOM_PATH = exports.RELAY_HUB_PATH = exports.RELAY_PROOF = exports.RELAY_STALE_MS = exports.RELAY_PING_MS = exports.RELAY_PONG = exports.RELAY_PING = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_BLOB_MAX_DAYS = exports.RELAY_CONTENT_MAX_BYTES = exports.RELAY_SHARE_DAYS = exports.RELAY_SHARE_CHUNK_BYTES = exports.RELAY_SHARE_MAX_BYTES = exports.RELAY_SHARE_PAGE = exports.RELAY_PAIR_PAGE = exports.RELAY_BLOB_PATH = exports.RELAY_VIEWER_HEADERS = exports.RELAY_FRAME_HEADER_CSP = exports.RELAY_FRAME_CSP = exports.RELAY_FRAME_PATH = exports.RELAY_CONTENT_PATH = exports.RELAY_SHARE_PATH = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
3
+ exports.RELAY_HUB_RATE = exports.RELAY_HUB_IDLE_DAYS = exports.RELAY_HUB_ASK_DAYS = exports.RELAY_HUB_MAX_ASKS = exports.RELAY_HUB_MAX_STATE = exports.RELAY_HUB_MAX_KEYS = exports.RELAY_HUB_MAX_VALUE = exports.RELAY_FRIENDS_MAX = exports.RELAY_INVITE_MAX_DAYS = exports.RELAY_ROOM_CAPACITY_MAX = exports.RELAY_ROOM_MAX_MEMBERS = exports.RELAY_HUB_MAX_MEMBERS = exports.RELAY_ROOM_PATH = exports.RELAY_HUB_PATH = exports.RELAY_PROOF = exports.RELAY_STALE_MS = exports.RELAY_PING_MS = exports.RELAY_PONG = exports.RELAY_PING = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_BLOB_MAX_DAYS = exports.RELAY_CONTENT_MAX_BYTES = exports.RELAY_SHARE_DAYS = exports.RELAY_SHARE_CHUNK_BYTES = exports.RELAY_SHARE_MAX_BYTES = exports.RELAY_SHARE_PAGE = exports.RELAY_PAIR_PAGE = exports.RELAY_BLOB_PATH = exports.RELAY_VIEWER_HEADERS = exports.RELAY_FRAME_HEADER_CSP = exports.RELAY_FRAME_CSP = exports.RELAY_FRAME_PATH = exports.RELAY_CONTENT_PATH = exports.RELAY_SHARE_PATH = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
4
+ exports.relayDefaultUrl = relayDefaultUrl;
4
5
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
5
6
  exports.RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
7
+ /**
8
+ * The relay of this deployment (docs/design/ta-deploy.md 3): TA_RELAY_URL, which a distribution of Ta sets for its own
9
+ * people, else the official one. New pairings, rooms and links go there unless relays.json says otherwise.
10
+ */
11
+ function relayDefaultUrl(env) {
12
+ var _a, _b;
13
+ if (env === void 0) { env = (_a = globalThis.process) === null || _a === void 0 ? void 0 : _a.env; }
14
+ return ((_b = env === null || env === void 0 ? void 0 : env.TA_RELAY_URL) === null || _b === void 0 ? void 0 : _b.trim().replace(/\/+$/, '')) || exports.RELAY_DEFAULT_URL;
15
+ }
6
16
  /** What the relay serves: hosts connect to the first, devices to the second. */
7
17
  exports.RELAY_HOST_PATH = '/v1/host/';
8
18
  exports.RELAY_CONNECT_PATH = '/v1/connect/';
@@ -38,16 +48,19 @@ exports.RELAY_VIEWER_HEADERS = {
38
48
  };
39
49
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
40
50
  exports.RELAY_BLOB_PATH = '/v1/blob/';
51
+ /** Open in Ta on the plain pages of the relay: the link of the page, its fragment too, to the Ta app (RelayOpenInTa). */
52
+ 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>
53
+ <script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
41
54
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
42
55
  exports.RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
43
56
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
44
57
  <h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
45
- <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p></body>`;
58
+ <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p>${OPEN_IN_TA}</body>`;
46
59
  /** The page a link of an extension opens in a browser. */
47
60
  exports.RELAY_SHARE_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
48
61
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
49
62
  <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>
50
- <p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p></body>`;
63
+ <p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p>${OPEN_IN_TA}</body>`;
51
64
  /** The largest package that is handed on: a gzipped tar of a package of the user, its code and a few assets. */
52
65
  exports.RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
53
66
  /** The bytes of a chunk, base64 in a sealed message: well under the 1 MiB of a message of Cloudflare. */
@@ -99,6 +112,10 @@ exports.RELAY_ROOM_PATH = '/r';
99
112
  exports.RELAY_HUB_MAX_MEMBERS = 101;
100
113
  /** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
101
114
  exports.RELAY_ROOM_MAX_MEMBERS = 16;
115
+ /** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
116
+ exports.RELAY_ROOM_CAPACITY_MAX = 100;
117
+ /** How long a link that invites works at most, in days. */
118
+ exports.RELAY_INVITE_MAX_DAYS = 365;
102
119
  /** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
103
120
  exports.RELAY_FRIENDS_MAX = 100;
104
121
  /** 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: {