@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,73 @@
|
|
|
1
|
+
import { type RelayChatState, type RelayChatStore } from './relay-chat-store.js';
|
|
2
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
3
|
+
import { type Live } from './relay-hubs-live.js';
|
|
4
|
+
import { type RelayHubEvent, type RelayHubFailure, type RelayHubLogItem } from './relay-protocol.js';
|
|
5
|
+
import { type RelayChatLine, type RelayChatLineExtra } from './relay-room-chat.js';
|
|
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 {};
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { RelayChatMemory } from './relay-chat-store.js';
|
|
2
|
+
import { RelayHubUtil } from './relay-hub-util.js';
|
|
3
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
4
|
+
import { OPEN, READY_MS } from './relay-hubs-live.js';
|
|
5
|
+
import { RELAY_HUB_SYNC_PAGE } from './relay-protocol.js';
|
|
6
|
+
import { RELAY_CHAT_KEY, RELAY_CHAT_TEXT_MAX, RelayRoomChatUtil } from './relay-room-chat.js';
|
|
7
|
+
/** Ids of lines taken kept to know one again: the relay could serve an old one anew. */
|
|
8
|
+
const IDS_KEPT = 2000;
|
|
9
|
+
/** How soon after the last line taken the relay is told how far this Ta has the log. */
|
|
10
|
+
const ACK_MS = 2000;
|
|
11
|
+
/** The log of a room as this Ta follows it while it runs. */
|
|
12
|
+
class LiveChat {
|
|
13
|
+
constructor() {
|
|
14
|
+
/** A page of the log asked for, not come yet. */
|
|
15
|
+
this.syncing = false;
|
|
16
|
+
this.sending = false;
|
|
17
|
+
this.acked = 0;
|
|
18
|
+
this.lastAt = 0;
|
|
19
|
+
this.ids = new Set();
|
|
20
|
+
/** What changes the state, one after another. */
|
|
21
|
+
this.chain = Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
run(work) {
|
|
24
|
+
const done = this.chain.then(work);
|
|
25
|
+
this.chain = done.catch(() => undefined);
|
|
26
|
+
return done;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The part of {@link RelayHubs} that keeps what is said in the rooms (docs/design/ta-chat-history.md 4–6): the log of
|
|
31
|
+
* each room read from the relay after the last number taken, kept on this computer (RelayChatStore); lines said go
|
|
32
|
+
* into an outbox first and to the relay once it is reached, the same line again being the same message there, so
|
|
33
|
+
* nothing is lost nor said twice; a number missed is read again; the relay is told how far this Ta has it.
|
|
34
|
+
*/
|
|
35
|
+
export class RelayHubsChats extends RelayHubsAdmins {
|
|
36
|
+
constructor() {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
var _c, _d;
|
|
39
|
+
super(...arguments);
|
|
40
|
+
this.chats = (_d = (_c = this.options.chats) !== null && _c !== void 0 ? _c : (_b = (_a = this.options.store).chats) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _d !== void 0 ? _d : new RelayChatMemory();
|
|
41
|
+
this.following = new Map();
|
|
42
|
+
}
|
|
43
|
+
/** The latest lines of a room kept here, before the line `before` when given; those not sent yet last. */
|
|
44
|
+
async readChat(roomId, options = {}) {
|
|
45
|
+
var _a;
|
|
46
|
+
var _b;
|
|
47
|
+
const live = this.live.get(roomId);
|
|
48
|
+
if (!live) {
|
|
49
|
+
throw new Error('No such room.');
|
|
50
|
+
}
|
|
51
|
+
const limit = Math.min(Math.max(Math.round(Number(options.limit) || 100), 1), 1000);
|
|
52
|
+
const before = typeof options.before === 'string' ? options.before : undefined;
|
|
53
|
+
const chat = await this.chatOf(live);
|
|
54
|
+
const read = await this.chats.read(roomId, { limit, ...(before ? { before } : {}) });
|
|
55
|
+
return before ? read : { lines: [...read.lines, ...((_b = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.outbox) !== null && _b !== void 0 ? _b : [])], more: read.more };
|
|
56
|
+
}
|
|
57
|
+
/** Says it as this Ta in a room: kept at once, sent once the relay is reached. The line, `pending` till then. */
|
|
58
|
+
async say(roomId, text, extra = {}) {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
60
|
+
var _e;
|
|
61
|
+
const live = this.member(roomId);
|
|
62
|
+
const said = String(text !== null && text !== void 0 ? text : '')
|
|
63
|
+
.trim()
|
|
64
|
+
.slice(0, RELAY_CHAT_TEXT_MAX);
|
|
65
|
+
const added = RelayRoomChatUtil.extra(extra);
|
|
66
|
+
if (!said && !((_a = added.images) === null || _a === void 0 ? void 0 : _a.length) && !((_b = added.files) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const chat = await this.chatOf(live);
|
|
70
|
+
const identity = await this.identityOf(live);
|
|
71
|
+
const name = (_e = (_d = (_c = live.roster) === null || _c === void 0 ? void 0 : _c.members.find(member => member.member === live.record.member)) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : identity.name;
|
|
72
|
+
const line = await chat.run(async () => {
|
|
73
|
+
const state = chat.state;
|
|
74
|
+
const at = Math.max(Date.now(), chat.lastAt + 1);
|
|
75
|
+
chat.lastAt = at;
|
|
76
|
+
const made = {
|
|
77
|
+
id: RelayRoomChatUtil.id(),
|
|
78
|
+
from: live.record.member,
|
|
79
|
+
name,
|
|
80
|
+
text: said,
|
|
81
|
+
at,
|
|
82
|
+
...added,
|
|
83
|
+
pending: true,
|
|
84
|
+
};
|
|
85
|
+
state.outbox.push(made);
|
|
86
|
+
await this.chats.setState(roomId, state);
|
|
87
|
+
return made;
|
|
88
|
+
});
|
|
89
|
+
this.emit({ type: 'chat', room: roomId, line });
|
|
90
|
+
void this.flush(live, chat);
|
|
91
|
+
return line;
|
|
92
|
+
}
|
|
93
|
+
/** What the hub of a room says of its log; `synced` once connected, to read what came while away. */
|
|
94
|
+
async heardLog(live, event) {
|
|
95
|
+
var _a;
|
|
96
|
+
var _b;
|
|
97
|
+
if (live.record.status !== 'member' || !live.keys) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const chat = await this.chatOf(live);
|
|
101
|
+
switch (event.t) {
|
|
102
|
+
case 'synced':
|
|
103
|
+
chat.syncing = false;
|
|
104
|
+
await chat.run(() => this.migrate(live, chat));
|
|
105
|
+
this.sync(live, chat);
|
|
106
|
+
return;
|
|
107
|
+
case 'log':
|
|
108
|
+
chat.syncing = false;
|
|
109
|
+
await chat.run(() => this.took(live, chat, event.items));
|
|
110
|
+
if (event.more) {
|
|
111
|
+
this.sync(live, chat);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
this.ack(live, chat);
|
|
115
|
+
void this.flush(live, chat);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
case 'post': {
|
|
119
|
+
const seq = (_b = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _b !== void 0 ? _b : 0;
|
|
120
|
+
if (event.item.s === seq + 1) {
|
|
121
|
+
await chat.run(() => this.took(live, chat, [event.item]));
|
|
122
|
+
clearTimeout(chat.ackTimer);
|
|
123
|
+
chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
|
|
124
|
+
}
|
|
125
|
+
else if (event.item.s > seq) {
|
|
126
|
+
// One missed between: read from where this Ta is.
|
|
127
|
+
this.sync(live, chat);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** The bytes kept on this computer of what was said in each room this Ta is in; with `before`, what clearChat would free. */
|
|
134
|
+
async chatUsage(before) {
|
|
135
|
+
const usage = {};
|
|
136
|
+
const until = typeof before === 'number' && Number.isFinite(before) ? before : undefined;
|
|
137
|
+
for (const id of this.live.keys()) {
|
|
138
|
+
usage[id] = await this.chats.usage(id, until).catch(() => 0);
|
|
139
|
+
}
|
|
140
|
+
return usage;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* What was said in a room goes from this computer: all of it, or what was said before `before` (a month at a time).
|
|
144
|
+
* The others keep theirs; the pages read the chat again.
|
|
145
|
+
*/
|
|
146
|
+
async clearChat(roomId, before) {
|
|
147
|
+
if (!this.live.has(roomId)) {
|
|
148
|
+
throw new Error('No such room.');
|
|
149
|
+
}
|
|
150
|
+
const chat = this.following.get(roomId);
|
|
151
|
+
const clear = () => this.chats.clear(roomId, typeof before === 'number' && Number.isFinite(before) ? before : undefined);
|
|
152
|
+
await (chat ? chat.run(clear) : clear());
|
|
153
|
+
if (chat) {
|
|
154
|
+
chat.ids.clear();
|
|
155
|
+
}
|
|
156
|
+
this.emit({ type: 'rooms' });
|
|
157
|
+
}
|
|
158
|
+
/** The room was left: what was said there goes from this computer. */
|
|
159
|
+
async forgetChat(roomId) {
|
|
160
|
+
const chat = this.following.get(roomId);
|
|
161
|
+
clearTimeout(chat === null || chat === void 0 ? void 0 : chat.ackTimer);
|
|
162
|
+
this.following.delete(roomId);
|
|
163
|
+
await this.chats.forget(roomId).catch(() => undefined);
|
|
164
|
+
}
|
|
165
|
+
chatOf(live) {
|
|
166
|
+
var _a;
|
|
167
|
+
let chat = this.following.get(live.record.id);
|
|
168
|
+
if (!chat) {
|
|
169
|
+
chat = new LiveChat();
|
|
170
|
+
this.following.set(live.record.id, chat);
|
|
171
|
+
}
|
|
172
|
+
const found = chat;
|
|
173
|
+
(_a = found.loading) !== null && _a !== void 0 ? _a : (found.loading = (async () => {
|
|
174
|
+
var _a;
|
|
175
|
+
const state = (_a = (await this.chats.state(live.record.id))) !== null && _a !== void 0 ? _a : { seq: 0, outbox: [] };
|
|
176
|
+
const latest = await this.chats.read(live.record.id, { limit: IDS_KEPT });
|
|
177
|
+
for (const line of latest.lines) {
|
|
178
|
+
found.ids.add(line.id);
|
|
179
|
+
}
|
|
180
|
+
found.lastAt = Math.max(0, ...latest.lines.map(line => line.at), ...state.outbox.map(line => line.at));
|
|
181
|
+
found.acked = state.seq;
|
|
182
|
+
found.state = state;
|
|
183
|
+
return state;
|
|
184
|
+
})());
|
|
185
|
+
return found.loading.then(() => found);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
|
|
189
|
+
* when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
|
|
190
|
+
*/
|
|
191
|
+
async migrate(live, chat) {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
const state = chat.state;
|
|
194
|
+
if (state.migrated) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const since = live.record.own ? 0 : ((_a = live.record.joinedAt) !== null && _a !== void 0 ? _a : 0);
|
|
198
|
+
const lines = [];
|
|
199
|
+
for (const [key, value] of live.state) {
|
|
200
|
+
const part = RelayRoomChatUtil.parse(key);
|
|
201
|
+
if (part && key.startsWith(RELAY_CHAT_KEY) && value.value !== null) {
|
|
202
|
+
lines.push(...((_b = RelayRoomChatUtil.lines(part.member, value.value, value.by)) !== null && _b !== void 0 ? _b : []));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const kept = lines.filter(line => line.at >= since && !chat.ids.has(line.id)).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
206
|
+
if (kept.length) {
|
|
207
|
+
await this.chats.append(live.record.id, kept);
|
|
208
|
+
for (const line of kept) {
|
|
209
|
+
chat.ids.add(line.id);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
state.migrated = true;
|
|
213
|
+
await this.chats.setState(live.record.id, state);
|
|
214
|
+
}
|
|
215
|
+
/** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
|
|
216
|
+
async took(live, chat, items) {
|
|
217
|
+
var _a, _b;
|
|
218
|
+
var _c;
|
|
219
|
+
const state = chat.state;
|
|
220
|
+
const keys = live.keys;
|
|
221
|
+
if (!keys) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const signs = { ...state.signs };
|
|
225
|
+
for (const member of (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []) {
|
|
226
|
+
signs[member.member] = member.sign;
|
|
227
|
+
}
|
|
228
|
+
state.signs = signs;
|
|
229
|
+
const taken = [];
|
|
230
|
+
for (const item of items) {
|
|
231
|
+
if (!(item.s > state.seq)) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
state.seq = item.s;
|
|
235
|
+
const signed = await RelayHubUtil.openIn(keys, item.d, member => signs[member]);
|
|
236
|
+
const line = signed && signed.m === item.m ? RelayRoomChatUtil.signed(item.m, (_b = signed.b) === null || _b === void 0 ? void 0 : _b.chat, item.s) : undefined;
|
|
237
|
+
if (!line || chat.ids.has(line.id)) {
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
this.remember(chat, line.id);
|
|
241
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
242
|
+
taken.push(line);
|
|
243
|
+
}
|
|
244
|
+
if (taken.length) {
|
|
245
|
+
await this.chats.append(live.record.id, taken);
|
|
246
|
+
}
|
|
247
|
+
await this.chats.setState(live.record.id, state);
|
|
248
|
+
for (const line of taken) {
|
|
249
|
+
this.emit({ type: 'chat', room: live.record.id, line });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** Sends the lines waiting, one after another, while the room is reached. */
|
|
253
|
+
async flush(live, chat) {
|
|
254
|
+
var _a, _b;
|
|
255
|
+
if (chat.sending || chat.syncing || !(((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced)) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
chat.sending = true;
|
|
259
|
+
try {
|
|
260
|
+
for (;;) {
|
|
261
|
+
const line = (_b = chat.state) === null || _b === void 0 ? void 0 : _b.outbox.find(waiting => waiting.pending);
|
|
262
|
+
if (!line) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const result = await this.post(live, line);
|
|
266
|
+
if (result.error === 'large' || result.error === 'invalid') {
|
|
267
|
+
await chat.run(() => this.refused(live, chat, line));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (!result.ok || !(await chat.run(() => this.posted(live, chat, line, result.v)))) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
finally {
|
|
276
|
+
chat.sending = false;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async post(live, line) {
|
|
280
|
+
const identity = await this.identityOf(live);
|
|
281
|
+
const [stored] = RelayRoomChatUtil.stored([line]);
|
|
282
|
+
const d = await RelayHubUtil.sealIn(live.keys, identity, live.record.member, { chat: stored });
|
|
283
|
+
const id = this.nextId++;
|
|
284
|
+
return new Promise(resolve => {
|
|
285
|
+
const timer = setTimeout(() => {
|
|
286
|
+
live.done.delete(id);
|
|
287
|
+
resolve({ ok: false, v: 0 });
|
|
288
|
+
}, READY_MS);
|
|
289
|
+
live.done.set(id, answer => {
|
|
290
|
+
clearTimeout(timer);
|
|
291
|
+
resolve(answer);
|
|
292
|
+
});
|
|
293
|
+
try {
|
|
294
|
+
this.request(live, { t: 'post', id, mid: line.id, d });
|
|
295
|
+
}
|
|
296
|
+
catch (_a) {
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
live.done.delete(id);
|
|
299
|
+
resolve({ ok: false, v: 0 });
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/** 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. */
|
|
304
|
+
async posted(live, chat, line, seq) {
|
|
305
|
+
var _a;
|
|
306
|
+
const state = chat.state;
|
|
307
|
+
if (seq <= state.seq || !((_a = chat.state) === null || _a === void 0 ? void 0 : _a.outbox.some(waiting => waiting.id === line.id))) {
|
|
308
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
309
|
+
await this.chats.setState(live.record.id, state);
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
if (seq !== state.seq + 1) {
|
|
313
|
+
this.sync(live, chat);
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
const { pending: _pending, ...sent } = line;
|
|
317
|
+
const taken = { ...sent, seq };
|
|
318
|
+
state.seq = seq;
|
|
319
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
320
|
+
this.remember(chat, line.id);
|
|
321
|
+
await this.chats.append(live.record.id, [taken]);
|
|
322
|
+
await this.chats.setState(live.record.id, state);
|
|
323
|
+
this.emit({ type: 'chat', room: live.record.id, line: taken });
|
|
324
|
+
clearTimeout(chat.ackTimer);
|
|
325
|
+
chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
async refused(live, chat, line) {
|
|
329
|
+
const state = chat.state;
|
|
330
|
+
const { pending: _pending, ...rest } = line;
|
|
331
|
+
const failed = { ...rest, failed: true };
|
|
332
|
+
state.outbox = state.outbox.map(waiting => (waiting.id === line.id ? failed : waiting));
|
|
333
|
+
await this.chats.setState(live.record.id, state);
|
|
334
|
+
this.emit({ type: 'chat', room: live.record.id, line: failed });
|
|
335
|
+
}
|
|
336
|
+
sync(live, chat) {
|
|
337
|
+
var _a, _b;
|
|
338
|
+
var _c;
|
|
339
|
+
if (chat.syncing || ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== OPEN) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
chat.syncing = true;
|
|
343
|
+
try {
|
|
344
|
+
this.request(live, { t: 'sync', after: (_c = (_b = chat.state) === null || _b === void 0 ? void 0 : _b.seq) !== null && _c !== void 0 ? _c : 0, limit: RELAY_HUB_SYNC_PAGE });
|
|
345
|
+
}
|
|
346
|
+
catch (_d) {
|
|
347
|
+
chat.syncing = false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
ack(live, chat) {
|
|
351
|
+
var _a, _b;
|
|
352
|
+
var _c;
|
|
353
|
+
clearTimeout(chat.ackTimer);
|
|
354
|
+
const seq = (_c = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _c !== void 0 ? _c : 0;
|
|
355
|
+
if (seq > chat.acked && ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN) {
|
|
356
|
+
chat.acked = seq;
|
|
357
|
+
this.request(live, { t: 'ack', seq });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
remember(chat, id) {
|
|
361
|
+
chat.ids.add(id);
|
|
362
|
+
if (chat.ids.size > IDS_KEPT) {
|
|
363
|
+
chat.ids.delete(chat.ids.values().next().value);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
//# sourceMappingURL=relay-hubs-chats.js.map
|
|
@@ -3,7 +3,7 @@ import { type RelayHubAskBody, type RelayHubMember } from './relay-hub-util.js';
|
|
|
3
3
|
import { type Live, type MessageBody } from './relay-hubs-live.js';
|
|
4
4
|
import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus } from './relay-hubs-protocol.js';
|
|
5
5
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
|
|
6
|
-
import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
|
|
6
|
+
import { type RelayHubEntry, type RelayHubEvent, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
|
|
7
7
|
/**
|
|
8
8
|
* The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
|
|
9
9
|
* connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
|
|
@@ -62,6 +62,8 @@ export declare abstract class RelayHubsConnection {
|
|
|
62
62
|
protected signOf(live: Live): (member: string) => string | undefined;
|
|
63
63
|
protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
|
|
64
64
|
protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
|
|
65
|
+
/** What the hub says of the log of the room, and `synced` (RelayHubsChats). */
|
|
66
|
+
protected heardLog(_live: Live, _event: RelayHubEvent): Promise<void>;
|
|
65
67
|
/** A member set a key naming another: the owner clears it (RelayHubs). */
|
|
66
68
|
protected foreignSet(_live: Live, _key: string): void;
|
|
67
69
|
/** Ids of those the user chose as friends. */
|
|
@@ -266,6 +266,11 @@ export class RelayHubsConnection {
|
|
|
266
266
|
resolve();
|
|
267
267
|
}
|
|
268
268
|
this.emit({ type: 'rooms' });
|
|
269
|
+
await this.heardLog(live, event);
|
|
270
|
+
return;
|
|
271
|
+
case 'log':
|
|
272
|
+
case 'post':
|
|
273
|
+
await this.heardLog(live, event);
|
|
269
274
|
return;
|
|
270
275
|
case 'state':
|
|
271
276
|
await this.readEntry(live, event.entry, true);
|
|
@@ -521,6 +526,8 @@ export class RelayHubsConnection {
|
|
|
521
526
|
}
|
|
522
527
|
this.emit({ type: 'message', room: live.record.id, from, name: (_b = sender === null || sender === void 0 ? void 0 : sender.name) !== null && _b !== void 0 ? _b : '', data: signed.b.d });
|
|
523
528
|
}
|
|
529
|
+
/** What the hub says of the log of the room, and `synced` (RelayHubsChats). */
|
|
530
|
+
async heardLog(_live, _event) { }
|
|
524
531
|
/** A member set a key naming another: the owner clears it (RelayHubs). */
|
|
525
532
|
foreignSet(_live, _key) { }
|
|
526
533
|
/** Ids of those the user chose as friends. */
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { RelayChatStore } from './relay-chat-store.js';
|
|
1
2
|
import type { RelayIdentity, RelayPublicIdentity } from './relay-identity.js';
|
|
3
|
+
import type { RelayChatLine } from './relay-room-chat.js';
|
|
2
4
|
/** `member` in the room; `waiting` for the owner to answer; `refused` by the owner; `gone`: closed, removed, or the invitation no longer works. */
|
|
3
5
|
export type RelayRoomStatus = 'member' | 'waiting' | 'refused' | 'gone';
|
|
4
6
|
/** A room this Ta is in or asked to join, in `rooms.json` of the agent directory (docs/design/ta-link.md 3.5). */
|
|
@@ -73,6 +75,8 @@ export interface RelayRoomMember {
|
|
|
73
75
|
self: boolean;
|
|
74
76
|
/** In a browser, without Ta: it only chats (docs/design/ta-link.md 3.8). */
|
|
75
77
|
guest?: boolean;
|
|
78
|
+
/** When it was let in, as the roster says: what was said before is not its to read, unless the room says so. */
|
|
79
|
+
joinedAt?: number;
|
|
76
80
|
}
|
|
77
81
|
export interface RelayRoomInfo {
|
|
78
82
|
id: string;
|
|
@@ -89,6 +93,8 @@ export interface RelayRoomInfo {
|
|
|
89
93
|
capacity?: number;
|
|
90
94
|
/** The rules its owner set, for all (RelayHubRules); none when it set none. */
|
|
91
95
|
rules?: Record<string, boolean>;
|
|
96
|
+
/** Messages from before they joined a newcomer reads, as its owner set; none: none. */
|
|
97
|
+
history?: number;
|
|
92
98
|
ownerName: string;
|
|
93
99
|
status: RelayRoomStatus;
|
|
94
100
|
reason?: RelayRoomRecord['reason'];
|
|
@@ -165,6 +171,12 @@ export type RelayRoomEvent = {
|
|
|
165
171
|
from: string;
|
|
166
172
|
name: string;
|
|
167
173
|
data: unknown;
|
|
174
|
+
}
|
|
175
|
+
/** A line of the chat of a room: said here (`pending`), sent, refused (`failed`), or taken from another member. */
|
|
176
|
+
| {
|
|
177
|
+
type: 'chat';
|
|
178
|
+
room: string;
|
|
179
|
+
line: RelayChatLine;
|
|
168
180
|
} | {
|
|
169
181
|
type: 'state';
|
|
170
182
|
room: string;
|
|
@@ -175,6 +187,8 @@ export type RelayRoomEvent = {
|
|
|
175
187
|
};
|
|
176
188
|
/** Where a {@link RelayHubs} keeps what it knows: files of the agent directory for Ta, the storage of a browser for a guest. */
|
|
177
189
|
export interface RelayHubsStore {
|
|
190
|
+
/** Where what is said in the rooms is kept, when not in memory (RelayHubsOptions.chats says otherwise). */
|
|
191
|
+
chats?(): RelayChatStore | undefined;
|
|
178
192
|
read<T>(name: RelayHubsStoreName): T | undefined;
|
|
179
193
|
/** Holds keys: kept where the user alone reads it. */
|
|
180
194
|
write(name: RelayHubsStoreName, value: unknown): void;
|
|
@@ -192,6 +206,8 @@ export interface RelayHubsOptions {
|
|
|
192
206
|
guest?: boolean;
|
|
193
207
|
/** The relay new rooms are made on; `use` the feature a room is for (relays.json), `rooms` when none is said. */
|
|
194
208
|
relay(use?: string): string;
|
|
209
|
+
/** Where what is said in the rooms is kept (docs/design/ta-chat-history.md 8); in memory without. */
|
|
210
|
+
chats?: RelayChatStore;
|
|
195
211
|
/** First wait before connecting again; it doubles up to 30 seconds. */
|
|
196
212
|
reconnectDelayMs?: number;
|
|
197
213
|
pingMs?: number;
|
|
@@ -2,7 +2,7 @@ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends.js
|
|
|
2
2
|
import { RelayFriends } from './relay-friends.js';
|
|
3
3
|
import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites.js';
|
|
4
4
|
import { type RelayHubAskBody } from './relay-hub-util.js';
|
|
5
|
-
import {
|
|
5
|
+
import { RelayHubsChats } from './relay-hubs-chats.js';
|
|
6
6
|
import { Live } from './relay-hubs-live.js';
|
|
7
7
|
import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
|
|
8
8
|
import { type RelayPublicIdentity } from './relay-identity.js';
|
|
@@ -12,7 +12,7 @@ import { type RelayPublicIdentity } from './relay-identity.js';
|
|
|
12
12
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
13
13
|
* their own (RelayFriends).
|
|
14
14
|
*/
|
|
15
|
-
export declare class RelayHubs extends
|
|
15
|
+
export declare class RelayHubs extends RelayHubsChats {
|
|
16
16
|
protected readonly book: RelayFriends;
|
|
17
17
|
protected readonly groups: RelayGroupInvites;
|
|
18
18
|
protected readonly sends: RelayFriendSends;
|
package/esm/common/relay-hubs.js
CHANGED
|
@@ -3,7 +3,7 @@ import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-frie
|
|
|
3
3
|
import { RelayGroupInvites } from './relay-group-invites.js';
|
|
4
4
|
import { RELAY_GUESTS_PER_HOUR, RELAY_GUESTS_RULE, RelayHubCharterUtil } from './relay-hub-charter.js';
|
|
5
5
|
import { RelayHubUtil } from './relay-hub-util.js';
|
|
6
|
-
import {
|
|
6
|
+
import { RelayHubsChats } from './relay-hubs-chats.js';
|
|
7
7
|
import { Live, OPEN, READY_MS } from './relay-hubs-live.js';
|
|
8
8
|
import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
|
|
9
9
|
import { RELAY_INVITE_MAX_DAYS, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
@@ -14,7 +14,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
14
14
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
15
15
|
* their own (RelayFriends).
|
|
16
16
|
*/
|
|
17
|
-
export class RelayHubs extends
|
|
17
|
+
export class RelayHubs extends RelayHubsChats {
|
|
18
18
|
constructor() {
|
|
19
19
|
super(...arguments);
|
|
20
20
|
this.book = new RelayFriends({
|
|
@@ -230,8 +230,8 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
230
230
|
}
|
|
231
231
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
232
232
|
async answer(roomId, askId, accept) {
|
|
233
|
-
var _a, _b;
|
|
234
|
-
var
|
|
233
|
+
var _a, _b, _c;
|
|
234
|
+
var _d, _e;
|
|
235
235
|
const live = this.managed(roomId);
|
|
236
236
|
const asked = live.asks.get(askId);
|
|
237
237
|
if (!asked) {
|
|
@@ -252,7 +252,7 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
252
252
|
...(live.record.unlisted ? { unlisted: true } : {}),
|
|
253
253
|
};
|
|
254
254
|
const ask = asked.body;
|
|
255
|
-
if (((
|
|
255
|
+
if (((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _d !== void 0 ? _d : 0) >= this.capacityOf(live)) {
|
|
256
256
|
throw new Error('The room is full.');
|
|
257
257
|
}
|
|
258
258
|
const head = live.record.owner;
|
|
@@ -262,7 +262,7 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
262
262
|
const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
|
|
263
263
|
const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
|
|
264
264
|
const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
|
|
265
|
-
this.request(live, { t: 'admit', ask: askId, answer, roster });
|
|
265
|
+
this.request(live, { t: 'admit', ask: askId, answer, roster, history: (_e = (_c = live.charter) === null || _c === void 0 ? void 0 : _c.history) !== null && _e !== void 0 ? _e : 0 });
|
|
266
266
|
}
|
|
267
267
|
live.asks.delete(askId);
|
|
268
268
|
this.emit({ type: 'rooms' });
|
|
@@ -299,6 +299,7 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
299
299
|
this.disconnect(live);
|
|
300
300
|
this.live.delete(roomId);
|
|
301
301
|
this.save();
|
|
302
|
+
await this.forgetChat(roomId);
|
|
302
303
|
this.emit({ type: 'rooms' });
|
|
303
304
|
}
|
|
304
305
|
/** To every other member, or to one (`to`, its member id). */
|
|
@@ -498,10 +499,10 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
498
499
|
}
|
|
499
500
|
}
|
|
500
501
|
info(live) {
|
|
501
|
-
var _a, _b;
|
|
502
|
-
var
|
|
502
|
+
var _a, _b, _c;
|
|
503
|
+
var _d, _e;
|
|
503
504
|
const { record } = live;
|
|
504
|
-
const members = ((
|
|
505
|
+
const members = ((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _d !== void 0 ? _d : []).map(member => { var _a; return ({
|
|
505
506
|
member: member.member,
|
|
506
507
|
id: member.id,
|
|
507
508
|
name: member.name,
|
|
@@ -511,6 +512,7 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
511
512
|
online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
|
|
512
513
|
self: member.member === record.member,
|
|
513
514
|
...(member.guest ? { guest: true } : {}),
|
|
515
|
+
...(Number.isFinite(member.at) ? { joinedAt: member.at } : {}),
|
|
514
516
|
}); });
|
|
515
517
|
return {
|
|
516
518
|
id: record.id,
|
|
@@ -521,12 +523,13 @@ export class RelayHubs extends RelayHubsAdmins {
|
|
|
521
523
|
relay: record.relay,
|
|
522
524
|
owner: record.own,
|
|
523
525
|
...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
|
|
524
|
-
...(record.own ? { capacity: (
|
|
526
|
+
...(record.own ? { capacity: (_e = record.capacity) !== null && _e !== void 0 ? _e : RELAY_ROOM_MAX_MEMBERS } : {}),
|
|
525
527
|
...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
|
|
528
|
+
...(((_b = live.charter) === null || _b === void 0 ? void 0 : _b.history) ? { history: live.charter.history } : {}),
|
|
526
529
|
ownerName: record.owner.name,
|
|
527
530
|
status: record.status,
|
|
528
531
|
...(record.reason ? { reason: record.reason } : {}),
|
|
529
|
-
connected: ((
|
|
532
|
+
connected: ((_c = live.socket) === null || _c === void 0 ? void 0 : _c.readyState) === OPEN && (live.synced || record.status === 'waiting'),
|
|
530
533
|
members,
|
|
531
534
|
createdAt: record.createdAt,
|
|
532
535
|
...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
|