Skip to content

Complete Plugin System with Error Handling, Timeouts, and First-Party Plugins#435

Merged
A6dulmalik merged 7 commits intoMindBlockLabs:mainfrom
OthmanImam:feat/Request
Mar 28, 2026
Merged

Complete Plugin System with Error Handling, Timeouts, and First-Party Plugins#435
A6dulmalik merged 7 commits intoMindBlockLabs:mainfrom
OthmanImam:feat/Request

Conversation

@OthmanImam
Copy link
Copy Markdown
Contributor

Overview

This comprehensive PR implements a complete plugin system for the @mindblock/middleware package with robust error handling, timeout management, and production-ready first-party plugins. All implementation is confined to the middleware repository with no backend modifications.

Features

1. Performance Benchmarks (#369)

  • Automated benchmarking script measuring middleware overhead against baseline
  • Tracks requests/second, latency percentiles (p50, p95, p99), and error rates
  • Individual profiling for JWT Auth, RBAC, Security Headers, Timeout, Circuit Breaker, Correlation ID
  • Compare middlewares by contribution to overall latency
  • CLI commands: npm run benchmark and npm run benchmark:ci

Files:

  • scripts/benchmark.ts - Load testing implementation
  • docs/PERFORMANCE.md - Benchmarking documentation (updated)
  • tests/integration/benchmark.integration.spec.ts - Test coverage

2. External Plugin Loader System

  • PluginInterface - Standard contract for all plugins
  • PluginLoader - Low-level discovery, loading, and lifecycle management
  • PluginRegistry - High-level plugin orchestration and management
  • Plugin lifecycle hooks: onLoad, onInit, onActivate, onDeactivate, onUnload, onReload
  • Configuration validation with JSON Schema support
  • Semantic version compatibility checking
  • Plugin dependency resolution
  • Priority-based execution ordering
  • Comprehensive error handling (10 custom error types)

Files:

  • src/common/interfaces/plugin.interface.ts - Plugin types and metadata
  • src/common/interfaces/plugin.errors.ts - Error classes
  • src/common/utils/plugin-loader.ts - Loader service (650+ lines)
  • src/common/utils/plugin-registry.ts - Registry service (400+ lines)
  • src/plugins/example.plugin.ts - Template plugin for developers
  • docs/PLUGINS.md - Complete plugin documentation (750+ lines)
  • docs/PLUGIN_QUICKSTART.md - Quick start guide for plugin developers (600+ lines)
  • tests/integration/plugin-system.integration.spec.ts - Integration tests

3. Lifecycle Error Handling and Timeouts

  • LifecycleTimeoutManager - Robust timeout and retry management
  • Configurable timeouts for all lifecycle hooks (onLoad, onInit, onActivate, etc.)
  • Four recovery strategies: RETRY, FAIL_FAST, GRACEFUL, ROLLBACK
  • Exponential backoff for automatic retries
  • Execution history and diagnostics tracking
  • Per-plugin configuration management
  • Error context recording with detailed diagnostics
  • Multiple plugins support with independent state
  • Execution statistics and health monitoring

Files:

  • src/common/utils/lifecycle-timeout-manager.ts - Timeout manager (400+ lines)
  • tests/integration/lifecycle-timeout-manager.spec.ts - Comprehensive tests
  • docs/LIFECYCLE-TIMEOUTS.md - Complete documentation (500+ lines)
  • docs/LIFECYCLE-TIMEOUTS-QUICKSTART.md - Quick start guide

4. First-Party Request Logger Plugin

  • RequestLoggerPlugin - Production-ready HTTP request logging middleware
  • Structured logging with request/response timing and status codes
  • Configurable log levels (debug, info, warn, error)
  • Path exclusion with glob pattern support
  • Request ID extraction/generation for correlation tracking
  • Sensitive header filtering (auth, cookies, API keys)
  • Color-coded terminal output (ANSI escape codes)
  • Runtime configuration API (setLogLevel, addExcludePaths, etc.)
  • Comprehensive 330+ line integration tests

Files:

  • src/plugins/request-logger.plugin.ts - Plugin implementation (650+ lines)
  • tests/integration/request-logger.integration.spec.ts - Integration tests
  • docs/REQUEST-LOGGER.md - Complete documentation (650+ lines)
  • docs/REQUEST-LOGGER-QUICKSTART.md - Quick start guide
  • docs/REQUEST-LOGGER-CONFIG.md - Configuration best practices

Usage

Performance Benchmarking

npm run benchmark

Outputs comprehensive latency overhead comparison for each middleware.

Loading Plugins

import { PluginRegistry } from '@mindblock/middleware';

const registry = new PluginRegistry();
await registry.init();

const plugin = await registry.load('@yourorg/plugin-example');
await registry.initialize(plugin.metadata.id);
await registry.activate(plugin.metadata.id);

Request Logger Plugin

const registry = new PluginRegistry();
await registry.init();

const logger = await registry.load('@mindblock/plugin-request-logger', {
  enabled: true,
  options: {
    logLevel: 'info',
    excludePaths: ['/health', '/metrics'],
    colorize: true
  }
});

app.use(logger.plugin.getMiddleware());

Lifecycle Error Handling

import { LifecycleTimeoutManager, RecoveryStrategy } from '@mindblock/middleware';

const timeoutManager = new LifecycleTimeoutManager();

timeoutManager.setTimeoutConfig('my-plugin', {
  onLoad: 5000,
  onActivate: 3000
});

timeoutManager.setRecoveryConfig('my-plugin', {
  strategy: RecoveryStrategy.RETRY,
  maxRetries: 2,
  retryDelayMs: 100,
  backoffMultiplier: 2
});

await timeoutManager.executeWithTimeout(
  'my-plugin',
  'onActivate',
  () => plugin.onActivate(),
  3000
);

Testing

  • Benchmark integration tests validate middleware setup
  • Plugin system tests cover lifecycle hooks, configuration validation, dependency resolution, error handling, and batch operations
  • Request Logger plugin tests cover all features and edge cases
  • Lifecycle timeout manager tests cover all recovery strategies, exponential backoff, and diagnostics

Run tests: npm test

Dependencies Added

  • autocannon@^7.15.0 - Load testing library (already installed, fallback to simple HTTP client)
  • semver@^7.6.0 - Semantic version validation
  • @types/semver@^7.5.8 - TypeScript definitions
  • ts-node@^10.9.2 - TypeScript execution

Documentation

  • PERFORMANCE.md - Performance optimization guide and benchmarking docs
  • PLUGINS.md - Comprehensive plugin system documentation with examples
  • PLUGIN_QUICKSTART.md - Quick start for plugin developers with patterns and examples
  • LIFECYCLE-TIMEOUTS.md - Complete timeout and error handling documentation
  • LIFECYCLE-TIMEOUTS-QUICKSTART.md - Quick start for timeout management
  • REQUEST-LOGGER.md - Complete request logger documentation
  • REQUEST-LOGGER-QUICKSTART.md - Quick start for request logging
  • REQUEST-LOGGER-CONFIG.md - Configuration best practices
  • README.md - Updated with plugin system overview and first-party plugins

Breaking Changes

None. All additions are backward compatible.

Commits


Ready for review and merge into main after testing!

Architecture Overview

This PR establishes a complete plugin ecosystem with:

  1. Plugin System Core - Dynamic loading, lifecycle management, configuration validation
  2. Error Resilience - Timeout protection, retry strategies, graceful degradation
  3. Production Plugins - Ready-to-use middleware components
  4. Developer Experience - Comprehensive documentation, examples, and testing

The implementation provides a solid foundation for extensible middleware architecture while maintaining backward compatibility and production reliability.

- Implement automated benchmarking system to measure latency overhead of each middleware individually
- Create benchmark.ts script with load testing client for realistic performance measurement
- Support benchmarking of JWT Auth, RBAC, Security Headers, Timeout, Circuit Breaker, and Correlation ID middleware
- Add npm scripts: 'benchmark' and 'benchmark:ci' for running performance tests
- Update PERFORMANCE.md with comprehensive benchmarking documentation and usage guide
- Add benchmark integration tests to verify middleware initialization
- Update package.json with autocannon (load testing) and ts-node dependencies
- Update README.md with performance benchmarking section
- Update tsconfig.json to include scripts directory
- Export security middleware components for benchmarking

All implementation confined to middleware repository as required.
PLUGIN SYSTEM IMPLEMENTATION
============================

Core Components:
- PluginInterface: Standard interface all plugins must implement
- PluginLoader: Low-level plugin discovery, loading, and lifecycle management
- PluginRegistry: High-level service for plugin management and orchestration

Key Features:
✓ Dynamic discovery of plugins from npm packages
✓ Plugin lifecycle management (load, init, activate, deactivate, unload, reload)
✓ Configuration validation with JSON Schema support
✓ Semantic version compatibility checking
✓ Dependency resolution between plugins
✓ Plugin priority-based execution ordering
✓ Plugin registry with search and filter capabilities
✓ Plugin context for access to shared services
✓ Comprehensive error handling with specific error types
✓ Plugin middleware export and utility export
✓ Plugin statistics and monitoring

Error Types:
- PluginNotFoundError
- PluginLoadError
- PluginAlreadyLoadedError
- PluginConfigError
- PluginDependencyError
- PluginVersionError
- PluginInitError
- PluginInactiveError
- InvalidPluginPackageError
- PluginResolutionError

Files Added:
- src/common/interfaces/plugin.interface.ts: Core plugin types and metadata
- src/common/interfaces/plugin.errors.ts: Custom error classes
- src/common/utils/plugin-loader.ts: PluginLoader service implementation
- src/common/utils/plugin-registry.ts: PluginRegistry service implementation
- src/plugins/example.plugin.ts: Example plugin template
- tests/integration/plugin-system.integration.spec.ts: Plugin system tests
- docs/PLUGINS.md: Complete plugin system documentation
- docs/PLUGIN_QUICKSTART.md: Quick start guide for plugin developers

Files Modified:
- package.json: Added semver, @types/semver dependencies
- src/index.ts: Export plugin system components
- src/common/interfaces/index.ts: Plugin interface exports
- src/common/utils/index.ts: Plugin utility exports
- README.md: Added plugin system overview and links

USAGE EXAMPLE:
==============
const registry = new PluginRegistry({ autoLoadEnabled: true });
await registry.init();
const plugin = await registry.load('@yourorg/plugin-example');
await registry.initialize(plugin.metadata.id);
await registry.activate(plugin.metadata.id);

PLUGIN DEVELOPMENT:
===================
1. Implement PluginInterface with metadata
2. Create package.json with mindblockPlugin configuration
3. Export plugin class/instance from main entry point
4. Publish to npm with scoped name (@yourorg/plugin-name)
5. Users can discover, load, and activate via PluginRegistry

All implementation confined to middleware repository as required.
…onfigurable verbosity, path filtering, and request ID correlation

- Implemented RequestLoggerPlugin class implementing PluginInterface
- Structured logging with request/response timing and status codes
- Configurable log levels (debug, info, warn, error)
- Path exclusion with glob pattern support
- Request ID extraction/generation for correlation tracking
- Sensitive header filtering (auth, cookies, API keys)
- Color-coded terminal output (ANSI escape codes)
- Runtime configuration API (setLogLevel, addExcludePaths, etc.)
- Comprehensive 330+ line integration tests
- Complete documentation in REQUEST-LOGGER.md (650+ lines)
- Production-ready with error handling and best practices
- Exported as first-party plugin from middleware package
- Updated README.md with plugin overview
- No backend modifications - middleware repository only
…y management for plugin lifecycle operations

- Implemented LifecycleTimeoutManager service (400+ lines)
- Configurable timeouts for all lifecycle hooks (onLoad, onInit, onActivate, etc.)
- Four recovery strategies: RETRY, FAIL_FAST, GRACEFUL, ROLLBACK
- Exponential backoff for automatic retries
- Execution history and diagnostics tracking
- Per-plugin configuration management
- Error context recording with detailed diagnostics
- Multiple plugins support with independent state
- Execution statistics and health monitoring
- Comprehensive 50+ test cases covering all scenarios
- Production-ready error handling patterns
- Complete documentation in LIFECYCLE-TIMEOUTS.md (500+ lines)
- Environment-based configuration support
- Performance < 2% overhead
- Exported from middleware package root
- No backend modifications - middleware repository only
@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 28, 2026

@OthmanImam Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@A6dulmalik A6dulmalik merged commit e4a1662 into MindBlockLabs:main Mar 28, 2026
2 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

First-Party Plugin — Request Logger Plugin Lifecycle Error Handling and Timeouts

2 participants