UNPKG

@cocalc/server

Version:

CoCalc server functionality: functions used by either the hub and the next.js server

84 lines 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProject = exports.COCALC_MODES = void 0; const database_1 = require("@cocalc/database"); const logger_1 = __importDefault(require("@cocalc/backend/logger")); const connection_1 = __importDefault(require("@cocalc/server/projects/connection")); const single_user_1 = __importDefault(require("./single-user")); const multi_user_1 = __importDefault(require("./multi-user")); const kucalc_1 = __importDefault(require("./kucalc")); const kubernetes_1 = __importDefault(require("./kubernetes")); exports.COCALC_MODES = [ "single-user", "multi-user", "kucalc", "kubernetes", ]; // NOTE: you can't *change* the mode -- caching just caches what you first set. let cached = undefined; function init(mode) { const winston = (0, logger_1.default)("project-control"); winston.debug("init", mode); if (!mode) { mode = process.env.COCALC_MODE; } if (cached !== undefined) { winston.info("using cached project control client"); return cached; } if (!mode) { throw Error("you can only call projects/control with no mode argument AFTER it has been initialized by the hub or if you set the COCALC_MODE env var"); } winston.info("creating project control client"); let getProject; switch (mode) { case "single-user": getProject = single_user_1.default; break; case "multi-user": getProject = multi_user_1.default; break; case "kucalc": getProject = kucalc_1.default; break; case "kubernetes": getProject = kubernetes_1.default; break; default: throw Error(`invalid mode "${mode}"`); } winston.info(`project controller created with mode ${mode}`); const database = (0, database_1.db)(); database.compute_server = getProject; // This is used by the database when handling certain writes to make sure // that the there is a connection to the corresponding project, so that // the project can respond. database.ensure_connection_to_project = async (project_id, cb) => { winston.debug("ensure_connection_to_project --", project_id); try { await (0, connection_1.default)(project_id); cb?.(); } catch (err) { winston.debug("WARNING: unable to make a connection to", project_id, err); cb?.(err); } }; cached = getProject; return getProject; } exports.default = init; const getProject = (project_id) => { if (cached == null) { if (process.env["COCALC_MODE"]) { return init(process.env["COCALC_MODE"])(project_id); } throw Error("must call init first or set the environment variable COCALC_MODE"); } return cached(project_id); }; exports.getProject = getProject; //# sourceMappingURL=index.js.map