not-too-fast 0.0.0-stage → 1.0.0

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/algorithm.js ADDED
@@ -0,0 +1,43 @@
1
+ const buckets = new Map();
2
+
3
+ const Limit = 5;
4
+ const refreshRate = 10;
5
+
6
+
7
+
8
+
9
+
10
+ export function tokenBucketAlgorithm(ip) {
11
+ let bucket = buckets.get(ip);
12
+
13
+ if (!bucket) {
14
+ bucket = {
15
+ tokens: Limit,
16
+ last_time: Date.now(),
17
+ };
18
+
19
+ bucket.tokens -= 1;
20
+
21
+ buckets.set(ip, bucket);
22
+
23
+ return true;
24
+ }
25
+
26
+ const elapsedTime = (Date.now() - bucket.last_time) / 1000;
27
+
28
+ const new_tokens = elapsedTime / refreshRate;
29
+
30
+ bucket.tokens = Math.min(Limit, bucket.tokens + new_tokens);
31
+
32
+ if (bucket.tokens <= 0) {
33
+ buckets.set(ip, bucket);
34
+ return false;
35
+ }
36
+
37
+ bucket.tokens -= 1;
38
+ bucket.last_time = Date.now();
39
+
40
+ buckets.set(ip, bucket);
41
+
42
+ return true;
43
+ }
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,29 @@
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
+
20
+ next();
21
+ }
22
+ }
23
+
24
+
25
+ export function tokenBucketRateLimiter({limit,refreshRate}){
26
+
27
+ return rateLimiter(limit,refreshRate);
28
+
29
+ }
package/package.json CHANGED
@@ -1,6 +1,18 @@
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.0",
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": ["index.js","main.js","algorithm.js"],
11
+ "devDependencies": {},
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "keywords": [],
16
+ "author": "",
17
+ "license": "ISC"
18
+ }
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.