@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.
- package/esm/common/index.d.ts +2 -0
- package/esm/common/index.js +2 -0
- package/esm/common/relay-blobs.d.ts +36 -2
- package/esm/common/relay-blobs.js +33 -5
- package/esm/common/relay-chat-store.d.ts +69 -0
- package/esm/common/relay-chat-store.js +50 -0
- package/esm/common/relay-hub-charter.d.ts +2 -0
- package/esm/common/relay-hub-charter.js +3 -0
- package/esm/common/relay-hub-log.d.ts +65 -0
- package/esm/common/relay-hub-log.js +147 -0
- package/esm/common/relay-hub.d.ts +3 -0
- package/esm/common/relay-hub.js +18 -0
- package/esm/common/relay-hubs-admins.d.ts +1 -0
- package/esm/common/relay-hubs-admins.js +10 -6
- package/esm/common/relay-hubs-chats.d.ts +73 -0
- package/esm/common/relay-hubs-chats.js +367 -0
- package/esm/common/relay-hubs-connection.d.ts +3 -1
- package/esm/common/relay-hubs-connection.js +7 -0
- package/esm/common/relay-hubs-protocol.d.ts +16 -0
- package/esm/common/relay-hubs.d.ts +2 -2
- package/esm/common/relay-hubs.js +14 -11
- package/esm/common/relay-info.d.ts +23 -1
- package/esm/common/relay-info.js +25 -2
- package/esm/common/relay-protocol.d.ts +60 -1
- package/esm/common/relay-protocol.js +15 -0
- package/esm/common/relay-room-chat.d.ts +126 -4
- package/esm/common/relay-room-chat.js +230 -6
- package/esm/guest/guest-i18n.d.ts +2 -0
- package/esm/guest/guest-i18n.js +8 -0
- package/esm/guest/guest.js +8 -5
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/relay-chat-files.d.ts +52 -0
- package/esm/node/relay-chat-files.js +224 -0
- package/esm/node/relay-hub-files.d.ts +11 -1
- package/esm/node/relay-hub-files.js +8 -1
- package/esm/node/relay-server.d.ts +9 -1
- package/esm/node/relay-server.js +29 -18
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.js +2 -0
- package/lib/common/relay-blobs.d.ts +36 -2
- package/lib/common/relay-blobs.js +34 -6
- package/lib/common/relay-chat-store.d.ts +70 -0
- package/lib/common/relay-chat-store.js +54 -0
- package/lib/common/relay-hub-charter.d.ts +2 -0
- package/lib/common/relay-hub-charter.js +3 -0
- package/lib/common/relay-hub-log.d.ts +66 -0
- package/lib/common/relay-hub-log.js +151 -0
- package/lib/common/relay-hub.d.ts +3 -0
- package/lib/common/relay-hub.js +18 -0
- package/lib/common/relay-hubs-admins.d.ts +1 -0
- package/lib/common/relay-hubs-admins.js +9 -5
- package/lib/common/relay-hubs-chats.d.ts +74 -0
- package/lib/common/relay-hubs-chats.js +371 -0
- package/lib/common/relay-hubs-connection.d.ts +3 -1
- package/lib/common/relay-hubs-connection.js +7 -0
- package/lib/common/relay-hubs-protocol.d.ts +16 -0
- package/lib/common/relay-hubs.d.ts +2 -2
- package/lib/common/relay-hubs.js +14 -11
- package/lib/common/relay-info.d.ts +23 -1
- package/lib/common/relay-info.js +26 -3
- package/lib/common/relay-protocol.d.ts +60 -1
- package/lib/common/relay-protocol.js +16 -1
- package/lib/common/relay-room-chat.d.ts +126 -4
- package/lib/common/relay-room-chat.js +231 -7
- package/lib/guest/guest-i18n.d.ts +2 -0
- package/lib/guest/guest-i18n.js +8 -0
- package/lib/guest/guest.js +7 -4
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/relay-chat-files.d.ts +53 -0
- package/lib/node/relay-chat-files.js +228 -0
- package/lib/node/relay-hub-files.d.ts +11 -1
- package/lib/node/relay-hub-files.js +8 -1
- package/lib/node/relay-server.d.ts +9 -1
- package/lib/node/relay-server.js +27 -16
- package/package.json +3 -3
- package/viewer/join.html +4 -4
- package/viewer/page.html +10 -10
- package/viewer/room.html +15 -15
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
/** The bytes of what is kept of a room, its lines and their state; with `before`, of what `clear` would take. */
|
|
34
|
+
usage(room: string, before?: number): Promise<number>;
|
|
35
|
+
/**
|
|
36
|
+
* What was said in a room goes from this computer (docs/design/ta-chat-history.md 10): all of it, or with `before`
|
|
37
|
+
* what was said before then (a month at a time where it is kept by the month). Where the log of the room is read up to
|
|
38
|
+
* stays: what went is not read again from the relay.
|
|
39
|
+
*/
|
|
40
|
+
clear(room: string, before?: number): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
export declare namespace RelayChatStoreUtil {
|
|
43
|
+
/** The page of `lines` (the earliest first) that `read` answers. */
|
|
44
|
+
function page(lines: readonly RelayChatLine[], options: {
|
|
45
|
+
before?: string;
|
|
46
|
+
limit: number;
|
|
47
|
+
}): {
|
|
48
|
+
lines: RelayChatLine[];
|
|
49
|
+
more: boolean;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** What is said, in memory: a guest's, and tests'. */
|
|
53
|
+
export declare class RelayChatMemory implements RelayChatStore {
|
|
54
|
+
protected readonly lines: Map<string, RelayChatLine[]>;
|
|
55
|
+
protected readonly states: Map<string, RelayChatState>;
|
|
56
|
+
read(room: string, options: {
|
|
57
|
+
before?: string;
|
|
58
|
+
limit: number;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
lines: RelayChatLine[];
|
|
61
|
+
more: boolean;
|
|
62
|
+
}>;
|
|
63
|
+
append(room: string, lines: RelayChatLine[]): Promise<void>;
|
|
64
|
+
state(room: string): Promise<RelayChatState | undefined>;
|
|
65
|
+
setState(room: string, state: RelayChatState): Promise<void>;
|
|
66
|
+
forget(room: string): Promise<void>;
|
|
67
|
+
usage(room: string, before?: number): Promise<number>;
|
|
68
|
+
clear(room: string, before?: number): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=relay-chat-store.d.ts.map
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
async usage(room, before) {
|
|
43
|
+
var _a;
|
|
44
|
+
const lines = (_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : [];
|
|
45
|
+
return new TextEncoder().encode(JSON.stringify(before === undefined ? lines : lines.filter(line => line.at < before))).length;
|
|
46
|
+
}
|
|
47
|
+
async clear(room, before) {
|
|
48
|
+
var _a;
|
|
49
|
+
const lines = (_a = this.lines.get(room)) !== null && _a !== void 0 ? _a : [];
|
|
50
|
+
this.lines.set(room, before === undefined ? [] : lines.filter(line => line.at >= before));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.RelayChatMemory = RelayChatMemory;
|
|
54
|
+
//# 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
|
|
@@ -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);
|
package/lib/common/relay-hub.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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 = (
|
|
39
|
-
const roster = (
|
|
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,74 @@
|
|
|
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 bytes kept on this computer of what was said in each room this Ta is in; with `before`, what clearChat would free. */
|
|
43
|
+
chatUsage(before?: number): Promise<Record<string, number>>;
|
|
44
|
+
/**
|
|
45
|
+
* What was said in a room goes from this computer: all of it, or what was said before `before` (a month at a time).
|
|
46
|
+
* The others keep theirs; the pages read the chat again.
|
|
47
|
+
*/
|
|
48
|
+
clearChat(roomId: string, before?: number): Promise<void>;
|
|
49
|
+
/** The room was left: what was said there goes from this computer. */
|
|
50
|
+
protected forgetChat(roomId: string): Promise<void>;
|
|
51
|
+
protected chatOf(live: Live): Promise<LiveChat>;
|
|
52
|
+
/**
|
|
53
|
+
* The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
|
|
54
|
+
* when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
|
|
55
|
+
*/
|
|
56
|
+
protected migrate(live: Live, chat: LiveChat): Promise<void>;
|
|
57
|
+
/** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
|
|
58
|
+
protected took(live: Live, chat: LiveChat, items: readonly RelayHubLogItem[]): Promise<void>;
|
|
59
|
+
/** Sends the lines waiting, one after another, while the room is reached. */
|
|
60
|
+
protected flush(live: Live, chat: LiveChat): Promise<void>;
|
|
61
|
+
protected post(live: Live, line: RelayChatLine): Promise<{
|
|
62
|
+
ok: boolean;
|
|
63
|
+
v: number;
|
|
64
|
+
error?: RelayHubFailure;
|
|
65
|
+
}>;
|
|
66
|
+
/** 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. */
|
|
67
|
+
protected posted(live: Live, chat: LiveChat, line: RelayChatLine, seq: number): Promise<boolean>;
|
|
68
|
+
protected refused(live: Live, chat: LiveChat, line: RelayChatLine): Promise<void>;
|
|
69
|
+
protected sync(live: Live, chat: LiveChat): void;
|
|
70
|
+
protected ack(live: Live, chat: LiveChat): void;
|
|
71
|
+
protected remember(chat: LiveChat, id: string): void;
|
|
72
|
+
}
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=relay-hubs-chats.d.ts.map
|