@celljs/ta-relay 4.22.0 → 4.23.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 +59 -0
  6. package/esm/common/relay-chat-store.js +40 -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 +66 -0
  16. package/esm/common/relay-hubs-chats.js +342 -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 +14 -0
  20. package/esm/common/relay-hubs.d.ts +2 -2
  21. package/esm/common/relay-hubs.js +13 -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 +46 -0
  34. package/esm/node/relay-chat-files.js +199 -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 +7 -1
  38. package/esm/node/relay-server.js +23 -13
  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 +60 -0
  44. package/lib/common/relay-chat-store.js +44 -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 +67 -0
  54. package/lib/common/relay-hubs-chats.js +346 -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 +14 -0
  58. package/lib/common/relay-hubs.d.ts +2 -2
  59. package/lib/common/relay-hubs.js +13 -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 +47 -0
  72. package/lib/node/relay-chat-files.js +203 -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 +7 -1
  76. package/lib/node/relay-server.js +21 -11
  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
@@ -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;
@@ -84,7 +90,7 @@ export declare class RelayServer {
84
90
  protected web(path: string, response: ServerResponse): void;
85
91
  close(): Promise<void>;
86
92
  /** 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>;
93
+ protected blob(id: string, request: IncomingMessage, response: ServerResponse, media?: RelayMedia): Promise<void>;
88
94
  protected blobCopy(id: string): {
89
95
  blobs: RelayBlobs;
90
96
  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));
@@ -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) {
@@ -0,0 +1,60 @@
1
+ import type { RelayChatLine } from './relay-room-chat';
2
+ /** What a Ta keeps of the log of each room besides its lines (docs/design/ta-chat-history.md 6). */
3
+ export interface RelayChatState {
4
+ /** The number of the last message of the log taken. */
5
+ seq: number;
6
+ /** Lines said here, not yet taken by the relay, in the order said (`pending`), or refused (`failed`). */
7
+ outbox: RelayChatLine[];
8
+ /** The signing keys of members who said something, kept for their lines once they left. */
9
+ signs?: Record<string, string>;
10
+ /** What was said in the state of the room before its log is kept here (RelayHubsChats.migrate). */
11
+ migrated?: true;
12
+ }
13
+ /**
14
+ * Where a Ta keeps what is said in its rooms (docs/design/ta-chat-history.md 8): files of the agent directory, sealed,
15
+ * for Ta (RelayChatFiles); memory for a guest in a browser, who reads the log again when the page comes back.
16
+ */
17
+ export interface RelayChatStore {
18
+ /** The latest `limit` lines kept, before the line `before` (its id) when given, the earliest first; `more` when older are kept. */
19
+ read(room: string, options: {
20
+ before?: string;
21
+ limit: number;
22
+ }): Promise<{
23
+ lines: RelayChatLine[];
24
+ more: boolean;
25
+ }>;
26
+ /** Adds lines after those kept. */
27
+ append(room: string, lines: RelayChatLine[]): Promise<void>;
28
+ /** None till the room was first synced on this computer. */
29
+ state(room: string): Promise<RelayChatState | undefined>;
30
+ setState(room: string, state: RelayChatState): Promise<void>;
31
+ /** The room was left: what was said there goes. */
32
+ forget(room: string): Promise<void>;
33
+ }
34
+ export declare namespace RelayChatStoreUtil {
35
+ /** The page of `lines` (the earliest first) that `read` answers. */
36
+ function page(lines: readonly RelayChatLine[], options: {
37
+ before?: string;
38
+ limit: number;
39
+ }): {
40
+ lines: RelayChatLine[];
41
+ more: boolean;
42
+ };
43
+ }
44
+ /** What is said, in memory: a guest's, and tests'. */
45
+ export declare class RelayChatMemory implements RelayChatStore {
46
+ protected readonly lines: Map<string, RelayChatLine[]>;
47
+ protected readonly states: Map<string, RelayChatState>;
48
+ read(room: string, options: {
49
+ before?: string;
50
+ limit: number;
51
+ }): Promise<{
52
+ lines: RelayChatLine[];
53
+ more: boolean;
54
+ }>;
55
+ append(room: string, lines: RelayChatLine[]): Promise<void>;
56
+ state(room: string): Promise<RelayChatState | undefined>;
57
+ setState(room: string, state: RelayChatState): Promise<void>;
58
+ forget(room: string): Promise<void>;
59
+ }
60
+ //# sourceMappingURL=relay-chat-store.d.ts.map
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayChatMemory = exports.RelayChatStoreUtil = void 0;
4
+ var RelayChatStoreUtil;
5
+ (function (RelayChatStoreUtil) {
6
+ /** The page of `lines` (the earliest first) that `read` answers. */
7
+ function page(lines, options) {
8
+ const end = options.before === undefined ? lines.length : lines.findIndex(line => line.id === options.before);
9
+ if (end < 0) {
10
+ return { lines: [], more: false };
11
+ }
12
+ const start = Math.max(end - Math.max(options.limit, 1), 0);
13
+ return { lines: lines.slice(start, end), more: start > 0 };
14
+ }
15
+ RelayChatStoreUtil.page = page;
16
+ })(RelayChatStoreUtil || (exports.RelayChatStoreUtil = RelayChatStoreUtil = {}));
17
+ /** What is said, in memory: a guest's, and tests'. */
18
+ class RelayChatMemory {
19
+ constructor() {
20
+ this.lines = new Map();
21
+ this.states = new Map();
22
+ }
23
+ async read(room, options) {
24
+ var _a;
25
+ return RelayChatStoreUtil.page((_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : [], options);
26
+ }
27
+ async append(room, lines) {
28
+ var _a;
29
+ this.lines.set(room, [...((_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : []), ...lines]);
30
+ }
31
+ async state(room) {
32
+ const state = this.states.get(room);
33
+ return state && structuredClone(state);
34
+ }
35
+ async setState(room, state) {
36
+ this.states.set(room, structuredClone(state));
37
+ }
38
+ async forget(room) {
39
+ this.lines.delete(room);
40
+ this.states.delete(room);
41
+ }
42
+ }
43
+ exports.RelayChatMemory = RelayChatMemory;
44
+ //# sourceMappingURL=relay-chat-store.js.map
@@ -21,6 +21,8 @@ export interface RelayHubCharterBody {
21
21
  admins: RelayHubAdmin[];
22
22
  rules: RelayHubRules;
23
23
  capacity?: number;
24
+ /** Messages from before they joined a newcomer reads (docs/design/ta-chat-history.md 12): none by default, RELAY_HUB_HISTORY_MAX at most. */
25
+ history?: number;
24
26
  }
25
27
  /** The charter as the roster carries it: `p` its JSON, `s` the owner's signature of it. */
26
28
  export interface RelayHubCharter {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelayHubCharterUtil = exports.RELAY_HUB_MAX_ADMINS = exports.RELAY_GUESTS_PER_HOUR = exports.RELAY_GUESTS_RULE = void 0;
4
4
  const relay_hub_util_1 = require("./relay-hub-util");
5
5
  const relay_identity_1 = require("./relay-identity");
6
+ const relay_protocol_1 = require("./relay-protocol");
6
7
  /** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
7
8
  exports.RELAY_GUESTS_RULE = 'guests';
8
9
  exports.RELAY_GUESTS_PER_HOUR = 30;
@@ -42,6 +43,7 @@ var RelayHubCharterUtil;
42
43
  return undefined;
43
44
  }
44
45
  const capacity = Math.round(Number(body.capacity));
46
+ const history = Math.min(Math.round(Number(body.history) || 0), relay_protocol_1.RELAY_HUB_HISTORY_MAX);
45
47
  return {
46
48
  room,
47
49
  v: Number(body.v),
@@ -51,6 +53,7 @@ var RelayHubCharterUtil;
51
53
  .map(admin => ({ member: admin.member, id: admin.id })),
52
54
  rules: rules(body.rules),
53
55
  ...(capacity >= 2 ? { capacity } : {}),
56
+ ...(history > 0 ? { history } : {}),
54
57
  };
55
58
  }
56
59
  catch (_a) {
@@ -0,0 +1,66 @@
1
+ import { type RelayHubEvent, type RelayHubLogItem, type RelayHubRequest } from './relay-protocol';
2
+ /** Where the log is kept: the storage of the room (RelayHubStorage). */
3
+ export interface RelayHubLogStorage {
4
+ get<T>(key: string): Promise<T | undefined>;
5
+ put(key: string, value: unknown): Promise<void>;
6
+ delete(key: string): Promise<unknown>;
7
+ }
8
+ /** What the log knows of itself: the numbers it holds, `first` past `last` when none, and their characters. */
9
+ export interface RelayHubLogMeta {
10
+ first: number;
11
+ last: number;
12
+ chars: number;
13
+ }
14
+ /** Each member: what it has (`seen`), and the first number it may read (`floor`, a newcomer's). */
15
+ type LogMembers = Record<string, {
16
+ seen?: number;
17
+ floor?: number;
18
+ }>;
19
+ type Kept = RelayHubLogItem & {
20
+ mid: string;
21
+ };
22
+ type LogRequest = Extract<RelayHubRequest, {
23
+ t: 'post' | 'sync' | 'ack';
24
+ }>;
25
+ /**
26
+ * The log of a room at the relay (docs/design/ta-chat-history.md 5): what members post, numbered in the order it came,
27
+ * sealed; a member reads it from where it is (`sync`) and says how far it has it (`ack`). Posting the same line again
28
+ * (its id) is the same message. Kept while some member lacks it, RELAY_HUB_LOG_KEEP_DAYS once all have it,
29
+ * RELAY_HUB_LOG_MAX_DAYS at most, and within RELAY_HUB_LOG_MAX and RELAY_HUB_LOG_CHARS, the oldest going first.
30
+ */
31
+ export declare class RelayHubLog {
32
+ protected readonly storage: RelayHubLogStorage;
33
+ protected readonly now: () => number;
34
+ constructor(storage: RelayHubLogStorage, now?: () => number);
35
+ /** What a member asked of the log: the answer to it, and what the others hear. */
36
+ receive(member: string, request: LogRequest, members: readonly string[]): Promise<{
37
+ reply?: RelayHubEvent;
38
+ broadcast?: RelayHubEvent;
39
+ }>;
40
+ meta(): Promise<RelayHubLogMeta>;
41
+ /** Adds a message, numbered after the last; one of the same id already there is answered with its number. */
42
+ post(member: string, mid: string, d: string, members: readonly string[]): Promise<{
43
+ item: RelayHubLogItem;
44
+ fresh: boolean;
45
+ }>;
46
+ /** A page of the log after `after`, from the member's floor on: `first` past `after + 1` tells what is gone. */
47
+ read(member: string, after: number, limit: number): Promise<{
48
+ items: RelayHubLogItem[];
49
+ first: number;
50
+ last: number;
51
+ more: boolean;
52
+ }>;
53
+ ack(member: string, seq: number, members: readonly string[]): Promise<void>;
54
+ /** A newcomer: it reads the log from `history` messages before now on (none by default, RELAY_HUB_HISTORY_MAX at most). */
55
+ admitted(member: string, history: unknown): Promise<void>;
56
+ /** A member left or was taken out: it holds nothing back any more. */
57
+ gone(member: string): Promise<void>;
58
+ /** Lets go of the oldest messages past what is kept, then writes what the log knows of itself. */
59
+ prune(meta: RelayHubLogMeta, members: readonly string[], all?: LogMembers): Promise<void>;
60
+ protected members(): Promise<LogMembers>;
61
+ /** Numbers padded, so that keys sort as they come. */
62
+ static key(seq: number): string;
63
+ protected static item({ s, m, d, at }: Kept): RelayHubLogItem;
64
+ }
65
+ export {};
66
+ //# sourceMappingURL=relay-hub-log.d.ts.map