@celljs/ta-relay 4.15.2 → 4.17.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 (58) hide show
  1. package/esm/common/index.d.ts +1 -0
  2. package/esm/common/index.js +1 -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-hub.js +2 -2
  7. package/esm/common/relay-hubs-identity.d.ts +36 -0
  8. package/esm/common/relay-hubs-identity.js +156 -0
  9. package/esm/common/relay-hubs-protocol.d.ts +4 -0
  10. package/esm/common/relay-hubs-util.d.ts +11 -0
  11. package/esm/common/relay-hubs-util.js +74 -0
  12. package/esm/common/relay-hubs.d.ts +17 -44
  13. package/esm/common/relay-hubs.js +37 -235
  14. package/esm/common/relay-protocol.d.ts +9 -0
  15. package/esm/common/relay-protocol.js +13 -0
  16. package/esm/common/relay-spot.d.ts +15 -3
  17. package/esm/common/relay-spot.js +17 -5
  18. package/esm/node/index.d.ts +1 -0
  19. package/esm/node/index.js +1 -0
  20. package/esm/node/relay-memory-storage.d.ts +26 -0
  21. package/esm/node/relay-memory-storage.js +60 -0
  22. package/esm/node/relay-server.d.ts +14 -26
  23. package/esm/node/relay-server.js +33 -60
  24. package/esm/node/relay-store.js +4 -4
  25. package/esm/node/relays-file.d.ts +27 -0
  26. package/esm/node/relays-file.js +59 -4
  27. package/esm/viewer/viewer.js +33 -1
  28. package/lib/common/index.d.ts +1 -0
  29. package/lib/common/index.js +1 -0
  30. package/lib/common/relay-content.d.ts +13 -0
  31. package/lib/common/relay-content.js +43 -1
  32. package/lib/common/relay-conversation.d.ts +14 -0
  33. package/lib/common/relay-hub.js +1 -1
  34. package/lib/common/relay-hubs-identity.d.ts +37 -0
  35. package/lib/common/relay-hubs-identity.js +160 -0
  36. package/lib/common/relay-hubs-protocol.d.ts +4 -0
  37. package/lib/common/relay-hubs-util.d.ts +12 -0
  38. package/lib/common/relay-hubs-util.js +77 -0
  39. package/lib/common/relay-hubs.d.ts +17 -44
  40. package/lib/common/relay-hubs.js +36 -234
  41. package/lib/common/relay-protocol.d.ts +9 -0
  42. package/lib/common/relay-protocol.js +15 -1
  43. package/lib/common/relay-spot.d.ts +15 -3
  44. package/lib/common/relay-spot.js +17 -5
  45. package/lib/node/index.d.ts +1 -0
  46. package/lib/node/index.js +1 -0
  47. package/lib/node/relay-memory-storage.d.ts +27 -0
  48. package/lib/node/relay-memory-storage.js +65 -0
  49. package/lib/node/relay-server.d.ts +14 -26
  50. package/lib/node/relay-server.js +34 -61
  51. package/lib/node/relay-store.js +3 -3
  52. package/lib/node/relays-file.d.ts +27 -0
  53. package/lib/node/relays-file.js +58 -3
  54. package/lib/viewer/viewer.js +32 -0
  55. package/package.json +3 -2
  56. package/viewer/join.html +3 -3
  57. package/viewer/page.html +9 -9
  58. package/viewer/room.html +10 -10
@@ -1,9 +1,10 @@
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
4
  const relay_friends_1 = require("./relay-friends");
5
5
  const relay_hub_util_1 = require("./relay-hub-util");
6
6
  const relay_hubs_connection_1 = require("./relay-hubs-connection");
7
+ const relay_hubs_identity_1 = require("./relay-hubs-identity");
7
8
  const relay_identity_1 = require("./relay-identity");
8
9
  const relay_protocol_1 = require("./relay-protocol");
9
10
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -13,7 +14,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
13
14
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
14
15
  * their own (RelayFriends).
15
16
  */
16
- class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
17
+ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
17
18
  constructor() {
18
19
  super(...arguments);
19
20
  this.book = new relay_friends_1.RelayFriends({
@@ -68,48 +69,6 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
68
69
  this.listeners.add(listener);
69
70
  return () => this.listeners.delete(listener);
70
71
  }
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
72
  list() {
114
73
  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
74
  }
@@ -158,7 +117,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
158
117
  }
159
118
  /**
160
119
  * 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.
120
+ * one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
162
121
  */
163
122
  async invite(roomId, options = {}) {
164
123
  var _a;
@@ -181,7 +140,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
181
140
  return linkOf(latest);
182
141
  }
183
142
  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;
143
+ 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
144
  await this.ready(live);
186
145
  this.request(live, { t: 'invite', hash: await relay_identity_1.RelayKeys.hash(await relay_hub_util_1.RelayHubUtil.inviteProof(secret)), expiresAt });
187
146
  // Asks sealed with an invitation of before are still read, while they wait.
@@ -189,15 +148,17 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
189
148
  this.save();
190
149
  return linkOf({ secret, expiresAt });
191
150
  }
