Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/proxy/version.ts
1496 views
1
import Cookies from "cookies";
2
import { versionCookieName } from "@cocalc/util/consts";
3
import basePath from "@cocalc/backend/base-path";
4
import getServerSettings from "../servers/server-settings";
5
import getLogger from "../logger";
6
7
export let minVersion: number = 0;
8
const logger = getLogger("proxy:version");
9
10
// Import to wait until we know the valid min_version before serving.
11
let initialized = false;
12
export async function init(): Promise<void> {
13
if (initialized) {
14
return;
15
}
16
initialized = true;
17
const serverSettings = await getServerSettings();
18
minVersion = serverSettings.version.version_min_browser ?? 0;
19
serverSettings.table.on("change", () => {
20
minVersion = serverSettings.version.version_min_browser ?? 0;
21
});
22
}
23
24
// Returns true if the version check **fails**
25
// If res is not null, sends a message. If it is
26
// null, just returns true but doesn't send a response.
27
export function versionCheckFails(req, res?): boolean {
28
if (minVersion == 0) {
29
// If no minimal version is set, no need to do further work,
30
// since we'll pass it.
31
return false;
32
}
33
if (req.url?.includes("raw/.smc/ws")) {
34
// TODO: currently we do not do version checks on the websocket directly to a project,
35
// which is used by the compute server filesystem for sync. We don't have a good
36
// way in place to automatically update those containers yet, etc.
37
return false;
38
}
39
const cookies = new Cookies(req);
40
/* NOTE: The name of the cookie $VERSION_COOKIE_NAME is
41
also used in the frontend code file @cocalc/frontend/set-version-cookie.js
42
but everybody imports it from @cocalc/util/consts.
43
*/
44
const rawVal = cookies.get(versionCookieName(basePath));
45
if (!rawVal) {
46
// compute servers use this! They don't set any version cookies.
47
return false;
48
}
49
const version = parseInt(rawVal);
50
logger.debug("version check", { version, minVersion });
51
if (isNaN(version) || version < minVersion) {
52
if (res != null) {
53
// status code 4xx to indicate this is a client problem and not
54
// 5xx, a server problem
55
// 426 means "upgrade required"
56
res.writeHead(426, { "Content-Type": "text/html" });
57
res.end(
58
`426 (UPGRADE REQUIRED): reload CoCalc tab or restart your browser -- version=${version} < minVersion=${minVersion}`,
59
);
60
}
61
return true;
62
} else {
63
return false;
64
}
65
}
66
67