@jfungus/ratelimit-unstorage
Storage adapter using unstorage - connect to Redis, Cloudflare KV, Vercel KV, and many more backends.
Installation
npm install @jfungus/ratelimit-unstorage unstorageWhen to Use
- Multiple server instances - Share rate limit state across servers
- Serverless - Persist state between cold starts
- Edge deployments - Use Cloudflare KV, Vercel KV, etc.
For single-instance deployments, the built-in MemoryStore is simpler and faster.
Basic Usage
import { createStorage } from "unstorage";import { createUnstorageStore } from "@jfungus/ratelimit-unstorage";
const storage = createStorage(); // Memory by defaultconst store = createUnstorageStore({ storage });
// Use with any framework adapterrateLimiter({ limit: 100, windowMs: 60 * 1000, store }); // 1 minuteRedis
import { createStorage } from "unstorage";import redisDriver from "unstorage/drivers/redis";import { createUnstorageStore } from "@jfungus/ratelimit-unstorage";
const storage = createStorage({ driver: redisDriver({ url: process.env.REDIS_URL, // redis://localhost:6379 }),});
const store = createUnstorageStore({ storage });Cloudflare KV
import { createStorage } from "unstorage";import cloudflareKVBindingDriver from "unstorage/drivers/cloudflare-kv-binding";import { createUnstorageStore } from "@jfungus/ratelimit-unstorage";
const storage = createStorage({ driver: cloudflareKVBindingDriver({ binding: "RATE_LIMIT_KV", }),});
const store = createUnstorageStore({ storage });Vercel KV
import { createStorage } from "unstorage";import vercelKVDriver from "unstorage/drivers/vercel-kv";import { createUnstorageStore } from "@jfungus/ratelimit-unstorage";
const storage = createStorage({ driver: vercelKVDriver(),});
const store = createUnstorageStore({ storage });Options
| Option | Type | Default | Description |
|---|---|---|---|
storage | Storage | Required | unstorage instance |
prefix | string | 'ratelimit:' | Key prefix |
const store = createUnstorageStore({ storage, prefix: "myapp:ratelimit:", // Custom prefix});Nuxt Helper
For Nuxt applications using useStorage():
import { createNuxtStore } from "@jfungus/ratelimit-unstorage";
// In a Nuxt server route or middlewareconst store = createNuxtStore(useStorage());
// With custom prefixconst store = createNuxtStore(useStorage(), "myapp:ratelimit:");Supported Drivers
Any unstorage driver works:
- Redis
- Cloudflare KV
- Vercel KV
- MongoDB
- Planetscale
- Upstash
- Memory
- Filesystem
- And many more…
Related
- unstorage Documentation
- Stores - Store interface details
- @jfungus/ratelimit - Core library