UNPKG

@webfaas/webfaas-core

Version:

WebFaaS Framework - Core

131 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = void 0; const Log_1 = require("../Log/Log"); const ILog_1 = require("../Log/ILog"); const valueEnvironmentRegex = /\${[a-zA-Z0-9_:]*}/g; /** * Configuration. Automatic search file config and load */ class Config { /** * * @param fileOrObject file string or object */ constructor(log) { /** * process object config */ this.processConfig = (node, prefix) => { let keys = Object.keys(node); for (var i = 0; i < keys.length; i++) { try { let fullKey; let key; let item; let keyEnv; let envValue; key = keys[i]; item = this.parseValue(node[key]); node[key] = item; if (prefix) { fullKey = prefix + "." + key; } else { fullKey = key; } this.configByKey[fullKey] = item; if (typeof (item) === "object") { this.processConfig(item, fullKey); } else { if (typeof (item) !== "string") { this.configByKey[fullKey] = item; } } } catch (errTry) { this.log.writeError("processConfig", errTry, null, __filename); } } }; this.originalConfigObj = null; this.configByKey = {}; this.log = log || new Log_1.Log(); } /** * parse value. ex: environment variables */ parseValue(value) { if (typeof (value) === "object") { return value; } else if (typeof (value) === "string") { let valueText = value.toString(); let selfLog = this.log; let valueTextParsed = valueText.replace(valueEnvironmentRegex, function (matchText, ...args) { let keyEnvArray = matchText.substring(2, matchText.length - 1).split(":"); let keyEnv = keyEnvArray[0]; let keyEnvDefaultValue = keyEnvArray[1] || ""; let envValue = process.env[keyEnv]; if (envValue) { selfLog.write(ILog_1.LogLevelEnum.TRACE, "parseValue", ILog_1.LogCodeEnum.PROCESS.toString(), "keyEnv: " + keyEnv + ", envValue: " + envValue, null, __filename); return envValue; } else { return keyEnvDefaultValue; } }); return valueTextParsed; } else { return value; } } /** * Read config from file or object * @param fileOrObject file string or object */ read(fileOrObject) { this.originalConfigObj = null; //clean if (fileOrObject) { if (typeof (fileOrObject) === "object") { this.originalConfigObj = fileOrObject; } else { let filePath = fileOrObject; try { this.originalConfigObj = require(filePath); this.log.write(ILog_1.LogLevelEnum.INFO, "read", ILog_1.LogCodeEnum.OPENFILE.toString(), "config file [" + filePath + "] loaded", null, __filename); } catch (errTry) { this.log.writeError("read", errTry, { file: filePath }, __filename); } } if (this.originalConfigObj) { this.configByKey = {}; //clean this.processConfig(this.originalConfigObj, null); this.log.write(ILog_1.LogLevelEnum.DEBUG, "read", ILog_1.LogCodeEnum.PROCESS.toString(), "config object processed", null, __filename); } else { this.log.write(ILog_1.LogLevelEnum.DEBUG, "read", ILog_1.LogCodeEnum.PROCESS.toString(), "config object not processed", null, __filename); } } } /** * Return value from key * @param key key value * @param defaultValue default value */ get(key, defaultValue) { let value = this.configByKey[key]; if (value === undefined) { return defaultValue || null; } else { return value; } } } exports.Config = Config; //# sourceMappingURL=Config.js.map