Files
stack/packages/plugins/src/loader.mjs
T
jason.woltje 7fdb488270 Themes as installed plugins, chosen per organization (S4, plan C5)
Add a reviewed `theme` plugin kind (interfaceVersion 1, export `theme`, switch
"list"). A module returns data only: a displayName and a flat map of colour
custom properties for light, dim and dark. checkTheme validates names and
#rrggbb colours strictly and checks each text/background pair for contrast; a
bad module is refused and logged, never served.

The ten shipped palettes become the first ten modules under
packages/plugins/theme/<name>/. Modules are found by directory scan
(loader.discoverModules, loader.loadInstalled), not by a list in the Console.
loadModule and createLoader are unchanged.

The organization file takes `appearance: { theme, mode }`, validated in
packages/business/src/appearance.mjs; a missing appearance is valid.
GET /api/themes (themes-http.mjs) serves the installed, valid themes.

Browser: the Settings picker lists /api/themes. Precedence is a per-organization
browser override, then the organization's saved theme, then the built-in default
(Harbor, dark). Clearing the override restores the organization's choice. An
uninstalled or invalid saved theme renders the default with a visible warning
and keeps the saved value. A legacy global browser preference is not copied to
every organization. The organization is named in text, so colour is never the
only cue. A plain load writes no appearance record. The default look is
unchanged: computed styles match the base commit element for element.
2026-11-07 12:18:43 -06:00

222 lines
9.0 KiB
JavaScript

