@celljs/ta-relay 4.21.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.
- package/esm/common/index.d.ts +3 -0
- package/esm/common/index.js +3 -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 +59 -0
- package/esm/common/relay-chat-store.js +40 -0
- package/esm/common/relay-hub-charter.d.ts +7 -1
- package/esm/common/relay-hub-charter.js +6 -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 +66 -0
- package/esm/common/relay-hubs-chats.js +342 -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 +14 -0
- package/esm/common/relay-hubs.d.ts +5 -2
- package/esm/common/relay-hubs.js +31 -13
- 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-app.d.ts +106 -0
- package/esm/common/relay-room-app.js +171 -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 +5 -0
- package/esm/guest/guest-i18n.js +20 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -0
- package/esm/guest/guest.js +84 -11
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/relay-chat-files.d.ts +46 -0
- package/esm/node/relay-chat-files.js +199 -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 +7 -1
- package/esm/node/relay-server.js +23 -13
- package/lib/common/index.d.ts +3 -0
- package/lib/common/index.js +3 -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 +60 -0
- package/lib/common/relay-chat-store.js +44 -0
- package/lib/common/relay-hub-charter.d.ts +7 -1
- package/lib/common/relay-hub-charter.js +7 -1
- 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 +67 -0
- package/lib/common/relay-hubs-chats.js +346 -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 +14 -0
- package/lib/common/relay-hubs.d.ts +5 -2
- package/lib/common/relay-hubs.js +30 -12
- 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-app.d.ts +107 -0
- package/lib/common/relay-room-app.js +176 -0
- 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 +5 -0
- package/lib/guest/guest-i18n.js +20 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -0
- package/lib/guest/guest.js +83 -10
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/relay-chat-files.d.ts +47 -0
- package/lib/node/relay-chat-files.js +203 -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 +7 -1
- package/lib/node/relay-server.js +21 -11
- package/package.json +3 -3
- package/viewer/join.html +17 -6
- package/viewer/page.html +10 -10
- package/viewer/room.html +26 -15
|
@@ -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
|
-
|
|
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
|
-
|
|
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 +
|
|
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 >
|
|
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 >
|
|
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
|
|
@@ -7,9 +7,13 @@ export interface RelayHubAdmin {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* The rules of a room, its owner's for all (docs/design/ta-link.md 8): `approve`, friends invited ask as anyone else;
|
|
10
|
-
*
|
|
10
|
+
* `guests` (RELAY_GUESTS_RULE), those asking from a browser come in without asking; the others are the packages' that
|
|
11
|
+
* read them (ta-rooms: `agents`, `doubles`, `tasks`), each false to forbid.
|
|
11
12
|
*/
|
|
12
13
|
export type RelayHubRules = Record<string, boolean>;
|
|
14
|
+
/** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
|
|
15
|
+
export declare const RELAY_GUESTS_RULE = "guests";
|
|
16
|
+
export declare const RELAY_GUESTS_PER_HOUR = 30;
|
|
13
17
|
/** What the owner of a room signs for all: who its admins are, its rules, how many it lets in. `v` grows with each change. */
|
|
14
18
|
export interface RelayHubCharterBody {
|
|
15
19
|
room: string;
|
|
@@ -17,6 +21,8 @@ export interface RelayHubCharterBody {
|
|
|
17
21
|
admins: RelayHubAdmin[];
|
|
18
22
|
rules: RelayHubRules;
|
|
19
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;
|
|
20
26
|
}
|
|
21
27
|
/** The charter as the roster carries it: `p` its JSON, `s` the owner's signature of it. */
|
|
22
28
|
export interface RelayHubCharter {
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RelayHubCharterUtil = exports.RELAY_HUB_MAX_ADMINS = void 0;
|
|
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");
|
|
7
|
+
/** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
|
|
8
|
+
exports.RELAY_GUESTS_RULE = 'guests';
|
|
9
|
+
exports.RELAY_GUESTS_PER_HOUR = 30;
|
|
6
10
|
/** Admins of a room at most. */
|
|
7
11
|
exports.RELAY_HUB_MAX_ADMINS = 8;
|
|
8
12
|
const MAX_RULES = 16;
|
|
@@ -39,6 +43,7 @@ var RelayHubCharterUtil;
|
|
|
39
43
|
return undefined;
|
|
40
44
|
}
|
|
41
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);
|
|
42
47
|
return {
|
|
43
48
|
room,
|
|
44
49
|
v: Number(body.v),
|
|
@@ -48,6 +53,7 @@ var RelayHubCharterUtil;
|
|
|
48
53
|
.map(admin => ({ member: admin.member, id: admin.id })),
|
|
49
54
|
rules: rules(body.rules),
|
|
50
55
|
...(capacity >= 2 ? { capacity } : {}),
|
|
56
|
+
...(history > 0 ? { history } : {}),
|
|
51
57
|
};
|
|
52
58
|
}
|
|
53
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);
|