update
This commit is contained in:
282
readme.plan.md
282
readme.plan.md
@ -1,76 +1,258 @@
|
||||
# DCRouter Project Improvement Plan
|
||||
# SMTP Server Refactoring Plan
|
||||
|
||||
## Type Safety Enhancement Plan
|
||||
## Problem Statement
|
||||
|
||||
Our goal is to improve type safety across the DCRouter codebase to reduce runtime errors, improve developer experience, and make the code more maintainable. This document outlines the specific changes we'll implement.
|
||||
The SMTP server implementation in `classes.smtpserver.ts` has grown to be too large and complex, with over 1300 lines of code. This makes it difficult to maintain, test, and extend. We need to refactor it into multiple smaller, focused files to improve maintainability and adhere to the single responsibility principle.
|
||||
|
||||
### 1. Platform Service Interface Improvements
|
||||
## Refactoring Goals
|
||||
|
||||
- [ ] Create a comprehensive `IPlatformService` interface to replace `any` type usage
|
||||
- [ ] Define specific methods and properties that platform services should implement
|
||||
- [ ] Update all references to use the new interface
|
||||
1. Improve code organization by splitting the SMTPServer class into multiple focused modules
|
||||
2. Enhance testability by making components more isolated and easier to mock
|
||||
3. Improve maintainability by reducing file sizes and complexity
|
||||
4. Preserve existing functionality and behavior while improving the architecture
|
||||
|
||||
### 2. SMTP Session Type Safety
|
||||
## Proposed File Structure
|
||||
|
||||
- [ ] Define a proper `ISmtpSession` interface with all required properties
|
||||
- [ ] Ensure consistent usage across SMTPServer implementation
|
||||
- [ ] Add proper validation for session properties
|
||||
```
|
||||
mail/
|
||||
└── delivery/
|
||||
├── smtp/
|
||||
│ ├── index.ts - Main export file
|
||||
│ ├── interfaces.ts - All SMTP-related interfaces
|
||||
│ ├── constants.ts - Constants and enums
|
||||
│ ├── smtp-server.ts - Main server class (core functionality)
|
||||
│ ├── session-manager.ts - Session creation and management
|
||||
│ ├── command-handler.ts - SMTP command processing
|
||||
│ ├── data-handler.ts - Email data processing
|
||||
│ ├── tls-handler.ts - TLS and STARTTLS functionality
|
||||
│ ├── security-handler.ts - Security checks and validations
|
||||
│ ├── connection-manager.ts - Connection management and cleanup
|
||||
│ └── utils/
|
||||
│ ├── validation.ts - Validation utilities
|
||||
│ ├── logging.ts - Logging utilities
|
||||
│ └── helpers.ts - Other helper functions
|
||||
├── classes.smtpserver.ts - Legacy file (will be deprecated)
|
||||
└── interfaces.ts - Main delivery interfaces
|
||||
```
|
||||
|
||||
### 3. Configuration Type Enhancements
|
||||
## Module Responsibilities
|
||||
|
||||
- [ ] Replace loose config objects with strictly typed interfaces
|
||||
- [ ] Add validation functions for all configuration objects
|
||||
- [ ] Create union types for configuration options with specific values
|
||||
### 1. smtp-server.ts (150-200 lines)
|
||||
- Core server initialization and lifecycle management
|
||||
- Server startup and shutdown
|
||||
- Port binding and socket creation
|
||||
- Delegates to other modules for specific functionality
|
||||
|
||||
### 4. Function Return Types
|
||||
### 2. session-manager.ts (100-150 lines)
|
||||
- Session creation and tracking
|
||||
- Session timeout management
|
||||
- Session cleanup
|
||||
- Session state management
|
||||
|
||||
- [ ] Audit all async functions to ensure they have explicit return types
|
||||
- [ ] Replace `Promise<any>` with specific return type interfaces
|
||||
- [ ] Add proper error types for rejected promises
|
||||
### 3. command-handler.ts (200-250 lines)
|
||||
- SMTP command parsing and routing
|
||||
- Command validation
|
||||
- Command implementation (EHLO, MAIL FROM, RCPT TO, etc.)
|
||||
- Response formatting
|
||||
|
||||
### 5. String Literal Types and Enums
|
||||
### 4. data-handler.ts (150-200 lines)
|
||||
- Email data collection and processing
|
||||
- DATA command handling
|
||||
- MIME parsing
|
||||
- Email creation and forwarding
|
||||
- Email storage
|
||||
|
||||
- [ ] Replace string constants with proper TypeScript enums or string literal unions
|
||||
- [ ] Create specific types for email status values, priorities, etc.
|
||||
- [ ] Ensure consistent usage throughout the codebase
|
||||
### 5. tls-handler.ts (100-150 lines)
|
||||
- TLS connection handling
|
||||
- STARTTLS implementation
|
||||
- Certificate management
|
||||
- Secure socket creation
|
||||
|
||||
### 6. Event System Types
|
||||
### 6. security-handler.ts (100-150 lines)
|
||||
- IP reputation checking
|
||||
- Access control
|
||||
- Rate limiting
|
||||
- Security logging
|
||||
- SPAM detection
|
||||
|
||||
- [ ] Create typed event emitters for all event-based components
|
||||
- [ ] Define specific event payload interfaces for each event type
|
||||
- [ ] Ensure type safety for event handlers
|
||||
### 7. connection-manager.ts (100-150 lines)
|
||||
- Connection tracking and limits
|
||||
- Idle connection cleanup
|
||||
- Connection error handling
|
||||
- Socket event handling
|
||||
|
||||
### 7. Authentication Data Types
|
||||
### 8. interfaces.ts (100-150 lines)
|
||||
- All interface definitions
|
||||
- Type aliases
|
||||
- Type guards
|
||||
- Enum definitions
|
||||
|
||||
- [ ] Create proper interfaces for authentication data
|
||||
- [ ] Replace generic Record types with specific property interfaces
|
||||
- [ ] Add validation for auth data objects
|
||||
## Implementation Strategy
|
||||
|
||||
### 8. Email Attachment Type Safety
|
||||
### Phase 1: Initial Structure and Scaffolding (Days 1-2)
|
||||
|
||||
- [ ] Define comprehensive interfaces for email attachments
|
||||
- [ ] Ensure consistent usage between different email handling components
|
||||
- [ ] Add validation for attachment properties
|
||||
1. Create the folder structure and empty files
|
||||
- Create `mail/delivery/smtp` directory
|
||||
- Create all module files with basic exports
|
||||
- Set up barrel file (index.ts)
|
||||
|
||||
### 9. Processing Mode Type Safety
|
||||
2. Move interfaces to the new interfaces.ts file
|
||||
- Extract all interfaces from current implementation
|
||||
- Add proper documentation
|
||||
- Add any missing interface properties
|
||||
|
||||
- [ ] Create discriminated unions for different email processing modes
|
||||
- [ ] Add type guards to ensure safe handling of mode-specific properties
|
||||
- [ ] Ensure proper validation of processing mode values
|
||||
3. Extract constants and enums to constants.ts
|
||||
- Move all constants and enums to dedicated file
|
||||
- Ensure proper typing and documentation
|
||||
- Replace magic numbers with named constants
|
||||
|
||||
### 10. Third-Party Integration Types
|
||||
4. Set up the basic structure for each module
|
||||
- Define basic class skeletons
|
||||
- Set up dependency injection structure
|
||||
- Document interfaces for each module
|
||||
|
||||
- [ ] Review and update type definitions for third-party dependencies
|
||||
- [ ] Create additional type definitions where missing
|
||||
- [ ] Ensure consistent usage of external libraries
|
||||
### Phase 2: Gradual Implementation Transfer (Days 3-7)
|
||||
|
||||
## Implementation Order
|
||||
1. Start with utility modules
|
||||
- Implement validation.ts with email validation functions
|
||||
- Create logging.ts with structured logging utilities
|
||||
- Build helpers.ts with common helper functions
|
||||
|
||||
We'll implement these improvements in the following order:
|
||||
2. Implement session-manager.ts
|
||||
- Extract session creation and management logic
|
||||
- Implement timeout handling
|
||||
- Add session state management functions
|
||||
|
||||
1. First, focus on the core interfaces (Platform Service, SMTP Session)
|
||||
2. Next, improve configuration types as they affect multiple components
|
||||
3. Then, address function return types and string literals
|
||||
4. Finally, handle the remaining specialized types (auth, attachments, etc.)
|
||||
3. Implement connection-manager.ts
|
||||
- Extract connection tracking code
|
||||
- Implement connection limits
|
||||
- Add socket event handling logic
|
||||
|
||||
This approach allows us to tackle the most widely-used types first, providing the greatest immediate benefit while establishing patterns for the rest of the implementation.
|
||||
4. Implement command-handler.ts
|
||||
- Extract command parsing and processing
|
||||
- Split command handlers into separate methods
|
||||
- Implement command validation and routing
|
||||
|
||||
5. Implement data-handler.ts
|
||||
- Extract email data processing logic
|
||||
- Implement DATA command handling
|
||||
- Add email storage and forwarding
|
||||
|
||||
6. Implement tls-handler.ts
|
||||
- Extract TLS connection handling
|
||||
- Implement STARTTLS functionality
|
||||
- Add certificate management
|
||||
|
||||
7. Implement security-handler.ts
|
||||
- Extract IP reputation checking
|
||||
- Implement security logging
|
||||
- Add access control functionality
|
||||
|
||||
### Phase 3: Core Server Refactoring (Days 8-10)
|
||||
|
||||
1. Refactor the main SMTPServer class
|
||||
- Update constructor to create and initialize components
|
||||
- Implement dependency injection for all modules
|
||||
- Delegate functionality to appropriate modules
|
||||
- Reduce core class to server lifecycle management
|
||||
|
||||
2. Update event handling
|
||||
- Ensure proper event propagation between modules
|
||||
- Implement event delegation pattern
|
||||
- Add missing event handlers
|
||||
|
||||
3. Implement cross-module communication
|
||||
- Define clear interfaces for module interaction
|
||||
- Ensure proper data flow between components
|
||||
- Avoid circular dependencies
|
||||
|
||||
4. Verify functionality preservation
|
||||
- Ensure all methods have equivalent implementations
|
||||
- Add logging to trace execution flow
|
||||
- Create test cases for edge conditions
|
||||
|
||||
### Phase 4: Legacy Compatibility (Days 11-12)
|
||||
|
||||
1. Create facade in classes.smtpserver.ts
|
||||
- Keep original class signature
|
||||
- Delegate to new implementation internally
|
||||
- Ensure backward compatibility
|
||||
|
||||
2. Add deprecation notices
|
||||
- Mark legacy file with deprecation comments
|
||||
- Add migration guide in comments
|
||||
- Document breaking changes if any
|
||||
|
||||
3. Update documentation
|
||||
- Create detailed documentation for new architecture
|
||||
- Add examples of how to use the new modules
|
||||
- Document extension points
|
||||
|
||||
4. Create comprehensive tests
|
||||
- Ensure all modules have proper unit tests
|
||||
- Add integration tests between modules
|
||||
- Verify backward compatibility with existing code
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. Unit Testing
|
||||
- Create unit tests for each module in isolation
|
||||
- Mock dependencies for pure unit testing
|
||||
- Test edge cases and error handling
|
||||
|
||||
2. Integration Testing
|
||||
- Test interactions between modules
|
||||
- Verify correct event propagation
|
||||
- Test full SMTP communication flow
|
||||
|
||||
3. Regression Testing
|
||||
- Ensure all existing tests pass
|
||||
- Verify no functionality is lost
|
||||
- Compare performance metrics
|
||||
|
||||
4. Compatibility Testing
|
||||
- Test with existing code that uses the legacy class
|
||||
- Verify backward compatibility
|
||||
- Document any necessary migration steps
|
||||
|
||||
## Timeline Estimate
|
||||
|
||||
- Phase 1: 1-2 days
|
||||
- Phase 2: 3-5 days
|
||||
- Phase 3: 2-3 days
|
||||
- Phase 4: 1-2 days
|
||||
|
||||
Total: 7-12 days of development time
|
||||
|
||||
## Success Criteria
|
||||
|
||||
1. All existing tests pass with the new implementation
|
||||
2. No regression in functionality or performance
|
||||
3. Each file is less than 300 lines of code
|
||||
4. Improved code organization and documentation
|
||||
5. Better separation of concerns between modules
|
||||
6. Easier maintenance and extension of SMTP functionality
|
||||
|
||||
## Risks and Mitigations
|
||||
|
||||
### Risk: Breaking existing functionality
|
||||
**Mitigation**: Comprehensive test coverage and backward compatibility facade
|
||||
|
||||
### Risk: Performance degradation due to additional indirection
|
||||
**Mitigation**: Performance benchmarking before and after refactoring
|
||||
|
||||
### Risk: Increased complexity due to distributed code
|
||||
**Mitigation**: Clear documentation and proper module interfaces
|
||||
|
||||
### Risk: Time overrun due to unforeseen dependencies
|
||||
**Mitigation**: Incremental approach with working checkpoints after each phase
|
||||
|
||||
## Future Extensions
|
||||
|
||||
Once this refactoring is complete, we can more easily:
|
||||
|
||||
1. Add support for additional SMTP extensions
|
||||
2. Implement pluggable authentication mechanisms
|
||||
3. Add more sophisticated security features
|
||||
4. Improve performance with targeted optimizations
|
||||
5. Create specialized versions for different use cases
|
Reference in New Issue
Block a user