Skip to content

linklet-io/node-redis-ratelimit-js

 
 

Repository files navigation

Node Redis Rate Limit

npm (scoped) Tests

A fork of @upstash/ratelimit that uses Redis' redis package instead of Upstash's @upstash/redis. For Node.js/Bun/Deno serverful environments. Strips out Upstash specific features - analytics, deny list, etc.

Quick Start

Install

npm

npm install @linklet-io/node-redis-ratelimit

Basic Usage

import { Ratelimit } from "@linklet-io/node-redis-ratelimit";
import { createClient } from "redis";

// Create a new ratelimiter, that allows 10 requests per 10 seconds
const ratelimit = new Ratelimit({
  redis: createClient(),
  limiter: Ratelimit.slidingWindow(10, "10 s"),
  /**
   * Optional prefix for the keys used in redis. This is useful if you want to share a redis
   * instance with other applications and want to avoid key collisions. The default prefix is
   * "@linklet-io/node-redis-ratelimit"
   */
  prefix: "@linklet-io/node-redis-ratelimit",
});

// Use a constant string to limit all requests with a single ratelimit
// Or use a userID, apiKey or ip address for individual limits.
const identifier = "api";
const { success } = await ratelimit.limit(identifier);

if (!success) {
  return "Unable to process at this time";
}
doExpensiveCalculation();
return "Here you go!";

Contributing

Running tests

To run the tests, you will need to have a redis instance. Example using docker:

docker run -p 6379:6379 -it redis/redis-stack-server:latest

Once you have a redis instance, simply run:

bun test

Packages

No packages published

Languages

  • TypeScript 94.2%
  • JavaScript 5.8%