UNPKG

@cocalc/server

Version:

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

73 lines 2.93 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSoftwareEnvironments = void 0; // This is used by the hub to adjust the "customization" variable for the user visible site, and also by manage in on-prem, to actually get the associated configuration const logger_1 = __importDefault(require("@cocalc/backend/logger")); const sanitize_software_envs_1 = require("@cocalc/util/sanitize-software-envs"); const fs_1 = require("fs"); const promises_1 = require("fs/promises"); const path_1 = require("path"); const logger = (0, logger_1.default)("hub:webapp-config"); const L = logger.debug; const W = logger.warn; const cache = { server: null, webapp: null, }; /** * A configuration for available software environments could be stored at the location of $COCALC_SOFTWARE_ENVIRONMENTS. */ async function getSoftwareEnvironments(purpose) { if (cache[purpose] === null) { cache[purpose] = (await readConfig(purpose)) ?? false; } const data = cache[purpose]; return data === false ? null : data; } exports.getSoftwareEnvironments = getSoftwareEnvironments; async function readConfig(purpose) { const dir = process.env.COCALC_SOFTWARE_ENVIRONMENTS; if (!dir) return null; // Check if a file "software.json" and "registry" exist and are readable at the directory "dir": const softwareFn = (0, path_1.join)(dir, "software.json"); const registryFn = (0, path_1.join)(dir, "registry"); if (!(await isReadable(softwareFn))) { W(`WARNING: $COCALC_SOFTWARE_ENVIRONMENTS is defined but ${softwareFn} does not exist`); return null; } if (!(await isReadable(registryFn))) { W(`WARNING: $COCALC_SOFTWARE_ENVIRONMENTS is defined but ${registryFn} does not exist`); return null; } // read the content of registry and trim it to a one line string const registry = (await (0, promises_1.readFile)(registryFn)).toString().trim(); // parse the content of softwareFn as json try { const software = JSON.parse((await (0, promises_1.readFile)(softwareFn)).toString()); const dbg = (...msg) => L(...msg); const sanitized = (0, sanitize_software_envs_1.sanitizeSoftwareEnv)({ software, registry, purpose }, dbg); return sanitized; } catch (err) { W(`WARNING: ${softwareFn} is not a valid JSON file -- ${err}`); return null; } } async function isReadable(path) { try { await (0, promises_1.access)(path, fs_1.constants.R_OK); } catch (err) { return false; } return true; } //# sourceMappingURL=software-envs.js.map