@ratelock/local
v0.2.0
Published
In-memory storage backend for RateLock rate limiting system
Maintainers
Readme
@ratelock/local
Zero-dependency, in-memory rate limiting. Perfect for single-process applications.
When to Use
- Single-server or single-process deployments
- Development and testing environments
- When you want zero external dependencies
- When sub-millisecond latency matters
Installation
npm install @ratelock/localQuick Start
Fixed Window
import { fixedWindow } from '@ratelock/local'
const limiter = await fixedWindow({
limit: 100,
windowMs: 60_000, // 1 minute
})
const result = await limiter.check('user:123')
// { allowed: true, remaining: 99, reset: 1716000060000 }Sliding Window
import { slidingWindow } from '@ratelock/local'
const limiter = await slidingWindow({
limit: 100,
windowMs: 60_000,
})Token Bucket
import { tokenBucket } from '@ratelock/local'
const limiter = await tokenBucket({
capacity: 10, // Max burst size
refillRate: 1, // Tokens per second
})Individual Fixed Window
import { individualFixedWindow } from '@ratelock/local'
const limiter = await individualFixedWindow({
limit: 10,
windowMs: 60_000,
})Using the Result
const result = await limiter.check('user:123')
if (!result.allowed) {
return new Response('Too Many Requests', {
status: 429,
headers: {
'Retry-After': String(Math.ceil((result.reset - Date.now()) / 1000)),
},
})
}Batch Checks
const results = await limiter.checkBatch(['user:1', 'user:2', 'user:3'])Cleanup
await limiter.destroy() // Clears internal stateDocumentation
Full API reference and guides at ratelock.vercel.app.
