UNPKG

@sailboat-computer/event-bus

Version:

Standardized event bus for sailboat computer v3 with resilience features and offline capabilities

269 lines 12.3 kB
"use strict"; /** * Event bus configuration validation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.validateConfig = exports.DEFAULT_MEMORY_CONFIG = exports.DEFAULT_REDIS_CONFIG = exports.DEFAULT_CONFIG = void 0; const errors_1 = require("./errors"); /** * Default event bus configuration */ exports.DEFAULT_CONFIG = { adapter: { type: 'memory', config: { serviceName: 'default-service' } }, offlineBuffer: { maxSize: 10000, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false }, deadLetterQueue: { enabled: true, maxSize: 1000, maxAttempts: 3 }, monitoring: { enabled: true, alertThresholds: { failedPublishesThreshold: 5, deadLetterQueueSizeThreshold: 10, reconnectionAttemptsThreshold: 3, processingTimeThreshold: 1000 }, checkInterval: 60000 // 1 minute } }; /** * Default Redis adapter configuration */ exports.DEFAULT_REDIS_CONFIG = { url: 'redis://localhost:6379', consumerGroup: 'default-group', consumerName: 'default-consumer', maxBatchSize: 100, pollInterval: 1000, reconnectOptions: { baseDelay: 1000, maxDelay: 30000, maxRetries: 10 } }; /** * Default memory adapter configuration */ exports.DEFAULT_MEMORY_CONFIG = { eventTtl: 3600000, // 1 hour simulateLatency: false, latencyRange: [10, 50] }; /** * Validate event bus configuration * * @param config - Event bus configuration * @returns Validated configuration with defaults applied * @throws ConfigurationError if configuration is invalid */ function validateConfig(config) { // Start with default configuration const validatedConfig = { ...exports.DEFAULT_CONFIG, ...config }; // Validate adapter configuration if (!validatedConfig.adapter) { throw new errors_1.ConfigurationError('Adapter configuration is required'); } // Validate adapter type if (!validatedConfig.adapter.type) { throw new errors_1.ConfigurationError('Adapter type is required'); } if (validatedConfig.adapter.type !== 'redis' && validatedConfig.adapter.type !== 'memory') { throw new errors_1.ConfigurationError(`Invalid adapter type: ${validatedConfig.adapter.type}`); } // Validate adapter-specific configuration if (!validatedConfig.adapter.config) { validatedConfig.adapter.config = validatedConfig.adapter.type === 'redis' ? { ...exports.DEFAULT_REDIS_CONFIG, serviceName: 'default-service' } : { ...exports.DEFAULT_MEMORY_CONFIG, serviceName: 'default-service' }; } // Validate Redis adapter configuration if (validatedConfig.adapter.type === 'redis') { validatedConfig.adapter.config = validateRedisConfig(validatedConfig.adapter.config); } // Validate memory adapter configuration if (validatedConfig.adapter.type === 'memory') { validatedConfig.adapter.config = validateMemoryConfig(validatedConfig.adapter.config); } // Validate offline buffer configuration if (!validatedConfig.offlineBuffer) { validatedConfig.offlineBuffer = { ...exports.DEFAULT_CONFIG.offlineBuffer }; } else { if (typeof validatedConfig.offlineBuffer.maxSize !== 'number' || validatedConfig.offlineBuffer.maxSize < 0) { throw new errors_1.ConfigurationError(`Invalid offline buffer max size: ${validatedConfig.offlineBuffer.maxSize}`); } if (typeof validatedConfig.offlineBuffer.priorityRetention !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid offline buffer priority retention: ${validatedConfig.offlineBuffer.priorityRetention}`); } } // Validate metrics configuration if (!validatedConfig.metrics) { validatedConfig.metrics = { ...exports.DEFAULT_CONFIG.metrics }; } else { if (typeof validatedConfig.metrics.enabled !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid metrics enabled: ${validatedConfig.metrics.enabled}`); } if (typeof validatedConfig.metrics.detailedTimings !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid metrics detailed timings: ${validatedConfig.metrics.detailedTimings}`); } } // Validate dead letter queue configuration if (!validatedConfig.deadLetterQueue) { validatedConfig.deadLetterQueue = { ...exports.DEFAULT_CONFIG.deadLetterQueue }; } else { if (typeof validatedConfig.deadLetterQueue.enabled !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid dead letter queue enabled: ${validatedConfig.deadLetterQueue.enabled}`); } if (typeof validatedConfig.deadLetterQueue.maxSize !== 'number' || validatedConfig.deadLetterQueue.maxSize < 0) { throw new errors_1.ConfigurationError(`Invalid dead letter queue max size: ${validatedConfig.deadLetterQueue.maxSize}`); } if (typeof validatedConfig.deadLetterQueue.maxAttempts !== 'number' || validatedConfig.deadLetterQueue.maxAttempts < 1) { throw new errors_1.ConfigurationError(`Invalid dead letter queue max attempts: ${validatedConfig.deadLetterQueue.maxAttempts}`); } } // Validate monitoring configuration if (!validatedConfig.monitoring) { validatedConfig.monitoring = { ...exports.DEFAULT_CONFIG.monitoring }; } else { if (typeof validatedConfig.monitoring.enabled !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid monitoring enabled: ${validatedConfig.monitoring.enabled}`); } // Validate alert thresholds if (!validatedConfig.monitoring.alertThresholds) { validatedConfig.monitoring.alertThresholds = { ...exports.DEFAULT_CONFIG.monitoring.alertThresholds }; } else { const { alertThresholds } = validatedConfig.monitoring; if (alertThresholds.failedPublishesThreshold !== undefined && (typeof alertThresholds.failedPublishesThreshold !== 'number' || alertThresholds.failedPublishesThreshold < 0)) { throw new errors_1.ConfigurationError(`Invalid failed publishes threshold: ${alertThresholds.failedPublishesThreshold}`); } if (alertThresholds.deadLetterQueueSizeThreshold !== undefined && (typeof alertThresholds.deadLetterQueueSizeThreshold !== 'number' || alertThresholds.deadLetterQueueSizeThreshold < 0)) { throw new errors_1.ConfigurationError(`Invalid dead letter queue size threshold: ${alertThresholds.deadLetterQueueSizeThreshold}`); } if (alertThresholds.reconnectionAttemptsThreshold !== undefined && (typeof alertThresholds.reconnectionAttemptsThreshold !== 'number' || alertThresholds.reconnectionAttemptsThreshold < 0)) { throw new errors_1.ConfigurationError(`Invalid reconnection attempts threshold: ${alertThresholds.reconnectionAttemptsThreshold}`); } if (alertThresholds.processingTimeThreshold !== undefined && (typeof alertThresholds.processingTimeThreshold !== 'number' || alertThresholds.processingTimeThreshold < 0)) { throw new errors_1.ConfigurationError(`Invalid processing time threshold: ${alertThresholds.processingTimeThreshold}`); } } // Validate check interval if (validatedConfig.monitoring.checkInterval !== undefined && (typeof validatedConfig.monitoring.checkInterval !== 'number' || validatedConfig.monitoring.checkInterval < 1000)) { throw new errors_1.ConfigurationError(`Invalid monitoring check interval: ${validatedConfig.monitoring.checkInterval}`); } } return validatedConfig; } exports.validateConfig = validateConfig; /** * Validate Redis adapter configuration * * @param config - Redis adapter configuration * @returns Validated configuration with defaults applied * @throws ConfigurationError if configuration is invalid */ function validateRedisConfig(config) { // Start with default configuration const validatedConfig = { ...exports.DEFAULT_REDIS_CONFIG, ...config }; // Validate required fields if (!validatedConfig.serviceName) { throw new errors_1.ConfigurationError('Service name is required'); } if (!validatedConfig.url) { throw new errors_1.ConfigurationError('Redis URL is required'); } if (!validatedConfig.consumerGroup) { throw new errors_1.ConfigurationError('Consumer group is required'); } if (!validatedConfig.consumerName) { throw new errors_1.ConfigurationError('Consumer name is required'); } // Validate numeric fields if (typeof validatedConfig.maxBatchSize !== 'number' || validatedConfig.maxBatchSize <= 0) { throw new errors_1.ConfigurationError(`Invalid max batch size: ${validatedConfig.maxBatchSize}`); } if (typeof validatedConfig.pollInterval !== 'number' || validatedConfig.pollInterval <= 0) { throw new errors_1.ConfigurationError(`Invalid poll interval: ${validatedConfig.pollInterval}`); } // Validate reconnect options if (!validatedConfig.reconnectOptions) { validatedConfig.reconnectOptions = { ...exports.DEFAULT_REDIS_CONFIG.reconnectOptions }; } else { if (typeof validatedConfig.reconnectOptions.baseDelay !== 'number' || validatedConfig.reconnectOptions.baseDelay <= 0) { throw new errors_1.ConfigurationError(`Invalid reconnect base delay: ${validatedConfig.reconnectOptions.baseDelay}`); } if (typeof validatedConfig.reconnectOptions.maxDelay !== 'number' || validatedConfig.reconnectOptions.maxDelay <= 0) { throw new errors_1.ConfigurationError(`Invalid reconnect max delay: ${validatedConfig.reconnectOptions.maxDelay}`); } if (typeof validatedConfig.reconnectOptions.maxRetries !== 'number' || validatedConfig.reconnectOptions.maxRetries < 0) { throw new errors_1.ConfigurationError(`Invalid reconnect max retries: ${validatedConfig.reconnectOptions.maxRetries}`); } } return validatedConfig; } /** * Validate memory adapter configuration * * @param config - Memory adapter configuration * @returns Validated configuration with defaults applied * @throws ConfigurationError if configuration is invalid */ function validateMemoryConfig(config) { // Start with default configuration const validatedConfig = { ...exports.DEFAULT_MEMORY_CONFIG, ...config }; // Validate required fields if (!validatedConfig.serviceName) { throw new errors_1.ConfigurationError('Service name is required'); } // Validate optional fields if (validatedConfig.eventTtl !== undefined && (typeof validatedConfig.eventTtl !== 'number' || validatedConfig.eventTtl < 0)) { throw new errors_1.ConfigurationError(`Invalid event TTL: ${validatedConfig.eventTtl}`); } if (validatedConfig.simulateLatency !== undefined && typeof validatedConfig.simulateLatency !== 'boolean') { throw new errors_1.ConfigurationError(`Invalid simulate latency: ${validatedConfig.simulateLatency}`); } if (validatedConfig.latencyRange !== undefined) { if (!Array.isArray(validatedConfig.latencyRange) || validatedConfig.latencyRange.length !== 2) { throw new errors_1.ConfigurationError(`Invalid latency range: ${validatedConfig.latencyRange}`); } if (typeof validatedConfig.latencyRange[0] !== 'number' || validatedConfig.latencyRange[0] < 0) { throw new errors_1.ConfigurationError(`Invalid latency range min: ${validatedConfig.latencyRange[0]}`); } if (typeof validatedConfig.latencyRange[1] !== 'number' || validatedConfig.latencyRange[1] < validatedConfig.latencyRange[0]) { throw new errors_1.ConfigurationError(`Invalid latency range max: ${validatedConfig.latencyRange[1]}`); } } return validatedConfig; } //# sourceMappingURL=config.js.map