0.0.8 • Published 2 months ago

@rlimit/storage v0.0.8

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

rlimit.com rate limiting storage

A distributed rate limiting store for the hono-rate-limiter and express-rate-limit middleware.

Prerequisites

Sign up for a free account at rlimit.com and create a namespace, or use example as namespace ID to test the functionality.

Installation

From the npm registry:

# Using npm
> npm install @rlimit/storage
# Using yarn or pnpm
> yarn/pnpm add @rlimit/storage

Usage

Importing

This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects.

This package requires you to use Node 16 or above.

Import it in a CommonJS project (type: commonjs or no type field in package.json) as follows:

const { Store } = require("@rlimit/storage");

Import it in a ESM project (type: module in package.json) as follows:

import { Store } from "@rlimit/storage";

Examples

To use it with a hono-rate-limiter middleware:

Interactive demo on ratelimit.new/hono-rate-limiter

import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { Store } from "@rlimit/storage";

import honoRateLimiter from "hono-rate-limiter";

const limiter = honoRateLimiter.rateLimiter({
  windowMs: 60 * 1000, // 1 minute
  limit: 20, // Limit each key (from keyGenerator) to 20 requests per `window` (here, per 1 minute)
  standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
  keyGenerator: () => "<unique_key>", // Method to generate custom identifiers for clients, default to IP address
  store: new Store({
    namespace: "example", // your rlimit.com namespace ID
  }),
});

const app = new Hono();

app.use(limiter);

app.get("/", (c) => {
  // your application logic here
  return c.json({ message: "Hello Hono!" });
});

serve(app);

To use it with a express-rate-limit middleware:

Interactive demo on rlimit.com/demos/nodejs-express

import express from "express";
import { rateLimit } from "express-rate-limit";
import { Store } from "@rlimit/storage";

const app = express();

let rlimit = rateLimit({
  // limit IP address to 3 requests every 5 seconds
  limit: 3,
  windowMs: 5_000,
  store: new Store({
    namespace: "example", // your rlimit.com namespace ID
  }),
});

app.use(rlimit);
app.get("/", async (req, res) => {
  res.send("Hello World!");
});

app.listen(3000, () => {
  console.log("Express application started");
});
0.0.8

2 months ago

0.0.5

2 months ago

0.0.7

2 months ago

0.0.6

2 months ago

0.0.3

2 months ago

0.0.4

2 months ago

0.0.2

2 months ago

0.0.1

2 months ago

0.0.0

2 months ago