@geekmidas/rate-limit 0.1.0 → 0.2.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/rate-limit",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -12,18 +12,34 @@
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://github.com/geekmidas/toolbox.git"
15
+ "url": "https://github.com/geekmidas/toolbox"
16
16
  },
17
17
  "publishConfig": {
18
18
  "registry": "https://registry.npmjs.org/",
19
19
  "access": "public"
20
20
  },
21
- "dependencies": {
22
- "@geekmidas/logger": "0.0.1",
23
- "@geekmidas/services": "0.0.1",
24
- "@geekmidas/cache": "0.0.7"
21
+ "dependencies": {},
22
+ "peerDependencies": {
23
+ "@geekmidas/cache": "^0.1.0",
24
+ "@geekmidas/logger": "^0.3.0",
25
+ "@geekmidas/services": "^0.1.0"
26
+ },
27
+ "peerDependenciesMeta": {
28
+ "@geekmidas/cache": {
29
+ "optional": true
30
+ },
31
+ "@geekmidas/logger": {
32
+ "optional": true
33
+ },
34
+ "@geekmidas/services": {
35
+ "optional": true
36
+ }
37
+ },
38
+ "devDependencies": {
39
+ "@geekmidas/cache": "^0.1.0",
40
+ "@geekmidas/logger": "^0.3.0",
41
+ "@geekmidas/services": "^0.1.0"
25
42
  },
26
- "peerDependencies": {},
27
43
  "scripts": {
28
44
  "ts": "tsc --noEmit --skipLibCheck src/**/*.ts"
29
45
  }
@@ -0,0 +1,110 @@
1
+ import { InMemoryCache } from '@geekmidas/cache/memory';
2
+ import { bench, describe } from 'vitest';
3
+ import {
4
+ type RateLimitConfig,
5
+ type RateLimitContext,
6
+ type RateLimitData,
7
+ checkRateLimit,
8
+ } from '../index';
9
+
10
+ // Helper to create a mock context
11
+ function createContext(ip: string): RateLimitContext {
12
+ return {
13
+ header: (key: string) => (key === 'x-forwarded-for' ? ip : undefined),
14
+ services: {},
15
+ logger: {
16
+ info: () => {},
17
+ error: () => {},
18
+ debug: () => {},
19
+ warn: () => {},
20
+ } as any,
21
+ session: {},
22
+ path: '/test',
23
+ method: 'GET',
24
+ };
25
+ }
26
+
27
+ describe('Rate Limiting', () => {
28
+ const cache = new InMemoryCache<RateLimitData>();
29
+ const config: RateLimitConfig<RateLimitData> = {
30
+ limit: 100,
31
+ windowMs: 60000,
32
+ cache,
33
+ };
34
+
35
+ bench('checkRateLimit - single IP', async () => {
36
+ const ctx = createContext('127.0.0.1');
37
+ await checkRateLimit(config, ctx);
38
+ });
39
+
40
+ bench('checkRateLimit - varying IPs', async () => {
41
+ const ip = `192.168.1.${Math.floor(Math.random() * 255)}`;
42
+ const ctx = createContext(ip);
43
+ await checkRateLimit(config, ctx);
44
+ });
45
+ });
46
+
47
+ describe('Rate Limiting - High Volume', () => {
48
+ bench('100 requests same IP', async () => {
49
+ const cache = new InMemoryCache<RateLimitData>();
50
+ const config: RateLimitConfig<RateLimitData> = {
51
+ limit: 1000,
52
+ windowMs: 60000,
53
+ cache,
54
+ };
55
+ const ctx = createContext('10.0.0.1');
56
+
57
+ for (let i = 0; i < 100; i++) {
58
+ await checkRateLimit(config, ctx);
59
+ }
60
+ });
61
+
62
+ bench('100 requests different IPs', async () => {
63
+ const cache = new InMemoryCache<RateLimitData>();
64
+ const config: RateLimitConfig<RateLimitData> = {
65
+ limit: 100,
66
+ windowMs: 60000,
67
+ cache,
68
+ };
69
+
70
+ for (let i = 0; i < 100; i++) {
71
+ const ctx = createContext(`10.0.0.${i}`);
72
+ await checkRateLimit(config, ctx);
73
+ }
74
+ });
75
+ });
76
+
77
+ describe('Rate Limiting - Window Sizes', () => {
78
+ bench('1 second window', async () => {
79
+ const cache = new InMemoryCache<RateLimitData>();
80
+ const config: RateLimitConfig<RateLimitData> = {
81
+ limit: 10,
82
+ windowMs: 1000,
83
+ cache,
84
+ };
85
+ const ctx = createContext('127.0.0.1');
86
+ await checkRateLimit(config, ctx);
87
+ });
88
+
89
+ bench('1 minute window', async () => {
90
+ const cache = new InMemoryCache<RateLimitData>();
91
+ const config: RateLimitConfig<RateLimitData> = {
92
+ limit: 100,
93
+ windowMs: 60000,
94
+ cache,
95
+ };
96
+ const ctx = createContext('127.0.0.1');
97
+ await checkRateLimit(config, ctx);
98
+ });
99
+
100
+ bench('1 hour window', async () => {
101
+ const cache = new InMemoryCache<RateLimitData>();
102
+ const config: RateLimitConfig<RateLimitData> = {
103
+ limit: 1000,
104
+ windowMs: 3600000,
105
+ cache,
106
+ };
107
+ const ctx = createContext('127.0.0.1');
108
+ await checkRateLimit(config, ctx);
109
+ });
110
+ });