2026-09-13 05:56:27 +03:00
|
|
|
/**
|
|
|
|
|
* Server-side persistence for the graph render settings (the settings-panel
|
|
|
|
|
* controls): data/render-settings.json — read-modify-write, NOT a ledger.
|
|
|
|
|
*
|
|
|
|
|
* The shape, defaults, and sanitizer live in src/core/render-settings.ts
|
|
|
|
|
* (pure, shared with the client); this module only moves bytes. Whatever
|
|
|
|
|
* comes off disk is sanitized by the caller on load — and we sanitize on
|
|
|
|
|
* write too, so the file can never hold an out-of-range value that would
|
|
|
|
|
* silently shift a slider.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import { RENDER_SETTINGS_FILE } from "../../config.ts";
|
|
|
|
|
import { DEFAULT_RENDER_SETTINGS, sanitizeRenderSettings, type GraphRenderSettings } from "../core/render-settings.ts";
|
2026-10-10 13:54:17 +03:00
|
|
|
import { assertGraphFormat, ensureGraphFormat } from "./graph-format.ts";
|
2026-09-13 05:56:27 +03:00
|
|
|
|
|
|
|
|
/** The persisted settings, or null when no file exists yet (first boot). */
|
|
|
|
|
export function readRenderSettings(): GraphRenderSettings | null {
|
2026-10-10 13:54:17 +03:00
|
|
|
assertGraphFormat(); // honest refusal on a newer-stamped data dir
|
2026-09-13 05:56:27 +03:00
|
|
|
let raw: string;
|
|
|
|
|
try {
|
|
|
|
|
raw = fs.readFileSync(RENDER_SETTINGS_FILE, "utf-8");
|
|
|
|
|
} catch {
|
|
|
|
|
return null; // never booted / unreadable → caller uses defaults
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return sanitizeRenderSettings(JSON.parse(raw));
|
|
|
|
|
} catch {
|
|
|
|
|
return null; // corrupt → defaults, never crash the view
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Persist settings (sanitized). Returns what was written. */
|
|
|
|
|
export function writeRenderSettings(raw: unknown): GraphRenderSettings {
|
|
|
|
|
const settings = sanitizeRenderSettings(raw);
|
|
|
|
|
fs.mkdirSync(path.dirname(RENDER_SETTINGS_FILE), { recursive: true });
|
2026-10-10 13:54:17 +03:00
|
|
|
ensureGraphFormat(); // the lazy format stamp (graph-format.ts)
|
2026-09-13 05:56:27 +03:00
|
|
|
fs.writeFileSync(RENDER_SETTINGS_FILE, JSON.stringify(settings, null, "\t"), "utf-8");
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { DEFAULT_RENDER_SETTINGS };
|