@celljs/ta-relay 4.10.0 → 4.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.en.md +13 -1
  2. package/README.md +13 -1
  3. package/esm/common/index.d.ts +9 -0
  4. package/esm/common/index.js +9 -0
  5. package/esm/common/relay-blobs.d.ts +54 -0
  6. package/esm/common/relay-blobs.js +153 -0
  7. package/esm/common/relay-card.d.ts +23 -0
  8. package/esm/common/relay-card.js +30 -0
  9. package/esm/common/relay-content.d.ts +104 -0
  10. package/esm/common/relay-content.js +316 -0
  11. package/esm/common/relay-conversation.d.ts +59 -0
  12. package/esm/common/relay-conversation.js +9 -0
  13. package/esm/common/relay-hub-util.d.ts +91 -0
  14. package/esm/common/relay-hub-util.js +164 -0
  15. package/esm/common/relay-hub.d.ts +125 -0
  16. package/esm/common/relay-hub.js +438 -0
  17. package/esm/common/relay-hubs.d.ts +271 -0
  18. package/esm/common/relay-hubs.js +844 -0
  19. package/esm/common/relay-identity.d.ts +46 -0
  20. package/esm/common/relay-identity.js +126 -0
  21. package/esm/common/relay-protocol.d.ts +211 -2
  22. package/esm/common/relay-protocol.js +53 -0
  23. package/esm/common/relay-room-chat.d.ts +80 -0
  24. package/esm/common/relay-room-chat.js +152 -0
  25. package/esm/common/relay-socket.d.ts +8 -0
  26. package/esm/common/relay-socket.js +23 -2
  27. package/esm/guest/guest-i18n.d.ts +47 -0
  28. package/esm/guest/guest-i18n.js +97 -0
  29. package/esm/guest/guest-style.d.ts +1 -0
  30. package/esm/guest/guest-style.js +78 -0
  31. package/esm/guest/guest.d.ts +1 -0
  32. package/esm/guest/guest.js +371 -0
  33. package/esm/node/index.d.ts +2 -0
  34. package/esm/node/index.js +2 -0
  35. package/esm/node/relay-host.d.ts +25 -3
  36. package/esm/node/relay-host.js +115 -4
  37. package/esm/node/relay-hub-files.d.ts +12 -0
  38. package/esm/node/relay-hub-files.js +39 -0
  39. package/esm/node/relay-server.d.ts +46 -1
  40. package/esm/node/relay-server.js +227 -1
  41. package/esm/node/relay-store.d.ts +72 -0
  42. package/esm/node/relay-store.js +123 -2
  43. package/esm/node/relay-viewer.d.ts +7 -0
  44. package/esm/node/relay-viewer.js +33 -0
  45. package/esm/viewer/viewer-i18n.d.ts +56 -0
  46. package/esm/viewer/viewer-i18n.js +117 -0
  47. package/esm/viewer/viewer-markdown.d.ts +7 -0
  48. package/esm/viewer/viewer-markdown.js +198 -0
  49. package/esm/viewer/viewer-style.d.ts +1 -0
  50. package/esm/viewer/viewer-style.js +72 -0
  51. package/esm/viewer/viewer.d.ts +8 -0
  52. package/esm/viewer/viewer.js +378 -0
  53. package/lib/common/index.d.ts +9 -0
  54. package/lib/common/index.js +9 -0
  55. package/lib/common/relay-blobs.d.ts +55 -0
  56. package/lib/common/relay-blobs.js +157 -0
  57. package/lib/common/relay-card.d.ts +24 -0
  58. package/lib/common/relay-card.js +33 -0
  59. package/lib/common/relay-content.d.ts +105 -0
  60. package/lib/common/relay-content.js +320 -0
  61. package/lib/common/relay-conversation.d.ts +60 -0
  62. package/lib/common/relay-conversation.js +12 -0
  63. package/lib/common/relay-hub-util.d.ts +92 -0
  64. package/lib/common/relay-hub-util.js +167 -0
  65. package/lib/common/relay-hub.d.ts +126 -0
  66. package/lib/common/relay-hub.js +442 -0
  67. package/lib/common/relay-hubs.d.ts +272 -0
  68. package/lib/common/relay-hubs.js +848 -0
  69. package/lib/common/relay-identity.d.ts +47 -0
  70. package/lib/common/relay-identity.js +129 -0
  71. package/lib/common/relay-protocol.d.ts +211 -2
  72. package/lib/common/relay-protocol.js +54 -1
  73. package/lib/common/relay-room-chat.d.ts +81 -0
  74. package/lib/common/relay-room-chat.js +156 -0
  75. package/lib/common/relay-socket.d.ts +8 -0
  76. package/lib/common/relay-socket.js +23 -2
  77. package/lib/guest/guest-i18n.d.ts +48 -0
  78. package/lib/guest/guest-i18n.js +100 -0
  79. package/lib/guest/guest-style.d.ts +2 -0
  80. package/lib/guest/guest-style.js +81 -0
  81. package/lib/guest/guest.d.ts +2 -0
  82. package/lib/guest/guest.js +373 -0
  83. package/lib/node/index.d.ts +2 -0
  84. package/lib/node/index.js +2 -0
  85. package/lib/node/relay-host.d.ts +25 -3
  86. package/lib/node/relay-host.js +115 -4
  87. package/lib/node/relay-hub-files.d.ts +13 -0
  88. package/lib/node/relay-hub-files.js +43 -0
  89. package/lib/node/relay-server.d.ts +46 -1
  90. package/lib/node/relay-server.js +226 -0
  91. package/lib/node/relay-store.d.ts +72 -0
  92. package/lib/node/relay-store.js +121 -0
  93. package/lib/node/relay-viewer.d.ts +8 -0
  94. package/lib/node/relay-viewer.js +32 -0
  95. package/lib/viewer/viewer-i18n.d.ts +57 -0
  96. package/lib/viewer/viewer-i18n.js +120 -0
  97. package/lib/viewer/viewer-markdown.d.ts +8 -0
  98. package/lib/viewer/viewer-markdown.js +201 -0
  99. package/lib/viewer/viewer-style.d.ts +2 -0
  100. package/lib/viewer/viewer-style.js +75 -0
  101. package/lib/viewer/viewer.d.ts +9 -0
  102. package/lib/viewer/viewer.js +381 -0
  103. package/package.json +5 -4
  104. package/viewer/frame.html +1 -0
  105. package/viewer/page.html +80 -0
  106. package/viewer/room.html +87 -0
