@celljs/ta-relay 4.9.1 → 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 (118) hide show
  1. package/README.en.md +29 -2
  2. package/README.md +29 -2
  3. package/esm/common/index.d.ts +10 -0
  4. package/esm/common/index.js +10 -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-cipher.d.ts +15 -0
  10. package/esm/common/relay-cipher.js +62 -2
  11. package/esm/common/relay-content.d.ts +104 -0
  12. package/esm/common/relay-content.js +316 -0
  13. package/esm/common/relay-conversation.d.ts +59 -0
  14. package/esm/common/relay-conversation.js +9 -0
  15. package/esm/common/relay-files.d.ts +108 -0
  16. package/esm/common/relay-files.js +170 -0
  17. package/esm/common/relay-hub-util.d.ts +91 -0
  18. package/esm/common/relay-hub-util.js +164 -0
  19. package/esm/common/relay-hub.d.ts +125 -0
  20. package/esm/common/relay-hub.js +438 -0
  21. package/esm/common/relay-hubs.d.ts +271 -0
  22. package/esm/common/relay-hubs.js +844 -0
  23. package/esm/common/relay-identity.d.ts +46 -0
  24. package/esm/common/relay-identity.js +126 -0
  25. package/esm/common/relay-protocol.d.ts +217 -2
  26. package/esm/common/relay-protocol.js +53 -0
  27. package/esm/common/relay-room-chat.d.ts +80 -0
  28. package/esm/common/relay-room-chat.js +152 -0
  29. package/esm/common/relay-socket.d.ts +8 -0
  30. package/esm/common/relay-socket.js +33 -4
  31. package/esm/guest/guest-i18n.d.ts +47 -0
  32. package/esm/guest/guest-i18n.js +97 -0
  33. package/esm/guest/guest-style.d.ts +1 -0
  34. package/esm/guest/guest-style.js +78 -0
  35. package/esm/guest/guest.d.ts +1 -0
  36. package/esm/guest/guest.js +371 -0
  37. package/esm/node/index.d.ts +3 -0
  38. package/esm/node/index.js +3 -0
  39. package/esm/node/relay-files-host.d.ts +27 -0
  40. package/esm/node/relay-files-host.js +130 -0
  41. package/esm/node/relay-host.d.ts +35 -2
  42. package/esm/node/relay-host.js +150 -13
  43. package/esm/node/relay-hub-files.d.ts +12 -0
  44. package/esm/node/relay-hub-files.js +39 -0
  45. package/esm/node/relay-server.d.ts +46 -1
  46. package/esm/node/relay-server.js +227 -1
  47. package/esm/node/relay-store.d.ts +72 -0
  48. package/esm/node/relay-store.js +123 -2
  49. package/esm/node/relay-viewer.d.ts +7 -0
  50. package/esm/node/relay-viewer.js +33 -0
  51. package/esm/viewer/viewer-i18n.d.ts +56 -0
  52. package/esm/viewer/viewer-i18n.js +117 -0
  53. package/esm/viewer/viewer-markdown.d.ts +7 -0
  54. package/esm/viewer/viewer-markdown.js +198 -0
  55. package/esm/viewer/viewer-style.d.ts +1 -0
  56. package/esm/viewer/viewer-style.js +72 -0
  57. package/esm/viewer/viewer.d.ts +8 -0
  58. package/esm/viewer/viewer.js +378 -0
  59. package/lib/common/index.d.ts +10 -0
  60. package/lib/common/index.js +10 -0
  61. package/lib/common/relay-blobs.d.ts +55 -0
  62. package/lib/common/relay-blobs.js +157 -0
  63. package/lib/common/relay-card.d.ts +24 -0
  64. package/lib/common/relay-card.js +33 -0
  65. package/lib/common/relay-cipher.d.ts +15 -0
  66. package/lib/common/relay-cipher.js +63 -3
  67. package/lib/common/relay-content.d.ts +105 -0
  68. package/lib/common/relay-content.js +320 -0
  69. package/lib/common/relay-conversation.d.ts +60 -0
  70. package/lib/common/relay-conversation.js +12 -0
  71. package/lib/common/relay-files.d.ts +109 -0
  72. package/lib/common/relay-files.js +174 -0
  73. package/lib/common/relay-hub-util.d.ts +92 -0
  74. package/lib/common/relay-hub-util.js +167 -0
  75. package/lib/common/relay-hub.d.ts +126 -0
  76. package/lib/common/relay-hub.js +442 -0
  77. package/lib/common/relay-hubs.d.ts +272 -0
  78. package/lib/common/relay-hubs.js +848 -0
  79. package/lib/common/relay-identity.d.ts +47 -0
  80. package/lib/common/relay-identity.js +129 -0
  81. package/lib/common/relay-protocol.d.ts +217 -2
  82. package/lib/common/relay-protocol.js +54 -1
  83. package/lib/common/relay-room-chat.d.ts +81 -0
  84. package/lib/common/relay-room-chat.js +156 -0
  85. package/lib/common/relay-socket.d.ts +8 -0
  86. package/lib/common/relay-socket.js +32 -3
  87. package/lib/guest/guest-i18n.d.ts +48 -0
  88. package/lib/guest/guest-i18n.js +100 -0
  89. package/lib/guest/guest-style.d.ts +2 -0
  90. package/lib/guest/guest-style.js +81 -0
  91. package/lib/guest/guest.d.ts +2 -0
  92. package/lib/guest/guest.js +373 -0
  93. package/lib/node/index.d.ts +3 -0
  94. package/lib/node/index.js +3 -0
  95. package/lib/node/relay-files-host.d.ts +28 -0
  96. package/lib/node/relay-files-host.js +134 -0
  97. package/lib/node/relay-host.d.ts +35 -2
  98. package/lib/node/relay-host.js +149 -12
  99. package/lib/node/relay-hub-files.d.ts +13 -0
  100. package/lib/node/relay-hub-files.js +43 -0
  101. package/lib/node/relay-server.d.ts +46 -1
  102. package/lib/node/relay-server.js +226 -0
  103. package/lib/node/relay-store.d.ts +72 -0
  104. package/lib/node/relay-store.js +121 -0
  105. package/lib/node/relay-viewer.d.ts +8 -0
  106. package/lib/node/relay-viewer.js +32 -0
  107. package/lib/viewer/viewer-i18n.d.ts +57 -0
  108. package/lib/viewer/viewer-i18n.js +120 -0
  109. package/lib/viewer/viewer-markdown.d.ts +8 -0
  110. package/lib/viewer/viewer-markdown.js +201 -0
  111. package/lib/viewer/viewer-style.d.ts +2 -0
  112. package/lib/viewer/viewer-style.js +75 -0
  113. package/lib/viewer/viewer.d.ts +9 -0
  114. package/lib/viewer/viewer.js +381 -0
  115. package/package.json +5 -4
  116. package/viewer/frame.html +1 -0
  117. package/viewer/page.html +80 -0
  118. package/viewer/room.html +87 -0
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayBlobClient = exports.RelayBlobs = void 0;
4
+ const relay_bytes_1 = require("./relay-bytes");
5
+ const relay_protocol_1 = require("./relay-protocol");
6
+ /** Below the 2 MiB of a value of a Durable Object. */
7
+ const CHUNK_BYTES = 1024 * 1024;
8
+ /** The sealing adds an IV, a tag and the header of the content. */
9
+ const MAX_BYTES = relay_protocol_1.RELAY_CONTENT_MAX_BYTES + 64 * 1024;
10
+ const FAIL_WINDOW_MS = 10 * 60 * 1000;
11
+ const MAX_FAILS = 20;
12
+ const DAY_MS = 24 * 60 * 60 * 1000;
13
+ const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'x-ta-auth', 'access-control-allow-methods': 'GET, OPTIONS' };
14
+ /**
15
+ * One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
16
+ * deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
17
+ * nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
18
+ */
19
+ class RelayBlobs {
20
+ constructor(storage, now = Date.now) {
21
+ this.storage = storage;
22
+ this.now = now;
23
+ }
24
+ async handle(request) {
25
+ switch (request.method) {
26
+ case 'OPTIONS':
27
+ return new Response(null, { status: 204, headers: READ_HEADERS });
28
+ case 'GET':
29
+ return this.read(request);
30
+ case 'PUT':
31
+ return this.write(request);
32
+ case 'DELETE':
33
+ return this.remove(request);
34
+ default:
35
+ return RelayBlobs.answer(405, 'Not allowed.');
36
+ }
37
+ }
38
+ async read(request) {
39
+ var _a, _b;
40
+ const meta = await this.meta();
41
+ if (!meta) {
42
+ return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
43
+ }
44
+ const fails = (_a = (await this.storage.get('fails'))) !== null && _a !== void 0 ? _a : { count: 0, since: 0 };
45
+ const now = this.now();
46
+ const recent = now - fails.since < FAIL_WINDOW_MS ? fails : { count: 0, since: now };
47
+ if (recent.count >= MAX_FAILS) {
48
+ return RelayBlobs.answer(429, 'Too many tries.', READ_HEADERS);
49
+ }
50
+ if ((await RelayBlobs.hash((_b = request.headers.get('x-ta-auth')) !== null && _b !== void 0 ? _b : '')) !== meta.authHash) {
51
+ await this.storage.put('fails', { count: recent.count + 1, since: recent.since });
52
+ return RelayBlobs.answer(403, 'Refused.', READ_HEADERS);
53
+ }
54
+ const parts = [];
55
+ for (let index = 0; index < meta.chunks; index++) {
56
+ const part = await this.storage.get(`c${index}`);
57
+ if (!part) {
58
+ return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
59
+ }
60
+ parts.push(new Uint8Array(part));
61
+ }
62
+ return new Response(relay_bytes_1.RelayBytes.concat(...parts), {
63
+ headers: { ...READ_HEADERS, 'content-type': 'application/octet-stream', 'cache-control': 'no-store' },
64
+ });
65
+ }
66
+ async write(request) {
67
+ var _a, _b;
68
+ var _c;
69
+ const writeHash = await RelayBlobs.hash(RelayBlobs.bearer(request));
70
+ const before = await this.storage.get('meta');
71
+ if (before && before.writeHash !== writeHash && before.expiresAt > this.now()) {
72
+ return RelayBlobs.answer(403, 'Another host keeps this copy.');
73
+ }
74
+ const authHash = (_c = request.headers.get('x-ta-auth-hash')) !== null && _c !== void 0 ? _c : '';
75
+ const expiresAt = Number(request.headers.get('x-ta-expires'));
76
+ const now = this.now();
77
+ if (RelayBlobs.bearer(request).length < 32 ||
78
+ !/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
79
+ !(expiresAt > now) ||
80
+ expiresAt > now + relay_protocol_1.RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
81
+ return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
82
+ }
83
+ const length = Number(request.headers.get('content-length'));
84
+ if (length > MAX_BYTES) {
85
+ return RelayBlobs.answer(413, 'Too large.');
86
+ }
87
+ const bytes = new Uint8Array(await request.arrayBuffer());
88
+ if (bytes.length === 0 || bytes.length > MAX_BYTES) {
89
+ return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
90
+ }
91
+ await this.storage.deleteAll();
92
+ const chunks = Math.ceil(bytes.length / CHUNK_BYTES);
93
+ for (let index = 0; index < chunks; index++) {
94
+ await this.storage.put(`c${index}`, bytes.slice(index * CHUNK_BYTES, (index + 1) * CHUNK_BYTES));
95
+ }
96
+ await this.storage.put('meta', { writeHash, authHash, expiresAt, size: bytes.length, chunks });
97
+ await ((_b = (_a = this.storage).setAlarm) === null || _b === void 0 ? void 0 : _b.call(_a, expiresAt));
98
+ return RelayBlobs.answer(201, 'Kept.');
99
+ }
100
+ async remove(request) {
101
+ const meta = await this.storage.get('meta');
102
+ if (meta && meta.writeHash !== (await RelayBlobs.hash(RelayBlobs.bearer(request)))) {
103
+ return RelayBlobs.answer(403, 'Refused.');
104
+ }
105
+ await this.storage.deleteAll();
106
+ return RelayBlobs.answer(200, 'Deleted.');
107
+ }
108
+ /** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
109
+ async meta() {
110
+ const meta = await this.storage.get('meta');
111
+ if (meta && meta.expiresAt <= this.now()) {
112
+ await this.storage.deleteAll();
113
+ return undefined;
114
+ }
115
+ return meta;
116
+ }
117
+ static bearer(request) {
118
+ var _a;
119
+ var _b, _c;
120
+ 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 : '';
121
+ }
122
+ static async hash(text) {
123
+ const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
124
+ return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(digest));
125
+ }
126
+ static answer(status, text, headers = {}) {
127
+ return new Response(`${text}\n`, { status, headers: { ...headers, 'content-type': 'text/plain; charset=utf-8' } });
128
+ }
129
+ }
130
+ exports.RelayBlobs = RelayBlobs;
131
+ /** The bytes of a copy at most, sealed. */
132
+ RelayBlobs.MAX_BYTES = MAX_BYTES;
133
+ /** What the host that shared content does with its copy on the relay. */
134
+ var RelayBlobClient;
135
+ (function (RelayBlobClient) {
136
+ async function put(copy) {
137
+ const response = await fetch(`${copy.relay}${relay_protocol_1.RELAY_BLOB_PATH}${copy.share}`, {
138
+ method: 'PUT',
139
+ headers: {
140
+ authorization: `Bearer ${copy.token}`,
141
+ 'x-ta-auth-hash': copy.authHash,
142
+ 'x-ta-expires': String(copy.expiresAt),
143
+ 'content-type': 'application/octet-stream',
144
+ },
145
+ body: copy.bytes,
146
+ });
147
+ if (!response.ok) {
148
+ throw new Error(`The relay did not keep the copy: ${response.status} ${(await response.text()).trim()}`);
149
+ }
150
+ }
151
+ RelayBlobClient.put = put;
152
+ async function remove(relay, share, token) {
153
+ await fetch(`${relay}${relay_protocol_1.RELAY_BLOB_PATH}${share}`, { method: 'DELETE', headers: { authorization: `Bearer ${token}` } });
154
+ }
155
+ RelayBlobClient.remove = remove;
156
+ })(RelayBlobClient || (exports.RelayBlobClient = RelayBlobClient = {}));
157
+ //# sourceMappingURL=relay-blobs.js.map
@@ -0,0 +1,24 @@
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
+ }
24
+ //# sourceMappingURL=relay-card.d.ts.map
@@ -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
@@ -4,10 +4,16 @@ export type RelaySide = 'client' | 'host';
4
4
  * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
