agent-recall-mcp 3.5.2 → 3.5.4
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 +254 -143
- package/dist/build-server.d.ts +28 -0
- package/dist/build-server.d.ts.map +1 -0
- package/dist/build-server.js +55 -0
- package/dist/build-server.js.map +1 -0
- package/dist/http/activity.d.ts +60 -0
- package/dist/http/activity.d.ts.map +1 -0
- package/dist/http/activity.js +254 -0
- package/dist/http/activity.js.map +1 -0
- package/dist/http/alerts.d.ts +84 -0
- package/dist/http/alerts.d.ts.map +1 -0
- package/dist/http/alerts.js +213 -0
- package/dist/http/alerts.js.map +1 -0
- package/dist/http/cli.d.ts +3 -0
- package/dist/http/cli.d.ts.map +1 -0
- package/dist/http/cli.js +515 -0
- package/dist/http/cli.js.map +1 -0
- package/dist/http/export-tar.d.ts +23 -0
- package/dist/http/export-tar.d.ts.map +1 -0
- package/dist/http/export-tar.js +264 -0
- package/dist/http/export-tar.js.map +1 -0
- package/dist/http/federation.d.ts +45 -0
- package/dist/http/federation.d.ts.map +1 -0
- package/dist/http/federation.js +185 -0
- package/dist/http/federation.js.map +1 -0
- package/dist/http/film/film.css +80 -0
- package/dist/http/film/film.js +446 -0
- package/dist/http/film/human.jpg +0 -0
- package/dist/http/film/m0.jpg +0 -0
- package/dist/http/film/m2.jpg +0 -0
- package/dist/http/film/m3.jpg +0 -0
- package/dist/http/film/wrong.jpg +0 -0
- package/dist/http/film.d.ts +16 -0
- package/dist/http/film.d.ts.map +1 -0
- package/dist/http/film.js +119 -0
- package/dist/http/film.js.map +1 -0
- package/dist/http/home/cabinet.svg +1 -0
- package/dist/http/home/drawer-project.svg +1 -0
- package/dist/http/home/drawer-rules.svg +1 -0
- package/dist/http/home/drawer-you.svg +1 -0
- package/dist/http/home/fig1.svg +242 -0
- package/dist/http/home/fig2-1.svg +61 -0
- package/dist/http/home/fig2-2.svg +92 -0
- package/dist/http/home/fig3.svg +1 -0
- package/dist/http/home/home.css +509 -0
- package/dist/http/home/home.html +517 -0
- package/dist/http/home/icons.svg +15 -0
- package/dist/http/home.d.ts +23 -0
- package/dist/http/home.d.ts.map +1 -0
- package/dist/http/home.js +295 -0
- package/dist/http/home.js.map +1 -0
- package/dist/http/identities.d.ts +29 -0
- package/dist/http/identities.d.ts.map +1 -0
- package/dist/http/identities.js +91 -0
- package/dist/http/identities.js.map +1 -0
- package/dist/http/import-memory.d.ts +161 -0
- package/dist/http/import-memory.d.ts.map +1 -0
- package/dist/http/import-memory.js +718 -0
- package/dist/http/import-memory.js.map +1 -0
- package/dist/http/jsonfile.d.ts +6 -0
- package/dist/http/jsonfile.d.ts.map +1 -0
- package/dist/http/jsonfile.js +60 -0
- package/dist/http/jsonfile.js.map +1 -0
- package/dist/http/legal/PRIVACY.md +70 -0
- package/dist/http/legal.d.ts +10 -0
- package/dist/http/legal.d.ts.map +1 -0
- package/dist/http/legal.js +35 -0
- package/dist/http/legal.js.map +1 -0
- package/dist/http/limits.d.ts +59 -0
- package/dist/http/limits.d.ts.map +1 -0
- package/dist/http/limits.js +163 -0
- package/dist/http/limits.js.map +1 -0
- package/dist/http/markdown-lite.d.ts +2 -0
- package/dist/http/markdown-lite.d.ts.map +1 -0
- package/dist/http/markdown-lite.js +86 -0
- package/dist/http/markdown-lite.js.map +1 -0
- package/dist/http/multipart.d.ts +43 -0
- package/dist/http/multipart.d.ts.map +1 -0
- package/dist/http/multipart.js +152 -0
- package/dist/http/multipart.js.map +1 -0
- package/dist/http/oauth-app.d.ts +75 -0
- package/dist/http/oauth-app.d.ts.map +1 -0
- package/dist/http/oauth-app.js +259 -0
- package/dist/http/oauth-app.js.map +1 -0
- package/dist/http/oauth-config.d.ts +46 -0
- package/dist/http/oauth-config.d.ts.map +1 -0
- package/dist/http/oauth-config.js +122 -0
- package/dist/http/oauth-config.js.map +1 -0
- package/dist/http/oauth-pages.d.ts +40 -0
- package/dist/http/oauth-pages.d.ts.map +1 -0
- package/dist/http/oauth-pages.js +97 -0
- package/dist/http/oauth-pages.js.map +1 -0
- package/dist/http/oauth-provider.d.ts +124 -0
- package/dist/http/oauth-provider.d.ts.map +1 -0
- package/dist/http/oauth-provider.js +486 -0
- package/dist/http/oauth-provider.js.map +1 -0
- package/dist/http/oauth-store.d.ts +128 -0
- package/dist/http/oauth-store.d.ts.map +1 -0
- package/dist/http/oauth-store.js +317 -0
- package/dist/http/oauth-store.js.map +1 -0
- package/dist/http/redirect-policy.d.ts +16 -0
- package/dist/http/redirect-policy.d.ts.map +1 -0
- package/dist/http/redirect-policy.js +83 -0
- package/dist/http/redirect-policy.js.map +1 -0
- package/dist/http/review-pages.d.ts +39 -0
- package/dist/http/review-pages.d.ts.map +1 -0
- package/dist/http/review-pages.js +78 -0
- package/dist/http/review-pages.js.map +1 -0
- package/dist/http/review.d.ts +36 -0
- package/dist/http/review.d.ts.map +1 -0
- package/dist/http/review.js +220 -0
- package/dist/http/review.js.map +1 -0
- package/dist/http/serve.d.ts +58 -0
- package/dist/http/serve.d.ts.map +1 -0
- package/dist/http/serve.js +324 -0
- package/dist/http/serve.js.map +1 -0
- package/dist/http/site-pages.d.ts +110 -0
- package/dist/http/site-pages.d.ts.map +1 -0
- package/dist/http/site-pages.js +630 -0
- package/dist/http/site-pages.js.map +1 -0
- package/dist/http/site-theme.d.ts +13 -0
- package/dist/http/site-theme.d.ts.map +1 -0
- package/dist/http/site-theme.js +22 -0
- package/dist/http/site-theme.js.map +1 -0
- package/dist/http/site.d.ts +52 -0
- package/dist/http/site.d.ts.map +1 -0
- package/dist/http/site.js +353 -0
- package/dist/http/site.js.map +1 -0
- package/dist/http/stats.d.ts +139 -0
- package/dist/http/stats.d.ts.map +1 -0
- package/dist/http/stats.js +599 -0
- package/dist/http/stats.js.map +1 -0
- package/dist/http/tar-import.d.ts +69 -0
- package/dist/http/tar-import.d.ts.map +1 -0
- package/dist/http/tar-import.js +234 -0
- package/dist/http/tar-import.js.map +1 -0
- package/dist/http/tenant-paths.d.ts +14 -0
- package/dist/http/tenant-paths.d.ts.map +1 -0
- package/dist/http/tenant-paths.js +42 -0
- package/dist/http/tenant-paths.js.map +1 -0
- package/dist/http/tokens.d.ts +32 -0
- package/dist/http/tokens.d.ts.map +1 -0
- package/dist/http/tokens.js +137 -0
- package/dist/http/tokens.js.map +1 -0
- package/dist/http/trial.d.ts +55 -0
- package/dist/http/trial.d.ts.map +1 -0
- package/dist/http/trial.js +218 -0
- package/dist/http/trial.js.map +1 -0
- package/dist/http/web-session.d.ts +108 -0
- package/dist/http/web-session.d.ts.map +1 -0
- package/dist/http/web-session.js +267 -0
- package/dist/http/web-session.js.map +1 -0
- package/dist/index.js +12 -65
- package/dist/index.js.map +1 -1
- package/dist/lib/ambient-capture.d.ts.map +1 -1
- package/dist/lib/ambient-capture.js +4 -1
- package/dist/lib/ambient-capture.js.map +1 -1
- package/dist/lib/bookkeeping.d.ts +26 -0
- package/dist/lib/bookkeeping.d.ts.map +1 -0
- package/dist/lib/bookkeeping.js +31 -0
- package/dist/lib/bookkeeping.js.map +1 -0
- package/dist/lib/hosted-redact.d.ts +17 -0
- package/dist/lib/hosted-redact.d.ts.map +1 -0
- package/dist/lib/hosted-redact.js +57 -0
- package/dist/lib/hosted-redact.js.map +1 -0
- package/dist/lib/lifecycle-exit.d.ts +5 -13
- package/dist/lib/lifecycle-exit.d.ts.map +1 -1
- package/dist/lib/lifecycle-exit.js +38 -16
- package/dist/lib/lifecycle-exit.js.map +1 -1
- package/dist/server.d.ts +3 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +6 -3
- package/dist/server.js.map +1 -1
- package/dist/tools/annotations.d.ts +65 -0
- package/dist/tools/annotations.d.ts.map +1 -0
- package/dist/tools/annotations.js +4 -0
- package/dist/tools/annotations.js.map +1 -0
- package/dist/tools/check-action.d.ts.map +1 -1
- package/dist/tools/check-action.js +2 -0
- package/dist/tools/check-action.js.map +1 -1
- package/dist/tools/check.d.ts.map +1 -1
- package/dist/tools/check.js +6 -2
- package/dist/tools/check.js.map +1 -1
- package/dist/tools/digest.d.ts.map +1 -1
- package/dist/tools/digest.js +2 -0
- package/dist/tools/digest.js.map +1 -1
- package/dist/tools/pipeline-close.d.ts.map +1 -1
- package/dist/tools/pipeline-close.js +2 -0
- package/dist/tools/pipeline-close.js.map +1 -1
- package/dist/tools/pipeline-current.d.ts.map +1 -1
- package/dist/tools/pipeline-current.js +2 -0
- package/dist/tools/pipeline-current.js.map +1 -1
- package/dist/tools/pipeline-list.d.ts.map +1 -1
- package/dist/tools/pipeline-list.js +2 -0
- package/dist/tools/pipeline-list.js.map +1 -1
- package/dist/tools/pipeline-open.d.ts.map +1 -1
- package/dist/tools/pipeline-open.js +2 -0
- package/dist/tools/pipeline-open.js.map +1 -1
- package/dist/tools/pipeline-show.d.ts.map +1 -1
- package/dist/tools/pipeline-show.js +2 -0
- package/dist/tools/pipeline-show.js.map +1 -1
- package/dist/tools/recall.d.ts.map +1 -1
- package/dist/tools/recall.js +4 -1
- package/dist/tools/recall.js.map +1 -1
- package/dist/tools/register-rule.d.ts.map +1 -1
- package/dist/tools/register-rule.js +2 -0
- package/dist/tools/register-rule.js.map +1 -1
- package/dist/tools/remember.d.ts.map +1 -1
- package/dist/tools/remember.js +10 -1
- package/dist/tools/remember.js.map +1 -1
- package/dist/tools/session-end.d.ts +15 -0
- package/dist/tools/session-end.d.ts.map +1 -1
- package/dist/tools/session-end.js +13 -1
- package/dist/tools/session-end.js.map +1 -1
- package/dist/tools/session-start.d.ts +6 -1
- package/dist/tools/session-start.d.ts.map +1 -1
- package/dist/tools/session-start.js +24 -212
- package/dist/tools/session-start.js.map +1 -1
- package/package.json +11 -8
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Home {
|
|
2
|
+
/** The whole document, ready to send. */
|
|
3
|
+
html: string;
|
|
4
|
+
/** CSP source for the one inline <style>: sha256 of its exact (LF) text. */
|
|
5
|
+
styleHash: string;
|
|
6
|
+
}
|
|
7
|
+
/** The figure files (home/<name>.svg) and the template placeholders {{name}} they fill. */
|
|
8
|
+
export declare const HOME_FIGURES: readonly ["fig1", "fig2-1", "fig2-2", "fig3"];
|
|
9
|
+
/**
|
|
10
|
+
* Inline svg pieces the page positions itself (the drawer fronts sit inside <summary> elements).
|
|
11
|
+
* Filled verbatim: no class="art", because .art makes an svg a full-width block.
|
|
12
|
+
*/
|
|
13
|
+
export declare const HOME_PARTS: readonly ["cabinet", "drawer-rules", "drawer-you", "drawer-project"];
|
|
14
|
+
export type Loaded = {
|
|
15
|
+
home: Home;
|
|
16
|
+
} | {
|
|
17
|
+
reason: string;
|
|
18
|
+
};
|
|
19
|
+
/** Assemble the home page from `dir`, or say why not. Never throws. */
|
|
20
|
+
export declare function loadHomeWithReason(dir: string): Loaded;
|
|
21
|
+
export declare function loadHome(dir: string): Home | null;
|
|
22
|
+
export declare const HOME: Home | null;
|
|
23
|
+
//# sourceMappingURL=home.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"home.d.ts","sourceRoot":"","sources":["../../src/http/home.ts"],"names":[],"mappings":"AA6BA,MAAM,WAAW,IAAI;IACnB,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,4EAA4E;IAC5E,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,2FAA2F;AAC3F,eAAO,MAAM,YAAY,+CAAgD,CAAC;AAE1E;;;GAGG;AACH,eAAO,MAAM,UAAU,sEAAuE,CAAC;AA8G/F,MAAM,MAAM,MAAM,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AA+FzD,uEAAuE;AACvE,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAMtD;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAGjD;AAGD,eAAO,MAAM,IAAI,EAAE,IAAI,GAAG,IAA4C,CAAC"}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* home.ts — the home page at "/": the blueprint page (hero figure, two ways in, how it
|
|
3
|
+
* works, one measured example, clients, values, a link to the film).
|
|
4
|
+
*
|
|
5
|
+
* The page keeps the site's CSP shape and is stricter than the film: ONE inline style
|
|
6
|
+
* allowed by its sha256 hash, NO script at all (so script-src is 'none'), no image
|
|
7
|
+
* element, nothing from another origin. The figures are inline SVG, drawn once and
|
|
8
|
+
* checked here at load time, because they are the only large markup that is not ours
|
|
9
|
+
* line by line.
|
|
10
|
+
*
|
|
11
|
+
* The page has CSS-only controls (radio buttons, one checkbox, <details>) that switch panels
|
|
12
|
+
* without script; a visitor without CSS, an agent or a crawler still gets all the copy.
|
|
13
|
+
*
|
|
14
|
+
* The assets live beside this file in ./home/ (copied into dist by the build): home.html
|
|
15
|
+
* is a template with {{placeholders}}, home.css is the one stylesheet, four svg files are the
|
|
16
|
+
* figures, four more (the cabinet and its three drawer fronts) are parts the page positions
|
|
17
|
+
* itself, and icons.svg is the icon sprite. loadHome() assembles them and REFUSES the
|
|
18
|
+
* result (returns null) if anything could break the CSP or the markup: a stray script,
|
|
19
|
+
* style attribute, external URL, duplicate id or dangling <use>. When HOME is null the
|
|
20
|
+
* site serves the classic home page instead: a broken build must never take the front
|
|
21
|
+
* door down.
|
|
22
|
+
*
|
|
23
|
+
* The scroll film is served at /film (see film.ts); this page links to it.
|
|
24
|
+
*/
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import * as fs from "node:fs";
|
|
27
|
+
import { fileURLToPath } from "node:url";
|
|
28
|
+
import { REPO_URL, TELEGRAM_URL, footerNav } from "./site-pages.js";
|
|
29
|
+
/** The figure files (home/<name>.svg) and the template placeholders {{name}} they fill. */
|
|
30
|
+
export const HOME_FIGURES = ["fig1", "fig2-1", "fig2-2", "fig3"];
|
|
31
|
+
/**
|
|
32
|
+
* Inline svg pieces the page positions itself (the drawer fronts sit inside <summary> elements).
|
|
33
|
+
* Filled verbatim: no class="art", because .art makes an svg a full-width block.
|
|
34
|
+
*/
|
|
35
|
+
export const HOME_PARTS = ["cabinet", "drawer-rules", "drawer-you", "drawer-project"];
|
|
36
|
+
/** Largest page accepted at load; a bigger one means the wrong asset was shipped. */
|
|
37
|
+
const MAX_HTML_BYTES = 512 * 1024;
|
|
38
|
+
const sha = (s) => `'sha256-${createHash("sha256").update(s, "utf8").digest("base64")}'`;
|
|
39
|
+
/** The HTML parser turns CRLF into LF before a CSP hash is checked (Windows checkouts), so read the LF form. */
|
|
40
|
+
const lf = (s) => s.replace(/\r\n?/g, "\n");
|
|
41
|
+
/**
|
|
42
|
+
* Every attribute of every tag, read the way an HTML parser reads them: quotes are respected,
|
|
43
|
+
* so a quoted value (title="one = 1", title=">") never looks like an attribute or ends a tag,
|
|
44
|
+
* and "/" separates attributes (<svg/onload=...>). Text outside tags is skipped, so prose such
|
|
45
|
+
* as "style = none" is never mistaken for one. Names are lower-cased.
|
|
46
|
+
*/
|
|
47
|
+
function attrs(html) {
|
|
48
|
+
const out = [];
|
|
49
|
+
const n = html.length;
|
|
50
|
+
let i = 0;
|
|
51
|
+
while ((i = html.indexOf("<", i)) !== -1) {
|
|
52
|
+
i++;
|
|
53
|
+
// A doctype or bogus comment ("<!...>", "<?...>") runs to the first ">", as in the browser (real comments are already stripped).
|
|
54
|
+
if (html[i] === "!" || html[i] === "?") {
|
|
55
|
+
const end = html.indexOf(">", i);
|
|
56
|
+
if (end === -1)
|
|
57
|
+
break;
|
|
58
|
+
i = end + 1;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (!/[A-Za-z]/.test(html[i] ?? ""))
|
|
62
|
+
continue;
|
|
63
|
+
// Whitespace is HTML's (tab, LF, FF, CR, space), not JS \s: U+00A0 or U+FEFF is part of a name to the browser.
|
|
64
|
+
const t0 = i;
|
|
65
|
+
while (i < n && !/[\t\n\f\r />]/.test(html[i]))
|
|
66
|
+
i++;
|
|
67
|
+
const tag = html.slice(t0, i).toLowerCase();
|
|
68
|
+
for (;;) {
|
|
69
|
+
while (i < n && /[\t\n\f\r /]/.test(html[i]))
|
|
70
|
+
i++;
|
|
71
|
+
if (i >= n || html[i] === ">")
|
|
72
|
+
break;
|
|
73
|
+
const a0 = i;
|
|
74
|
+
// As in the HTML spec, a leading "=" is part of the name (so the name is never empty and "<p =x>" ends at its ">").
|
|
75
|
+
if (html[i] === "=")
|
|
76
|
+
i++;
|
|
77
|
+
while (i < n && !/[\t\n\f\r />=]/.test(html[i]))
|
|
78
|
+
i++;
|
|
79
|
+
const name = html.slice(a0, i).toLowerCase();
|
|
80
|
+
while (i < n && /[\t\n\f\r ]/.test(html[i]))
|
|
81
|
+
i++;
|
|
82
|
+
let value = null;
|
|
83
|
+
if (html[i] === "=") {
|
|
84
|
+
i++;
|
|
85
|
+
while (i < n && /[\t\n\f\r ]/.test(html[i]))
|
|
86
|
+
i++;
|
|
87
|
+
const q = html[i];
|
|
88
|
+
if (q === '"' || q === "'") {
|
|
89
|
+
const end = html.indexOf(q, i + 1);
|
|
90
|
+
value = html.slice(i + 1, end === -1 ? n : end);
|
|
91
|
+
i = end === -1 ? n : end + 1;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const v0 = i;
|
|
95
|
+
while (i < n && !/[\t\n\f\r >]/.test(html[i]))
|
|
96
|
+
i++;
|
|
97
|
+
value = html.slice(v0, i);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
out.push({ tag, name, value });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
/** Attributes whose value a browser follows as a URL. */
|
|
106
|
+
const URL_ATTR = /^(?:href|src|action|xlink:href|formaction)$/;
|
|
107
|
+
/** Browsers drop leading/trailing whitespace and any tab or newline inside a URL, so judge the value they would follow. */
|
|
108
|
+
const urlOf = (v) => v.replace(/[\u0000- ]/g, "");
|
|
109
|
+
/** A URL that leaves this origin: any scheme, or two leading slashes, where a browser reads "\" as "/" ("\\evil", "/\evil"). */
|
|
110
|
+
const ABSOLUTE_URL = /^(?:[a-z][a-z0-9+.-]*:|[\\/]{2})/i;
|
|
111
|
+
/** HTML comments as the parser ends them: "-->", "--!>", the empty "<!-->" / "<!--->", or the end of the input. */
|
|
112
|
+
const stripComments = (s) => s.replace(/<!--(?:>|->|[\s\S]*?(?:--!?>|$))/g, "");
|
|
113
|
+
/** Only the repo and the Telegram community, each as itself or a path, query or fragment under it (never "…AgentRecall-Xevil"). */
|
|
114
|
+
const allowedExternal = (u) => [REPO_URL, TELEGRAM_URL].some((base) => u === base || (u.startsWith(base) && /^[/?#]/.test(u.slice(base.length))));
|
|
115
|
+
function attrProblem(list) {
|
|
116
|
+
if (list.some((a) => a.name.startsWith("on")))
|
|
117
|
+
return "an on*= attribute";
|
|
118
|
+
if (list.some((a) => a.name === "style"))
|
|
119
|
+
return "a style= attribute";
|
|
120
|
+
// ping posts to its URLs on a click and srcset lists several; neither is needed, so neither is judged as a URL, just refused.
|
|
121
|
+
const fetching = list.find((a) => a.name === "ping" || a.name === "srcset");
|
|
122
|
+
if (fetching)
|
|
123
|
+
return `a ${fetching.name}= attribute`;
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
/** An svg is inlined into the page, so it may not carry anything the CSP would block or that reaches out. */
|
|
127
|
+
function svgProblem(svg) {
|
|
128
|
+
if (/<script/i.test(svg))
|
|
129
|
+
return "svg contains <script";
|
|
130
|
+
if (/<style/i.test(svg))
|
|
131
|
+
return "svg contains <style";
|
|
132
|
+
if (/<foreignObject/i.test(svg))
|
|
133
|
+
return "svg contains <foreignObject";
|
|
134
|
+
if (/<image/i.test(svg))
|
|
135
|
+
return "svg contains <image";
|
|
136
|
+
if (/<(?:set|animate|a)[\s/>]/i.test(svg))
|
|
137
|
+
return "svg contains <set, <animate or <a";
|
|
138
|
+
const list = attrs(svg);
|
|
139
|
+
const bad = attrProblem(list);
|
|
140
|
+
if (bad)
|
|
141
|
+
return `svg contains ${bad}`;
|
|
142
|
+
if (list.some((a) => a.name === "href" && a.value !== null && ABSOLUTE_URL.test(urlOf(a.value))))
|
|
143
|
+
return "svg contains an absolute href";
|
|
144
|
+
if (list.some((a) => a.name === "xlink:href"))
|
|
145
|
+
return "svg contains xlink:href";
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
/** Drop what is only valid in a standalone file: the XML prolog, comments, and the namespace (HTML5 does not need it). */
|
|
149
|
+
const cleanSvg = (s) => stripComments(s.replace(/<\?xml[^>]*\?>/g, "")).replace(/\sxmlns="http:\/\/www\.w3\.org\/2000\/svg"/g, "").trim();
|
|
150
|
+
/** The icons are drawn thin for a standalone sheet; on the page they are a touch bolder and lighter in colour so they read at 1px. */
|
|
151
|
+
const boldIcons = (s) => s.replace(/stroke="#9aaec3"/g, 'stroke="#c9d3df"').replace(/stroke-width="1"/g, 'stroke-width="1.3"').replace(/stroke-opacity="0\.45"/g, 'stroke-opacity="0.6"');
|
|
152
|
+
function assemble(dir) {
|
|
153
|
+
// A byte-order mark from an editor save is not content; dropping it keeps a harmless save from failing the checks.
|
|
154
|
+
const rd = (name) => lf(fs.readFileSync(`${dir}/${name}`, "utf8")).replace(/^\uFEFF/, "");
|
|
155
|
+
const css = rd("home.css").trim();
|
|
156
|
+
if (/<\/style/i.test(css))
|
|
157
|
+
return { reason: "home.css could close its own <style>" };
|
|
158
|
+
// The CSP would block these anyway; refusing them keeps "nothing from another origin" true of the asset itself.
|
|
159
|
+
// No "<" at all: then the style body reads the same whether it is parsed as raw text or as markup.
|
|
160
|
+
if (css.includes("<"))
|
|
161
|
+
return { reason: "home.css contains '<'" };
|
|
162
|
+
// A CSS escape can spell anything ("@\69mport"), and image-set() fetches like url(); the stylesheet needs neither.
|
|
163
|
+
if (css.includes("\\"))
|
|
164
|
+
return { reason: "home.css contains a backslash escape" };
|
|
165
|
+
if (/image-set\(/i.test(css))
|
|
166
|
+
return { reason: "home.css contains image-set()" };
|
|
167
|
+
if (/@import/i.test(css))
|
|
168
|
+
return { reason: "home.css contains @import" };
|
|
169
|
+
if (/url\(\s*["']?\s*(?:[a-z][a-z0-9+.-]*:|[\\/]{2})/i.test(css))
|
|
170
|
+
return { reason: "home.css loads an absolute url()" };
|
|
171
|
+
const figures = new Map();
|
|
172
|
+
for (const name of HOME_FIGURES) {
|
|
173
|
+
const svg = cleanSvg(rd(`${name}.svg`));
|
|
174
|
+
const bad = svgProblem(svg);
|
|
175
|
+
if (bad)
|
|
176
|
+
return { reason: `${name}.svg: ${bad}` };
|
|
177
|
+
if (!svg.startsWith("<svg "))
|
|
178
|
+
return { reason: `${name}.svg does not start with <svg` };
|
|
179
|
+
figures.set(name, svg.replace("<svg ", '<svg class="art" '));
|
|
180
|
+
}
|
|
181
|
+
const parts = new Map();
|
|
182
|
+
for (const name of HOME_PARTS) {
|
|
183
|
+
const svg = cleanSvg(rd(`${name}.svg`));
|
|
184
|
+
const bad = svgProblem(svg);
|
|
185
|
+
if (bad)
|
|
186
|
+
return { reason: `${name}.svg: ${bad}` };
|
|
187
|
+
if (!/^<svg[\s>]/.test(svg))
|
|
188
|
+
return { reason: `${name}.svg does not start with <svg` };
|
|
189
|
+
parts.set(name, svg);
|
|
190
|
+
}
|
|
191
|
+
const iconsRaw = cleanSvg(rd("icons.svg"));
|
|
192
|
+
const iconsBad = svgProblem(iconsRaw);
|
|
193
|
+
if (iconsBad)
|
|
194
|
+
return { reason: `icons.svg: ${iconsBad}` };
|
|
195
|
+
const symbols = (iconsRaw.match(/<symbol[\s\S]*?<\/symbol>/g) ?? []).join("");
|
|
196
|
+
if (!symbols)
|
|
197
|
+
return { reason: "icons.svg holds no <symbol>" };
|
|
198
|
+
const sprite = `<svg width="0" height="0" aria-hidden="true" focusable="false" class="sprite">${boldIcons(symbols)}</svg>`;
|
|
199
|
+
const fills = { css, icons: sprite, footer: footerNav("home"), repo: REPO_URL };
|
|
200
|
+
for (const [k, v] of figures)
|
|
201
|
+
fills[k] = v;
|
|
202
|
+
for (const [k, v] of parts)
|
|
203
|
+
fills[k] = v;
|
|
204
|
+
// Comments go before anything is checked, so the page that is checked is byte for byte the page that is served.
|
|
205
|
+
let html = stripComments(rd("home.html")).trimStart();
|
|
206
|
+
// split/join, not replace(): the values contain "$" sequences that replace() would interpret.
|
|
207
|
+
for (const [k, v] of Object.entries(fills))
|
|
208
|
+
html = html.split(`{{${k}}}`).join(v);
|
|
209
|
+
const left = html.match(/\{\{[^}]*\}\}/);
|
|
210
|
+
if (left)
|
|
211
|
+
return { reason: `placeholder left in the page: ${left[0]}` };
|
|
212
|
+
const count = (re) => (html.match(re) ?? []).length;
|
|
213
|
+
// Element checks match the name as a prefix: a browser also opens "<style/>" or "<iframe/>", so no terminator is required.
|
|
214
|
+
if (count(/<style/gi) !== 1)
|
|
215
|
+
return { reason: "the page must hold exactly one <style>" };
|
|
216
|
+
if (!html.includes(`<style>${css}</style>`))
|
|
217
|
+
return { reason: "the <style> is not the single css block" };
|
|
218
|
+
if (count(/<script/gi))
|
|
219
|
+
return { reason: "the page must hold no <script" };
|
|
220
|
+
// attrs() reads tags the way a browser does only where the two cannot disagree on where markup starts and
|
|
221
|
+
// ends. Comments, CDATA, processing instructions, end tags with attributes and raw-text elements are such
|
|
222
|
+
// places, and the page needs none of them, so the served bytes may not contain them at all.
|
|
223
|
+
if (/<!/.test(html.replace(/^<!doctype html>/i, "")))
|
|
224
|
+
return { reason: "a comment, CDATA or other <! construct is left in the page" };
|
|
225
|
+
if (html.includes("<?"))
|
|
226
|
+
return { reason: "a <? processing instruction is present" };
|
|
227
|
+
if (/<\/(?![a-zA-Z][a-zA-Z0-9-]*>)/.test(html))
|
|
228
|
+
return { reason: "an end tag that is not a bare </name> is present" };
|
|
229
|
+
if ([...html.matchAll(/<title\b[^>]*>([\s\S]*?)<\/title>/gi)].some((m) => m[1].includes("<")))
|
|
230
|
+
return { reason: "a <title> holds markup" };
|
|
231
|
+
const raw = /<(textarea|xmp|noscript|noembed|noframes|plaintext)/i.exec(html);
|
|
232
|
+
if (raw)
|
|
233
|
+
return { reason: `a <${raw[1].toLowerCase()}> is present` };
|
|
234
|
+
const list = attrs(html);
|
|
235
|
+
const bad = attrProblem(list);
|
|
236
|
+
if (bad)
|
|
237
|
+
return { reason: `${bad} is present` };
|
|
238
|
+
if (count(/javascript:/gi))
|
|
239
|
+
return { reason: "a javascript: URL is present" };
|
|
240
|
+
// The head links only the icon and the two markdown alternates; any other rel (stylesheet, preload, ...) would fetch.
|
|
241
|
+
const rel = list.find((a) => a.tag === "link" && a.name === "rel" && !/^(?:icon|alternate)$/i.test((a.value ?? "").trim()));
|
|
242
|
+
if (rel)
|
|
243
|
+
return { reason: `a <link rel=${(rel.value ?? "").trim()}> is present` };
|
|
244
|
+
if (count(/<img/gi))
|
|
245
|
+
return { reason: "an <img> is present" };
|
|
246
|
+
if (count(/<iframe/gi))
|
|
247
|
+
return { reason: "an <iframe> is present" };
|
|
248
|
+
// Elements that navigate, submit, re-base or embed: none belongs on this page (the CSP does not stop a meta refresh).
|
|
249
|
+
if (list.some((a) => a.tag === "meta" && a.name === "http-equiv"))
|
|
250
|
+
return { reason: "a <meta http-equiv> is present" };
|
|
251
|
+
const embed = /<(base|form|object|embed)/i.exec(html);
|
|
252
|
+
if (embed)
|
|
253
|
+
return { reason: `a <${embed[1].toLowerCase()}> is present` };
|
|
254
|
+
const ids = list.filter((a) => a.name === "id" && a.value !== null).map((a) => a.value);
|
|
255
|
+
const dup = ids.find((x, i) => ids.indexOf(x) !== i);
|
|
256
|
+
if (dup !== undefined)
|
|
257
|
+
return { reason: `duplicate id "${dup}"` };
|
|
258
|
+
for (const a of list) {
|
|
259
|
+
if (a.value === null)
|
|
260
|
+
continue;
|
|
261
|
+
if (a.tag === "use" && a.name === "href" && (!a.value.startsWith("#") || !ids.includes(a.value.slice(1)))) {
|
|
262
|
+
return { reason: `<use> points at a missing id (${a.value})` };
|
|
263
|
+
}
|
|
264
|
+
if (!URL_ATTR.test(a.name))
|
|
265
|
+
continue;
|
|
266
|
+
const u = urlOf(a.value);
|
|
267
|
+
// A character reference in a URL could spell a scheme the checks below never see ("javascript:").
|
|
268
|
+
if (u.includes("&"))
|
|
269
|
+
return { reason: `character reference in a URL (${u})` };
|
|
270
|
+
if (ABSOLUTE_URL.test(u) && !allowedExternal(u))
|
|
271
|
+
return { reason: `external URL ${u}` };
|
|
272
|
+
}
|
|
273
|
+
if (Buffer.byteLength(html, "utf8") > MAX_HTML_BYTES)
|
|
274
|
+
return { reason: "page larger than 512 KB" };
|
|
275
|
+
return { home: { html, styleHash: sha(css) } };
|
|
276
|
+
}
|
|
277
|
+
/** Assemble the home page from `dir`, or say why not. Never throws. */
|
|
278
|
+
export function loadHomeWithReason(dir) {
|
|
279
|
+
try {
|
|
280
|
+
return assemble(dir);
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
return { reason: `unreadable: ${e instanceof Error ? e.message : String(e)}` };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
export function loadHome(dir) {
|
|
287
|
+
const r = loadHomeWithReason(dir);
|
|
288
|
+
return "home" in r ? r.home : null;
|
|
289
|
+
}
|
|
290
|
+
const loaded = loadHomeWithReason(fileURLToPath(new URL("./home", import.meta.url)));
|
|
291
|
+
export const HOME = "home" in loaded ? loaded.home : null;
|
|
292
|
+
// A build without valid assets still serves a home page, but the operator must be able to see why it is the old one.
|
|
293
|
+
if ("reason" in loaded)
|
|
294
|
+
process.stderr.write(`[site] home page assets missing or invalid in dist/http/home (${loaded.reason}); serving the classic home page\n`);
|
|
295
|
+
//# sourceMappingURL=home.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"home.js","sourceRoot":"","sources":["../../src/http/home.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,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AAS,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;AACjD,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,kBAAkB,CAAC,GAAW;IAC5C,IAAI,CAAC;QACH,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IACjF,CAAC;AACH,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,GAAW;IAClC,MAAM,CAAC,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;AACrC,CAAC;AAED,MAAM,MAAM,GAAG,kBAAkB,CAAC,aAAa,CAAC,IAAI,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACrF,MAAM,CAAC,MAAM,IAAI,GAAgB,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;AACvE,qHAAqH;AACrH,IAAI,QAAQ,IAAI,MAAM;IAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,iEAAiE,MAAM,CAAC,MAAM,oCAAoC,CAAC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ProviderName } from "./oauth-config.js";
|
|
2
|
+
export declare function assertSubject(provider: ProviderName, subject: string | number): string;
|
|
3
|
+
/** `gh-`/`gg-` uids belong to exactly one federated account. */
|
|
4
|
+
export declare const RESERVED_UID_RE: RegExp;
|
|
5
|
+
/** A stored link the server refuses to follow (operator must fix it; retrying cannot help). */
|
|
6
|
+
export declare class IdentityLinkError extends Error {
|
|
7
|
+
}
|
|
8
|
+
/** The tenant an account gets when it is not linked elsewhere: `gh-<id>` / `gg-<sub>`. */
|
|
9
|
+
export declare function defaultUid(provider: ProviderName, subject: string | number): string;
|
|
10
|
+
/**
|
|
11
|
+
* Link (and so, in invite mode, invite) an account. `uid` omitted → the account's own
|
|
12
|
+
* `gh-<id>` / `gg-<sub>`. A reserved `gh-`/`gg-` uid can only be the account's own: linking
|
|
13
|
+
* GitHub 5 onto `gh-9` would hand GitHub user 9's memory to user 5.
|
|
14
|
+
*/
|
|
15
|
+
export declare function linkIdentity(dataDir: string, provider: ProviderName, subject: string | number, uid?: string): string;
|
|
16
|
+
/** Remove an account's link (in invite mode: un-invite it). Returns the uid it pointed to, or null if there was none. */
|
|
17
|
+
export declare function unlinkIdentity(dataDir: string, provider: ProviderName, subject: string | number): string | null;
|
|
18
|
+
export declare function listIdentities(dataDir: string): Array<{
|
|
19
|
+
provider: string;
|
|
20
|
+
subject: string;
|
|
21
|
+
uid: string;
|
|
22
|
+
}>;
|
|
23
|
+
/** uid for a federated account (operator link, else `gh-<id>` / `gg-<sub>`, always validated), and whether the operator linked it. */
|
|
24
|
+
export declare function resolveIdentity(dataDir: string, provider: ProviderName, subject: string | number): {
|
|
25
|
+
uid: string;
|
|
26
|
+
linked: boolean;
|
|
27
|
+
};
|
|
28
|
+
export declare function resolveUid(dataDir: string, provider: ProviderName, subject: string | number): string;
|
|
29
|
+
//# sourceMappingURL=identities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identities.d.ts","sourceRoot":"","sources":["../../src/http/identities.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAsBtD,wBAAgB,aAAa,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAItF;AAED,gEAAgE;AAChE,eAAO,MAAM,eAAe,QAAY,CAAC;AAEzC,+FAA+F;AAC/F,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;AAE/C,0FAA0F;AAC1F,wBAAgB,UAAU,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAEnF;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAYpH;AAED,yHAAyH;AACzH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAS/G;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAMzG;AAED,sIAAsI;AACtI,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CASnI;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAEpG"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* identities.ts — federated account → AgentRecall uid (tenant).
|
|
3
|
+
*
|
|
4
|
+
* Default uid: `gh-<github id>` / `gg-<google sub>`. An operator can link an account to
|
|
5
|
+
* an existing tenant with `agent-recall-server identity link`. File:
|
|
6
|
+
* <dataDir>/_server/identities.json (keys `github:<id>` / `google:<sub>`), written only by
|
|
7
|
+
* the CLI, read per request (mtime-cached) so a link applies at once.
|
|
8
|
+
*/
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { readJsonCached, readJsonFresh, writeJsonAtomic } from "./jsonfile.js";
|
|
11
|
+
import { validateUid } from "./tokens.js";
|
|
12
|
+
const SUBJECT_RE = { github: /^[1-9]\d{0,15}$/, google: /^\d{1,30}$/ };
|
|
13
|
+
const UID_PREFIX = { github: "gh-", google: "gg-" };
|
|
14
|
+
function filePath(dataDir) {
|
|
15
|
+
return path.join(dataDir, "_server", "identities.json");
|
|
16
|
+
}
|
|
17
|
+
function parse(raw) {
|
|
18
|
+
const f = raw;
|
|
19
|
+
if (f?.version !== 1 || typeof f.links !== "object" || f.links === null || Array.isArray(f.links))
|
|
20
|
+
return null;
|
|
21
|
+
return f;
|
|
22
|
+
}
|
|
23
|
+
const empty = () => ({ version: 1, links: {} });
|
|
24
|
+
export function assertSubject(provider, subject) {
|
|
25
|
+
const s = String(subject);
|
|
26
|
+
if (!SUBJECT_RE[provider].test(s))
|
|
27
|
+
throw new Error(`invalid ${provider} id "${s}"`);
|
|
28
|
+
return s;
|
|
29
|
+
}
|
|
30
|
+
/** `gh-`/`gg-` uids belong to exactly one federated account. */
|
|
31
|
+
export const RESERVED_UID_RE = /^g[hg]-/;
|
|
32
|
+
/** A stored link the server refuses to follow (operator must fix it; retrying cannot help). */
|
|
33
|
+
export class IdentityLinkError extends Error {
|
|
34
|
+
}
|
|
35
|
+
/** The tenant an account gets when it is not linked elsewhere: `gh-<id>` / `gg-<sub>`. */
|
|
36
|
+
export function defaultUid(provider, subject) {
|
|
37
|
+
return `${UID_PREFIX[provider]}${assertSubject(provider, subject)}`;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Link (and so, in invite mode, invite) an account. `uid` omitted → the account's own
|
|
41
|
+
* `gh-<id>` / `gg-<sub>`. A reserved `gh-`/`gg-` uid can only be the account's own: linking
|
|
42
|
+
* GitHub 5 onto `gh-9` would hand GitHub user 9's memory to user 5.
|
|
43
|
+
*/
|
|
44
|
+
export function linkIdentity(dataDir, provider, subject, uid) {
|
|
45
|
+
const id = assertSubject(provider, subject);
|
|
46
|
+
const own = defaultUid(provider, id);
|
|
47
|
+
const target = uid ?? own;
|
|
48
|
+
validateUid(target);
|
|
49
|
+
if (RESERVED_UID_RE.test(target) && target !== own) {
|
|
50
|
+
throw new Error(`uid "${target}" belongs to another ${target.startsWith("gh-") ? "GitHub" : "Google"} account; link ${provider} ${id} to "${own}" or to a plain uid`);
|
|
51
|
+
}
|
|
52
|
+
const file = readJsonFresh(filePath(dataDir), parse, empty);
|
|
53
|
+
file.links[`${provider}:${id}`] = target;
|
|
54
|
+
writeJsonAtomic(filePath(dataDir), file);
|
|
55
|
+
return target;
|
|
56
|
+
}
|
|
57
|
+
/** Remove an account's link (in invite mode: un-invite it). Returns the uid it pointed to, or null if there was none. */
|
|
58
|
+
export function unlinkIdentity(dataDir, provider, subject) {
|
|
59
|
+
const id = assertSubject(provider, subject);
|
|
60
|
+
const file = readJsonFresh(filePath(dataDir), parse, empty);
|
|
61
|
+
const key = `${provider}:${id}`;
|
|
62
|
+
const uid = file.links[key];
|
|
63
|
+
if (typeof uid !== "string")
|
|
64
|
+
return null;
|
|
65
|
+
delete file.links[key];
|
|
66
|
+
writeJsonAtomic(filePath(dataDir), file);
|
|
67
|
+
return uid;
|
|
68
|
+
}
|
|
69
|
+
export function listIdentities(dataDir) {
|
|
70
|
+
const file = readJsonFresh(filePath(dataDir), parse, empty);
|
|
71
|
+
return Object.entries(file.links).map(([k, uid]) => {
|
|
72
|
+
const [provider, ...rest] = k.split(":");
|
|
73
|
+
return { provider, subject: rest.join(":"), uid };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** uid for a federated account (operator link, else `gh-<id>` / `gg-<sub>`, always validated), and whether the operator linked it. */
|
|
77
|
+
export function resolveIdentity(dataDir, provider, subject) {
|
|
78
|
+
const id = assertSubject(provider, subject);
|
|
79
|
+
const linked = readJsonCached(filePath(dataDir), parse, empty).links[`${provider}:${id}`];
|
|
80
|
+
if (typeof linked === "string") {
|
|
81
|
+
// A hand-edited or pre-rule link onto someone else's gh-/gg- uid is refused, never followed.
|
|
82
|
+
if (RESERVED_UID_RE.test(linked) && linked !== defaultUid(provider, id))
|
|
83
|
+
throw new IdentityLinkError(`identity ${provider}:${id} is linked to reserved uid "${linked}"`);
|
|
84
|
+
return { uid: validateUid(linked), linked: true };
|
|
85
|
+
}
|
|
86
|
+
return { uid: validateUid(defaultUid(provider, id)), linked: false };
|
|
87
|
+
}
|
|
88
|
+
export function resolveUid(dataDir, provider, subject) {
|
|
89
|
+
return resolveIdentity(dataDir, provider, subject).uid;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=identities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identities.js","sourceRoot":"","sources":["../../src/http/identities.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAQ1C,MAAM,UAAU,GAAiC,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AACrG,MAAM,UAAU,GAAiC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AAElF,SAAS,QAAQ,CAAC,OAAe;IAC/B,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,EAAE,iBAAiB,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,KAAK,CAAC,GAAY;IACzB,MAAM,CAAC,GAAG,GAAmB,CAAC;IAC9B,IAAI,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,OAAO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Ae,EAAE,QAAsB,EAAE,OAAwB;IAC1F,OAAO,eAAe,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type * as http from "node:http";
|
|
2
|
+
export type ImportMode = "add" | "replace";
|
|
3
|
+
export interface ImportLimits {
|
|
4
|
+
/** Largest single file taken in; a bigger one is refused (and skipped). */
|
|
5
|
+
maxFileBytes: number;
|
|
6
|
+
maxEntries: number;
|
|
7
|
+
/** A read that gets nothing for this long ends the import. */
|
|
8
|
+
idleMs: number;
|
|
9
|
+
/** The whole upload must finish within this. */
|
|
10
|
+
totalMs: number;
|
|
11
|
+
/** Once the archive has started, it must average at least this many bytes per second over every window. */
|
|
12
|
+
minBytesPerSec: number;
|
|
13
|
+
windowMs: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const DEFAULT_IMPORT_LIMITS: ImportLimits;
|
|
16
|
+
/** No import may take in more than this, even with no quota configured. */
|
|
17
|
+
export declare const HARD_IMPORT_CAP: number;
|
|
18
|
+
/** Bytes of tar headers/padding/skipped entries allowed on top of what is accepted. */
|
|
19
|
+
export declare const RAW_SLACK: number;
|
|
20
|
+
export declare const TRASH_TTL_MS: number;
|
|
21
|
+
export declare const IMPORTS_PER_HOUR = 5;
|
|
22
|
+
/** A refusal the user should see: status, short title and a plain sentence. */
|
|
23
|
+
export declare class ImportAbort extends Error {
|
|
24
|
+
readonly status: number;
|
|
25
|
+
readonly title: string;
|
|
26
|
+
readonly retryAfter?: number | undefined;
|
|
27
|
+
constructor(status: number, title: string, message: string, retryAfter?: number | undefined);
|
|
28
|
+
}
|
|
29
|
+
export interface ImportHooks {
|
|
30
|
+
/** Test seams (a swap between a check and the next call): before a staged file's parent is checked / its file opened / a file is moved into the tenant. */
|
|
31
|
+
beforeFile?: (abs: string) => Promise<void>;
|
|
32
|
+
beforeOpen?: (abs: string) => Promise<void>;
|
|
33
|
+
beforeCommitFile?: (dest: string) => Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface ImportReport {
|
|
36
|
+
mode: ImportMode;
|
|
37
|
+
imported: number;
|
|
38
|
+
skippedExisting: number;
|
|
39
|
+
refused: {
|
|
40
|
+
path: number;
|
|
41
|
+
type: number;
|
|
42
|
+
size: number;
|
|
43
|
+
special: number;
|
|
44
|
+
duplicate: number;
|
|
45
|
+
};
|
|
46
|
+
/** Always-follow rule files left out because "also import rules" was not ticked. */
|
|
47
|
+
rulesSkipped: number;
|
|
48
|
+
projects: string[];
|
|
49
|
+
/** Active always-follow (p0) corrections the account has after the import. */
|
|
50
|
+
alwaysFollow: number;
|
|
51
|
+
bytes: number;
|
|
52
|
+
/** A few refused paths, made printable, so the user can see what was left out. */
|
|
53
|
+
samples: string[];
|
|
54
|
+
}
|
|
55
|
+
export declare class ImportGate {
|
|
56
|
+
private readonly now;
|
|
57
|
+
private readonly perHour;
|
|
58
|
+
private readonly maxConcurrent;
|
|
59
|
+
private readonly active;
|
|
60
|
+
private readonly ips;
|
|
61
|
+
/** At most this many imports run at once across all accounts (each holds a staging tree and bookkeeping in memory). */
|
|
62
|
+
static readonly MAX_CONCURRENT = 4;
|
|
63
|
+
private readonly times;
|
|
64
|
+
constructor(now: () => number, perHour?: number, maxConcurrent?: number);
|
|
65
|
+
/**
|
|
66
|
+
* null = granted (call release() later); otherwise why not:
|
|
67
|
+
* busy = this account already has one running; rate = too many this hour; ip = another import from
|
|
68
|
+
* this address is running; full = the server's global slots are all taken (not the caller's fault).
|
|
69
|
+
*/
|
|
70
|
+
tryAcquire(uid: string, ip: string): "busy" | "rate" | "ip" | "full" | null;
|
|
71
|
+
release(uid: string, ip: string): void;
|
|
72
|
+
isBusy(uid: string): boolean;
|
|
73
|
+
}
|
|
74
|
+
/** Deletes trash entries whose name-embedded timestamp is older than the TTL. Entries it cannot date are left alone. Returns how many were removed. */
|
|
75
|
+
export declare function sweepTrash(dataDir: string, nowMs: number, ttlMs?: number): Promise<number>;
|
|
76
|
+
/** Removes staging directories a crashed process left behind (older than an hour). */
|
|
77
|
+
export declare function sweepStaging(dataDir: string, nowMs: number, maxAgeMs?: number): Promise<number>;
|
|
78
|
+
/** A replace dies between its two renames within seconds; an older trash entry is a deliberate one (never resurrect it). */
|
|
79
|
+
export declare const ORPHAN_MAX_AGE_MS = 3600000;
|
|
80
|
+
/**
|
|
81
|
+
* If a replace was cut short (the process died between moving the tenant to trash and moving the new tree
|
|
82
|
+
* in), the tenant is missing (or an agent request re-created it empty) while its data sits in trash. Put the
|
|
83
|
+
* newest recent trash entry back. Run at server start for every account, and before an import for its account.
|
|
84
|
+
*/
|
|
85
|
+
export declare function restoreOrphans(dataDir: string, nowMs: number, log: (line: string) => void, only?: string): Promise<number>;
|
|
86
|
+
export interface SessionOptions {
|
|
87
|
+
dataDir: string;
|
|
88
|
+
uid: string;
|
|
89
|
+
mode: ImportMode;
|
|
90
|
+
/** Import the always-follow rule files too (isRuleFile). */
|
|
91
|
+
rules: boolean;
|
|
92
|
+
/** Bytes (disk-block rounded) this import may add. */
|
|
93
|
+
budgetBytes: number;
|
|
94
|
+
limits: ImportLimits;
|
|
95
|
+
hooks?: ImportHooks;
|
|
96
|
+
now: () => number;
|
|
97
|
+
}
|
|
98
|
+
export declare class ImportSession {
|
|
99
|
+
private readonly o;
|
|
100
|
+
readonly report: ImportReport;
|
|
101
|
+
private stagingDir;
|
|
102
|
+
private stagingReal;
|
|
103
|
+
private readonly tar;
|
|
104
|
+
private raw;
|
|
105
|
+
private readonly rawCap;
|
|
106
|
+
private cost;
|
|
107
|
+
private dirCount;
|
|
108
|
+
private layout;
|
|
109
|
+
private wrapper;
|
|
110
|
+
private readonly canon;
|
|
111
|
+
private readonly staged;
|
|
112
|
+
private readonly projects;
|
|
113
|
+
private cur;
|
|
114
|
+
constructor(o: SessionOptions);
|
|
115
|
+
begin(): Promise<void>;
|
|
116
|
+
get stagingPath(): string;
|
|
117
|
+
get rawBytes(): number;
|
|
118
|
+
/** Feeds archive bytes. Throws ImportAbort / TarError / fs errors; the caller must cleanup(). */
|
|
119
|
+
feed(chunk: Buffer): Promise<void>;
|
|
120
|
+
/** The upload is complete. */
|
|
121
|
+
endOfArchive(): void;
|
|
122
|
+
private refuse;
|
|
123
|
+
private onEntry;
|
|
124
|
+
private checkStagingParent;
|
|
125
|
+
private onData;
|
|
126
|
+
private onFileEnd;
|
|
127
|
+
/** Remove the staging directory and any open file. Safe to call twice; never throws. */
|
|
128
|
+
cleanup(): Promise<void>;
|
|
129
|
+
commit(): Promise<ImportReport>;
|
|
130
|
+
private commitReplace;
|
|
131
|
+
private commitAdd;
|
|
132
|
+
}
|
|
133
|
+
/** Active always-follow (p0) corrections in a tenant: projects/<slug>/corrections/*.json, not `_`-prefixed. Bounded. */
|
|
134
|
+
export declare function countAlwaysFollow(root: string): Promise<number>;
|
|
135
|
+
export interface FieldCheck {
|
|
136
|
+
ok: true;
|
|
137
|
+
mode: ImportMode;
|
|
138
|
+
rules: boolean;
|
|
139
|
+
}
|
|
140
|
+
export interface ReceiveOptions {
|
|
141
|
+
dataDir: string;
|
|
142
|
+
uid: string;
|
|
143
|
+
boundary: string;
|
|
144
|
+
quotaBytes: number;
|
|
145
|
+
now: () => number;
|
|
146
|
+
limits: ImportLimits;
|
|
147
|
+
hooks?: ImportHooks;
|
|
148
|
+
gate: ImportGate;
|
|
149
|
+
/** Rate-limit key of the client address (one import at a time per address). */
|
|
150
|
+
ip: string;
|
|
151
|
+
/** Called once, when the file part starts and BEFORE anything is staged. Throw ImportAbort to refuse. */
|
|
152
|
+
check(fields: Readonly<Record<string, string>>): FieldCheck;
|
|
153
|
+
log: (line: string) => void;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Reads the multipart body of POST /connect/import, stages and commits it.
|
|
157
|
+
* Rejects with ImportAbort (show the user) or another error (log it; say "failed").
|
|
158
|
+
* Always removes its staging directory and releases the per-user lock.
|
|
159
|
+
*/
|
|
160
|
+
export declare function receiveImport(req: http.IncomingMessage, o: ReceiveOptions): Promise<ImportReport>;
|
|
161
|
+
//# sourceMappingURL=import-memory.d.ts.map
|