Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ezier-project/ratelimit
An ezier ratelimiter for nodejs.
https://github.com/ezier-project/ratelimit
chai ezier mocha nodejs prettier ratelimiter swc typescript
Last synced: about 2 hours ago
JSON representation
An ezier ratelimiter for nodejs.
- Host: GitHub
- URL: https://github.com/ezier-project/ratelimit
- Owner: ezier-project
- License: mit
- Created: 2022-04-02T12:25:08.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T17:31:17.000Z (almost 2 years ago)
- Last Synced: 2024-11-14T19:02:53.568Z (1 day ago)
- Topics: chai, ezier, mocha, nodejs, prettier, ratelimiter, swc, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/@ezier/ratelimit
- Size: 226 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Ezier Ratelimiter
An ezier ratelimiter for nodejs.
![npm bundle size](https://img.shields.io/bundlephobia/min/@ezier/ratelimit?style=for-the-badge) ![npm](https://img.shields.io/npm/dm/@ezier/ratelimit?style=for-the-badge) ![NPM](https://img.shields.io/npm/l/@ezier/ratelimit?style=for-the-badge) ![npm](https://img.shields.io/npm/v/@ezier/ratelimit?style=for-the-badge)
# Why?
**This rate-limiter attempts to keep it e-z, with no additional functions.**
**All in all a basic package to rate-limit your clients.**
***Also used in the Fronvo [server](https://github.com/Fronvo/fronvo)***
# Installing
```
npm i @ezier/ratelimit
```# Documentation
**Documentation for the Ezier Ratelimiter can be found at https://ezier-project.github.io/ratelimit/.**# Examples
**Setup an instance of `EzierLimiter`:**
```ts
import { EzierLimiter } from '@ezier/ratelimit';const ezierLimiter = new EzierLimiter({
maxPoints: 10,
clearDelay: 1000
});
```**Consume points for a client:**
```ts
ezierLimiter.consumePoints('client-uid', 5);
```**Handle a consumption error:**
```ts
import { EzierLimiterError } from '@ezier/ratelimit';ezierLimiter.consumePoints('client-uid', 11)
.catch((err: EzierLimiterError) => {
// EzierLimiterError check
if(err.currentPoints) {
console.log(`Client requested ${err.requestedPoints} points when it has ${err.currentPoints} points and maxPoints are ${err.maxPoints}.`);
} else {
console.log(`[${err.name}]: ${err.message}`);
}
});
```**Register middleware:**
```ts
ezierLimiter.$use({
beforeConsumption: ({consumerKey, requestedPoints}) => {
console.log(`Attempting to consume ${requestedPoints} points for ${consumerKey}...`)
},afterConsumption: ({consumerKey, remainingPoints}) => {
console.log(`[${consumerKey}]: ${remainingPoints} points remaining.`);
}
});
```**Stop the rate-limiter:**
```ts
ezierLimiter.stop()
.then(() => {
console.log('The Ezier Ratelimiter has been stopped.');
});
```Made by [Shadofer](https://github.com/shadofer) with joy.