Skip to content

@jfungus/ratelimit-unstorage

Storage adapter using unstorage - connect to Redis, Cloudflare KV, Vercel KV, and many more backends.

Installation

Terminal window
npm install @jfungus/ratelimit-unstorage unstorage

When 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 default
const store = createUnstorageStore({ storage });
// Use with any framework adapter
rateLimiter({ limit: 100, windowMs: 60 * 1000, store }); // 1 minute

Redis

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

OptionTypeDefaultDescription
storageStorageRequiredunstorage instance
prefixstring'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 middleware
const store = createNuxtStore(useStorage());
// With custom prefix
const store = createNuxtStore(useStorage(), "myapp:ratelimit:");

Supported Drivers

Any unstorage driver works:

  • Redis
  • Cloudflare KV
  • Vercel KV
  • MongoDB
  • Planetscale
  • Upstash
  • Memory
  • Filesystem
  • And many more…