ShtukbaseStandard
package file→ .shtukbase/packages/live/server/settings.ts

server/settings.ts

41 lines, 2.3 KB.

  1. // The project's settings of the live server: the header of its page `/base/live.md` (the page the package puts
  2. // into the project). The same reading serves the server and the check of the norms, so both see one truth.
  3. import { existsSync, readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
  6. export const PAGE = '/base/live.md';
  7. export const DEFAULT_PORT = 4322;
  8. export const LOOPBACK = new Set(['127.0.0.1', 'localhost', '::1']);
  9. /** `shtuk check`: every check of the project, the way the tool runs them. */
  10. export const CHECK_ALL = 'shtuk check';
  11. export type Settings = { host: string; port: number; run: string[]; problems: string[] };
  12. /** Settings from the page; `problems` says what in them breaks a norm (the server refuses to start on any). */
  13. export function readSettings(root: string): Settings {
  14. const out: Settings = { host: '127.0.0.1', port: DEFAULT_PORT, run: [], problems: [] };
  15. const f = join(root, PAGE.slice(1));
  16. if (!existsSync(f)) return out;
  17. const fm = parseYaml(splitFrontMatter(readFileSync(f, 'utf8'))[0]);
  18. if (fm.host !== undefined && fm.host !== null) {
  19. out.host = pystr(fm.host).trim();
  20. 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)`);
  21. }
  22. if (fm.port !== undefined && fm.port !== null) {
  23. 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`);
  24. else out.port = fm.port;
  25. }
  26. for (const k of ['token', 'auth', 'public', 'open', 'noToken', 'anonymousWrite'])
  27. if (k in fm) out.problems.push(`${k}: - writing without the token cannot be configured: remove the field (LIVE-R02)`);
  28. const run = fm.run ?? [];
  29. if (!Array.isArray(run)) out.problems.push('run: - a list of lines "package action" is needed (LIVE-R03)');
  30. else
  31. for (const x of run) {
  32. const s = pystr(x).trim().replace(/\s+/g, ' ');
  33. if (typeof x !== 'string' || !/^[a-z0-9][a-z0-9-]* [a-z0-9][a-z0-9_-]*$/i.test(s))
  34. out.problems.push(`run: "${pystr(x)}" - a line "package action" is needed, without asterisks or patterns (LIVE-R03)`);
  35. else out.run.push(s);
  36. }
  37. return out;
  38. }