ayiin 2.0.45 → 2.0.47
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/methods/limiter.js +21 -11
- package/package.json +1 -1
package/dist/methods/limiter.js
CHANGED
|
@@ -84,33 +84,43 @@ class Limiter extends tools_1.default {
|
|
|
84
84
|
return res.status(429).json({ success: false, error: msg });
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
|
-
const windowMs = this.toMilliseconds(options.windowMs);
|
|
88
|
-
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
89
87
|
// ambil level sekarang
|
|
90
88
|
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
89
|
+
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
90
|
+
// tentukan windowMs sesuai level
|
|
91
|
+
let windowMs;
|
|
92
|
+
if (Array.isArray(options.windowMs)) {
|
|
93
|
+
const idx = Math.min(level, options.windowMs.length - 1);
|
|
94
|
+
const win = options.windowMs[idx];
|
|
95
|
+
windowMs = this.toMilliseconds(win);
|
|
97
96
|
}
|
|
98
97
|
else {
|
|
99
|
-
|
|
98
|
+
windowMs = this.toMilliseconds(options.windowMs);
|
|
100
99
|
}
|
|
101
100
|
// hitung permintaan sekarang
|
|
102
101
|
const hits = await store.incr(rateKey);
|
|
103
102
|
if (hits === 1) {
|
|
104
103
|
await store.expire(rateKey, windowMs);
|
|
105
104
|
}
|
|
105
|
+
// tentukan limit sesuai level (langsung pakai level sebagai index)
|
|
106
|
+
let currentLimit;
|
|
107
|
+
if (Array.isArray(options.limit)) {
|
|
108
|
+
const limitIdx = Math.min(level, options.limit.length - 1);
|
|
109
|
+
currentLimit = options.limit[limitIdx];
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
currentLimit = Number(options.limit);
|
|
113
|
+
}
|
|
106
114
|
// cek level jika permintaan melebihi limit
|
|
107
115
|
if (hits > currentLimit) {
|
|
108
116
|
level += 1;
|
|
109
117
|
await store.set(levelKey, level.toString(), refreshAtMs);
|
|
118
|
+
// reset hits untuk level baru
|
|
119
|
+
await store.set(rateKey, "0", windowMs);
|
|
110
120
|
let floodwaitMs;
|
|
111
121
|
if (Array.isArray(options.floodwaitMs)) {
|
|
112
|
-
const
|
|
113
|
-
const floodwait = options.floodwaitMs[
|
|
122
|
+
const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
|
|
123
|
+
const floodwait = options.floodwaitMs[idx];
|
|
114
124
|
floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
|
|
115
125
|
}
|
|
116
126
|
else {
|