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
148 lines (144 loc) • 4.3 kB
JavaScript
'use strict';
var events = require('events');
var utils = require('../../utils/utils.js');
/**
* Memory Manager for Fortified Function Core
* Handles memory monitoring, cleanup, and resource management
*/
class MemoryManager extends events.EventEmitter {
constructor(options) {
super();
this.options = options;
if (this.options.memoryPool) {
this.setupMemoryMonitoring();
}
}
/**
* Check if memory usage is within limits
*/
checkMemoryLimits() {
const currentUsage = utils.FortifiedUtils.getCurrentMemoryUsage();
const isExceeded = utils.FortifiedUtils.isMemoryLimitExceeded(currentUsage, this.options.maxMemoryUsage);
if (isExceeded) {
this.emit("memory_limit_exceeded", {
current: currentUsage,
limit: this.options.maxMemoryUsage,
});
}
return !isExceeded;
}
/**
* Get current memory usage information
*/
getMemoryInfo() {
const current = utils.FortifiedUtils.getCurrentMemoryUsage();
const limit = this.options.maxMemoryUsage;
const percentage = (current / limit) * 100;
const withinLimits = !utils.FortifiedUtils.isMemoryLimitExceeded(current, limit);
return {
current,
limit,
percentage,
withinLimits,
};
}
/**
* Force garbage collection if available
*/
forceGarbageCollection() {
if (global.gc) {
try {
global.gc();
this.emit("garbage_collection_forced");
return true;
}
catch (error) {
this.emit("garbage_collection_failed", { error });
return false;
}
}
return false;
}
/**
* Clean up execution context memory
*/
cleanupExecutionMemory(context) {
// Destroy secure buffers
for (const buffer of context.secureBuffers.values()) {
try {
buffer.destroy();
}
catch (error) {
this.emit("buffer_cleanup_error", {
executionId: context.executionId,
error,
});
}
}
// Clear encrypted parameters
context.encryptedParameters.clear();
this.emit("execution_memory_cleaned", {
executionId: context.executionId,
});
}
/**
* Schedule memory cleanup with delay
*/
scheduleMemoryCleanup(context, callback) {
const cleanup = () => {
this.cleanupExecutionMemory(context);
if (callback) {
callback();
}
};
if (this.options.memoryWipeDelay > 0) {
setTimeout(cleanup, this.options.memoryWipeDelay);
}
else {
cleanup();
}
}
/**
* Monitor memory usage and emit warnings
*/
setupMemoryMonitoring() {
this.memoryMonitorInterval = setInterval(() => {
const memoryInfo = this.getMemoryInfo();
this.emit("memory_status", memoryInfo);
// Emit warnings at different thresholds
if (memoryInfo.percentage > 90) {
this.emit("memory_critical", memoryInfo);
}
else if (memoryInfo.percentage > 75) {
this.emit("memory_warning", memoryInfo);
}
// Auto cleanup if memory is critical
if (memoryInfo.percentage > 95) {
this.forceGarbageCollection();
}
}, 5000); // Check every 5 seconds
}
/**
* Get memory usage statistics
*/
getMemoryStats() {
const memUsage = process.memoryUsage();
return {
heapUsed: memUsage.heapUsed,
heapTotal: memUsage.heapTotal,
external: memUsage.external,
rss: memUsage.rss,
};
}
/**
* Clean up resources
*/
destroy() {
if (this.memoryMonitorInterval) {
clearInterval(this.memoryMonitorInterval);
}
this.removeAllListeners();
}
}
exports.MemoryManager = MemoryManager;
//# sourceMappingURL=memory-manager.js.map