mirror of
https://github.com/bitbaum/limitkit.git
synced 2026-12-04 05:48:24 +01:00
Twelve near-identical rate limiters existed across nine repos of this fleet.
orangecat alone carried four, and its ADR-0002 to unify them sat "Proposed" for
seven months while the count doubled — the measured proof that writing a plan
down is not the same as extracting the code.
What actually varied between the twelve was ONE thing: where the counts live
(eleven in process memory, one in Upstash). Everything else — window
arithmetic, refusal shape, headers, client-IP extraction — was the same idea
written twelve slightly different ways, with independently re-invented bugs.
So the algorithm is fixed and the store is the seam:
slidingWindow / fixedWindow pure over store + clock. Both shapes shipped in
the fleet, and they refuse differently; the
choice is now named at the call site instead of
implied by whichever file got copied.
Store / MemoryStore two methods. The default is BOUNDED (LRU past
maxKeys): one replaced implementation kept a
bare Map keyed by stranger IPs with no eviction
— a slow memory leak fed by the public
internet, now impossible by construction.
toHeaders / clientIp the HTTP edges everyone also rewrote. Standard
X-RateLimit-* trio, Retry-After on refusals
only — exactly what orangecat's ADR specified.
Two decisions worth naming:
A REFUSAL COUNTS NOTHING. Several replaced implementations let refused
requests extend the window, so a scripted attacker hammering a key locked
the legitimate user behind the same NAT out indefinitely. Here a hammered
key recovers the moment the attacker stops; there is a test for exactly
this shape.
retryAfterSeconds IS DERIVED, never fabricated — computed from when the
oldest counted hit ages out. "Try again shortly" on a window that opens in
an hour is the same lie ai-ration's 429 classifier exists to prevent, one
layer down.
Deliberately not included, per SHARED.md's extraction rules: no HTTP client,
no middleware, no framework types, no distributed store (the seam is two
methods; your Redis is your ops posture), and NO LIMIT VALUES — how many
attempts a login route allows is app semantics, asserted locally.
Every function takes `now` as an argument, so all of it is testable without
sleeping — which is why this package has 14 tests and the twelve files it
replaces, collectively, had almost none. Tests import the package BY NAME, so
the exports map is exercised on every run, not at the first consumer's
install.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
90 lines
3.8 KiB
JavaScript
90 lines
3.8 KiB
JavaScript
/**
|
|
* The clock is an argument everywhere, so every case below proves its claim
|
|
* without sleeping — the thing none of the twelve replaced implementations
|
|
* could do, which is why none of them had tests.
|
|
*/
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { slidingWindow, fixedWindow, MemoryStore } from 'limitkit';
|
|
|
|
const RULE = { limit: 3, windowMs: 60_000 };
|
|
const T0 = 1_000_000;
|
|
|
|
test('sliding: allows up to the limit, then refuses', () => {
|
|
const rl = slidingWindow(RULE);
|
|
assert.equal(rl.check('k', T0).allowed, true);
|
|
assert.equal(rl.check('k', T0 + 1).allowed, true);
|
|
assert.equal(rl.check('k', T0 + 2).allowed, true);
|
|
const refused = rl.check('k', T0 + 3);
|
|
assert.equal(refused.allowed, false);
|
|
assert.equal(refused.remaining, 0);
|
|
});
|
|
|
|
test('sliding: the refusal names WHEN, derived from the oldest hit', () => {
|
|
const rl = slidingWindow(RULE);
|
|
rl.check('k', T0); rl.check('k', T0 + 10_000); rl.check('k', T0 + 20_000);
|
|
const refused = rl.check('k', T0 + 30_000);
|
|
// The oldest hit (T0) ages out at T0+60s; from T0+30s that is 30s away.
|
|
assert.equal(refused.retryAfterSeconds, 30);
|
|
assert.equal(refused.resetAt, T0 + 60_000);
|
|
});
|
|
|
|
test('sliding: the window actually slides — old hits age out one by one', () => {
|
|
const rl = slidingWindow(RULE);
|
|
rl.check('k', T0); rl.check('k', T0 + 10_000); rl.check('k', T0 + 20_000);
|
|
// At T0+61s the first hit has aged out: exactly one slot free.
|
|
assert.equal(rl.check('k', T0 + 61_000).allowed, true);
|
|
assert.equal(rl.check('k', T0 + 61_001).allowed, false);
|
|
});
|
|
|
|
test('A REFUSAL COUNTS NOTHING — hammering cannot extend the lockout', () => {
|
|
// The failure this prevents: an attacker retrying in a loop keeps the
|
|
// window eternally full, and the legitimate user behind the same NAT never
|
|
// gets back in. Refusals must not feed the counter.
|
|
const rl = slidingWindow(RULE);
|
|
rl.check('k', T0); rl.check('k', T0 + 1); rl.check('k', T0 + 2);
|
|
for (let i = 0; i < 50; i++) rl.check('k', T0 + 10_000 + i);
|
|
// All three real hits age out at T0+60_002 regardless of the hammering.
|
|
assert.equal(rl.check('k', T0 + 61_000).allowed, true);
|
|
});
|
|
|
|
test('keys are independent', () => {
|
|
const rl = slidingWindow(RULE);
|
|
rl.check('a', T0); rl.check('a', T0); rl.check('a', T0);
|
|
assert.equal(rl.check('a', T0 + 1).allowed, false);
|
|
assert.equal(rl.check('b', T0 + 1).allowed, true);
|
|
});
|
|
|
|
test('peek decides without counting', () => {
|
|
const rl = slidingWindow(RULE);
|
|
for (let i = 0; i < 10; i++) rl.peek('k', T0 + i);
|
|
assert.equal(rl.check('k', T0 + 11).allowed, true, 'peeks must not consume the allowance');
|
|
});
|
|
|
|
test('fixed: the whole allowance returns when the bucket rolls', () => {
|
|
const rl = fixedWindow(RULE);
|
|
const start = 1_200_000; // aligned: divisible by 60_000
|
|
rl.check('k', start); rl.check('k', start + 1); rl.check('k', start + 2);
|
|
assert.equal(rl.check('k', start + 3).allowed, false);
|
|
assert.equal(rl.check('k', start + 60_000).allowed, true, 'new bucket, fresh allowance');
|
|
});
|
|
|
|
test('fixed: refusal points at the bucket boundary', () => {
|
|
const rl = fixedWindow(RULE);
|
|
const start = 1_200_000;
|
|
rl.check('k', start); rl.check('k', start); rl.check('k', start);
|
|
const refused = rl.check('k', start + 45_000);
|
|
assert.equal(refused.retryAfterSeconds, 15);
|
|
});
|
|
|
|
test('a shared store serves multiple limiters without cross-talk', () => {
|
|
const store = new MemoryStore();
|
|
const login = slidingWindow({ limit: 1, windowMs: 60_000 }, store);
|
|
const search = slidingWindow({ limit: 5, windowMs: 60_000 }, store);
|
|
// Same underlying store, DIFFERENT keys per concern — the app namespaces.
|
|
assert.equal(login.check('login:1.2.3.4', T0).allowed, true);
|
|
assert.equal(login.check('login:1.2.3.4', T0 + 1).allowed, false);
|
|
assert.equal(search.check('search:1.2.3.4', T0 + 1).allowed, true);
|
|
});
|