Files
fristy 650807c1fb
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
chore: restart the repository with a clean history
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
2026-11-10 11:31:13 +03:00

150 lines
6.3 KiB
JavaScript

const fs = require("node:fs");
const path = require("node:path");
const yaml = require("js-yaml");
const logger = require("./logger");
const DEFAULTS = {
listen: "0.0.0.0",
port: 8080,
httpsPort: 443,
redirectPort: 80,
baseDomain: "",
publicUrl: "",
tlsMode: "proxy",
acmeDirectoryUrl: "",
trustProxy: false,
httpMode: "subdomain",
gracePeriod: 30,
};
const ENV = {
listen: "FRISLIT_LISTEN",
port: "FRISLIT_PORT",
httpsPort: "FRISLIT_HTTPS_PORT",
redirectPort: "FRISLIT_REDIRECT_PORT",
tlsMode: "FRISLIT_TLS_MODE",
acmeDirectoryUrl: "FRISLIT_ACME_DIRECTORY",
baseDomain: "FRISLIT_BASE_DOMAIN",
publicUrl: "FRISLIT_PUBLIC_URL",
trustProxy: "FRISLIT_TRUST_PROXY",
httpMode: "FRISLIT_HTTP_MODE",
gracePeriod: "FRISLIT_GRACE_PERIOD",
};
const UI_SETTING_KEYS = ["baseDomain", "publicUrl", "httpMode", "gracePeriod", "tlsMode", "trustProxy"];
const PINNED_BY_ENV = { tlsMode: "The HTTPS mode" };
// A deployment configured for tunlit keeps working: FRISLIT_* wins, TUNLIT_* is the fallback.
const readEnv = name => {
const value = process.env[name];
if (value !== undefined && value !== "") return value;
const old = name.replace(/^FRISLIT_/, "TUNLIT_");
const legacy = process.env[old];
if (legacy !== undefined && legacy !== "") {
logger.warn(`Using the deprecated ${old}; rename it to ${name}`);
return legacy;
}
return undefined;
};
const pinned = (config, key) => key in PINNED_BY_ENV && config.sources[key] === "env";
const parseBool = value => ["1", "true", "yes", "on"].includes(String(value).trim().toLowerCase());
const normalize = config => {
config.port = Number(config.port);
config.httpsPort = Number(config.httpsPort);
config.redirectPort = Number(config.redirectPort);
config.gracePeriod = Number(config.gracePeriod);
config.baseDomain = String(config.baseDomain || "").trim().toLowerCase().replace(/^\.+/, "").replace(/\.+$/, "");
config.httpMode = String(config.httpMode || "subdomain").trim().toLowerCase();
config.tlsMode = String(config.tlsMode || "proxy").trim().toLowerCase();
config.acmeDirectoryUrl = String(config.acmeDirectoryUrl || "").trim();
config.managesTls = config.tlsMode === "acme";
config.trustProxy = config.managesTls
? false
: typeof config.trustProxy === "boolean" ? config.trustProxy : parseBool(config.trustProxy);
config.publicUrlConfigured = String(config.publicUrlConfigured ?? config.publicUrl ?? "").trim().replace(/\/+$/, "");
config.publicUrl = config.publicUrlConfigured || (config.baseDomain ? `https://${config.baseDomain}` : "");
config.publicScheme = config.publicUrl.startsWith("http://") ? "http" : "https";
config.wildcard = config.httpMode === "subdomain";
config.ready = !!config.baseDomain;
};
const validate = config => {
const problems = [];
if (!["subdomain", "path"].includes(config.httpMode)) problems.push("httpMode must be \"subdomain\" or \"path\"");
if (!["proxy", "acme"].includes(config.tlsMode)) problems.push("tlsMode must be \"proxy\" or \"acme\"");
for (const key of ["port", "httpsPort", "redirectPort"]) {
if (!Number.isInteger(config[key]) || config[key] < 1 || config[key] > 65535) problems.push(`${key} must be a number between 1 and 65535`);
}
if (!Number.isFinite(config.gracePeriod) || config.gracePeriod < 0) problems.push("gracePeriod must be a non-negative number of seconds");
if (config.publicUrlConfigured && !/^https?:\/\/[^/\s]+$/.test(config.publicUrlConfigured)) problems.push("publicUrl must look like https://host[:port] without a path");
if (config.baseDomain && !/^[a-z0-9.-]+$/.test(config.baseDomain)) problems.push("baseDomain must be a hostname");
return problems;
};
const loadConfig = () => {
const file = readEnv("FRISLIT_CONFIG") || path.join(process.cwd(), "config.yml");
let fromFile = {};
if (fs.existsSync(file)) {
const parsed = yaml.load(fs.readFileSync(file, "utf8"));
if (parsed && typeof parsed !== "object") throw new Error(`${file} must contain a YAML mapping`);
fromFile = parsed || {};
}
const config = { ...DEFAULTS };
const sources = Object.fromEntries(Object.keys(DEFAULTS).map(key => [key, "default"]));
for (const key of Object.keys(DEFAULTS)) {
if (fromFile[key] !== undefined && fromFile[key] !== null && fromFile[key] !== "") { config[key] = fromFile[key]; sources[key] = "file"; }
}
for (const [key, name] of Object.entries(ENV)) {
const value = readEnv(name);
if (value !== undefined) { config[key] = value; sources[key] = "env"; }
}
normalize(config);
const problems = validate(config);
if (problems.length) throw new Error("Invalid configuration:\n - " + problems.join("\n - "));
config.sources = sources;
config.configFile = file;
return config;
};
const applySettings = (config, requested) => {
const settings = { ...requested };
for (const key of Object.keys(settings).filter(key => pinned(config, key))) {
if (String(settings[key]).trim().toLowerCase() !== config[key]) {
throw Object.assign(new Error(`${PINNED_BY_ENV[key]} is set by ${ENV[key]}`), { code: "invalid_settings" });
}
delete settings[key];
}
const next = { ...config, ...settings };
if ("publicUrl" in settings) next.publicUrlConfigured = settings.publicUrl;
normalize(next);
const problems = validate(next);
if (problems.length) throw Object.assign(new Error(problems.join(", ")), { code: "invalid_settings" });
Object.assign(config, next);
for (const key of Object.keys(settings)) config.sources[key] = "ui";
return config;
};
const applyStored = (config, stored) => {
for (const key of UI_SETTING_KEYS) {
const value = stored[key];
if (value === undefined || value === null || value === "" || pinned(config, key)) continue;
config[key] = value;
config.sources[key] = "ui";
}
if ("publicUrl" in stored) config.publicUrlConfigured = stored.publicUrl;
normalize(config);
const problems = validate(config);
if (problems.length) throw new Error("Stored settings are invalid:\n - " + problems.join("\n - "));
return config;
};
module.exports = { loadConfig, applySettings, applyStored, UI_SETTING_KEYS };