192
- /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
151
+ /**
152
+ * Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
153
+ * A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
154
+ */
193
155
  async join(text) {
194
156
  const link = relay_hub_util_1.RelayHubUtil.parse(text);
195
- const identity = await this.identity();
196
157
  const known = this.live.get(link.room);
197
158
  if (known && (known.record.status === 'member' || known.record.status === 'waiting')) {
198
159
  return this.info(known);
199
160
  }
200
- if (link.owner === identity.id) {
161
+ if (known === null || known === void 0 ? void 0 : known.record.own) {
201
162
  throw new Error('This is a room of your own.');
202
163
  }
203
164
  const friends = relay_friends_1.RelayFriendsUtil.isKind(link.kind);
@@ -243,7 +204,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
243
204
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
244
205
  async answer(roomId, askId, accept) {
245
206
  var _a;
246
- var _b;
207
+ var _b, _c;
247
208
  const live = this.owned(roomId);
248
209
  const asked = live.asks.get(askId);
249
210
  if (!asked) {
@@ -263,8 +224,8 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
263
224
  ...(live.record.unlisted ? { unlisted: true } : {}),
264
225
  };
265
226
  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) {
227
+ 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);
228
+ if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
268
229
  throw new Error('The room is full.');
269
230
  }
270
231
  const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
@@ -275,6 +236,27 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
275
236
  live.asks.delete(askId);
276
237
  this.emit({ type: 'rooms' });
277
238
  }
239
+ /**
240
+ * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
241
+ * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
242
+ */
243
+ configure(roomId, options) {
244
+ const live = this.owned(roomId);
245
+ if (options.members !== undefined) {
246
+ const members = Math.round(Number(options.members));
247
+ if (!(members >= 2 && members <= relay_protocol_1.RELAY_ROOM_CAPACITY_MAX)) {
248
+ throw new Error(`A room takes 2 to ${relay_protocol_1.RELAY_ROOM_CAPACITY_MAX} members.`);
249
+ }
250
+ if (members === relay_protocol_1.RELAY_ROOM_MAX_MEMBERS) {
251
+ delete live.record.capacity;
252
+ }
253
+ else {
254
+ live.record.capacity = members;
255
+ }
256
+ this.save();
257
+ }
258
+ return this.info(live);
259
+ }
278
260
  /** The owner takes a member out: the relay closes it at once. */
279
261
  async remove(roomId, member) {
280
262
  const live = this.owned(roomId);
@@ -361,114 +343,6 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
361
343
  }
362
344
  return { ok: result.ok, version: result.v };
363
345
  }
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
346
  /** Friends of the user's other computer, with those of this Ta. */
473
347
  mergeFriends(friends) {
474
348
  return this.book.merge(friends);
@@ -554,7 +428,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
554
428
  }
555
429
  info(live) {
556
430
  var _a, _b;
557
- var _c;
431
+ var _c, _d;
558
432
  const { record } = live;
559
433
  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
434
  member: member.member,
@@ -574,6 +448,7 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
574
448
  ...(record.unlisted ? { unlisted: true } : {}),
575
449
  relay: record.relay,
576
450
  owner: record.own,
451
+ ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS } : {}),
577
452
  ownerName: record.owner.name,
578
453
  status: record.status,
579
454
  ...(record.reason ? { reason: record.reason } : {}),
@@ -585,77 +460,4 @@ class RelayHubs extends relay_hubs_connection_1.RelayHubsConnection {
585
460
  }
586
461
  }
587
462
  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
463
  //# sourceMappingURL=relay-hubs.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/";
@@ -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/';
@@ -99,6 +109,10 @@ exports.RELAY_ROOM_PATH = '/r';
99
109
  exports.RELAY_HUB_MAX_MEMBERS = 101;
