Files
corescope-rx/src/config.js
T

35 lines
1.5 KiB
JavaScript

// Runtime deployment config, fetched from config.json (served next to
// index.html) at startup. Nothing is baked into the bundle — sysops edit
// config.json, not source. See config.example.json for the shape.
let cfg = null;
// normalizeConfig validates + normalizes a parsed config.json object. Throws on
// a missing required field (mqttUrl). resolveUrl is optional (empty = node-name
// resolution disabled).
export function normalizeConfig(raw) {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('config.json: expected a JSON object');
const c = {
mqttUrl: String(raw.mqttUrl || '').trim(),
mqttUsername: String(raw.mqttUsername || '').trim(),
mqttPassword: raw.mqttPassword == null ? '' : String(raw.mqttPassword),
resolveUrl: String(raw.resolveUrl || '').trim(),
};
if (!c.mqttUrl) throw new Error('config.json: "mqttUrl" is required');
return c;
}
// loadConfig fetches + normalizes config.json once and caches it. Throws if the
// file is missing/unreadable or invalid JSON.
export async function loadConfig(url = 'config.json') {
if (cfg) return cfg;
const r = await fetch(url, { cache: 'no-store' });
if (!r.ok) throw new Error('config.json not found (HTTP ' + r.status + ')');
let raw;
try { raw = await r.json(); } catch (e) { throw new Error('config.json: invalid JSON — ' + e.message); }
cfg = normalizeConfig(raw);
return cfg;
}
export function getConfig() { return cfg; }
export function setConfig(c) { cfg = c; } // test seam