@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
@@ -5,6 +5,7 @@ export * from './relay-accounts.js';
5
5
  export * from './relay-blobs.js';
6
6
  export * from './relay-bytes.js';
7
7
  export * from './relay-card.js';
8
+ export * from './relay-chat-store.js';
8
9
  export * from './relay-cipher.js';
9
10
  export * from './relay-content.js';
10
11
  export * from './relay-conversation.js';
@@ -15,6 +16,7 @@ export * from './relay-group-invites.js';
15
16
  export * from './relay-host-calls.js';
16
17
  export * from './relay-hub.js';
17
18
  export * from './relay-hub-charter.js';
19
+ export * from './relay-hub-log.js';
18
20
  export * from './relay-hub-util.js';
19
21
  export * from './relay-hubs.js';
20
22
  export * from './relay-hubs-protocol.js';
@@ -5,6 +5,7 @@ export * from './relay-accounts.js';
5
5
  export * from './relay-blobs.js';
6
6
  export * from './relay-bytes.js';
7
7
  export * from './relay-card.js';
8
+ export * from './relay-chat-store.js';
8
9
  export * from './relay-cipher.js';
9
10
  export * from './relay-content.js';
10
11
  export * from './relay-conversation.js';
@@ -15,6 +16,7 @@ export * from './relay-group-invites.js';
15
16
  export * from './relay-host-calls.js';
16
17
  export * from './relay-hub.js';
17
18
  export * from './relay-hub-charter.js';
19
+ export * from './relay-hub-log.js';
18
20
  export * from './relay-hub-util.js';
19
21
  export * from './relay-hubs.js';
20
22
  export * from './relay-hubs-protocol.js';