// The plug-in loader (REQ-PLUG-1). It switches on the modules the
// organization file's `plugins` object names, one key per kind, and loads
// only packages/plugins/<kind>/<name>/index.mjs from the tree this file runs
// from. It takes no path from config and loads nothing outside that tree
// (lead decision 143). A loaded module runs with the host's authority.
//
// A refusal keeps the kind's last good modules and is logged. A kind refused
// on its first load has nothing switched on. `loaded()` reports what each
// kind actually has switched on.
import { lstatSync, readdirSync, realpathSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { KINDS, NAME, PluginError, namesFor } from "./kinds.mjs";
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const refuse = (code, message, name) => {
throw new PluginError(code, message, name);
};
// Run one filesystem read. Any error becomes a refusal for this path, so load()
// logs it and keeps the kind's old modules (Darkwing N1, Filbert F2, sage-0314).
function fsCall(path, read) {
try {
return read(path);
} catch (e) {
if (e.code === "ENOENT" || e.code === "ENOTDIR") refuse("missing", `${path} is missing`);
refuse("unreadable", `${path} can't be read (${e.code ?? e.message})`);
}
}
// lstat one path component: it must exist, be the expected type, and not be a symlink.
function component(path, directory) {
const st = fsCall(path, lstatSync);
if (st.isSymbolicLink()) refuse("symlink", `${path} is a symlink`);
if (directory ? !st.isDirectory() : !st.isFile()) {
refuse("missing", `${path} is not a ${directory ? "directory" : "regular file"}`);
}
}
async function loadModule(spec, name, barrier) {
const kindDir = join(ROOT, spec.kind);
const nameDir = join(kindDir, name);
const index = join(nameDir, "index.mjs");
component(ROOT, true);
component(kindDir, true);
component(nameDir, true);
component(index, false);
barrier("checked", { kind: spec.kind, name });
// Last check before import: the resolved file is still the one checked
// above. Ancestors of packages/plugins are not checked (sage-0301).
const expected = join(fsCall(ROOT, realpathSync), spec.kind, name, "index.mjs");
const real = fsCall(index, realpathSync);
if (real !== expected) refuse("symlink", `${index} resolves to ${real}`);
let module;
try {
module = await import(pathToFileURL(expected).href);
} catch (e) {
refuse("load-failed", `${spec.kind} ${name} failed to load: ${e.message}`);
}
for (const key of ["interfaceVersion", ...spec.exports]) {
if (!Object.hasOwn(module, key)) refuse("missing-export", `${spec.kind} ${name} doesn't export ${key}`);
}
for (const key of spec.exports) {
if (typeof module[key] !== "function") refuse("missing-export", `${spec.kind} ${name} export ${key} isn't a function`);
}
if (module.interfaceVersion !== spec.interfaceVersion) {
refuse("interface-version",
`${spec.kind} ${name} implements interface ${module.interfaceVersion}, not ${spec.interfaceVersion}`);
}
return Object.freeze({ name, module });
}
// True when packages/plugins/<kind>/<name>/index.mjs exists in this tree as a real file, with no
// symlink on the way down. The setters use it to refuse a name no module answers to before it is
// written; the loader's own checks still run at load.
export function hasModule(kind, name) {
try {
if (!Object.hasOwn(KINDS, kind) || !NAME.test(name)) return false;
for (const dir of [ROOT, join(ROOT, kind), join(ROOT, kind, name)]) component(dir, true);
component(join(ROOT, kind, name, "index.mjs"), false);
return true;
} catch {
return false;
}
}
// The directories packages/plugins/<kind>/ holds in this tree (stream S4). A kind whose modules are
// data, like theme, has no setting to switch a module on: installing one is putting its directory
// here, so the tree is the list. `names` fit NAME, sorted; a symlinked entry is kept so the load
// refuses it by name. `invalid` are directories that don't fit NAME. Plain files (a README) are
// ignored. A missing kind directory is an empty list; a symlinked one is refused.
export function discoverModules(kind) {
if (!Object.hasOwn(KINDS, kind)) refuse("unknown-kind", `unknown kind ${JSON.stringify(kind)}`);
const kindDir = join(ROOT, kind);
component(ROOT, true);
try {
lstatSync(kindDir);
} catch (e) {
if (e.code === "ENOENT") return { names: [], invalid: [] };
}
component(kindDir, true);
const entries = fsCall(kindDir, dir => readdirSync(dir, { withFileTypes: true }));
const names = [], invalid = [];
for (const entry of entries) {
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
(NAME.test(entry.name) ? names : invalid).push(entry.name);
}
return { names: names.sort(), invalid: invalid.sort() };
}
// Loads every installed module of one kind through the same checks as a switched-on module
// (loadModule, unchanged). One refused module is reported and never hides the others, and nothing
// here changes what createLoader has switched on. Node caches an import by path, so a module
// whose file changed is read again only after a restart; a removed directory is gone at once.
export async function loadInstalled(kind, { barrier = () => {} } = {}) {
if (!Object.hasOwn(KINDS, kind)) refuse("unknown-kind", `unknown kind ${JSON.stringify(kind)}`);
const spec = KINDS[kind];
const modules = [], refused = [];
const report = (name, e) => {
if (!(e instanceof PluginError)) throw e;
refused.push({ event: "plugin-refused", kind, name: e.plugin ?? name, code: e.code, message: e.message });
};
let found;
try {
found = discoverModules(kind);
} catch (e) {
report(null, e);
return { modules, refused };
}
for (const name of found.invalid) report(name, new PluginError("bad-name", `${kind}/${name} doesn't match ${NAME}`, name));
for (const name of found.names) {
try {
modules.push(await loadModule(spec, name, barrier));
} catch (e) {
report(name, e);
}
}
return { modules, refused };
}
export function createLoader({ log, barrier = () => {} }) {
if (typeof log !== "function") throw new TypeError("createLoader needs a log callback");
const current = new Map(Object.keys(KINDS).map(kind => [kind, Object.freeze([])]));
let queue = Promise.resolve();
function refused(kind, name, e, report) {
if (!(e instanceof PluginError)) throw e;
// kept: what stays switched on, for the kind or (whole setting refused) for every kind.
const entry = {
event: "plugin-refused", kind, name: e.plugin ?? name, code: e.code, message: e.message,
kept: kind === null ? loaded() : current.has(kind) ? current.get(kind).map(m => m.name) : [],
};
log(entry);
report.push(entry);
}
async function apply(plugins) {
const report = [];
// A malformed whole setting refuses every kind; an unknown kind refuses only itself.
if (plugins === null || typeof plugins !== "object" || Array.isArray(plugins)) {
refused(null, null, new PluginError("bad-setting", "plugins must be an object with one key per kind"), report);
return { loaded: loaded(), refused: report };
}
for (const kind of Object.keys(plugins)) {
if (!Object.hasOwn(KINDS, kind)) {
refused(kind, null, new PluginError("unknown-kind", `plugins names unknown kind ${JSON.stringify(kind)}`), report);
}
}
for (const [kind, spec] of Object.entries(KINDS)) {
// An absent key switches the kind off.
let names = [];
try {
if (Object.hasOwn(plugins, kind)) names = namesFor(spec, plugins[kind], `plugins.${kind}`, refuse);
} catch (e) {
refused(kind, typeof plugins[kind] === "string" ? plugins[kind] : null, e, report);
continue;
}
const next = [];
let ok = true;
for (const name of names) {
try {
next.push(await loadModule(spec, name, barrier));
} catch (e) {
refused(kind, name, e, report);
ok = false;
break;
}
}
if (ok) current.set(kind, Object.freeze(next));
}
return { loaded: loaded(), refused: report };
}
// Loads run one at a time, so a kind's switch never interleaves.
function load(plugins) {
const run = queue.then(() => apply(plugins));
queue = run.catch(() => {});
return run;
}
function loaded() {
return Object.fromEntries([...current].map(([kind, modules]) => [kind, modules.map(m => m.name)]));
}
function modules(kind) {
if (!current.has(kind)) refuse("unknown-kind", `unknown kind ${JSON.stringify(kind)}`);
return current.get(kind).map(m => m.module);
}
// Capture names and modules together, without exposing mutable state or a dispatch lock.
function snapshot(kind) {
if (!current.has(kind)) refuse("unknown-kind", `unknown kind ${JSON.stringify(kind)}`);
return current.get(kind);
}
return { load, loaded, modules, snapshot };
}
// Shared pure plug-in setting classification; no check implementation is loaded.
export { isDailyModelQuota } from "./model-quota.mjs";