@wardenjs/ssr-ratelimit
v0.2.0
Published
Sliding-window rate limiting as a decorator for Fetch-API servers, with in-memory and Upstash Redis stores.
Maintainers
Readme
@wardenjs/ssr-ratelimit
Part of warden's server track: security controls as decorators for any Fetch-API handler (Next.js route handlers, Hono, Bun, Deno, Workers). Here SSR means server side, not server-side rendering.
Sliding-window rate limiting as a decorator.
npm i @wardenjs/ssr @wardenjs/ssr-ratelimitimport { memoryStore, rateLimiter, upstashStore } from '@wardenjs/ssr-ratelimit';
const limiter = rateLimiter({
store: process.env.UPSTASH_URL
? upstashStore({ url: process.env.UPSTASH_URL, token: process.env.UPSTASH_TOKEN!, fail: 'closed' })
: memoryStore(),
ip: { trustedHops: 1 }, // proxies that append to X-Forwarded-For
});
export const POST = chain()
.use(limiter.limit({ scope: 'login', limit: 5, windowSeconds: 60 }))
.handle(login);
// Per user instead of per IP, after authentication:
chain().use(session.required()).use(limiter.limit({ scope: 'export', limit: 10, windowSeconds: 3600, key: (_r, c) => c.principal.id }));- Blocks with 429,
Retry-After(never 0) and IETFRateLimit-*headers; allowed responses carry the headers too. memoryStoreis per process and bounded (maxEntries); use a shared store with more than one instance.upstashStoreis atomic (INCRBY+EXPIRE NXin one transaction).failis required:'closed'blocks during an outage (for login),'open'allows.- Any
RateLimitStorewith an atomic increment works. - Configure
ipfor your proxy setup. The left side ofX-Forwarded-Foris client-controlled and never used.
