@canmertinyo/rate-limit-express 1.2.4 → 1.2.8
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/dist/in-memory-storage.d.ts +3 -3
- package/dist/in-memory-storage.js +4 -1
- package/dist/rate.limiter.d.ts +4 -4
- package/dist/rate.limiter.js +13 -13
- package/package.json +1 -1
- package/src/in-memory-storage.ts +12 -4
- package/src/rate.limiter.ts +21 -20
@@ -2,7 +2,7 @@ import { IRateLimiter, IRateLimitRecord } from "./rate.limiter";
|
|
2
2
|
export declare class InMemoryStorage implements IRateLimiter {
|
3
3
|
private store;
|
4
4
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
5
|
-
createRateLimitRecord(record: IRateLimitRecord):
|
6
|
-
updateRateLimitRecord(key: string, timestamp: number, count: number):
|
7
|
-
increment(key: string):
|
5
|
+
createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord;
|
6
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): IRateLimitRecord;
|
7
|
+
increment(key: string): IRateLimitRecord;
|
8
8
|
}
|
@@ -9,16 +9,19 @@ class InMemoryStorage {
|
|
9
9
|
return this.store.find((x) => x.key === key);
|
10
10
|
}
|
11
11
|
createRateLimitRecord(record) {
|
12
|
-
this.store.push(record);
|
12
|
+
var index = this.store.push(record);
|
13
|
+
return this.store[index - 1];
|
13
14
|
}
|
14
15
|
updateRateLimitRecord(key, timestamp, count) {
|
15
16
|
var index = this.store.findIndex((x) => x.key === key);
|
16
17
|
this.store[index].count = count;
|
17
18
|
this.store[index].timestamp = timestamp;
|
19
|
+
return this.store[index];
|
18
20
|
}
|
19
21
|
increment(key) {
|
20
22
|
var index = this.store.findIndex((x) => x.key === key);
|
21
23
|
this.store[index].count += 1;
|
24
|
+
return this.store[index];
|
22
25
|
}
|
23
26
|
}
|
24
27
|
exports.InMemoryStorage = InMemoryStorage;
|
package/dist/rate.limiter.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
2
2
|
import { RateLimiterOptions } from "./interfaces/rate-limiter-options.interface";
|
3
|
-
export declare function rateLimiter(options
|
3
|
+
export declare function rateLimiter(options: RateLimiterOptions): (req: Request, res: Response, next: NextFunction) => void;
|
4
4
|
export interface IRateLimitRecord {
|
5
5
|
key: string;
|
6
6
|
count: number;
|
@@ -8,7 +8,7 @@ export interface IRateLimitRecord {
|
|
8
8
|
}
|
9
9
|
export interface IRateLimiter {
|
10
10
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
11
|
-
createRateLimitRecord(record: IRateLimitRecord):
|
12
|
-
updateRateLimitRecord(key: string, timestamp: number, count: number):
|
13
|
-
increment(key: string):
|
11
|
+
createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord;
|
12
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): IRateLimitRecord;
|
13
|
+
increment(key: string): IRateLimitRecord;
|
14
14
|
}
|
package/dist/rate.limiter.js
CHANGED
@@ -2,29 +2,29 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.rateLimiter = rateLimiter;
|
4
4
|
const utils_1 = require("./utils");
|
5
|
-
const
|
6
|
-
|
5
|
+
const in_memory_storage_1 = require("./in-memory-storage");
|
6
|
+
const inMemoryStorage = new in_memory_storage_1.InMemoryStorage();
|
7
|
+
function rateLimiter(options) {
|
7
8
|
return (req, res, next) => {
|
9
|
+
var _a;
|
8
10
|
const ip = (0, utils_1.getIp)(req);
|
9
11
|
const currentTime = (0, utils_1.getTimestamp)();
|
10
12
|
const startTime = currentTime - options.ms;
|
11
|
-
var storage = options.storage;
|
13
|
+
var storage = (_a = options.storage) !== null && _a !== void 0 ? _a : inMemoryStorage;
|
12
14
|
var record = storage.getRateLimitRecord(ip);
|
13
15
|
if (!record) {
|
14
|
-
record = {
|
15
|
-
|
16
|
-
|
16
|
+
record = storage.createRateLimitRecord({
|
17
|
+
key: ip,
|
18
|
+
count: 0,
|
19
|
+
timestamp: currentTime,
|
20
|
+
});
|
17
21
|
}
|
18
22
|
if (startTime > record.timestamp) {
|
19
|
-
record =
|
20
|
-
|
21
|
-
console.log("update", record);
|
23
|
+
record = storage.updateRateLimitRecord(ip, currentTime, 1);
|
24
|
+
return next();
|
22
25
|
}
|
23
26
|
if (record.count < options.maxRequest) {
|
24
|
-
|
25
|
-
record.count += 1;
|
26
|
-
storage.increment(record.key);
|
27
|
-
console.log("inc after", record.count);
|
27
|
+
record = storage.increment(record.key);
|
28
28
|
return next();
|
29
29
|
}
|
30
30
|
res
|
package/package.json
CHANGED
package/src/in-memory-storage.ts
CHANGED
@@ -7,18 +7,26 @@ export class InMemoryStorage implements IRateLimiter {
|
|
7
7
|
return this.store.find((x) => x.key === key);
|
8
8
|
}
|
9
9
|
|
10
|
-
createRateLimitRecord(record: IRateLimitRecord):
|
11
|
-
this.store.push(record);
|
10
|
+
createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord {
|
11
|
+
var index = this.store.push(record);
|
12
|
+
return this.store[index - 1];
|
12
13
|
}
|
13
14
|
|
14
|
-
updateRateLimitRecord(
|
15
|
+
updateRateLimitRecord(
|
16
|
+
key: string,
|
17
|
+
timestamp: number,
|
18
|
+
count: number
|
19
|
+
): IRateLimitRecord {
|
15
20
|
var index = this.store.findIndex((x) => x.key === key);
|
16
21
|
this.store[index].count = count;
|
17
22
|
this.store[index].timestamp = timestamp;
|
23
|
+
|
24
|
+
return this.store[index];
|
18
25
|
}
|
19
26
|
|
20
|
-
increment(key: string):
|
27
|
+
increment(key: string): IRateLimitRecord {
|
21
28
|
var index = this.store.findIndex((x) => x.key === key);
|
22
29
|
this.store[index].count += 1;
|
30
|
+
return this.store[index];
|
23
31
|
}
|
24
32
|
}
|
package/src/rate.limiter.ts
CHANGED
@@ -1,37 +1,34 @@
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
2
2
|
import { getIp, getTimestamp } from "./utils";
|
3
|
-
import {
|
4
|
-
|
5
|
-
RateLimiterOptions,
|
6
|
-
} from "./interfaces/rate-limiter-options.interface";
|
3
|
+
import { RateLimiterOptions } from "./interfaces/rate-limiter-options.interface";
|
4
|
+
import { InMemoryStorage } from "./in-memory-storage";
|
7
5
|
|
8
|
-
|
6
|
+
const inMemoryStorage = new InMemoryStorage();
|
7
|
+
|
8
|
+
export function rateLimiter(options: RateLimiterOptions) {
|
9
9
|
return (req: Request, res: Response, next: NextFunction) => {
|
10
10
|
const ip = getIp(req);
|
11
11
|
const currentTime = getTimestamp();
|
12
12
|
const startTime = currentTime - options.ms;
|
13
|
-
var storage = options.storage
|
13
|
+
var storage = options.storage ?? inMemoryStorage;
|
14
14
|
|
15
15
|
var record = storage.getRateLimitRecord(ip);
|
16
16
|
|
17
17
|
if (!record) {
|
18
|
-
record = {
|
19
|
-
|
20
|
-
|
18
|
+
record = storage.createRateLimitRecord({
|
19
|
+
key: ip,
|
20
|
+
count: 0,
|
21
|
+
timestamp: currentTime,
|
22
|
+
});
|
21
23
|
}
|
22
24
|
|
23
25
|
if (startTime > record.timestamp) {
|
24
|
-
record =
|
25
|
-
|
26
|
-
console.log("update", record);
|
26
|
+
record = storage.updateRateLimitRecord(ip, currentTime, 1);
|
27
|
+
return next();
|
27
28
|
}
|
28
29
|
|
29
30
|
if (record.count < options.maxRequest) {
|
30
|
-
|
31
|
-
record.count += 1;
|
32
|
-
storage.increment(record.key);
|
33
|
-
console.log("inc after", record.count);
|
34
|
-
|
31
|
+
record = storage.increment(record.key);
|
35
32
|
return next();
|
36
33
|
}
|
37
34
|
|
@@ -49,7 +46,11 @@ export interface IRateLimitRecord {
|
|
49
46
|
|
50
47
|
export interface IRateLimiter {
|
51
48
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
52
|
-
createRateLimitRecord(record: IRateLimitRecord):
|
53
|
-
updateRateLimitRecord(
|
54
|
-
|
49
|
+
createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord;
|
50
|
+
updateRateLimitRecord(
|
51
|
+
key: string,
|
52
|
+
timestamp: number,
|
53
|
+
count: number
|
54
|
+
): IRateLimitRecord;
|
55
|
+
increment(key: string): IRateLimitRecord;
|
55
56
|
}
|