UNPKG

xypriss-security

Version:

XyPriss Security is an advanced JavaScript security library designed for enterprise applications. It provides military-grade encryption, secure data structures, quantum-resistant cryptography, and comprehensive security utilities for modern web applicatio

352 lines (349 loc) 11 kB
import { fortifiedLogger } from './fortified-logger.js'; /** * XyPrissSecurity - Fortified Function Configuration Management * Centralized configuration system for optimal performance settings */ /** * Performance profile configurations */ const PERFORMANCE_PROFILES = { minimal: { ultraFast: "minimal", autoEncrypt: false, parameterValidation: false, stackTraceProtection: false, smartSecurity: false, threatDetection: false, memoize: true, smartCaching: false, predictiveAnalytics: false, anomalyDetection: false, auditLog: false, performanceTracking: false, debugMode: false, detailedMetrics: false, enableJIT: false, enableSIMD: false, enableWebAssembly: false, memoryOptimization: "none", }, standard: { ultraFast: "standard", autoEncrypt: false, parameterValidation: false, stackTraceProtection: false, smartSecurity: false, threatDetection: false, memoize: true, smartCaching: true, predictiveAnalytics: false, anomalyDetection: false, auditLog: false, performanceTracking: true, debugMode: false, detailedMetrics: false, enableJIT: true, enableSIMD: true, enableWebAssembly: false, memoryOptimization: "standard", }, maximum: { ultraFast: "maximum", autoEncrypt: false, parameterValidation: false, stackTraceProtection: false, smartSecurity: false, threatDetection: false, memoize: true, smartCaching: true, predictiveAnalytics: true, anomalyDetection: false, auditLog: false, performanceTracking: true, debugMode: false, detailedMetrics: false, enableJIT: true, enableSIMD: true, enableWebAssembly: true, memoryOptimization: "aggressive", enableVectorization: true, enableParallelExecution: true, enableZeroCopy: true, enableNativeOptimizations: true, }, secure: { ultraFast: false, autoEncrypt: true, parameterValidation: true, stackTraceProtection: true, smartSecurity: true, threatDetection: true, memoize: true, smartCaching: true, predictiveAnalytics: false, anomalyDetection: true, auditLog: true, performanceTracking: true, debugMode: false, detailedMetrics: true, enableJIT: false, enableSIMD: false, enableWebAssembly: false, memoryOptimization: "standard", }, development: { ultraFast: false, autoEncrypt: false, parameterValidation: true, stackTraceProtection: false, smartSecurity: false, threatDetection: false, memoize: true, smartCaching: true, predictiveAnalytics: true, anomalyDetection: true, auditLog: true, performanceTracking: true, debugMode: true, detailedMetrics: true, enableJIT: false, enableSIMD: false, enableWebAssembly: false, memoryOptimization: "none", }, }; /** * Default configuration values */ const DEFAULT_CONFIG = { // Performance mode ultraFast: "maximum", // Security options autoEncrypt: false, secureParameters: [], parameterValidation: false, memoryWipeDelay: 0, stackTraceProtection: false, smartSecurity: false, threatDetection: false, // Performance options memoize: true, timeout: 5000, retries: 1, maxRetryDelay: 1000, smartCaching: true, cacheStrategy: "adaptive", cacheTTL: 600000, maxCacheSize: 10000, errorHandling: "graceful", precompile: true, optimizeExecution: true, // Extreme performance options enableJIT: true, enableSIMD: true, enableWebAssembly: true, memoryOptimization: "aggressive", enableVectorization: true, enableParallelExecution: true, enableZeroCopy: true, enableNativeOptimizations: true, jitThreshold: 5, simdThreshold: 4, // Smart actions autoTuning: true, predictiveAnalytics: true, adaptiveTimeout: true, intelligentRetry: true, anomalyDetection: false, performanceRegression: false, // Monitoring options auditLog: false, performanceTracking: true, debugMode: false, detailedMetrics: false, // Memory management memoryPool: true, maxMemoryUsage: 500 * 1024 * 1024, autoCleanup: true, smartMemoryManagement: true, memoryPressureHandling: true, }; /** * Configuration manager for fortified functions */ class FortifiedConfig { constructor() { this.profileOverrides = new Map(); this.globalConfig = { ...DEFAULT_CONFIG }; } static getInstance() { if (!FortifiedConfig.instance) { FortifiedConfig.instance = new FortifiedConfig(); } return FortifiedConfig.instance; } /** * Apply a performance profile */ applyProfile(profile) { const profileConfig = PERFORMANCE_PROFILES[profile]; this.globalConfig = { ...this.globalConfig, ...profileConfig }; fortifiedLogger.info("CONFIG", `Applied performance profile: ${profile}`, { profile, appliedSettings: Object.keys(profileConfig), }); } /** * Update global configuration */ updateGlobalConfig(options) { const previousConfig = { ...this.globalConfig }; this.globalConfig = { ...this.globalConfig, ...options }; fortifiedLogger.info("CONFIG", "Global configuration updated", { changedKeys: Object.keys(options), previousConfig: this.getChangedValues(previousConfig, options), newConfig: this.getChangedValues(this.globalConfig, options), }); } /** * Get current global configuration */ getGlobalConfig() { return { ...this.globalConfig }; } /** * Create configuration for a specific function */ createFunctionConfig(functionId, options = {}) { const profileOverride = this.profileOverrides.get(functionId) || {}; const finalConfig = { ...this.globalConfig, ...profileOverride, ...options, }; fortifiedLogger.debug("CONFIG", `Configuration created for function: ${functionId}`, { functionId, hasProfileOverride: this.profileOverrides.has(functionId), hasCustomOptions: Object.keys(options).length > 0, }); return finalConfig; } /** * Set profile override for a specific function */ setFunctionProfile(functionId, profile) { const profileConfig = PERFORMANCE_PROFILES[profile]; this.profileOverrides.set(functionId, profileConfig); fortifiedLogger.info("CONFIG", `Profile override set for function: ${functionId}`, { functionId, profile, overrideKeys: Object.keys(profileConfig), }); } /** * Remove profile override for a specific function */ removeFunctionProfile(functionId) { const removed = this.profileOverrides.delete(functionId); if (removed) { fortifiedLogger.info("CONFIG", `Profile override removed for function: ${functionId}`, { functionId, }); } } /** * Get recommended configuration based on environment */ getRecommendedConfig() { // Detect environment const isProduction = process.env.NODE_ENV === "production"; const isDevelopment = process.env.NODE_ENV === "development"; const isTest = process.env.NODE_ENV === "test"; let profile; let reasoning; if (isProduction) { profile = "maximum"; reasoning = "Production environment detected - using maximum performance profile"; } else if (isDevelopment) { profile = "development"; reasoning = "Development environment detected - using development profile with debugging"; } else if (isTest) { profile = "minimal"; reasoning = "Test environment detected - using minimal profile for fast tests"; } else { profile = "standard"; reasoning = "Unknown environment - using standard profile"; } const config = { ...this.globalConfig, ...PERFORMANCE_PROFILES[profile], }; return { profile, config, reasoning }; } /** * Validate configuration */ validateConfig(config) { const errors = []; const warnings = []; // Validate timeout if (config.timeout !== undefined && config.timeout < 1000) { warnings.push("Timeout less than 1 second may cause premature timeouts"); } // Validate cache size if (config.maxCacheSize !== undefined && config.maxCacheSize > 50000) { warnings.push("Large cache size may impact memory usage"); } // Validate memory usage if (config.maxMemoryUsage !== undefined && config.maxMemoryUsage > 1024 * 1024 * 1024) { warnings.push("Memory limit over 1GB may impact system performance"); } // Validate JIT threshold if (config.jitThreshold !== undefined && config.jitThreshold < 2) { warnings.push("Very low JIT threshold may cause compilation overhead"); } // Check for conflicting options if (config.ultraFast === "minimal" && config.detailedMetrics === true) { warnings.push("Detailed metrics may reduce performance in minimal mode"); } if (config.autoEncrypt === true && config.ultraFast === "maximum") { warnings.push("Encryption may reduce performance in maximum speed mode"); } return { valid: errors.length === 0, errors, warnings, }; } /** * Get changed values between two configurations */ getChangedValues(config, changes) { const result = {}; for (const key of Object.keys(changes)) { if (config[key] !== undefined) { result[key] = config[key]; } } return result; } /** * Reset to default configuration */ reset() { this.globalConfig = { ...DEFAULT_CONFIG }; this.profileOverrides.clear(); fortifiedLogger.info("CONFIG", "Configuration reset to defaults"); } } // Export singleton instance const fortifiedConfig = FortifiedConfig.getInstance(); export { DEFAULT_CONFIG, FortifiedConfig, PERFORMANCE_PROFILES, fortifiedConfig }; //# sourceMappingURL=fortified-config.js.map