Repository navigation
rate-limit: v4.1.0
@routup/rate-limit4.1.0 adds response-time counting, window-safe refunds and configurable proxy trust. These notes coverrate-limit-v4.0.3→rate-limit-v4.1.0.
👀 Highlights
🔐 Choose which proxies can supply the client address
The new trustProxy option controls how the default keyGenerator reads X-Forwarded-For. For an app reachable directly, use the connection address so clients cannot choose a fresh quota key by changing that header:
import { App } from 'routup';
import { rateLimit } from '@routup/rate-limit';
const app = new App();
app.use(rateLimit({ trustProxy: false }));Behind your own proxies, you can specify trusted hops, addresses/CIDRs or a predicate. The default remains true (trust all proxies), so existing keys do not change; a custom keyGenerator is unaffected.
⏱️ Count only after the response is known
With countAfterResponse: true, pending requests no longer reserve quota slots. Combine it with skipSuccessfulRequest so slow successful requests, such as long-polls, do not temporarily exhaust a failed-request budget:
app.use('/api', rateLimit({
max: 5,
skipSuccessfulRequest: true,
countAfterResponse: true,
}));The limiter checks the current count before forwarding and counts only responses that are not skipped. Counted responses carry the updated quota state in their headers.
Warning
Concurrent requests can pass before any response is counted, so a burst can exceed max. This mode requires a store with get(); MemoryStore supports it. The default remains reserve-then-refund.
🪟 Refunds stay in the window that counted them
With skipSuccessfulRequest or skipFailedRequest, a slow request can finish after the counter resets. MemoryStore now ignores refunds for an ended window instead of subtracting from the new window's quota. After an ignored refund, the response headers and request rate-limit info reflect the store's current state, rather than a locally calculated decrement.
Custom stores can opt into the same protection by returning an opaque window token from increment() and ignoring decrement(key, window) when that window has ended. Stores without a token retain their existing behaviour.
⬆️ Upgrading
npm install @routup/rate-limit@^4.1.0Review proxy trust for your deployment and implement get() in a custom store before enabling response-time counting.
👉 Changelog
4.1.0 (2026-11-22)
Features
- adopt routup 6.5.0 (#884) (1553a5e)
- rate-limit: keep refunds in their window and add countAfterResponse (65e2538), closes #880
Bug Fixes
- rate-limit: report the store's state after an ignored refund (a0db978)