gcf-board 0.18.4 → 0.18.5
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/dist/server/app.js +52 -20
- package/dist/server/auth.js +3 -3
- package/dist/server/events.js +6 -1
- package/dist/server/moves.js +16 -2
- package/dist/server/talks.js +40 -28
- package/dist/server/ws.js +187 -0
- package/dist/web/assets/index-CeIBZt2A.css +1 -0
- package/dist/web/assets/index-Pv8r1y-f.js +88 -0
- package/dist/web/index.html +2 -2
- package/node_modules/gcf-session/lib/chat.js +12 -2
- package/node_modules/gcf-session/package.json +1 -1
- package/package.json +1 -1
- package/dist/web/assets/index-CV00U5y_.js +0 -81
- package/dist/web/assets/index-r-gA2aJJ.css +0 -1
package/dist/server/app.js
CHANGED
|
@@ -12,7 +12,8 @@ import { pipeline } from 'node:stream/promises';
|
|
|
12
12
|
import os from 'node:os';
|
|
13
13
|
import path from 'node:path';
|
|
14
14
|
import { BOARD_SCHEMA, runModeOf, toolOf, } from '../shared/board.js';
|
|
15
|
-
import { admit, cookieName, hostAllowed, tokenInUrl } from './auth.js';
|
|
15
|
+
import { admit, cookieName, fromPage, hostAllowed, tokenInUrl } from './auth.js';
|
|
16
|
+
import { upgrade } from './ws.js';
|
|
16
17
|
import { briefError, refusalOf, runCli } from './cli.js';
|
|
17
18
|
import { broadcast, planRev, SESSION_TTL_MIN, subscribe, watchProject } from './events.js';
|
|
18
19
|
import { attachesUnit, CHAT_BODY_MAX, chatInput, IMAGE_EXT, talkInput, withUploads, } from './chat.js';
|
|
@@ -24,7 +25,7 @@ import { addProject, homeDir, listProjects, projectOf, removeProject, yoloFile,
|
|
|
24
25
|
import { allThemes } from './themes.js';
|
|
25
26
|
import { askIfDue, askUsage, configureUsage, lastUsage } from './usage.js';
|
|
26
27
|
import { adoptRuns, busyCheck, keepProjects, sweepRuns, closeReport, forgetProject, resumeQueue, runsOf, setAutopilot, startRun, unqueue, } from './runs.js';
|
|
27
|
-
import { acceptProposal, agentDefaults, agentPresets, chatCard,
|
|
28
|
+
import { acceptProposal, agentDefaults, agentPresets, chatCard, deniedGroupIn, lastDeniedOf, lastRunEnd, chatOf, entryTalk, listTalks, openTalk, pickTalk, prepareTalk, startDesign, startTalk, stopTalk, talkEntry, talkId, talkLive, talkSaid, talkWaits, watchTalks, writeTalk, } from './talks.js';
|
|
28
29
|
import { threadDir, threadOfEntry } from 'gcf-session';
|
|
29
30
|
const send = (res, status, body) => {
|
|
30
31
|
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
@@ -94,7 +95,9 @@ const questionText = (b, move, id) => {
|
|
|
94
95
|
return (b.questions ?? []).find((q) => q.id === id)?.question ?? null;
|
|
95
96
|
};
|
|
96
97
|
/** The most paths one edits diff takes: a refusal past this many is read in the console. */
|
|
97
|
-
const EDIT_PATHS_MAX =
|
|
98
|
+
const EDIT_PATHS_MAX = 2000;
|
|
99
|
+
/** The same for an older core, each path an argument (core 0.20.1). */
|
|
100
|
+
const EDIT_ARGS_MAX = 200;
|
|
98
101
|
const SLUG = /^[a-z0-9][a-z0-9-]*$/;
|
|
99
102
|
// A model or effort word for one run: plain, as the harness takes it.
|
|
100
103
|
const WORD = /^[A-Za-z0-9][\w.-]{0,63}$/;
|
|
@@ -417,6 +420,7 @@ export function createApp(o) {
|
|
|
417
420
|
// lock is this board's run or a console's.
|
|
418
421
|
const mine = new Set(own.runs.active.flatMap((r) => r.ids));
|
|
419
422
|
for (const i of board.items) {
|
|
423
|
+
const chat = path.join(p.path, '.gcf-session', 'runs', `${i.id}.chat.jsonl`);
|
|
420
424
|
if (i.running)
|
|
421
425
|
i.active_at = i.log ? activeAt(path.join(p.path, i.log)) : null;
|
|
422
426
|
// What it does now: its log's last tool call (Tails «Первым» 3).
|
|
@@ -424,19 +428,19 @@ export function createApp(o) {
|
|
|
424
428
|
i.doing = i.log ? doingOf(path.join(p.path, i.log)) : null;
|
|
425
429
|
// How its last run ended, while no run goes (Tails «Первым» 2).
|
|
426
430
|
else
|
|
427
|
-
i.run_end = lastRunEnd(
|
|
431
|
+
i.run_end = lastRunEnd(chat);
|
|
432
|
+
// Its last call a deny rule refused, running or not: one refused just before the run
|
|
433
|
+
// ended is told too; the page says each once.
|
|
434
|
+
i.denied = lastDeniedOf(chat);
|
|
428
435
|
// A card waits in its run's chat: it is in "waiting for me" (Board-PRD §1.3).
|
|
429
|
-
if (i.running)
|
|
430
|
-
const chat = path.join(p.path, '.gcf-session', 'runs', `${i.id}.chat.jsonl`);
|
|
436
|
+
if (i.running)
|
|
431
437
|
i.chat_card = chatCard(chat);
|
|
432
|
-
}
|
|
433
438
|
if (i.running_kind === 'run')
|
|
434
439
|
i.running_kind = mine.has(i.id) ? 'board' : 'console';
|
|
435
440
|
// A block for a refused command: the deny group its run's last refusal named, for
|
|
436
441
|
// "Allow <group> and run again" (docs/Autonomy-Plan.md, E). Never git-history.
|
|
437
442
|
if (i.status === 'blocked' && /needs permission/.test(i.blocked ?? '')) {
|
|
438
|
-
const
|
|
439
|
-
const group = deniedGroupOf(chatEvents(chat));
|
|
443
|
+
const group = deniedGroupIn(chat);
|
|
440
444
|
if (group)
|
|
441
445
|
i.denied_group = group;
|
|
442
446
|
}
|
|
@@ -816,30 +820,42 @@ export function createApp(o) {
|
|
|
816
820
|
out: r.out.trim(),
|
|
817
821
|
err: briefError(r.err),
|
|
818
822
|
...(refusal && { refusal }),
|
|
823
|
+
// acceptProposal wrote the outcome to the card; a new item's refused dry run did not.
|
|
824
|
+
...(body.proposal !== undefined && { held: true }),
|
|
819
825
|
});
|
|
820
826
|
},
|
|
821
827
|
],
|
|
822
828
|
[
|
|
823
829
|
// The hand edits a `dirty` refusal names (core 0.20): the staged part and the part not
|
|
824
830
|
// staged, read by `gcf edits diff`. Paths under gcf/ only, as the refusal gives them.
|
|
825
|
-
'
|
|
831
|
+
// A POST of `{paths}`: a refusal of many paths runs past a URL's header limit.
|
|
832
|
+
'POST',
|
|
826
833
|
/^\/api\/p\/([^/]+)\/edits\/diff$/,
|
|
827
|
-
async (
|
|
834
|
+
async (req, res, m) => {
|
|
828
835
|
const p = await projectOf(m[1]);
|
|
829
|
-
const paths =
|
|
830
|
-
//
|
|
831
|
-
//
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
+
const { paths } = await bodyOf(req);
|
|
837
|
+
// A core that takes them one a line (core 0.20.2) gets them on stdin; an older one each
|
|
838
|
+
// as `--path=`, a list Windows' 32K command line holds.
|
|
839
|
+
const onStdin = domains
|
|
840
|
+
.get(p.name)
|
|
841
|
+
?.moves?.['edits-commit']?.fields.some((f) => f.key === 'paths' && f.lines);
|
|
842
|
+
const most = onStdin ? EDIT_PATHS_MAX : EDIT_ARGS_MAX;
|
|
843
|
+
// The core takes only paths git names changed; these bounds keep a page gone wrong out.
|
|
844
|
+
if (!Array.isArray(paths) ||
|
|
845
|
+
!paths.length ||
|
|
846
|
+
paths.length > most ||
|
|
847
|
+
paths.some((x) => typeof x !== 'string' ||
|
|
848
|
+
!x.startsWith('gcf/') ||
|
|
849
|
+
/[\\\r\n]/.test(x) ||
|
|
850
|
+
x.split('/').includes('..')))
|
|
851
|
+
throw new HttpError(400, `paths: 1 to ${most} paths under gcf/, with /`);
|
|
836
852
|
const r = await cli(p, [
|
|
837
853
|
'gcf',
|
|
838
854
|
'edits',
|
|
839
855
|
'diff',
|
|
840
856
|
'--json',
|
|
841
|
-
...paths.map((x) => `--path=${x}`),
|
|
842
|
-
]);
|
|
857
|
+
...(onStdin ? ['--path-file=-'] : paths.map((x) => `--path=${x}`)),
|
|
858
|
+
], undefined, onStdin ? paths.join('\n') : undefined);
|
|
843
859
|
if (!r.ok)
|
|
844
860
|
throw new HttpError(400, briefError(r.err || r.out));
|
|
845
861
|
send(res, 200, jsonOf(r, 'gcf edits diff'));
|
|
@@ -1098,6 +1114,22 @@ export function createApp(o) {
|
|
|
1098
1114
|
send(res, e instanceof HttpError ? e.status : 500, { error: e.message });
|
|
1099
1115
|
});
|
|
1100
1116
|
});
|
|
1117
|
+
// The page's event streams, one WebSocket a page (ws.ts): Host and Origin the board's own.
|
|
1118
|
+
const pages = new Set();
|
|
1119
|
+
server.on('upgrade', (req, socket) => {
|
|
1120
|
+
pages.add(socket);
|
|
1121
|
+
socket.on('close', () => pages.delete(socket));
|
|
1122
|
+
upgrade(req, socket, o.port, (r) => hostAllowed(r, o.port) && fromPage(r, o.port));
|
|
1123
|
+
});
|
|
1124
|
+
// An upgraded socket is no HTTP request the server drops by itself: a close drops it too.
|
|
1125
|
+
for (const k of ['close', 'closeAllConnections']) {
|
|
1126
|
+
const own = server[k].bind(server);
|
|
1127
|
+
server[k] = ((...a) => {
|
|
1128
|
+
for (const s of pages)
|
|
1129
|
+
s.destroy();
|
|
1130
|
+
return own(...a);
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1101
1133
|
// A broken projects.json must not take the server down: the page shows the error instead.
|
|
1102
1134
|
server.on('listening', () => watchAll().catch((e) => console.error(`gcf-board: ${e.message}`)));
|
|
1103
1135
|
server.on('close', () => {
|
package/dist/server/auth.js
CHANGED
|
@@ -36,14 +36,14 @@ function cookieOf(req, name) {
|
|
|
36
36
|
}
|
|
37
37
|
const hostsOf = (port) => [`127.0.0.1:${port}`, `localhost:${port}`];
|
|
38
38
|
export const hostAllowed = (req, port) => hostsOf(port).includes(req.headers.host ?? '');
|
|
39
|
+
/** Whether a request comes from the board's own page: its Origin, which no other site can fake. */
|
|
40
|
+
export const fromPage = (req, port) => hostsOf(port).some((h) => req.headers.origin === `http://${h}`);
|
|
39
41
|
/** Whether a request may go on: its Host, its Origin on a write, its token. */
|
|
40
42
|
export function admit(req, port, token) {
|
|
41
|
-
const hosts = hostsOf(port);
|
|
42
43
|
if (!hostAllowed(req, port))
|
|
43
44
|
return { ok: false, status: 403, message: 'the board answers 127.0.0.1 and localhost only' };
|
|
44
45
|
const write = !['GET', 'HEAD'].includes(req.method ?? 'GET');
|
|
45
|
-
|
|
46
|
-
if (write && !(origin && hosts.some((h) => origin === `http://${h}`)))
|
|
46
|
+
if (write && !fromPage(req, port))
|
|
47
47
|
return { ok: false, status: 403, message: 'a change comes from the board page only' };
|
|
48
48
|
if (!same(cookieOf(req, cookieName(port)), token))
|
|
49
49
|
return { ok: false, status: 401, message: 'open the URL gcf-board printed at start' };
|
package/dist/server/events.js
CHANGED
|
@@ -103,7 +103,12 @@ export function watchProject(p, debounceMs = 300, ttlMin = () => SESSION_TTL_MIN
|
|
|
103
103
|
// Not there: the board read shows what is missing.
|
|
104
104
|
}
|
|
105
105
|
};
|
|
106
|
-
|
|
106
|
+
// gcf/.metrics is no board state: `gcf audit` appends to its logs on every call, and the run
|
|
107
|
+
// gate a drawer asks after each read is one; a read on it was a read every 2 s, for ever.
|
|
108
|
+
watchDir(path.join(p.path, 'gcf'), (file) => {
|
|
109
|
+
if (!/^\.metrics(?:[\\/]|$)/.test(file ?? ''))
|
|
110
|
+
fire();
|
|
111
|
+
});
|
|
107
112
|
// shortcut: a plan an item links outside .ai-factory/plans shows its new text only when its
|
|
108
113
|
// checkbox count moves; watch those files too if operators edit such plans by hand.
|
|
109
114
|
watchDir(path.join(p.path, '.ai-factory'), (file) => {
|
package/dist/server/moves.js
CHANGED
|
@@ -24,13 +24,22 @@ const opt = (b, key, flag = key) => {
|
|
|
24
24
|
const v = text(b, key);
|
|
25
25
|
return v === undefined ? [] : [`--${flag}=${v}`];
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
/** A list of text, its empty entries left out. */
|
|
28
|
+
const texts = (b, key) => {
|
|
28
29
|
const v = b[key];
|
|
29
30
|
if (v === undefined)
|
|
30
31
|
return [];
|
|
31
32
|
if (!Array.isArray(v) || v.some((x) => typeof x !== 'string'))
|
|
32
33
|
throw new HttpError(400, `${key} must be a list of text`);
|
|
33
|
-
return v.filter(Boolean)
|
|
34
|
+
return v.filter(Boolean);
|
|
35
|
+
};
|
|
36
|
+
const many = (b, key, flag = key) => texts(b, key).map((x) => `--${flag}=${x}`);
|
|
37
|
+
/** A list that goes on stdin one a line (`lines`): an entry holding a line break is no entry. */
|
|
38
|
+
const lines = (b, key) => {
|
|
39
|
+
const v = texts(b, key);
|
|
40
|
+
if (v.some((x) => /[\r\n]/.test(x)))
|
|
41
|
+
throw new HttpError(400, `${key}: an entry with a line break goes from a console`);
|
|
42
|
+
return v;
|
|
34
43
|
};
|
|
35
44
|
// The core limits a tag to 40 characters too.
|
|
36
45
|
const TAG = /^[a-z0-9][a-z0-9:-]{0,39}$/;
|
|
@@ -113,6 +122,8 @@ function fieldArgs(f, b) {
|
|
|
113
122
|
const flag = f.flag ?? f.key;
|
|
114
123
|
if (f.kind === 'slug')
|
|
115
124
|
return [];
|
|
125
|
+
if (f.kind === 'list' && f.lines)
|
|
126
|
+
return lines(b, f.key).length ? [`--${flag}-file=-`] : [];
|
|
116
127
|
if (f.kind === 'list')
|
|
117
128
|
return many(b, f.key, flag);
|
|
118
129
|
if (f.kind === 'remove')
|
|
@@ -145,6 +156,9 @@ export function moveInput(b, moves = {}) {
|
|
|
145
156
|
const cmd = typeof b.cmd === 'string' ? b.cmd : '';
|
|
146
157
|
if (!Object.hasOwn(moves, cmd))
|
|
147
158
|
return undefined;
|
|
159
|
+
const list = moves[cmd].fields.find((x) => x.kind === 'list' && x.lines && applies(x, b));
|
|
160
|
+
if (list)
|
|
161
|
+
return lines(b, list.key).join('\n') || undefined;
|
|
148
162
|
const f = moves[cmd].fields.find((x) => x.stdin && applies(x, b));
|
|
149
163
|
return f ? text(b, f.key) : undefined;
|
|
150
164
|
}
|
package/dist/server/talks.js
CHANGED
|
@@ -431,47 +431,58 @@ export function stopTalk(p, t) {
|
|
|
431
431
|
function eventsOf(td) {
|
|
432
432
|
return chatEvents(path.join(td, 'chat.jsonl'));
|
|
433
433
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
export function chatCard(file) {
|
|
434
|
+
const runs = new Map();
|
|
435
|
+
const NONE = { stamp: '', card: null, end: null, denied: null, group: null };
|
|
436
|
+
/** A run's chat file's facts, read again only when the file changed. */
|
|
437
|
+
function runChat(file) {
|
|
439
438
|
let stamp = '';
|
|
440
439
|
try {
|
|
441
440
|
const st = statSync(file);
|
|
442
441
|
stamp = `${st.mtimeMs}:${st.size}`;
|
|
443
442
|
}
|
|
444
443
|
catch {
|
|
445
|
-
return
|
|
444
|
+
return NONE;
|
|
446
445
|
}
|
|
447
|
-
const kept =
|
|
446
|
+
const kept = runs.get(file);
|
|
448
447
|
if (kept?.stamp === stamp)
|
|
449
|
-
return kept
|
|
450
|
-
const card = cardIn(chatEvents(file));
|
|
451
|
-
cards.set(file, { stamp, card });
|
|
452
|
-
return card;
|
|
453
|
-
}
|
|
454
|
-
const ends = new Map();
|
|
455
|
-
/** How the last run in a run's chat file ended (`run_end`), or null; read again only when it changed. */
|
|
456
|
-
export function lastRunEnd(file) {
|
|
457
|
-
let stamp = '';
|
|
458
|
-
try {
|
|
459
|
-
const st = statSync(file);
|
|
460
|
-
stamp = `${st.mtimeMs}:${st.size}`;
|
|
461
|
-
}
|
|
462
|
-
catch {
|
|
463
|
-
return null;
|
|
464
|
-
}
|
|
465
|
-
const kept = ends.get(file);
|
|
466
|
-
if (kept?.stamp === stamp)
|
|
467
|
-
return kept.end;
|
|
448
|
+
return kept;
|
|
468
449
|
const events = chatEvents(file);
|
|
469
450
|
const at = events.findLastIndex((e) => e.type === 'run_end');
|
|
470
451
|
// A session started after it: that run has not ended yet.
|
|
471
452
|
const later = events.slice(at + 1).some((e) => e.type === 'session');
|
|
472
453
|
const end = at < 0 || later ? null : events[at];
|
|
473
|
-
|
|
474
|
-
|
|
454
|
+
const facts = {
|
|
455
|
+
stamp,
|
|
456
|
+
card: cardIn(events),
|
|
457
|
+
end,
|
|
458
|
+
denied: lastDenied(events),
|
|
459
|
+
group: deniedGroupOf(events),
|
|
460
|
+
};
|
|
461
|
+
runs.set(file, facts);
|
|
462
|
+
return facts;
|
|
463
|
+
}
|
|
464
|
+
/** The card that waits in a run's chat file (`<id>.chat.jsonl`), or null. */
|
|
465
|
+
export const chatCard = (file) => runChat(file).card;
|
|
466
|
+
/** How the last run in a run's chat file ended (`run_end`), or null. */
|
|
467
|
+
export const lastRunEnd = (file) => runChat(file).end;
|
|
468
|
+
/** The deny group the last refusal of a run's chat file named (deniedGroupOf), or null. */
|
|
469
|
+
export const deniedGroupIn = (file) => runChat(file).group;
|
|
470
|
+
/** The last call a deny rule refused in a run's chat file, or null. */
|
|
471
|
+
export const lastDeniedOf = (file) => runChat(file).denied;
|
|
472
|
+
/**
|
|
473
|
+
* The last call a deny rule refused in a chat (`denied` `by: 'rule'`), as the page tells of it:
|
|
474
|
+
* the operator's own No, a timeout and an engine's refusal are not a rule's.
|
|
475
|
+
*/
|
|
476
|
+
export function lastDenied(events) {
|
|
477
|
+
const e = events.findLast((x) => x.type === 'denied' && x.by === 'rule');
|
|
478
|
+
if (e?.type !== 'denied' || !e.at)
|
|
479
|
+
return null;
|
|
480
|
+
return {
|
|
481
|
+
at: e.at,
|
|
482
|
+
tool: e.tool,
|
|
483
|
+
...(e.title && { title: e.title }),
|
|
484
|
+
...(e.group && { group: e.group }),
|
|
485
|
+
};
|
|
475
486
|
}
|
|
476
487
|
/** The events of a chat file: a thread's, or a run's `<id>.chat.jsonl`; none without one. */
|
|
477
488
|
/**
|
|
@@ -769,6 +780,7 @@ async function talkOf(p, th, ids, questionOf) {
|
|
|
769
780
|
card,
|
|
770
781
|
// A thread whose engine died during the wait waits for nothing.
|
|
771
782
|
limit_until: live ? limitIn(events) : null,
|
|
783
|
+
denied: lastDenied(events),
|
|
772
784
|
proposals: await proposalsOf(td, events, ids, questionOf, () => writeDir(p, th.id)),
|
|
773
785
|
};
|
|
774
786
|
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// One WebSocket per page carries all its event streams (Tails «Позже» 2). A browser keeps 6
|
|
2
|
+
// HTTP/1.1 connections a host for all its tabs, and an EventSource holds one for its life: with
|
|
3
|
+
// a few tabs open the board read no longer got through. WebSockets have a limit of their own,
|
|
4
|
+
// in the hundreds. Each stream is the same SSE route, asked by the server of itself on
|
|
5
|
+
// 127.0.0.1 with the page's cookie: the routes stay as they are, and their gate (Host, cookie)
|
|
6
|
+
// runs for every stream. The socket itself checks Host and Origin: a page of another site can
|
|
7
|
+
// open a WebSocket to 127.0.0.1 with the board's cookie, but it cannot fake its Origin.
|
|
8
|
+
//
|
|
9
|
+
// Page to server, a text frame each: {op: 'open', id, url}, {op: 'close', id}.
|
|
10
|
+
// Server to page: {id, open: true}, {id, data: '<SSE text as the route wrote it>'},
|
|
11
|
+
// {id, end: <status>}: 200 the stream dropped (the page opens it again), another the route
|
|
12
|
+
// refused it (the page gives it up, as an EventSource does), 0 the server could not be reached.
|
|
13
|
+
import { createHash } from 'node:crypto';
|
|
14
|
+
import { request } from 'node:http';
|
|
15
|
+
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
16
|
+
// A page sends only short JSON: anything longer, binary or fragmented is not the board's page.
|
|
17
|
+
const IN_MAX = 4096;
|
|
18
|
+
// A page opens a handful of streams: more is a page gone wrong.
|
|
19
|
+
const STREAMS_MAX = 64;
|
|
20
|
+
/** A server frame: unmasked, final, `op` text (1), close (8) or pong (10). */
|
|
21
|
+
export function frame(payload, op = 1) {
|
|
22
|
+
const body = typeof payload === 'string' ? Buffer.from(payload) : payload;
|
|
23
|
+
const n = body.length;
|
|
24
|
+
const head = n < 126
|
|
25
|
+
? Buffer.from([0x80 | op, n])
|
|
26
|
+
: n < 65536
|
|
27
|
+
? Buffer.from([0x80 | op, 126, n >> 8, n & 255])
|
|
28
|
+
: Buffer.concat([Buffer.from([0x80 | op, 127]), bigLength(n)]);
|
|
29
|
+
return Buffer.concat([head, body]);
|
|
30
|
+
}
|
|
31
|
+
const bigLength = (n) => {
|
|
32
|
+
const b = Buffer.alloc(8);
|
|
33
|
+
b.writeBigUInt64BE(BigInt(n));
|
|
34
|
+
return b;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The page's frames in `buf`, read as far as they are whole: each `{op, text}`, and the bytes
|
|
38
|
+
* left for the next chunk. `bad` for a frame the board's page never sends: unmasked, cut into
|
|
39
|
+
* fragments, binary, over IN_MAX.
|
|
40
|
+
*/
|
|
41
|
+
export function readFrames(buf) {
|
|
42
|
+
const frames = [];
|
|
43
|
+
let at = 0;
|
|
44
|
+
while (buf.length - at >= 2) {
|
|
45
|
+
const fin = buf[at] & 0x80;
|
|
46
|
+
const op = buf[at] & 0x0f;
|
|
47
|
+
const masked = buf[at + 1] & 0x80;
|
|
48
|
+
let n = buf[at + 1] & 0x7f;
|
|
49
|
+
let p = at + 2;
|
|
50
|
+
if (n === 126) {
|
|
51
|
+
if (buf.length - p < 2)
|
|
52
|
+
break;
|
|
53
|
+
n = buf.readUInt16BE(p);
|
|
54
|
+
p += 2;
|
|
55
|
+
}
|
|
56
|
+
else if (n === 127)
|
|
57
|
+
return { frames, rest: Buffer.alloc(0), bad: true };
|
|
58
|
+
if (!fin || !masked || n > IN_MAX || ![1, 8, 9, 10].includes(op))
|
|
59
|
+
return { frames, rest: Buffer.alloc(0), bad: true };
|
|
60
|
+
if (buf.length - p < 4 + n)
|
|
61
|
+
break;
|
|
62
|
+
const mask = buf.subarray(p, p + 4);
|
|
63
|
+
const body = Buffer.from(buf.subarray(p + 4, p + 4 + n));
|
|
64
|
+
for (let i = 0; i < n; i++)
|
|
65
|
+
body[i] ^= mask[i & 3];
|
|
66
|
+
frames.push({ op, text: body.toString('utf8') });
|
|
67
|
+
at = p + 4 + n;
|
|
68
|
+
}
|
|
69
|
+
return { frames, rest: buf.subarray(at), bad: false };
|
|
70
|
+
}
|
|
71
|
+
const refuse = (socket, status, text) => {
|
|
72
|
+
socket.end(`HTTP/1.1 ${status} ${text}\r\nconnection: close\r\ncontent-length: 0\r\n\r\n`);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A stream's path: one of the board's own API routes, never another host; printable ASCII only,
|
|
76
|
+
* as the page's `encodeURIComponent` gives it (`http.request` throws on anything else).
|
|
77
|
+
*/
|
|
78
|
+
export const apiPath = (url) => typeof url === 'string' && /^\/api\/[\x21-\x5b\x5d-\x7e]{1,2040}$/.test(url);
|
|
79
|
+
/**
|
|
80
|
+
* Take an upgrade to `/api/ws`, its Host and Origin checked by `allowed`: the page's streams
|
|
81
|
+
* from then on. Any other upgrade is refused.
|
|
82
|
+
*/
|
|
83
|
+
export function upgrade(req, socket, port, allowed) {
|
|
84
|
+
// Node drops its own error listener once it hands the socket over: a reset is no crash.
|
|
85
|
+
socket.on('error', () => socket.destroy());
|
|
86
|
+
// The gate first; a path is compared as it is, never parsed (`//` throws in `new URL`).
|
|
87
|
+
if (!allowed(req))
|
|
88
|
+
return refuse(socket, 403, 'Forbidden');
|
|
89
|
+
const key = req.headers['sec-websocket-key'];
|
|
90
|
+
if ((req.url ?? '').split('?')[0] !== '/api/ws' || typeof key !== 'string')
|
|
91
|
+
return refuse(socket, 404, 'Not Found');
|
|
92
|
+
const accept = createHash('sha1').update(`${key}${GUID}`).digest('base64');
|
|
93
|
+
socket.write(`HTTP/1.1 101 Switching Protocols\r\nupgrade: websocket\r\nconnection: Upgrade\r\nsec-websocket-accept: ${accept}\r\n\r\n`);
|
|
94
|
+
const streams = new Map();
|
|
95
|
+
let open = true;
|
|
96
|
+
const send = (msg) => open && socket.write(frame(JSON.stringify(msg)));
|
|
97
|
+
const end = (id, status) => {
|
|
98
|
+
if (!streams.delete(id))
|
|
99
|
+
return;
|
|
100
|
+
send({ id, end: status });
|
|
101
|
+
};
|
|
102
|
+
const start = (id, url) => {
|
|
103
|
+
const r = request({
|
|
104
|
+
host: '127.0.0.1',
|
|
105
|
+
port,
|
|
106
|
+
path: url,
|
|
107
|
+
// The page's own cookie and Host: the route's gate judges the stream as it would the page.
|
|
108
|
+
headers: {
|
|
109
|
+
cookie: req.headers.cookie ?? '',
|
|
110
|
+
host: req.headers.host,
|
|
111
|
+
accept: 'text/event-stream',
|
|
112
|
+
},
|
|
113
|
+
agent: false,
|
|
114
|
+
}, (res) => {
|
|
115
|
+
if (res.statusCode !== 200) {
|
|
116
|
+
res.resume();
|
|
117
|
+
return end(id, res.statusCode ?? 0);
|
|
118
|
+
}
|
|
119
|
+
send({ id, open: true });
|
|
120
|
+
res.setEncoding('utf8');
|
|
121
|
+
res.on('data', (data) => streams.has(id) && send({ id, data }));
|
|
122
|
+
res.on('close', () => end(id, 200));
|
|
123
|
+
});
|
|
124
|
+
r.on('error', () => end(id, 0));
|
|
125
|
+
r.end();
|
|
126
|
+
streams.set(id, r);
|
|
127
|
+
};
|
|
128
|
+
const close = () => {
|
|
129
|
+
if (!open)
|
|
130
|
+
return;
|
|
131
|
+
open = false;
|
|
132
|
+
// Each stream's own close drops it from the route's listeners (subscribe, follow).
|
|
133
|
+
for (const r of streams.values())
|
|
134
|
+
r.destroy();
|
|
135
|
+
streams.clear();
|
|
136
|
+
socket.destroy();
|
|
137
|
+
};
|
|
138
|
+
let held = Buffer.alloc(0);
|
|
139
|
+
socket.on('data', (chunk) => {
|
|
140
|
+
const { frames, rest, bad } = readFrames(Buffer.concat([held, chunk]));
|
|
141
|
+
held = rest;
|
|
142
|
+
if (bad)
|
|
143
|
+
return close();
|
|
144
|
+
for (const f of frames) {
|
|
145
|
+
if (f.op === 8) {
|
|
146
|
+
socket.write(frame('', 8));
|
|
147
|
+
return close();
|
|
148
|
+
}
|
|
149
|
+
if (f.op === 9)
|
|
150
|
+
socket.write(frame(f.text, 10));
|
|
151
|
+
if (f.op !== 1)
|
|
152
|
+
continue;
|
|
153
|
+
let m;
|
|
154
|
+
try {
|
|
155
|
+
m = JSON.parse(f.text);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return close();
|
|
159
|
+
}
|
|
160
|
+
const id = m.id;
|
|
161
|
+
if (typeof id !== 'number' || !Number.isInteger(id))
|
|
162
|
+
return close();
|
|
163
|
+
if (m.op === 'close') {
|
|
164
|
+
streams.get(id)?.destroy();
|
|
165
|
+
streams.delete(id);
|
|
166
|
+
}
|
|
167
|
+
else if (m.op === 'open' && apiPath(m.url)) {
|
|
168
|
+
// Asked twice (the socket came back as its retry was due): the one going stays.
|
|
169
|
+
if (streams.has(id))
|
|
170
|
+
continue;
|
|
171
|
+
if (streams.size >= STREAMS_MAX)
|
|
172
|
+
return close();
|
|
173
|
+
try {
|
|
174
|
+
start(id, m.url);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
// A request Node will not send (a header it refuses): refused, the page gives it up.
|
|
178
|
+
send({ id, end: 400 });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else
|
|
182
|
+
return close();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
socket.on('error', close);
|
|
186
|
+
socket.on('close', close);
|
|
187
|
+
}
|