@celljs/ta-relay 4.22.0 → 4.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +59 -0
- package/esm/common/relay-chat-store.js +40 -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 +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 +2 -2
- package/esm/common/relay-hubs.js +13 -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 +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 +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 +60 -0
- package/lib/common/relay-chat-store.js +44 -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 +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 +2 -2
- package/lib/common/relay-hubs.js +13 -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 +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 +4 -4
- package/viewer/page.html +10 -10
- package/viewer/room.html +15 -15
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/** The bytes of a file of each kind at most: what passes between members in a minute or two. */
|
|
2
|
+
export const RELAY_CHAT_FILE_BYTES = { file: 50 * 1024 * 1024, video: 30 * 1024 * 1024, voice: 2 * 1024 * 1024 };
|
|
3
|
+
/** How long a voice message and a video are at most. */
|
|
4
|
+
export const RELAY_CHAT_VOICE_MS = 60000;
|
|
5
|
+
export const RELAY_CHAT_VIDEO_MS = 3 * 60000;
|
|
6
|
+
/** The files of a line at most. */
|
|
7
|
+
export const RELAY_CHAT_FILES = 9;
|
|
1
8
|
/** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
|
|
2
9
|
export const RELAY_CHAT_KEY = 'chat:';
|
|
3
10
|
/** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
|
|
@@ -5,6 +12,15 @@ export const RELAY_CHAT_PART_BYTES = 8 * 1024;
|
|
|
5
12
|
/** The parts of each member kept: the oldest is cleared as a new one starts. */
|
|
6
13
|
export const RELAY_CHAT_PARTS = 6;
|
|
7
14
|
export const RELAY_CHAT_TEXT_MAX = 2000;
|
|
15
|
+
/** The pictures of a line at most, as WeChat sends nine at once. */
|
|
16
|
+
export const RELAY_CHAT_IMAGES = 9;
|
|
17
|
+
/** The bytes of a picture at most. */
|
|
18
|
+
export const RELAY_CHAT_IMAGE_BYTES = 2 * 1024 * 1024;
|
|
19
|
+
/** The characters of the data URL of a thumbnail at most. */
|
|
20
|
+
export const RELAY_CHAT_THUMB_CHARS = 900;
|
|
21
|
+
const IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
22
|
+
/** Lines a chat holds at once: the latest; older ones are read as the page asks (RelayRoomChat.older). */
|
|
23
|
+
export const RELAY_CHAT_WINDOW = 500;
|
|
8
24
|
const TRIES = 6;
|
|
9
25
|
export var RelayRoomChatUtil;
|
|
10
26
|
(function (RelayRoomChatUtil) {
|
|
@@ -44,17 +60,141 @@ export var RelayRoomChatUtil;
|
|
|
44
60
|
...(line.person === true && to.length && line.double !== true && !line.agent ? { person: true } : {}),
|
|
45
61
|
...(typeof line.re === 'string' ? { re: line.re } : {}),
|
|
46
62
|
...(line.task === true && to.length ? { task: true } : {}),
|
|
63
|
+
...images(line.images),
|
|
64
|
+
...files(line.files),
|
|
47
65
|
};
|
|
48
66
|
}
|
|
49
67
|
RelayRoomChatUtil.extra = extra;
|
|
68
|
+
/** The files of a stored line, as far as each is well formed. */
|
|
69
|
+
function files(value) {
|
|
70
|
+
if (!Array.isArray(value)) {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
const files = [];
|
|
74
|
+
for (const file of value.slice(0, RELAY_CHAT_FILES)) {
|
|
75
|
+
const kind = file === null || file === void 0 ? void 0 : file.kind;
|
|
76
|
+
if (!file ||
|
|
77
|
+
(kind !== 'file' && kind !== 'voice' && kind !== 'video') ||
|
|
78
|
+
typeof file.id !== 'string' ||
|
|
79
|
+
!/^[a-f0-9]{24}$/.test(file.id) ||
|
|
80
|
+
typeof file.sha !== 'string' ||
|
|
81
|
+
!/^[0-9a-f]{64}$/.test(file.sha) ||
|
|
82
|
+
!Number.isInteger(file.bytes) ||
|
|
83
|
+
file.bytes <= 0 ||
|
|
84
|
+
file.bytes > RELAY_CHAT_FILE_BYTES[kind]) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const name = [...(typeof file.name === 'string' ? file.name : '')]
|
|
88
|
+
.filter(char => char.charCodeAt(0) >= 32 && char !== '/' && char !== '\\')
|
|
89
|
+
.join('')
|
|
90
|
+
.slice(0, 120) || kind;
|
|
91
|
+
const type = typeof file.type === 'string' && /^[\w.+-]+\/[\w.+-]+$/.test(file.type) ? file.type.slice(0, 100) : 'application/octet-stream';
|
|
92
|
+
const most = kind === 'voice' ? RELAY_CHAT_VOICE_MS : RELAY_CHAT_VIDEO_MS;
|
|
93
|
+
const ms = kind !== 'file' && Number.isFinite(file.ms) ? Math.min(Math.max(Math.round(file.ms), 0), most) : undefined;
|
|
94
|
+
const size = kind === 'video' && Number.isInteger(file.w) && Number.isInteger(file.h) && file.w > 0 && file.h > 0;
|
|
95
|
+
const thumb = kind === 'video' &&
|
|
96
|
+
typeof file.thumb === 'string' &&
|
|
97
|
+
file.thumb.length <= RELAY_CHAT_THUMB_CHARS &&
|
|
98
|
+
/^data:image\/(jpeg|png|webp);base64,/.test(file.thumb)
|
|
99
|
+
? file.thumb
|
|
100
|
+
: undefined;
|
|
101
|
+
files.push({
|
|
102
|
+
id: file.id,
|
|
103
|
+
kind,
|
|
104
|
+
name,
|
|
105
|
+
type,
|
|
106
|
+
bytes: file.bytes,
|
|
107
|
+
sha: file.sha,
|
|
108
|
+
...(ms !== undefined ? { ms } : {}),
|
|
109
|
+
...(size ? { w: file.w, h: file.h } : {}),
|
|
110
|
+
...(thumb ? { thumb } : {}),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return files.length ? { files } : {};
|
|
114
|
+
}
|
|
115
|
+
RelayRoomChatUtil.files = files;
|
|
116
|
+
/** The pictures of a stored line, as far as each is well formed. */
|
|
117
|
+
function images(value) {
|
|
118
|
+
if (!Array.isArray(value)) {
|
|
119
|
+
return {};
|
|
120
|
+
}
|
|
121
|
+
const images = [];
|
|
122
|
+
for (const image of value.slice(0, RELAY_CHAT_IMAGES)) {
|
|
123
|
+
if (image &&
|
|
124
|
+
typeof image.id === 'string' &&
|
|
125
|
+
/^[A-Za-z0-9-]{8,64}$/.test(image.id) &&
|
|
126
|
+
typeof image.type === 'string' &&
|
|
127
|
+
IMAGE_TYPES.includes(image.type) &&
|
|
128
|
+
typeof image.sha === 'string' &&
|
|
129
|
+
/^[0-9a-f]{64}$/.test(image.sha) &&
|
|
130
|
+
Number.isInteger(image.w) &&
|
|
131
|
+
Number.isInteger(image.h) &&
|
|
132
|
+
Number.isInteger(image.bytes) &&
|
|
133
|
+
image.w > 0 &&
|
|
134
|
+
image.h > 0 &&
|
|
135
|
+
image.bytes > 0 &&
|
|
136
|
+
image.bytes <= RELAY_CHAT_IMAGE_BYTES) {
|
|
137
|
+
const thumb = typeof image.thumb === 'string' && image.thumb.length <= RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(image.thumb)
|
|
138
|
+
? image.thumb
|
|
139
|
+
: undefined;
|
|
140
|
+
const blob = image.blob;
|
|
141
|
+
const ok = !!blob &&
|
|
142
|
+
typeof blob.id === 'string' &&
|
|
143
|
+
/^[A-Za-z0-9_-]{16,64}$/.test(blob.id) &&
|
|
144
|
+
typeof blob.read === 'string' &&
|
|
145
|
+
/^[A-Za-z0-9_-]{43}$/.test(blob.read) &&
|
|
146
|
+
typeof blob.key === 'string' &&
|
|
147
|
+
/^[A-Za-z0-9_-]{43}$/.test(blob.key) &&
|
|
148
|
+
Number.isFinite(blob.until);
|
|
149
|
+
images.push({
|
|
150
|
+
id: image.id,
|
|
151
|
+
type: image.type,
|
|
152
|
+
w: image.w,
|
|
153
|
+
h: image.h,
|
|
154
|
+
bytes: image.bytes,
|
|
155
|
+
sha: image.sha,
|
|
156
|
+
...(thumb ? { thumb } : {}),
|
|
157
|
+
...(ok && blob ? { blob: { id: blob.id, read: blob.read, key: blob.key, until: blob.until } } : {}),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return images.length ? { images } : {};
|
|
162
|
+
}
|
|
163
|
+
/** What a line says in a word where only text shows (the list of chats, a notice, a page of guests). */
|
|
164
|
+
function preview(line, picture, words = { file: '[file]', voice: '[voice]', video: '[video]' }) {
|
|
165
|
+
var _a;
|
|
166
|
+
var _b;
|
|
167
|
+
const pictures = ((_a = line.images) === null || _a === void 0 ? void 0 : _a.length) ? (line.images.length > 1 ? `${picture}×${line.images.length}` : picture) : '';
|
|
168
|
+
const files = ((_b = line.files) !== null && _b !== void 0 ? _b : []).map(file => (file.kind === 'file' ? `${words.file} ${file.name}` : words[file.kind]));
|
|
169
|
+
return [pictures, ...files, line.text].filter(Boolean).join(' ');
|
|
170
|
+
}
|
|
171
|
+
RelayRoomChatUtil.preview = preview;
|
|
50
172
|
function stored(lines) {
|
|
51
|
-
return lines.map(({ from: _from, ...line }) => line);
|
|
173
|
+
return lines.map(({ from: _from, seq: _seq, pending: _pending, failed: _failed, ...line }) => line);
|
|
52
174
|
}
|
|
53
175
|
RelayRoomChatUtil.stored = stored;
|
|
176
|
+
/** A line of the log, as its member signed it: well formed, or none. */
|
|
177
|
+
function signed(member, value, seq) {
|
|
178
|
+
const line = value;
|
|
179
|
+
if (!line || typeof line.id !== 'string' || typeof line.text !== 'string' || typeof line.at !== 'number') {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const name = typeof line.name === 'string' ? line.name.slice(0, 64) : '';
|
|
183
|
+
const text = line.text.slice(0, RELAY_CHAT_TEXT_MAX);
|
|
184
|
+
return { id: line.id.slice(0, 40), from: member, name, text, at: line.at, ...extra(line), ...(seq ? { seq } : {}) };
|
|
185
|
+
}
|
|
186
|
+
RelayRoomChatUtil.signed = signed;
|
|
54
187
|
function bytes(lines) {
|
|
55
188
|
return new TextEncoder().encode(JSON.stringify(lines)).length;
|
|
56
189
|
}
|
|
57
190
|
RelayRoomChatUtil.bytes = bytes;
|
|
191
|
+
/** An error of a call a Ta service of before does not know (JSON-RPC's method not found, or its words). */
|
|
192
|
+
function unknownMethod(error) {
|
|
193
|
+
var _a;
|
|
194
|
+
const found = error;
|
|
195
|
+
return (found === null || found === void 0 ? void 0 : found.code) === -32601 || /method not found|not a function|unknown method|no such method/i.test(String((_a = found === null || found === void 0 ? void 0 : found.message) !== null && _a !== void 0 ? _a : error));
|
|
196
|
+
}
|
|
197
|
+
RelayRoomChatUtil.unknownMethod = unknownMethod;
|
|
58
198
|
function id() {
|
|
59
199
|
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
60
200
|
}
|
|
@@ -72,6 +212,9 @@ export class RelayRoomChat {
|
|
|
72
212
|
this.parts = new Map();
|
|
73
213
|
this.listeners = new Set();
|
|
74
214
|
this.all = [];
|
|
215
|
+
/** Older lines are kept than those read (the log, RelayRoomChatRooms.readChat). */
|
|
216
|
+
this.olderKept = false;
|
|
217
|
+
this.log = typeof rooms.readChat === 'function' && typeof rooms.say === 'function';
|
|
75
218
|
}
|
|
76
219
|
/** All that is said, the earliest first. */
|
|
77
220
|
get lines() {
|
|
@@ -81,7 +224,26 @@ export class RelayRoomChat {
|
|
|
81
224
|
this.listeners.add(listener);
|
|
82
225
|
return () => this.listeners.delete(listener);
|
|
83
226
|
}
|
|
227
|
+
/** Whether there are older lines than those read: `older` reads them. */
|
|
228
|
+
get more() {
|
|
229
|
+
return this.olderKept;
|
|
230
|
+
}
|
|
84
231
|
async load() {
|
|
232
|
+
if (this.log && this.rooms.readChat) {
|
|
233
|
+
const read = await this.rooms.readChat(this.room, { limit: RELAY_CHAT_WINDOW }).catch((error) => {
|
|
234
|
+
if (!RelayRoomChatUtil.unknownMethod(error)) {
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
this.log = false;
|
|
238
|
+
return undefined;
|
|
239
|
+
});
|
|
240
|
+
if (read) {
|
|
241
|
+
this.olderKept = read.more;
|
|
242
|
+
this.all = Array.isArray(read.lines) ? read.lines : [];
|
|
243
|
+
this.tell();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
85
247
|
const state = await this.rooms.getState(this.room, RELAY_CHAT_KEY);
|
|
86
248
|
this.parts.clear();
|
|
87
249
|
for (const [key, value] of Object.entries(state)) {
|
|
@@ -89,8 +251,60 @@ export class RelayRoomChat {
|
|
|
89
251
|
}
|
|
90
252
|
this.changed();
|
|
91
253
|
}
|
|
254
|
+
/** Lines before those read, a page of them: how many came. */
|
|
255
|
+
async older(limit = 100) {
|
|
256
|
+
const first = this.all[0];
|
|
257
|
+
if (!this.log || !this.rooms.readChat || !first || !this.olderKept) {
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
const read = await this.rooms.readChat(this.room, { before: first.id, limit });
|
|
261
|
+
const known = new Set(this.all.map(line => line.id));
|
|
262
|
+
const lines = read.lines.filter(line => !known.has(line.id));
|
|
263
|
+
this.olderKept = read.more;
|
|
264
|
+
this.all = [...lines, ...this.all];
|
|
265
|
+
this.tell();
|
|
266
|
+
return lines.length;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* What happened in the room, as its event tells: a line of the log (`chat`), or a key of the state set (`state`,
|
|
270
|
+
* the chat of a Ta service of before). Whether it was of the chat.
|
|
271
|
+
*/
|
|
272
|
+
hear(event) {
|
|
273
|
+
if (event.room !== undefined && event.room !== this.room) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
if (event.type === 'chat' && event.line) {
|
|
277
|
+
this.heardLine(event.line);
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
return event.type === 'state' && typeof event.key === 'string' && this.heard(event.key, event);
|
|
281
|
+
}
|
|
282
|
+
/** A line said or taken: one of the same id is replaced (sent, refused), a new one comes last. */
|
|
283
|
+
heardLine(line) {
|
|
284
|
+
const index = this.all.findIndex(each => each.id === line.id);
|
|
285
|
+
if (index >= 0) {
|
|
286
|
+
const next = [...this.all];
|
|
287
|
+
next[index] = line;
|
|
288
|
+
this.all = next;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const next = [...this.all, line];
|
|
292
|
+
// A line taken while one of this Ta waits goes before it: the pending come last.
|
|
293
|
+
const pending = next.findIndex(each => each.pending);
|
|
294
|
+
if (pending >= 0 && !line.pending) {
|
|
295
|
+
next.splice(next.length - 1, 1);
|
|
296
|
+
next.splice(pending, 0, line);
|
|
297
|
+
}
|
|
298
|
+
this.all = next.length > RELAY_CHAT_WINDOW * 2 ? next.slice(-RELAY_CHAT_WINDOW) : next;
|
|
299
|
+
this.olderKept || (this.olderKept = next.length > this.all.length);
|
|
300
|
+
}
|
|
301
|
+
this.tell();
|
|
302
|
+
}
|
|
92
303
|
/** A key of the room set, as its event tells: whether it was of the chat. */
|
|
93
304
|
heard(key, value) {
|
|
305
|
+
if (this.log) {
|
|
306
|
+
return !!RelayRoomChatUtil.parse(key);
|
|
307
|
+
}
|
|
94
308
|
if (!RelayRoomChatUtil.parse(key)) {
|
|
95
309
|
return false;
|
|
96
310
|
}
|
|
@@ -107,17 +321,24 @@ export class RelayRoomChat {
|
|
|
107
321
|
* again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
|
|
108
322
|
*/
|
|
109
323
|
async say(text, self, extra = {}) {
|
|
110
|
-
var _a;
|
|
111
|
-
var
|
|
324
|
+
var _a, _b, _c;
|
|
325
|
+
var _d, _e;
|
|
112
326
|
const said = text.trim().slice(0, RELAY_CHAT_TEXT_MAX);
|
|
113
|
-
if (!said) {
|
|
327
|
+
if (!said && !((_a = extra.images) === null || _a === void 0 ? void 0 : _a.length) && !((_b = extra.files) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
114
328
|
return undefined;
|
|
115
329
|
}
|
|
330
|
+
if (this.log && this.rooms.say) {
|
|
331
|
+
const line = await this.rooms.say(this.room, said, RelayRoomChatUtil.extra(extra));
|
|
332
|
+
if (line) {
|
|
333
|
+
this.heardLine(line);
|
|
334
|
+
}
|
|
335
|
+
return line;
|
|
336
|
+
}
|
|
116
337
|
for (let tries = 0; tries < TRIES; tries++) {
|
|
117
338
|
const latest = this.latest(self.member);
|
|
118
339
|
// After what it said last, even within the same millisecond: the lines of a member keep their order.
|
|
119
340
|
const last = latest === null || latest === void 0 ? void 0 : latest.lines[latest.lines.length - 1];
|
|
120
|
-
const at = Math.max(Date.now(), ((
|
|
341
|
+
const at = Math.max(Date.now(), ((_d = last === null || last === void 0 ? void 0 : last.at) !== null && _d !== void 0 ? _d : 0) + 1);
|
|
121
342
|
const line = {
|
|
122
343
|
id: RelayRoomChatUtil.id(),
|
|
123
344
|
from: self.member,
|
|
@@ -131,7 +352,7 @@ export class RelayRoomChat {
|
|
|
131
352
|
const lines = fits && latest ? [...latest.lines, line] : [line];
|
|
132
353
|
const key = RelayRoomChatUtil.key(self.member, part);
|
|
133
354
|
const value = RelayRoomChatUtil.stored(lines);
|
|
134
|
-
const result = await this.rooms.setState(this.room, key, value, (
|
|
355
|
+
const result = await this.rooms.setState(this.room, key, value, (_e = (_c = this.parts.get(key)) === null || _c === void 0 ? void 0 : _c.version) !== null && _e !== void 0 ? _e : 0);
|
|
135
356
|
if (result.ok) {
|
|
136
357
|
this.put(key, { value, version: result.version, by: self.member });
|
|
137
358
|
this.changed();
|
|
@@ -170,6 +391,9 @@ export class RelayRoomChat {
|
|
|
170
391
|
}
|
|
171
392
|
changed() {
|
|
172
393
|
this.all = [...this.parts.values()].flatMap(part => part.lines).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
394
|
+
this.tell();
|
|
395
|
+
}
|
|
396
|
+
tell() {
|
|
173
397
|
for (const listener of this.listeners) {
|
|
174
398
|
listener();
|
|
175
399
|
}
|
package/esm/guest/guest-i18n.js
CHANGED
|
@@ -10,6 +10,8 @@ const zh = {
|
|
|
10
10
|
namePlaceholder: '大家看到的名字',
|
|
11
11
|
join: '申请加入',
|
|
12
12
|
joining: '正在申请…',
|
|
13
|
+
picture: '[图片,在 Ta 里看]',
|
|
14
|
+
file: '[文件、语音或视频,在 Ta 里看]',
|
|
13
15
|
keep: '你的名字和一把只属于这个浏览器的钥匙保存在本机;清除浏览器数据后要重新申请。',
|
|
14
16
|
haveTa: '装了 Ta?在 Ta 应用里扫码或粘贴这个链接,还能一起玩小镇等游戏。',
|
|
15
17
|
waiting: '已向{by}申请',
|
|
@@ -58,6 +60,8 @@ const zhTw = {
|
|
|
58
60
|
namePlaceholder: '大家看到的名字',
|
|
59
61
|
join: '申請加入',
|
|
60
62
|
joining: '正在申請…',
|
|
63
|
+
picture: '[圖片,在 Ta 裡看]',
|
|
64
|
+
file: '[檔案、語音或影片,在 Ta 裡看]',
|
|
61
65
|
keep: '你的名字和一把只屬於這個瀏覽器的鑰匙儲存在本機;清除瀏覽器資料後要重新申請。',
|
|
62
66
|
haveTa: '裝了 Ta?在 Ta 應用裡掃碼或貼上這個連結,還能一起玩小鎮等遊戲。',
|
|
63
67
|
waiting: '已向{by}申請',
|
|
@@ -106,6 +110,8 @@ const en = {
|
|
|
106
110
|
namePlaceholder: 'What everyone sees',
|
|
107
111
|
join: 'Ask to join',
|
|
108
112
|
joining: 'Asking…',
|
|
113
|
+
picture: '[Picture: see it in Ta]',
|
|
114
|
+
file: '[File, voice or video: see it in Ta]',
|
|
109
115
|
keep: 'Your name and a key of this browser alone are kept on this device; clear its data and you ask again.',
|
|
110
116
|
haveTa: 'Have Ta? Scan this link in the Ta app or paste it there, to play the Town and other games together too.',
|
|
111
117
|
waiting: 'Asked {by}',
|
|
@@ -154,6 +160,8 @@ const ja = {
|
|
|
154
160
|
namePlaceholder: 'みんなに見える名前',
|
|
155
161
|
join: '参加をリクエスト',
|
|
156
162
|
joining: 'リクエスト中…',
|
|
163
|
+
picture: '[画像:Ta で表示]',
|
|
164
|
+
file: '[ファイル・ボイス・動画:Ta で表示]',
|
|
157
165
|
keep: 'あなたの名前とこのブラウザ専用のキーはこのデバイスに保存されます。ブラウザデータをクリアすると再度リクエストが必要です。',
|
|
158
166
|
haveTa: 'Ta をインストール済みですか?Ta アプリでこのリンクをスキャンするかペーストして、タウンなどのゲームも一緒にプレイできます。',
|
|
159
167
|
waiting: '{by}さんにリクエスト済み',
|
package/esm/guest/guest.js
CHANGED
|
@@ -3,7 +3,7 @@ import { RelayHubUtil } from '../common/relay-hub-util.js';
|
|
|
3
3
|
import { RelayHubs } from '../common/relay-hubs.js';
|
|
4
4
|
import { RelayOpenInTa } from '../common/relay-open-in-ta.js';
|
|
5
5
|
import { RELAY_BOARD_KEY, RelayBoardUtil } from '../common/relay-room-app.js';
|
|
6
|
-
import { RelayRoomChat } from '../common/relay-room-chat.js';
|
|
6
|
+
import { RelayRoomChat, RelayRoomChatUtil } from '../common/relay-room-chat.js';
|
|
7
7
|
import { ViewerMarkdown } from '../viewer/viewer-markdown.js';
|
|
8
8
|
import { GuestI18n } from './guest-i18n.js';
|
|
9
9
|
import { GUEST_STYLE } from './guest-style.js';
|
|
@@ -99,9 +99,12 @@ class GuestPage {
|
|
|
99
99
|
}
|
|
100
100
|
start() {
|
|
101
101
|
this.hubs.onEvent(event => {
|
|
102
|
-
var _a;
|
|
103
|
-
if (event.type === '
|
|
104
|
-
(_a = this.chat) === null || _a === void 0 ? void 0 : _a.
|
|
102
|
+
var _a, _b;
|
|
103
|
+
if (event.type === 'chat' && event.room === this.link.room) {
|
|
104
|
+
(_a = this.chat) === null || _a === void 0 ? void 0 : _a.hear(event);
|
|
105
|
+
}
|
|
106
|
+
else if (event.type === 'state' && event.room === this.link.room) {
|
|
107
|
+
(_b = this.chat) === null || _b === void 0 ? void 0 : _b.heard(event.key, event);
|
|
105
108
|
if (event.key.startsWith(RELAY_BOARD_KEY)) {
|
|
106
109
|
this.heardBoard(event.key, event.value, event.by);
|
|
107
110
|
}
|
|
@@ -326,7 +329,7 @@ class GuestPage {
|
|
|
326
329
|
if (separator) {
|
|
327
330
|
nodes.push(div('when', GuestI18n.time(line.at)));
|
|
328
331
|
}
|
|
329
|
-
const bubble = div('bubble', ViewerMarkdown.render(line
|
|
332
|
+
const bubble = div('bubble', ViewerMarkdown.render(RelayRoomChatUtil.preview(line, t('picture'), { file: t('file'), voice: t('file'), video: t('file') })));
|
|
330
333
|
bubble.title = GuestI18n.time(line.at);
|
|
331
334
|
if (line.from === self && !line.agent) {
|
|
332
335
|
nodes.push(div('mine', bubble));
|
package/esm/node/index.d.ts
CHANGED
package/esm/node/index.js
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type RelayChatState, type RelayChatStore } from '../common/relay-chat-store.js';
|
|
2
|
+
import type { RelayChatLine } from '../common/relay-room-chat.js';
|
|
3
|
+
/**
|
|
4
|
+
* The key what is said in the rooms is sealed with on this computer (docs/design/ta-chat-history.md 12): in the
|
|
5
|
+
* keychain of macOS (the service `Ta chats`, an account of the agent directory), elsewhere or with
|
|
6
|
+
* `TA_CHATS_KEY=file` in `chats/.key`, readable by the user alone. Made the first time.
|
|
7
|
+
*/
|
|
8
|
+
export declare namespace RelayChatKeys {
|
|
9
|
+
const SERVICE = "Ta chats";
|
|
10
|
+
function local(agentDir: string, env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): () => Promise<Uint8Array>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* What is said in the rooms, kept in `chats/<room>/` of the agent directory (docs/design/ta-chat-history.md 8): a
|
|
14
|
+
* file a month (`2026-11.log`), lines only added, each sealed on its own (AES-256-GCM), so that a line cut short by a
|
|
15
|
+
* crash loses that line alone; and the state of the log (`state`), sealed, written whole. A month goes as a file.
|
|
16
|
+
*/
|
|
17
|
+
export declare class RelayChatFiles implements RelayChatStore {
|
|
18
|
+
readonly dir: string;
|
|
19
|
+
protected readonly key: () => Promise<Uint8Array>;
|
|
20
|
+
protected readonly now: () => number;
|
|
21
|
+
/** The lines of each room read, as long as nothing was added that is not in them. */
|
|
22
|
+
protected readonly cache: Map<string, {
|
|
23
|
+
lines: RelayChatLine[];
|
|
24
|
+
months: number;
|
|
25
|
+
all: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
constructor(dir: string, key: () => Promise<Uint8Array>, now?: () => number);
|
|
28
|
+
read(room: string, options: {
|
|
29
|
+
before?: string;
|
|
30
|
+
limit: number;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
lines: RelayChatLine[];
|
|
33
|
+
more: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
append(room: string, lines: RelayChatLine[]): Promise<void>;
|
|
36
|
+
state(room: string): Promise<RelayChatState | undefined>;
|
|
37
|
+
setState(room: string, state: RelayChatState): Promise<void>;
|
|
38
|
+
forget(room: string): Promise<void>;
|
|
39
|
+
protected roomDir(room: string): string;
|
|
40
|
+
/** The months kept of a room, the earliest first. */
|
|
41
|
+
protected months(room: string): string[];
|
|
42
|
+
protected readMonth(room: string, month: string): Promise<RelayChatLine[]>;
|
|
43
|
+
/** base64url of a nonce of 12 bytes, the text sealed and its tag. */
|
|
44
|
+
static seal(key: Uint8Array, text: string): string;
|
|
45
|
+
static open(key: Uint8Array, sealed: string): string | undefined;
|
|
46
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto';
|
|
3
|
+
import { appendFileSync, chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { RelayChatStoreUtil } from '../common/relay-chat-store.js';
|
|
6
|
+
const ROOM = /^[A-Za-z0-9_-]{1,64}$/;
|
|
7
|
+
const MONTH = /^\d{4}-\d{2}\.log$/;
|
|
8
|
+
const STATE = 'state';
|
|
9
|
+
/**
|
|
10
|
+
* The key what is said in the rooms is sealed with on this computer (docs/design/ta-chat-history.md 12): in the
|
|
11
|
+
* keychain of macOS (the service `Ta chats`, an account of the agent directory), elsewhere or with
|
|
12
|
+
* `TA_CHATS_KEY=file` in `chats/.key`, readable by the user alone. Made the first time.
|
|
13
|
+
*/
|
|
14
|
+
export var RelayChatKeys;
|
|
15
|
+
(function (RelayChatKeys) {
|
|
16
|
+
RelayChatKeys.SERVICE = 'Ta chats';
|
|
17
|
+
function local(agentDir, env = process.env, platform = process.platform) {
|
|
18
|
+
let key;
|
|
19
|
+
return () => {
|
|
20
|
+
key !== null && key !== void 0 ? key : (key = platform === 'darwin' && env.TA_CHATS_KEY !== 'file' ? keychain(agentDir).catch(() => file(agentDir)) : file(agentDir));
|
|
21
|
+
return key;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
RelayChatKeys.local = local;
|
|
25
|
+
async function keychain(agentDir) {
|
|
26
|
+
const account = createHash('sha256').update(agentDir).digest('base64url').slice(0, 16);
|
|
27
|
+
const found = await run('security', ['find-generic-password', '-s', RelayChatKeys.SERVICE, '-a', account, '-w']);
|
|
28
|
+
const kept = found.code === 0 ? Buffer.from(found.stdout.trim(), 'base64') : undefined;
|
|
29
|
+
if ((kept === null || kept === void 0 ? void 0 : kept.length) === 32) {
|
|
30
|
+
return kept;
|
|
31
|
+
}
|
|
32
|
+
const made = randomBytes(32);
|
|
33
|
+
// Through the input of `security -i`: a key never goes in the arguments, which `ps` shows.
|
|
34
|
+
await run('security', ['-i'], `add-generic-password -U -s "${RelayChatKeys.SERVICE}" -a "${account}" -l "${RelayChatKeys.SERVICE}" -w "${made.toString('base64')}"\n`);
|
|
35
|
+
const again = await run('security', ['find-generic-password', '-s', RelayChatKeys.SERVICE, '-a', account, '-w']);
|
|
36
|
+
if (again.code !== 0 || Buffer.from(again.stdout.trim(), 'base64').length !== 32) {
|
|
37
|
+
throw new Error('The keychain did not keep the key of the chats.');
|
|
38
|
+
}
|
|
39
|
+
return Buffer.from(again.stdout.trim(), 'base64');
|
|
40
|
+
}
|
|
41
|
+
async function file(agentDir) {
|
|
42
|
+
const dir = join(agentDir, 'chats');
|
|
43
|
+
const path = join(dir, '.key');
|
|
44
|
+
if (existsSync(path)) {
|
|
45
|
+
const kept = Buffer.from(readFileSync(path, 'utf8').trim(), 'base64');
|
|
46
|
+
if (kept.length === 32) {
|
|
47
|
+
return kept;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
51
|
+
const made = randomBytes(32);
|
|
52
|
+
writeFileSync(path, `${made.toString('base64')}\n`, { mode: 0o600 });
|
|
53
|
+
chmodSync(path, 0o600);
|
|
54
|
+
return made;
|
|
55
|
+
}
|
|
56
|
+
function run(command, args, input) {
|
|
57
|
+
return new Promise(resolve => {
|
|
58
|
+
const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'ignore'] });
|
|
59
|
+
let stdout = '';
|
|
60
|
+
child.stdout.on('data', chunk => {
|
|
61
|
+
stdout += chunk;
|
|
62
|
+
});
|
|
63
|
+
child.on('error', () => resolve({ code: -1, stdout }));
|
|
64
|
+
child.on('close', code => resolve({ code: code !== null && code !== void 0 ? code : -1, stdout }));
|
|
65
|
+
child.stdin.end(input !== null && input !== void 0 ? input : '');
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
})(RelayChatKeys || (RelayChatKeys = {}));
|
|
69
|
+
/**
|
|
70
|
+
* What is said in the rooms, kept in `chats/<room>/` of the agent directory (docs/design/ta-chat-history.md 8): a
|
|
71
|
+
* file a month (`2026-11.log`), lines only added, each sealed on its own (AES-256-GCM), so that a line cut short by a
|
|
72
|
+
* crash loses that line alone; and the state of the log (`state`), sealed, written whole. A month goes as a file.
|
|
73
|
+
*/
|
|
74
|
+
export class RelayChatFiles {
|
|
75
|
+
constructor(dir, key, now = Date.now) {
|
|
76
|
+
this.dir = dir;
|
|
77
|
+
this.key = key;
|
|
78
|
+
this.now = now;
|
|
79
|
+
/** The lines of each room read, as long as nothing was added that is not in them. */
|
|
80
|
+
this.cache = new Map();
|
|
81
|
+
}
|
|
82
|
+
async read(room, options) {
|
|
83
|
+
const months = this.months(room);
|
|
84
|
+
const cached = this.cache.get(room);
|
|
85
|
+
let read = cached !== null && cached !== void 0 ? cached : { lines: [], months: 0, all: months.length === 0 };
|
|
86
|
+
const enough = () => {
|
|
87
|
+
const end = options.before === undefined ? read.lines.length : read.lines.findIndex(line => line.id === options.before);
|
|
88
|
+
return end >= 0 && end - options.limit > 0;
|
|
89
|
+
};
|
|
90
|
+
while (!read.all && !enough()) {
|
|
91
|
+
const month = months[months.length - 1 - read.months];
|
|
92
|
+
const lines = await this.readMonth(room, month);
|
|
93
|
+
read = { lines: [...lines, ...read.lines], months: read.months + 1, all: read.months + 1 >= months.length };
|
|
94
|
+
}
|
|
95
|
+
this.cache.set(room, read);
|
|
96
|
+
const page = RelayChatStoreUtil.page(read.lines, options);
|
|
97
|
+
return { lines: page.lines, more: page.more || !read.all };
|
|
98
|
+
}
|
|
99
|
+
async append(room, lines) {
|
|
100
|
+
if (!lines.length) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const key = await this.key();
|
|
104
|
+
const dir = this.roomDir(room);
|
|
105
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
106
|
+
const date = new Date(this.now());
|
|
107
|
+
const month = `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, '0')}.log`;
|
|
108
|
+
const path = join(dir, month);
|
|
109
|
+
const fresh = !existsSync(path);
|
|
110
|
+
appendFileSync(path, lines.map(line => `${RelayChatFiles.seal(key, JSON.stringify(line))}\n`).join(''), { mode: 0o600 });
|
|
111
|
+
const cached = this.cache.get(room);
|
|
112
|
+
if (cached) {
|
|
113
|
+
cached.lines.push(...lines);
|
|
114
|
+
if (fresh) {
|
|
115
|
+
cached.months++;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async state(room) {
|
|
120
|
+
const path = join(this.roomDir(room), STATE);
|
|
121
|
+
if (!existsSync(path)) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
const opened = RelayChatFiles.open(await this.key(), readFileSync(path, 'utf8').trim());
|
|
125
|
+
if (!opened) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const state = JSON.parse(opened);
|
|
129
|
+
return { ...state, seq: Number(state.seq) || 0, outbox: Array.isArray(state.outbox) ? state.outbox : [] };
|
|
130
|
+
}
|
|
131
|
+
async setState(room, state) {
|
|
132
|
+
const key = await this.key();
|
|
133
|
+
const dir = this.roomDir(room);
|
|
134
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
135
|
+
const path = join(dir, STATE);
|
|
136
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
137
|
+
writeFileSync(temporary, `${RelayChatFiles.seal(key, JSON.stringify(state))}\n`, { mode: 0o600 });
|
|
138
|
+
renameSync(temporary, path);
|
|
139
|
+
}
|
|
140
|
+
async forget(room) {
|
|
141
|
+
this.cache.delete(room);
|
|
142
|
+
rmSync(this.roomDir(room), { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
roomDir(room) {
|
|
145
|
+
if (!ROOM.test(room)) {
|
|
146
|
+
throw new Error('Not a room.');
|
|
147
|
+
}
|
|
148
|
+
return join(this.dir, room);
|
|
149
|
+
}
|
|
150
|
+
/** The months kept of a room, the earliest first. */
|
|
151
|
+
months(room) {
|
|
152
|
+
try {
|
|
153
|
+
return readdirSync(this.roomDir(room))
|
|
154
|
+
.filter(name => MONTH.test(name))
|
|
155
|
+
.sort();
|
|
156
|
+
}
|
|
157
|
+
catch (_a) {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async readMonth(room, month) {
|
|
162
|
+
const key = await this.key();
|
|
163
|
+
const lines = [];
|
|
164
|
+
for (const sealed of readFileSync(join(this.roomDir(room), month), 'utf8').split('\n')) {
|
|
165
|
+
const opened = sealed && RelayChatFiles.open(key, sealed);
|
|
166
|
+
if (opened) {
|
|
167
|
+
try {
|
|
168
|
+
lines.push(JSON.parse(opened));
|
|
169
|
+
}
|
|
170
|
+
catch (_a) {
|
|
171
|
+
// A line cut short: that line alone is lost.
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return lines;
|
|
176
|
+
}
|
|
177
|
+
/** base64url of a nonce of 12 bytes, the text sealed and its tag. */
|
|
178
|
+
static seal(key, text) {
|
|
179
|
+
const iv = randomBytes(12);
|
|
180
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
181
|
+
const sealed = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
|
|
182
|
+
return Buffer.concat([iv, sealed, cipher.getAuthTag()]).toString('base64url');
|
|
183
|
+
}
|
|
184
|
+
static open(key, sealed) {
|
|
185
|
+
try {
|
|
186
|
+
const bytes = Buffer.from(sealed, 'base64url');
|
|
187
|
+
if (bytes.length < 28) {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
const decipher = createDecipheriv('aes-256-gcm', key, bytes.subarray(0, 12));
|
|
191
|
+
decipher.setAuthTag(bytes.subarray(bytes.length - 16));
|
|
192
|
+
return Buffer.concat([decipher.update(bytes.subarray(12, bytes.length - 16)), decipher.final()]).toString('utf8');
|
|
193
|
+
}
|
|
194
|
+
catch (_a) {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=relay-chat-files.js.map
|