$ show express-rate-limit/express-slow-down
express-slow-down is an Express.js middleware library that rate-limits clients by slowing down responses instead of blocking them outright. It wraps `express-rate-limit` internally, using a custom handler that computes a delay based on request count and configurable options like `delayAfter`, `delayMs`, and `maxDelayMs`. The middleware increments a per-client counter (via a store, defaulting to an in-memory store), and when the count exceeds a threshold, it delays subsequent responses. It supports custom stores, async functions for dynamic delays, and standard/legacy rate-limit headers. The codebase is well-structured TypeScript with comprehensive unit and integration tests, and follows modern ES module and TypeScript practices.
The LLM identified 2 files worth highlighting for reuse:
Provides `expectNoDelay` and `expectDelay` helper functions that mock `setTimeout`, simulate Express request/response objects, and verify whether a middleware applies a delay. The pattern is self-contained, reusable for any Express middleware that uses timers, and demonstrates expert knowledge of timer mocking and event emitter impersonation.
Implements `MockStore` and `MockLegacyStore` that conform to the `express-rate-limit` Store interface, with tracking of method calls. This pattern can be extracted and reused in any project that needs to test custom rate-limit stores, demonstrating proper interface adherence and testability.