@abhinavsrivastava_119/rateflow-core
v1.0.9
Published
A high-performance, zero-dependency rate limiting library for Node.js and the browser.
Maintainers
Readme
RateFlow 🛡️
A high-performance, zero-dependency rate-limiting library for Node.js and modern browsers. Built with TypeScript for type safety and predictability.
Features
- ⚡ Zero Dependencies: Lightweight and fast.
- 🔄 Multiple Algorithms:
- Token Bucket: Handle conflicting traffic with burst allowances.
- Leaky Bucket: Smooth out traffic spikes into a constant flow.
- Sliding Window: Precise, time-based limiting without window boundary issues.
- 🟦 TypeScript Native: Full type definitions include.
- 🖥️ Dashboard Included: Visualize and debug your rate limits with the included interactive dashboard.
Installation
npm install @rateflow/coreQuick Start
import { RateFlow } from '@rateflow/core';
// Create a sliding window limiter
const limiter = RateFlow.create({
type: 'sliding-window',
options: {
windowSizeInMs: 1000,
maxRequests: 5
}
});
// Use it
if (limiter.take()) {
console.log("Request allowed");
} else {
console.log("Rate limit exceeded");
}Algorithms
Token Bucket
Ideal for bursty traffic. Allows a specified number of tokens to be used instantly, with tokens replenishing at a fixed rate.
Leaky Bucket
Maintains a constant outflow rate. Perfect for smoothing out traffic and ensuring a steady load on upstream services.
Sliding Window
The most precise approach. Prevents "edge cases" where traffic spikes occur at the boundary of a fixed window by tracking actual timestamps.
Dashboard
The project includes a premium dashboard for exploring the algorithms. To run it locally:
npm install
npm run dev:dashboard