UNPKG

kist

Version:

Lightweight Package Pipeline Processor with Plugin Architecture

91 lines 3.22 kB
import { AbstractProcess } from "../abstract/AbstractProcess.js"; import { defaultConfig } from "./defaultConfig.js"; export class ConfigStore extends AbstractProcess { constructor() { super(); this.config = defaultConfig; this.logDebug("ConfigStore initialized with default configuration."); } static getInstance() { if (!ConfigStore.instance) { ConfigStore.instance = new ConfigStore(); } return ConfigStore.instance; } get(key) { const keys = key.split("."); let current = this.config; for (const k of keys) { if (current[k] === undefined) { return undefined; } current = current[k]; } this.logDebug(`Configuration key "${key}" retrieved with value: ${JSON.stringify(current)}`); return current; } set(key, value) { const keys = key.split("."); let current = this.config; for (let i = 0; i < keys.length - 1; i++) { const k = keys[i]; if (k === "__proto__" || k === "constructor" || k === "prototype") { this.logWarn(`Attempted to set protected key "${k}". This operation is blocked for security reasons.`); return; } if (!current[k] || typeof current[k] !== "object") { current[k] = {}; } current = current[k]; } const finalKey = keys[keys.length - 1]; if (finalKey === "__proto__" || finalKey === "constructor" || finalKey === "prototype") { this.logWarn(`Attempted to set protected key "${finalKey}". This operation is blocked for security reasons.`); return; } current[finalKey] = value; this.logDebug(`Set configuration key "${key}" to: ${JSON.stringify(value)}`); } merge(newConfig) { this.config = this.deepMerge(this.config, newConfig); this.logDebug("Configuration successfully merged."); } getConfig() { return this.config; } print() { console.log("Current Configuration:"); console.log(JSON.stringify(this.config, null, 2)); } deepMerge(target, source) { if (typeof target !== "object" || target === null) { return source; } for (const key of Object.keys(source)) { if (key === "__proto__" || key === "constructor" || key === "prototype") { this.logWarn(`Skipping potentially unsafe key: "${key}"`); continue; } if (source[key] && typeof source[key] === "object" && !Array.isArray(source[key])) { if (!target[key] || typeof target[key] !== "object") { target[key] = {}; } target[key] = this.deepMerge(target[key], source[key]); } else { target[key] = source[key]; } } return target; } } ConfigStore.instance = null; //# sourceMappingURL=ConfigStore%20copy.js.map