neurastream 0.2.23 → 0.2.24
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
CHANGED
|
@@ -121,7 +121,7 @@ An answer appends one record to `<queue_state>/owner-inbox.jsonl` (plan, questio
|
|
|
121
121
|
|
|
122
122
|
## Static pages (`neurastream pages`)
|
|
123
123
|
|
|
124
|
-
`neurastream pages on [--port N] [--dir D]` installs the systemd user unit and serves regular files in the configured pages directory on the tailnet; `pages publish <file> [name] [--force]` publishes an artifact and prints its MagicDNS URL. `pages list`
|
|
124
|
+
`neurastream pages on [--port N] [--dir D]` installs the systemd user unit and serves regular files in the configured pages directory on the tailnet; `pages publish <file> [name] [--force] [--keep]` publishes an artifact and prints its MagicDNS URL, then `expires YYYY-MM-DD` or `kept`. A page without `--keep` is deleted `pages.keep_days` days after publishing (default 15). `pages list` prints each file as `<name> expires YYYY-MM-DD` or `<name> kept`, and `pages off` stops the service. Publish, list, and serve remove expired files first; serve repeats that sweep every hour. Requests are limited to the configured owner's tailnet login or loopback; hidden files, symlinks and paths outside the directory are not served.
|
|
125
125
|
|
|
126
126
|
## Session rollover
|
|
127
127
|
|
package/package.json
CHANGED
package/src/cli/pages.ts
CHANGED
|
@@ -2,20 +2,49 @@ import { readdirSync } from "node:fs";
|
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { defaultRunSource } from "../adapters/omp/sessions.ts";
|
|
4
4
|
import { loadConfig } from "../core/config.ts";
|
|
5
|
+
import { readExpiry, sweepPages } from "../core/pages-expiry.ts";
|
|
5
6
|
import { startPagesServer } from "../core/pages.ts";
|
|
6
7
|
import { pagesOff, pagesOn, publishPage, type Runner } from "../core/pages-setup.ts";
|
|
7
8
|
import { realTailscale, type Tailscale } from "../core/tailscale.ts";
|
|
8
9
|
|
|
9
|
-
export interface PagesDeps { home?: string; runner?: Runner; tailscale?: Tailscale }
|
|
10
|
+
export interface PagesDeps { home?: string; runner?: Runner; tailscale?: Tailscale; clock?: () => Date }
|
|
11
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
10
12
|
export async function runPages(args: string[] = [], deps: PagesDeps = {}): Promise<number> {
|
|
11
|
-
const home=deps.home ?? homedir();
|
|
13
|
+
const home = deps.home ?? homedir();
|
|
14
|
+
const clock = deps.clock ?? (() => new Date());
|
|
12
15
|
try {
|
|
13
|
-
if(args[0]==="on") { let port:number|undefined,dir:string|undefined; for(let i=1;i<args.length;i++){if(args[i]==="--port")port=Number(args[++i]);else if(args[i]==="--dir")dir=args[++i];else return 2;} return pagesOn({home,runner:deps.runner,tailscale:deps.tailscale,port,dir}); }
|
|
14
|
-
if(args[0]==="off"&&args.length===1)return pagesOff({home,runner:deps.runner});
|
|
15
|
-
const cfg=loadConfig({home});
|
|
16
|
-
if(args[0]==="serve"&&args.length===1)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (args[0] === "on") { let port: number | undefined, dir: string | undefined; for (let i = 1; i < args.length; i++) { if (args[i] === "--port") port = Number(args[++i]); else if (args[i] === "--dir") dir = args[++i]; else return 2; } return pagesOn({ home, runner: deps.runner, tailscale: deps.tailscale, port, dir }); }
|
|
17
|
+
if (args[0] === "off" && args.length === 1) return pagesOff({ home, runner: deps.runner });
|
|
18
|
+
const cfg = loadConfig({ home });
|
|
19
|
+
if (args[0] === "serve" && args.length === 1) {
|
|
20
|
+
if (!cfg.pages.enabled) throw new Error("pages.enabled is false");
|
|
21
|
+
sweepPages(cfg.pages.dir, cfg.pages.keep_days, clock());
|
|
22
|
+
startPagesServer({ cfg, tailscale: deps.tailscale ?? realTailscale, source: defaultRunSource(cfg) });
|
|
23
|
+
setInterval(() => { try { sweepPages(cfg.pages.dir, cfg.pages.keep_days, clock()); } catch (e) { console.error(e instanceof Error ? e.message : String(e)); } }, HOUR_MS);
|
|
24
|
+
return new Promise<number>(() => {});
|
|
25
|
+
}
|
|
26
|
+
if (args[0] === "publish" && args[1]) {
|
|
27
|
+
let force = false, keep = false, name: string | undefined;
|
|
28
|
+
for (const arg of args.slice(2)) { if (arg === "--force") force = true; else if (arg === "--keep") keep = true; else if (!name) name = arg; else return 2; }
|
|
29
|
+
const now = clock();
|
|
30
|
+
sweepPages(cfg.pages.dir, cfg.pages.keep_days, now);
|
|
31
|
+
const n = publishPage(args[1], name, force, cfg.pages.dir, keep, cfg.pages.keep_days, now);
|
|
32
|
+
const host = (deps.tailscale ?? realTailscale).dnsName() ?? "tailnet";
|
|
33
|
+
console.log(`http://${host}:${cfg.pages.port}/${encodeURIComponent(n)}`);
|
|
34
|
+
const expires = readExpiry(cfg.pages.dir)[n]?.expires;
|
|
35
|
+
console.log(expires ? `expires ${expires.slice(0, 10)}` : "kept");
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
if (args[0] === "list" && args.length === 1) {
|
|
39
|
+
sweepPages(cfg.pages.dir, cfg.pages.keep_days, clock());
|
|
40
|
+
const map = readExpiry(cfg.pages.dir);
|
|
41
|
+
for (const f of readdirSync(cfg.pages.dir, { withFileTypes: true }).filter(e => e.isFile() && !e.name.startsWith("."))) {
|
|
42
|
+
const expires = map[f.name]?.expires;
|
|
43
|
+
console.log(`${f.name} ${expires ? `expires ${expires.slice(0, 10)}` : "kept"}`);
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
} catch (e) { console.error(e instanceof Error ? e.message : String(e)); return 1; }
|
|
48
|
+
console.error("usage: neurastream pages on [--port N] [--dir D] | off | serve | publish <file> [<name>] [--force] [--keep] | list");
|
|
49
|
+
return 2;
|
|
21
50
|
}
|
package/src/core/config.ts
CHANGED
|
@@ -136,7 +136,7 @@ export interface Config {
|
|
|
136
136
|
/** The owner's phone page (`neurastream ui`): binds the tailnet address only. */
|
|
137
137
|
ui: { enabled: boolean; port: number; owner_login: string };
|
|
138
138
|
/** Static pages hosted over the tailnet. */
|
|
139
|
-
pages: { enabled: boolean; port: number; dir: string };
|
|
139
|
+
pages: { enabled: boolean; port: number; dir: string; keep_days: number };
|
|
140
140
|
rollover: { enabled: boolean; max_session_mb: number; exit_grace_s: number };
|
|
141
141
|
metrics: { report_days: number; correction_patterns: string[]; ci_pipelines_cmd: string };
|
|
142
142
|
/** Incident detectors (src/core/incident-detectors.ts): when a sighting becomes an incident. */
|
|
@@ -409,6 +409,7 @@ function check(cfg: Config): void {
|
|
|
409
409
|
if (!(cfg.ui.port >= 1 && cfg.ui.port <= 65535)) throw new ConfigError("ui.port must be between 1 and 65535");
|
|
410
410
|
if (!Number.isInteger(cfg.pages.port) || cfg.pages.port < 1 || cfg.pages.port > 65535) throw new ConfigError("pages.port must be an integer between 1 and 65535");
|
|
411
411
|
if (typeof cfg.pages.dir !== "string" || cfg.pages.dir.length === 0) throw new ConfigError("pages.dir must be a non-empty path");
|
|
412
|
+
if (!Number.isInteger(cfg.pages.keep_days) || cfg.pages.keep_days < 1) throw new ConfigError("pages.keep_days must be an integer >= 1");
|
|
412
413
|
for (const p of cfg.metrics.correction_patterns) {
|
|
413
414
|
try {
|
|
414
415
|
new RegExp(p, "i");
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { lstatSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export interface ExpiryRecord { published: string; expires: string | null }
|
|
5
|
+
export type ExpiryMap = Record<string, ExpiryRecord>;
|
|
6
|
+
|
|
7
|
+
const RECORD = ".expiry.json";
|
|
8
|
+
const DAY_MS = 86_400_000;
|
|
9
|
+
|
|
10
|
+
function expiryRecord(value: unknown): ExpiryRecord | null {
|
|
11
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
12
|
+
const published = "published" in value ? value.published : undefined;
|
|
13
|
+
const expires = "expires" in value ? value.expires : undefined;
|
|
14
|
+
if (typeof published !== "string") return null;
|
|
15
|
+
if (expires !== null && typeof expires !== "string") return null;
|
|
16
|
+
return { published, expires };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function readExpiry(dir: string): ExpiryMap {
|
|
20
|
+
const path = join(dir, RECORD);
|
|
21
|
+
let st;
|
|
22
|
+
try { st = lstatSync(path); } catch { return {}; }
|
|
23
|
+
if (!st.isFile()) return {};
|
|
24
|
+
try {
|
|
25
|
+
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
26
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
|
|
27
|
+
const out: ExpiryMap = {};
|
|
28
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
29
|
+
const rec = expiryRecord(value);
|
|
30
|
+
if (rec) out[name] = rec;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
} catch { return {}; }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function writeExpiry(dir: string, map: ExpiryMap): void {
|
|
37
|
+
const tmp = join(dir, `.expiry.${process.pid}.${Date.now()}.tmp`);
|
|
38
|
+
writeFileSync(tmp, `${JSON.stringify(map)}\n`);
|
|
39
|
+
renameSync(tmp, join(dir, RECORD));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function recordPublish(dir: string, name: string, keep: boolean, keepDays: number, now: Date): ExpiryRecord {
|
|
43
|
+
const map = readExpiry(dir);
|
|
44
|
+
const existing = map[name];
|
|
45
|
+
const rec: ExpiryRecord = {
|
|
46
|
+
published: now.toISOString(),
|
|
47
|
+
expires: keep || existing?.expires === null ? null : new Date(now.getTime() + keepDays * DAY_MS).toISOString(),
|
|
48
|
+
};
|
|
49
|
+
map[name] = rec;
|
|
50
|
+
writeExpiry(dir, map);
|
|
51
|
+
return rec;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function sweepPages(dir: string, keepDays: number, now: Date): { removed: string[] } {
|
|
55
|
+
let dirStat;
|
|
56
|
+
try { dirStat = lstatSync(dir); } catch { return { removed: [] }; }
|
|
57
|
+
if (!dirStat.isDirectory()) return { removed: [] };
|
|
58
|
+
const map = readExpiry(dir);
|
|
59
|
+
const next: ExpiryMap = {};
|
|
60
|
+
const removed: string[] = [];
|
|
61
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
62
|
+
if (entry.name.startsWith(".") || !entry.isFile()) continue;
|
|
63
|
+
const path = join(dir, entry.name);
|
|
64
|
+
let st;
|
|
65
|
+
try { st = lstatSync(path); } catch { continue; }
|
|
66
|
+
if (!st.isFile()) continue;
|
|
67
|
+
const rec = map[entry.name];
|
|
68
|
+
const expiresAt = rec?.expires == null ? null : Date.parse(rec.expires);
|
|
69
|
+
if (typeof expiresAt === "number" && Number.isFinite(expiresAt) && expiresAt < now.getTime()) {
|
|
70
|
+
unlinkSync(path);
|
|
71
|
+
removed.push(entry.name);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (rec && (rec.expires === null || (typeof expiresAt === "number" && Number.isFinite(expiresAt)))) next[entry.name] = rec;
|
|
75
|
+
else next[entry.name] = { published: now.toISOString(), expires: new Date(now.getTime() + keepDays * DAY_MS).toISOString() };
|
|
76
|
+
}
|
|
77
|
+
writeExpiry(dir, next);
|
|
78
|
+
return { removed };
|
|
79
|
+
}
|
package/src/core/pages-setup.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { homedir } from "node:os";
|
|
|
4
4
|
import { dirname, join, basename, extname } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { configFile, loadConfig } from "./config.ts";
|
|
7
|
+
import { recordPublish } from "./pages-expiry.ts";
|
|
7
8
|
import { realTailscale, type Tailscale } from "./tailscale.ts";
|
|
8
9
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
9
10
|
export const PAGES_UNIT_TEMPLATE = join(HERE, "../../templates/systemd/neurastream-pages.service");
|
|
@@ -45,9 +46,9 @@ export function pagesOff(opts: Setup = {}): number {
|
|
|
45
46
|
const cfg=loadConfig({home}); writeFileSync(file,editBlock(old,{enabled:false,port:cfg.pages.port,dir:cfg.pages.dir}));
|
|
46
47
|
const r=runner(["--user","stop","neurastream-pages"]); if(r.status!==0){console.error(r.stderr);return 1;} return 0;
|
|
47
48
|
}
|
|
48
|
-
export function publishPage(file: string, name: string | undefined, force: boolean, dir: string): string {
|
|
49
|
+
export function publishPage(file: string, name: string | undefined, force: boolean, dir: string, keep = false, keepDays = 15, now = new Date()): string {
|
|
49
50
|
const source=basename(file), base=(name ?? source.slice(0,source.length-extname(source).length)).toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"");
|
|
50
51
|
const targetName=base+extname(source).toLowerCase(), target=join(dir,targetName); mkdirSync(dir,{recursive:true});
|
|
51
|
-
if(existsSync(target)){const a=readFileSync(file),b=readFileSync(target); if(a.equals(b))
|
|
52
|
-
copyFileSync(file,target); return targetName;
|
|
52
|
+
if(existsSync(target)){const a=readFileSync(file),b=readFileSync(target); if(a.equals(b)){recordPublish(dir,targetName,keep,keepDays,now);return targetName;} if(!force) throw new Error(`pages: ${targetName} exists with different content (use --force)`);}
|
|
53
|
+
copyFileSync(file,target); recordPublish(dir,targetName,keep,keepDays,now); return targetName;
|
|
53
54
|
}
|
package/templates/config.yml
CHANGED
|
@@ -53,4 +53,4 @@ Open the file at two widths and check the root plus one drilled level:
|
|
|
53
53
|
|
|
54
54
|
## Host
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
Build the HTML in a scratch directory (`mktemp -d`), not in a repo. Publish it with `neurastream pages publish <file>`. The page is deleted after 15 days. Add `--keep` only when the owner's prompt says to keep it permanently. Never commit the HTML into a repo unless the owner asks.
|