@celljs/ta-relay 4.12.0 → 4.13.1

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 (74) hide show
  1. package/README.en.md +26 -1
  2. package/README.md +26 -1
  3. package/esm/common/index.d.ts +3 -0
  4. package/esm/common/index.js +3 -0
  5. package/esm/common/relay-friends.d.ts +154 -0
  6. package/esm/common/relay-friends.js +482 -0
  7. package/esm/common/relay-hub-util.d.ts +9 -0
  8. package/esm/common/relay-hub-util.js +19 -2
  9. package/esm/common/relay-hubs-connection.d.ts +17 -5
  10. package/esm/common/relay-hubs-connection.js +40 -37
  11. package/esm/common/relay-hubs-protocol.d.ts +51 -7
  12. package/esm/common/relay-hubs.d.ts +67 -5
  13. package/esm/common/relay-hubs.js +305 -16
  14. package/esm/common/relay-identity.d.ts +2 -0
  15. package/esm/common/relay-info.d.ts +44 -0
  16. package/esm/common/relay-info.js +94 -0
  17. package/esm/common/relay-protocol.d.ts +7 -3
  18. package/esm/common/relay-protocol.js +7 -3
  19. package/esm/common/relay-room-chat.d.ts +6 -1
  20. package/esm/common/relay-room-chat.js +1 -0
  21. package/esm/common/relay-spot.d.ts +122 -0
  22. package/esm/common/relay-spot.js +179 -0
  23. package/esm/guest/guest-i18n.d.ts +2 -0
  24. package/esm/guest/guest-i18n.js +4 -0
  25. package/esm/guest/guest.js +9 -1
  26. package/esm/node/index.d.ts +2 -0
  27. package/esm/node/index.js +2 -0
  28. package/esm/node/relay-file-storage.d.ts +32 -0
  29. package/esm/node/relay-file-storage.js +101 -0
  30. package/esm/node/relay-host.d.ts +63 -28
  31. package/esm/node/relay-host.js +145 -52
  32. package/esm/node/relay-server.d.ts +37 -3
  33. package/esm/node/relay-server.js +193 -37
  34. package/esm/node/relay-store.d.ts +34 -6
  35. package/esm/node/relay-store.js +54 -22
  36. package/esm/node/relays-file.d.ts +139 -0
  37. package/esm/node/relays-file.js +373 -0
  38. package/lib/common/index.d.ts +3 -0
  39. package/lib/common/index.js +3 -0
  40. package/lib/common/relay-friends.d.ts +155 -0
  41. package/lib/common/relay-friends.js +486 -0
  42. package/lib/common/relay-hub-util.d.ts +9 -0
  43. package/lib/common/relay-hub-util.js +19 -2
  44. package/lib/common/relay-hubs-connection.d.ts +17 -5
  45. package/lib/common/relay-hubs-connection.js +40 -37
  46. package/lib/common/relay-hubs-protocol.d.ts +51 -7
  47. package/lib/common/relay-hubs.d.ts +67 -5
  48. package/lib/common/relay-hubs.js +306 -17
  49. package/lib/common/relay-identity.d.ts +2 -0
  50. package/lib/common/relay-info.d.ts +45 -0
  51. package/lib/common/relay-info.js +97 -0
  52. package/lib/common/relay-protocol.d.ts +7 -3
  53. package/lib/common/relay-protocol.js +8 -4
  54. package/lib/common/relay-room-chat.d.ts +6 -1
  55. package/lib/common/relay-room-chat.js +1 -0
  56. package/lib/common/relay-spot.d.ts +123 -0
  57. package/lib/common/relay-spot.js +183 -0
  58. package/lib/guest/guest-i18n.d.ts +2 -0
  59. package/lib/guest/guest-i18n.js +4 -0
  60. package/lib/guest/guest.js +9 -1
  61. package/lib/node/index.d.ts +2 -0
  62. package/lib/node/index.js +2 -0
  63. package/lib/node/relay-file-storage.d.ts +33 -0
  64. package/lib/node/relay-file-storage.js +105 -0
  65. package/lib/node/relay-host.d.ts +63 -28
  66. package/lib/node/relay-host.js +146 -53
  67. package/lib/node/relay-server.d.ts +37 -3
  68. package/lib/node/relay-server.js +193 -37
  69. package/lib/node/relay-store.d.ts +34 -6
  70. package/lib/node/relay-store.js +54 -22
  71. package/lib/node/relays-file.d.ts +140 -0
  72. package/lib/node/relays-file.js +378 -0
  73. package/package.json +2 -2
  74. package/viewer/room.html +10 -10
