XyPriss Configuration API (cfg)
The __cfg__ global API provides a centralized, singleton-based management system for the XyPriss Server Configuration (XPSC). It is designed to resolve initialization order conflicts and provide a consistent interface for accessing and updating server settings across modular architectures.
Architecture Overview
The configuration system operates as a singleton instance within the ConfigurationManager class. It maintains the internal state of the server and ensures that all components—including plugins, middleware, and core services—reference the same data structure.
Core Methods
Retrieval
cfg.get(section)
Retrieves a specific configuration segment. This method is type-safe and returns the current state of the requested section.
const serverPort = __cfg__.get("server").port;
cfg.getAll()
Returns a complete snapshot of the current configuration object.
const fullConfig = __cfg__.getAll();
Modification
cfg.update(section, partialValue)
Performs a deep merge of the provided partial object into the specified configuration section. This method is preferred for runtime updates before the server has fully started.
__cfg__.update("server", {
port: 8080,
host: "0.0.0.0",
});
cfg.merge(partialConfig)
Merges a partial ServerOptions object into the global configuration. This is used during the initialization phase to apply user-defined settings over the defaults.
Immutability Enforcement
A critical feature of the XyPriss configuration system is its transition to a read-only state.
- Initialization: During the call to
createServer(), the configuration is merged and validated. - Locking: Once the server instance is created, the configuration object is passed through the
__const__.$make()engine. - Runtime Protection: After locking, any attempt to use
__cfg__.update()or modify the object properties directly will throw aXyPrissConsterror.
This mechanism prevents "configuration drift" where plugins or rogue modules attempt to alter server behavior after the boot sequence has completed.
Best Practices
- Accessing Values: Always use
__cfg__.get()inside functions or methods rather than caching values in local variables during module load. This ensures you always have the most recent configuration. - Plugin Configuration: Plugins should use the
onRegisterhook to inspect or suggest configuration changes via__cfg__before the server locks the state. - Environment Overrides: Use
__sys__.__ENV__in conjunction with__cfg__.update()to apply environment-specific settings during the bootstrap phase.