visualproof 0.4.3 → 0.5.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/README.md +16 -7
- package/dist/assets/latex/visualproof.sty +19 -9
- package/dist/assets/tips/tour-agent.webp +0 -0
- package/dist/assets/tips/tour-arrange.webp +0 -0
- package/dist/assets/tips/tour-artwork.webp +0 -0
- package/dist/assets/tips/tour-checks.webp +0 -0
- package/dist/assets/tips/tour-keys.webp +0 -0
- package/dist/assets/tips/tour-library.webp +0 -0
- package/dist/assets/tips/tour-project.webp +0 -0
- package/dist/assets/tips/tour-review.webp +0 -0
- package/dist/assets/tips/tour-share.webp +0 -0
- package/dist/assets/tips/tour-source.webp +0 -0
- package/dist/assets/tips/tour-target.webp +0 -0
- package/dist/assets/tips/tour-view.webp +0 -0
- package/dist/assets/tips/tour.json +117 -112
- package/dist/assets/tips/tour.webp +0 -0
- package/dist/mcp/visualproof-mcp.mjs +7 -3
- package/dist/server/access.js +37 -1
- package/dist/server/accesspage.js +130 -0
- package/dist/server/agent.js +11 -5
- package/dist/server/agentlibrary.js +1 -1
- package/dist/server/agentviews.js +1 -1
- package/dist/server/artcmd.js +1 -1
- package/dist/server/askfollow.js +1 -4
- package/dist/server/buildwatch.js +133 -0
- package/dist/server/deploy.js +844 -0
- package/dist/server/doctor.js +15 -3
- package/dist/server/events.js +20 -5
- package/dist/server/export.js +17 -4
- package/dist/server/fetchurl.js +30 -13
- package/dist/server/film.js +1 -1
- package/dist/server/history.js +3 -1
- package/dist/server/historyapi.js +100 -50
- package/dist/server/historythread.js +206 -0
- package/dist/server/hover.js +8 -0
- package/dist/server/identity.js +25 -1
- package/dist/server/library.js +10 -3
- package/dist/server/live.js +206 -28
- package/dist/server/lostfound.js +3 -2
- package/dist/server/main.js +361 -50
- package/dist/server/poppler.js +48 -10
- package/dist/server/renames.js +10 -5
- package/dist/server/review.js +32 -2
- package/dist/server/reviewapi.js +1 -1
- package/dist/server/status.js +144 -50
- package/dist/server/talk.js +7 -2
- package/dist/server/usage.js +51 -0
- package/dist/server/views.js +21 -7
- package/dist/shared/board.js +2 -1
- package/dist/shared/collab.js +14 -8
- package/dist/shared/deploy.js +35 -0
- package/dist/shared/doc/latex.js +5 -3
- package/dist/shared/doc/outline.js +6 -3
- package/dist/shared/layout.js +297 -70
- package/dist/shared/libm.js +366 -0
- package/dist/shared/library.js +11 -6
- package/dist/shared/palette.js +1 -1
- package/dist/shared/review.js +7 -5
- package/dist/shared/search.js +7 -3
- package/dist/shared/separate.js +556 -32
- package/dist/shared/views.js +8 -1
- package/dist/web/bundle/app.js +3595 -1520
- package/dist/web/bundle/edges.js +44 -16
- package/dist/web/bundle/layout.js +1026 -80
- package/dist/web/index.html +2 -2
- package/dist/web/src/artwork.js +25 -4
- package/dist/web/src/axiomring.js +42 -0
- package/dist/web/src/bundles.js +52 -16
- package/dist/web/src/canvas.js +109 -54
- package/dist/web/src/overview.js +4 -1
- package/dist/web/src/past.js +28 -0
- package/dist/web/src/scene.js +13 -12
- package/dist/web/src/selection.js +47 -4
- package/dist/web/src/status.js +32 -2
- package/dist/web/src/theme.js +108 -0
- package/dist/web/style.css +181 -45
- package/docs/agent-api/following.md +1 -1
- package/docs/agent-api/history.md +3 -0
- package/docs/agent-api/library.md +1 -1
- package/docs/agent-api/requests.md +4 -1
- package/docs/agent-api/views.md +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// What a browser is shown where nothing lets it in (CollabSpec decisions 7 and 12, the owner's
|
|
2
|
+
// decision of 2026-10-18). A server with `--access-key-file` answered every request without the key
|
|
3
|
+
// or its cookie 404 `{"error":"not found"}`, as if there were nothing there: whoever opened the
|
|
4
|
+
// address without the link, or in a browser that had forgotten the cookie, took the service for
|
|
5
|
+
// down. A browser asking for a page is now shown a small page that says the site is private and
|
|
6
|
+
// what to do; everything else — the API, the event stream, agents, files — is answered as before.
|
|
7
|
+
//
|
|
8
|
+
// The page tells an outsider that a VisualProof server is there, and nothing else: it is the same
|
|
9
|
+
// bytes on every server, made from the variant alone (and whether the address alone opens the site,
|
|
10
|
+
// `--public`), never from the site — no project name, version, people, counts or anything the site
|
|
11
|
+
// holds. Of a link that does not open the site, it never says which it was (a key replaced, a
|
|
12
|
+
// personal or read-only link revoked, one never made): only someone who once held a real link could
|
|
13
|
+
// tell revoked from unknown, and the names of who revoked it are the site's.
|
|
14
|
+
//
|
|
15
|
+
// Self-contained: its style inline (allowed by its digest, nothing else), light and dark by the
|
|
16
|
+
// system's choice, no script, nothing fetched.
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { KIND_HEADER } from "./identity.js";
|
|
19
|
+
/** Whether a request is a browser asking for a page: GET or HEAD, accepting HTML, for a page's
|
|
20
|
+
* path — not the API (the event stream and the agents' documentation are in it), not a file (a
|
|
21
|
+
* name with an extension, but for `.html`; the fonts, the tips, pdf.js) — and from no agent: one
|
|
22
|
+
* that sends `Authorization: Bearer` or says it is one. A sign-in proxy's `Authorization: Basic`,
|
|
23
|
+
* passed on, is still a browser's. Anything else keeps the answer it had. */
|
|
24
|
+
export function asksForPage(req, path) {
|
|
25
|
+
if (req.method !== "GET" && req.method !== "HEAD")
|
|
26
|
+
return false;
|
|
27
|
+
if (/^Bearer\s/i.test(req.headers.authorization ?? ""))
|
|
28
|
+
return false;
|
|
29
|
+
const kind = req.headers[KIND_HEADER];
|
|
30
|
+
if ((Array.isArray(kind) ? kind[0] : kind)?.trim().toLowerCase() === "agent")
|
|
31
|
+
return false;
|
|
32
|
+
if (!acceptsHtml(req.headers.accept))
|
|
33
|
+
return false;
|
|
34
|
+
if (path === "/api" || path.startsWith("/api/") || /^\/(fonts|tips|pdfjs)\//.test(path))
|
|
35
|
+
return false;
|
|
36
|
+
const last = path.slice(path.lastIndexOf("/") + 1);
|
|
37
|
+
return !/\.[A-Za-z0-9]+$/.test(last) || /\.html$/i.test(last);
|
|
38
|
+
}
|
|
39
|
+
/** Whether an `Accept` header takes HTML: `text/html` among its media ranges, not at q=0. A browser
|
|
40
|
+
* navigating says so first; a script's fetch, curl and an `<img>` do not. */
|
|
41
|
+
export function acceptsHtml(accept) {
|
|
42
|
+
return (accept ?? "").split(",").some((range) => {
|
|
43
|
+
const [type, ...params] = range.split(";").map((s) => s.trim());
|
|
44
|
+
if (type.toLowerCase() !== "text/html")
|
|
45
|
+
return false;
|
|
46
|
+
const q = params.find((p) => /^q=/i.test(p));
|
|
47
|
+
return q === undefined || Number(q.slice(2)) > 0;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** The page's style: the wordmark as the page's `.logo` draws it (web/style.css), the page's
|
|
51
|
+
* colours, light or dark as the system is. */
|
|
52
|
+
const STYLE = `
|
|
53
|
+
:root { color-scheme: light dark; --bg: #f5f5f7; --text: #1d1d1f; --text-2: #6e6e73; --code: rgba(0, 0, 0, 0.05); --link: #6a4be0; --grad: linear-gradient(100deg, #5e5ce6 0%, #bf5af2 52%, #ff375f 100%); }
|
|
54
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #0b0b0d; --text: #f5f5f7; --text-2: #a1a1a6; --code: rgba(255, 255, 255, 0.08); --link: #ab97ff; } }
|
|
55
|
+
* { box-sizing: border-box; }
|
|
56
|
+
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
|
|
57
|
+
body { margin: 0; min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 20px; background: var(--bg); color: var(--text); font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif; }
|
|
58
|
+
main { max-width: 30rem; text-align: center; }
|
|
59
|
+
.logo { display: inline-block; margin-bottom: 18px; font-size: 40px; font-weight: 700; letter-spacing: -0.02em; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
|
|
60
|
+
h1 { margin: 0 0 8px; font-size: 20px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.3; }
|
|
61
|
+
p { margin: 0 0 8px; color: var(--text-2); }
|
|
62
|
+
code { font: 0.86em "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace; padding: 1px 5px; border-radius: 5px; background: var(--code); color: var(--text); white-space: nowrap; }
|
|
63
|
+
a { color: var(--link); }
|
|
64
|
+
@media (max-width: 480px) { .logo { font-size: 34px; } h1 { font-size: 18px; } p { font-size: 15px; } }
|
|
65
|
+
`;
|
|
66
|
+
/** what the page's CSP allows: its style, by its digest; the `data:` icon (no request for one) */
|
|
67
|
+
export const ACCESS_PAGE_CSP = `default-src 'none'; style-src 'sha256-${createHash("sha256").update(STYLE).digest("base64")}'; img-src data:; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`;
|
|
68
|
+
/** Each page's title and words, as HTML: the same for every server. */
|
|
69
|
+
const WORDS = {
|
|
70
|
+
private: {
|
|
71
|
+
title: "Private",
|
|
72
|
+
head: "This site is private",
|
|
73
|
+
lines: ["Open it with the link you were given. If you could open it before, this browser has forgotten the link: open it again."],
|
|
74
|
+
},
|
|
75
|
+
link: {
|
|
76
|
+
title: "Link no longer valid",
|
|
77
|
+
head: "This link no longer opens this site",
|
|
78
|
+
lines: ["It was replaced or revoked. Ask whoever runs the site for a new one."],
|
|
79
|
+
},
|
|
80
|
+
host: {
|
|
81
|
+
title: "Name not answered",
|
|
82
|
+
head: "This server does not answer this name",
|
|
83
|
+
lines: ["It answers only the names it was started for. Whoever runs it can add this one: start it with <code>--public-url <its address></code> or <code>--allowed-host <name></code>."],
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
/** The line a link's page adds where the address alone opens the site read-only (`--public`). */
|
|
87
|
+
const PUBLIC_LINE = `Anyone may read the site without one: <a href="/">open it read-only</a>.`;
|
|
88
|
+
/** The page, whole: made from `kind` alone, and whether the address opens the site read-only. */
|
|
89
|
+
export function accessPageHtml(kind, publicRead = false) {
|
|
90
|
+
const w = WORDS[kind];
|
|
91
|
+
const lines = kind === "link" && publicRead ? [...w.lines, PUBLIC_LINE] : w.lines;
|
|
92
|
+
return `<!doctype html>
|
|
93
|
+
<html lang="en">
|
|
94
|
+
<head>
|
|
95
|
+
<meta charset="utf-8">
|
|
96
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
97
|
+
<meta name="color-scheme" content="light dark">
|
|
98
|
+
<meta name="robots" content="noindex">
|
|
99
|
+
<title>${w.title} · VisualProof</title>
|
|
100
|
+
<link rel="icon" href="data:,">
|
|
101
|
+
<style>${STYLE}</style>
|
|
102
|
+
</head>
|
|
103
|
+
<body>
|
|
104
|
+
<main>
|
|
105
|
+
<div class="logo">VisualProof</div>
|
|
106
|
+
<h1>${w.head}</h1>
|
|
107
|
+
${lines.map((l) => `<p>${l}</p>`).join("\n")}
|
|
108
|
+
</main>
|
|
109
|
+
</body>
|
|
110
|
+
</html>
|
|
111
|
+
`;
|
|
112
|
+
}
|
|
113
|
+
/** Answer a request with the page: 403, never kept by a cache, with the headers a page needs to
|
|
114
|
+
* stand alone. Cookies to forget (a revoked link's) are set on `res` before. */
|
|
115
|
+
export function sendAccessPage(res, kind, publicRead = false) {
|
|
116
|
+
if (res.headersSent || res.writableEnded)
|
|
117
|
+
return;
|
|
118
|
+
const body = Buffer.from(accessPageHtml(kind, publicRead));
|
|
119
|
+
res.writeHead(403, {
|
|
120
|
+
"content-type": "text/html; charset=utf-8",
|
|
121
|
+
"content-length": String(body.length),
|
|
122
|
+
"cache-control": "no-store",
|
|
123
|
+
// the same address answers JSON to what does not accept HTML
|
|
124
|
+
vary: "accept",
|
|
125
|
+
"content-security-policy": ACCESS_PAGE_CSP,
|
|
126
|
+
"x-content-type-options": "nosniff",
|
|
127
|
+
"referrer-policy": "no-referrer",
|
|
128
|
+
});
|
|
129
|
+
res.end(body);
|
|
130
|
+
}
|
package/dist/server/agent.js
CHANGED
|
@@ -572,7 +572,8 @@ export class AgentApi {
|
|
|
572
572
|
settings: st.settings,
|
|
573
573
|
source,
|
|
574
574
|
fileOf: (module) => v.modules.get(module)?.file ?? null,
|
|
575
|
-
|
|
575
|
+
// a branch's build is of its commit, not of what is checked out meanwhile (DeploySpec)
|
|
576
|
+
build: { fingerprint: this.world.graph?.fingerprint ?? null, commit: shownOr(this.world.shownCommit?.(), root) },
|
|
576
577
|
...(tv !== undefined ? { titles: titleContext(this.world) } : {}),
|
|
577
578
|
board: boardSpots({ g: v.g, review, rec, memory: this.world.views?.book.memoryOf(rec.id, rec.clustering) ?? null, whole: v.meta.layouts?.[rec.clustering], notes }),
|
|
578
579
|
files,
|
|
@@ -772,7 +773,7 @@ export class AgentApi {
|
|
|
772
773
|
if (job.state === "running")
|
|
773
774
|
return fail(409, "That document is still being typeset.", `Ask again in a few seconds, or wait for GET /api/agent/export/job?id=${job.id} to say done.`, { job }, { "retry-after": "2" });
|
|
774
775
|
if (job.state === "failed")
|
|
775
|
-
return fail(409, `That document could not be typeset: ${job.error ?? "XeLaTeX failed"}.`, "Its job has the end of XeLaTeX's log (GET /api/agent/export/job).", { job });
|
|
776
|
+
return fail(409, `That document could not be typeset: ${(job.error ?? "XeLaTeX failed").replace(/\.$/, "")}.`, "Its job has the end of XeLaTeX's log (GET /api/agent/export/job).", { job });
|
|
776
777
|
const f = ex.file(id, w);
|
|
777
778
|
if (!f)
|
|
778
779
|
return fail(404, "That document has left the cache.", "Ask for it again: POST /api/agent/export.");
|
|
@@ -2334,10 +2335,11 @@ export class AgentApi {
|
|
|
2334
2335
|
const me = whoOf(a.caller);
|
|
2335
2336
|
const b = body(a);
|
|
2336
2337
|
if (b.leave === true) {
|
|
2338
|
+
// an agent with a token that leaves is offline until it listens or asks again — unless its
|
|
2339
|
+
// MCP server says it is a session that a newer one replaced, which goes on
|
|
2340
|
+
if (a.caller.token && this.world.talk && !this.world.talk.left(a.caller.token.id, a.session ?? null))
|
|
2341
|
+
return ok({ ok: true, left: false, note: "Another session of your token listens now: it stays." });
|
|
2337
2342
|
this.world.presence.drop(agentKey(me));
|
|
2338
|
-
// an agent with a token that leaves is offline until it listens or asks again
|
|
2339
|
-
if (a.caller.token)
|
|
2340
|
-
this.world.talk?.left(a.caller.token.id);
|
|
2341
2343
|
return ok({ ok: true, left: true });
|
|
2342
2344
|
}
|
|
2343
2345
|
const p = this.goingTo(a, b);
|
|
@@ -3255,6 +3257,10 @@ function clampInt(s, dflt, lo, hi) {
|
|
|
3255
3257
|
/** The project's commit, where it is a git repository: `git rev-parse`, which only reads (and is
|
|
3256
3258
|
* told to take no lock), asked at most once every ten seconds. Null where git is not there or the
|
|
3257
3259
|
* project is not a repository. */
|
|
3260
|
+
/** The commit a document of the graph on show is of: a branch's build's, else what git says now. */
|
|
3261
|
+
function shownOr(shown, root) {
|
|
3262
|
+
return shown === undefined ? commitOf(root) : shown;
|
|
3263
|
+
}
|
|
3258
3264
|
export function commitOf(root) {
|
|
3259
3265
|
const now = Date.now();
|
|
3260
3266
|
const was = commits.get(root);
|
|
@@ -108,7 +108,7 @@ export function libraryEndpoints(world, refuse) {
|
|
|
108
108
|
},
|
|
109
109
|
{
|
|
110
110
|
method: "POST", path: "/api/agent/library/url", needs: "advise",
|
|
111
|
-
about: `Add the file at an address to the library: the server fetches it once and keeps it (an arXiv abstract's address, arxiv.org/abs/…, gives its PDF). http and https only; never an address of a private or local network; at most ${MAX_FILE / (1024 * 1024)} MB; the type is read from the bytes: a PDF, PNG, JPEG, WebP or GIF, never SVG. The same bytes are stored once: a file already in the library is answered as it is (existed: true), restored where it was archived.`,
|
|
111
|
+
about: `Add the file at an address to the library: the server fetches it once and keeps it (an arXiv abstract's address, arxiv.org/abs/…, gives its PDF). http and https only; never an address of a private or local network; at most ${MAX_FILE / (1024 * 1024)} MB; the type is read from the bytes: a PDF, PNG, JPEG, WebP or GIF, never SVG. The same bytes are stored once: a file already in the library is answered as it is (existed: true), restored where it was archived (another's at level full only).`,
|
|
112
112
|
params: { url: "the file's address" },
|
|
113
113
|
returns: "{ok, file, existed}",
|
|
114
114
|
run: write((l, a, who) => l.addUrl(body(a).url, who)),
|
|
@@ -289,7 +289,7 @@ export function viewEndpoints(world, refuseFull) {
|
|
|
289
289
|
return fail(400, q, "Send the scope as its text or its JSON form.");
|
|
290
290
|
const c = V.book.setScope(v.id, q, a.caller.name);
|
|
291
291
|
if (typeof c === "string")
|
|
292
|
-
return fail(400, c, "");
|
|
292
|
+
return fail(400, c, fixedScope(v) ? "POST views/duplicate {id: \"project\"} makes a copy of it to rescope." : "");
|
|
293
293
|
if (c)
|
|
294
294
|
V.changed(c, true);
|
|
295
295
|
there(whoOf(a.caller), v);
|
package/dist/server/artcmd.js
CHANGED
|
@@ -204,7 +204,7 @@ export function makeArtwork(prep, review, project, ask) {
|
|
|
204
204
|
},
|
|
205
205
|
targets: s.targets === "chosen" ? s.shownTargets : s.targets === "plain" ? [] : idx.map((j) => g.nodes[j].name),
|
|
206
206
|
selection: sel,
|
|
207
|
-
notesLeftOut: s.notes ? (project.notes ?? []).length : 0,
|
|
207
|
+
notesLeftOut: s.notes ? (project.notes ?? []).length + (ask.projectNotes ?? 0) : 0,
|
|
208
208
|
svg,
|
|
209
209
|
file: (kind) => artName(title, kind),
|
|
210
210
|
};
|
package/dist/server/askfollow.js
CHANGED
|
@@ -16,13 +16,10 @@
|
|
|
16
16
|
// The agents' endpoint (POST /api/agent/ask-follow) is server/agent.ts's, in its shape
|
|
17
17
|
// (server/agentviews.ts, AgentEndpoint), as the notes' are (server/agentnotes.ts).
|
|
18
18
|
import { randomBytes } from "node:crypto";
|
|
19
|
-
import { sameName } from "../shared/collab.js";
|
|
19
|
+
import { nameKey, sameName } from "../shared/collab.js";
|
|
20
20
|
import { ASK_GRACE_MS, ASK_MS, ASK_WORDS, askLine, cleanAnswer, foldAnswers } from "../shared/askfollow.js";
|
|
21
21
|
import { fail, whoOf } from "./agent.js";
|
|
22
22
|
import { agentKey } from "./presence.js";
|
|
23
|
-
/** The key a name counts under: whatever the case, however Unicode writes it (shared/collab.ts,
|
|
24
|
-
* sameName). */
|
|
25
|
-
const nameKey = (s) => s.trim().normalize("NFKC").toLowerCase();
|
|
26
23
|
export class Asks {
|
|
27
24
|
port;
|
|
28
25
|
ms;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// What tells the live loop (server/live.ts) to look at the build again. It looked every second at
|
|
2
|
+
// every module's files, three stats each and three more, and listed every process (`ps`): on
|
|
3
|
+
// NavierStokesAndEuler (2,657 modules) 26,000 stats a second, 7 to 10 % of a core with nothing
|
|
4
|
+
// happening. Now the folders the modules' sources and outputs are in are watched (`fs.watch`:
|
|
5
|
+
// inotify, FSEvents, ReadDirectoryChangesW), and the loop looks when one says something moved in
|
|
6
|
+
// it, while a build goes on and settles, and every SAFETY_MS besides. Where watching cannot be
|
|
7
|
+
// trusted, the loop looks every second as before: on a network or FUSE filesystem, which does not
|
|
8
|
+
// tell of changes made elsewhere (statfs, Linux); where a folder cannot be watched (too many
|
|
9
|
+
// watches); or once looks found changes no watch told of, twice.
|
|
10
|
+
import { existsSync, statfsSync, watch } from "node:fs";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
/** how often the loop looks whatever the watches say: a change they did not tell is found then */
|
|
13
|
+
export const SAFETY_MS = 30_000;
|
|
14
|
+
/** looks that found a change no watch told, before the watches are given up */
|
|
15
|
+
const MISSES = 2;
|
|
16
|
+
/** Linux filesystems that do not tell inotify of changes made elsewhere (linux/magic.h): NFS, SMB
|
|
17
|
+
* and CIFS, FUSE (sshfs, virtiofs: Docker's shares), 9p (WSL's Windows drives), Ceph, AFS, Coda,
|
|
18
|
+
* VirtualBox's shared folders, GPFS, Lustre */
|
|
19
|
+
const REMOTE = new Map([
|
|
20
|
+
[0x6969, "NFS"], [0x517b, "SMB"], [0xff534d42, "CIFS"], [0xfe534d42, "SMB2"], [0x65735546, "FUSE"], [0x01021997, "9p"],
|
|
21
|
+
[0x00c36400, "Ceph"], [0x5346414f, "AFS"], [0x73757245, "Coda"], [0x786f4256, "vboxsf"], [0x47504653, "GPFS"], [0x0bd00bd0, "Lustre"],
|
|
22
|
+
]);
|
|
23
|
+
/** Whether the live loop looks at the build's files now: when a watch said something moved, where
|
|
24
|
+
* the watches cannot be trusted, while a compile runs or the set of them changed, while a build
|
|
25
|
+
* goes on and settles or its files are still to be read, and every SAFETY_MS besides. A server
|
|
26
|
+
* at rest does none of these. */
|
|
27
|
+
export function shouldLook(r) {
|
|
28
|
+
return r.heard || r.polling || r.running > 0 || r.runsChanged || r.active || r.sinceMove < r.settle + 2 * r.tick || r.unread || r.sinceLook >= SAFETY_MS;
|
|
29
|
+
}
|
|
30
|
+
/** The folders a project's modules are in: their sources' and their outputs'. */
|
|
31
|
+
export function moduleDirs(p, mods) {
|
|
32
|
+
const out = new Set();
|
|
33
|
+
for (const m of mods.values()) {
|
|
34
|
+
out.add(dirname(join(p.root, m.file)));
|
|
35
|
+
out.add(dirname(join(p.libDir, ...m.name.split("."))));
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** The filesystem `dir` is on, where it is one that does not tell of changes made elsewhere. */
|
|
40
|
+
function remoteFs(dir) {
|
|
41
|
+
if (process.platform !== "linux")
|
|
42
|
+
return null;
|
|
43
|
+
try {
|
|
44
|
+
return REMOTE.get(statfsSync(dir).type) ?? null;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class BuildWatch {
|
|
51
|
+
/** something moved since the last look; at first, so that the first look is made at once */
|
|
52
|
+
dirty = true;
|
|
53
|
+
/** why the folders are not watched and the loop looks every second; null while they are */
|
|
54
|
+
polling = null;
|
|
55
|
+
watchers = new Map();
|
|
56
|
+
misses = 0;
|
|
57
|
+
say;
|
|
58
|
+
constructor(say = () => { }) {
|
|
59
|
+
this.say = say;
|
|
60
|
+
}
|
|
61
|
+
/** Watch the folders `dirs` (each, or where it is not there yet, the nearest one above it that is,
|
|
62
|
+
* up to `root`), and no others. */
|
|
63
|
+
watch(dirs, root) {
|
|
64
|
+
if (this.polling)
|
|
65
|
+
return;
|
|
66
|
+
const wanted = new Set();
|
|
67
|
+
for (const d of dirs) {
|
|
68
|
+
let at = d;
|
|
69
|
+
// an output folder not built yet: the project's own folder at the highest
|
|
70
|
+
while (!existsSync(at) && at !== root && dirname(at) !== at)
|
|
71
|
+
at = dirname(at);
|
|
72
|
+
wanted.add(at);
|
|
73
|
+
}
|
|
74
|
+
for (const [d, w] of this.watchers)
|
|
75
|
+
if (!wanted.has(d)) {
|
|
76
|
+
w.close();
|
|
77
|
+
this.watchers.delete(d);
|
|
78
|
+
}
|
|
79
|
+
for (const d of wanted) {
|
|
80
|
+
if (this.watchers.has(d))
|
|
81
|
+
continue;
|
|
82
|
+
const fs = remoteFs(d);
|
|
83
|
+
if (fs)
|
|
84
|
+
return this.poll(`${d} is on ${fs}, which does not tell of changes made elsewhere`);
|
|
85
|
+
let w;
|
|
86
|
+
try {
|
|
87
|
+
// never what keeps the server running
|
|
88
|
+
w = watch(d, { persistent: false });
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
return this.poll(`${d} cannot be watched (${e instanceof Error ? e.message : String(e)})`);
|
|
92
|
+
}
|
|
93
|
+
w.on("change", () => (this.dirty = true));
|
|
94
|
+
// a folder gone, a watch the system dropped: watched again at the next look that finds it
|
|
95
|
+
w.on("error", () => {
|
|
96
|
+
w.close();
|
|
97
|
+
if (this.watchers.get(d) === w)
|
|
98
|
+
this.watchers.delete(d);
|
|
99
|
+
this.dirty = true;
|
|
100
|
+
});
|
|
101
|
+
this.watchers.set(d, w);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** A look found a change no watch told of: the watches are made again, and given up once that
|
|
105
|
+
* happened MISSES times. */
|
|
106
|
+
missed() {
|
|
107
|
+
if (this.polling)
|
|
108
|
+
return;
|
|
109
|
+
if (++this.misses >= MISSES)
|
|
110
|
+
return this.poll("looks found changes no watch told of");
|
|
111
|
+
for (const w of this.watchers.values())
|
|
112
|
+
w.close();
|
|
113
|
+
this.watchers.clear();
|
|
114
|
+
// made again at the next look, made at once
|
|
115
|
+
this.dirty = true;
|
|
116
|
+
}
|
|
117
|
+
/** How many folders are watched. */
|
|
118
|
+
get size() {
|
|
119
|
+
return this.watchers.size;
|
|
120
|
+
}
|
|
121
|
+
poll(why) {
|
|
122
|
+
this.polling = why;
|
|
123
|
+
for (const w of this.watchers.values())
|
|
124
|
+
w.close();
|
|
125
|
+
this.watchers.clear();
|
|
126
|
+
this.say(`watch: ${why}; the build is looked at every second`);
|
|
127
|
+
}
|
|
128
|
+
close() {
|
|
129
|
+
for (const w of this.watchers.values())
|
|
130
|
+
w.close();
|
|
131
|
+
this.watchers.clear();
|
|
132
|
+
}
|
|
133
|
+
}
|