@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
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayHubLog = void 0;
4
+ const relay_protocol_1 = require("./relay-protocol");
5
+ const DAY_MS = 24 * 60 * 60 * 1000;
6
+ /** Messages let go of at most at once: the rest go the next time. */
7
+ const PRUNE_STEP = 256;
8
+ const META = 'log';
9
+ const MEMBERS = 'logm';
10
+ const ITEM = 'm:';
11
+ const MID = 'i:';
12
+ const MID_FORMAT = /^[A-Za-z0-9_-]{8,40}$/;
13
+ /**
14
+ * The log of a room at the relay (docs/design/ta-chat-history.md 5): what members post, numbered in the order it came,
15
+ * sealed; a member reads it from where it is (`sync`) and says how far it has it (`ack`). Posting the same line again
16
+ * (its id) is the same message. Kept while some member lacks it, RELAY_HUB_LOG_KEEP_DAYS once all have it,
17
+ * RELAY_HUB_LOG_MAX_DAYS at most, and within RELAY_HUB_LOG_MAX and RELAY_HUB_LOG_CHARS, the oldest going first.
18
+ */
19
+ class RelayHubLog {
20
+ constructor(storage, now = Date.now) {
21
+ this.storage = storage;
22
+ this.now = now;
23
+ }
24
+ /** What a member asked of the log: the answer to it, and what the others hear. */
25
+ async receive(member, request, members) {
26
+ switch (request.t) {
27
+ case 'post': {
28
+ const id = Number(request.id);
29
+ if (typeof request.d !== 'string' || !MID_FORMAT.test(String(request.mid))) {
30
+ return { reply: { t: 'done', id, ok: false, v: 0, error: 'invalid' } };
31
+ }
32
+ if (request.d.length > relay_protocol_1.RELAY_HUB_MAX_VALUE) {
33
+ return { reply: { t: 'done', id, ok: false, v: 0, error: 'large' } };
34
+ }
35
+ const { item, fresh } = await this.post(member, request.mid, request.d, members);
36
+ return { reply: { t: 'done', id, ok: true, v: item.s }, ...(fresh ? { broadcast: { t: 'post', item } } : {}) };
37
+ }
38
+ case 'sync':
39
+ return { reply: { t: 'log', ...(await this.read(member, Number(request.after) || 0, Number(request.limit) || relay_protocol_1.RELAY_HUB_SYNC_PAGE)) } };
40
+ case 'ack':
41
+ await this.ack(member, Number(request.seq) || 0, members);
42
+ return {};
43
+ }
44
+ }
45
+ async meta() {
46
+ var _a;
47
+ return (_a = (await this.storage.get(META))) !== null && _a !== void 0 ? _a : { first: 1, last: 0, chars: 0 };
48
+ }
49
+ /** Adds a message, numbered after the last; one of the same id already there is answered with its number. */
50
+ async post(member, mid, d, members) {
51
+ const known = await this.storage.get(MID + mid);
52
+ const kept = known === undefined ? undefined : await this.storage.get(RelayHubLog.key(known));
53
+ if (kept && kept.m === member) {
54
+ return { item: RelayHubLog.item(kept), fresh: false };
55
+ }
56
+ const meta = await this.meta();
57
+ const item = { s: meta.last + 1, m: member, d, at: this.now() };
58
+ await this.storage.put(RelayHubLog.key(item.s), { ...item, mid });
59
+ await this.storage.put(MID + mid, item.s);
60
+ meta.last = item.s;
61
+ meta.chars += d.length;
62
+ await this.prune(meta, members);
63
+ return { item, fresh: true };
64
+ }
65
+ /** A page of the log after `after`, from the member's floor on: `first` past `after + 1` tells what is gone. */
66
+ async read(member, after, limit) {
67
+ var _a;
68
+ var _b;
69
+ const meta = await this.meta();
70
+ const floor = (_b = (_a = (await this.members())[member]) === null || _a === void 0 ? void 0 : _a.floor) !== null && _b !== void 0 ? _b : 0;
71
+ const start = Math.max(after + 1, floor + 1, meta.first);
72
+ const end = Math.min(meta.last, start + Math.min(Math.max(limit, 1), relay_protocol_1.RELAY_HUB_SYNC_PAGE) - 1);
73
+ const items = [];
74
+ for (let seq = start; seq <= end; seq++) {
75
+ const kept = await this.storage.get(RelayHubLog.key(seq));
76
+ if (kept) {
77
+ items.push(RelayHubLog.item(kept));
78
+ }
79
+ }
80
+ return { items, first: Math.max(meta.first, floor + 1), last: meta.last, more: end < meta.last };
81
+ }
82
+ async ack(member, seq, members) {
83
+ var _a;
84
+ var _b;
85
+ const all = await this.members();
86
+ const meta = await this.meta();
87
+ const seen = Math.min(Math.floor(seq), meta.last);
88
+ if (!(seen > ((_b = (_a = all[member]) === null || _a === void 0 ? void 0 : _a.seen) !== null && _b !== void 0 ? _b : 0))) {
89
+ return;
90
+ }
91
+ all[member] = { ...all[member], seen };
92
+ await this.storage.put(MEMBERS, all);
93
+ await this.prune(meta, members, all);
94
+ }
95
+ /** A newcomer: it reads the log from `history` messages before now on (none by default, RELAY_HUB_HISTORY_MAX at most). */
96
+ async admitted(member, history) {
97
+ const meta = await this.meta();
98
+ const back = Math.min(Math.max(Math.floor(Number(history) || 0), 0), relay_protocol_1.RELAY_HUB_HISTORY_MAX);
99
+ const all = await this.members();
100
+ all[member] = { floor: Math.max(meta.last - back, 0), seen: meta.last };
101
+ await this.storage.put(MEMBERS, all);
102
+ }
103
+ /** A member left or was taken out: it holds nothing back any more. */
104
+ async gone(member) {
105
+ const all = await this.members();
106
+ if (all[member]) {
107
+ delete all[member];
108
+ await this.storage.put(MEMBERS, all);
109
+ }
110
+ }
111
+ /** Lets go of the oldest messages past what is kept, then writes what the log knows of itself. */
112
+ async prune(meta, members, all) {
113
+ const now = this.now();
114
+ let seenByAll;
115
+ for (let step = 0; step < PRUNE_STEP && meta.first <= meta.last; step++) {
116
+ const kept = await this.storage.get(RelayHubLog.key(meta.first));
117
+ const count = meta.last - meta.first + 1;
118
+ let drop = !kept || count > relay_protocol_1.RELAY_HUB_LOG_MAX || meta.chars > relay_protocol_1.RELAY_HUB_LOG_CHARS || now - kept.at > relay_protocol_1.RELAY_HUB_LOG_MAX_DAYS * DAY_MS;
119
+ if (!drop && kept && now - kept.at > relay_protocol_1.RELAY_HUB_LOG_KEEP_DAYS * DAY_MS) {
120
+ if (seenByAll === undefined) {
121
+ const known = all !== null && all !== void 0 ? all : (await this.members());
122
+ 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);
123
+ }
124
+ drop = kept.s <= seenByAll;
125
+ }
126
+ if (!drop) {
127
+ break;
128
+ }
129
+ if (kept) {
130
+ meta.chars = Math.max(meta.chars - kept.d.length, 0);
131
+ await this.storage.delete(MID + kept.mid);
132
+ }
133
+ await this.storage.delete(RelayHubLog.key(meta.first));
134
+ meta.first++;
135
+ }
136
+ await this.storage.put(META, meta);
137
+ }
138
+ async members() {
139
+ var _a;
140
+ return (_a = (await this.storage.get(MEMBERS))) !== null && _a !== void 0 ? _a : {};
141
+ }
142
+ /** Numbers padded, so that keys sort as they come. */
143
+ static key(seq) {
144
+ return ITEM + String(seq).padStart(12, '0');
145
+ }
146
+ static item({ s, m, d, at }) {
147
+ return { s, m, d, at };
148
+ }
149
+ }
150
+ exports.RelayHubLog = RelayHubLog;
151
+ //# sourceMappingURL=relay-hub-log.js.map
@@ -1,3 +1,4 @@
1
+ import { RelayHubLog } from './relay-hub-log';
1
2
  import { type RelayHubAsk, type RelayHubEvent, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol';
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);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelayHub = exports.RelayHubCloseCode = void 0;
4
4
  const relay_hub_charter_1 = require("./relay-hub-charter");
