@celljs/ta-relay 4.22.0 → 4.24.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 (80) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-blobs.d.ts +36 -2
  4. package/esm/common/relay-blobs.js +33 -5
  5. package/esm/common/relay-chat-store.d.ts +69 -0
  6. package/esm/common/relay-chat-store.js +50 -0
  7. package/esm/common/relay-hub-charter.d.ts +2 -0
  8. package/esm/common/relay-hub-charter.js +3 -0
  9. package/esm/common/relay-hub-log.d.ts +65 -0
  10. package/esm/common/relay-hub-log.js +147 -0
  11. package/esm/common/relay-hub.d.ts +3 -0
  12. package/esm/common/relay-hub.js +18 -0
  13. package/esm/common/relay-hubs-admins.d.ts +1 -0
  14. package/esm/common/relay-hubs-admins.js +10 -6
  15. package/esm/common/relay-hubs-chats.d.ts +73 -0
  16. package/esm/common/relay-hubs-chats.js +367 -0
  17. package/esm/common/relay-hubs-connection.d.ts +3 -1
  18. package/esm/common/relay-hubs-connection.js +7 -0
  19. package/esm/common/relay-hubs-protocol.d.ts +16 -0
  20. package/esm/common/relay-hubs.d.ts +2 -2
  21. package/esm/common/relay-hubs.js +14 -11
  22. package/esm/common/relay-info.d.ts +23 -1
  23. package/esm/common/relay-info.js +25 -2
  24. package/esm/common/relay-protocol.d.ts +60 -1
  25. package/esm/common/relay-protocol.js +15 -0
  26. package/esm/common/relay-room-chat.d.ts +126 -4
  27. package/esm/common/relay-room-chat.js +230 -6
  28. package/esm/guest/guest-i18n.d.ts +2 -0
  29. package/esm/guest/guest-i18n.js +8 -0
  30. package/esm/guest/guest.js +8 -5
  31. package/esm/node/index.d.ts +1 -0
  32. package/esm/node/index.js +1 -0
  33. package/esm/node/relay-chat-files.d.ts +52 -0
  34. package/esm/node/relay-chat-files.js +224 -0
  35. package/esm/node/relay-hub-files.d.ts +11 -1
  36. package/esm/node/relay-hub-files.js +8 -1
  37. package/esm/node/relay-server.d.ts +9 -1
  38. package/esm/node/relay-server.js +29 -18
  39. package/lib/common/index.d.ts +2 -0
  40. package/lib/common/index.js +2 -0
  41. package/lib/common/relay-blobs.d.ts +36 -2
  42. package/lib/common/relay-blobs.js +34 -6
  43. package/lib/common/relay-chat-store.d.ts +70 -0
  44. package/lib/common/relay-chat-store.js +54 -0
  45. package/lib/common/relay-hub-charter.d.ts +2 -0
  46. package/lib/common/relay-hub-charter.js +3 -0
  47. package/lib/common/relay-hub-log.d.ts +66 -0
  48. package/lib/common/relay-hub-log.js +151 -0
  49. package/lib/common/relay-hub.d.ts +3 -0
  50. package/lib/common/relay-hub.js +18 -0
  51. package/lib/common/relay-hubs-admins.d.ts +1 -0
  52. package/lib/common/relay-hubs-admins.js +9 -5
  53. package/lib/common/relay-hubs-chats.d.ts +74 -0
  54. package/lib/common/relay-hubs-chats.js +371 -0
  55. package/lib/common/relay-hubs-connection.d.ts +3 -1
  56. package/lib/common/relay-hubs-connection.js +7 -0
  57. package/lib/common/relay-hubs-protocol.d.ts +16 -0
  58. package/lib/common/relay-hubs.d.ts +2 -2
  59. package/lib/common/relay-hubs.js +14 -11
  60. package/lib/common/relay-info.d.ts +23 -1
  61. package/lib/common/relay-info.js +26 -3
  62. package/lib/common/relay-protocol.d.ts +60 -1
  63. package/lib/common/relay-protocol.js +16 -1
  64. package/lib/common/relay-room-chat.d.ts +126 -4
  65. package/lib/common/relay-room-chat.js +231 -7
  66. package/lib/guest/guest-i18n.d.ts +2 -0
  67. package/lib/guest/guest-i18n.js +8 -0
  68. package/lib/guest/guest.js +7 -4
  69. package/lib/node/index.d.ts +1 -0
  70. package/lib/node/index.js +1 -0
  71. package/lib/node/relay-chat-files.d.ts +53 -0
  72. package/lib/node/relay-chat-files.js +228 -0
  73. package/lib/node/relay-hub-files.d.ts +11 -1
  74. package/lib/node/relay-hub-files.js +8 -1
  75. package/lib/node/relay-server.d.ts +9 -1
  76. package/lib/node/relay-server.js +27 -16
  77. package/package.json +3 -3
  78. package/viewer/join.html +4 -4
  79. package/viewer/page.html +10 -10
  80. package/viewer/room.html +15 -15
