Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/hub/proxy/version.ts
Views: 687
import Cookies from "cookies";12import { versionCookieName } from "@cocalc/util/consts";3import basePath from "@cocalc/backend/base-path";4import getServerSettings from "../servers/server-settings";5import getLogger from "../logger";67let minVersion: number = 0;89const logger = getLogger("proxy:version");1011// Import to wait until we know the valid min_version before serving.12export async function init(): Promise<void> {13const serverSettings = await getServerSettings();14minVersion = serverSettings.version["min_version"] ?? 0;15serverSettings.table.on("change", () => {16minVersion = serverSettings.version["min_version"] ?? 0;17});18}1920// Returns true if the version check **fails**21// If res is not null, sends a message. If it is22// null, just returns true but doesn't send a response.23export function versionCheckFails(req, res?): boolean {24if (minVersion == 0) {25// If no minimal version is set, no need to do further work,26// since we'll pass it.27return false;28}29const cookies = new Cookies(req);30/* NOTE: The name of the cookie $VERSION_COOKIE_NAME is31also used in the frontend code file @cocalc/frontend/set-version-cookie.js32but everybody imports it from @cocalc/util/consts.33*/34const rawVal = cookies.get(versionCookieName(basePath));35if (rawVal == null) {36return true;37}38const version = parseInt(rawVal);39logger.debug("version check", { version, minVersion });40if (isNaN(version) || version < minVersion) {41if (res != null) {42// status code 4xx to indicate this is a client problem and not43// 5xx, a server problem44// 426 means "upgrade required"45res.writeHead(426, { "Content-Type": "text/html" });46res.end(47`426 (UPGRADE REQUIRED): reload CoCalc tab or restart your browser -- version=${version} < minVersion=${minVersion}`48);49}50return true;51} else {52return false;53}54}555657