5
5
  * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
6
6
  * message replayed, dropped or reordered by the relay fails to open.
7
+ *
8
+ * Encrypted bytes do not compress, so a message is compressed before it is sealed, once both sides said
9
+ * they can (`RelayHello.z`, `RelayWelcome.z`, then `compress`): JSON shrinks to a third or less, which
10
+ * is what a phone on a slow network waits for.
7
11
  */
8
12
  export declare class RelayCipher {
9
13
  protected readonly sendKey: CryptoKey;
10
14
  protected readonly receiveKey: CryptoKey;
15
+ /** Messages sealed from now are compressed when large, and those opened may be: both sides set it at the same message. */
16
+ compress: boolean;
11
17
  protected sent: number;
12
18
  protected received: number;
13
19
  protected sealing: Promise<unknown>;
@@ -18,6 +24,15 @@ export declare class RelayCipher {
18
24
  seal(text: string): Promise<string>;
19
25
  /** Rejects what this side's peer did not seal, or not as the next message. */
20
26
  open(frame: string): Promise<string>;
27
+ /** The bytes of a message to seal: as they are until compression is agreed, then behind the byte that says how. */
28
+ protected pack(text: string): Promise<Uint8Array>;
29
+ protected unpack(bytes: Uint8Array): Promise<string>;
21
30
  protected static iv(counter: number): Uint8Array;
22
31
  }
32
+ /** deflate-raw of the platform (CompressionStream), where it has it: browsers, Node.js 21.2 and later, Bun, Deno. */
33
+ export declare namespace RelayDeflate {
34
+ function supported(): boolean;
35
+ function compress(bytes: Uint8Array): Promise<Uint8Array>;
36
+ function decompress(bytes: Uint8Array): Promise<Uint8Array>;
37
+ }
23
38
  //# sourceMappingURL=relay-cipher.d.ts.map
@@ -1,20 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayCipher = void 0;
3
+ exports.RelayDeflate = exports.RelayCipher = void 0;
4
4
  const relay_bytes_1 = require("./relay-bytes");
5
5
  const encoder = new TextEncoder();
6
6
  const decoder = new TextDecoder();
7
7
  const IV_LENGTH = 12;
8
+ /** The first byte of a message once compression is agreed: what follows. */
9
+ const PLAIN = 0;
10
+ const DEFLATED = 1;
11
+ /** Smaller messages go as they are: they are about as small compressed. */
12
+ const DEFLATE_MIN_BYTES = 1024;
8
13
  /**
9
14
  * The encryption of one connection between a paired device and ta serve: AES-256-GCM with a key for
10
15
  * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
11
16
  * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
12
17
  * message replayed, dropped or reordered by the relay fails to open.
18
+ *
19
+ * Encrypted bytes do not compress, so a message is compressed before it is sealed, once both sides said
20
+ * they can (`RelayHello.z`, `RelayWelcome.z`, then `compress`): JSON shrinks to a third or less, which
21
+ * is what a phone on a slow network waits for.
13
22
  */
14
23
  class RelayCipher {
15
24
  constructor(sendKey, receiveKey) {
16
25
  this.sendKey = sendKey;
17
26
  this.receiveKey = receiveKey;
27
+ /** Messages sealed from now are compressed when large, and those opened may be: both sides set it at the same message. */
28
+ this.compress = false;
18
29
  this.sent = 0;
19
30
  this.received = 0;
20
31
  this.sealing = Promise.resolve();
@@ -32,7 +43,7 @@ class RelayCipher {
32
43
  seal(text) {
33
44
  const iv = RelayCipher.iv(this.sent++);
34
45
  const sealed = this.sealing.then(async () => {
35
- const data = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, this.sendKey, encoder.encode(text));
46
+ const data = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, this.sendKey, (await this.pack(text)));
36
47
  return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(data));
37
48
  });
38
49
  this.sealing = sealed.catch(() => undefined);
@@ -43,11 +54,29 @@ class RelayCipher {
43
54
  const iv = RelayCipher.iv(this.received++);
44
55
  const opened = this.opening.then(async () => {
45
56
  const data = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: iv }, this.receiveKey, relay_bytes_1.RelayBytes.fromBase64(frame));
46
- return decoder.decode(data);
57
+ return this.unpack(new Uint8Array(data));
47
58
  });
48
59
  this.opening = opened.catch(() => undefined);
49
60
  return opened;
50
61
  }
