ai_co_discuss 0.4.62 → 0.4.63
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/package.json +1 -1
- package/runner/relay_channel.mjs +21 -3
- package/web/app.js +51 -6
- package/web/modern.css +1 -1
- package/web/modern.js +6 -4
package/package.json
CHANGED
package/runner/relay_channel.mjs
CHANGED
|
@@ -33,6 +33,13 @@ const EVENT_RETRY_MS = 3000;
|
|
|
33
33
|
const SLOW_REQUEST_MS = 3000;
|
|
34
34
|
// A WebSocket frame on the website is at most 1 MiB; leave room for the envelope around the ciphertext.
|
|
35
35
|
const RESPONSE_MAX_BYTES = 700 * 1024;
|
|
36
|
+
// The website closes the whole connection on a frame over 1 MiB, failing every request on it; a larger answer is
|
|
37
|
+
// replaced by an error so only that request fails.
|
|
38
|
+
const FRAME_MAX_BYTES = 1024 * 1024 - 1024;
|
|
39
|
+
// Diagnostics: answers left unsent beyond this are written to the service log, at most once a minute; a connection
|
|
40
|
+
// filled with large answers (images) holds back every other answer and the heartbeat behind them.
|
|
41
|
+
const BACKLOG_LOG_BYTES = 4 * 1024 * 1024;
|
|
42
|
+
const BACKLOG_LOG_MS = 60000;
|
|
36
43
|
const ROOM_API_PATH = /^\/api\/(rooms|folders|revisions)(\/[A-Za-z0-9_.~-]+)*(\?[A-Za-z0-9_.~%=&,:+-]*)?$/;
|
|
37
44
|
// Attachment uploads arrive in parts and are sent to the local website whole, as one browser upload would be.
|
|
38
45
|
const UPLOAD_PATH = /^\/api\/rooms\/[A-Za-z0-9_-]+\/attachments(\/[A-Za-z0-9_-]+\/import)?(\?[A-Za-z0-9_.~%=&,:+-]*)?$/;
|
|
@@ -212,7 +219,11 @@ export async function answer_request(home, site, link, frame, { run = run_launch
|
|
|
212
219
|
}
|
|
213
220
|
// Only what kind of request it was and how it ended: never its content or query.
|
|
214
221
|
report?.({ action: request.action, route: typeof request.payload?.path === 'string' ? request.payload.path.split('?')[0] : '', status: response.status });
|
|
215
|
-
|
|
222
|
+
const answer = { type: 'response', id, envelope: await seal_compressed(key, response, `${id}:response`) };
|
|
223
|
+
if (JSON.stringify(answer).length <= FRAME_MAX_BYTES) return answer;
|
|
224
|
+
report?.({ action: request.action, route: typeof request.payload?.path === 'string' ? request.payload.path.split('?')[0] : '', status: 413 });
|
|
225
|
+
return { type: 'response', id, oversized: true,
|
|
226
|
+
envelope: await seal_compressed(key, { status: 413, body: { error: '回应超过中转单帧上限,未能送出' } }, `${id}:response`) };
|
|
216
227
|
}
|
|
217
228
|
|
|
218
229
|
/** Pass the local website's change notices to the devices while the relay is connected. */
|
|
@@ -327,7 +338,7 @@ async function hold_channel(home, site, { connected, log, signal }) {
|
|
|
327
338
|
};
|
|
328
339
|
// Only a pong proves both directions work: requests can keep arriving while this computer's answers are stuck.
|
|
329
340
|
// A website that never answers pings is not judged by them.
|
|
330
|
-
let opened = false, awaiting_pong = false, pong_seen = false;
|
|
341
|
+
let opened = false, awaiting_pong = false, pong_seen = false, backlog_logged = 0;
|
|
331
342
|
socket.addEventListener('open', () => {
|
|
332
343
|
opened = true;
|
|
333
344
|
connected.add(site);
|
|
@@ -363,8 +374,15 @@ async function hold_channel(home, site, { connected, log, signal }) {
|
|
|
363
374
|
const answer = await answer_request(home, site, link, frame, { report: value => { kind = value; } });
|
|
364
375
|
const answered = Date.now();
|
|
365
376
|
const open = socket.readyState === WebSocket.OPEN;
|
|
366
|
-
|
|
377
|
+
const { oversized, ...frame_out } = answer;
|
|
378
|
+
if (open) socket.send(JSON.stringify(frame_out));
|
|
367
379
|
const label = `${kind.action}${kind.route ? ` ${kind.route}` : ''} → ${kind.status}`;
|
|
380
|
+
if (oversized) log(`${site}: 中转回应超过单帧上限,已改为报错:${label}`);
|
|
381
|
+
const backlog = socket.bufferedAmount || 0;
|
|
382
|
+
if (open && backlog > BACKLOG_LOG_BYTES && answered - backlog_logged > BACKLOG_LOG_MS) {
|
|
383
|
+
backlog_logged = answered;
|
|
384
|
+
log(`${site}: 中转发送积压 ${(backlog / 1048576).toFixed(1)} MB(最近:${label})`);
|
|
385
|
+
}
|
|
368
386
|
if (!open) log(`${site}: 中转回应未送出(连接已断开):${label},处理 ${answered - received} ms`);
|
|
369
387
|
else if (answered - received > SLOW_REQUEST_MS) log(`${site}: 中转请求较慢:${label},处理 ${answered - received} ms`);
|
|
370
388
|
} catch (error) { log(`${site}: 中转请求 ${frame.id} 无法处理(${Date.now() - received} ms):${error.message}`); }
|
package/web/app.js
CHANGED
|
@@ -76,12 +76,22 @@ const ROOM_RELAY_PATH = /^\/api\/(rooms|folders|revisions)(\/|\?|$)/;
|
|
|
76
76
|
// One part is base64-encoded, encrypted and base64-encoded again (about 16/9 of its size), and the sealed request
|
|
77
77
|
// must stay inside the hosted site's 512 KiB request limit (backend/worker.mjs MAX_BODY_BYTES).
|
|
78
78
|
const RELAY_UPLOAD_PART_BYTES = 256 * 1024;
|
|
79
|
+
// Files of the computer's rooms come back in frames of about 1 MB over the one relay connection that also carries every
|
|
80
|
+
// room read: only this many are read at once, so a room full of images cannot hold back its messages until they time out
|
|
81
|
+
// (2026-11-16: opening a room with 51 screenshots read 46 MB at once; message reads and sends timed out, and the retried
|
|
82
|
+
// parts were sent again).
|
|
83
|
+
const RELAY_FILE_CONCURRENCY = 2;
|
|
84
|
+
// Images and media of the computer's rooms are read once they come this near the screen, not all when a room opens.
|
|
85
|
+
const RELAY_MEDIA_MARGIN = "1000px 0px";
|
|
79
86
|
const COMPUTER_UNREACHABLE = t('电脑未连接:请先与电脑配对,并确认电脑开机且运行 codiscuss');
|
|
80
87
|
let local_connection = null;
|
|
81
88
|
let computer_connecting = false;
|
|
82
89
|
let relay_machine = null;
|
|
83
90
|
// Object URLs of files read from the computer's rooms, by their room route.
|
|
84
91
|
const relay_files = new Map();
|
|
92
|
+
// Relay file reads waiting for a free slot, and how many are running (RELAY_FILE_CONCURRENCY).
|
|
93
|
+
const relay_file_queue = [];
|
|
94
|
+
let relay_file_running = 0;
|
|
85
95
|
// The room list and room views last read, with their ETags, by route; an unchanged read is answered 304 and reuses them.
|
|
86
96
|
const ROOM_READ_PATH = /^\/api\/rooms(\/[^/?]+)?$/;
|
|
87
97
|
const room_reads = new Map();
|
|
@@ -2310,7 +2320,7 @@ function close_attachment_preview() {
|
|
|
2310
2320
|
*/
|
|
2311
2321
|
async function attachment_url(file) {
|
|
2312
2322
|
const path = `/api/rooms/${encodeURIComponent(file.room_id)}/attachments/${encodeURIComponent(file.id)}/content`;
|
|
2313
|
-
return state.remote_rooms ? relay_file_url(path) : path;
|
|
2323
|
+
return state.remote_rooms ? relay_file_url(path, { urgent: true }) : path;
|
|
2314
2324
|
}
|
|
2315
2325
|
|
|
2316
2326
|
async function preview_attachment(input) {
|
|
@@ -3064,8 +3074,9 @@ async function relay_exchange(action, payload, wait) {
|
|
|
3064
3074
|
const { key, envelope } = await seal_relay_request(machine, id, { action, payload, ...target });
|
|
3065
3075
|
const result = await request_json(`/api/machines/${encodeURIComponent(machine.id)}/relay`, { id, envelope, ...(wait ? { wait } : {}) })
|
|
3066
3076
|
.catch(error => {
|
|
3067
|
-
// A request given up in the network may still have reached the computer, so its outcome is unconfirmed.
|
|
3068
|
-
|
|
3077
|
+
// A request given up in the network may still have reached the computer, so its outcome is unconfirmed. Safari
|
|
3078
|
+
// reports the timeout as a plain AbortError ("Fetch is aborted"); the timeout is this request's only abort.
|
|
3079
|
+
if (error.name === "TimeoutError" || error.name === "AbortError") throw Object.assign(new Error(t('结果待确认:电脑「{value1}」未在限时内答复,请稍后查看房间,确认是否已生效后再重试', { value1: machine.name })),
|
|
3069
3080
|
{ unconfirmed: true, launch: { stage: "unconfirmed" } });
|
|
3070
3081
|
throw error.status === 401 ? Object.assign(error, { need: "pairing" }) : error;
|
|
3071
3082
|
});
|
|
@@ -3201,24 +3212,58 @@ async function read_room_file(path) {
|
|
|
3201
3212
|
return new Uint8Array(await response.arrayBuffer());
|
|
3202
3213
|
}
|
|
3203
3214
|
|
|
3215
|
+
/** Run one relay file read when a slot is free; a file the user opened (`urgent`) goes ahead of images on the page. */
|
|
3216
|
+
function queue_relay_file(read, urgent) {
|
|
3217
|
+
return new Promise((resolve, reject) => {
|
|
3218
|
+
const job = () => {
|
|
3219
|
+
relay_file_running += 1;
|
|
3220
|
+
read().then(resolve, reject).finally(() => { relay_file_running -= 1; relay_file_queue.shift()?.(); });
|
|
3221
|
+
};
|
|
3222
|
+
if (relay_file_running < RELAY_FILE_CONCURRENCY) job();
|
|
3223
|
+
else if (urgent) relay_file_queue.unshift(job);
|
|
3224
|
+
else relay_file_queue.push(job);
|
|
3225
|
+
});
|
|
3226
|
+
}
|
|
3227
|
+
|
|
3204
3228
|
/** Read one file of the computer's rooms through the relay once, and keep it as an object URL for this page. */
|
|
3205
|
-
function relay_file_url(path) {
|
|
3229
|
+
function relay_file_url(path, { urgent = false } = {}) {
|
|
3206
3230
|
if (!relay_files.has(path)) {
|
|
3207
|
-
relay_files.set(path, relay_room_bytes(path).then(blob => URL.createObjectURL(blob))
|
|
3231
|
+
relay_files.set(path, queue_relay_file(() => relay_room_bytes(path), urgent).then(blob => URL.createObjectURL(blob))
|
|
3208
3232
|
.catch(error => { relay_files.delete(path); throw error; }));
|
|
3209
3233
|
}
|
|
3210
3234
|
return relay_files.get(path);
|
|
3211
3235
|
}
|
|
3212
3236
|
|
|
3237
|
+
/** Read a relayed image or media file once it nears the screen. */
|
|
3238
|
+
const relay_media_observer = typeof IntersectionObserver === "function" ? new IntersectionObserver((entries) => {
|
|
3239
|
+
for (const entry of entries) {
|
|
3240
|
+
if (!entry.isIntersecting) continue;
|
|
3241
|
+
relay_media_observer.unobserve(entry.target);
|
|
3242
|
+
load_relay_media(entry.target);
|
|
3243
|
+
}
|
|
3244
|
+
}, { rootMargin: RELAY_MEDIA_MARGIN }) : null;
|
|
3245
|
+
|
|
3246
|
+
/** Give one relayed image or media element its file; one that cannot be read keeps its name. */
|
|
3247
|
+
function load_relay_media(media) {
|
|
3248
|
+
if (!media.isConnected || media.getAttribute("src")) return;
|
|
3249
|
+
relay_file_url(media.dataset.relaySrc).then(url => { media.src = url; }).catch(() => { media.dataset.relayFailed = "1"; });
|
|
3250
|
+
}
|
|
3251
|
+
|
|
3213
3252
|
/** Show images of the computer's rooms as they appear; one that cannot be read keeps its name. */
|
|
3214
3253
|
// Only the added nodes are searched: a whole-document query on every change walks the entire transcript.
|
|
3215
3254
|
function show_relay_images(records) {
|
|
3216
3255
|
const selector = ":is(img, video, audio)[data-relay-src]:not([src]):not([data-relay-failed])";
|
|
3217
3256
|
for (const record of records) {
|
|
3257
|
+
// A node taken off the page before it neared the screen is no longer watched.
|
|
3258
|
+
if (relay_media_observer) for (const node of record.removedNodes) {
|
|
3259
|
+
if (node.nodeType !== Node.ELEMENT_NODE) continue;
|
|
3260
|
+
for (const image of node.matches("[data-relay-src]") ? [node] : node.querySelectorAll("[data-relay-src]")) relay_media_observer.unobserve(image);
|
|
3261
|
+
}
|
|
3218
3262
|
for (const node of record.addedNodes) {
|
|
3219
3263
|
if (node.nodeType !== Node.ELEMENT_NODE || !node.isConnected) continue;
|
|
3220
3264
|
for (const image of node.matches(selector) ? [node] : node.querySelectorAll(selector)) {
|
|
3221
|
-
|
|
3265
|
+
if (relay_media_observer) relay_media_observer.observe(image);
|
|
3266
|
+
else load_relay_media(image);
|
|
3222
3267
|
}
|
|
3223
3268
|
}
|
|
3224
3269
|
}
|