fortify2-js
Version:
MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.
128 lines (124 loc) • 4.51 kB
JavaScript
;
var events = require('events');
var utils = require('../../utils/utils.js');
/**
* Execution Engine for Fortified Function Core
* Handles function execution with timeout, retries, and security
*/
class ExecutionEngine extends events.EventEmitter {
constructor(options, securityManager) {
super();
this.options = options;
this.securityManager = securityManager;
}
/**
* Execute function with security monitoring and error handling
*/
async executeWithSecurity(originalFunction, context, args) {
const { executionId } = context;
// Set up timeout
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Function execution timeout after ${this.options.timeout}ms`));
}, this.options.timeout);
});
// Execute with retry logic
let lastError = null;
for (let attempt = 0; attempt <= this.options.retries; attempt++) {
try {
const executionPromise = this.securityManager.executeWithStackProtection(originalFunction, args);
const result = await Promise.race([
executionPromise,
timeoutPromise,
]);
this.emit("execution_success", { executionId, attempt });
return result;
}
catch (error) {
lastError = error;
this.emit("execution_error", { executionId, attempt, error });
if (attempt < this.options.retries) {
const delay = utils.FortifiedUtils.calculateRetryDelay(attempt, this.options.maxRetryDelay);
await utils.FortifiedUtils.sleep(delay);
this.emit("execution_retry", {
executionId,
attempt: attempt + 1,
delay,
});
}
}
}
throw lastError;
}
/**
* Execute function with full monitoring and validation
*/
async executeWithMonitoring(originalFunction, context, args, preExecutionHook, postExecutionHook) {
// Pre-execution validation and hooks
if (preExecutionHook) {
await preExecutionHook();
}
this.emit("execution_started", {
executionId: context.executionId,
timestamp: new Date(),
});
try {
// Execute with security and monitoring
const result = await this.executeWithSecurity(originalFunction, context, args);
// Post-execution hooks
if (postExecutionHook) {
await postExecutionHook(result);
}
this.emit("execution_completed", {
executionId: context.executionId,
duration: performance.now() - context.startTime,
});
return result;
}
catch (error) {
this.emit("execution_failed", {
executionId: context.executionId,
error: error,
duration: performance.now() - context.startTime,
});
throw error;
}
}
/**
* Validate execution prerequisites
*/
validateExecution(args) {
// Check if arguments are valid
if (!Array.isArray(args)) {
throw new Error("Invalid arguments provided to fortified function");
}
// Additional validation can be added here
this.emit("execution_validated", { argsLength: args.length });
}
/**
* Create timeout promise for execution
*/
createTimeoutPromise(timeoutMs) {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Function execution timeout after ${timeoutMs}ms`));
}, timeoutMs);
});
}
/**
* Execute single attempt with timeout
*/
async executeSingleAttempt(originalFunction, args, timeoutMs) {
const timeoutPromise = this.createTimeoutPromise(timeoutMs);
const executionPromise = this.securityManager.executeWithStackProtection(originalFunction, args);
return await Promise.race([executionPromise, timeoutPromise]);
}
/**
* Clean up resources
*/
destroy() {
this.removeAllListeners();
}
}
exports.ExecutionEngine = ExecutionEngine;
//# sourceMappingURL=execution-engine.js.map