@canmertinyo/rate-limit-express 1.1.1 → 1.1.3
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.
@@ -3,5 +3,6 @@ export declare class InMemoryStorage implements IRateLimiter {
|
|
3
3
|
private store;
|
4
4
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
5
5
|
createRateLimitRecord(record: IRateLimitRecord): void;
|
6
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): void;
|
6
7
|
increment(key: string): void;
|
7
8
|
}
|
@@ -11,6 +11,11 @@ class InMemoryStorage {
|
|
11
11
|
createRateLimitRecord(record) {
|
12
12
|
this.store.push(record);
|
13
13
|
}
|
14
|
+
updateRateLimitRecord(key, timestamp, count) {
|
15
|
+
var index = this.store.findIndex((x) => x.key === key);
|
16
|
+
this.store[index].count = count;
|
17
|
+
this.store[index].timestamp = timestamp;
|
18
|
+
}
|
14
19
|
increment(key) {
|
15
20
|
var index = this.store.findIndex((x) => x.key === key);
|
16
21
|
this.store[index].count += 1;
|
package/dist/rate.limiter.d.ts
CHANGED
@@ -9,5 +9,6 @@ export interface IRateLimitRecord {
|
|
9
9
|
export interface IRateLimiter {
|
10
10
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
11
11
|
createRateLimitRecord(record: IRateLimitRecord): void;
|
12
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): void;
|
12
13
|
increment(key: string): void;
|
13
14
|
}
|
package/dist/rate.limiter.js
CHANGED
@@ -11,17 +11,17 @@ function rateLimiter(options = rate_limiter_options_interface_1.defaultOptions)
|
|
11
11
|
var storage = options.storage;
|
12
12
|
var record = storage.getRateLimitRecord(ip);
|
13
13
|
if (!record) {
|
14
|
-
record = { count:
|
14
|
+
record = { count: 1, timestamp: currentTime, key: ip };
|
15
15
|
storage.createRateLimitRecord(record);
|
16
|
+
return next();
|
16
17
|
}
|
17
|
-
if (record.
|
18
|
-
storage.
|
19
|
-
next();
|
18
|
+
if (record.timestamp < startTime) {
|
19
|
+
storage.updateRateLimitRecord(record.key, currentTime, 1);
|
20
|
+
return next();
|
20
21
|
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
.json({ message: "Too many requests, please try again later." });
|
22
|
+
if (record.count < options.maxRequest) {
|
23
|
+
storage.increment(ip);
|
24
|
+
return next();
|
25
25
|
}
|
26
26
|
};
|
27
27
|
}
|
package/package.json
CHANGED
package/src/in-memory-storage.ts
CHANGED
@@ -11,6 +11,12 @@ export class InMemoryStorage implements IRateLimiter {
|
|
11
11
|
this.store.push(record);
|
12
12
|
}
|
13
13
|
|
14
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): void {
|
15
|
+
var index = this.store.findIndex((x) => x.key === key);
|
16
|
+
this.store[index].count = count;
|
17
|
+
this.store[index].timestamp = timestamp;
|
18
|
+
}
|
19
|
+
|
14
20
|
increment(key: string): void {
|
15
21
|
var index = this.store.findIndex((x) => x.key === key);
|
16
22
|
this.store[index].count += 1;
|
package/src/rate.limiter.ts
CHANGED
@@ -15,17 +15,19 @@ export function rateLimiter(options: RateLimiterOptions = defaultOptions) {
|
|
15
15
|
var record = storage.getRateLimitRecord(ip);
|
16
16
|
|
17
17
|
if (!record) {
|
18
|
-
record = { count:
|
18
|
+
record = { count: 1, timestamp: currentTime, key: ip };
|
19
19
|
storage.createRateLimitRecord(record);
|
20
|
+
return next();
|
20
21
|
}
|
21
22
|
|
22
|
-
if (record.
|
23
|
+
if (record.timestamp < startTime) {
|
24
|
+
storage.updateRateLimitRecord(record.key, currentTime, 1);
|
25
|
+
return next();
|
26
|
+
}
|
27
|
+
|
28
|
+
if (record.count < options.maxRequest) {
|
23
29
|
storage.increment(ip);
|
24
|
-
next();
|
25
|
-
} else {
|
26
|
-
res
|
27
|
-
.status(420)
|
28
|
-
.json({ message: "Too many requests, please try again later." });
|
30
|
+
return next();
|
29
31
|
}
|
30
32
|
};
|
31
33
|
}
|
@@ -39,5 +41,6 @@ export interface IRateLimitRecord {
|
|
39
41
|
export interface IRateLimiter {
|
40
42
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
41
43
|
createRateLimitRecord(record: IRateLimitRecord): void;
|
44
|
+
updateRateLimitRecord(key: string, timestamp: number, count: number): void;
|
42
45
|
increment(key: string): void;
|
43
46
|
}
|