Architecture Overview โ
The get-cookie library follows a well-structured, modular architecture designed for maintainability, extensibility, and performance.
Core Design Patterns โ
Strategy Pattern โ
The codebase implements the Strategy Pattern for browser-specific cookie extraction:
BaseCookieQueryStrategy (Abstract)
โโโ BaseChromiumCookieQueryStrategy
โ โโโ ChromeCookieQueryStrategy
โ โโโ EdgeCookieQueryStrategy
โ โโโ ArcCookieQueryStrategy
โ โโโ OperaCookieQueryStrategy
โ โโโ OperaGXCookieQueryStrategy
โโโ FirefoxCookieQueryStrategy
โโโ SafariCookieQueryStrategy
โโโ CompositeCookieQueryStrategy
Each strategy encapsulates browser-specific logic while maintaining a consistent interface.
Factory Pattern โ
CookieStrategyFactory creates appropriate strategies based on:
- Browser type detection
- Cookie store file analysis
- Automatic fallback to composite strategy
Composite Pattern โ
CompositeCookieQueryStrategy aggregates multiple strategies to query across all browsers simultaneously.
SQL Infrastructure โ
The library includes a sophisticated SQL layer for database operations:
DatabaseConnectionManager โ
- Connection Pooling: Maintains reusable database connections
- Lifecycle Management: Handles connection creation, reuse, and cleanup
- Retry Logic: Automatic retry on database lock conditions
- Thread Safety: Ensures safe concurrent access
QueryMonitor โ
- Performance Tracking: Monitors query execution times
- Slow Query Detection: Identifies performance bottlenecks
- Metrics Collection: Gathers statistics for optimization
- Debug Logging: Detailed query logging for troubleshooting
CookieQueryBuilder โ
- Type-Safe Construction: Strongly typed query building
- SQL Injection Prevention: Parameterized queries and validation
- Index Optimization: Leverages database indexes for performance
- Cross-Platform SQL: Handles platform-specific SQL variations
BrowserLockHandler โ
- Lock Detection: Identifies when browsers have locked databases
- Graceful Retry: Exponential backoff for locked resources
- Process Detection: Checks if browsers are running
- Error Recovery: Intelligent error handling and recovery
Platform-Specific Implementations โ
macOS โ
- Keychain Integration: Secure password retrieval from macOS Keychain
- Container Access: Safari container permission handling
- Binary Cookie Decoder: Custom parser for Safari's binary format
Windows โ
- DPAPI Integration: Windows Data Protection API for Chrome/Edge
- Registry Access: Browser installation detection
- Multi-Variant Support: Firefox Developer Edition, ESR variants
Linux โ
- Keyring Support: libsecret integration for Chrome passwords
- XDG Compliance: Follows XDG base directory specification
- Fallback Mechanisms: Hardcoded keys when keyring unavailable
Key Components โ
Core Browser Module (src/core/browsers/
) โ
- Strategy Implementations: Browser-specific query strategies
- SQL Utilities: Database connection and query management
- Platform Controls: OS-specific browser management
- Lock Handling: Database lock detection and retry
CLI Module (src/cli/
) โ
- Command Interface: Argument parsing and validation
- Output Handlers: JSON, rendered, and dump formats
- Service Layer: Cookie query orchestration
- Error Reporting: User-friendly error messages
Utilities (src/utils/
) โ
- Process Detection: Check running browser processes
- Platform Utils: OS detection and path resolution
- Date Handling: Chrome timestamp conversion
- Encryption: Cookie decryption utilities
- Error Utils: Standardized error handling
- JWT Validation: Token parsing and validation
Type Definitions (src/types/
) โ
- Zod Schemas: Runtime validation schemas
- TypeScript Types: Compile-time type safety
- Browser Types: Browser-specific type definitions
- API Contracts: Public API type definitions
Data Flow โ
Request Initiation
- CLI command or API call with cookie query parameters
Strategy Selection
- Factory creates appropriate strategy based on browser parameter
- Falls back to composite strategy for multi-browser queries
Cookie Extraction
- Strategy locates browser cookie database
- Handles platform-specific encryption/decryption
- Manages database locking and retries
Query Execution
- SQL query built with CookieQueryBuilder
- Connection obtained from DatabaseConnectionManager
- Query monitored by QueryMonitor
Result Processing
- Raw cookies transformed to ExportedCookie format
- Metadata attached (browser, file, decryption status)
- Results filtered and formatted
Output Rendering
- Output handler formats results based on CLI flags
- Results returned to user or written to stdout
Performance Optimizations โ
Database Access โ
- Connection Pooling: Reuses database connections
- Indexed Queries: Uses database indexes for fast lookups
- Batch Processing: Processes multiple profiles efficiently
- Lazy Loading: Only loads necessary data
Memory Management โ
- Streaming: Processes large result sets without loading all into memory
- Resource Cleanup: Ensures connections and files are properly closed
- Garbage Collection: Efficient memory usage patterns
Error Handling โ
- Graceful Degradation: Continues operation when individual browsers fail
- Retry Mechanisms: Automatic retry for transient failures
- Detailed Logging: Debug-level logging for troubleshooting
- User-Friendly Errors: Clear error messages for common issues
Security Considerations โ
Encryption Handling โ
- Platform-Native: Uses OS-provided encryption mechanisms
- No Key Storage: Never stores or logs encryption keys
- Secure Memory: Clears sensitive data after use
Permission Model โ
- Least Privilege: Only requests necessary permissions
- Sandboxing: Respects browser sandbox boundaries
- Read-Only: Never modifies browser data
Data Protection โ
- No Network Access: Purely local operations
- No Data Collection: No telemetry or analytics
- Secure Defaults: Safe default configurations
Extensibility โ
Adding New Browsers โ
- Extend
BaseCookieQueryStrategy
orBaseChromiumCookieQueryStrategy
- Implement browser-specific logic
- Register in
CookieStrategyFactory
- Add tests and documentation
Custom Output Formats โ
- Implement
OutputHandler
interface - Register in output handler factory
- Add CLI flag support
Platform Extensions โ
- Add platform detection in
platformUtils
- Implement platform-specific paths
- Add encryption/decryption support
- Update browser strategies
Testing Architecture โ
Unit Tests โ
- Strategy Tests: Individual browser strategy testing
- Utility Tests: Platform and encryption utility testing
- Mock Implementations: MockCookieQueryStrategy for testing
Integration Tests โ
- Database Tests: SQLite operation testing
- Encryption Tests: Platform-specific encryption testing
- CLI Tests: Command-line interface testing
Test Infrastructure โ
- Jest Configuration: TypeScript support via ts-jest
- Single Worker Mode: Prevents SQLite concurrency issues
- Fixture Data: Real-world cookie database samples
- Mock Factories: Consistent test data generation