tiny-essentials 1.8.5 โ 1.9.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/dist/TinyEssentials.js +351 -28
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/TinyLevelUp.js +125 -15
- package/dist/TinyLevelUp.min.js +1 -1
- package/dist/TinyPromiseQueue.js +27 -13
- package/dist/TinyPromiseQueue.min.js +1 -1
- package/dist/TinyRateLimiter.js +236 -0
- package/dist/TinyRateLimiter.min.js +1 -0
- package/dist/legacy/libs/userLevel.cjs +125 -15
- package/dist/legacy/libs/userLevel.d.mts +86 -59
- package/dist/legacy/libs/userLevel.mjs +123 -15
- package/dist/v1/build/TinyRateLimiter.cjs +7 -0
- package/dist/v1/build/TinyRateLimiter.d.mts +3 -0
- package/dist/v1/build/TinyRateLimiter.mjs +2 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.mjs +2 -1
- package/dist/v1/libs/TinyPromiseQueue.cjs +27 -13
- package/dist/v1/libs/TinyPromiseQueue.d.mts +38 -0
- package/dist/v1/libs/TinyPromiseQueue.mjs +26 -13
- package/dist/v1/libs/TinyRateLimit.cjs +196 -0
- package/dist/v1/libs/TinyRateLimit.d.mts +86 -0
- package/dist/v1/libs/TinyRateLimit.mjs +171 -0
- package/dist/v1/libs/TinyRateLimiter.cjs +197 -0
- package/dist/v1/libs/TinyRateLimiter.d.mts +86 -0
- package/dist/v1/libs/TinyRateLimiter.mjs +173 -0
- package/docs/README.md +1 -0
- package/docs/libs/TinyLevelUp.md +208 -68
- package/docs/libs/TinyRateLimiter.md +156 -0
- package/package.json +3 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# ๐ง `TinyRateLimiter` โ A Simple Per-User Rate Limiter
|
|
2
|
+
|
|
3
|
+
The `TinyRateLimiter` is a flexible and lightweight JavaScript class designed to help you limit actions (like requests or commands) per user. You can define rules like **maximum hits**, **time windows**, and benefit from automatic memory cleanup. โจ
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## ๐ฆ Constructor
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
new TinyRateLimiter(options)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### ๐ง Options
|
|
14
|
+
|
|
15
|
+
| Option | Type | Default | Description |
|
|
16
|
+
| ----------------- | -------- | ----------- | ---------------------------------------------- |
|
|
17
|
+
| `maxHits` | `number` | `undefined` | Max number of hits per user before blocking ๐ง |
|
|
18
|
+
| `interval` | `number` | `undefined` | Time window in milliseconds โฑ๏ธ |
|
|
19
|
+
| `cleanupInterval` | `number` | `undefined` | Interval to auto-clean inactive users ๐งน |
|
|
20
|
+
| `maxIdle` | `number` | `300000` | Max idle time per user before cleanup ๐ค |
|
|
21
|
+
|
|
22
|
+
> โ ๏ธ At least one of `maxHits` or `interval` must be defined.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ๐ Methods
|
|
27
|
+
|
|
28
|
+
### ๐ `hit(userId: string): void`
|
|
29
|
+
|
|
30
|
+
Registers a hit for the given `userId`.
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
rateLimiter.hit("user123");
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
It tracks timestamps internally and automatically cleans old entries based on `interval` and `maxHits`.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
### โ `isRateLimited(userId: string): boolean`
|
|
41
|
+
|
|
42
|
+
Checks if the given `userId` is currently rate limited.
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
if (rateLimiter.isRateLimited("user123")) {
|
|
46
|
+
console.log("Too many actions! Please slow down.");
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
### ๐ `reset(userId: string): void`
|
|
53
|
+
|
|
54
|
+
Resets all stored data for the given user.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
rateLimiter.reset("user123");
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
### ๐ ๏ธ `setData(userId: string, timestamps: number[]): void`
|
|
63
|
+
|
|
64
|
+
Sets the hit timestamps for a specific user. Useful for restoring or mocking state.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
rateLimiter.setData("user123", [Date.now() - 1000, Date.now()]);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
### ๐ฅ `getData(userId: string): number[]`
|
|
73
|
+
|
|
74
|
+
Returns all hit timestamps for a given user.
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
const hits = rateLimiter.getData("user123");
|
|
78
|
+
console.log(hits); // [timestamp1, timestamp2, ...]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### ๐ง `getMaxHits(): number`
|
|
84
|
+
|
|
85
|
+
Returns the configured `maxHits` value, or throws if invalid.
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
### โณ `getInterval(): number`
|
|
90
|
+
|
|
91
|
+
Returns the configured `interval` value, or throws if invalid.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
### ๐งผ `destroy(): void`
|
|
96
|
+
|
|
97
|
+
Stops internal timers and clears all stored data. Ideal for cleanup in tests or long-running apps.
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
rateLimiter.destroy();
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## ๐งฝ Automatic Cleanup
|
|
106
|
+
|
|
107
|
+
Inactive users are automatically removed every `cleanupInterval` milliseconds if they haven't had any hits for longer than `maxIdle`.
|
|
108
|
+
|
|
109
|
+
This helps reduce memory usage and keeps your limiter lean and clean! ๐ชถ
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## ๐ฅ Errors
|
|
114
|
+
|
|
115
|
+
The constructor will throw if:
|
|
116
|
+
|
|
117
|
+
* Neither `maxHits` nor `interval` are provided.
|
|
118
|
+
* Any of the values are not positive integers.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## ๐งช Example Usage
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
const limiter = new TinyRateLimiter({
|
|
126
|
+
maxHits: 5,
|
|
127
|
+
interval: 10000,
|
|
128
|
+
cleanupInterval: 60000,
|
|
129
|
+
maxIdle: 120000
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
limiter.hit("user42");
|
|
133
|
+
|
|
134
|
+
if (limiter.isRateLimited("user42")) {
|
|
135
|
+
console.log("Rate limited! ๐");
|
|
136
|
+
} else {
|
|
137
|
+
console.log("Action allowed โ
");
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## ๐ Summary
|
|
144
|
+
|
|
145
|
+
| Feature | Support |
|
|
146
|
+
| -------------------- | ------- |
|
|
147
|
+
| Per-user tracking | โ
|
|
|
148
|
+
| Max hits | โ
|
|
|
149
|
+
| Time window interval | โ
|
|
|
150
|
+
| Automatic cleanup | โ
|
|
|
151
|
+
| Manual data control | โ
|
|
|
152
|
+
| Lightweight + fast | โ
|
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
Made with ๐ to protect your app from spammers and abusers โ in a cute and efficient way! ๐
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
"test:mjs:promisequeue": "node test/index.mjs promiseQueue",
|
|
11
11
|
"test:mjs:objtype": "node test/index.mjs objType",
|
|
12
12
|
"test:mjs:jsoncolor": "node test/index.mjs colorStringify",
|
|
13
|
+
"test:mjs:ratelimit": "node test/index.mjs rateLimit",
|
|
14
|
+
"test:mjs:levelup": "node test/index.mjs levelUp",
|
|
13
15
|
"fix:prettier": "npm run fix:prettier:src && npm run fix:prettier:test && npm run fix:prettier:rollup.config && npm run fix:prettier:webpack.config",
|
|
14
16
|
"fix:prettier:src": "prettier --write ./src/*",
|
|
15
17
|
"fix:prettier:test": "prettier --write ./test/*",
|