not-too-fast 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +84 -0
- package/algorithm.js +1 -4
- package/main.js +0 -1
- package/package.json +6 -2
package/README.MD
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# your-package-name
|
|
2
|
+
|
|
3
|
+
A lightweight, dependency-free **token bucket** rate limiter middleware for Express.
|
|
4
|
+
|
|
5
|
+
Each client (identified by IP address) gets a bucket of tokens. Every request consumes one token, and tokens refill over time. When the bucket is empty, requests are rejected with `429 Too Many Requests`.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install your-package-name
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import express from "express";
|
|
17
|
+
import { tokenBucketRateLimiter } from "your-package-name";
|
|
18
|
+
|
|
19
|
+
const app = express();
|
|
20
|
+
|
|
21
|
+
app.use(
|
|
22
|
+
tokenBucketRateLimiter({
|
|
23
|
+
limit: 5,
|
|
24
|
+
refreshRate: 10,
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
app.get("/", (req, res) => {
|
|
29
|
+
res.send("Hello World");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
app.listen(3000);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
With the config above, each IP can make a burst of up to **5 requests**, after which one new request is allowed every **10 seconds**.
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
| Option | Type | Description |
|
|
40
|
+
| ------------- | -------- | ------------------------------------------------------------------ |
|
|
41
|
+
| `limit` | `number` | Maximum tokens in the bucket (the largest burst a client can make). |
|
|
42
|
+
| `refreshRate` | `number` | Seconds it takes to refill **one** token. |
|
|
43
|
+
|
|
44
|
+
## How It Works
|
|
45
|
+
|
|
46
|
+
1. A client's first request creates a bucket with `limit` tokens and consumes one.
|
|
47
|
+
2. On each later request, tokens are refilled based on the time elapsed since the last request: `elapsedSeconds / refreshRate`, capped at `limit`.
|
|
48
|
+
3. If at least one full token is available, it is consumed and the request continues.
|
|
49
|
+
4. Otherwise the request is rejected.
|
|
50
|
+
|
|
51
|
+
## Response When Rate Limited
|
|
52
|
+
|
|
53
|
+
Status: `429 Too Many Requests`
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"message": "Too many requests,Try again later",
|
|
58
|
+
"success": false
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Applying to Specific Routes
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
app.use("/api/login", tokenBucketRateLimiter({ limit: 3, refreshRate: 30 }));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Behind a Proxy
|
|
69
|
+
|
|
70
|
+
The limiter identifies clients using `req.ip`. If your app runs behind a reverse proxy or load balancer (Nginx, Cloudflare, Heroku, etc.), enable trust proxy so `req.ip` reflects the real client IP:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
app.set("trust proxy", 1);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Limitations
|
|
77
|
+
|
|
78
|
+
- Buckets are stored **in memory**, so they reset on server restart.
|
|
79
|
+
- Not shared across multiple server instances or processes. For distributed setups, use a shared store such as Redis.
|
|
80
|
+
- Clients are identified by IP only.
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
package/algorithm.js
CHANGED
package/main.js
CHANGED
package/package.json
CHANGED
|
@@ -4,10 +4,14 @@
|
|
|
4
4
|
},
|
|
5
5
|
"type": "module",
|
|
6
6
|
"name": "not-too-fast",
|
|
7
|
-
"version": "1.0.
|
|
7
|
+
"version": "1.0.1",
|
|
8
8
|
"description": "It is a simple rate limiter which uses token bucket rate limiting algorithm to protect an application from the brute for attacks.",
|
|
9
9
|
"main": "index.js",
|
|
10
|
-
"files": [
|
|
10
|
+
"files": [
|
|
11
|
+
"index.js",
|
|
12
|
+
"main.js",
|
|
13
|
+
"algorithm.js"
|
|
14
|
+
],
|
|
11
15
|
"devDependencies": {},
|
|
12
16
|
"scripts": {
|
|
13
17
|
"test": "echo \"Error: no test specified\" && exit 1"
|