@limitkit/core 0.1.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/README.md +117 -0
- package/dist/index.d.mts +702 -0
- package/dist/index.d.ts +702 -0
- package/dist/index.js +385 -0
- package/dist/index.mjs +347 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# @limitkit/core
|
|
2
|
+
|
|
3
|
+
Core rate limiting engine for **LimitKit**.
|
|
4
|
+
|
|
5
|
+
Provides the `RateLimiter`, rule system, and algorithm abstractions used by all LimitKit integrations.
|
|
6
|
+
|
|
7
|
+
👉 Main project: https://github.com/alphatrann/limitkit
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @limitkit/core
|
|
15
|
+
````
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Basic Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { RateLimiter } from "@limitkit/core"
|
|
23
|
+
import { InMemoryStore, InMemoryFixedWindow } from "@limitkit/memory"
|
|
24
|
+
|
|
25
|
+
const limiter = new RateLimiter({
|
|
26
|
+
store: new InMemoryStore(),
|
|
27
|
+
rules: [
|
|
28
|
+
{
|
|
29
|
+
name: "global",
|
|
30
|
+
key: (req) => req.ip,
|
|
31
|
+
policy: new InMemoryFixedWindow({
|
|
32
|
+
name: "fixed-window",
|
|
33
|
+
window: 60,
|
|
34
|
+
limit: 100
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Supported Algorithms
|
|
44
|
+
|
|
45
|
+
LimitKit supports multiple rate limiting algorithms:
|
|
46
|
+
|
|
47
|
+
* Fixed Window
|
|
48
|
+
* Sliding Window
|
|
49
|
+
* Sliding Window Counter
|
|
50
|
+
* Token Bucket
|
|
51
|
+
* Leaky Bucket
|
|
52
|
+
* GCRA
|
|
53
|
+
|
|
54
|
+
Algorithms are provided by store-specific packages such as `@limitkit/memory` or `@limitkit/redis`.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Rules
|
|
59
|
+
|
|
60
|
+
Each rule defines how a request is evaluated.
|
|
61
|
+
|
|
62
|
+
Example rule:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
{
|
|
66
|
+
name: "per-ip",
|
|
67
|
+
key: (req) => req.ip,
|
|
68
|
+
policy: new InMemoryFixedWindow({
|
|
69
|
+
name: "fixed-window",
|
|
70
|
+
window: 60,
|
|
71
|
+
limit: 100
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Rules can define:
|
|
77
|
+
|
|
78
|
+
* `name` — unique rule identifier
|
|
79
|
+
* `key` — request key (e.g. IP or user ID)
|
|
80
|
+
* `policy` — rate limit algorithm
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Custom Algorithms
|
|
85
|
+
|
|
86
|
+
You can create custom algorithms by implementing the `Algorithm` interface.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { Algorithm } from "@limitkit/core"
|
|
90
|
+
|
|
91
|
+
class MyAlgorithm implements Algorithm<MyConfig> {
|
|
92
|
+
|
|
93
|
+
constructor(public readonly config: MyConfig) {}
|
|
94
|
+
|
|
95
|
+
validate(): void {
|
|
96
|
+
if (this.config.limit <= 0) {
|
|
97
|
+
throw new Error("Invalid configuration")
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Custom Stores
|
|
107
|
+
|
|
108
|
+
Stores control where rate limiting state is stored.
|
|
109
|
+
|
|
110
|
+
Custom stores can implement the `Store` interface.
|
|
111
|
+
|
|
112
|
+
Example use cases:
|
|
113
|
+
|
|
114
|
+
* DynamoDB
|
|
115
|
+
* PostgreSQL
|
|
116
|
+
* MongoDB
|
|
117
|
+
* Cloudflare KV
|