package/README.en.md CHANGED
@@ -53,4 +53,16 @@ A Ta service can also hand an extension of its computer to another through the r
53
53
  ## Known Limitations
54
54
 
55
55
  - When going through Relay, only WebSocket calls work: local file preview goes through the channel of `RelayFiles` (above), while other uses of `/api/files` (an address put together for a phone) still do not.
56
- - Device keys are long-term with no forward secrecy: if a key leaks, old traffic in recordings can be decrypted. Unpairing invalidates the key.
56
+ - Device keys are long-term with no forward secrecy: if a key leaks, old traffic in recordings can be decrypted. Unpairing invalidates the key.
57
+
58
+ ## Content shared (conversations, pages, files)
59
+
60
+ `RelayContentUtil` (common) and the `content` mode of `RelayHost`: links `https://<relay>/s#v=1&h=<host>&s=<share>&k=<key>` (`p=1` asks a password, `m=self` only for its owner and without a key, `o=1` a copy on the relay). The viewer comes with the share as its device id, but **proves first** that it has the key (the host's `ready` has no `proof`), else one with the link could try passwords offline; the host counts wrong keys of each share, refuses it 10 minutes after 10 in 10 minutes, and says a wrong key in the clear (`{t:'refused', reason:'key'}`). Shares are `contents` of `relay.json`, their bytes in `shares/<id>` of the agent directory.
61
+
62
+ "Opens while this computer is off": the host seals the content with a key derived from its own and `PUT /v1/blob/<share>` (`RelayBlobs`; on the Worker a `RelayBlobObject` for each share, deleted by its alarm when it runs out; in memory in `ta relay`). It is read with `x-ta-auth`, derived from the key of the content, of which the relay keeps the SHA-256; 20 wrong ones answer 429.
63
+
64
+ `/s` is the viewer for a browser (`src/viewer/`, built by `scripts/build-viewer.ts` into `viewer/page.html`, its script allowed by its hash), which decrypts on the page; a shared HTML page runs in the sandboxed frame of `/s/frame` (no `allow-same-origin`, no `connect-src`). Design notes: [docs/design/ta-link.md](../../docs/design/ta-link.md).
65
+
66
+ ## Rooms (`/v1/hub/<room>`)
67
+
68
+ 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.
package/README.md CHANGED
@@ -53,4 +53,16 @@ Ta 服务也能经中继把本机的一个扩展交给另一台电脑(`RelaySt
53
53
  ## 已知限制
54
54
 
55
55
  - 经 Relay 时只有 WebSocket 上的调用可用:本地文件的预览走 `RelayFiles` 的通道(见上),`/api/files` 的其他用法(比如自己拼一个地址给手机)仍然不行。
56
- - 设备密钥是长期的,没有前向保密:密钥泄露后,录下的旧流量也能解开。取消配对即可让它失效。
56
+ - 设备密钥是长期的,没有前向保密:密钥泄露后,录下的旧流量也能解开。取消配对即可让它失效。
57
+
58
+ ## 分享内容(对话、网页、文件)
59
+
60
+ `RelayContentUtil`(common)与 `RelayHost` 的 `content` 模式:链接 `https://<relay>/s#v=1&h=<主机>&s=<分享>&k=<密钥>`(`p=1` 要口令,`m=self` 仅自己、没有密钥,`o=1` 中继上有副本)。查看方用分享 id 当设备 id 握手,但**由查看方先证明**有密钥(主机的 `ready` 不带 `proof`),否则拿到链接的人能离线猜口令;主机按分享计数,10 分钟内错 10 次暂停 10 分钟,口令错时先发明文 `{t:'refused', reason:'key'}`。内容记在 `relay.json` 的 `contents`,字节在 agent 目录的 `shares/<id>`。
61
+
62
+ "电脑关机也能打开":主机把内容用派生密钥加封后 `PUT /v1/blob/<分享 id>`(`RelayBlobs`,Worker 里是每个分享一个 `RelayBlobObject`,到期由 alarm 删除;`ta relay` 存在内存里)。读要带 `x-ta-auth`(由内容密钥派生,中继只存它的 SHA-256),错 20 次返回 429。
63
+
64
+ `/s` 是浏览器里的查看页(`src/viewer/`,`scripts/build-viewer.ts` 打成 `viewer/page.html`,脚本按哈希放行),在页面里解密后显示;分享的 HTML 放进 `/s/frame` 的沙箱 iframe(无 `allow-same-origin`,禁止 `connect-src`)。设计记录见 [docs/design/ta-link.md](../../docs/design/ta-link.md)。
65
+
66
+ ## 房间(`/v1/hub/<房间>`)
67
+
68
+ 几台 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 不采用访客写的其他状态和发的消息。
@@ -1,9 +1,18 @@
1
1
  export * from './qr-code.js';
2
+ export * from './relay-blobs.js';
2
3
  export * from './relay-bytes.js';
4
+ export * from './relay-card.js';
3
5
  export * from './relay-cipher.js';
6
+ export * from './relay-content.js';
7
+ export * from './relay-conversation.js';
4
8
  export * from './relay-files.js';
9
+ export * from './relay-hub.js';
10
+ export * from './relay-hub-util.js';
11
+ export * from './relay-hubs.js';
12
+ export * from './relay-identity.js';
5
13
  export * from './relay-pairing.js';
6
14
  export * from './relay-protocol.js';
7
15
  export * from './relay-room.js';
16
+ export * from './relay-room-chat.js';
8
17
  export * from './relay-share.js';
9
18
  export * from './relay-socket.js';
@@ -1,10 +1,19 @@
1
1
  export * from './qr-code.js';
2
+ export * from './relay-blobs.js';
2
3
  export * from './relay-bytes.js';
4
+ export * from './relay-card.js';
3
5
  export * from './relay-cipher.js';
6
+ export * from './relay-content.js';
7
+ export * from './relay-conversation.js';
4
8
  export * from './relay-files.js';
9
+ export * from './relay-hub.js';
10
+ export * from './relay-hub-util.js';
11
+ export * from './relay-hubs.js';
12
+ export * from './relay-identity.js';
5
13
  export * from './relay-pairing.js';
6
14
  export * from './relay-protocol.js';
7
15
  export * from './relay-room.js';
16
+ export * from './relay-room-chat.js';
8
17
  export * from './relay-share.js';
9
18
  export * from './relay-socket.js';
10
19
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,54 @@
1
+ /** Where a copy is kept: a Durable Object's storage, or a map in `ta relay`. Values are structured-cloned. */
2
+ export interface RelayBlobStorage {
3
+ get<T>(key: string): Promise<T | undefined>;
4
+ put(key: string, value: unknown): Promise<void>;
5
+ deleteAll(): Promise<void>;
6
+ /** When the copy runs out: the storage deletes it then (an alarm), or the relay finds it so on the next request. */
7
+ setAlarm?(at: number): Promise<void>;
8
+ }
9
+ interface BlobMeta {
10
+ /** SHA-256 of the token of the host that wrote it first, who alone writes or deletes it. */
11
+ writeHash: string;
12
+ /** SHA-256 of what reads it (RelayContentUtil.authToken). */
13
+ authHash: string;
14
+ expiresAt: number;
15
+ size: number;
16
+ chunks: number;
17
+ }
18
+ /**
19
+ * One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
20
+ * deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
21
+ * nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
22
+ */
23
+ export declare class RelayBlobs {
24
+ protected readonly storage: RelayBlobStorage;
25
+ protected readonly now: () => number;
26
+ /** The bytes of a copy at most, sealed. */
27
+ static readonly MAX_BYTES: number;
28
+ constructor(storage: RelayBlobStorage, now?: () => number);
29
+ handle(request: Request): Promise<Response>;
30
+ protected read(request: Request): Promise<Response>;
31
+ protected write(request: Request): Promise<Response>;
32
+ protected remove(request: Request): Promise<Response>;
33
+ /** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
34
+ protected meta(): Promise<BlobMeta | undefined>;
35
+ protected static bearer(request: Request): string;
36
+ protected static hash(text: string): Promise<string>;
37
+ protected static answer(status: number, text: string, headers?: Record<string, string>): Response;
38
+ }
39
+ /** What the host that shared content does with its copy on the relay. */
40
+ export declare namespace RelayBlobClient {
41
+ interface Copy {
42
+ relay: string;
43
+ share: string;
44
+ /** The host's token of the copy. */
45
+ token: string;
46
+ /** SHA-256 of the token that reads it. */
47
+ authHash: string;
48
+ expiresAt: number;
49
+ bytes: Uint8Array;
50
+ }
51
+ function put(copy: Copy): Promise<void>;
52
+ function remove(relay: string, share: string, token: string): Promise<void>;
53
+ }
54
+ export {};
@@ -0,0 +1,153 @@
1
+ import { RelayBytes } from './relay-bytes.js';
2
+ import { RELAY_BLOB_MAX_DAYS, RELAY_BLOB_PATH, RELAY_CONTENT_MAX_BYTES } from './relay-protocol.js';
3
+ /** Below the 2 MiB of a value of a Durable Object. */
4
+ const CHUNK_BYTES = 1024 * 1024;
5
+ /** The sealing adds an IV, a tag and the header of the content. */
6
+ const MAX_BYTES = RELAY_CONTENT_MAX_BYTES + 64 * 1024;
7
+ const FAIL_WINDOW_MS = 10 * 60 * 1000;
8
+ const MAX_FAILS = 20;
9
+ const DAY_MS = 24 * 60 * 60 * 1000;
10
+ const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'x-ta-auth', 'access-control-allow-methods': 'GET, OPTIONS' };
11
+ /**
12
+ * One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
13
+ * deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
14
+ * nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
15
+ */
16
+ export class RelayBlobs {
17
+ constructor(storage, now = Date.now) {
18
+ this.storage = storage;
19
+ this.now = now;
20
+ }
21
+ async handle(request) {
22
+ switch (request.method) {
23
+ case 'OPTIONS':
24
+ return new Response(null, { status: 204, headers: READ_HEADERS });
25
+ case 'GET':
26
+ return this.read(request);
27
+ case 'PUT':
28
+ return this.write(request);
29
+ case 'DELETE':
30
+ return this.remove(request);
31
+ default:
32
+ return RelayBlobs.answer(405, 'Not allowed.');
33
+ }
34
+ }
35
+ async read(request) {
36
+ var _a, _b;
37
+ const meta = await this.meta();
38
+ if (!meta) {
39
+ return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
40
+ }
41
+ const fails = (_a = (await this.storage.get('fails'))) !== null && _a !== void 0 ? _a : { count: 0, since: 0 };
42
+ const now = this.now();
43
+ const recent = now - fails.since < FAIL_WINDOW_MS ? fails : { count: 0, since: now };
44
+ if (recent.count >= MAX_FAILS) {
45
+ return RelayBlobs.answer(429, 'Too many tries.', READ_HEADERS);
46
+ }
47
+ if ((await RelayBlobs.hash((_b = request.headers.get('x-ta-auth')) !== null && _b !== void 0 ? _b : '')) !== meta.authHash) {
48
+ await this.storage.put('fails', { count: recent.count + 1, since: recent.since });
49
+ return RelayBlobs.answer(403, 'Refused.', READ_HEADERS);
50
+ }
51
+ const parts = [];
52
+ for (let index = 0; index < meta.chunks; index++) {
53
+ const part = await this.storage.get(`c${index}`);
54
+ if (!part) {
55
+ return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
56
+ }
57
+ parts.push(new Uint8Array(part));
58
+ }
59
+ return new Response(RelayBytes.concat(...parts), {
60
+ headers: { ...READ_HEADERS, 'content-type': 'application/octet-stream', 'cache-control': 'no-store' },
61
+ });
62
+ }
63
+ async write(request) {
64
+ var _a, _b;
65
+ var _c;
66
+ const writeHash = await RelayBlobs.hash(RelayBlobs.bearer(request));
67
+ const before = await this.storage.get('meta');
68
+ if (before && before.writeHash !== writeHash && before.expiresAt > this.now()) {
69
+ return RelayBlobs.answer(403, 'Another host keeps this copy.');
70
+ }
71
+ const authHash = (_c = request.headers.get('x-ta-auth-hash')) !== null && _c !== void 0 ? _c : '';
72
+ const expiresAt = Number(request.headers.get('x-ta-expires'));
73
+ const now = this.now();
74
+ if (RelayBlobs.bearer(request).length < 32 ||
75
+ !/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
76
+ !(expiresAt > now) ||
77
+ expiresAt > now + RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
78
+ return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
79
+ }
80
+ const length = Number(request.headers.get('content-length'));
81
+ if (length > MAX_BYTES) {
82
+ return RelayBlobs.answer(413, 'Too large.');
83
+ }
84
+ const bytes = new Uint8Array(await request.arrayBuffer());
85
+ if (bytes.length === 0 || bytes.length > MAX_BYTES) {
86
+ return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
87
+ }
88
+ await this.storage.deleteAll();
89
+ const chunks = Math.ceil(bytes.length / CHUNK_BYTES);
90
+ for (let index = 0; index < chunks; index++) {
91
+ await this.storage.put(`c${index}`, bytes.slice(index * CHUNK_BYTES, (index + 1) * CHUNK_BYTES));
92
+ }
93
+ await this.storage.put('meta', { writeHash, authHash, expiresAt, size: bytes.length, chunks });
94
+ await ((_b = (_a = this.storage).setAlarm) === null || _b === void 0 ? void 0 : _b.call(_a, expiresAt));
95
+ return RelayBlobs.answer(201, 'Kept.');
96
+ }
97
+ async remove(request) {
98
+ const meta = await this.storage.get('meta');
99
+ if (meta && meta.writeHash !== (await RelayBlobs.hash(RelayBlobs.bearer(request)))) {
100
+ return RelayBlobs.answer(403, 'Refused.');
101
+ }
102
+ await this.storage.deleteAll();
103
+ return RelayBlobs.answer(200, 'Deleted.');
104
+ }
105
+ /** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
106
+ async meta() {
107
+ const meta = await this.storage.get('meta');
108
+ if (meta && meta.expiresAt <= this.now()) {
109
+ await this.storage.deleteAll();
110
+ return undefined;
111
+ }
112
+ return meta;
113
+ }
114
+ static bearer(request) {
115
+ var _a;
116
+ var _b, _c;
117
+ return (_c = (_a = /^Bearer (\S+)$/.exec((_b = request.headers.get('authorization')) !== null && _b !== void 0 ? _b : '')) === null || _a === void 0 ? void 0 : _a[1]) !== null && _c !== void 0 ? _c : '';
118
+ }
119
+ static async hash(text) {
120
+ const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
121
+ return RelayBytes.toBase64(new Uint8Array(digest));
122
+ }
123
+ static answer(status, text, headers = {}) {
124
+ return new Response(`${text}\n`, { status, headers: { ...headers, 'content-type': 'text/plain; charset=utf-8' } });
125
+ }
126
+ }
127
+ /** The bytes of a copy at most, sealed. */
128
+ RelayBlobs.MAX_BYTES = MAX_BYTES;
129
+ /** What the host that shared content does with its copy on the relay. */
130
+ export var RelayBlobClient;
131
+ (function (RelayBlobClient) {
132
+ async function put(copy) {
133
+ const response = await fetch(`${copy.relay}${RELAY_BLOB_PATH}${copy.share}`, {
134
+ method: 'PUT',
135
+ headers: {
136
+ authorization: `Bearer ${copy.token}`,
137
+ 'x-ta-auth-hash': copy.authHash,
138
+ 'x-ta-expires': String(copy.expiresAt),
139
+ 'content-type': 'application/octet-stream',
140
+ },
141
+ body: copy.bytes,
142
+ });
143
+ if (!response.ok) {
144
+ throw new Error(`The relay did not keep the copy: ${response.status} ${(await response.text()).trim()}`);
145
+ }
146
+ }
147
+ RelayBlobClient.put = put;
148
+ async function remove(relay, share, token) {
149
+ await fetch(`${relay}${RELAY_BLOB_PATH}${share}`, { method: 'DELETE', headers: { authorization: `Bearer ${token}` } });
150
+ }
151
+ RelayBlobClient.remove = remove;
152
+ })(RelayBlobClient || (RelayBlobClient = {}));
153
+ //# sourceMappingURL=relay-blobs.js.map
@@ -0,0 +1,23 @@
1
+ /** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
2
+ export declare const RELAY_AGENT_KIND = "ta-agent";
3
+ /**
4
+ * An agent of Ta as its card shares it: who it is and its role, what its secrets taken out (Redactor), not where it
5
+ * works nor what it said. The Ta that adds it fits the role to the project it is added for.
6
+ */
7
+ export interface RelayAgentCard {
8
+ v: 1;
9
+ name: string;
10
+ role: string;
11
+ /** One or two characters on its avatar. */
12
+ mark: string;
13
+ /** Which of the faces of ta-channels it has. */
14
+ face?: number;
15
+ /** The colour of its face, for a page that does not draw the faces (the viewer of the relay). */
16
+ color?: string;
17
+ /** How many places of the role were hidden as secrets. */
18
+ redacted?: number;
19
+ }
20
+ export declare namespace RelayAgentCards {
21
+ /** A card read from what a link shares; undefined for what is none. */
22
+ function parse(data: Uint8Array | string): RelayAgentCard | undefined;
23
+ }
@@ -0,0 +1,30 @@
1
+ /** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
2
+ export const RELAY_AGENT_KIND = 'ta-agent';
3
+ export var RelayAgentCards;
4
+ (function (RelayAgentCards) {
5
+ const MAX_ROLE = 8000;
6
+ /** A card read from what a link shares; undefined for what is none. */
7
+ function parse(data) {
8
+ try {
9
+ const card = JSON.parse(typeof data === 'string' ? data : new TextDecoder().decode(data));
10
+ if ((card === null || card === void 0 ? void 0 : card.v) !== 1 || typeof card.name !== 'string' || !card.name.trim() || typeof card.role !== 'string' || !card.role.trim()) {
11
+ return undefined;
12
+ }
13
+ const color = typeof card.color === 'string' && /^#[0-9a-fA-F]{6}$/.test(card.color) ? card.color : undefined;
14
+ return {
15
+ v: 1,
16
+ name: card.name.trim().slice(0, 32),
17
+ role: card.role.trim().slice(0, MAX_ROLE),
18
+ mark: typeof card.mark === 'string' && card.mark.trim() ? card.mark.trim().slice(0, 2) : card.name.trim().slice(0, 1).toUpperCase(),
19
+ ...(Number.isInteger(card.face) ? { face: card.face } : {}),
20
+ ...(color ? { color } : {}),
21
+ ...(typeof card.redacted === 'number' && card.redacted > 0 ? { redacted: card.redacted } : {}),
22
+ };
23
+ }
24
+ catch (_a) {
25
+ return undefined;
26
+ }
27
+ }
28
+ RelayAgentCards.parse = parse;
29
+ })(RelayAgentCards || (RelayAgentCards = {}));
30
+ //# sourceMappingURL=relay-card.js.map
@@ -0,0 +1,104 @@
1
+ import { type RelayIdentity } from './relay-identity.js';
2
+ /**
3
+ * Who may open a link of content: anyone with it, anyone with it and its password, the one who shared it, or Ta friends
4
+ * the one who shared it chose (`people`), in their own Ta (docs/design/ta-link.md 3.6).
5
+ */
6
+ export type RelayContentAccess = 'link' | 'password' | 'self' | 'people';
7
+ /** What a link of content shared names. The key is in the fragment, which a browser does not send. */
8
+ export interface RelayContentLink {
9
+ relay: string;
10
+ host: string;
11
+ /** The id of the share, as a device's on the relay. */
12
+ share: string;
13
+ /** base64url, 32 bytes; none for `self` and `people`. */
14
+ key?: string;
15
+ access: RelayContentAccess;
16
+ /** For `people`: the identity of the one who shared it, whose key the key of a friend is agreed with. */
17
+ owner?: string;
18
+ /** A copy is kept on the relay, sealed: it opens while the computer that shared it is off. */
19
+ offline?: boolean;
20
+ /**
21
+ * What it shares, in the link (`c`) for kinds other than a conversation or a file (an agent's card), so that the
22
+ * package that opens it claims the link before it is fetched. Not secret.
23
+ */
24
+ kind?: string;
25
+ }
26
+ /** What is shared: a conversation (`ta-conversation`, JSON), a file or a page (its bytes). */
27
+ export interface RelayContentHeader {
28
+ kind: string;
29
+ title: string;
30
+ createdAt: number;
31
+ /** The media type of a file. */
32
+ type?: string;
33
+ }
34
+ export interface RelayContentItem extends RelayContentHeader {
35
+ data: Uint8Array;
36
+ }
37
+ export type RelayContentFailure =
38
+ /** A password is needed, or the one given is wrong (`wrong`). */
39
+ 'password' | 'wrong'
40
+ /** Too many wrong passwords of late: try again later. */
41
+ | 'busy'
42
+ /** Stopped, or past its time. */
43
+ | 'gone'
44
+ /** The computer that shared it does not run Ta now, and there is no copy on the relay. */
45
+ | 'offline'
46
+ /** Only the one who shared it opens it, in their own Ta. */
47
+ | 'self'
48
+ /** Only the Ta friends it was shared with open it, in their own Ta. */
49
+ | 'people' | 'invalid' | 'timeout' | 'large';
50
+ export declare class RelayContentError extends Error {
51
+ readonly reason: RelayContentFailure;
52
+ constructor(reason: RelayContentFailure, message: string);
53
+ }
54
+ export interface RelayContentFetchOptions {
55
+ password?: string;
56
+ /** For `people`: this Ta, and the key the one who shared it agrees keys with (of its friends). */
57
+ people?: {
58
+ identity: RelayIdentity;
59
+ ownerBox: string;
60
+ };
61
+ maxBytes?: number;
62
+ /** For all of it. Default 60 seconds. */
63
+ timeoutMs?: number;
64
+ }
65
+ /**
66
+ * Content shared by a link (docs/design/ta-link.md): its link, its keys, and how it is fetched, from the
67
+ * computer that shared it through the relay, or from the copy the relay keeps sealed. Runs the same in a
68
+ * browser (the viewer of the relay), in ta serve and in a Worker: WebCrypto, fetch and WebSocket only.
69
+ */
70
+ export declare namespace RelayContentUtil {
71
+ /**
72
+ * `https://<relay>/s#v=1&h=<host>&s=<share>&k=<key>`, with `p=1` for a password, `m=self`, `o=1` for a copy on the relay,
73
+ * `c=<kind>` for what is no conversation or file.
74
+ */
75
+ function link(content: RelayContentLink): string;
76
+ function is(text: string): boolean;
77
+ /** Throws on what is not a link of {@link link}. */
78
+ function parse(text: string): RelayContentLink;
79
+ /**
80
+ * The key the content is sealed with: the key of the link, or with a password, one derived from both
81
+ * (PBKDF2 of the password, then HKDF with the key of the link), so that neither alone opens it.
82
+ */
83
+ function contentKey(linkKey: Uint8Array, share: string, password?: string): Promise<Uint8Array>;
84
+ /**
85
+ * The key of content shared with a Ta friend (`people`): only the two make it, each with its own private key and the
86
+ * other's public one, for this share alone.
87
+ */
88
+ function peopleKey(identity: RelayIdentity, otherBox: string, share: string): Promise<Uint8Array>;
89
+ /** What reads the copy on the relay, which keeps only its SHA-256 (`x-ta-auth`). */
90
+ function authToken(key: Uint8Array): Promise<string>;
91
+ function sha256(text: string): Promise<string>;
92
+ /** The bytes of the copy on the relay: a random IV, then AES-256-GCM of {@link encode}. */
93
+ function seal(key: Uint8Array, item: RelayContentItem): Promise<Uint8Array>;
94
+ /** Throws when it was not sealed with this key. */
95
+ function open(key: Uint8Array, bytes: Uint8Array): Promise<RelayContentItem>;
96
+ /** The header as JSON behind its length (4 bytes), then the data. */
97
+ function encode(item: RelayContentItem): Uint8Array;
98
+ function decode(bytes: Uint8Array): RelayContentItem;
99
+ /**
100
+ * What a link shares: from the copy on the relay when it has one, else from the computer that shared it,
101
+ * end to end through the relay. Rejects with a RelayContentError that says why it cannot.
102
+ */
103
+ function fetch(text: string | RelayContentLink, options?: RelayContentFetchOptions): Promise<RelayContentItem>;
104
+ }