@@ -1,3 +1,4 @@
1
+ import { RelayFriendsUtil } from './relay-friends.js';
1
2
  import { RelayHubCloseCode } from './relay-hub.js';
2
3
  import { RelayHubUtil } from './relay-hub-util.js';
3
4
  import { RelayIdentityUtil } from './relay-identity.js';
@@ -6,7 +7,6 @@ import { RELAY_HUB_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, } from './relay-
6
7
  import { RELAY_CHAT_KEY } from './relay-room-chat.js';
7
8
  const MAX_RECONNECT_MS = 30000;
8
9
  const SKEW_MS = 5 * 60 * 1000;
9
- const MAX_FRIEND_ROOMS = 8;
10
10
  /** How long a call waits for the room to be connected, or for the relay to answer. */
11
11
  export const READY_MS = 15000;
12
12
  export const OPEN = 1;
@@ -32,7 +32,7 @@ export class Live {
32
32
  /**
33
33
  * The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
34
34
  * connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
35
- * signatures of the roster; what is kept of the rooms and the friends.
35
+ * signatures of the roster; what is kept of the rooms.
36
36
  */
37
37
  export class RelayHubsConnection {
38
38
  constructor(options) {
@@ -60,6 +60,11 @@ export class RelayHubsConnection {
60
60
  this.own = made;
61
61
  return made;
62
62
  }
63
+ /** Who this Ta is in a room: the identity it joined with when it took on another since (RelayRoomRecord.identity). */
64
+ async identityOf(live) {
65
+ var _a;
66
+ return (_a = live.record.identity) !== null && _a !== void 0 ? _a : this.identity();
67
+ }
63
68
  requestOf(live, ask, friends) {
64
69
  return {
65
70
  room: live.record.id,
@@ -95,7 +100,7 @@ export class RelayHubsConnection {
95
100
  }
96
101
  async sendBody(live, body, to) {
97
102
  await this.ready(live);
98
- const identity = await this.identity();
103
+ const identity = await this.identityOf(live);
99
104
  const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
100
105
  this.request(live, { t: 'send', d, ...(to ? { to } : {}) });
101
106
  }
@@ -103,7 +108,7 @@ export class RelayHubsConnection {
103
108
  async nextRoster(live, change) {
104
109
  var _a, _b;
105
110
  var _c, _d;
106
- const identity = await this.identity();
111
+ const identity = await this.identityOf(live);
107
112
  const body = { v: ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.v) !== null && _c !== void 0 ? _c : 0) + 1, members: change((_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : []) };
108
113
  const d = await RelayHubUtil.seal(live.keys.seal, identity, live.record.member, body);
109
114
  live.roster = body;
@@ -168,7 +173,7 @@ export class RelayHubsConnection {
168
173
  ask: record.member,
169
174
  token: record.token,
170
175
  proof: await RelayHubUtil.inviteProof(record.invite),
171
- d: await RelayHubUtil.sealAsk(record.invite, await this.identity(), record.member, Date.now(), !!this.options.guest),
176
+ d: await RelayHubUtil.sealAsk(record.invite, await this.identityOf(live), record.member, Date.now(), !!this.options.guest),
172
177
  };
173
178
  }
174
179
  else {
@@ -334,7 +339,7 @@ export class RelayHubsConnection {
334
339
  async admitted(live, answer) {
335
340
  const { record } = live;
336
341
  const opened = record.invite
337
- ? await RelayHubUtil.openAnswer(await this.identity(), record.id, record.member, record.owner.id, answer, record.invite)
342
+ ? await RelayHubUtil.openAnswer(await this.identityOf(live), record.id, record.member, record.owner.id, answer, record.invite)
338
343
  : undefined;
339
344
  if (!opened) {
340
345
  // Not signed by the owner the link named: whoever answered, it is not that room.
@@ -344,9 +349,10 @@ export class RelayHubsConnection {
344
349
  live.record = {
345
350
  ...record,
346
351
  key: opened.body.key,
347
- kind: String(opened.body.kind),
352
+ kind: RelayFriendsUtil.kindOf(String(opened.body.kind)),
348
353
  title: String(opened.body.title).slice(0, 80),
349
354
  ...(opened.body.view ? { view: String(opened.body.view) } : {}),
355
+ ...(opened.body.unlisted === true ? { unlisted: true } : {}),
350
356
  owner: opened.owner,
351
357
  status: 'member',
352
358
  joinedAt: Date.now(),
@@ -366,13 +372,21 @@ export class RelayHubsConnection {
366
372
  live.asks.set(ask.member, { body: ask, invite: invite.secret });
367
373
  if (fresh || !this.told.has(ask.member)) {
368
374
  this.told.add(ask.member);
369
- this.emit({ type: 'request', request: this.requestOf(live, ask, this.friendIds()) });
370
- this.emit({ type: 'rooms' });
375
+ this.heardAsk(live, ask);
371
376
  }
372
377
  return;
373
378
  }
374
379
  }
375
380
  }
381
+ /** One asks to join a room of this Ta, heard the first time. */
382
+ heardAsk(live, ask) {
383
+ this.emit({ type: 'request', request: this.requestOf(live, ask, this.friendIds()) });
384
+ this.emit({ type: 'rooms' });
385
+ }
386
+ /** The members of a roster, read: those of them the user chose as friends are kept up to date. */
387
+ heardMembers(_members) { }
388
+ /** A member asks the user to be friends, by the link of its room of friends. */
389
+ offered(_live, _from, _link) { }
376
390
  async readRoster(live, sealed) {
377
391
  var _a;
378
392
  const owner = live.record.owner;
@@ -395,8 +409,8 @@ export class RelayHubsConnection {
395
409
  return;
396
410
  }
397
411
  live.roster = { v: signed.b.v, members };
398
- const own = (await this.identity()).id;
399
- this.meet(live.record.title, members.filter(member => member.id !== own && !member.guest));
412
+ const own = (await this.identityOf(live)).id;
413
+ this.heardMembers(members.filter(member => member.id !== own && !member.guest));
400
414
  if (!members.some(member => member.member === live.record.member) && !live.record.own && live.record.status === 'member') {
401
415
  this.setStatus(live, 'gone', 'removed');
402
416
  }
@@ -446,6 +460,12 @@ export class RelayHubsConnection {
446
460
  // A guest only chats, in the state: the messages of games are not its.
447
461
  return;
448
462
  }
463
+ if (typeof signed.b.friend === 'string') {
464
+ if (sender && !sender.guest) {
465
+ this.offered(live, sender, signed.b.friend);
466
+ }
467
+ return;
468
+ }
449
469
  if (typeof signed.b.name === 'string') {
450
470
  if (live.record.own && sender) {
451
471
  const name = signed.b.name.trim().slice(0, 40);
@@ -455,33 +475,9 @@ export class RelayHubsConnection {
455
475
  }
456
476
  this.emit({ type: 'message', room: live.record.id, from, name: (_b = sender === null || sender === void 0 ? void 0 : sender.name) !== null && _b !== void 0 ? _b : '', data: signed.b.d });
457
477
  }
458
- /** Those of a room are friends: kept with their keys and the rooms shared. */
459
- meet(title, members) {
460
- if (!members.length) {
461
- return;
462
- }
463
- const friends = this.readFriends();
464
- const now = Date.now();
465
- for (const member of members) {
466
- const known = friends.find(friend => friend.id === member.id);
467
- if (known) {
468
- known.name = member.name;
469
- known.seenAt = now;
470
- known.rooms = [...known.rooms.filter(room => room !== title), title].slice(-MAX_FRIEND_ROOMS);
471
- }
472
- else {
473
- friends.push({ ...RelayIdentityUtil.publicOf(member), rooms: [title], metAt: now, seenAt: now });
474
- }
475
- }
476
- this.options.store.write('friends', { friends });
477
- }
478
+ /** Ids of those the user chose as friends. */
478
479
  friendIds() {
479
- return new Set(this.readFriends().map(friend => friend.id));
480
- }
481
- readFriends() {
482
- var _a;
483
- const friends = (_a = this.options.store.read('friends')) === null || _a === void 0 ? void 0 : _a.friends;
484
- return Array.isArray(friends) ? friends : [];
480
+ return new Set();
485
481
  }
486
482
  readRooms() {
487
483
  var _a;
@@ -491,7 +487,14 @@ export class RelayHubsConnection {
491
487
  save() {
492
488
  this.options.store.write('rooms', { rooms: [...this.live.values()].map(live => live.record) });
493
489
  }
490
+ /** To the listeners, then what follows from it (RelayHubs: the friends settle). */
494
491
  emit(event) {
492
+ this.notify(event);
493
+ this.after(event);
494
+ }
495
+ after(_event) { }
496
+ /** To the listeners alone. */
497
+ notify(event) {
495
498
  for (const listener of this.listeners) {
496
499
  try {
497
500
  listener(event);
@@ -1,4 +1,4 @@
1
- import type { RelayPublicIdentity } from './relay-identity.js';
1
+ import type { RelayIdentity, RelayPublicIdentity } from './relay-identity.js';
2
2
  /** `member` in the room; `waiting` for the owner to answer; `refused` by the owner; `gone`: closed, removed, or the invitation no longer works. */
3
3
  export type RelayRoomStatus = 'member' | 'waiting' | 'refused' | 'gone';
4
4
  /** A room this Ta is in or asked to join, in `rooms.json` of the agent directory (docs/design/ta-link.md 3.5). */
@@ -8,6 +8,7 @@ export interface RelayRoomRecord {
8
8
  kind: string;
9
9
  title: string;
10
10
  view?: string;
11
+ unlisted?: true;
11
12
  /** This Ta's id in the room, and what the relay knows it by. */
12
13
  member: string;
13
14
  token: string;
@@ -32,6 +33,23 @@ export interface RelayRoomRecord {
32
33
  asked?: boolean;
33
34
  createdAt: number;
34
35
  joinedAt?: number;
36
+ /**
37
+ * The identity this Ta is in the room when it is not the one of identity.json: the one it joined with, kept when
38
+ * this Ta took on the identity of the user's other computer (RelayHubs.importIdentity).
39
+ */
40
+ identity?: RelayIdentity;
41
+ }
42
+ /** What makes a Ta who it is in rooms, private keys and all: for the user's own computers alone (a move, a shared identity). */
43
+ export interface RelayIdentityBundle {
44
+ identity: RelayIdentity;
45
+ rooms: RelayRoomRecord[];
46
+ friends: RelayFriend[];
47
+ }
48
+ /** What {@link RelayIdentityBundle} did here: whether this Ta took on another identity, the rooms and friends it got. */
49
+ export interface RelayIdentityImport {
50
+ switched: boolean;
51
+ rooms: number;
52
+ friends: number;
35
53
  }
36
54
  export interface RelayRoomMember {
37
55
  member: string;
@@ -49,6 +67,9 @@ export interface RelayRoomInfo {
49
67
  kind: string;
50
68
  title: string;
51
69
  view?: string;
70
+ unlisted?: true;
71
+ /** The relay it is on. */
72
+ relay: string;
52
73
  owner: boolean;
53
74
  ownerName: string;
54
75
  status: RelayRoomStatus;
@@ -73,13 +94,31 @@ export interface RelayRoomRequest {
73
94
  guest?: boolean;
74
95
  at: number;
75
96
  }
76
- /** One met in a room (docs/design/ta-link.md 3.1), in `friends.json`. */
77
- export interface RelayFriend extends RelayPublicIdentity {
78
- /** Titles of rooms shared, the latest last. */
79
- rooms: string[];
97
+ /**
98
+ * One the user chose as a friend (docs/design/ta-link.md 3.1), in `friends.json`: its keys once known (from its link
99
+ * only its id and name), when chosen and last heard of.
100
+ */
101
+ export interface RelayFriend extends Pick<RelayPublicIdentity, 'id' | 'name'>, Partial<Pick<RelayPublicIdentity, 'sign' | 'box'>> {
102
+ metAt: number;
103
+ seenAt: number;
104
+ }
105
+ /** A friend as the pages show it: `friend` once each is in the room of the other, `adding` till then. */
106
+ export interface RelayFriendInfo {
107
+ id: string;
108
+ name: string;
109
+ status: 'friend' | 'adding';
110
+ online: boolean;
80
111
  metAt: number;
81
112
  seenAt: number;
82
113
  }
114
+ /** One asking the user to be friends: by the link of the user's room of friends, or in a room the two share (its title). */
115
+ export interface RelayFriendRequest {
116
+ id: string;
117
+ name: string;
118
+ at: number;
119
+ via: 'link' | 'room';
120
+ room?: string;
121
+ }
83
122
  export interface RelayRoomValue {
84
123
  value: unknown;
85
124
  version: number;
@@ -91,6 +130,11 @@ export type RelayRoomEvent = {
91
130
  } | {
92
131
  type: 'request';
93
132
  request: RelayRoomRequest;
133
+ }
134
+ /** One asks the user to be friends. */
135
+ | {
136
+ type: 'friend';
137
+ request: RelayFriendRequest;
94
138
  } | {
95
139
  type: 'message';
96
140
  room: string;
@@ -122,8 +166,8 @@ export interface RelayHubsOptions {
122
166
  * others hear only what it says (`chat:` of the state), not its messages or the rest of the state.
123
167
  */
124
168
  guest?: boolean;
125
- /** The relay new rooms are made on. */
126
- relay(): string;
169
+ /** The relay new rooms are made on; `use` the feature a room is for (relays.json), `rooms` when none is said. */
170
+ relay(use?: string): string;
127
171
  /** First wait before connecting again; it doubles up to 30 seconds. */
128
172
  reconnectDelayMs?: number;
129
173
  pingMs?: number;
@@ -1,12 +1,16 @@
1
+ import { RelayFriends } from './relay-friends.js';
2
+ import { type RelayHubAskBody } from './relay-hub-util.js';
1
3
  import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
2
- import type { RelayFriend, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
3
- import { type RelayPublicIdentity } from './relay-identity.js';
4
+ import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayIdentityBundle, RelayIdentityImport, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
5
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
4
6
  /**
5
7
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
6
8
  * and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
7
- * the others; every message signed by its member and sealed with the key of the room.
9
+ * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
10
+ * their own (RelayFriends).
8
11
  */
9
12
  export declare class RelayHubs extends RelayHubsConnection {
13
+ protected readonly book: RelayFriends;
10
14
  /** Connects to the rooms of rooms.json, and keeps connected. */
11
15
  start(): void;
12
16
  stop(): void;
@@ -19,6 +23,8 @@ export declare class RelayHubs extends RelayHubsConnection {
19
23
  kind: string;
20
24
  title: string;
21
25
  view?: string;
26
+ unlisted?: boolean;
27
+ use?: string;
22
28
  }): Promise<RelayRoomInfo>;
23
29
  /**
24
30
  * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
@@ -53,9 +59,65 @@ export declare class RelayHubs extends RelayHubsConnection {
53
59
  ok: boolean;
54
60
  version: number;
55
61
  }>;
56
- friends(): RelayFriend[];
57
- removeFriend(id: string): boolean;
62
+ /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
63
+ exportIdentity(): Promise<RelayIdentityBundle>;
64
+ /**
65
+ * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
66
+ * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
67
+ * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
68
+ */
69
+ importIdentity(bundle: RelayIdentityBundle, options?: {
70
+ shared?: boolean;
71
+ }): Promise<RelayIdentityImport>;
72
+ /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
73
+ setShared(shared: boolean): Promise<void>;
74
+ /**
75
+ * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
76
+ * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
77
+ * as another member.
78
+ */
79
+ upsert(record: RelayRoomRecord): boolean;
80
+ /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
81
+ forget(roomId: string, member: string): boolean;
82
+ /** Friends of the user's other computer, with those of this Ta. */
83
+ mergeFriends(friends: RelayFriend[]): number;
84
+ /** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
85
+ friends(): (RelayFriend & RelayPublicIdentity)[];
86
+ /** The friends and those being added, for the pages. */
87
+ listFriends(): RelayFriendInfo[];
88
+ /** The room of friends of this Ta, made the first time: other features keep there what they give friends. */
89
+ friendRoom(): Promise<RelayRoomInfo>;
90
+ /** The link (and code) that asks to be the user's friend; with `renew`, a new one, the one before no longer working. */
91
+ friendLink(renew?: boolean): Promise<{
92
+ link: string;
93
+ expiresAt: number;
94
+ }>;
95
+ /** Asks the one of a link to be friends, on the user's word. */
96
+ addFriend(link: string): Promise<void>;
97
+ /** Asks a member of a room the user is in to be friends. */
98
+ offerFriend(roomId: string, member: string): Promise<void>;
99
+ friendRequests(): RelayFriendRequest[];
100
+ /** The user's answer to one asking to be friends. */
101
+ answerFriend(id: string, accept: boolean): Promise<void>;
102
+ /** No longer friends, on both sides. */
103
+ removeFriend(id: string): Promise<boolean>;
104
+ protected isFriends(live: Live): boolean;
105
+ protected friendIds(): Set<string>;
106
+ protected heardAsk(live: Live, ask: RelayHubAskBody): void;
107
+ protected heardMembers(members: RelayPublicIdentity[]): void;
108
+ protected offered(live: Live, from: RelayPublicIdentity, link: string): void;
109
+ /** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
110
+ protected after(event: RelayRoomEvent): void;
58
111
  protected owned(roomId: string): Live;
59
112
  protected member(roomId: string): Live;
60
113
  protected info(live: Live): RelayRoomInfo;
61
114
  }
115
+ export declare namespace RelayHubsUtil {
116
+ function isPrivate(identity: unknown): identity is RelayIdentity;
117
+ function isRecord(record: unknown): record is RelayRoomRecord;
118
+ /**
119
+ * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
120
+ * key and the owner's keys once either has them, the invitations of both, made once either made it.
121
+ */
122
+ function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
123
+ }