100
110
  /** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
101
111
  exports.RELAY_ROOM_MAX_MEMBERS = 16;
112
+ /** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
113
+ exports.RELAY_ROOM_CAPACITY_MAX = 100;
114
+ /** How long a link that invites works at most, in days. */
115
+ exports.RELAY_INVITE_MAX_DAYS = 365;
102
116
  /** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
103
117
  exports.RELAY_FRIENDS_MAX = 100;
104
118
  /** 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: {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelaySpot = exports.RelaySpotTries = exports.RelaySpotCloseCode = exports.RELAY_SPOT_TRIES_MS = exports.RELAY_SPOT_TRIES = exports.RELAY_SPOT_MESSAGE = exports.RELAY_SPOT_MAX = exports.RELAY_SPOT_JOIN_MS = exports.RELAY_SPOT_PATH = void 0;
4
+ const relay_bytes_1 = require("./relay-bytes");
4
5
  const relay_protocol_1 = require("./relay-protocol");
5
6
  /**
6
7
  * A spot (docs/design/ta-nearby.md 2.2): the Ta that typed the same code of six digits, on networks that keep them
@@ -28,6 +29,8 @@ var RelaySpotCloseCode;
28
29
  RelaySpotCloseCode[RelaySpotCloseCode["FULL"] = 4429] = "FULL";
29
30
  /** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
30
31
  RelaySpotCloseCode[RelaySpotCloseCode["TRIES"] = 4428] = "TRIES";
32
+ /** Opened already, by one whose key this one does not hold: a new code is needed. Counted as a failure. */
33
+ RelaySpotCloseCode[RelaySpotCloseCode["TAKEN"] = 4409] = "TAKEN";
31
34
  })(RelaySpotCloseCode || (exports.RelaySpotCloseCode = RelaySpotCloseCode = {}));
32
35
  /**
33
36
  * The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
@@ -126,7 +129,7 @@ class RelaySpot {
126
129
  }
127
130
  }
128
131
  enter(socket, message) {
129
- var _a;
132
+ var _a, _b, _c, _d;
130
133
  if (message.t !== 'open' && message.t !== 'join') {
131
134
  socket.close(RelaySpotCloseCode.BAD, 'Open or join first.');
132
135
  return;
@@ -134,19 +137,27 @@ class RelaySpot {
134
137
  const others = this.others(socket);
135
138
  const now = this.now();
136
139
  const opened = others.length ? Math.min(...others.map(other => { var _a; var _b; return (_b = (_a = other.who) === null || _a === void 0 ? void 0 : _a.opened) !== null && _b !== void 0 ? _b : now; })) : now;
140
+ const key = (_b = (_a = others.find(other => { var _a; return (_a = other.who) === null || _a === void 0 ? void 0 : _a.k; })) === null || _a === void 0 ? void 0 : _a.who) === null || _b === void 0 ? void 0 : _b.k;
141
+ const back = message.t === 'open' && !!key && !!message.k && message.k === key;
137
142
  if (!others.length && message.t === 'join') {
138
- (_a = this.missed) === null || _a === void 0 ? void 0 : _a.call(this, socket);
143
+ (_c = this.missed) === null || _c === void 0 ? void 0 : _c.call(this, socket);
139
144
  socket.close(RelaySpotCloseCode.NONE, 'No such spot.');
140
145
  }
141
- else if (now - opened > exports.RELAY_SPOT_JOIN_MS) {
146
+ else if (others.length && message.t === 'open' && !back) {
147
+ (_d = this.missed) === null || _d === void 0 ? void 0 : _d.call(this, socket);
148
+ socket.close(RelaySpotCloseCode.TAKEN, 'Opened already.');
149
+ }
150
+ else if (!back && now - opened > exports.RELAY_SPOT_JOIN_MS) {
142
151
  socket.close(RelaySpotCloseCode.LATE, 'Too late to come in.');
143
152
  }
144
153
  else if (others.length >= exports.RELAY_SPOT_MAX) {
145
154
  socket.close(RelaySpotCloseCode.FULL, 'Full.');
146
155
  }
147
156
  else {
148
- socket.who = { opened };
149
- socket.send(JSON.stringify({ t: 'ready', opened }));
157
+ const k = key !== null && key !== void 0 ? key : relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.random(18));
158
+ socket.who = { opened, k };
159
+ const mine = message.t === 'open';
160
+ socket.send(JSON.stringify({ t: 'ready', opened, ...(mine ? { k } : {}) }));
150
161
  }
151
162
  }
152
163
  /** The others in the spot. */
