Path: blob/master/src/packages/backend/misc/env-to-number.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import getLogger from "../logger";67const L = getLogger("env-to-number").debug;89// parse environment variable and convert to integer, with fallback if number could not be parsed10export function envToInt(name: string, fallback: number) {11const value = process.env[name];12if (value == null) {13// L(`envToInt: using fallback value ${fallback} for ${name}`);14return fallback;15}16const parsed = parseInt(value);17if (isNaN(parsed)) {18L(19`envToInt: could not parse ${name}=${value}, using fallback value ${fallback}`20);21return fallback;22}23return parsed;24}2526// parse environment variable and convert to float, with fallback if number could not be parsed27export function envToFloat(name: string, fallback: number) {28const value = process.env[name];29if (value == null) {30L(`envToFloat: using fallback value ${fallback} for ${name}`);31return fallback;32}33const parsed = parseFloat(value);34if (isNaN(parsed)) {35L(36`envToFloat: could not parse ${name}=${value}, using fallback value ${fallback}`37);38return fallback;39}40return parsed;41}424344