not-too-fast 0.0.0-stage → 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 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 ADDED
@@ -0,0 +1,40 @@
1
+ const buckets = new Map();
2
+
3
+
4
+
5
+
6
+
7
+ export function tokenBucketAlgorithm(ip,Limit,refreshRate) {
8
+ let bucket = buckets.get(ip);
9
+
10
+ if (!bucket) {
11
+ bucket = {
12
+ tokens: Limit,
13
+ last_time: Date.now(),
14
+ };
15
+
16
+ bucket.tokens -= 1;
17
+
18
+ buckets.set(ip, bucket);
19
+
20
+ return true;
21
+ }
22
+
23
+ const elapsedTime = (Date.now() - bucket.last_time) / 1000;
24
+
25
+ const new_tokens = elapsedTime / refreshRate;
26
+
27
+ bucket.tokens = Math.min(Limit, bucket.tokens + new_tokens);
28
+
29
+ if (bucket.tokens <= 0) {
30
+ buckets.set(ip, bucket);
31
+ return false;
32
+ }
33
+
34
+ bucket.tokens -= 1;
35
+ bucket.last_time = Date.now();
36
+
37
+ buckets.set(ip, bucket);
38
+
39
+ return true;
40
+ }
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { tokenBucketRateLimiter } from "./main.js";
2
+ export { tokenBucketRateLimiter } ;
3
+
4
+
5
+ app.use(
6
+ tokenBucketRateLimiter({
7
+ limit: 5,
8
+ refreshRate: 10,
9
+ })
10
+ );
11
+
package/main.js ADDED
@@ -0,0 +1,28 @@
1
+ import { tokenBucketAlgorithm } from "./algorithm.js";
2
+
3
+
4
+ function rateLimiter(limit,refreshRate) {
5
+
6
+ return (req,res,next)=>{
7
+
8
+ const ip = req.ip;
9
+
10
+ const allowed = tokenBucketAlgorithm(ip,limit,refreshRate );
11
+
12
+ if (!allowed) {
13
+ return res.status(429).json({
14
+ message: "Too many requests,Try again later",
15
+ success: false,
16
+ });
17
+ }
18
+
19
+ next();
20
+ }
21
+ }
22
+
23
+
24
+ export function tokenBucketRateLimiter({limit,refreshRate}){
25
+
26
+ return rateLimiter(limit,refreshRate);
27
+
28
+ }
package/package.json CHANGED
@@ -1,6 +1,22 @@
1
1
  {
2
+ "dependencies": {
3
+ "express": "^5.3.0"
4
+ },
5
+ "type": "module",
2
6
  "name": "not-too-fast",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
7
+ "version": "1.0.1",
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
+ "main": "index.js",
10
+ "files": [
11
+ "index.js",
12
+ "main.js",
13
+ "algorithm.js"
14
+ ],
15
+ "devDependencies": {},
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1"
18
+ },
19
+ "keywords": [],
20
+ "author": "",
21
+ "license": "ISC"
22
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.