@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
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayAgentCards = exports.RELAY_AGENT_KIND = void 0;
4
+ /** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
5
+ exports.RELAY_AGENT_KIND = 'ta-agent';
6
+ var RelayAgentCards;
7
+ (function (RelayAgentCards) {
8
+ const MAX_ROLE = 8000;
9
+ /** A card read from what a link shares; undefined for what is none. */
10
+ function parse(data) {
11
+ try {
12
+ const card = JSON.parse(typeof data === 'string' ? data : new TextDecoder().decode(data));
13
+ 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()) {
14
+ return undefined;
15
+ }
16
+ const color = typeof card.color === 'string' && /^#[0-9a-fA-F]{6}$/.test(card.color) ? card.color : undefined;
17
+ return {
18
+ v: 1,
19
+ name: card.name.trim().slice(0, 32),
20
+ role: card.role.trim().slice(0, MAX_ROLE),
21
+ mark: typeof card.mark === 'string' && card.mark.trim() ? card.mark.trim().slice(0, 2) : card.name.trim().slice(0, 1).toUpperCase(),
22
+ ...(Number.isInteger(card.face) ? { face: card.face } : {}),
23
+ ...(color ? { color } : {}),
24
+ ...(typeof card.redacted === 'number' && card.redacted > 0 ? { redacted: card.redacted } : {}),
25
+ };
26
+ }
27
+ catch (_a) {
28
+ return undefined;
29
+ }
30
+ }
31
+ RelayAgentCards.parse = parse;
32
+ })(RelayAgentCards || (exports.RelayAgentCards = RelayAgentCards = {}));
33
+ //# sourceMappingURL=relay-card.js.map
@@ -0,0 +1,105 @@
1
+ import { type RelayIdentity } from './relay-identity';
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
+ }
105
+ //# sourceMappingURL=relay-content.d.ts.map
@@ -0,0 +1,320 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayContentUtil = exports.RelayContentError = void 0;
4
+ const relay_bytes_1 = require("./relay-bytes");
5
+ const relay_identity_1 = require("./relay-identity");
6
+ const relay_protocol_1 = require("./relay-protocol");
7
+ const relay_socket_1 = require("./relay-socket");
8
+ const encoder = new TextEncoder();
9
+ const decoder = new TextDecoder();
10
+ class RelayContentError extends Error {
11
+ constructor(reason, message) {
12
+ super(message);
13
+ this.reason = reason;
14
+ this.name = 'RelayContentError';
15
+ }
16
+ }
17
+ exports.RelayContentError = RelayContentError;
18
+ const PBKDF2_ITERATIONS = 600000;
19
+ /** Kinds the links of before carry no `c` for. */
20
+ const PLAIN_KINDS = new Set(['ta-conversation', 'file']);
21
+ const KIND = /^[a-z0-9][a-z0-9-]{0,31}$/;
22
+ /**
23
+ * Content shared by a link (docs/design/ta-link.md): its link, its keys, and how it is fetched, from the
24
+ * computer that shared it through the relay, or from the copy the relay keeps sealed. Runs the same in a
25
+ * browser (the viewer of the relay), in ta serve and in a Worker: WebCrypto, fetch and WebSocket only.
26
+ */
27
+ var RelayContentUtil;
28
+ (function (RelayContentUtil) {
29
+ /**
30
+ * `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,
31
+ * `c=<kind>` for what is no conversation or file.
32
+ */
33
+ function link(content) {
34
+ var _a;
35
+ const fragment = new URLSearchParams({ v: '1', h: content.host, s: content.share });
36
+ if (content.access === 'self') {
37
+ fragment.set('m', 'self');
38
+ }
39
+ else if (content.access === 'people') {
40
+ fragment.set('m', 'people');
41
+ fragment.set('o', (_a = content.owner) !== null && _a !== void 0 ? _a : '');
42
+ }
43
+ else if (content.key) {
44
+ fragment.set('k', content.key);
45
+ }
46
+ if (content.access === 'password') {
47
+ fragment.set('p', '1');
48
+ }
49
+ if (content.offline && content.access !== 'self' && content.access !== 'people') {
50
+ fragment.set('o', '1');
51
+ }
52
+ if (content.kind && !PLAIN_KINDS.has(content.kind) && KIND.test(content.kind)) {
53
+ fragment.set('c', content.kind);
54
+ }
55
+ return `${content.relay}${relay_protocol_1.RELAY_CONTENT_PATH}#${fragment}`;
56
+ }
57
+ RelayContentUtil.link = link;
58
+ function is(text) {
59
+ try {
60
+ parse(text);
61
+ return true;
62
+ }
63
+ catch (_a) {
64
+ return false;
65
+ }
66
+ }
67
+ RelayContentUtil.is = is;
68
+ /** Throws on what is not a link of {@link link}. */
69
+ function parse(text) {
70
+ var _a, _b;
71
+ const url = new URL(text.trim());
72
+ const fragment = new URLSearchParams(url.hash.slice(1));
73
+ const [host, share, key] = ['h', 's', 'k'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
74
+ const self = fragment.get('m') === 'self';
75
+ const people = fragment.get('m') === 'people';
76
+ const owner = (_a = fragment.get('o')) !== null && _a !== void 0 ? _a : '';
77
+ if (!/^https?:$/.test(url.protocol) ||
78
+ url.pathname !== relay_protocol_1.RELAY_CONTENT_PATH ||
79
+ fragment.get('v') !== '1' ||
80
+ !relay_protocol_1.RELAY_ID.test(host) ||
81
+ !relay_protocol_1.RELAY_ID.test(share) ||
82
+ (people ? !/^[A-Za-z0-9_-]{22}$/.test(owner) : !self && !/^[A-Za-z0-9_-]{43}$/.test(key))) {
83
+ throw new RelayContentError('invalid', 'Not a link of something shared from Ta.');
84
+ }
85
+ const kind = (_b = fragment.get('c')) !== null && _b !== void 0 ? _b : '';
86
+ if (people) {
87
+ return { relay: url.origin, host, share, access: 'people', owner, offline: false, ...(KIND.test(kind) ? { kind } : {}) };
88
+ }
89
+ return {
90
+ relay: url.origin,
91
+ host,
92
+ share,
93
+ key: self ? undefined : key,
94
+ access: self ? 'self' : fragment.get('p') === '1' ? 'password' : 'link',
95
+ offline: !self && fragment.get('o') === '1',
96
+ ...(KIND.test(kind) ? { kind } : {}),
97
+ };
98
+ }
99
+ RelayContentUtil.parse = parse;
100
+ /**
101
+ * The key the content is sealed with: the key of the link, or with a password, one derived from both
102
+ * (PBKDF2 of the password, then HKDF with the key of the link), so that neither alone opens it.
103
+ */
104
+ async function contentKey(linkKey, share, password) {
105
+ if (password === undefined) {
106
+ return linkKey;
107
+ }
108
+ const subtle = globalThis.crypto.subtle;
109
+ const secret = await subtle.importKey('raw', encoder.encode(password.normalize('NFC')), 'PBKDF2', false, ['deriveBits']);
110
+ const stretched = await subtle.deriveBits({ name: 'PBKDF2', hash: 'SHA-256', salt: encoder.encode(`ta-share:${share}`), iterations: PBKDF2_ITERATIONS }, secret, 256);
111
+ return hkdf(linkKey, new Uint8Array(stretched), 'ta-share/1 password');
112
+ }
113
+ RelayContentUtil.contentKey = contentKey;
114
+ /**
115
+ * The key of content shared with a Ta friend (`people`): only the two make it, each with its own private key and the
116
+ * other's public one, for this share alone.
117
+ */
118
+ function peopleKey(identity, otherBox, share) {
119
+ return relay_identity_1.RelayIdentityUtil.agree(identity, otherBox, share, 'ta-share/1 people');
120
+ }
121
+ RelayContentUtil.peopleKey = peopleKey;
122
+ /** What reads the copy on the relay, which keeps only its SHA-256 (`x-ta-auth`). */
123
+ async function authToken(key) {
124
+ return relay_bytes_1.RelayBytes.toBase64(await hkdf(key, new Uint8Array(0), 'ta-share/1 auth'));
125
+ }
126
+ RelayContentUtil.authToken = authToken;
127
+ async function sha256(text) {
128
+ return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', encoder.encode(text))));
129
+ }
130
+ RelayContentUtil.sha256 = sha256;
131
+ /** The bytes of the copy on the relay: a random IV, then AES-256-GCM of {@link encode}. */
132
+ async function seal(key, item) {
133
+ const iv = relay_bytes_1.RelayBytes.random(12);
134
+ const sealed = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, await blobKey(key), encode(item));
135
+ return relay_bytes_1.RelayBytes.concat(iv, new Uint8Array(sealed));
136
+ }
137
+ RelayContentUtil.seal = seal;
138
+ /** Throws when it was not sealed with this key. */
139
+ async function open(key, bytes) {
140
+ const opened = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: bytes.subarray(0, 12) }, await blobKey(key), bytes.subarray(12));
141
+ return decode(new Uint8Array(opened));
142
+ }
143
+ RelayContentUtil.open = open;
144
+ /** The header as JSON behind its length (4 bytes), then the data. */
145
+ function encode(item) {
146
+ const { data, ...header } = item;
147
+ const head = encoder.encode(JSON.stringify(header));
148
+ const length = new Uint8Array(4);
149
+ new DataView(length.buffer).setUint32(0, head.length);
150
+ return relay_bytes_1.RelayBytes.concat(length, head, data);
151
+ }
152
+ RelayContentUtil.encode = encode;
153
+ function decode(bytes) {
154
+ const length = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0);
155
+ const header = JSON.parse(decoder.decode(bytes.subarray(4, 4 + length)));
156
+ return {
157
+ kind: String(header.kind),
158
+ title: String(header.title),
159
+ createdAt: Number(header.createdAt),
160
+ type: header.type,
161
+ data: bytes.slice(4 + length),
162
+ };
163
+ }
164
+ RelayContentUtil.decode = decode;
165
+ /**
166
+ * What a link shares: from the copy on the relay when it has one, else from the computer that shared it,
167
+ * end to end through the relay. Rejects with a RelayContentError that says why it cannot.
168
+ */
169
+ async function fetch(text, options = {}) {
170
+ const link = typeof text === 'string' ? parse(text) : text;
171
+ if (link.access === 'people') {
172
+ if (!options.people) {
173
+ throw new RelayContentError('people', 'Only the Ta friends it was shared with open it, in their own Ta.');
174
+ }
175
+ return fromHost(link, await peopleKey(options.people.identity, options.people.ownerBox, link.share), options);
176
+ }
177
+ if (link.access === 'self' || !link.key) {
178
+ throw new RelayContentError('self', 'Only the one who shared it opens it, in their own Ta.');
179
+ }
180
+ if (link.access === 'password' && !options.password) {
181
+ throw new RelayContentError('password', 'It is shared with a password.');
182
+ }
183
+ const key = await contentKey(relay_bytes_1.RelayBytes.fromBase64(link.key), link.share, link.access === 'password' ? options.password : undefined);
184
+ if (link.offline) {
185
+ const copy = await fromRelay(link, key);
186
+ if (copy) {
187
+ return copy;
188
+ }
189
+ }
190
+ return fromHost(link, key, options);
191
+ }
192
+ RelayContentUtil.fetch = fetch;
193
+ /** The copy on the relay; undefined when there is none (any more), to ask the computer instead. */
194
+ async function fromRelay(link, key) {
195
+ let response;
196
+ try {
197
+ response = await globalThis.fetch(`${link.relay}${relay_protocol_1.RELAY_BLOB_PATH}${link.share}`, { headers: { 'x-ta-auth': await authToken(key) } });
198
+ }
199
+ catch (_a) {
200
+ return undefined;
201
+ }
202
+ if (response.status === 403) {
203
+ throw link.access === 'password'
204
+ ? new RelayContentError('wrong', 'The password is wrong.')
205
+ : new RelayContentError('gone', 'This link no longer works.');
206
+ }
207
+ if (response.status === 429) {
208
+ throw new RelayContentError('busy', 'Too many wrong passwords: try again in a few minutes.');
209
+ }
210
+ if (!response.ok) {
211
+ return undefined;
212
+ }
213
+ try {
214
+ return await open(key, new Uint8Array(await response.arrayBuffer()));
215
+ }
216
+ catch (_b) {
217
+ throw new RelayContentError('wrong', 'The password is wrong.');
218
+ }
219
+ }
220
+ function fromHost(link, key, options) {
221
+ var _a;
222
+ const max = (_a = options.maxBytes) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_CONTENT_MAX_BYTES;
223
+ const pairing = { relay: link.relay, host: link.host, device: link.share, key: relay_bytes_1.RelayBytes.toBase64(key) };
224
+ return new Promise((resolve, reject) => {
225
+ var _a;
226
+ const socket = new relay_socket_1.RelaySocket(pairing, { proveFirst: true });
227
+ const chunks = [];
228
+ let received = 0;
229
+ let header;
230
+ let done = false;
231
+ const finish = (error, item) => {
232
+ if (done) {
233
+ return;
234
+ }
235
+ done = true;
236
+ clearTimeout(timer);
237
+ socket.onclose = null;
238
+ socket.close();
239
+ if (item) {
240
+ resolve(item);
241
+ }
242
+ else {
243
+ reject(error);
244
+ }
245
+ };
246
+ const timer = setTimeout(() => finish(new RelayContentError('timeout', 'The computer that shared it took too long to send it.')), (_a = options.timeoutMs) !== null && _a !== void 0 ? _a : 60000);
247
+ socket.onmessage = ({ data }) => {
248
+ let message;
249
+ try {
250
+ message = JSON.parse(data);
251
+ }
252
+ catch (_a) {
253
+ finish(new RelayContentError('invalid', 'The computer that shared it sent what is not content.'));
254
+ return;
255
+ }
256
+ if (message.t === 'error') {
257
+ finish(new RelayContentError('gone', message.message));
258
+ }
259
+ else if (message.t === 'content') {
260
+ if (!(message.size >= 0) || message.size > max) {
261
+ finish(new RelayContentError('large', `It is larger than ${Math.round(max / 1024 / 1024)} MB.`));
262
+ return;
263
+ }
264
+ header = {
265
+ kind: String(message.kind),
266
+ title: String(message.title),
267
+ createdAt: Number(message.createdAt),
268
+ type: message.type,
269
+ size: message.size,
270
+ };
271
+ }
272
+ else if (message.t === 'chunk' && header) {
273
+ const bytes = relay_bytes_1.RelayBytes.fromBase64(message.d);
274
+ received += bytes.length;
275
+ if (received > header.size) {
276
+ finish(new RelayContentError('invalid', 'It is larger than it said.'));
277
+ return;
278
+ }
279
+ chunks.push(bytes);
280
+ }
281
+ else if (message.t === 'end' && header) {
282
+ if (received !== header.size) {
283
+ finish(new RelayContentError('invalid', 'It came short.'));
284
+ return;
285
+ }
286
+ const { size: _size, ...rest } = header;
287
+ finish(undefined, { ...rest, data: relay_bytes_1.RelayBytes.concat(...chunks) });
288
+ }
289
+ };
290
+ socket.onclose = ({ code, reason }) => finish(closed(code, reason, link));
291
+ });
292
+ }
293
+ function closed(code, reason, link) {
294
+ if (code === relay_socket_1.RelaySocket.REFUSED) {
295
+ if (reason === 'key') {
296
+ return link.access === 'password'
297
+ ? new RelayContentError('wrong', 'The password is wrong.')
298
+ : new RelayContentError('gone', 'This link no longer works.');
299
+ }
300
+ if (reason === 'busy') {
301
+ return new RelayContentError('busy', 'Too many wrong passwords: try again in a few minutes.');
302
+ }
303
+ return new RelayContentError('gone', 'This link no longer works: the share was stopped or ran out.');
304
+ }
305
+ return code === relay_protocol_1.RelayCloseCode.HOST_OFFLINE || code === relay_protocol_1.RelayCloseCode.TIMEOUT
306
+ ? new RelayContentError('offline', 'The computer that shared it is not running Ta now. Try again later.')
307
+ : new RelayContentError('offline', 'The computer that shared it cannot be reached now. Try again later.');
308
+ }
309
+ async function blobKey(key) {
310
+ const bytes = await hkdf(key, new Uint8Array(0), 'ta-share/1 blob');
311
+ return globalThis.crypto.subtle.importKey('raw', bytes, 'AES-GCM', false, ['encrypt', 'decrypt']);
312
+ }
313
+ async function hkdf(key, salt, info) {
314
+ const subtle = globalThis.crypto.subtle;
315
+ const secret = await subtle.importKey('raw', key, 'HKDF', false, ['deriveBits']);
316
+ const bits = await subtle.deriveBits({ name: 'HKDF', hash: 'SHA-256', salt: salt, info: encoder.encode(info) }, secret, 256);
317
+ return new Uint8Array(bits);
318
+ }
319
+ })(RelayContentUtil || (exports.RelayContentUtil = RelayContentUtil = {}));
320
+ //# sourceMappingURL=relay-content.js.map
@@ -0,0 +1,60 @@
1
+ /**
2
+ * A conversation shared by a link (RelayContentHeader.kind `ta-conversation`): pi's messages of its branch, as
3
+ * the one who shared it chose to hand on (docs/design/ta-link.md 2.8). The viewer of the relay draws it; Ta
4
+ * draws it too, and imports it as a conversation of its own.
5
+ */
6
+ export declare const RELAY_CONVERSATION_KIND = "ta-conversation";
7
+ /** A file or a page: its bytes, with RelayContentHeader.type. */
8
+ export declare const RELAY_FILE_KIND = "file";
9
+ export type RelayConversationPart = {
10
+ type: 'text';
11
+ text: string;
12
+ } | {
13
+ type: 'thinking';
14
+ thinking: string;
15
+ } | {
16
+ type: 'image';
17
+ data?: string;
18
+ mimeType?: string;
19
+ } | {
20
+ type: 'toolCall';
21
+ id: string;
22
+ name: string;
23
+ arguments: Record<string, unknown>;
24
+ };
25
+ /** pi's AgentMessage, the parts a conversation shows; the rest of what pi keeps passes through untouched. */
26
+ export type RelayConversationMessage = {
27
+ role: 'user';
28
+ content: string | RelayConversationPart[];
29
+ timestamp?: number;
30
+ } | {
31
+ role: 'assistant';
32
+ content: RelayConversationPart[];
33
+ model?: string;
34
+ provider?: string;
35
+ stopReason?: string;
36
+ errorMessage?: string;
37
+ timestamp?: number;
38
+ } | {
39
+ role: 'toolResult';
40
+ toolCallId: string;
41
+ toolName: string;
42
+ content: RelayConversationPart[];
43
+ isError?: boolean;
44
+ timestamp?: number;
45
+ };
46
+ export interface RelayConversation {
47
+ v: 1;
48
+ title: string;
49
+ /** The name of its directory, for a line under the title. */
50
+ place?: string;
51
+ messages: RelayConversationMessage[];
52
+ /** What was left out when it was shared: the outputs of its tools, and how many images. */
53
+ omitted?: {
54
+ outputs?: boolean;
55
+ images?: number;
56
+ };
57
+ /** How many places looked like a secret, and were taken out. */
58
+ redacted?: number;
59
+ }
60
+ //# sourceMappingURL=relay-conversation.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RELAY_FILE_KIND = exports.RELAY_CONVERSATION_KIND = void 0;
4
+ /**
5
+ * A conversation shared by a link (RelayContentHeader.kind `ta-conversation`): pi's messages of its branch, as
6
+ * the one who shared it chose to hand on (docs/design/ta-link.md 2.8). The viewer of the relay draws it; Ta
7
+ * draws it too, and imports it as a conversation of its own.
8
+ */
9
+ exports.RELAY_CONVERSATION_KIND = 'ta-conversation';
10
+ /** A file or a page: its bytes, with RelayContentHeader.type. */
11
+ exports.RELAY_FILE_KIND = 'file';
12
+ //# sourceMappingURL=relay-conversation.js.map
@@ -0,0 +1,92 @@
1
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
2
+ /** What a link inviting into a room names (docs/design/ta-link.md 3.3); all of it in the fragment, which a browser does not send. */
3
+ export interface RelayHubLink {
4
+ relay: string;
5
+ room: string;
6
+ /** base64url, 32 bytes: what an ask proves and is sealed with. */
7
+ invite: string;
8
+ /** The identity id of the owner: the answer must be signed by it. */
9
+ owner: string;
10
+ title: string;
11
+ /** The owner's name, as they gave it. */
12
+ by: string;
13
+ }
14
+ /** One of a room, as its owner lists them in the roster. */
15
+ export interface RelayHubMember extends RelayPublicIdentity {
16
+ /** The id of the member in this room alone (not its identity). */
17
+ member: string;
18
+ owner?: boolean;
19
+ /** A guest in a browser (docs/design/ta-link.md 3.8): the others hear only what it says. */
20
+ guest?: boolean;
21
+ at: number;
22
+ }
23
+ export interface RelayHubRosterBody {
24
+ v: number;
25
+ members: RelayHubMember[];
26
+ }
27
+ /** What one asking to join says of itself, sealed with the key of the invitation and signed. */
28
+ export interface RelayHubAskBody extends RelayPublicIdentity {
29
+ member: string;
30
+ at: number;
31
+ /** Asks from a browser, without Ta: for the owner to know, and to admit as a guest. */
32
+ guest?: boolean;
33
+ }
34
+ /** What the owner gives one it admits, sealed for it alone: the key of the room, and what the room is. */
35
+ export interface RelayHubAnswerBody {
36
+ key: string;
37
+ kind: string;
38
+ title: string;
39
+ view?: string;
40
+ }
41
+ export interface RelayHubSigned<T> {
42
+ /** The member who signed it. */
43
+ m: string;
44
+ at: number;
45
+ b: T;
46
+ }
47
+ /** The keys of a room, made from its key `R` once. */
48
+ export interface RelayHubKeys {
49
+ seal: CryptoKey;
50
+ names: Uint8Array;
51
+ }
52
+ /** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
53
+ export declare namespace RelayHubUtil {
54
+ /** `https://<relay>/r#v=1&r=<room>&i=<invite>&o=<owner>&n=<title>&by=<name>`. */
55
+ function link(link: RelayHubLink): string;
56
+ function is(text: string): boolean;
57
+ /** Throws on what is not a link of {@link link}. */
58
+ function parse(text: string): RelayHubLink;
59
+ function randomId(): string;
60
+ function randomKey(): string;
61
+ function keys(roomKey: string): Promise<RelayHubKeys>;
62
+ /** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
63
+ function keyId(keys: RelayHubKeys, name: string): Promise<string>;
64
+ /** What proves an ask comes from one with the invitation; the relay keeps its {@link RelayKeys.hash}. */
65
+ function inviteProof(invite: string): Promise<string>;
66
+ function inviteKey(invite: string): Promise<CryptoKey>;
67
+ /** Signed by `identity` as `member`, then sealed with `key`. */
68
+ function seal<T>(key: CryptoKey, identity: RelayIdentity, member: string, body: T, at?: number): Promise<string>;
69
+ /**
70
+ * What {@link seal} sealed, once its signature checks with the signing key `signOf` gives for its member;
71
+ * undefined when it does not open, is not signed by that member, or is of another time than `now` (with `skewMs`).
72
+ */
73
+ function open<T>(key: CryptoKey, sealed: string, signOf: (member: string) => string | undefined, options?: {
74
+ now?: number;
75
+ skewMs?: number;
76
+ }): Promise<RelayHubSigned<T> | undefined>;
77
+ /** An ask: sealed with the key of the invitation, signed by the one who asks (its keys inside), whom the owner checks. */
78
+ function sealAsk(invite: string, identity: RelayIdentity, member: string, at?: number, guest?: boolean): Promise<string>;
79
+ /** Undefined for an ask that does not open with this invitation, or is not signed by the keys it gives. */
80
+ function openAsk(invite: string, sealed: string): Promise<RelayHubAskBody | undefined>;
81
+ /**
82
+ * The owner's answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by
83
+ * the owner; all of it sealed with the invitation the ask came with, so that the relay does not see who the owner is.
84
+ */
85
+ function sealAnswer(owner: RelayIdentity, room: string, ask: RelayHubAskBody, body: RelayHubAnswerBody, invite: string): Promise<string>;
86
+ /** Undefined unless it is signed by the owner the link names, and opens for this identity. */
87
+ function openAnswer(identity: RelayIdentity, room: string, member: string, ownerId: string, answer: string, invite: string): Promise<{
88
+ owner: RelayPublicIdentity;
89
+ body: RelayHubAnswerBody;
90
+ } | undefined>;
91
+ }
92
+ //# sourceMappingURL=relay-hub-util.d.ts.map