@@ -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 {
@@ -4,6 +4,8 @@ import { RELAY_BLOB_MAX_DAYS, RELAY_BLOB_PATH, RELAY_CONTENT_MAX_BYTES } from '.
4
4
  const CHUNK_BYTES = 1024 * 1024;
5
5
  /** The sealing adds an IV, a tag and the header of the content. */
6
6
  const MAX_BYTES = RELAY_CONTENT_MAX_BYTES + 64 * 1024;
7
+ /** What sealing adds to a picture of a chat: a nonce and a tag. */
8
+ export const RELAY_MEDIA_SEAL_BYTES = 28;
7
9
  const FAIL_WINDOW_MS = 10 * 60 * 1000;
8
10
  const MAX_FAILS = 20;
9
11
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -14,11 +16,18 @@ const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow
14
16
  * nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
15
17
  */
16
18
  export class RelayBlobs {
17
- constructor(storage, now = Date.now) {
19
+ constructor(storage, now = Date.now,
20
+ /** What a copy may be at most: content shared by default; a picture of a chat as the operator set it (RelayMedia). */
21
+ limits = { bytes: MAX_BYTES, days: RELAY_BLOB_MAX_DAYS }) {
18
22
  this.storage = storage;
19
23
  this.now = now;
24
+ this.limits = limits;
20
25
  }
21
- async handle(request) {
26
+ /** `limits`: of this request, the relay's own (a picture of a chat), not what a client says. */
27
+ async handle(request, limits) {
28
+ if (limits) {
29
+ return new RelayBlobs(this.storage, this.now, limits).handle(request);
30
+ }
22
31
  switch (request.method) {
23
32
  case 'OPTIONS':
24
33
  return new Response(null, { status: 204, headers: READ_HEADERS });
@@ -74,15 +83,15 @@ export class RelayBlobs {
74
83
  if (RelayBlobs.bearer(request).length < 32 ||
75
84
  !/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
76
85
  !(expiresAt > now) ||
77
- expiresAt > now + RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
86
+ expiresAt > now + this.limits.days * DAY_MS + DAY_MS) {
78
87
  return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
79
88
  }
80
89
  const length = Number(request.headers.get('content-length'));
81
- if (length > MAX_BYTES) {
90
+ if (length > this.limits.bytes) {
82
91
  return RelayBlobs.answer(413, 'Too large.');
83
92
  }
84
93
  const bytes = new Uint8Array(await request.arrayBuffer());
85
- if (bytes.length === 0 || bytes.length > MAX_BYTES) {
94
+ if (bytes.length === 0 || bytes.length > this.limits.bytes) {
86
95
  return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
87
96
  }
88
97
  await this.storage.deleteAll();
@@ -126,6 +135,25 @@ export class RelayBlobs {
126
135
  }
127
136
  /** The bytes of a copy at most, sealed. */
128
137
  RelayBlobs.MAX_BYTES = MAX_BYTES;
138
+ export var RelayBlobsUtil;
139
+ (function (RelayBlobsUtil) {
140
+ /**
141
+ * The limits of a copy of a picture of a chat on a relay its operator lets keep them (RelayMedia), as the Worker
142
+ * hands them to the object of the copy in a header of its own (a client's is taken away).
143
+ */
144
+ RelayBlobsUtil.LIMITS_HEADER = 'x-ta-relay-limits';
145
+ function limitsOf(media) {
146
+ return { bytes: media.bytes + RELAY_MEDIA_SEAL_BYTES, days: media.days };
147
+ }
148
+ RelayBlobsUtil.limitsOf = limitsOf;
149
+ function readLimits(header) {
150
+ const [bytes, days] = String(header !== null && header !== void 0 ? header : '')
151
+ .split(',')
152
+ .map(Number);
153
+ return bytes > 0 && days > 0 ? { bytes, days } : undefined;
154
+ }
155
+ RelayBlobsUtil.readLimits = readLimits;
156
+ })(RelayBlobsUtil || (RelayBlobsUtil = {}));
129
157
  /** What the host that shared content does with its copy on the relay. */
130
158
  export var RelayBlobClient;
131
159
  (function (RelayBlobClient) {
@@ -0,0 +1,59 @@
1
+ import type { RelayChatLine } from './relay-room-chat.js';
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
+ }
@@ -0,0 +1,40 @@
1
+ export var RelayChatStoreUtil;
2
+ (function (RelayChatStoreUtil) {
3
+ /** The page of `lines` (the earliest first) that `read` answers. */
4
+ function page(lines, options) {
5
+ const end = options.before === undefined ? lines.length : lines.findIndex(line => line.id === options.before);
6
+ if (end < 0) {
7
+ return { lines: [], more: false };
8
+ }
9
+ const start = Math.max(end - Math.max(options.limit, 1), 0);
10
+ return { lines: lines.slice(start, end), more: start > 0 };
11
+ }
12
+ RelayChatStoreUtil.page = page;
13
+ })(RelayChatStoreUtil || (RelayChatStoreUtil = {}));
14
+ /** What is said, in memory: a guest's, and tests'. */
15
+ export class RelayChatMemory {
16
+ constructor() {
17
+ this.lines = new Map();
18
+ this.states = new Map();
19
+ }
20
+ async read(room, options) {
21
+ var _a;
22
+ return RelayChatStoreUtil.page((_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : [], options);
23
+ }
24
+ async append(room, lines) {
25
+ var _a;
26
+ this.lines.set(room, [...((_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : []), ...lines]);
27
+ }
28
+ async state(room) {
29
+ const state = this.states.get(room);
30
+ return state && structuredClone(state);
31
+ }
32
+ async setState(room, state) {
33
+ this.states.set(room, structuredClone(state));
34
+ }
35
+ async forget(room) {
36
+ this.lines.delete(room);
37
+ this.states.delete(room);
38
+ }
39
+ }
40
+ //# 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 {
@@ -1,5 +1,6 @@
1
1
  import { RelayHubUtil } from './relay-hub-util.js';
2
2
  import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
3
+ import { RELAY_HUB_HISTORY_MAX } from './relay-protocol.js';
3
4
  /** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
4
5
  export const RELAY_GUESTS_RULE = 'guests';
5
6
  export const RELAY_GUESTS_PER_HOUR = 30;
@@ -39,6 +40,7 @@ export var RelayHubCharterUtil;
39
40
  return undefined;
40
41
  }
41
42
  const capacity = Math.round(Number(body.capacity));
43
+ const history = Math.min(Math.round(Number(body.history) || 0), RELAY_HUB_HISTORY_MAX);
42
44
  return {
43
45
  room,
44
46
  v: Number(body.v),
@@ -48,6 +50,7 @@ export var RelayHubCharterUtil;
48
50
  .map(admin => ({ member: admin.member, id: admin.id })),
49
51
  rules: rules(body.rules),
50
52
  ...(capacity >= 2 ? { capacity } : {}),
53
+ ...(history > 0 ? { history } : {}),
51
54
  };
52
55
  }
53
56
  catch (_a) {
@@ -0,0 +1,65 @@
1
+ import { type RelayHubEvent, type RelayHubLogItem, type RelayHubRequest } from './relay-protocol.js';
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 {};
@@ -0,0 +1,147 @@
1
+ import { RELAY_HUB_HISTORY_MAX, RELAY_HUB_LOG_CHARS, RELAY_HUB_LOG_KEEP_DAYS, RELAY_HUB_LOG_MAX, RELAY_HUB_LOG_MAX_DAYS, RELAY_HUB_MAX_VALUE, RELAY_HUB_SYNC_PAGE, } from './relay-protocol.js';
2
+ const DAY_MS = 24 * 60 * 60 * 1000;
3
+ /** Messages let go of at most at once: the rest go the next time. */
4
+ const PRUNE_STEP = 256;
5
+ const META = 'log';
6
+ const MEMBERS = 'logm';
7
+ const ITEM = 'm:';
8
+ const MID = 'i:';
9
+ const MID_FORMAT = /^[A-Za-z0-9_-]{8,40}$/;
10
+ /**
11
+ * The log of a room at the relay (docs/design/ta-chat-history.md 5): what members post, numbered in the order it came,
12
+ * sealed; a member reads it from where it is (`sync`) and says how far it has it (`ack`). Posting the same line again
13
+ * (its id) is the same message. Kept while some member lacks it, RELAY_HUB_LOG_KEEP_DAYS once all have it,
14
+ * RELAY_HUB_LOG_MAX_DAYS at most, and within RELAY_HUB_LOG_MAX and RELAY_HUB_LOG_CHARS, the oldest going first.
15
+ */
16
+ export class RelayHubLog {
17
+ constructor(storage, now = Date.now) {
18
+ this.storage = storage;
19
+ this.now = now;
20
+ }
21
+ /** What a member asked of the log: the answer to it, and what the others hear. */
22
+ async receive(member, request, members) {
23
+ switch (request.t) {
24
+ case 'post': {
25
+ const id = Number(request.id);
26
+ if (typeof request.d !== 'string' || !MID_FORMAT.test(String(request.mid))) {
27
+ return { reply: { t: 'done', id, ok: false, v: 0, error: 'invalid' } };
28
+ }
29
+ if (request.d.length > RELAY_HUB_MAX_VALUE) {
30
+ return { reply: { t: 'done', id, ok: false, v: 0, error: 'large' } };
31
+ }
32
+ const { item, fresh } = await this.post(member, request.mid, request.d, members);
33
+ return { reply: { t: 'done', id, ok: true, v: item.s }, ...(fresh ? { broadcast: { t: 'post', item } } : {}) };
34
+ }
35
+ case 'sync':
36
+ return { reply: { t: 'log', ...(await this.read(member, Number(request.after) || 0, Number(request.limit) || RELAY_HUB_SYNC_PAGE)) } };
37
+ case 'ack':
38
+ await this.ack(member, Number(request.seq) || 0, members);
39
+ return {};
40
+ }
41
+ }
42
+ async meta() {
43
+ var _a;
44
+ return (_a = (await this.storage.get(META))) !== null && _a !== void 0 ? _a : { first: 1, last: 0, chars: 0 };
45
+ }
46
+ /** Adds a message, numbered after the last; one of the same id already there is answered with its number. */
47
+ async post(member, mid, d, members) {
48
+ const known = await this.storage.get(MID + mid);
49
+ const kept = known === undefined ? undefined : await this.storage.get(RelayHubLog.key(known));
50
+ if (kept && kept.m === member) {
51
+ return { item: RelayHubLog.item(kept), fresh: false };
52
+ }
53
+ const meta = await this.meta();
54
+ const item = { s: meta.last + 1, m: member, d, at: this.now() };
55
+ await this.storage.put(RelayHubLog.key(item.s), { ...item, mid });
56
+ await this.storage.put(MID + mid, item.s);
57
+ meta.last = item.s;
58
+ meta.chars += d.length;
59
+ await this.prune(meta, members);
60
+ return { item, fresh: true };
61
+ }
62
+ /** A page of the log after `after`, from the member's floor on: `first` past `after + 1` tells what is gone. */
63
+ async read(member, after, limit) {
64
+ var _a;
65
+ var _b;
66
+ const meta = await this.meta();
67
+ const floor = (_b = (_a = (await this.members())[member]) === null || _a === void 0 ? void 0 : _a.floor) !== null && _b !== void 0 ? _b : 0;
68
+ const start = Math.max(after + 1, floor + 1, meta.first);
69
+ const end = Math.min(meta.last, start + Math.min(Math.max(limit, 1), RELAY_HUB_SYNC_PAGE) - 1);
70
+ const items = [];
71
+ for (let seq = start; seq <= end; seq++) {
72
+ const kept = await this.storage.get(RelayHubLog.key(seq));
73
+ if (kept) {
74
+ items.push(RelayHubLog.item(kept));
75
+ }
76
+ }
77
+ return { items, first: Math.max(meta.first, floor + 1), last: meta.last, more: end < meta.last };
78
+ }
79
+ async ack(member, seq, members) {
80
+ var _a;
81
+ var _b;
82
+ const all = await this.members();
83
+ const meta = await this.meta();
84
+ const seen = Math.min(Math.floor(seq), meta.last);
85
+ if (!(seen > ((_b = (_a = all[member]) === null || _a === void 0 ? void 0 : _a.seen) !== null && _b !== void 0 ? _b : 0))) {
86
+ return;
87
+ }
88
+ all[member] = { ...all[member], seen };
89
+ await this.storage.put(MEMBERS, all);
90
+ await this.prune(meta, members, all);
91
+ }
92
+ /** A newcomer: it reads the log from `history` messages before now on (none by default, RELAY_HUB_HISTORY_MAX at most). */
93
+ async admitted(member, history) {
94
+ const meta = await this.meta();
95
+ const back = Math.min(Math.max(Math.floor(Number(history) || 0), 0), RELAY_HUB_HISTORY_MAX);
96
+ const all = await this.members();
97
+ all[member] = { floor: Math.max(meta.last - back, 0), seen: meta.last };
98
+ await this.storage.put(MEMBERS, all);
99
+ }
100
+ /** A member left or was taken out: it holds nothing back any more. */
101
+ async gone(member) {
102
+ const all = await this.members();
103
+ if (all[member]) {
104
+ delete all[member];
105
+ await this.storage.put(MEMBERS, all);
106
+ }
107
+ }
108
+ /** Lets go of the oldest messages past what is kept, then writes what the log knows of itself. */
109
+ async prune(meta, members, all) {
110
+ const now = this.now();
111
+ let seenByAll;
112
+ for (let step = 0; step < PRUNE_STEP && meta.first <= meta.last; step++) {
113
+ const kept = await this.storage.get(RelayHubLog.key(meta.first));
114
+ const count = meta.last - meta.first + 1;
115
+ let drop = !kept || count > RELAY_HUB_LOG_MAX || meta.chars > RELAY_HUB_LOG_CHARS || now - kept.at > RELAY_HUB_LOG_MAX_DAYS * DAY_MS;
116
+ if (!drop && kept && now - kept.at > RELAY_HUB_LOG_KEEP_DAYS * DAY_MS) {
117
+ if (seenByAll === undefined) {
118
+ const known = all !== null && all !== void 0 ? all : (await this.members());
119
+ seenByAll = Math.min(...members.map(member => { var _a; var _b; return (_b = (_a = known[member]) === null || _a === void 0 ? void 0 : _a.seen) !== null && _b !== void 0 ? _b : 0; }), meta.last);
120
+ }
121
+ drop = kept.s <= seenByAll;
122
+ }
123
+ if (!drop) {
124
+ break;
125
+ }
126
+ if (kept) {
127
+ meta.chars = Math.max(meta.chars - kept.d.length, 0);
128
+ await this.storage.delete(MID + kept.mid);
129
+ }
130
+ await this.storage.delete(RelayHubLog.key(meta.first));
131
+ meta.first++;
132
+ }
133
+ await this.storage.put(META, meta);
134
+ }
135
+ async members() {
136
+ var _a;
137
+ return (_a = (await this.storage.get(MEMBERS))) !== null && _a !== void 0 ? _a : {};
138
+ }
139
+ /** Numbers padded, so that keys sort as they come. */
140
+ static key(seq) {
141
+ return ITEM + String(seq).padStart(12, '0');
142
+ }
143
+ static item({ s, m, d, at }) {
144
+ return { s, m, d, at };
145
+ }
146
+ }
147
+ //# sourceMappingURL=relay-hub-log.js.map
@@ -1,3 +1,4 @@
1
+ import { RelayHubLog } from './relay-hub-log.js';
1
2
  import { type RelayHubAsk, type RelayHubEvent, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
2
3
  /** Where a hub keeps a room: a Durable Object's storage, or a map in `ta relay`. Values are structured-cloned. */
3
4
  export interface RelayHubStorage {
@@ -83,6 +84,8 @@ export declare class RelayHub {
83
84
  second: number;
84
85
  count: number;
85
86
  }>;
87
+ /** What is said in the room (docs/design/ta-chat-history.md 5). */
88
+ protected readonly log: RelayHubLog;
86
89
  constructor(storage: RelayHubStorage,
87
90
  /** The open connections of the room. */
88
91
  sockets: () => RelayHubSocket[], now?: () => number);
@@ -1,4 +1,5 @@
1
1
  import { RELAY_HUB_MAX_ADMINS } from './relay-hub-charter.js';
2
+ import { RelayHubLog } from './relay-hub-log.js';
2
3
  import { RelayKeys } from './relay-identity.js';
3
4
  import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_INVITE_MAX_DAYS, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
4
5
  /** Close codes of a hub. */
@@ -35,6 +36,7 @@ export class RelayHub {
35
36
  this.sockets = sockets;
36
37
  this.now = now;
37
38
  this.rates = new Map();
39
+ this.log = new RelayHubLog(storage, now);
38
40
  }
39
41
  async receive(socket, text) {
40
42
  if (text === RELAY_PING) {
@@ -233,6 +235,19 @@ export class RelayHub {
233
235
  return this.forward(socket, member, request);
234
236
  case 'set':
235
237
  return this.set(socket, member, meta, request);
238
+ case 'post':
239
+ case 'sync':
240
+ case 'ack': {
241
+ const { reply, broadcast } = await this.log.receive(member, request, Object.keys(members));
242
+ if (reply) {
243
+ this.send(socket, reply);
244
+ }
245
+ if (broadcast) {
246
+ await this.touch(meta);
247
+ this.broadcast(broadcast, socket);
248
+ }
249
+ return;
250
+ }
236
251
  case 'leave':
237
252
  if (owner) {
238
253
  return this.closeRoom();
@@ -240,6 +255,7 @@ export class RelayHub {
240
255
  delete members[member];
241
256
  await this.storage.put('members', members);
242
257
  await this.unmake(meta, member);
258
+ await this.log.gone(member);
243
259
  for (const other of this.members(member)) {
244
260
  other.close(RelayHubCloseCode.REMOVED, 'Left the room.');
245
261
  }
@@ -304,6 +320,7 @@ export class RelayHub {
304
320
  delete members[request.member];
305
321
  await this.storage.put('members', members);
306
322
  await this.unmake(meta, request.member);
323
+ await this.log.gone(request.member);
307
324
  for (const other of this.members(request.member)) {
308
325
  this.refuse(other, 'auth', 'Removed by the owner.', RelayHubCloseCode.REMOVED);
309
326
  }
@@ -377,6 +394,7 @@ export class RelayHub {
377
394
  }
378
395
  members[request.ask] = { hash: pending.hash, at: this.now() };
379
396
  await this.storage.put('members', members);
397
+ await this.log.admitted(request.ask, request.history);
380
398
  asks[request.ask] = { ...pending, answer: request.answer };
381
399
  await this.storage.put('asks', asks);
382
400
  await this.setRoster(socket, request.roster);
@@ -16,6 +16,7 @@ export declare abstract class RelayHubsAdmins extends RelayHubsIdentity {
16
16
  members?: number;
17
17
  admins?: string[];
18
18
  rules?: RelayHubRules;
19
+ history?: number;
19
20
  }): Promise<RelayRoomInfo>;
20
21
  /** The members it lets in at most: the owner's own choice, else the one its charter says. */
21
22
  protected capacityOf(live: Live): number;
@@ -1,7 +1,7 @@
1
1
  import { RELAY_FRIENDS_KIND } from './relay-friends.js';
2
2
  import { RELAY_HUB_MAX_ADMINS, RelayHubCharterUtil } from './relay-hub-charter.js';
3
3
  import { RelayHubsIdentity } from './relay-hubs-identity.js';
4
- import { RELAY_FRIENDS_MAX, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
4
+ import { RELAY_FRIENDS_MAX, RELAY_HUB_HISTORY_MAX, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
5
5
  /**
6
6
  * The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
7
7
  * signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
@@ -13,8 +13,8 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
13
13
  * Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
14
14
  */
15
15
  async configure(roomId, options) {
16
- var _a;
17
- var _b, _c;
16
+ var _a, _b;
17
+ var _c, _d;
18
18
  const live = this.owned(roomId);
19
19
  let capacity = live.record.capacity;
20
20
  if (options.members !== undefined) {
@@ -29,11 +29,14 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
29
29
  }
30
30
  this.save();
31
31
  }
32
- if (options.admins === undefined && options.rules === undefined && (options.members === undefined || !live.charter)) {
32
+ const history = options.history === undefined
33
+ ? (_a = live.charter) === null || _a === void 0 ? void 0 : _a.history
34
+ : Math.min(Math.max(Math.round(Number(options.history) || 0), 0), RELAY_HUB_HISTORY_MAX) || undefined;
35
+ if (options.admins === undefined && options.rules === undefined && options.history === undefined && (options.members === undefined || !live.charter)) {
33
36
  return this.info(live);
34
37
  }
35
- const before = (_b = live.charter) !== null && _b !== void 0 ? _b : { room: live.record.id, v: 0, admins: [], rules: {} };
36
- const roster = (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : [];
38
+ const before = (_c = live.charter) !== null && _c !== void 0 ? _c : { room: live.record.id, v: 0, admins: [], rules: {} };
39
+ const roster = (_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : [];
37
40
  const admins = options.admins === undefined
38
41
  ? before.admins
39
42
  : [...new Set(options.admins.map(String))]
@@ -47,6 +50,7 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
47
50
  admins,
48
51
  rules: options.rules === undefined ? before.rules : RelayHubCharterUtil.rules(options.rules),
49
52
  ...(capacity ? { capacity } : {}),
53
+ ...(history ? { history } : {}),
50
54
  };
51
55
  await this.ready(live);
52
56
  await this.writeRoster(live, same => same, charter);