@talak-web3/rate-limit 1.0.1
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/README.md +98 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# @talak-web3/rate-limit
|
|
2
|
+
|
|
3
|
+
Rate limiting utilities for talak-web3 with both in-memory and Redis-backed implementations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @talak-web3/rate-limit
|
|
9
|
+
# or
|
|
10
|
+
yarn add @talak-web3/rate-limit
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @talak-web3/rate-limit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### In-Memory (Development/Testing)
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { createRateLimiter } from '@talak-web3/rate-limit';
|
|
21
|
+
|
|
22
|
+
const limiter = createRateLimiter({
|
|
23
|
+
type: 'memory',
|
|
24
|
+
capacity: 10, // Max 10 requests
|
|
25
|
+
refillPerSecond: 1, // Refill 1 token per second
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const result = await limiter.check('user:123');
|
|
29
|
+
if (result.allowed) {
|
|
30
|
+
console.log(`Allowed! ${result.remaining} requests remaining`);
|
|
31
|
+
} else {
|
|
32
|
+
console.log(`Rate limited. Try again at ${new Date(result.resetAt!).toISOString()}`);
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Redis (Production)
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import Redis from 'ioredis';
|
|
40
|
+
import { createRateLimiter } from '@talak-web3/rate-limit';
|
|
41
|
+
|
|
42
|
+
const redis = new Redis(process.env.REDIS_URL);
|
|
43
|
+
|
|
44
|
+
const limiter = createRateLimiter({
|
|
45
|
+
type: 'redis',
|
|
46
|
+
redis,
|
|
47
|
+
capacity: 100, // Max 100 requests
|
|
48
|
+
refillPerSecond: 10, // Refill 10 tokens per second
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const result = await limiter.check('ip:192.168.1.1');
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `createRateLimiter(opts)`
|
|
57
|
+
|
|
58
|
+
Factory function that returns a rate limiter instance.
|
|
59
|
+
|
|
60
|
+
#### Options
|
|
61
|
+
|
|
62
|
+
- `type`: `'memory'` or `'redis'`
|
|
63
|
+
- `capacity`: Maximum number of requests allowed
|
|
64
|
+
- `refillPerSecond`: Rate at which tokens are refilled
|
|
65
|
+
- `redis`: Redis client instance (required for `type: 'redis'`)
|
|
66
|
+
|
|
67
|
+
### `RateLimiter` Interface
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface RateLimiter {
|
|
71
|
+
check(key: string, cost?: number): Promise<RateLimitResult>;
|
|
72
|
+
reset(key: string): Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface RateLimitResult {
|
|
76
|
+
allowed: boolean;
|
|
77
|
+
remaining: number;
|
|
78
|
+
resetAt?: number; // Timestamp when limit resets
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Algorithms
|
|
83
|
+
|
|
84
|
+
### Token Bucket (In-Memory)
|
|
85
|
+
|
|
86
|
+
- Simple token bucket algorithm
|
|
87
|
+
- Tokens refill at a constant rate
|
|
88
|
+
- Good for single-process applications
|
|
89
|
+
|
|
90
|
+
### Sliding Window (Redis)
|
|
91
|
+
|
|
92
|
+
- Uses Redis sorted sets for precise rate limiting
|
|
93
|
+
- Works across multiple processes/servers
|
|
94
|
+
- Atomic operations via Lua scripts
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@talak-web3/rate-limit",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Rate limiting utilities for talak-web3",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "node build-simple.cjs",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"clean": "rm -rf dist"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"ioredis": "^5.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"ioredis": {
|
|
29
|
+
"optional": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"rate-limit",
|
|
34
|
+
"token-bucket",
|
|
35
|
+
"sliding-window",
|
|
36
|
+
"web3"
|
|
37
|
+
],
|
|
38
|
+
"author": "Dagim Abebe",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/dagimabebe/talak-web3.git",
|
|
43
|
+
"directory": "packages/talak-web3-rate-limit"
|
|
44
|
+
}
|
|
45
|
+
}
|