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/proxy/version.ts
Views: 687
1
import Cookies from "cookies";
2
3
import { versionCookieName } from "@cocalc/util/consts";
4
import basePath from "@cocalc/backend/base-path";
5
import getServerSettings from "../servers/server-settings";
6
import getLogger from "../logger";
7
8
let minVersion: number = 0;
9
10
const logger = getLogger("proxy:version");
11
12
// Import to wait until we know the valid min_version before serving.
13
export async function init(): Promise<void> {
14
const serverSettings = await getServerSettings();
15
minVersion = serverSettings.version["min_version"] ?? 0;
16
serverSettings.table.on("change", () => {
17
minVersion = serverSettings.version["min_version"] ?? 0;
18
});
19
}
20
21
// Returns true if the version check **fails**
22
// If res is not null, sends a message. If it is
23
// null, just returns true but doesn't send a response.
24
export function versionCheckFails(req, res?): boolean {
25
if (minVersion == 0) {
26
// If no minimal version is set, no need to do further work,
27
// since we'll pass it.
28
return false;
29
}
30
const cookies = new Cookies(req);
31
/* NOTE: The name of the cookie $VERSION_COOKIE_NAME is
32
also used in the frontend code file @cocalc/frontend/set-version-cookie.js
33
but everybody imports it from @cocalc/util/consts.
34
*/
35
const rawVal = cookies.get(versionCookieName(basePath));
36
if (rawVal == null) {
37
return true;
38
}
39
const version = parseInt(rawVal);
40
logger.debug("version check", { version, minVersion });
41
if (isNaN(version) || version < minVersion) {
42
if (res != null) {
43
// status code 4xx to indicate this is a client problem and not
44
// 5xx, a server problem
45
// 426 means "upgrade required"
46
res.writeHead(426, { "Content-Type": "text/html" });
47
res.end(
48
`426 (UPGRADE REQUIRED): reload CoCalc tab or restart your browser -- version=${version} < minVersion=${minVersion}`
49
);
50
}
51
return true;
52
} else {
53
return false;
54
}
55
}
56
57