UNPKG

vulcain-corejs

Version:
130 lines 5.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const abstractions_1 = require("../abstractions"); const fs = require("fs"); const readline = require("readline"); const system_1 = require("../../globals/system"); var ConfigurationDataType; (function (ConfigurationDataType) { ConfigurationDataType[ConfigurationDataType["KeyValue"] = 0] = "KeyValue"; ConfigurationDataType[ConfigurationDataType["Json"] = 1] = "Json"; ConfigurationDataType[ConfigurationDataType["VulcainConfig"] = 2] = "VulcainConfig"; })(ConfigurationDataType = exports.ConfigurationDataType || (exports.ConfigurationDataType = {})); class FileConfigurationSource { constructor(path, mode = ConfigurationDataType.Json) { this.path = path; this.mode = mode; this._values = new Map(); this._disabled = false; if (!path) { this._disabled = true; return; } try { if (!fs.existsSync(this.path)) { system_1.Service.log.info(null, () => "CONFIGURATIONS : File " + path + " doesn't exist."); this._disabled = true; } } catch (e) { system_1.Service.log.error(null, e, () => "Invalid path when reading file configuration source at " + this.path + ". Are you using an unmounted docker volume ?"); } } get(name) { let item = this._values.get(name); if (item) return item.value; return undefined; } readJsonValues(vulcainConfig) { return new Promise((resolve) => { fs.readFile(this.path, "utf-8", (err, data) => { if (!err) { try { let obj = data && JSON.parse(data); obj = obj && vulcainConfig ? obj.config : obj; if (obj) { for (let key of Object.keys(obj)) { let encrypted = false; let val = obj[key]; if (typeof val === "object") { val = val.value; encrypted = val.encrypted; } this.updateValue(key, val, encrypted); } } resolve(true); return; } catch (e) { err = e; } } system_1.Service.log.error(null, err, () => "File configuration source - Error when reading json values"); resolve(false); }); }); } readKeyValues() { const re = /^\s*([\w\$_][\d\w\._\-\$]*)\s*=\s*(.*)/; let self = this; return new Promise((resolve) => { try { const rl = readline.createInterface({ input: fs.createReadStream(self.path, { encoding: 'UTF-8' }) }); rl.on('line', function (line) { if (line) { try { let m = line.match(re); if (m) { let encrypted = false; let val = (m[2] && m[2].trim().replace(/^"|"$/g, '')) || null; if (val && val[0] === "!") { val = val.substr(1); encrypted = true; } self.updateValue(m[1], val, encrypted); } } catch (err) { system_1.Service.log.error(null, err, () => `File configuration source - Error when reading key values line ${line}`); } } }); rl.on('close', () => resolve(true)); } catch (err) { system_1.Service.log.error(null, err, () => "File configuration source - Error when reading key values"); resolve(false); } }); } updateValue(name, value, encrypted) { this._values.set(name, { value: encrypted ? system_1.Service.decrypt(value) : value, encrypted, key: name }); let v = encrypted ? "********" : value; system_1.Service.log.info(null, () => `CONFIG: Setting property value '${v}' for key ${name}`); } readProperties() { if (this._disabled) return; return new Promise((resolve) => { try { fs.stat(this.path, async (err, stats) => { if (!err) { if (this.mode === ConfigurationDataType.KeyValue) await this.readKeyValues(); else await this.readJsonValues(this.mode === ConfigurationDataType.VulcainConfig); } resolve(new abstractions_1.DataSource(this._values.values())); }); } catch (ex) { } }); } } exports.FileConfigurationSource = FileConfigurationSource; //# sourceMappingURL=fileConfigurationSource.js.map