@@ -0,0 +1,224 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto';
3
+ import { appendFileSync, chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
4
+ import { join } from 'node:path';
5
+ import { RelayChatStoreUtil } from '../common/relay-chat-store.js';
6
+ const ROOM = /^[A-Za-z0-9_-]{1,64}$/;
7
+ const MONTH = /^\d{4}-\d{2}\.log$/;
8
+ const STATE = 'state';
9
+ /**
10
+ * The key what is said in the rooms is sealed with on this computer (docs/design/ta-chat-history.md 12): in the
11
+ * keychain of macOS (the service `Ta chats`, an account of the agent directory), elsewhere or with
12
+ * `TA_CHATS_KEY=file` in `chats/.key`, readable by the user alone. Made the first time.
13
+ */
14
+ export var RelayChatKeys;
15
+ (function (RelayChatKeys) {
16
+ RelayChatKeys.SERVICE = 'Ta chats';
17
+ function local(agentDir, env = process.env, platform = process.platform) {
18
+ let key;
19
+ return () => {
20
+ key !== null && key !== void 0 ? key : (key = platform === 'darwin' && env.TA_CHATS_KEY !== 'file' ? keychain(agentDir).catch(() => file(agentDir)) : file(agentDir));
21
+ return key;
22
+ };
23
+ }
24
+ RelayChatKeys.local = local;
25
+ async function keychain(agentDir) {
26
+ const account = createHash('sha256').update(agentDir).digest('base64url').slice(0, 16);
27
+ const found = await run('security', ['find-generic-password', '-s', RelayChatKeys.SERVICE, '-a', account, '-w']);
28
+ const kept = found.code === 0 ? Buffer.from(found.stdout.trim(), 'base64') : undefined;
29
+ if ((kept === null || kept === void 0 ? void 0 : kept.length) === 32) {
30
+ return kept;
31
+ }
32
+ const made = randomBytes(32);
33
+ // Through the input of `security -i`: a key never goes in the arguments, which `ps` shows.
34
+ await run('security', ['-i'], `add-generic-password -U -s "${RelayChatKeys.SERVICE}" -a "${account}" -l "${RelayChatKeys.SERVICE}" -w "${made.toString('base64')}"\n`);
35
+ const again = await run('security', ['find-generic-password', '-s', RelayChatKeys.SERVICE, '-a', account, '-w']);
36
+ if (again.code !== 0 || Buffer.from(again.stdout.trim(), 'base64').length !== 32) {
37
+ throw new Error('The keychain did not keep the key of the chats.');
38
+ }
39
+ return Buffer.from(again.stdout.trim(), 'base64');
40
+ }
41
+ async function file(agentDir) {
42
+ const dir = join(agentDir, 'chats');
43
+ const path = join(dir, '.key');
44
+ if (existsSync(path)) {
45
+ const kept = Buffer.from(readFileSync(path, 'utf8').trim(), 'base64');
46
+ if (kept.length === 32) {
47
+ return kept;
48
+ }
49
+ }
50
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
51
+ const made = randomBytes(32);
52
+ writeFileSync(path, `${made.toString('base64')}\n`, { mode: 0o600 });
53
+ chmodSync(path, 0o600);
54
+ return made;
55
+ }
56
+ function run(command, args, input) {
57
+ return new Promise(resolve => {
58
+ const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'ignore'] });
59
+ let stdout = '';
60
+ child.stdout.on('data', chunk => {
61
+ stdout += chunk;
62
+ });
63
+ child.on('error', () => resolve({ code: -1, stdout }));
64
+ child.on('close', code => resolve({ code: code !== null && code !== void 0 ? code : -1, stdout }));
65
+ child.stdin.end(input !== null && input !== void 0 ? input : '');
66
+ });
67
+ }
68
+ })(RelayChatKeys || (RelayChatKeys = {}));
69
+ /**
70
+ * What is said in the rooms, kept in `chats/<room>/` of the agent directory (docs/design/ta-chat-history.md 8): a
71
+ * file a month (`2026-11.log`), lines only added, each sealed on its own (AES-256-GCM), so that a line cut short by a
72
+ * crash loses that line alone; and the state of the log (`state`), sealed, written whole. A month goes as a file.
73
+ */
74
+ export class RelayChatFiles {
75
+ constructor(dir, key, now = Date.now) {
76
+ this.dir = dir;
77
+ this.key = key;
78
+ this.now = now;
79
+ /** The lines of each room read, as long as nothing was added that is not in them. */
80
+ this.cache = new Map();
81
+ }
82
+ async read(room, options) {
83
+ const months = this.months(room);
84
+ const cached = this.cache.get(room);
85
+ let read = cached !== null && cached !== void 0 ? cached : { lines: [], months: 0, all: months.length === 0 };
86
+ const enough = () => {
87
+ const end = options.before === undefined ? read.lines.length : read.lines.findIndex(line => line.id === options.before);
88
+ return end >= 0 && end - options.limit > 0;
89
+ };
90
+ while (!read.all && !enough()) {
91
+ const month = months[months.length - 1 - read.months];
92
+ const lines = await this.readMonth(room, month);
93
+ read = { lines: [...lines, ...read.lines], months: read.months + 1, all: read.months + 1 >= months.length };
94
+ }
95
+ this.cache.set(room, read);
96
+ const page = RelayChatStoreUtil.page(read.lines, options);
97
+ return { lines: page.lines, more: page.more || !read.all };
98
+ }
99
+ async append(room, lines) {
100
+ if (!lines.length) {
101
+ return;
102
+ }
103
+ const key = await this.key();
104
+ const dir = this.roomDir(room);
105
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
106
+ const date = new Date(this.now());
107
+ const month = `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, '0')}.log`;
108
+ const path = join(dir, month);
109
+ const fresh = !existsSync(path);
110
+ appendFileSync(path, lines.map(line => `${RelayChatFiles.seal(key, JSON.stringify(line))}\n`).join(''), { mode: 0o600 });
111
+ const cached = this.cache.get(room);
112
+ if (cached) {
113
+ cached.lines.push(...lines);
114
+ if (fresh) {
115
+ cached.months++;
116
+ }
117
+ }
118
+ }
119
+ async state(room) {
120
+ const path = join(this.roomDir(room), STATE);
121
+ if (!existsSync(path)) {
122
+ return undefined;
123
+ }
124
+ const opened = RelayChatFiles.open(await this.key(), readFileSync(path, 'utf8').trim());
125
+ if (!opened) {
126
+ return undefined;
127
+ }
128
+ const state = JSON.parse(opened);
129
+ return { ...state, seq: Number(state.seq) || 0, outbox: Array.isArray(state.outbox) ? state.outbox : [] };
130
+ }
131
+ async setState(room, state) {
132
+ const key = await this.key();
133
+ const dir = this.roomDir(room);
134
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
135
+ const path = join(dir, STATE);
136
+ const temporary = `${path}.${process.pid}.tmp`;
137
+ writeFileSync(temporary, `${RelayChatFiles.seal(key, JSON.stringify(state))}\n`, { mode: 0o600 });
138
+ renameSync(temporary, path);
139
+ }
140
+ async forget(room) {
141
+ this.cache.delete(room);
142
+ rmSync(this.roomDir(room), { recursive: true, force: true });
143
+ }
144
+ /** The bytes of what is kept of a room; with `before`, of the months `clear` would take (those wholly before it). */
145
+ async usage(room, before) {
146
+ try {
147
+ const dir = this.roomDir(room);
148
+ const names = before === undefined ? readdirSync(dir) : this.months(room).filter(month => RelayChatFiles.monthEnd(month) <= before);
149
+ return names.reduce((sum, name) => { var _a; var _b; return sum + ((_b = (_a = statSync(join(dir, name), { throwIfNoEntry: false })) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : 0); }, 0);
150
+ }
151
+ catch (_a) {
152
+ return 0;
153
+ }
154
+ }
155
+ /** All the months of a room, or those wholly before `before`; a month after it stays whole. */
156
+ async clear(room, before) {
157
+ this.cache.delete(room);
158
+ for (const month of this.months(room)) {
159
+ if (before === undefined || RelayChatFiles.monthEnd(month) <= before) {
160
+ rmSync(join(this.roomDir(room), month), { force: true });
161
+ }
162
+ }
163
+ }
164
+ /** When the month of a file (`2026-11.log`) ends, UTC. */
165
+ static monthEnd(month) {
166
+ const [year, number] = month.slice(0, 7).split('-').map(Number);
167
+ return Date.UTC(year, number, 1);
168
+ }
169
+ roomDir(room) {
170
+ if (!ROOM.test(room)) {
171
+ throw new Error('Not a room.');
172
+ }
173
+ return join(this.dir, room);
174
+ }
175
+ /** The months kept of a room, the earliest first. */
176
+ months(room) {
177
+ try {
178
+ return readdirSync(this.roomDir(room))
179
+ .filter(name => MONTH.test(name))
180
+ .sort();
181
+ }
182
+ catch (_a) {
183
+ return [];
184
+ }
185
+ }
186
+ async readMonth(room, month) {
187
+ const key = await this.key();
188
+ const lines = [];
189
+ for (const sealed of readFileSync(join(this.roomDir(room), month), 'utf8').split('\n')) {
190
+ const opened = sealed && RelayChatFiles.open(key, sealed);
191
+ if (opened) {
192
+ try {
193
+ lines.push(JSON.parse(opened));
194
+ }
195
+ catch (_a) {
196
+ // A line cut short: that line alone is lost.
197
+ }
198
+ }
199
+ }
200
+ return lines;
201
+ }
202
+ /** base64url of a nonce of 12 bytes, the text sealed and its tag. */
203
+ static seal(key, text) {
204
+ const iv = randomBytes(12);
205
+ const cipher = createCipheriv('aes-256-gcm', key, iv);
206
+ const sealed = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
207
+ return Buffer.concat([iv, sealed, cipher.getAuthTag()]).toString('base64url');
208
+ }
209
+ static open(key, sealed) {
210
+ try {
211
+ const bytes = Buffer.from(sealed, 'base64url');
212
+ if (bytes.length < 28) {
213
+ return undefined;
214
+ }
215
+ const decipher = createDecipheriv('aes-256-gcm', key, bytes.subarray(0, 12));
216
+ decipher.setAuthTag(bytes.subarray(bytes.length - 16));
217
+ return Buffer.concat([decipher.update(bytes.subarray(12, bytes.length - 16)), decipher.final()]).toString('utf8');
218
+ }
219
+ catch (_a) {
220
+ return undefined;
221
+ }
222
+ }
223
+ }
224
+ //# sourceMappingURL=relay-chat-files.js.map
@@ -1,8 +1,18 @@
1
+ import type { RelayChatStore } from '../common/relay-chat-store.js';
1
2
  import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs-protocol.js';
2
3
  /** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
3
4
  export declare class RelayHubFiles implements RelayHubsStore {
4
5
  readonly agentDir: string;
5
- constructor(agentDir: string);
6
+ /** What is said in the rooms is kept in `chats/`, sealed with the key of the keychain (RelayChatKeys); else in memory. */
7
+ protected readonly options: {
8
+ chats?: boolean;
9
+ };
10
+ constructor(agentDir: string,
11
+ /** What is said in the rooms is kept in `chats/`, sealed with the key of the keychain (RelayChatKeys); else in memory. */
12
+ options?: {
13
+ chats?: boolean;
14
+ });
15
+ chats(): RelayChatStore | undefined;
6
16
  file(name: RelayHubsStoreName): string;
7
17
  read<T>(name: RelayHubsStoreName): T | undefined;
8
18
  /** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
@@ -1,10 +1,17 @@
1
1
  import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
2
  import { userInfo } from 'node:os';
3
3
  import { dirname, join } from 'node:path';
4
+ import { RelayChatFiles, RelayChatKeys } from './relay-chat-files.js';
4
5
  /** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
5
6
  export class RelayHubFiles {
6
- constructor(agentDir) {
7
+ constructor(agentDir,
8
+ /** What is said in the rooms is kept in `chats/`, sealed with the key of the keychain (RelayChatKeys); else in memory. */
9
+ options = {}) {
7
10
  this.agentDir = agentDir;
11
+ this.options = options;
12
+ }
13
+ chats() {
14
+ return this.options.chats ? new RelayChatFiles(join(this.agentDir, 'chats'), RelayChatKeys.local(this.agentDir)) : undefined;
8
15
  }