5
+ const relay_hub_log_1 = require("./relay-hub-log");
5
6
  const relay_identity_1 = require("./relay-identity");
6
7
  const relay_protocol_1 = require("./relay-protocol");
7
8
  /** Close codes of a hub. */
@@ -38,6 +39,7 @@ class RelayHub {
38
39
  this.sockets = sockets;
39
40
  this.now = now;
40
41
  this.rates = new Map();
42
+ this.log = new relay_hub_log_1.RelayHubLog(storage, now);
41
43
  }
42
44
  async receive(socket, text) {
43
45
  if (text === relay_protocol_1.RELAY_PING) {
@@ -236,6 +238,19 @@ class RelayHub {
236
238
  return this.forward(socket, member, request);
237
239
  case 'set':
238
240
  return this.set(socket, member, meta, request);
241
+ case 'post':
242
+ case 'sync':
243
+ case 'ack': {
244
+ const { reply, broadcast } = await this.log.receive(member, request, Object.keys(members));
245
+ if (reply) {
246
+ this.send(socket, reply);
247
+ }
248
+ if (broadcast) {
249
+ await this.touch(meta);
250
+ this.broadcast(broadcast, socket);
251
+ }
252
+ return;
253
+ }
239
254
  case 'leave':
240
255
  if (owner) {
241
256
  return this.closeRoom();
@@ -243,6 +258,7 @@ class RelayHub {
243
258
  delete members[member];
244
259
  await this.storage.put('members', members);
245
260
  await this.unmake(meta, member);
261
+ await this.log.gone(member);
246
262
  for (const other of this.members(member)) {
247
263
  other.close(RelayHubCloseCode.REMOVED, 'Left the room.');
248
264
  }
@@ -307,6 +323,7 @@ class RelayHub {
307
323
  delete members[request.member];
308
324
  await this.storage.put('members', members);
309
325
  await this.unmake(meta, request.member);
326
+ await this.log.gone(request.member);
310
327
  for (const other of this.members(request.member)) {
311
328
  this.refuse(other, 'auth', 'Removed by the owner.', RelayHubCloseCode.REMOVED);
312
329
  }
@@ -380,6 +397,7 @@ class RelayHub {
380
397
  }
381
398
  members[request.ask] = { hash: pending.hash, at: this.now() };
382
399
  await this.storage.put('members', members);
400
+ await this.log.admitted(request.ask, request.history);
383
401
  asks[request.ask] = { ...pending, answer: request.answer };
384
402
  await this.storage.put('asks', asks);
385
403
  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;
@@ -16,8 +16,8 @@ class RelayHubsAdmins extends relay_hubs_identity_1.RelayHubsIdentity {
16
16
  * Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
17
17
  */
18
18
  async configure(roomId, options) {
19
- var _a;
20
- var _b, _c;
19
+ var _a, _b;
20
+ var _c, _d;
21
21
  const live = this.owned(roomId);
22
22
  let capacity = live.record.capacity;
23
23
  if (options.members !== undefined) {
@@ -32,11 +32,14 @@ class RelayHubsAdmins extends relay_hubs_identity_1.RelayHubsIdentity {
32
32
  }
33
33
  this.save();
34
34
  }
35
- if (options.admins === undefined && options.rules === undefined && (options.members === undefined || !live.charter)) {
35
+ const history = options.history === undefined
36
+ ? (_a = live.charter) === null || _a === void 0 ? void 0 : _a.history
37
+ : Math.min(Math.max(Math.round(Number(options.history) || 0), 0), relay_protocol_1.RELAY_HUB_HISTORY_MAX) || undefined;
38
+ if (options.admins === undefined && options.rules === undefined && options.history === undefined && (options.members === undefined || !live.charter)) {
36
39
  return this.info(live);
37
40
  }
38
- const before = (_b = live.charter) !== null && _b !== void 0 ? _b : { room: live.record.id, v: 0, admins: [], rules: {} };
39
- const roster = (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : [];
41
+ const before = (_c = live.charter) !== null && _c !== void 0 ? _c : { room: live.record.id, v: 0, admins: [], rules: {} };
42
+ const roster = (_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : [];
40
43
  const admins = options.admins === undefined
41
44
  ? before.admins
42
45
  : [...new Set(options.admins.map(String))]
@@ -50,6 +53,7 @@ class RelayHubsAdmins extends relay_hubs_identity_1.RelayHubsIdentity {
50
53
  admins,
51
54
  rules: options.rules === undefined ? before.rules : relay_hub_charter_1.RelayHubCharterUtil.rules(options.rules),
52
55
  ...(capacity ? { capacity } : {}),
56
+ ...(history ? { history } : {}),
53
57
  };
54
58
  await this.ready(live);
55
59
  await this.writeRoster(live, same => same, charter);
@@ -0,0 +1,67 @@
1
+ import { type RelayChatState, type RelayChatStore } from './relay-chat-store';
2
+ import { RelayHubsAdmins } from './relay-hubs-admins';
3
+ import { type Live } from './relay-hubs-live';
4
+ import { type RelayHubEvent, type RelayHubFailure, type RelayHubLogItem } from './relay-protocol';
5
+ import { type RelayChatLine, type RelayChatLineExtra } from './relay-room-chat';
6
+ /** The log of a room as this Ta follows it while it runs. */
7
+ declare class LiveChat {
8
+ state?: RelayChatState;
9
+ loading?: Promise<RelayChatState>;
10
+ /** A page of the log asked for, not come yet. */
11
+ syncing: boolean;
12
+ sending: boolean;
13
+ acked: number;
14
+ ackTimer?: ReturnType<typeof setTimeout>;
15
+ lastAt: number;
16
+ readonly ids: Set<string>;
17
+ /** What changes the state, one after another. */
18
+ protected chain: Promise<unknown>;
19
+ run<T>(work: () => Promise<T>): Promise<T>;
20
+ }
21
+ /**
22
+ * The part of {@link RelayHubs} that keeps what is said in the rooms (docs/design/ta-chat-history.md 4–6): the log of
23
+ * each room read from the relay after the last number taken, kept on this computer (RelayChatStore); lines said go
24
+ * into an outbox first and to the relay once it is reached, the same line again being the same message there, so
25
+ * nothing is lost nor said twice; a number missed is read again; the relay is told how far this Ta has it.
26
+ */
27
+ export declare abstract class RelayHubsChats extends RelayHubsAdmins {
28
+ protected readonly chats: RelayChatStore;
29
+ protected readonly following: Map<string, LiveChat>;
30
+ /** The latest lines of a room kept here, before the line `before` when given; those not sent yet last. */
31
+ readChat(roomId: string, options?: {
32
+ before?: string;
33
+ limit?: number;
34
+ }): Promise<{
35
+ lines: RelayChatLine[];
36
+ more: boolean;
37
+ }>;
38
+ /** Says it as this Ta in a room: kept at once, sent once the relay is reached. The line, `pending` till then. */
39
+ say(roomId: string, text: string, extra?: RelayChatLineExtra): Promise<RelayChatLine | undefined>;
40
+ /** What the hub of a room says of its log; `synced` once connected, to read what came while away. */
41
+ protected heardLog(live: Live, event: RelayHubEvent): Promise<void>;
42
+ /** The room was left: what was said there goes from this computer. */
43
+ protected forgetChat(roomId: string): Promise<void>;
44
+ protected chatOf(live: Live): Promise<LiveChat>;
45
+ /**
46
+ * The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
47
+ * when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
48
+ */
49
+ protected migrate(live: Live, chat: LiveChat): Promise<void>;
50
+ /** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
51
+ protected took(live: Live, chat: LiveChat, items: readonly RelayHubLogItem[]): Promise<void>;
52
+ /** Sends the lines waiting, one after another, while the room is reached. */
53
+ protected flush(live: Live, chat: LiveChat): Promise<void>;
54
+ protected post(live: Live, line: RelayChatLine): Promise<{
55
+ ok: boolean;
56
+ v: number;
57
+ error?: RelayHubFailure;
58
+ }>;
59
+ /** The relay took a line of this Ta as number `seq`: kept when it is the next, else read with what came between. Whether to go on. */
60
+ protected posted(live: Live, chat: LiveChat, line: RelayChatLine, seq: number): Promise<boolean>;
61
+ protected refused(live: Live, chat: LiveChat, line: RelayChatLine): Promise<void>;
62
+ protected sync(live: Live, chat: LiveChat): void;
63
+ protected ack(live: Live, chat: LiveChat): void;
64
+ protected remember(chat: LiveChat, id: string): void;
65
+ }
66
+ export {};
67
+ //# sourceMappingURL=relay-hubs-chats.d.ts.map