@canmertinyo/rate-limit-express 1.1.2 → 1.1.4

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;
@@ -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
  }
@@ -11,12 +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: 0, timestamp: currentTime, key: ip };
14
+ record = { count: 1, timestamp: currentTime, key: ip };
15
15
  storage.createRateLimitRecord(record);
16
+ return next();
16
17
  }
17
- if (record.count <= options.maxRequest && record.timestamp >= startTime) {
18
+ else if (record.timestamp < startTime) {
19
+ storage.updateRateLimitRecord(record.key, currentTime, 1);
20
+ return next();
21
+ }
22
+ else if (record.count < options.maxRequest) {
18
23
  storage.increment(ip);
19
- next();
24
+ return next();
20
25
  }
21
26
  else {
22
27
  res
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canmertinyo/rate-limit-express",
3
3
  "description": "A simple rate-limiting middleware for Express.js",
4
- "version": "1.1.2",
4
+ "version": "1.1.4",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "types": "dist/index.d.ts",
@@ -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;
@@ -15,13 +15,15 @@ export function rateLimiter(options: RateLimiterOptions = defaultOptions) {
15
15
  var record = storage.getRateLimitRecord(ip);
16
16
 
17
17
  if (!record) {
18
- record = { count: 0, timestamp: currentTime, key: ip };
18
+ record = { count: 1, timestamp: currentTime, key: ip };
19
19
  storage.createRateLimitRecord(record);
20
- }
21
-
22
- if (record.count <= options.maxRequest && record.timestamp >= startTime) {
20
+ return next();
21
+ } else if (record.timestamp < startTime) {
22
+ storage.updateRateLimitRecord(record.key, currentTime, 1);
23
+ return next();
24
+ } else if (record.count < options.maxRequest) {
23
25
  storage.increment(ip);
24
- next();
26
+ return next();
25
27
  } else {
26
28
  res
27
29
  .status(420)
@@ -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
  }