9
16
  file(name) {
10
17
  return join(this.agentDir, `${name}.json`);
@@ -5,6 +5,7 @@ import { type TaDeploymentEnv } from '@celljs/ta-protocol/lib/common/deployment-
5
5
  import type { RelayAccountProvider } from '../common/relay-accounts.js';
6
6
  import { RelayBlobs } from '../common/relay-blobs.js';
7
7
  import { RelayHub, type RelayHubWho } from '../common/relay-hub.js';
8
+ import { type RelayMedia } from '../common/relay-info.js';
8
9
  import { RelayRoom } from '../common/relay-room.js';
9
10
  import { RelaySpot, type RelaySpotSocket, type RelaySpotWho } from '../common/relay-spot.js';
10
11
  import { FileRelayStorage } from './relay-file-storage.js';
@@ -33,6 +34,11 @@ export interface RelayServerOptions {
33
34
  version?: string;
34
35
  /** The ways to sign in to accounts (docs/design/ta-account.md): GitHub with Ta's own app, unless given. */
35
36
  accounts?: RelayAccountProvider[];
37
+ /**
38
+ * Sealed copies of the pictures of chats it keeps (RELAY_MEDIA_PATH, docs/design/ta-chat-history.md 7), as its
39
+ * operator set it (`ta relay` reads TA_MEDIA_DAYS and TA_MEDIA_MAX_MB, a day by default, none with 0); none here unless given.
40
+ */
41
+ media?: RelayMedia;
36
42
  }
37
43
  interface Room {
38
44
  secretHash: string;
@@ -46,6 +52,8 @@ interface Hub {
46
52
  sockets: Map<WebSocketConnection, {
47
53
  who?: RelayHubWho;
48
54
  }>;
55
+ /** What all the connections of the room ask, one after another, as a Durable Object takes its events (RelayHub awaits between read and write). */
56
+ chain: Promise<void>;
49
57
  }
50
58
  /**
51
59
  * `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
@@ -84,7 +92,7 @@ export declare class RelayServer {
84
92
  protected web(path: string, response: ServerResponse): void;
85
93
  close(): Promise<void>;
86
94
  /** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
87
- protected blob(id: string, request: IncomingMessage, response: ServerResponse): Promise<void>;
95
+ protected blob(id: string, request: IncomingMessage, response: ServerResponse, media?: RelayMedia): Promise<void>;
88
96
  protected blobCopy(id: string): {
89
97
  blobs: RelayBlobs;
90
98
  storage: MemoryBlobStorage | FileRelayStorage;
@@ -5,10 +5,10 @@ import { join } from 'node:path';
5
5
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
6
6
  import { TA_WELL_KNOWN_PATH, TaDeploymentUtil } from '@celljs/ta-protocol/lib/common/deployment-protocol';
7
7
  import { RELAY_ACCOUNT_PATH } from '../common/relay-account.js';
8
- import { RelayBlobs } from '../common/relay-blobs.js';
8
+ import { RelayBlobs, RelayBlobsUtil } from '../common/relay-blobs.js';
9
9
  import { RelayHub } from '../common/relay-hub.js';
10
10
  import { RELAY_INFO_PATH, RelayInfoUtil } from '../common/relay-info.js';
11
- import { RELAY_BLOB_PATH, RELAY_CONNECT_PATH, RELAY_CONTENT_PATH, RELAY_FRAME_HEADER_CSP, RELAY_FRAME_PATH, RELAY_HOST_PATH, RELAY_HUB_PATH, RELAY_ID, RELAY_PAIR_PAGE, RELAY_PAIR_PATH, RELAY_ROOM_PATH, RELAY_SHARE_PAGE, RELAY_SHARE_PATH, RELAY_VIEWER_HEADERS, RelayCloseCode, } from '../common/relay-protocol.js';
11
+ import { RELAY_BLOB_PATH, RELAY_CONNECT_PATH, RELAY_CONTENT_PATH, RELAY_FRAME_HEADER_CSP, RELAY_FRAME_PATH, RELAY_HOST_PATH, RELAY_HUB_PATH, RELAY_ID, RELAY_MEDIA_PATH, RELAY_PAIR_PAGE, RELAY_PAIR_PATH, RELAY_ROOM_PATH, RELAY_SHARE_PAGE, RELAY_SHARE_PATH, RELAY_VIEWER_HEADERS, RelayCloseCode, } from '../common/relay-protocol.js';
12
12
  import { RelayRoom } from '../common/relay-room.js';
13
13
  import { RELAY_SPOT_PATH, RelaySpot, RelaySpotCloseCode, RelaySpotTries } from '../common/relay-spot.js';
14
14
  import { RELAY_JOIN_PATH, RELAY_WEB_PATH } from '../common/relay-team.js';
@@ -93,7 +93,7 @@ export class RelayServer {
93
93
  }
94
94
  if (path === RELAY_INFO_PATH) {
95
95
  // Without a directory for its data, rooms and copies live in memory: gone when the relay restarts.
96
- const info = RelayInfoUtil.of(!!this.options.data);
96
+ const info = RelayInfoUtil.of(!!this.options.data, this.options.media);
97
97
  response.writeHead(200, { 'content-type': 'application/json', 'access-control-allow-origin': '*', 'cache-control': 'no-store' });
98
98
  response.end(JSON.stringify(info));
99
99
  return;
@@ -102,8 +102,15 @@ export class RelayServer {
102
102
  this.accounts.serve(request, response);
103
103
  return;
104
104
  }
105
- if (path.startsWith(RELAY_BLOB_PATH)) {
106
- this.blob(path.slice(RELAY_BLOB_PATH.length), request, response).catch(() => {
105
+ const media = path.startsWith(RELAY_MEDIA_PATH);
106
+ if (media && !this.options.media) {
107
+ // Its operator lets it keep no pictures of chats.
108
+ response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' }).end('Not found.\n');
109
+ return;
110
+ }
111
+ if (path.startsWith(RELAY_BLOB_PATH) || media) {
112
+ const id = path.slice((media ? RELAY_MEDIA_PATH : RELAY_BLOB_PATH).length);
113
+ this.blob(id, request, response, media ? this.options.media : undefined).catch(() => {
107
114
  // A request aborted or that failed: an unhandled rejection would end the relay.
108
115
  if (!response.headersSent) {
109
116
  response.writeHead(400);
@@ -194,37 +201,40 @@ export class RelayServer {
194
201
  return new Promise(resolve => (this.server ? this.server.close(() => resolve()) : resolve()));
195
202
  }
196
203
  /** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
197
- async blob(id, request, response) {
198
- var _a;
204
+ async blob(id, request, response, media) {
205
+ var _a, _b;
199
206
  if (!RELAY_ID.test(id)) {
200
207
  response.writeHead(404).end();
201
208
  return;
202
209
  }
203
- const read = await RelayServer.body(request, RelayBlobs.MAX_BYTES);
210
+ const limits = media && RelayBlobsUtil.limitsOf(media);
211
+ const read = await RelayServer.body(request, (_a = limits === null || limits === void 0 ? void 0 : limits.bytes) !== null && _a !== void 0 ? _a : RelayBlobs.MAX_BYTES);
204
212
  if (!read) {
205
213
  // Refused before it is all in memory, whatever its content-length said.
206
214
  response.writeHead(413, { connection: 'close' }).end('Too large.\n');
207
215
  return;
208
216
  }
209
217
  // Kept only while it holds a copy: requests for ids of no copy leave nothing behind.
210
- const copy = this.blobCopy(id);
218
+ const key = media ? `media-${id}` : id;
219
+ const copy = this.blobCopy(key);
211
220
  const headers = new Headers();
212
221
  for (const [name, value] of Object.entries(request.headers)) {
213
222
  if (typeof value === 'string') {
214
223
  headers.set(name, value);
215
224
  }
216
225
  }
217
- const method = (_a = request.method) !== null && _a !== void 0 ? _a : 'GET';
226
+ const method = (_b = request.method) !== null && _b !== void 0 ? _b : 'GET';
218
227
  const body = read.length && method !== 'GET' && method !== 'HEAD' ? read : undefined;
219
228
  copy.pending++;
220
229
  let answer;
221
230
  try {
222
- answer = await copy.blobs.handle(new Request(`http://relay${request.url}`, { method, headers, body }));
231
+ headers.delete(RelayBlobsUtil.LIMITS_HEADER);
232
+ answer = await copy.blobs.handle(new Request(`http://relay${request.url}`, { method, headers, body }), limits);
223
233
  }
224
234
  finally {
225
235
  copy.pending--;
226
- if (copy.pending === 0 && copy.storage.empty && this.blobs.get(id) === copy) {
227
- this.blobs.delete(id);
236
+ if (copy.pending === 0 && copy.storage.empty && this.blobs.get(key) === copy) {
237
+ this.blobs.delete(key);
228
238
  }
229
239
  }
230
240
  response.writeHead(answer.status, Object.fromEntries(answer.headers));
@@ -427,7 +437,6 @@ export class RelayServer {
427
437
  const current = this.hubOf(id);
428
438
  const { sockets, hub: relay } = current;
429
439
  sockets.set(connection, {});
430
- let chain = Promise.resolve();
431
440
  const socket = {
432
441
  key: connection,
433
442
  get who() {
@@ -444,17 +453,19 @@ export class RelayServer {
444
453
  close: (code, reason) => connection.close(code, reason),
445
454
  };
446
455
  connection.onMessage(text => {
447
- chain = chain.then(() => relay.receive(socket, text)).catch(() => undefined);
456
+ current.chain = current.chain.then(() => relay.receive(socket, text)).catch(() => undefined);
448
457
  });
449
458
  connection.onClose(() => {
450
- void chain.then(() => {
459
+ current.chain = current.chain
460
+ .then(() => {
451
461
  if (sockets.has(connection)) {
452
462
  relay.closed(socket);
453
463
  sockets.delete(connection);
454
464
  }
455
465
  // Connections to ids of no room (refused, or the room closed) leave nothing behind.
456
466
  this.dropHub(id, current);
457
- });
467
+ })
468
+ .catch(() => undefined);
458
469
  });
459
470
  }
460
471
  /** The hub of a room: in memory, or read from `data` when it was kept. */
@@ -477,7 +488,7 @@ export class RelayServer {
477
488
  close: (code, reason) => other.close(code, reason),
478
489
  };
479
490
  };
480
- const made = { sockets };
491
+ const made = { sockets, chain: Promise.resolve() };
481
492
  const storage = this.options.data
482
493
  ? new FileRelayStorage(join(this.options.data, 'hubs', id), () => { var _a; return void ((_a = made.hub) === null || _a === void 0 ? void 0 : _a.alarm().then(() => this.dropHub(id, made))); })
483
494
  : new MemoryHubStorage();
@@ -5,6 +5,7 @@ export * from './relay-accounts';
5
5
  export * from './relay-blobs';
6
6
  export * from './relay-bytes';
7
7
  export * from './relay-card';
8
+ export * from './relay-chat-store';
8
9
  export * from './relay-cipher';
9
10
  export * from './relay-content';
10
11
  export * from './relay-conversation';
@@ -15,6 +16,7 @@ export * from './relay-group-invites';
15
16
  export * from './relay-host-calls';
16
17
  export * from './relay-hub';
17
18
  export * from './relay-hub-charter';
19
+ export * from './relay-hub-log';
18
20
  export * from './relay-hub-util';
19
21
  export * from './relay-hubs';
20
22
  export * from './relay-hubs-protocol';
@@ -21,6 +21,7 @@ __exportStar(require("./relay-accounts"), exports);
21
21
  __exportStar(require("./relay-blobs"), exports);
22
22
  __exportStar(require("./relay-bytes"), exports);
23
23
  __exportStar(require("./relay-card"), exports);
24
+ __exportStar(require("./relay-chat-store"), exports);
24
25
  __exportStar(require("./relay-cipher"), exports);
25
26
  __exportStar(require("./relay-content"), exports);
26
27
  __exportStar(require("./relay-conversation"), exports);
@@ -31,6 +32,7 @@ __exportStar(require("./relay-group-invites"), exports);
31
32
  __exportStar(require("./relay-host-calls"), exports);
32
33
  __exportStar(require("./relay-hub"), exports);
33
34
  __exportStar(require("./relay-hub-charter"), exports);
35
+ __exportStar(require("./relay-hub-log"), exports);
34
36
  __exportStar(require("./relay-hub-util"), exports);
35
37
  __exportStar(require("./relay-hubs"), exports);
36
38
  __exportStar(require("./relay-hubs-protocol"), exports);
@@ -15,6 +15,8 @@ interface BlobMeta {
15
15
  size: number;
16
16
  chunks: number;
17
17
  }
18
+ /** What sealing adds to a picture of a chat: a nonce and a tag. */
19
+ export declare const RELAY_MEDIA_SEAL_BYTES = 28;
18
20
  /**
19
21
  * One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
20
22
  * deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
@@ -23,10 +25,24 @@ interface BlobMeta {
23
25
  export declare class RelayBlobs {
24
26
  protected readonly storage: RelayBlobStorage;
25
27
  protected readonly now: () => number;
28
+ /** What a copy may be at most: content shared by default; a picture of a chat as the operator set it (RelayMedia). */
29
+ protected readonly limits: {
30
+ bytes: number;
31
+ days: number;
32
+ };
26
33
  /** The bytes of a copy at most, sealed. */
27
34
  static readonly MAX_BYTES: number;
28
- constructor(storage: RelayBlobStorage, now?: () => number);
29
- handle(request: Request): Promise<Response>;
35
+ constructor(storage: RelayBlobStorage, now?: () => number,
36
+ /** What a copy may be at most: content shared by default; a picture of a chat as the operator set it (RelayMedia). */
37
+ limits?: {
38
+ bytes: number;
39
+ days: number;
40
+ });
41
+ /** `limits`: of this request, the relay's own (a picture of a chat), not what a client says. */
42
+ handle(request: Request, limits?: {
43
+ bytes: number;
44
+ days: number;
45
+ }): Promise<Response>;
30
46
  protected read(request: Request): Promise<Response>;
31
47
  protected write(request: Request): Promise<Response>;
32
48
  protected remove(request: Request): Promise<Response>;
@@ -36,6 +52,24 @@ export declare class RelayBlobs {
36
52
  protected static hash(text: string): Promise<string>;
37
53
  protected static answer(status: number, text: string, headers?: Record<string, string>): Response;
38
54
  }
55
+ export declare namespace RelayBlobsUtil {
56
+ /**
57
+ * The limits of a copy of a picture of a chat on a relay its operator lets keep them (RelayMedia), as the Worker
58
+ * hands them to the object of the copy in a header of its own (a client's is taken away).
59
+ */
60
+ const LIMITS_HEADER = "x-ta-relay-limits";
61
+ function limitsOf(media: {
62
+ bytes: number;
63
+ days: number;
64
+ }): {
65
+ bytes: number;
66
+ days: number;
67
+ };
68
+ function readLimits(header: string | null): {
69
+ bytes: number;
70
+ days: number;
71
+ } | undefined;
72
+ }
39
73
  /** What the host that shared content does with its copy on the relay. */
40
74
  export declare namespace RelayBlobClient {
41
75
  interface Copy {
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayBlobClient = exports.RelayBlobs = void 0;
3
+ exports.RelayBlobClient = exports.RelayBlobsUtil = exports.RelayBlobs = exports.RELAY_MEDIA_SEAL_BYTES = void 0;
4
4
  const relay_bytes_1 = require("./relay-bytes");
5
5
  const relay_protocol_1 = require("./relay-protocol");
6
6
  /** Below the 2 MiB of a value of a Durable Object. */
7
7
  const CHUNK_BYTES = 1024 * 1024;
8
8
  /** The sealing adds an IV, a tag and the header of the content. */
9
9
  const MAX_BYTES = relay_protocol_1.RELAY_CONTENT_MAX_BYTES + 64 * 1024;
10
+ /** What sealing adds to a picture of a chat: a nonce and a tag. */
11
+ exports.RELAY_MEDIA_SEAL_BYTES = 28;
10
12
  const FAIL_WINDOW_MS = 10 * 60 * 1000;
11
13
  const MAX_FAILS = 20;
12
14
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -17,11 +19,18 @@ const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow
17
19
  * nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
18
20
  */
19
21
  class RelayBlobs {
20
- constructor(storage, now = Date.now) {
22
+ constructor(storage, now = Date.now,
23
+ /** What a copy may be at most: content shared by default; a picture of a chat as the operator set it (RelayMedia). */
24
+ limits = { bytes: MAX_BYTES, days: relay_protocol_1.RELAY_BLOB_MAX_DAYS }) {
21
25
  this.storage = storage;
22
26
  this.now = now;
27
+ this.limits = limits;
23
28
  }
24
- async handle(request) {
29
+ /** `limits`: of this request, the relay's own (a picture of a chat), not what a client says. */
30
+ async handle(request, limits) {
31
+ if (limits) {
32
+ return new RelayBlobs(this.storage, this.now, limits).handle(request);
33
+ }
25
34
  switch (request.method) {
26
35
  case 'OPTIONS':
27
36
  return new Response(null, { status: 204, headers: READ_HEADERS });
@@ -77,15 +86,15 @@ class RelayBlobs {
77
86
  if (RelayBlobs.bearer(request).length < 32 ||
78
87
  !/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
79
88
  !(expiresAt > now) ||
80
- expiresAt > now + relay_protocol_1.RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
89
+ expiresAt > now + this.limits.days * DAY_MS + DAY_MS) {
81
90
  return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
82
91
  }
83
92
  const length = Number(request.headers.get('content-length'));
84
- if (length > MAX_BYTES) {
93
+ if (length > this.limits.bytes) {
85
94
  return RelayBlobs.answer(413, 'Too large.');
86
95
  }
87
96
  const bytes = new Uint8Array(await request.arrayBuffer());
88
- if (bytes.length === 0 || bytes.length > MAX_BYTES) {
97
+ if (bytes.length === 0 || bytes.length > this.limits.bytes) {
89
98
  return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
90
99
  }
91
100
  await this.storage.deleteAll();
@@ -130,6 +139,25 @@ class RelayBlobs {
130
139
  exports.RelayBlobs = RelayBlobs;
131
140
  /** The bytes of a copy at most, sealed. */
132
141
  RelayBlobs.MAX_BYTES = MAX_BYTES;
142
+ var RelayBlobsUtil;
143
+ (function (RelayBlobsUtil) {
144
+ /**
145
+ * The limits of a copy of a picture of a chat on a relay its operator lets keep them (RelayMedia), as the Worker
146
+ * hands them to the object of the copy in a header of its own (a client's is taken away).
147
+ */
148
+ RelayBlobsUtil.LIMITS_HEADER = 'x-ta-relay-limits';
149
+ function limitsOf(media) {
150
+ return { bytes: media.bytes + exports.RELAY_MEDIA_SEAL_BYTES, days: media.days };
151
+ }
152
+ RelayBlobsUtil.limitsOf = limitsOf;
153
+ function readLimits(header) {
154
+ const [bytes, days] = String(header !== null && header !== void 0 ? header : '')
155
+ .split(',')
156
+ .map(Number);
157
+ return bytes > 0 && days > 0 ? { bytes, days } : undefined;
158
+ }
159
+ RelayBlobsUtil.readLimits = readLimits;
160
+ })(RelayBlobsUtil || (exports.RelayBlobsUtil = RelayBlobsUtil = {}));
133
161
  /** What the host that shared content does with its copy on the relay. */
134
162
  var RelayBlobClient;
135
163
  (function (RelayBlobClient) {