62
+ /** The bytes of a message to seal: as they are until compression is agreed, then behind the byte that says how. */
63
+ async pack(text) {
64
+ const bytes = encoder.encode(text);
65
+ if (!this.compress) {
66
+ return bytes;
67
+ }
68
+ const deflated = bytes.length >= DEFLATE_MIN_BYTES ? await RelayDeflate.compress(bytes) : undefined;
69
+ return deflated && deflated.length < bytes.length
70
+ ? relay_bytes_1.RelayBytes.concat(Uint8Array.of(DEFLATED), deflated)
71
+ : relay_bytes_1.RelayBytes.concat(Uint8Array.of(PLAIN), bytes);
72
+ }
73
+ async unpack(bytes) {
74
+ if (!this.compress) {
75
+ return decoder.decode(bytes);
76
+ }
77
+ const body = bytes.subarray(1);
78
+ return decoder.decode(bytes[0] === DEFLATED ? await RelayDeflate.decompress(body) : body);
79
+ }
51
80
  static iv(counter) {
52
81
  const iv = new Uint8Array(IV_LENGTH);
53
82
  new DataView(iv.buffer).setUint32(IV_LENGTH - 8, Math.floor(counter / 2 ** 32));
@@ -56,4 +85,35 @@ class RelayCipher {
56
85
  }
57
86
  }
