@canmertinyo/rate-limit-express 1.2.3 → 1.2.6
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.
@@ -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];
|
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
@@ -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
@@ -11,19 +11,20 @@ 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 = {
|
15
|
-
|
14
|
+
record = storage.createRateLimitRecord({
|
15
|
+
key: ip,
|
16
|
+
count: 1,
|
17
|
+
timestamp: currentTime,
|
18
|
+
});
|
16
19
|
console.log("create", record);
|
17
20
|
}
|
18
21
|
if (startTime > record.timestamp) {
|
19
|
-
record =
|
20
|
-
storage.updateRateLimitRecord(ip, currentTime, 1);
|
22
|
+
record = storage.updateRateLimitRecord(ip, currentTime, 1);
|
21
23
|
console.log("update", record);
|
22
24
|
}
|
23
25
|
if (record.count < options.maxRequest) {
|
24
26
|
console.log("inc before", record.count);
|
25
|
-
record
|
26
|
-
storage.increment(record.key);
|
27
|
+
record = storage.increment(record.key);
|
27
28
|
console.log("inc after", record.count);
|
28
29
|
return next();
|
29
30
|
}
|
package/package.json
CHANGED
package/src/in-memory-storage.ts
CHANGED
@@ -7,18 +7,27 @@ 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];
|
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
|
+
|
31
|
+
return this.store[index];
|
23
32
|
}
|
24
33
|
}
|
package/src/rate.limiter.ts
CHANGED
@@ -15,21 +15,22 @@ export function rateLimiter(options: RateLimiterOptions = defaultOptions) {
|
|
15
15
|
var record = storage.getRateLimitRecord(ip);
|
16
16
|
|
17
17
|
if (!record) {
|
18
|
-
record = {
|
19
|
-
|
18
|
+
record = storage.createRateLimitRecord({
|
19
|
+
key: ip,
|
20
|
+
count: 1,
|
21
|
+
timestamp: currentTime,
|
22
|
+
});
|
20
23
|
console.log("create", record);
|
21
24
|
}
|
22
25
|
|
23
26
|
if (startTime > record.timestamp) {
|
24
|
-
record =
|
25
|
-
storage.updateRateLimitRecord(ip, currentTime, 1);
|
27
|
+
record = storage.updateRateLimitRecord(ip, currentTime, 1);
|
26
28
|
console.log("update", record);
|
27
29
|
}
|
28
30
|
|
29
31
|
if (record.count < options.maxRequest) {
|
30
32
|
console.log("inc before", record.count);
|
31
|
-
record
|
32
|
-
storage.increment(record.key);
|
33
|
+
record = storage.increment(record.key);
|
33
34
|
console.log("inc after", record.count);
|
34
35
|
|
35
36
|
return next();
|
@@ -49,7 +50,11 @@ export interface IRateLimitRecord {
|
|
49
50
|
|
50
51
|
export interface IRateLimiter {
|
51
52
|
getRateLimitRecord(key: string): IRateLimitRecord | undefined;
|
52
|
-
createRateLimitRecord(record: IRateLimitRecord):
|
53
|
-
updateRateLimitRecord(
|
54
|
-
|
53
|
+
createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord;
|
54
|
+
updateRateLimitRecord(
|
55
|
+
key: string,
|
56
|
+
timestamp: number,
|
57
|
+
count: number
|
58
|
+
): IRateLimitRecord;
|
59
|
+
increment(key: string): IRateLimitRecord;
|
55
60
|
}
|