Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+90
@@ -0,0 +1,90 @@
|
||||
import { MemoryAdapter } from "../adapters/memory.js";
|
||||
import type { SharedStore } from "../factory.js";
|
||||
|
||||
export interface FixedWindowRateLimiterOptions {
|
||||
store: SharedStore;
|
||||
/** Sub-namespace for this limiter (e.g. `"login-ip"`). */
|
||||
prefix: string;
|
||||
/** Window length in seconds. */
|
||||
windowSeconds: number;
|
||||
/** Allowed actions per window. */
|
||||
max: number;
|
||||
}
|
||||
|
||||
export interface FixedWindowDecision {
|
||||
/** True when the request is below the cap. */
|
||||
allowed: boolean;
|
||||
/** How many actions are still permitted in the current window. */
|
||||
remaining: number;
|
||||
/** Wall-clock time when the current window resets. */
|
||||
resetAt: Date;
|
||||
}
|
||||
|
||||
export interface FixedWindowRateLimiter {
|
||||
consume(key: string): Promise<FixedWindowDecision>;
|
||||
/** Test/admin helper: drop the counter for a key. */
|
||||
reset(key: string): Promise<void>;
|
||||
}
|
||||
|
||||
const FIXED_WINDOW_SCRIPT = `
|
||||
local current = redis.call('INCR', KEYS[1])
|
||||
if current == 1 then
|
||||
redis.call('EXPIRE', KEYS[1], tonumber(ARGV[1]))
|
||||
end
|
||||
local ttl = redis.call('TTL', KEYS[1])
|
||||
return { current, ttl }
|
||||
`;
|
||||
|
||||
// Register the memory-adapter equivalent so the same eval() call works
|
||||
// against both backends. The memory adapter resolves scripts by exact
|
||||
// string match.
|
||||
MemoryAdapter.registerScript(FIXED_WINDOW_SCRIPT, async (adapter, keys, args) => {
|
||||
const [key] = keys;
|
||||
const window = Number.parseInt(args[0] ?? "0", 10);
|
||||
const current = await adapter.incr(key);
|
||||
if (current === 1 && window > 0) {
|
||||
await adapter.expire(key, window);
|
||||
}
|
||||
// We do not implement TTL command on the adapter surface; the helper
|
||||
// computes resetAt locally for the memory case.
|
||||
return [current, window];
|
||||
});
|
||||
|
||||
/**
|
||||
* Create a Redis-style fixed-window rate limiter. The window starts on
|
||||
* the first `consume()` call and resets after `windowSeconds`. The
|
||||
* decision is computed atomically in Redis via Lua (and sequentially,
|
||||
* race-free, in the memory adapter).
|
||||
*/
|
||||
export function createFixedWindowRateLimiter(options: FixedWindowRateLimiterOptions): FixedWindowRateLimiter {
|
||||
if (options.windowSeconds <= 0) {
|
||||
throw new Error("createFixedWindowRateLimiter: windowSeconds must be > 0");
|
||||
}
|
||||
if (options.max <= 0) {
|
||||
throw new Error("createFixedWindowRateLimiter: max must be > 0");
|
||||
}
|
||||
|
||||
const store = options.store;
|
||||
const bucketKey = (key: string) => `rl:fixed:${options.prefix}:${key}`;
|
||||
|
||||
return {
|
||||
async consume(key) {
|
||||
const fullKey = store.key(bucketKey(key));
|
||||
const result = (await store.adapter.eval(FIXED_WINDOW_SCRIPT, [fullKey], [String(options.windowSeconds)])) as
|
||||
| [number | string, number | string]
|
||||
| undefined;
|
||||
const current = Number(result?.[0] ?? 0);
|
||||
const ttlReported = Number(result?.[1] ?? options.windowSeconds);
|
||||
const ttl = ttlReported > 0 ? ttlReported : options.windowSeconds;
|
||||
const remaining = Math.max(0, options.max - current);
|
||||
return {
|
||||
allowed: current <= options.max,
|
||||
remaining,
|
||||
resetAt: new Date(Date.now() + ttl * 1000),
|
||||
};
|
||||
},
|
||||
async reset(key) {
|
||||
await store.del(bucketKey(key));
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user