@@ -159,6 +170,7 @@ class RelaySpot {
159
170
  const id = (value) => typeof value === 'string' && value.length > 0 && value.length <= 64;
160
171
  switch (data.t) {
161
172
  case 'open':
173
+ return id(data.k) ? { t: 'open', k: data.k } : { t: 'open' };
162
174
  case 'join':
163
175
  case 'bye':
164
176
  return { t: data.t };
@@ -3,6 +3,7 @@ export * from './relay-file-storage';
3
3
  export * from './relay-files-host';
4
4
  export * from './relay-host';
5
5
  export * from './relay-hub-files';
6
+ export * from './relay-memory-storage';
6
7
  export * from './relay-server';
7
8
  export * from './relay-store';
8
9
  export * from './relay-viewer';
package/lib/node/index.js CHANGED
@@ -19,6 +19,7 @@ __exportStar(require("./relay-file-storage"), exports);
19
19
  __exportStar(require("./relay-files-host"), exports);
20
20
  __exportStar(require("./relay-host"), exports);
21
21
  __exportStar(require("./relay-hub-files"), exports);
22
+ __exportStar(require("./relay-memory-storage"), exports);
22
23
  __exportStar(require("./relay-server"), exports);
23
24
  __exportStar(require("./relay-store"), exports);
24
25
  __exportStar(require("./relay-viewer"), exports);
@@ -0,0 +1,27 @@
1
+ import type { RelayBlobStorage } from '../common/relay-blobs';
2
+ import type { RelayHubStorage } from '../common/relay-hub';
3
+ /** A copy of content in memory, deleted when it runs out. */
4
+ export declare class MemoryBlobStorage implements RelayBlobStorage {
5
+ protected readonly values: Map<string, unknown>;
6
+ protected timer?: ReturnType<typeof setTimeout>;
7
+ get<T>(key: string): Promise<T | undefined>;
8
+ put(key: string, value: unknown): Promise<void>;
9
+ deleteAll(): Promise<void>;
10
+ /** No copy is kept: nothing of it need stay in the relay's memory. */
11
+ get empty(): boolean;
12
+ setAlarm(at: number): Promise<void>;
13
+ }
14
+ /** A room in memory, as a Durable Object keeps it. */
15
+ export declare class MemoryHubStorage implements RelayHubStorage {
16
+ protected readonly values: Map<string, unknown>;
17
+ get<T>(key: string): Promise<T | undefined>;
18
+ put(key: string, value: unknown): Promise<void>;
19
+ delete(key: string): Promise<boolean>;
20
+ list<T>(options: {
21
+ prefix: string;
22
+ }): Promise<Map<string, T>>;
23
+ deleteAll(): Promise<void>;
24
+ /** No room was made, or it was closed. */
25
+ get empty(): boolean;
26
+ }
27
+ //# sourceMappingURL=relay-memory-storage.d.ts.map
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryHubStorage = exports.MemoryBlobStorage = void 0;
4
+ /** A copy of content in memory, deleted when it runs out. */
5
+ class MemoryBlobStorage {
6
+ constructor() {
7
+ this.values = new Map();
8
+ }
9
+ async get(key) {
10
+ return this.values.get(key);
11
+ }
12
+ async put(key, value) {
13
+ this.values.set(key, value);
14
+ }
15
+ async deleteAll() {
16
+ this.values.clear();
17
+ clearTimeout(this.timer);
18
+ }
19
+ /** No copy is kept: nothing of it need stay in the relay's memory. */
20
+ get empty() {
21
+ return this.values.size === 0;
22
+ }
23
+ async setAlarm(at) {
24
+ var _a, _b;
25
+ clearTimeout(this.timer);
26
+ // Past the 24 days of a timer, it is found run out on the next read.
27
+ this.timer = setTimeout(() => this.values.clear(), Math.min(Math.max(at - Date.now(), 0), 2 ** 31 - 1));
28
+ (_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
29
+ }
30
+ }
31
+ exports.MemoryBlobStorage = MemoryBlobStorage;
32
+ /** A room in memory, as a Durable Object keeps it. */
33
+ class MemoryHubStorage {
34
+ constructor() {
35
+ this.values = new Map();
36
+ }
37
+ async get(key) {
38
+ const value = this.values.get(key);
39
+ return value === undefined ? undefined : structuredClone(value);
40
+ }
41
+ async put(key, value) {
42
+ this.values.set(key, structuredClone(value));
43
+ }
44
+ async delete(key) {
45
+ return this.values.delete(key);
46
+ }
47
+ async list(options) {
48
+ const found = new Map();
49
+ for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
50
+ if (key.startsWith(options.prefix)) {
51
+ found.set(key, structuredClone(value));
52
+ }
53
+ }
54
+ return found;
55
+ }
56
+ async deleteAll() {
57
+ this.values.clear();
58
+ }
59
+ /** No room was made, or it was closed. */
60
+ get empty() {
61
+ return this.values.size === 0;
62
+ }
63
+ }
64
+ exports.MemoryHubStorage = MemoryHubStorage;
65
+ //# sourceMappingURL=relay-memory-storage.js.map