Path: blob/master/src/packages/hub/servers/server-settings.ts
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Synchronized table that tracks server settings.7*/89import { isEmpty } from "lodash";10import { once } from "@cocalc/util/async-utils";11import { EXTRAS as SERVER_SETTINGS_EXTRAS } from "@cocalc/util/db-schema/site-settings-extras";12import { AllSiteSettings } from "@cocalc/util/db-schema/types";13import { startswith } from "@cocalc/util/misc";14import { site_settings_conf as SITE_SETTINGS_CONF } from "@cocalc/util/schema";15import { database } from "./database";1617// Returns:18// - all: a mutable javascript object that is a map from each server setting to its current value.19// This includes VERY private info (e.g., stripe private key)20// - pub: similar, but only subset of public info that is needed for browser UI rendering.21// - version22// - table: the table, so you can watch for on change events...23// These get automatically updated when the database changes.2425export interface ServerSettingsDynamic {26all: AllSiteSettings;27pub: object;28version: {29version_min_browser?: number;30version_recommended_browser?: number;31version_min_project?: number;32};33table: any;34}3536let serverSettings: ServerSettingsDynamic | undefined = undefined;3738export default async function getServerSettings(): Promise<ServerSettingsDynamic> {39if (serverSettings != null) {40return serverSettings;41}42const table = database.server_settings_synctable();43serverSettings = { all: {}, pub: {}, version: {}, table: table };44const { all, pub, version } = serverSettings;45const update = async function () {46const allRaw = {};47table.get().forEach((record, field) => {48allRaw[field] = record.get("value");49});5051table.get().forEach(function (record, field) {52const rawValue = record.get("value");5354// process all values from the database according to the optional "to_val" mapping function55const spec = SITE_SETTINGS_CONF[field] ?? SERVER_SETTINGS_EXTRAS[field];56if (typeof spec?.to_val == "function") {57all[field] = spec.to_val(rawValue, allRaw);58} else {59if (typeof rawValue == "string" || typeof rawValue == "boolean") {60all[field] = rawValue;61}62}6364// export certain fields to "pub[...]" and some old code regarding the version numbers65if (SITE_SETTINGS_CONF[field]) {66if (startswith(field, "version_")) {67const field_val: number = (all[field] = parseInt(all[field]));68if (isNaN(field_val) || field_val * 1000 >= new Date().getTime()) {69// Guard against horrible error in which version is in future (so impossible) or NaN (e.g., an invalid string pasted by admin).70// In this case, just use 0, which is always satisifed.71all[field] = 0;72}73version[field] = all[field];74}75pub[field] = all[field];76}77});7879// set all default values80for (const config of [SITE_SETTINGS_CONF, SERVER_SETTINGS_EXTRAS]) {81for (const field in config) {82if (all[field] == null) {83const spec = config[field];84const fallbackVal =85spec?.to_val != null86? spec.to_val(spec.default, allRaw)87: spec.default;88// we don't bother to set empty strings or empty arrays89if (90(typeof fallbackVal === "string" && fallbackVal === "") ||91(Array.isArray(fallbackVal) && isEmpty(fallbackVal))92)93continue;94all[field] = fallbackVal;95// site-settings end up in the "pub" object as well96// while "all" is the one we keep to us, contains secrets97if (SITE_SETTINGS_CONF === config) {98pub[field] = all[field];99}100}101}102}103104// Since we want to tell users about the estimated LLM interaction price, we have to send the markup as well.105pub["_llm_markup"] = all.pay_as_you_go_openai_markup_percentage;106107// PRECAUTION: never make the required version bigger than version_recommended_browser. Very important108// not to stupidly completely eliminate all cocalc users by a typo...109for (const x of ["project", "browser"]) {110const field = `version_min_${x}`;111const minver = all[field] || 0;112const recomm = all["version_recommended_browser"] || 0;113pub[field] = version[field] = all[field] = Math.min(minver, recomm);114}115};116table.on("change", update);117table.on("init", update);118await once(table, "init");119return serverSettings;120}121122123