CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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