58
87
  exports.RelayCipher = RelayCipher;
88
+ /** deflate-raw of the platform (CompressionStream), where it has it: browsers, Node.js 21.2 and later, Bun, Deno. */
89
+ var RelayDeflate;
90
+ (function (RelayDeflate) {
91
+ let support;
92
+ function supported() {
93
+ if (support === undefined) {
94
+ try {
95
+ new CompressionStream('deflate-raw');
96
+ new DecompressionStream('deflate-raw');
97
+ support = true;
98
+ }
99
+ catch (_a) {
100
+ support = false;
101
+ }
102
+ }
103
+ return support;
104
+ }
105
+ RelayDeflate.supported = supported;
106
+ function compress(bytes) {
107
+ return through(bytes, new CompressionStream('deflate-raw'));
108
+ }
109
+ RelayDeflate.compress = compress;
110
+ function decompress(bytes) {
111
+ return through(bytes, new DecompressionStream('deflate-raw'));
112
+ }
113
+ RelayDeflate.decompress = decompress;
114
+ async function through(bytes, stream) {
115
+ const piped = new Blob([bytes]).stream().pipeThrough(stream);
116
+ return new Uint8Array(await new Response(piped).arrayBuffer());
117
+ }
118
+ })(RelayDeflate || (exports.RelayDeflate = RelayDeflate = {}));
59
119
  //# sourceMappingURL=relay-cipher.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