@the-node-forge/api-rate-limit 1.0.7
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/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/RateLimiter.d.ts +12 -0
- package/dist/RateLimiter.js +21 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +10 -0
- package/dist/middleware.d.ts +3 -0
- package/dist/middleware.js +18 -0
- package/package.json +83 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 The Node Forge
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
<div align="center">
|
2
|
+
|
3
|
+
# API Rate Limiter
|
4
|
+
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
6
|
+
|
7
|
+

|
8
|
+
|
9
|
+
[](https://www.npmjs.com/package/@the-node-forge/api-rate-limit)
|
10
|
+
[](https://github.com/The-Node-Forge/api-rate-limit/actions)
|
11
|
+

|
12
|
+
|
13
|
+
[Live Documentation](https://the-node-forge.github.io/api-rate-limit/)
|
14
|
+
|
15
|
+
</div>
|
16
|
+
|
17
|
+
A **simple and efficient API rate limiter** for JavaScript/TypeScript applications. This package helps developers **limit API requests per user**, preventing abuse and excessive usage.
|
18
|
+
|
19
|
+
## ✨ Features
|
20
|
+
|
21
|
+
- ✅ **Configurable Limits** – Set max requests per second, minute, or hour.
|
22
|
+
- ✅ **In-Memory Storage** – Fast and lightweight.
|
23
|
+
- ✅ **Express Middleware** – Easily integrate with Express.
|
24
|
+
- ✅ **Multiple Users** – Tracks each user separately.
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## 📦 Installation
|
29
|
+
|
30
|
+
```sh
|
31
|
+
npm install @the-node-forge/api-rate-limit
|
32
|
+
```
|
33
|
+
|
34
|
+
or using Yarn:
|
35
|
+
|
36
|
+
```sh
|
37
|
+
yarn add @the-node-forge/api-rate-limit
|
38
|
+
```
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
## 🛠️ Basic Usage
|
43
|
+
|
44
|
+
### **1️⃣ 🌐 JavaScript/TypeScript Example**
|
45
|
+
|
46
|
+
Easily integrate with **JavaScript/TypeScript**.
|
47
|
+
|
48
|
+
```javascript
|
49
|
+
// Javascript
|
50
|
+
import RateLimiter from '@the-node-forge/api-rate-limit';
|
51
|
+
|
52
|
+
// Create a rate limiter allowing 5 requests per minute
|
53
|
+
const limiter = new RateLimiter({ windowMs: 60000, maxRequests: 5 });
|
54
|
+
|
55
|
+
const userId = 'user123'; // Could be an API key or IP
|
56
|
+
|
57
|
+
if (limiter.isAllowed(userId)) {
|
58
|
+
console.log('Request allowed ✅');
|
59
|
+
} else {
|
60
|
+
console.log('Too many requests ❌');
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
---
|
65
|
+
|
66
|
+
### 2️⃣ 🌐 Express Middleware Example - Global
|
67
|
+
|
68
|
+
Easily integrate with an **Express API**.
|
69
|
+
|
70
|
+
```typescript
|
71
|
+
import express from 'express';
|
72
|
+
import { RateLimiter, rateLimitMiddleware } from '@the-node-forge/api-rate-limit';
|
73
|
+
|
74
|
+
const app = express();
|
75
|
+
|
76
|
+
// Set up rate limiter (10 requests per minute)
|
77
|
+
const limiter = new RateLimiter({ windowMs: 60000, maxRequests: 10 });
|
78
|
+
|
79
|
+
// Apply middleware globally
|
80
|
+
app.use(rateLimitMiddleware(limiter));
|
81
|
+
|
82
|
+
app.get('/', (req, res) => {
|
83
|
+
res.send('Welcome to my API!');
|
84
|
+
});
|
85
|
+
|
86
|
+
app.listen(3000, () => console.log('Server running on port 3000'));
|
87
|
+
```
|
88
|
+
|
89
|
+
---
|
90
|
+
|
91
|
+
### 3️⃣ 🌐 Express Middleware Example - Specific
|
92
|
+
|
93
|
+
Easily integrate with an **Express API**.
|
94
|
+
|
95
|
+
```typescript
|
96
|
+
import express from 'express';
|
97
|
+
import { RateLimiter, rateLimitMiddleware } from '@the-node-forge/api-rate-limit';
|
98
|
+
|
99
|
+
const app = express();
|
100
|
+
|
101
|
+
const limiterFiveReq = new RateLimiter({ windowMs: 60000, maxRequests: 5 });
|
102
|
+
const limiterTenReq = new RateLimiter({ windowMs: 60000, maxRequests: 10 });
|
103
|
+
|
104
|
+
// Apply rate limiting to only specific routes
|
105
|
+
app.get('/public', (req, res) => {
|
106
|
+
res.send('This route has no rate limiting!');
|
107
|
+
});
|
108
|
+
|
109
|
+
// Apply rate limiting only to this route
|
110
|
+
app.get('/limited', rateLimitMiddleware(limiterFiveReq), (req, res) => {
|
111
|
+
res.send('This route is rate-limited to 5 requests!');
|
112
|
+
});
|
113
|
+
|
114
|
+
app.post('/submit', rateLimitMiddleware(limiterTenReq), (req, res) => {
|
115
|
+
res.send('This POST request is also rate-limited to ten requests!');
|
116
|
+
});
|
117
|
+
|
118
|
+
app.listen(3000, () => console.log('Server running on port 3000'));
|
119
|
+
```
|
120
|
+
|
121
|
+
---
|
122
|
+
|
123
|
+
## ✅ **API Reference**
|
124
|
+
|
125
|
+
### **RateLimiter Class**
|
126
|
+
|
127
|
+
```typescript
|
128
|
+
new RateLimiter({ windowMs: number, maxRequests: number });
|
129
|
+
```
|
130
|
+
|
131
|
+
| Parameter | Type | Description |
|
132
|
+
| ------------- | -------- | -------------------------------------------------------- |
|
133
|
+
| `windowMs` | `number` | Time window in milliseconds (e.g., `60000` for 1 minute) |
|
134
|
+
| `maxRequests` | `number` | Maximum allowed requests in the given window |
|
135
|
+
|
136
|
+
#### **Methods**
|
137
|
+
|
138
|
+
```typescript
|
139
|
+
isAllowed(userId: string): boolean
|
140
|
+
```
|
141
|
+
|
142
|
+
| Method | Returns | Description |
|
143
|
+
| ------------------- | --------- | --------------------------------------------------------------------------- |
|
144
|
+
| `isAllowed(userId)` | `boolean` | Returns `true` if the user is allowed to make a request, otherwise `false`. |
|
145
|
+
|
146
|
+
---
|
147
|
+
|
148
|
+
### 💡 **Contributing**
|
149
|
+
|
150
|
+
Contributions are welcome! Please submit
|
151
|
+
[issues](https://github.com/The-Node-Forge/api-rate-limit/issues) or
|
152
|
+
[pull requests](https://github.com/The-Node-Forge/api-rate-limit/pulls).
|
153
|
+
|
154
|
+
---
|
155
|
+
|
156
|
+
### ⭐ Support
|
157
|
+
|
158
|
+
If you find this package useful, please \*\*give it a ⭐ on
|
159
|
+
[GitHub](https://github.com/The-Node-Forge/api-rate-limit 'GitHub Repository')
|
160
|
+
|
161
|
+
---
|
162
|
+
|
163
|
+
### 🔗 **Links**
|
164
|
+
|
165
|
+
- 📦 [NPM Package](https://www.npmjs.com/package/@the-node-forge/api-rate-limit)
|
166
|
+
- 🏗 [The-Node-Forge](https://github.com/The-Node-Forge)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
export interface RateLimitOptions {
|
2
|
+
windowMs: number;
|
3
|
+
maxRequests: number;
|
4
|
+
}
|
5
|
+
declare class RateLimiter {
|
6
|
+
private requests;
|
7
|
+
private windowMs;
|
8
|
+
private maxRequests;
|
9
|
+
constructor(options: RateLimitOptions);
|
10
|
+
isAllowed(userId: string): boolean;
|
11
|
+
}
|
12
|
+
export default RateLimiter;
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
class RateLimiter {
|
4
|
+
constructor(options) {
|
5
|
+
this.windowMs = options.windowMs;
|
6
|
+
this.maxRequests = options.maxRequests;
|
7
|
+
this.requests = new Map();
|
8
|
+
}
|
9
|
+
isAllowed(userId) {
|
10
|
+
const now = Date.now();
|
11
|
+
const timestamps = this.requests.get(userId) || [];
|
12
|
+
const filteredTimestamps = timestamps.filter((timestamp) => now - timestamp < this.windowMs);
|
13
|
+
if (filteredTimestamps.length >= this.maxRequests) {
|
14
|
+
return false;
|
15
|
+
}
|
16
|
+
filteredTimestamps.push(now);
|
17
|
+
this.requests.set(userId, filteredTimestamps);
|
18
|
+
return true;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
exports.default = RateLimiter;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.rateLimitMiddleware = exports.RateLimiter = void 0;
|
7
|
+
const RateLimiter_1 = __importDefault(require("./RateLimiter"));
|
8
|
+
exports.RateLimiter = RateLimiter_1.default;
|
9
|
+
const middleware_1 = require("./middleware");
|
10
|
+
Object.defineProperty(exports, "rateLimitMiddleware", { enumerable: true, get: function () { return middleware_1.rateLimitMiddleware; } });
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.rateLimitMiddleware = void 0;
|
4
|
+
const HTTP_STATUS = {
|
5
|
+
TOO_MANY_REQUESTS: 429,
|
6
|
+
};
|
7
|
+
const rateLimitMiddleware = (limiter) => {
|
8
|
+
return (req, res, next) => {
|
9
|
+
const userId = req.ip || '';
|
10
|
+
if (!limiter.isAllowed(userId)) {
|
11
|
+
return res
|
12
|
+
.status(HTTP_STATUS.TOO_MANY_REQUESTS)
|
13
|
+
.json({ message: 'Too many requests, please try again later.' });
|
14
|
+
}
|
15
|
+
next();
|
16
|
+
};
|
17
|
+
};
|
18
|
+
exports.rateLimitMiddleware = rateLimitMiddleware;
|
package/package.json
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
{
|
2
|
+
"name": "@the-node-forge/api-rate-limit",
|
3
|
+
"version": "1.0.7",
|
4
|
+
"description": "A simple and efficient API rate limiter for JavaScript/TypeScript applications",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"exports": {
|
8
|
+
"import": "./dist/index.js",
|
9
|
+
"require": "./dist/index.js"
|
10
|
+
},
|
11
|
+
"scripts": {
|
12
|
+
"test": "jest",
|
13
|
+
"test:watch": "jest --watch",
|
14
|
+
"test:coverage": "jest --coverage",
|
15
|
+
"format": "prettier --write .",
|
16
|
+
"format:check": "prettier --check .",
|
17
|
+
"lint": "eslint .",
|
18
|
+
"lint:fix": "eslint . --fix",
|
19
|
+
"prepublishOnly": "npm run build",
|
20
|
+
"build": "tsc",
|
21
|
+
"docs:generate": "typedoc --plugin typedoc-plugin-markdown --out docs/api src/index.ts",
|
22
|
+
"docs:build": "npm run docs:generate && cd docs && npm run build",
|
23
|
+
"docs:start": "npm run docs:generate && cd docs && npm start",
|
24
|
+
"docs:clean": "rm -rf docs/build && npm run docs:generate && npm run docs:build",
|
25
|
+
"docs:deploy": "cd docs && npm run deploy"
|
26
|
+
},
|
27
|
+
"repository": {
|
28
|
+
"type": "git",
|
29
|
+
"url": "https://github.com/The-Node-Forge/api-rate-limit"
|
30
|
+
},
|
31
|
+
"keywords": [
|
32
|
+
"rate-limit",
|
33
|
+
"api",
|
34
|
+
"express",
|
35
|
+
"typescript",
|
36
|
+
"npm-package",
|
37
|
+
"request-throttling",
|
38
|
+
"http",
|
39
|
+
"middleware",
|
40
|
+
"express-middleware",
|
41
|
+
"throttle",
|
42
|
+
"rate-limiter",
|
43
|
+
"web-security",
|
44
|
+
"api-protection",
|
45
|
+
"ddos-protection",
|
46
|
+
"request-limiting",
|
47
|
+
"nodejs",
|
48
|
+
"backend"
|
49
|
+
],
|
50
|
+
"author": {
|
51
|
+
"name": "Lanny MacMillan",
|
52
|
+
"url": "https://github.com/Lanny-MacMillan"
|
53
|
+
},
|
54
|
+
"license": "MIT",
|
55
|
+
"files": [
|
56
|
+
"dist",
|
57
|
+
"README.md"
|
58
|
+
],
|
59
|
+
"devDependencies": {
|
60
|
+
"@eslint/compat": "^1.2.5",
|
61
|
+
"@eslint/eslintrc": "^3.2.0",
|
62
|
+
"@eslint/js": "^9.19.0",
|
63
|
+
"@types/express": "^5.0.0",
|
64
|
+
"@types/jest": "^29.5.14",
|
65
|
+
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
66
|
+
"@typescript-eslint/parser": "^8.22.0",
|
67
|
+
"eslint": "^9.19.0",
|
68
|
+
"eslint-config-prettier": "^10.0.1",
|
69
|
+
"eslint-import-resolver-typescript": "^3.7.0",
|
70
|
+
"eslint-plugin-import": "^2.31.0",
|
71
|
+
"eslint-plugin-prettier": "^5.2.3",
|
72
|
+
"globals": "^15.14.0",
|
73
|
+
"jest": "^29.7.0",
|
74
|
+
"prettier": "^3.4.2",
|
75
|
+
"ts-jest": "^29.2.5",
|
76
|
+
"typedoc": "^0.27.6",
|
77
|
+
"typedoc-plugin-markdown": "^4.4.1",
|
78
|
+
"typescript": "^5.7.3"
|
79
|
+
},
|
80
|
+
"dependencies": {
|
81
|
+
"express": "^4.21.2"
|
82
|
+
}
|
83
|
+
}
|