@celljs/ta-relay 4.13.0 → 4.14.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/README.en.md +9 -1
  2. package/README.md +9 -1
  3. package/esm/common/index.d.ts +3 -0
  4. package/esm/common/index.js +3 -0
  5. package/esm/common/relay-friends.js +29 -4
  6. package/esm/common/relay-host-calls.d.ts +34 -0
  7. package/esm/common/relay-host-calls.js +126 -0
  8. package/esm/common/relay-hubs-connection.d.ts +9 -1
  9. package/esm/common/relay-hubs-connection.js +33 -1
  10. package/esm/common/relay-hubs-protocol.d.ts +9 -1
  11. package/esm/common/relay-hubs.d.ts +7 -0
  12. package/esm/common/relay-hubs.js +20 -3
  13. package/esm/common/relay-identity.d.ts +11 -0
  14. package/esm/common/relay-identity.js +12 -1
  15. package/esm/common/relay-protocol.d.ts +1 -1
  16. package/esm/common/relay-protocol.js +2 -1
  17. package/esm/common/relay-team.d.ts +144 -0
  18. package/esm/common/relay-team.js +200 -0
  19. package/esm/common/relay-web.d.ts +13 -0
  20. package/esm/common/relay-web.js +61 -0
  21. package/esm/join/join-i18n.d.ts +53 -0
  22. package/esm/join/join-i18n.js +103 -0
  23. package/esm/join/join.d.ts +1 -0
  24. package/esm/join/join.js +364 -0
  25. package/esm/node/relay-server.d.ts +7 -0
  26. package/esm/node/relay-server.js +28 -3
  27. package/esm/node/relay-viewer.d.ts +2 -0
  28. package/esm/node/relay-viewer.js +9 -4
  29. package/lib/common/index.d.ts +3 -0
  30. package/lib/common/index.js +3 -0
  31. package/lib/common/relay-friends.js +29 -4
  32. package/lib/common/relay-host-calls.d.ts +35 -0
  33. package/lib/common/relay-host-calls.js +130 -0
  34. package/lib/common/relay-hubs-connection.d.ts +9 -1
  35. package/lib/common/relay-hubs-connection.js +33 -1
  36. package/lib/common/relay-hubs-protocol.d.ts +9 -1
  37. package/lib/common/relay-hubs.d.ts +7 -0
  38. package/lib/common/relay-hubs.js +20 -3
  39. package/lib/common/relay-identity.d.ts +11 -0
  40. package/lib/common/relay-identity.js +12 -1
  41. package/lib/common/relay-protocol.d.ts +1 -1
  42. package/lib/common/relay-protocol.js +2 -1
  43. package/lib/common/relay-team.d.ts +145 -0
  44. package/lib/common/relay-team.js +204 -0
  45. package/lib/common/relay-web.d.ts +14 -0
  46. package/lib/common/relay-web.js +64 -0
  47. package/lib/join/join-i18n.d.ts +54 -0
  48. package/lib/join/join-i18n.js +106 -0
  49. package/lib/join/join.d.ts +2 -0
  50. package/lib/join/join.js +366 -0
  51. package/lib/node/relay-server.d.ts +7 -0
  52. package/lib/node/relay-server.js +27 -2
  53. package/lib/node/relay-viewer.d.ts +2 -0
  54. package/lib/node/relay-viewer.js +6 -1
  55. package/package.json +4 -4
  56. package/viewer/join.html +92 -0
  57. package/viewer/page.html +9 -9
  58. package/viewer/room.html +10 -10
package/README.en.md CHANGED
@@ -88,6 +88,14 @@ Which relay each feature uses is set by `relays.json` in the agent directory (de
88
88
 
89
89
  Rooms several Ta share ([docs/design/ta-link.md](../../docs/design/ta-link.md), section 3): `RelayHub` keeps on the relay the hashes of the members' tokens, the asks to join till the owner answers, the roster signed by the owner and the shared state, all sealed; it passes messages between members and sets the state on its version. On the Worker each room is a Durable Object (`RelayHubObject`); in `ta relay`, memory. On the side of Ta, `RelayHubs` (its identity in `identity.json`, rooms in `rooms.json`, friends in `friends.json`): making a room, links that invite (`/r#…`, `RelayHubUtil`), asks and the owner's answers (the key of the room sealed by ECDH and signed), and the signatures and sealing of messages and the state. `RelayHubs` is in `common/`, its store its own: `RelayHubFiles` for Ta, localStorage in a browser. A link that invites opens the page of web guests in a browser (`src/guest` → `viewer/room.html`, its script allowed by its hash, connecting to this relay alone): one without Ta asks there and, let in, only chats (`RelayRoomChat`); the Ta of the others take no other state it sets and none of its messages.
90
90
 
91
+ ## Joining another's Ta from a browser (`/j`, `/w/`)
92
+
93
+ A link that invites into a Ta (`/j#…`, `RelayTeamLinkUtil`; [docs/design/ta-team.md](../../docs/design/ta-team.md) 8.3) opens the page of joining in a browser (`src/join` → `viewer/join.html`, its script allowed by its hash, connecting to this relay alone): one without Ta asks with a name (`RelayTeamJoining`, through the device of the link, which may only `ask` and `wait`, end to end to the other ta serve, `via: 'web'`); once let in, the device comes sealed by ECDH for a key of that request alone (`RelayTeamSeal`), and the page goes to `/w/#pair=<pairing link>`. `/w/` is Ta's web page served by the relay (the same as ta serve's, its `base` made `/w/`, `RelayWebUtil`), which signs in with the `#pair=` of its address and reaches the Ta through the relay as the Ta app does; the pairing link is after `#`, never sent to the relay. A page closed while waiting goes on waiting when the link is opened again in the same browser (the request and its keys are kept there).
94
+
95
+ - Worker: `scripts/web.ts` of `apps/ta-relay` copies `apps/ta/dist/public` into `web/w/`, the static assets of the Worker (`ASSETS`), which it answers with the headers of `RelayWebUtil`.
96
+ - `ta relay`: the `web` option of `RelayServer` names those files (`ta relay` gives its own `dist/public`).
97
+ - The cost: the relay serves the page, so a relay broken into could change its script and read what a member in a browser sees. So one who came in from a browser gets no role nor grant that manages members (checked by the Ta service, see ta-team).
98
+
91
99
  ## Codes of a spot (`/v1/spot/<id>`)
