ayiin 2.0.38 → 2.0.39
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/index.d.ts +1 -0
- package/dist/methods/limiter.js +17 -2
- package/dist/types.d.ts +2 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/methods/limiter.js
CHANGED
|
@@ -51,6 +51,7 @@ class Limiter extends tools_1.default {
|
|
|
51
51
|
? options.identifyUser(req, res)
|
|
52
52
|
: (req.ip ?? "unknown");
|
|
53
53
|
const floodKey = `${options.keyPrefix}:flood:${userId}`;
|
|
54
|
+
const levelKey = `${options.keyPrefix}:level:${userId}`;
|
|
54
55
|
const blockedUntilStr = await store.get(floodKey);
|
|
55
56
|
if (blockedUntilStr) {
|
|
56
57
|
const blockedUntil = new Date(blockedUntilStr).getTime();
|
|
@@ -65,16 +66,30 @@ class Limiter extends tools_1.default {
|
|
|
65
66
|
const rateKey = `${options.keyPrefix}:rate:${userId}`;
|
|
66
67
|
const hits = await store.incr(rateKey);
|
|
67
68
|
const windowMs = this.toMilliseconds(options.windowMs);
|
|
68
|
-
const
|
|
69
|
+
const baseFloodwaitMs = this.toMilliseconds(options.floodwaitMs);
|
|
70
|
+
const levelTtlMs = this.toMilliseconds(options.levelTtl ?? { hours: 24 });
|
|
69
71
|
if (hits === 1) {
|
|
70
72
|
await store.expire(rateKey, windowMs);
|
|
71
73
|
}
|
|
72
74
|
if (hits > options.limit) {
|
|
75
|
+
let floodwaitMs = baseFloodwaitMs;
|
|
76
|
+
if (options.levels && options.levels.length > 0) {
|
|
77
|
+
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
78
|
+
level += 1;
|
|
79
|
+
// simpan level dengan TTL (reset otomatis setelah levelTtlMs)
|
|
80
|
+
await store.set(levelKey, level.toString(), levelTtlMs);
|
|
81
|
+
// pilih durasi floodwait dari config levels
|
|
82
|
+
const idx = Math.min(level - 1, options.levels.length - 1);
|
|
83
|
+
const levelDuration = options.levels[idx];
|
|
84
|
+
if (levelDuration) {
|
|
85
|
+
floodwaitMs = this.toMilliseconds(levelDuration);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
73
88
|
const untilDate = new Date(now + floodwaitMs).toISOString();
|
|
74
89
|
await store.set(floodKey, untilDate, floodwaitMs);
|
|
75
90
|
return res.status(429).json({
|
|
76
91
|
success: false,
|
|
77
|
-
error: `Terlalu banyak request ${options.keyPrefix}. Floodwait ${this.formatRemainingTime(floodwaitMs)}.`,
|
|
92
|
+
error: `Terlalu banyak request ${options.keyPrefix}. Floodwait aktif selama ${this.formatRemainingTime(floodwaitMs)}.`,
|
|
78
93
|
});
|
|
79
94
|
}
|
|
80
95
|
next();
|
package/dist/types.d.ts
CHANGED
|
@@ -4,11 +4,6 @@ export interface DurationInput {
|
|
|
4
4
|
minutes?: number;
|
|
5
5
|
seconds?: number;
|
|
6
6
|
}
|
|
7
|
-
export interface DurationInput {
|
|
8
|
-
hours?: number;
|
|
9
|
-
minutes?: number;
|
|
10
|
-
seconds?: number;
|
|
11
|
-
}
|
|
12
7
|
export interface RateLimiterOptions {
|
|
13
8
|
keyPrefix: string;
|
|
14
9
|
limit: number;
|
|
@@ -16,6 +11,8 @@ export interface RateLimiterOptions {
|
|
|
16
11
|
floodwaitMs: DurationInput;
|
|
17
12
|
identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
|
|
18
13
|
store?: RateLimitStore;
|
|
14
|
+
levels?: DurationInput[];
|
|
15
|
+
levelTtl?: DurationInput;
|
|
19
16
|
}
|
|
20
17
|
export interface RateLimitStore {
|
|
21
18
|
get(key: string): Promise<string | undefined>;
|