package file→ .shtukbase/packages/live/server/settings.ts
server/settings.ts
41 lines, 2.3 KB.
- // The project's settings of the live server: the header of its page `/base/live.md` (the page the package puts
- // into the project). The same reading serves the server and the check of the norms, so both see one truth.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
- export const PAGE = '/base/live.md';
- export const DEFAULT_PORT = 4322;
- export const LOOPBACK = new Set(['127.0.0.1', 'localhost', '::1']);
- /** `shtuk check`: every check of the project, the way the tool runs them. */
- export const CHECK_ALL = 'shtuk check';
- export type Settings = { host: string; port: number; run: string[]; problems: string[] };
- /** Settings from the page; `problems` says what in them breaks a norm (the server refuses to start on any). */
- export function readSettings(root: string): Settings {
- const out: Settings = { host: '127.0.0.1', port: DEFAULT_PORT, run: [], problems: [] };
- const f = join(root, PAGE.slice(1));
- if (!existsSync(f)) return out;
- const fm = parseYaml(splitFrontMatter(readFileSync(f, 'utf8'))[0]);
- if (fm.host !== undefined && fm.host !== null) {
- out.host = pystr(fm.host).trim();
- if (!LOOPBACK.has(out.host)) out.problems.push(`host: ${out.host} - the server listens only on this computer: 127.0.0.1, localhost or ::1 (LIVE-R01)`);
- }
- if (fm.port !== undefined && fm.port !== null) {
- if (typeof fm.port !== 'number' || !Number.isInteger(fm.port) || fm.port < 0 || fm.port > 65535) out.problems.push(`port: ${pystr(fm.port)} - a port number 1-65535 is needed`);
- else out.port = fm.port;
- }
- for (const k of ['token', 'auth', 'public', 'open', 'noToken', 'anonymousWrite'])
- if (k in fm) out.problems.push(`${k}: - writing without the token cannot be configured: remove the field (LIVE-R02)`);
- const run = fm.run ?? [];
- if (!Array.isArray(run)) out.problems.push('run: - a list of lines "package action" is needed (LIVE-R03)');
- else
- for (const x of run) {
- const s = pystr(x).trim().replace(/\s+/g, ' ');
- if (typeof x !== 'string' || !/^[a-z0-9][a-z0-9-]* [a-z0-9][a-z0-9_-]*$/i.test(s))
- out.problems.push(`run: "${pystr(x)}" - a line "package action" is needed, without asterisks or patterns (LIVE-R03)`);
- else out.run.push(s);
- }
- return out;
- }