UNPKG

vulcain-corejs

Version:
126 lines (124 loc) 5.32 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const configurationSource_1 = require("./configurationSource"); const system_1 = require("./../globals/system"); const fs = require("fs"); const readline = require("readline"); class FileConfigurationSource { constructor(path, mode = configurationSource_1.ConfigurationDataType.Json) { this.path = path; this.mode = mode; this._values = new Map(); try { if (!fs.existsSync(path)) { system_1.System.log.info(null, "CONFIGURATIONS : File " + path + " doesn't exist."); this._disabled = true; } } catch (e) { system_1.System.log.error(null, e, "Invalid path when reading file configuration source at " + path + ". Are you using an unmounted docker volume ?"); } } readValuesFromFile() { return new Promise((resolve) => { fs.stat(this.path, (err, stats) => __awaiter(this, void 0, void 0, function* () { if (!err) { if (!this._lastAccess || this._lastAccess < stats.mtime.getTime()) { this._lastAccess = stats.mtime.getTime(); if (this.mode === configurationSource_1.ConfigurationDataType.Json) yield this.readJsonValues(); else yield this.readKeyValues(); } } resolve(true); })); }); } readJsonValues() { return new Promise((resolve) => { fs.readFile(this.path, "utf-8", (err, data) => { if (!err) { try { let obj = JSON.parse(data); 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._values.set(key, { key, value: val, encrypted }); } resolve(true); return; } catch (e) { err = e; } } system_1.System.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, '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, ''); if (val && val[0] === "!") { val = val.substr(1); encrypted = true; } self._values.set(m[1], { value: val, key: m[1], encrypted }); } } catch (err) { system_1.System.log.error(null, err, `File configuration source - Error when reading key values line ${line}`); } } }); rl.on('close', () => resolve(true)); } catch (err) { system_1.System.log.error(null, err, "File configuration source - Error when reading key values"); resolve(false); } }); } pollPropertiesAsync(timeoutInMs) { return __awaiter(this, void 0, void 0, function* () { try { if (!this._disabled) yield this.readValuesFromFile(); const values = this._values; if (this._values.size > 0) this._values = new Map(); return new configurationSource_1.PollResult(this, values); } catch (e) { return null; } }); } } exports.FileConfigurationSource = FileConfigurationSource; //# sourceMappingURL=fileConfigurationSource.js.map