UNPKG

@cocalc/hub

Version:
90 lines 4.22 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ Object.defineProperty(exports, "__esModule", { value: true }); /* Synchronized table that tracks server settings. */ const async_utils_1 = require("@cocalc/util/async-utils"); const site_settings_extras_1 = require("@cocalc/util/db-schema/site-settings-extras"); const misc_1 = require("@cocalc/util/misc"); const schema_1 = require("@cocalc/util/schema"); const lodash_1 = require("lodash"); const database_1 = require("./database"); let serverSettings = undefined; async function getServerSettings() { if (serverSettings != null) { return serverSettings; } const table = database_1.database.server_settings_synctable(); serverSettings = { all: {}, pub: {}, version: {}, table: table }; const { all, pub, version } = serverSettings; const update = async function () { const allRaw = {}; table.get().forEach((record, field) => { allRaw[field] = record.get("value"); }); table.get().forEach(function (record, field) { const rawValue = record.get("value"); // process all values from the database according to the optional "to_val" mapping function const spec = schema_1.site_settings_conf[field] ?? site_settings_extras_1.EXTRAS[field]; if (typeof spec?.to_val == "function") { all[field] = spec.to_val(rawValue, allRaw); } else { if (typeof rawValue == "string" || typeof rawValue == "boolean") { all[field] = rawValue; } } // export certain fields to "pub[...]" and some old code regarding the version numbers if (schema_1.site_settings_conf[field]) { if ((0, misc_1.startswith)(field, "version_")) { const field_val = (all[field] = parseInt(all[field])); if (isNaN(field_val) || field_val * 1000 >= new Date().getTime()) { // Guard against horrible error in which version is in future (so impossible) or NaN (e.g., an invalid string pasted by admin). // In this case, just use 0, which is always satisifed. all[field] = 0; } version[field] = all[field]; } pub[field] = all[field]; } }); // set all default values for (const config of [schema_1.site_settings_conf, site_settings_extras_1.EXTRAS]) { for (const field in config) { if (all[field] == null) { const spec = config[field]; const fallbackVal = spec?.to_val != null ? spec.to_val(spec.default, allRaw) : spec.default; // we don't bother to set empty strings or empty arrays if (fallbackVal === "" || (0, lodash_1.isEmpty)(fallbackVal)) continue; all[field] = fallbackVal; // site-settings end up in the "pub" object as well // while "all" is the one we keep to us, contains secrets if (schema_1.site_settings_conf === config) { pub[field] = all[field]; } } } } // PRECAUTION: never make the required version bigger than version_recommended_browser. Very important // not to stupidly completely eliminate all cocalc users by a typo... for (const x of ["project", "browser"]) { const field = `version_min_${x}`; const minver = all[field] || 0; const recomm = all["version_recommended_browser"] || 0; pub[field] = version[field] = all[field] = Math.min(minver, recomm); } }; table.on("change", update); table.on("init", update); await (0, async_utils_1.once)(table, "init"); return serverSettings; } exports.default = getServerSettings; //# sourceMappingURL=server-settings.js.map