@canmertinyo/rate-limit-express 1.2.6 → 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.
@@ -10,7 +10,7 @@ class InMemoryStorage {
10
10
  }
11
11
  createRateLimitRecord(record) {
12
12
  var index = this.store.push(record);
13
- return this.store[index];
13
+ return this.store[index - 1];
14
14
  }
15
15
  updateRateLimitRecord(key, timestamp, count) {
16
16
  var index = this.store.findIndex((x) => x.key === key);
@@ -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;
@@ -2,30 +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
16
  record = storage.createRateLimitRecord({
15
17
  key: ip,
16
- count: 1,
18
+ count: 0,
17
19
  timestamp: currentTime,
18
20
  });
19
- console.log("create", record);
20
21
  }
21
22
  if (startTime > record.timestamp) {
22
23
  record = storage.updateRateLimitRecord(ip, currentTime, 1);
23
- console.log("update", record);
24
+ return next();
24
25
  }
25
26
  if (record.count < options.maxRequest) {
26
- console.log("inc before", record.count);
27
27
  record = storage.increment(record.key);
28
- console.log("inc after", record.count);
29
28
  return next();
30
29
  }
31
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.6",
4
+ "version": "1.2.8",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "types": "dist/index.d.ts",
@@ -9,7 +9,7 @@ export class InMemoryStorage implements IRateLimiter {
9
9
 
10
10
  createRateLimitRecord(record: IRateLimitRecord): IRateLimitRecord {
11
11
  var index = this.store.push(record);
12
- return this.store[index];
12
+ return this.store[index - 1];
13
13
  }
14
14
 
15
15
  updateRateLimitRecord(
@@ -27,7 +27,6 @@ export class InMemoryStorage implements IRateLimiter {
27
27
  increment(key: string): IRateLimitRecord {
28
28
  var index = this.store.findIndex((x) => x.key === key);
29
29
  this.store[index].count += 1;
30
-
31
30
  return this.store[index];
32
31
  }
33
32
  }
@@ -1,38 +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
18
  record = storage.createRateLimitRecord({
19
19
  key: ip,
20
- count: 1,
20
+ count: 0,
21
21
  timestamp: currentTime,
22
22
  });
23
- console.log("create", record);
24
23
  }
25
24
 
26
25
  if (startTime > record.timestamp) {
27
26
  record = storage.updateRateLimitRecord(ip, currentTime, 1);
28
- console.log("update", record);
27
+ return next();
29
28
  }
30
29
 
31
30
  if (record.count < options.maxRequest) {
32
- console.log("inc before", record.count);
33
31
  record = storage.increment(record.key);
34
- console.log("inc after", record.count);
35
-
36
32
  return next();
37
33
  }
38
34