@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 ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -0,0 +1,2 @@
1
+ import { rateLimiter } from "./rate.limiter";
2
+ export { rateLimiter as RateLimiter };
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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ import { RateLimiterOptions } from "./interfaces/rate-limiter-options.interface";
3
+ export declare function rateLimiter(options: RateLimiterOptions): (req: Request, res: Response, next: NextFunction) => void;
@@ -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
+ }
@@ -0,0 +1,6 @@
1
+ export type Store = {
2
+ [key: string]: {
3
+ count: number;
4
+ timestamp: number;
5
+ };
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { Request } from "express";
2
+ export declare const getIp: (req: Request) => string;
3
+ export declare const getTimestamp: () => number;
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,3 @@
1
+ import { rateLimiter } from "./rate.limiter";
2
+
3
+ export { rateLimiter as 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,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
+ }
@@ -0,0 +1,3 @@
1
+ export type Store = {
2
+ [key: string]: { count: number; timestamp: number };
3
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Request } from "express";
2
+
3
+ export const getIp = (req: Request): string => {
4
+ return req.ip || (req.headers["x-forwarded-for"] as string) || "unknown";
5
+ };
6
+
7
+ export const getTimestamp = (): number => {
8
+ return new Date().getTime();
9
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "CommonJS",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true
8
+ },
9
+ "include": ["src/**/*"],
10
+ "exclude": ["node_modules", "dist"]
11
+ }