@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.
@@ -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): void;
6
- updateRateLimitRecord(key: string, timestamp: number, count: number): void;
7
- increment(key: string): void;
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;
@@ -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?: RateLimiterOptions): (req: Request, res: Response, next: NextFunction) => void;
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): void;
12
- updateRateLimitRecord(key: string, timestamp: number, count: number): void;
13
- increment(key: string): void;
11
+ createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord;
12
+ updateRateLimitRecord(key: string, timestamp: number, count: number): IRateLimitRecord;
13
+ increment(key: string): IRateLimitRecord;
14
14
  }
@@ -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 rate_limiter_options_interface_1 = require("./interfaces/rate-limiter-options.interface");
6
- function rateLimiter(options = rate_limiter_options_interface_1.defaultOptions) {
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 = { key: ip, count: 1, timestamp: currentTime };
15
- storage.createRateLimitRecord(record);
16
- console.log("create", record);
16
+ record = storage.createRateLimitRecord({
17
+ key: ip,
18
+ count: 0,
19
+ timestamp: currentTime,
20
+ });
17
21
  }
18
22
  if (startTime > record.timestamp) {
19
- record = { key: ip, count: 1, timestamp: currentTime };
20
- storage.updateRateLimitRecord(ip, currentTime, 1);
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
- console.log("inc before", record.count);
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
@@ -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.2.4",
4
+ "version": "1.2.8",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "types": "dist/index.d.ts",
@@ -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): void {
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(key: string, timestamp: number, count: number): void {
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): void {
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
  }
@@ -1,37 +1,34 @@
1
1
  import { Request, Response, NextFunction } from "express";
2
2
  import { getIp, getTimestamp } from "./utils";
3
- import {
4
- defaultOptions,
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
- export function rateLimiter(options: RateLimiterOptions = defaultOptions) {
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 = { key: ip, count: 1, timestamp: currentTime };
19
- storage.createRateLimitRecord(record);
20
- console.log("create", record);
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 = { key: ip, count: 1, timestamp: currentTime };
25
- storage.updateRateLimitRecord(ip, currentTime, 1);
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
- console.log("inc before", record.count);
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): void;
53
- updateRateLimitRecord(key: string, timestamp: number, count: number): void;
54
- increment(key: string): void;
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
  }