@cocalc/hub
Version:
CoCalc: Backend webserver component
50 lines • 2.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.versionCheckFails = exports.init = void 0;
const cookies_1 = __importDefault(require("cookies"));
const consts_1 = require("@cocalc/util/consts");
const base_path_1 = __importDefault(require("@cocalc/backend/base-path"));
const server_settings_1 = __importDefault(require("../servers/server-settings"));
const logger_1 = __importDefault(require("../logger"));
let minVersion = 0;
const winston = (0, logger_1.default)("proxy: version");
// Import to wait until we know the valid min_version before serving.
async function init() {
const serverSettings = await (0, server_settings_1.default)();
minVersion = serverSettings.version["min_version"] ?? 0;
serverSettings.table.on("change", () => {
minVersion = serverSettings.version["min_version"] ?? 0;
});
}
exports.init = init;
// Returns true if the version check **fails**
// If res is not null, sends a message. If it is
// null, just returns true but doesn't send a response.
function versionCheckFails(req, res) {
const cookies = new cookies_1.default(req);
/* NOTE: The name of the cookie $VERSION_COOKIE_NAME is
also used in the frontend code file @cocalc/frontend/set-version-cookie.js
but everybody imports it from @cocalc/util/consts.
*/
const rawVal = cookies.get((0, consts_1.versionCookieName)(base_path_1.default));
const version = parseInt(rawVal);
winston.debug(`version check version=${version}, minVersion=${minVersion}`);
if (isNaN(version) || version < minVersion) {
if (res != null) {
// status code 4xx to indicate this is a client problem and not
// 5xx, a server problem
// 426 means "upgrade required"
res.writeHead(426, { "Content-Type": "text/html" });
res.end(`426 (UPGRADE REQUIRED): reload CoCalc tab or restart your browser -- version=${version} < minVersion=${minVersion}`);
}
return true;
}
else {
return false;
}
}
exports.versionCheckFails = versionCheckFails;
//# sourceMappingURL=version.js.map