92
100
 
93
- Where the network keeps devices apart (an office Wi-Fi that isolates them), Nearby of Ta meets by a code of six digits ([docs/design/ta-nearby.md](../../docs/design/ta-nearby.md) 2.2): `id` is a hash of the code, which the relay never knows. `RelaySpot` only keeps who is connected: the first to send `open` opens it, others `join` within 10 minutes, 50 at most; it passes each one's beacon (without port nor address) to the others, and sealed messages to one by its temporary id, which the relay cannot read; nothing is stored, and it is gone with the last one. On the Worker a `RelaySpotObject` for each code, in memory in `ta relay`. Against guessing: an address (an IPv6 one by its /64) that tried 10 codes nobody opened in 10 minutes is refused even a right one (close code 4428); only failures count (`RelaySpotTries`; on the Worker a `RelayTriesObject` for each address by `CF-Connecting-IP`, in `ta relay` the address of the connection).
101
+ Where the network keeps devices apart (an office Wi-Fi that isolates them), Nearby of Ta meets by a code of six digits ([docs/design/ta-nearby.md](../../docs/design/ta-nearby.md) 2.2): `id` is a hash of the code, which the relay never knows. `RelaySpot` only keeps who is connected: the first to send `open` opens it, others `join` within 10 minutes, 50 at most; it passes each one's beacon (without port nor address) to the others, and sealed messages to one by its temporary id, which the relay cannot read; nothing is stored, and it is gone with the last one. On the Worker a `RelaySpotObject` for each code, in memory in `ta relay`. Against guessing: an address (an IPv6 one by its /64) that tried 10 codes nobody opened in 10 minutes is refused even a right one (close code 4428); only failures count (`RelaySpotTries`; on the Worker a `RelayTriesObject` for each address by `CF-Connecting-IP`, in `ta relay` the address of the connection).
package/README.md CHANGED
@@ -88,6 +88,14 @@ Ta 服务也能经中继把本机的一个扩展交给另一台电脑(`RelaySt
88
88
 
89
89
  几台 Ta 共用的房间([docs/design/ta-link.md](../../docs/design/ta-link.md) 第 3 节):`RelayHub` 在中继上保存成员令牌的哈希、未处理的加入申请、房主签名的成员名单和共享状态(都是密文),转发成员之间的消息,按版本号写入状态;Worker 上每个房间一个 Durable Object(`RelayHubObject`),`ta relay` 里在内存。Ta 一侧是 `RelayHubs`(身份 `identity.json`、房间 `rooms.json`、好友 `friends.json`):建房、邀请链接(`/r#…`,`RelayHubUtil`)、申请与房主答复(ECDH 加密房间密钥并签名)、消息与状态的签名和加密。`RelayHubs` 在 `common/`,存储可换:Ta 用 `RelayHubFiles`,浏览器用 localStorage。邀请链接在浏览器里打开的是网页访客页(`src/guest` → `viewer/room.html`,脚本按哈希放行,只连本中继):没装 Ta 的人在网页里申请,房主同意后只能聊天(`RelayRoomChat`),其他成员的 Ta 不采用访客写的其他状态和发的消息。
90
90
 
91
+ ## 从浏览器加入别人的 Ta(`/j`、`/w/`)
92
+
93
+ 协作邀请链接(`/j#…`,`RelayTeamLinkUtil`;见 [docs/design/ta-team.md](../../docs/design/ta-team.md) 8.3)在浏览器里打开的是加入页(`src/join` → `viewer/join.html`,脚本按哈希放行,只连本中继):没装 Ta 的人填名字申请(`RelayTeamJoining`,用链接里那台只能 `ask` / `wait` 的设备,经中继端到端加密连到对方的 ta serve,`via: 'web'`),对方同意后设备用 ECDH 封给这次申请的临时密钥(`RelayTeamSeal`),页面拿到配对链接就跳到 `/w/#pair=<配对链接>`。`/w/` 是中继下发的 Ta 网页(和 ta serve 的同一份,`base` 改成 `/w/`,`RelayWebUtil`),从地址的 `#pair=` 登录后像 Ta 应用一样经中继连过去;配对链接只在 `#` 后,中继收不到。申请没等到答复就关了页面,同一个浏览器再打开链接会接着等(申请和临时密钥存在本机)。
94
+
95
+ - Worker:`apps/ta-relay` 的 `scripts/web.ts` 把 `apps/ta/dist/public` 拷到 `web/w/`,作为 Worker 的静态资源(`ASSETS`),由 Worker 按 `RelayWebUtil` 的头返回。
96
+ - `ta relay`:`RelayServer` 的 `web` 选项指向这份文件(`ta relay` 用自己 `dist/public`)。
97
+ - 代价:网页由中继下发,中继被攻破就能换掉脚本、读到网页版成员看得到的内容。所以从浏览器加入的人不能拿到管理成员的角色或授权(Ta 服务一侧检查,见 ta-team)。
98
+
91
99
  ## 在场码(`/v1/spot/<id>`)
92
100
 
93
- 同一网络里看不到彼此时(公司 Wi-Fi 隔离设备),Ta 的"附近"用 6 位在场码碰头([docs/design/ta-nearby.md](../../docs/design/ta-nearby.md) 2.2):`id` 是在场码的哈希,中继不知道数字本身。`RelaySpot` 只记谁连着:第一个发 `open` 的人打开它,10 分钟内别人 `join` 进来,最多 50 人;它把每个人的信标(不带端口和地址)转给其他人,把加密消息按临时 id 转给对方,内容中继读不到;什么都不存,最后一人离开就没了。Worker 上每个在场码一个 `RelaySpotObject`,`ta relay` 里在内存。防穷举:一个地址(IPv6 按 /64)10 分钟内试了 10 个没人打开的码,之后连正确的码也拒绝(关闭码 4428),只数失败(`RelaySpotTries`;Worker 上按 `CF-Connecting-IP` 每个地址一个 `RelayTriesObject`,`ta relay` 用连接的对端地址)。
101
+ 同一网络里看不到彼此时(公司 Wi-Fi 隔离设备),Ta 的"附近"用 6 位在场码碰头([docs/design/ta-nearby.md](../../docs/design/ta-nearby.md) 2.2):`id` 是在场码的哈希,中继不知道数字本身。`RelaySpot` 只记谁连着:第一个发 `open` 的人打开它,10 分钟内别人 `join` 进来,最多 50 人;它把每个人的信标(不带端口和地址)转给其他人,把加密消息按临时 id 转给对方,内容中继读不到;什么都不存,最后一人离开就没了。Worker 上每个在场码一个 `RelaySpotObject`,`ta relay` 里在内存。防穷举:一个地址(IPv6 按 /64)10 分钟内试了 10 个没人打开的码,之后连正确的码也拒绝(关闭码 4428),只数失败(`RelaySpotTries`;Worker 上按 `CF-Connecting-IP` 每个地址一个 `RelayTriesObject`,`ta relay` 用连接的对端地址)。
@@ -8,6 +8,7 @@ export * from './relay-conversation.js';
8
8
  export * from './relay-faces.js';
9
9
  export * from './relay-files.js';
10
10
  export * from './relay-friends.js';
11
+ export * from './relay-host-calls.js';
11
12
  export * from './relay-hub.js';
12
13
  export * from './relay-hub-util.js';
13
14
  export * from './relay-hubs.js';
@@ -21,3 +22,5 @@ export * from './relay-room-chat.js';
21
22
  export * from './relay-share.js';
22
23
  export * from './relay-socket.js';
23
24
  export * from './relay-spot.js';
25
+ export * from './relay-team.js';
26
+ export * from './relay-web.js';
@@ -8,6 +8,7 @@ export * from './relay-conversation.js';
8
8
  export * from './relay-faces.js';
9
9
  export * from './relay-files.js';
10
10
  export * from './relay-friends.js';
11
+ export * from './relay-host-calls.js';
11
12
  export * from './relay-hub.js';
12
13
  export * from './relay-hub-util.js';
13
14
  export * from './relay-hubs.js';
@@ -21,4 +22,6 @@ export * from './relay-room-chat.js';
21
22
  export * from './relay-share.js';
22
23
  export * from './relay-socket.js';
23
24
  export * from './relay-spot.js';
25
+ export * from './relay-team.js';
26
+ export * from './relay-web.js';
24
27
  //# sourceMappingURL=index.js.map
@@ -117,7 +117,13 @@ export class RelayFriends {
117
117
  ? this.rooms
118
118
  .asksOf(mine.id)
119
119
  .filter(ask => !ask.guest && !chosen.has(ask.id))
120
- .map(ask => ({ id: ask.id, name: ask.name, at: ask.at, via: 'link' }))
120
+ .map(ask => ({
121
+ id: ask.id,
122
+ name: ask.name,
123
+ ...(RelayIdentityUtil.isLook(ask.look) ? { look: ask.look } : {}),
124
+ at: ask.at,
125
+ via: 'link',
126
+ }))
121
127
  : [];
122
128
  const offers = book.offers.filter(offer => !chosen.has(offer.id) && !asks.some(ask => ask.id === offer.id)).map(RelayFriendsUtil.requestOf);
123
129
  return [...asks, ...offers].sort((a, b) => a.at - b.at);
@@ -126,7 +132,10 @@ export class RelayFriends {
126
132
  asked(ask) {
127
133
  if (!ask.guest && !this.read().friends.some(friend => friend.id === ask.id) && !this.told.has(ask.member)) {
128
134
  this.told.add(ask.member);
129
- this.emit({ type: 'friend', request: { id: ask.id, name: ask.name, at: ask.at, via: 'link' } });
135
+ this.emit({
136
+ type: 'friend',
137
+ request: { id: ask.id, name: ask.name, ...(RelayIdentityUtil.isLook(ask.look) ? { look: ask.look } : {}), at: ask.at, via: 'link' },
138
+ });
130
139
  this.emit({ type: 'rooms' });
131
140
  }
132
141
  this.changed();
@@ -174,12 +183,14 @@ export class RelayFriends {
174
183
  const mine = rooms.find(room => room.owner && room.kind === RELAY_FRIENDS_KIND);
175
184
  return this.read()
176
185
  .friends.map(friend => {
186
+ var _a, _b;
177
187
  const member = mine === null || mine === void 0 ? void 0 : mine.members.find(item => item.id === friend.id && !item.owner);
178
188
  const theirs = this.theirs(rooms, friend.id);
179
189
  const owner = theirs === null || theirs === void 0 ? void 0 : theirs.members.find(item => item.owner);
180
190
  return {
181
191
  id: friend.id,
182
192
  name: (member === null || member === void 0 ? void 0 : member.name) || (owner === null || owner === void 0 ? void 0 : owner.name) || friend.name,
193
+ ...(RelayIdentityUtil.isLook(((_a = member !== null && member !== void 0 ? member : owner) !== null && _a !== void 0 ? _a : friend).look) ? { look: ((_b = member !== null && member !== void 0 ? member : owner) !== null && _b !== void 0 ? _b : friend).look } : {}),
183
194
  status: member && (theirs === null || theirs === void 0 ? void 0 : theirs.status) === 'member' ? 'friend' : 'adding',
184
195
  online: !!(member === null || member === void 0 ? void 0 : member.online) || !!(owner === null || owner === void 0 ? void 0 : owner.online),
185
196
  metAt: friend.metAt,
@@ -206,7 +217,13 @@ export class RelayFriends {
206
217
  const now = Date.now();
207
218
  for (const member of members) {
208
219
  const known = book.friends.find(friend => friend.id === member.id);
209
- if (known && (known.name !== member.name || known.sign !== member.sign || known.box !== member.box || now - known.seenAt > DAY_MS)) {
220
+ if (known &&
221
+ (known.name !== member.name ||
222
+ known.look !== member.look ||
223
+ known.sign !== member.sign ||
224
+ known.box !== member.box ||
225
+ now - known.seenAt > DAY_MS)) {
226
+ delete known.look;
210
227
  Object.assign(known, RelayIdentityUtil.publicOf(member), { seenAt: now });
211
228
  changed = true;
212
229
  }
@@ -403,6 +420,7 @@ export var RelayFriendsUtil;
403
420
  return {
404
421
  id: friend.id,
405
422
  name: String((_a = friend.name) !== null && _a !== void 0 ? _a : '').slice(0, 40),
423
+ ...(RelayIdentityUtil.isLook(friend.look) ? { look: friend.look } : {}),
406
424
  ...(typeof friend.sign === 'string' ? { sign: friend.sign } : {}),
407
425
  ...(typeof friend.box === 'string' ? { box: friend.box } : {}),
408
426
  metAt: friend.metAt,
@@ -425,7 +443,14 @@ export var RelayFriendsUtil;
425
443
  }
426
444
  RelayFriendsUtil.mergeFriend = mergeFriend;
427
445
  function requestOf(offer) {
428
- return { id: offer.id, name: offer.name, at: offer.at, via: 'room', room: offer.room };
446
+ return {
447
+ id: offer.id,
448
+ name: offer.name,
449
+ ...(RelayIdentityUtil.isLook(offer.look) ? { look: offer.look } : {}),
450
+ at: offer.at,
451
+ via: 'room',
452
+ room: offer.room,
453
+ };
429
454
  }
430
455
  RelayFriendsUtil.requestOf = requestOf;
431
456
  /** `friends.json` as it is now: those of before kept only if they were Moments friends (see migrate). */
@@ -0,0 +1,34 @@
1
+ import { type RelayPairing } from './relay-pairing.js';
2
+ import { RelaySocket } from './relay-socket.js';
3
+ /**
4
+ * Calls of the host of a package on another Ta, through the relay with a device of it (end to end): asking to come in
5
+ * with the device of a link (RelayTeamJoining), or reading what is shared with the user there from their own Ta. The
6
+ * other Ta checks every call as it does those of its pages.
7
+ */
8
+ export declare class RelayHostCalls {
9
+ protected readonly pairing: RelayPairing;
10
+ /** The package whose host is called. */
11
+ protected readonly name: string;
12
+ protected socket?: RelaySocket;
13
+ protected opening?: Promise<void>;
14
+ protected next: number;
15
+ protected readonly calls: Map<number, {
16
+ resolve(value: unknown): void;
17
+ reject(error: Error): void;
18
+ timer: ReturnType<typeof setTimeout>;
19
+ }>;
20
+ protected closed: boolean;
21
+ constructor(pairing: RelayPairing,
22
+ /** The package whose host is called. */
23
+ name: string);
24
+ /** From the pairing link of a device. */
25
+ static of(link: string, name: string): RelayHostCalls;
26
+ open(): Promise<void>;
27
+ /** Opens, again after a pause when the connection drops before it opened (not when the other Ta refused the device). */
28
+ protected tryOpen(tries: number): Promise<void>;
29
+ protected openOnce(): Promise<void>;
30
+ callHost(method: string, ...params: unknown[]): Promise<unknown>;
31
+ close(): void;
32
+ protected failAll(error: Error): void;
33
+ protected receive(data: string): void;
34
+ }
@@ -0,0 +1,126 @@
1
+ import { RelayPairingUtil } from './relay-pairing.js';
2
+ import { RelaySocket } from './relay-socket.js';
3
+ /** How long a call to another Ta waits for its answer. */
4
+ const CALL_MS = 30000;
5
+ /** Tries to reach it, a while apart: a Ta that just made the link may be on its way to the relay. */
6
+ const OPEN_TRIES = 6;
7
+ const OPEN_PAUSE_MS = 700;
8
+ /**
9
+ * Calls of the host of a package on another Ta, through the relay with a device of it (end to end): asking to come in
10
+ * with the device of a link (RelayTeamJoining), or reading what is shared with the user there from their own Ta. The
11
+ * other Ta checks every call as it does those of its pages.
12
+ */
13
+ export class RelayHostCalls {
14
+ constructor(pairing,
15
+ /** The package whose host is called. */
16
+ name) {
17
+ this.pairing = pairing;
18
+ this.name = name;
19
+ this.next = 1;
20
+ this.calls = new Map();
21
+ this.closed = false;
22
+ }
23
+ /** From the pairing link of a device. */
24
+ static of(link, name) {
25
+ return new RelayHostCalls(RelayPairingUtil.parse(link), name);
26
+ }
27
+ open() {
28
+ var _a;
29
+ (_a = this.opening) !== null && _a !== void 0 ? _a : (this.opening = this.tryOpen(OPEN_TRIES));
30
+ return this.opening;
31
+ }
32
+ /** Opens, again after a pause when the connection drops before it opened (not when the other Ta refused the device). */
33
+ async tryOpen(tries) {
34
+ for (let left = tries;; left--) {
35
+ try {
36
+ await this.openOnce();
37
+ return;
38
+ }
39
+ catch (error) {
40
+ if (left <= 1 || this.closed || error.refused) {
41
+ this.opening = undefined;
42
+ throw error;
43
+ }
44
+ await new Promise(resolve => setTimeout(resolve, OPEN_PAUSE_MS));
45
+ }
46
+ }
47
+ }
48
+ openOnce() {
49
+ return new Promise((resolve, reject) => {
50
+ let opened = false;
51
+ const socket = new RelaySocket(this.pairing);
52
+ this.socket = socket;
53
+ socket.onopen = () => {
54
+ opened = true;
55
+ resolve();
56
+ };
57
+ socket.onerror = () => {
58
+ if (!opened) {
59
+ reject(new Error('The other Ta did not answer: is it running?'));
60
+ }
61
+ };
62
+ socket.onclose = event => {
63
+ const refused = event.code === RelaySocket.REFUSED;
64
+ const error = Object.assign(new Error(refused ? 'The other Ta no longer knows this device.' : 'The connection to the other Ta was lost.'), {
65
+ refused,
66
+ });
67
+ if (!opened) {
68
+ reject(error);
69
+ return;
70
+ }
71
+ this.failAll(error);
72
+ this.opening = undefined;
73
+ };
74
+ socket.onmessage = message => this.receive(message.data);
75
+ });
76
+ }
77
+ async callHost(method, ...params) {
78
+ await this.open();
79
+ const id = this.next++;
80
+ return new Promise((resolve, reject) => {
81
+ var _a;
82
+ const timer = setTimeout(() => {
83
+ this.calls.delete(id);
84
+ reject(new Error('The other Ta did not answer in time.'));
85
+ }, CALL_MS);
86
+ this.calls.set(id, { resolve, reject, timer });
87
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ jsonrpc: '2.0', id, method: 'TaPackageServer/callPackageHost', params: [this.name, method, params] }));
88
+ });
89
+ }
90
+ close() {
91
+ var _a;
92
+ this.closed = true;
93
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.close();
94
+ this.failAll(new Error('Stopped.'));
95
+ }
96
+ failAll(error) {
97
+ for (const call of this.calls.values()) {
98
+ clearTimeout(call.timer);
99
+ call.reject(error);
100
+ }
101
+ this.calls.clear();
102
+ }
103
+ receive(data) {
104
+ var _a;
105
+ let message;
106
+ try {
107
+ message = JSON.parse(data);
108
+ }
109
+ catch (_b) {
110
+ return;
111
+ }
112
+ const call = typeof message.id === 'number' ? this.calls.get(message.id) : undefined;
113
+ if (!call) {
114
+ return;
115
+ }
116
+ this.calls.delete(message.id);
117
+ clearTimeout(call.timer);
118
+ if (message.error) {
119
+ call.reject(new Error((_a = message.error.message) !== null && _a !== void 0 ? _a : 'Failed.'));
120
+ }
121
+ else {
122
+ call.resolve(message.result);
123
+ }
124
+ }
125
+ }
126
+ //# sourceMappingURL=relay-host-calls.js.map
@@ -5,8 +5,9 @@ import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest } from '
5
5
  /** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
6
6
  export interface MessageBody {
7
7
  d?: unknown;
8
- /** A member's new name, for the owner to put in the roster. */
8
+ /** A member's new name, for the owner to put in the roster; with its look (RelayPublicIdentity.look), '' for none. */
9
9
  name?: string;
10
+ look?: string;
10
11
  /** Asks the one it is sent to to be friends: the link of the sender's room of friends (RelayFriends.offer). */
11
12
  friend?: string;
12
13
  }
@@ -43,6 +44,8 @@ export declare class Live {
43
44
  timer?: ReturnType<typeof setTimeout>;
44
45
  pingTimer?: ReturnType<typeof setInterval>;
45
46
  awaitingPong: boolean;
47
+ /** The look this Ta last sent the owner, for the roster to say: once, whatever the owner does with it. */
48
+ toldLook?: string;
46
49
  /** Events are handled one after another: opening them is async. */
47
50
  chain: Promise<void>;
48
51
  constructor(record: RelayRoomRecord);
@@ -94,6 +97,11 @@ export declare abstract class RelayHubsConnection {
94
97
  /** A member asks the user to be friends, by the link of its room of friends. */
95
98
  protected offered(_live: Live, _from: RelayPublicIdentity, _link: string): void;
96
99
  protected readRoster(live: Live, sealed: string): Promise<void>;
100
+ /**
101
+ * The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
102
+ * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
103
+ */
104
+ protected keepLook(live: Live): Promise<void>;
97
105
  protected guestOf(live: Live, member: string): boolean;
98
106
  protected signOf(live: Live): (member: string) => string | undefined;
99
107
  protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
@@ -72,6 +72,7 @@ export class RelayHubsConnection {
72
72
  ask: ask.member,
73
73
  id: ask.id,
74
74
  name: ask.name,
75
+ ...(RelayIdentityUtil.isLook(ask.look) ? { look: ask.look } : {}),
75
76
  friend: friends.has(ask.id),
76
77
  ...(ask.guest ? { guest: true } : {}),
77
78
  at: ask.at,
@@ -413,6 +414,35 @@ export class RelayHubsConnection {
413
414
  this.heardMembers(members.filter(member => member.id !== own && !member.guest));
414
415
  if (!members.some(member => member.member === live.record.member) && !live.record.own && live.record.status === 'member') {
415
416
  this.setStatus(live, 'gone', 'removed');
417
+ return;
418
+ }
419
+ // Not awaited: what it sends waits for the room to be ready, which may wait for this event.
420
+ void this.keepLook(live);
421
+ }
422
+ /**
423
+ * The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
424
+ * a member tells the owner, once a connection. A room joined with another identity keeps that one's.
425
+ */
426
+ async keepLook(live) {
427
+ var _a, _b;
428
+ if (live.record.status !== 'member' || live.record.identity || !live.roster) {
429
+ return;
430
+ }
431
+ const identity = await this.identity();
432
+ const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
433
+ const self = live.roster.members.find(member => member.member === live.record.member);
434
+ if (!self || ((_b = self.look) !== null && _b !== void 0 ? _b : '') === look) {
435
+ return;
436
+ }
437
+ if (live.record.own) {
438
+ await this.writeRoster(live, members => members.map(member => (member.member === self.member ? RelayIdentityUtil.withLook(member, look) : member))).catch(() => undefined);
439
+ return;
440
+ }
441
+ const owner = live.roster.members.find(member => member.owner);
442
+ if (owner && live.toldLook !== look) {
443
+ await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
444
+ live.toldLook = look;
445
+ }, () => undefined);
416
446
  }
417
447
  }
418
448
  guestOf(live, member) {
@@ -469,7 +499,9 @@ export class RelayHubsConnection {
469
499
  if (typeof signed.b.name === 'string') {
470
500
  if (live.record.own && sender) {
471
501
  const name = signed.b.name.trim().slice(0, 40);
472
- await this.writeRoster(live, members => members.map(member => (member.member === from ? { ...member, name } : member))).catch(() => undefined);
502
+ // A Ta of before tells its name alone: its look stays as it was.
503
+ const look = typeof signed.b.look === 'string' ? signed.b.look : sender.look;
504
+ await this.writeRoster(live, members => members.map(member => (member.member === from ? RelayIdentityUtil.withLook({ ...member, name }, look) : member))).catch(() => undefined);
473
505
  }
474
506
  return;
475
507
  }
@@ -56,6 +56,8 @@ export interface RelayRoomMember {
56
56
  /** The identity of its Ta. */
57
57
  id: string;
58
58
  name: string;
59
+ /** The look of its double (RelayPublicIdentity.look). */
60
+ look?: string;
59
61
  owner: boolean;
60
62
  online: boolean;
61
63
  self: boolean;
@@ -88,6 +90,8 @@ export interface RelayRoomRequest {
88
90
  /** Its identity, and the name it gives. */
89
91
  id: string;
90
92
  name: string;
93
+ /** The look of its double (RelayPublicIdentity.look). */
94
+ look?: string;
91
95
  /** Met before, in another room. */
92
96
  friend: boolean;
93
97
  /** Asks from a browser, without Ta: admitted, it only chats. */
@@ -98,7 +102,7 @@ export interface RelayRoomRequest {
98
102
  * 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
103
  * only its id and name), when chosen and last heard of.
100
104
  */
101
- export interface RelayFriend extends Pick<RelayPublicIdentity, 'id' | 'name'>, Partial<Pick<RelayPublicIdentity, 'sign' | 'box'>> {
105
+ export interface RelayFriend extends Pick<RelayPublicIdentity, 'id' | 'name'>, Partial<Pick<RelayPublicIdentity, 'sign' | 'box' | 'look'>> {
102
106
  metAt: number;
103
107
  seenAt: number;
104
108
  }
@@ -106,6 +110,8 @@ export interface RelayFriend extends Pick<RelayPublicIdentity, 'id' | 'name'>, P
106
110
  export interface RelayFriendInfo {
107
111
  id: string;
108
112
  name: string;
113
+ /** The look of its double (RelayPublicIdentity.look). */
114
+ look?: string;
109
115
  status: 'friend' | 'adding';
110
116
  online: boolean;
111
117
  metAt: number;
@@ -115,6 +121,8 @@ export interface RelayFriendInfo {
115
121
  export interface RelayFriendRequest {
116
122
  id: string;
117
123
  name: string;
124
+ /** The look of its double (RelayPublicIdentity.look). */
125
+ look?: string;
118
126
  at: number;
119
127
  via: 'link' | 'room';
120
128
  room?: string;
@@ -17,6 +17,13 @@ export declare class RelayHubs extends RelayHubsConnection {
17
17
  onEvent(listener: (event: RelayRoomEvent) => void): () => void;
18
18
  /** The name others see; the owners of rooms this Ta is in hear it. */
19
19
  setName(name: string): Promise<RelayPublicIdentity>;
20
+ /**
21
+ * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
22
+ * look that is none. The owners of rooms this Ta is in hear it, as a name.
23
+ */
24
+ setLook(look: string | undefined): Promise<RelayPublicIdentity>;
25
+ /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
26
+ protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
20
27
  list(): RelayRoomInfo[];
21
28
  room(id: string): RelayRoomInfo | undefined;
22
29
  create(input: {
@@ -71,7 +71,20 @@ export class RelayHubs extends RelayHubsConnection {
71
71
  if (!trimmed) {
72
72
  throw new Error('A name is needed.');
73
73
  }
74
- const identity = { ...(await this.identity()), name: trimmed };
74
+ return this.changeSelf({ ...(await this.identity()), name: trimmed });
75
+ }
76
+ /**
77
+ * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
78
+ * look that is none. The owners of rooms this Ta is in hear it, as a name.
79
+ */
80
+ async setLook(look) {
81
+ const identity = await this.identity();
82
+ const next = RelayIdentityUtil.withLook(identity, look);
83
+ return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
84
+ }
85
+ /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
86
+ async changeSelf(identity) {
87
+ var _a;
75
88
  this.options.store.write('identity', identity);
76
89
  this.own = identity;
77
90
  for (const live of this.live.values()) {
@@ -80,12 +93,15 @@ export class RelayHubs extends RelayHubsConnection {
80
93
  continue;
81
94
  }
82
95
  if (live.record.own) {
83
- await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? { ...member, name: trimmed } : member))).catch(() => undefined);
96
+ await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
84
97
  }
85
98
  else if (live.roster) {
86
99
  const owner = live.roster.members.find(member => member.owner);
87
100
  if (owner) {
88
- await this.sendBody(live, { name: trimmed }, owner.member).catch(() => undefined);
101
+ const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
102
+ await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
103
+ live.toldLook = look;
104
+ }, () => undefined);
89
105
  }
90
106
  }
91
107
  }
@@ -541,6 +557,7 @@ export class RelayHubs extends RelayHubsConnection {
541
557
  member: member.member,
542
558
  id: member.id,
543
559
  name: member.name,
560
+ ...(RelayIdentityUtil.isLook(member.look) ? { look: member.look } : {}),
544
561
  owner: !!member.owner,
545
562
  online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
546
563
  self: member.member === record.member,
@@ -7,6 +7,11 @@ export interface RelayPublicIdentity {
7
7
  sign: string;
8
8
  /** ECDH P-256: what keys for it are agreed with. */
9
9
  box: string;
10
+ /**
11
+ * The kind of dinosaur its double is (a species of Ta's page, `trex`): others draw this Ta as that double. None from a
12
+ * Ta of before, or a browser.
13
+ */
14
+ look?: string;
10
15
  }
11
16
  /** A Ta's own identity, its private keys as JWK; in `identity.json` of the agent directory. */
12
17
  export interface RelayIdentity extends RelayPublicIdentity {
@@ -37,6 +42,12 @@ export declare namespace RelayIdentityUtil {
37
42
  /** Whether the keys are what the id says, and well formed. */
38
43
  function check(identity: RelayPublicIdentity): Promise<boolean>;
39
44
  function publicOf(identity: RelayPublicIdentity): RelayPublicIdentity;
45
+ /** Whether it is a look as a Ta gives it: a short word of letters, digits and dashes. */
46
+ function isLook(look: unknown): look is string;
47
+ /** `value` with `look`, or without any for one that is none. */
48
+ function withLook<T extends {
49
+ look?: string;
50
+ }>(value: T, look: unknown): T;
40
51
  function sign(identity: RelayIdentity, text: string): Promise<string>;
41
52
  /** False for a signature that is not of this text by this key, or not a signature at all. */
42
53
  function verify(signPublic: string, text: string, signature: string): Promise<boolean>;
@@ -88,9 +88,20 @@ export var RelayIdentityUtil;
88
88
  }
89
89
  RelayIdentityUtil.check = check;
90
90
  function publicOf(identity) {
91
- return { id: identity.id, name: identity.name, sign: identity.sign, box: identity.box };
91
+ return withLook({ id: identity.id, name: identity.name, sign: identity.sign, box: identity.box }, identity.look);
92
92
  }
93
93
  RelayIdentityUtil.publicOf = publicOf;
94
+ /** Whether it is a look as a Ta gives it: a short word of letters, digits and dashes. */
95
+ function isLook(look) {
96
+ return typeof look === 'string' && /^[a-z][a-z0-9-]{0,31}$/.test(look);
97
+ }
98
+ RelayIdentityUtil.isLook = isLook;
99
+ /** `value` with `look`, or without any for one that is none. */
100
+ function withLook(value, look) {
101
+ const { look: _, ...rest } = value;
102
+ return (isLook(look) ? { ...rest, look } : rest);
103
+ }
104
+ RelayIdentityUtil.withLook = withLook;
94
105
  async function sign(identity, text) {
95
106
  const key = await subtle().importKey('jwk', identity.signKey, SIGN, false, ['sign']);
96
107
  return RelayBytes.toBase64(new Uint8Array(await subtle().sign(SIGNATURE, key, encoder.encode(text))));
@@ -28,7 +28,7 @@ export declare const RELAY_FRAME_HEADER_CSP = "default-src 'none'; script-src 'u
28
28
  /** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
29
29
  export declare const RELAY_VIEWER_HEADERS: {
30
30
  readonly 'content-type': 'text/html; charset=utf-8';
31
- readonly 'cache-control': 'no-cache';
31
+ readonly 'cache-control': 'no-cache, no-transform';
32
32
  readonly 'referrer-policy': 'no-referrer';
33
33
  readonly 'x-content-type-options': 'nosniff';
34
34
  };
@@ -28,7 +28,8 @@ export const RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self
28
28
  /** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
29
29
  export const RELAY_VIEWER_HEADERS = {
30
30
  'content-type': 'text/html; charset=utf-8',
31
- 'cache-control': 'no-cache',
31
+ // no-transform: a CDN in front (Cloudflare's analytics) puts no script of its own into a page that keeps keys.
32
+ 'cache-control': 'no-cache, no-transform',
32
33
  'referrer-policy': 'no-referrer',
33
34
  'x-content-type-options': 'nosniff',
34
35
  };