@canmertinyo/rate-limit-express 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/.gitattributes +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/interfaces/rate-limiter-options.interface.d.ts +12 -0
- package/dist/interfaces/rate-limiter-options.interface.js +2 -0
- package/dist/rate.limiter.d.ts +3 -0
- package/dist/rate.limiter.js +28 -0
- package/dist/types/store.type.d.ts +6 -0
- package/dist/types/store.type.js +2 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +11 -0
- package/package.json +20 -0
- package/src/index.ts +3 -0
- package/src/interfaces/rate-limiter-options.interface.ts +12 -0
- package/src/rate.limiter.ts +32 -0
- package/src/types/store.type.ts +3 -0
- package/src/utils.ts +9 -0
- package/tsconfig.json +11 -0
package/.gitattributes
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RateLimiter = void 0;
|
4
|
+
const rate_limiter_1 = require("./rate.limiter");
|
5
|
+
Object.defineProperty(exports, "RateLimiter", { enumerable: true, get: function () { return rate_limiter_1.rateLimiter; } });
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* Options for the rate limiter configuration.
|
3
|
+
*
|
4
|
+
* @property ms - The time window in milliseconds for rate limiting.
|
5
|
+
* @property maxRequest - The maximum number of allowed requests in the time window.
|
6
|
+
*/
|
7
|
+
export interface RateLimiterOptions {
|
8
|
+
/** The time in milliseconds for rate limiting. */
|
9
|
+
ms: number;
|
10
|
+
/** The maximum number of allowed requests in the time window. */
|
11
|
+
maxRequest: number;
|
12
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.rateLimiter = rateLimiter;
|
4
|
+
const utils_1 = require("./utils");
|
5
|
+
const store = {};
|
6
|
+
function rateLimiter(options) {
|
7
|
+
return (req, res, next) => {
|
8
|
+
const ip = (0, utils_1.getIp)(req);
|
9
|
+
const currentTime = (0, utils_1.getTimestamp)();
|
10
|
+
const startTime = currentTime - options.ms * 1000;
|
11
|
+
if (!store[ip]) {
|
12
|
+
store[ip] = { count: 0, timestamp: currentTime };
|
13
|
+
}
|
14
|
+
if (store[ip].timestamp < startTime) {
|
15
|
+
store[ip].count = 0;
|
16
|
+
store[ip].timestamp = currentTime;
|
17
|
+
}
|
18
|
+
if (store[ip].count < options.maxRequest) {
|
19
|
+
store[ip].count += 1;
|
20
|
+
next();
|
21
|
+
}
|
22
|
+
else {
|
23
|
+
res
|
24
|
+
.status(420)
|
25
|
+
.json({ message: "Too many requests, please try again later." });
|
26
|
+
}
|
27
|
+
};
|
28
|
+
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getTimestamp = exports.getIp = void 0;
|
4
|
+
const getIp = (req) => {
|
5
|
+
return req.ip || req.headers["x-forwarded-for"] || "unknown";
|
6
|
+
};
|
7
|
+
exports.getIp = getIp;
|
8
|
+
const getTimestamp = () => {
|
9
|
+
return new Date().getTime();
|
10
|
+
};
|
11
|
+
exports.getTimestamp = getTimestamp;
|
package/package.json
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"name": "@canmertinyo/rate-limit-express",
|
3
|
+
"description": "A simple rate-limiting middleware for Express.js",
|
4
|
+
"version": "1.0.0",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"license": "MIT",
|
7
|
+
"types": "dist/index.d.ts",
|
8
|
+
"scripts": {
|
9
|
+
"build": "tsc"
|
10
|
+
},
|
11
|
+
"keywords": ["ratelimiter-express"],
|
12
|
+
"author": "c4nzin",
|
13
|
+
"devDependencies": {
|
14
|
+
"@types/express": "^5.0.0",
|
15
|
+
"typescript": "^5.7.2"
|
16
|
+
},
|
17
|
+
"dependencies": {
|
18
|
+
"express": "^4.21.1"
|
19
|
+
}
|
20
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* Options for the rate limiter configuration.
|
3
|
+
*
|
4
|
+
* @property ms - The time window in milliseconds for rate limiting.
|
5
|
+
* @property maxRequest - The maximum number of allowed requests in the time window.
|
6
|
+
*/
|
7
|
+
export interface RateLimiterOptions {
|
8
|
+
/** The time in milliseconds for rate limiting. */
|
9
|
+
ms: number;
|
10
|
+
/** The maximum number of allowed requests in the time window. */
|
11
|
+
maxRequest: number;
|
12
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
2
|
+
import { getIp, getTimestamp } from "./utils";
|
3
|
+
import { Store } from "./types/store.type";
|
4
|
+
import { RateLimiterOptions } from "./interfaces/rate-limiter-options.interface";
|
5
|
+
|
6
|
+
const store: Store = {};
|
7
|
+
|
8
|
+
export function rateLimiter(options: RateLimiterOptions) {
|
9
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
10
|
+
const ip = getIp(req);
|
11
|
+
const currentTime = getTimestamp();
|
12
|
+
const startTime = currentTime - options.ms * 1000;
|
13
|
+
|
14
|
+
if (!store[ip]) {
|
15
|
+
store[ip] = { count: 0, timestamp: currentTime };
|
16
|
+
}
|
17
|
+
|
18
|
+
if (store[ip].timestamp < startTime) {
|
19
|
+
store[ip].count = 0;
|
20
|
+
store[ip].timestamp = currentTime;
|
21
|
+
}
|
22
|
+
|
23
|
+
if (store[ip].count < options.maxRequest) {
|
24
|
+
store[ip].count += 1;
|
25
|
+
next();
|
26
|
+
} else {
|
27
|
+
res
|
28
|
+
.status(420)
|
29
|
+
.json({ message: "Too many requests, please try again later." });
|
30
|
+
}
|
31
|
+
};
|
32
|
+
}
|
package/src/utils.ts
ADDED