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

613 lines (607 loc) 21.2 kB
import { EventEmitter } from 'events'; import { Logger } from '../../../shared/logger/Logger.js'; import '../serializer/safe-serializer.js'; import './ultra-fast-allocator.js'; import { FortifiedUtils } from '../utils/utils.js'; /** * XyPrissSecurity - Ultra-Fast Execution Engine (Production Optimized) * Real-world performance optimizations for high-throughput applications */ class UltraFastEngine extends EventEmitter { constructor(options) { super(); this.workerPool = []; this.taskQueue = []; this.workerInitialized = false; this.performanceCounters = { totalExecutions: 0, cacheHits: 0, cacheMisses: 0, workerExecutions: 0, optimizedExecutions: 0, avgExecutionTime: 0, totalExecutionTime: 0, }; // Memory pool for frequent allocations** this.memoryPool = { arrays: new Map(), buffers: new Map(), }; /** * Memoization system** */ this.memoCache = new Map(); this.MEMO_TTL = 5 * 60 * 1000; // 5 minutes // Merge options with ultra-fast defaults** this.options = { ...options, // Override with ultra-fast optimizations (only if not explicitly set) enableJIT: options.enableJIT ?? true, enableSIMD: options.enableSIMD ?? true, enableZeroCopy: options.enableZeroCopy ?? true, enableWebAssembly: options.enableWebAssembly ?? false, // Disabled for stability jitThreshold: options.jitThreshold ?? 3, simdThreshold: options.simdThreshold ?? 8, }; this.util = new FortifiedUtils(); this.logger = new Logger({ enabled: true, level: "info", components: { server: true, cache: true, cluster: true, performance: true, fileWatcher: true, plugins: true, security: true, monitoring: true, routes: true, userApp: true, typescript: true, console: true, other: true, router: true, middleware: true, }, }); this.initializeEngine(); } /** * Initialize optimized execution engine** */ async initializeEngine() { // Initialize worker pool for CPU-intensive tasks if (typeof Worker !== "undefined" && this.options.enableWebAssembly) { await this.initializeWorkerPool(); } // Initialize memory pools this.initializeMemoryPools(); // Setup cache cleanup interval setInterval(() => this.cleanupCaches(), 60000); // Every minute this.emit("ready"); } /** * Initialize worker pool for parallel processing** */ async initializeWorkerPool() { const workerCount = Math.min(navigator.hardwareConcurrency || 4, 8); try { for (let i = 0; i < workerCount; i++) { const workerCode = this.generateWorkerCode(); const blob = new Blob([workerCode], { type: "application/javascript", }); const worker = new Worker(URL.createObjectURL(blob)); worker.onmessage = (e) => this.handleWorkerMessage(e); worker.onerror = (e) => console.warn("Worker error:", e); this.workerPool.push(worker); } this.workerInitialized = true; this.logger.debug("other", `Worker pool initialized with ${workerCount} workers`); } catch (error) { this.logger.debug("other", "Worker pool initialization failed:", error); this.workerInitialized = false; } } /** * Generate optimized worker code** */ generateWorkerCode() { return ` self.onmessage = function(e) { const { id, fnString, args } = e.data; try { // Create function from string const fn = new Function('return ' + fnString)(); // Execute with high precision timing const startTime = performance.now(); const result = fn.apply(null, args); const executionTime = performance.now() - startTime; self.postMessage({ id, success: true, result, executionTime }); } catch (error) { self.postMessage({ id, success: false, error: error.message }); } }; `; } /** * Handle worker messages** */ handleWorkerMessage(e) { const { id, success, result, error, executionTime } = e.data; const taskIndex = this.taskQueue.findIndex((task) => task.id === id); if (taskIndex !== -1) { const task = this.taskQueue.splice(taskIndex, 1)[0]; if (success) { task.resolve(result); this.performanceCounters.workerExecutions++; } else { task.reject(new Error(error)); } } } /** * Initialize memory pools for efficient allocation** */ initializeMemoryPools() { // Pre-allocate common array sizes const commonSizes = [8, 16, 32, 64, 128, 256, 512, 1024]; for (const size of commonSizes) { this.memoryPool.arrays.set(size, []); this.memoryPool.buffers.set(size, []); // Pre-allocate a few instances for (let i = 0; i < 3; i++) { this.memoryPool.arrays.get(size).push(new Float32Array(size)); this.memoryPool.buffers .get(size) .push(new ArrayBuffer(size * 4)); } } } /** * Lightning-fast execution with real optimizations** */ async executeLightning(fn, args, fnName = fn.name || "anonymous") { const startTime = performance.now(); this.performanceCounters.totalExecutions++; try { // Function cache with LRU** const cached = this.getCachedFunction(fnName, fn); if (cached) { this.performanceCounters.cacheHits++; const result = await cached(...args); this.updatePerformanceStats(fnName, startTime); return result; } // Parallel execution for CPU-intensive tasks** if (this.shouldUseWorker(fn, args)) { const result = await this.executeInWorker(fn, args); this.updatePerformanceStats(fnName, startTime); return result; } // Vectorized operations for arrays** if (this.shouldUseVectorization(args)) { const result = this.executeVectorized(fn, args); if (result !== null) { this.performanceCounters.optimizedExecutions++; this.updatePerformanceStats(fnName, startTime); return result; } } // Memoization for pure functions** if (this.isPureFunction(fn)) { const memoKey = this.generateMemoKey(fnName, args); const memoized = this.getMemoized(memoKey); if (memoized !== undefined) { this.updatePerformanceStats(fnName, startTime); return memoized; } const result = await fn(...args); this.setMemoized(memoKey, result); this.cacheFunction(fnName, fn); this.updatePerformanceStats(fnName, startTime); return result; } // **FALLBACK: Direct execution with optimization tracking** const result = await fn(...args); this.cacheFunction(fnName, fn); this.updatePerformanceStats(fnName, startTime); return result; } catch (error) { this.emit("error", { error, fnName, args }); throw error; } } /** * Intelligent function caching** */ getCachedFunction(fnName, fn) { const cached = this.util.FUNCTION_CACHE.get(fnName); if (cached) { cached.hitCount++; cached.lastUsed = Date.now(); return cached.fn; } this.performanceCounters.cacheMisses++; return null; } /** * Cache function for future use** */ cacheFunction(fnName, fn) { if (this.util.FUNCTION_CACHE.size >= this.util.CACHE_SIZE_LIMIT) { this.evictLeastUsedCache(); } this.util.FUNCTION_CACHE.set(fnName, { fn: fn, hitCount: 1, lastUsed: Date.now(), avgExecutionTime: 0, }); const stats = this.util.EXECUTION_STATS.get(fnName); if (stats && stats.count >= this.options.jitThreshold) { this.util.HOT_FUNCTIONS.add(fnName); } } /** * LRU cache eviction** */ evictLeastUsedCache() { let oldestTime = Date.now(); let oldestKey = ""; for (const [key, cached] of this.util.FUNCTION_CACHE.entries()) { if (cached.lastUsed < oldestTime) { oldestTime = cached.lastUsed; oldestKey = key; } } if (oldestKey) { this.util.FUNCTION_CACHE.delete(oldestKey); this.util.HOT_FUNCTIONS.delete(oldestKey); } } /** * Determine if function should use worker** */ shouldUseWorker(fn, args) { if (!this.workerInitialized) return false; const fnString = fn.toString(); const complexity = this.estimateComplexity(fnString, args); // Use worker for computationally intensive tasks return (complexity > 1000 || (fnString.includes("for") && fnString.length > 200) || args.some((arg) => Array.isArray(arg) && arg.length > 1000)); } /** * Execute function in worker pool** */ async executeInWorker(fn, args) { return new Promise((resolve, reject) => { const taskId = `task_${Date.now()}_${Math.random()}`; const task = { id: taskId, fn: fn.toString(), args, resolve, reject, }; this.taskQueue.push(task); // Find available worker const worker = this.workerPool[this.taskQueue.length % this.workerPool.length]; worker.postMessage({ id: taskId, fnString: fn.toString(), args, }); // Timeout protection setTimeout(() => { const index = this.taskQueue.findIndex((t) => t.id === taskId); if (index !== -1) { this.taskQueue.splice(index, 1); reject(new Error("Worker execution timeout")); } }, 30000); // 30 second timeout }); } /** * Estimate function complexity** */ estimateComplexity(fnString, args) { let complexity = fnString.length; // Add complexity for loops const loops = (fnString.match(/for\s*\(/g) || []).length + (fnString.match(/while\s*\(/g) || []).length; complexity += loops * 100; // Add complexity for array operations const arrayOps = (fnString.match(/\.map\(|\.filter\(|\.reduce\(/g) || []).length; complexity += arrayOps * 50; // Add complexity based on argument sizes for (const arg of args) { if (Array.isArray(arg)) { complexity += arg.length * 0.1; } } return complexity; } /** * Check if vectorization should be used** */ shouldUseVectorization(args) { return args.some((arg) => Array.isArray(arg) && arg.length >= this.options.simdThreshold && arg.every((item) => typeof item === "number")); } /** * Execute with vectorization** */ executeVectorized(fn, args) { const fnString = fn.toString(); // Find numeric arrays const numericArrays = args.filter((arg) => Array.isArray(arg) && arg.every((item) => typeof item === "number")); if (numericArrays.length === 0) return null; try { // Vector addition if (fnString.includes("+") && numericArrays.length >= 2) { const result = this.vectorAdd(numericArrays[0], numericArrays[1]); return result; } // Vector multiplication if (fnString.includes("*") && numericArrays.length >= 2) { const result = this.vectorMultiply(numericArrays[0], numericArrays[1]); return result; } // Vector sum/reduce if (fnString.includes("reduce") || fnString.includes("sum")) { const result = this.vectorSum(numericArrays[0]); return result; } // Vector map operations if (fnString.includes("map")) { const result = this.vectorMap(numericArrays[0], fn); return result; } } catch (error) { console.warn("Vectorization failed:", error); } return null; } /** * Optimized vector operations** */ vectorAdd(a, b) { const length = Math.min(a.length, b.length); const result = new Array(length); // Unrolled loop for better performance let i = 0; for (; i < length - 3; i += 4) { result[i] = a[i] + b[i]; result[i + 1] = a[i + 1] + b[i + 1]; result[i + 2] = a[i + 2] + b[i + 2]; result[i + 3] = a[i + 3] + b[i + 3]; } // Handle remaining elements for (; i < length; i++) { result[i] = a[i] + b[i]; } return result; } vectorMultiply(a, b) { const length = Math.min(a.length, b.length); const result = new Array(length); let i = 0; for (; i < length - 3; i += 4) { result[i] = a[i] * b[i]; result[i + 1] = a[i + 1] * b[i + 1]; result[i + 2] = a[i + 2] * b[i + 2]; result[i + 3] = a[i + 3] * b[i + 3]; } for (; i < length; i++) { result[i] = a[i] * b[i]; } return result; } vectorSum(arr) { let sum = 0; let i = 0; // Unrolled loop for better performance for (; i < arr.length - 3; i += 4) { sum += arr[i] + arr[i + 1] + arr[i + 2] + arr[i + 3]; } for (; i < arr.length; i++) { sum += arr[i]; } return sum; } vectorMap(arr, mapFn) { const result = new Array(arr.length); for (let i = 0; i < arr.length; i++) { result[i] = mapFn(arr[i]); } return result; } /** * Pure function detection** */ isPureFunction(fn) { const fnString = fn.toString(); // Simple heuristics for pure function detection const impurePatterns = [ "console.", "Math.random", "Date.", "fetch", "localStorage", "sessionStorage", "document.", "window.", "global.", "process.env", ]; return !impurePatterns.some((pattern) => fnString.includes(pattern)); } generateMemoKey(fnName, args) { return `${fnName}_${JSON.stringify(args)}`; } getMemoized(key) { const cached = this.memoCache.get(key); if (cached && Date.now() - cached.timestamp < this.MEMO_TTL) { return cached.value; } if (cached) { this.memoCache.delete(key); } return undefined; } setMemoized(key, value) { if (this.memoCache.size > 500) { // Clear old entries const cutoff = Date.now() - this.MEMO_TTL; for (const [k, v] of this.memoCache.entries()) { if (v.timestamp < cutoff) { this.memoCache.delete(k); } } } this.memoCache.set(key, { value, timestamp: Date.now() }); } /** * Update performance statistics** */ updatePerformanceStats(fnName, startTime) { const executionTime = performance.now() - startTime; this.performanceCounters.totalExecutionTime += executionTime; this.performanceCounters.avgExecutionTime = this.performanceCounters.totalExecutionTime / this.performanceCounters.totalExecutions; let stats = this.util.EXECUTION_STATS.get(fnName); if (!stats) { stats = { count: 0, totalTime: 0, avgTime: 0, minTime: Infinity, maxTime: 0, }; this.util.EXECUTION_STATS.set(fnName, stats); } stats.count++; stats.totalTime += executionTime; stats.avgTime = stats.totalTime / stats.count; stats.minTime = Math.min(stats.minTime, executionTime); stats.maxTime = Math.max(stats.maxTime, executionTime); } /** * Cache cleanup** */ cleanupCaches() { const now = Date.now(); const maxAge = 10 * 60 * 1000; // 10 minutes // Cleanup function cache for (const [key, cached] of this.util.FUNCTION_CACHE.entries()) { if (now - cached.lastUsed > maxAge && cached.hitCount < 5) { this.util.FUNCTION_CACHE.delete(key); this.util.HOT_FUNCTIONS.delete(key); } } // Cleanup memoization cache const cutoff = now - this.MEMO_TTL; for (const [key, cached] of this.memoCache.entries()) { if (cached.timestamp < cutoff) { this.memoCache.delete(key); } } // Return unused arrays to pool for (const [size, pool] of this.memoryPool.arrays.entries()) { if (pool.length > 5) { this.memoryPool.arrays.set(size, pool.slice(0, 5)); } } } /** * Get comprehensive performance statistics** */ getPerformanceStats() { const hotFunctionStats = Array.from(this.util.HOT_FUNCTIONS).map((name) => ({ name, stats: this.util.EXECUTION_STATS.get(name), })); return { ...this.performanceCounters, cacheSize: this.util.FUNCTION_CACHE.size, memoSize: this.memoCache.size, hotFunctions: hotFunctionStats, workerPoolSize: this.workerPool.length, workerInitialized: this.workerInitialized, memoryPoolStats: { arrayPools: Array.from(this.memoryPool.arrays.entries()).map(([size, pool]) => ({ size, count: pool.length, })), bufferPools: Array.from(this.memoryPool.buffers.entries()).map(([size, pool]) => ({ size, count: pool.length, })), }, optimizationLevel: this.getOptimizationLevel(), }; } /** * Get current optimization level** */ getOptimizationLevel() { let score = 0; if (this.util.FUNCTION_CACHE.size > 0) score += 25; if (this.workerInitialized) score += 25; if (this.util.HOT_FUNCTIONS.size > 0) score += 25; if (this.memoCache.size > 0) score += 25; if (score >= 90) return "Maximum"; if (score >= 70) return "High"; if (score >= 50) return "Medium"; if (score >= 25) return "Low"; return "Basic"; } /** * Cleanup and destroy** */ destroy() { // Terminate workers this.workerPool.forEach((worker) => worker.terminate()); this.workerPool.length = 0; // Clear all caches this.util.FUNCTION_CACHE.clear(); this.util.EXECUTION_STATS.clear(); this.util.HOT_FUNCTIONS.clear(); this.memoCache.clear(); // Clear memory pools this.memoryPool.arrays.clear(); this.memoryPool.buffers.clear(); this.emit("destroyed"); } } export { UltraFastEngine }; //# sourceMappingURL=ultra-fast-engine.js.map