ayiin 2.0.44 → 2.0.45
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 +19 -22
- package/dist/types.d.ts +0 -2
- package/package.json +1 -1
package/dist/methods/limiter.js
CHANGED
|
@@ -50,8 +50,9 @@ class Limiter extends tools_1.default {
|
|
|
50
50
|
const store = options.store ?? new MemoryStore();
|
|
51
51
|
if (options.refreshOnStart) {
|
|
52
52
|
if (typeof store.refresh === "function") {
|
|
53
|
-
store.refresh()
|
|
54
|
-
|
|
53
|
+
Promise.resolve(store.refresh()).then(() => {
|
|
54
|
+
console.log(`[RateLimiter] ${store.constructor.name} refreshed on start.`);
|
|
55
|
+
});
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
57
58
|
console.warn(`[RateLimiter] ${store.constructor.name} does not implement refresh() method.`);
|
|
@@ -66,20 +67,6 @@ class Limiter extends tools_1.default {
|
|
|
66
67
|
const floodKey = `${options.keyPrefix}:flood:${userId}`;
|
|
67
68
|
const levelKey = `${options.keyPrefix}:level:${userId}`;
|
|
68
69
|
const rateKey = `${options.keyPrefix}:rate:${userId}`;
|
|
69
|
-
const windowMs = this.toMilliseconds(options.windowMs);
|
|
70
|
-
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
71
|
-
// ambil level sekarang
|
|
72
|
-
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
73
|
-
// tentukan limit sesuai level
|
|
74
|
-
let currentLimit;
|
|
75
|
-
if (Array.isArray(options.limit)) {
|
|
76
|
-
const idx = Math.min(level, options.limit.length - 1);
|
|
77
|
-
const limit = options.limit[idx];
|
|
78
|
-
currentLimit = limit;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
currentLimit = Number(options.limit);
|
|
82
|
-
}
|
|
83
70
|
// cek jika user terblock atau tidak
|
|
84
71
|
const blockedUntilStr = await store.get(floodKey);
|
|
85
72
|
if (blockedUntilStr) {
|
|
@@ -91,14 +78,26 @@ class Limiter extends tools_1.default {
|
|
|
91
78
|
api: options.keyPrefix,
|
|
92
79
|
remainMs: remainingMs,
|
|
93
80
|
remain: this.formatRemainingTime(remainingMs),
|
|
94
|
-
limit: currentLimit,
|
|
95
|
-
level,
|
|
96
81
|
})
|
|
97
82
|
: (options.messageFormat ??
|
|
98
83
|
`[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
|
|
99
84
|
return res.status(429).json({ success: false, error: msg });
|
|
100
85
|
}
|
|
101
86
|
}
|
|
87
|
+
const windowMs = this.toMilliseconds(options.windowMs);
|
|
88
|
+
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
89
|
+
// ambil level sekarang
|
|
90
|
+
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
91
|
+
// tentukan limit sesuai level
|
|
92
|
+
let currentLimit;
|
|
93
|
+
if (Array.isArray(options.limit)) {
|
|
94
|
+
// gunakan level sebagai offset - 1
|
|
95
|
+
const limitIdx = Math.min(level - 1, options.limit.length - 1);
|
|
96
|
+
currentLimit = options.limit[limitIdx];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
currentLimit = Number(options.limit);
|
|
100
|
+
}
|
|
102
101
|
// hitung permintaan sekarang
|
|
103
102
|
const hits = await store.incr(rateKey);
|
|
104
103
|
if (hits === 1) {
|
|
@@ -110,8 +109,8 @@ class Limiter extends tools_1.default {
|
|
|
110
109
|
await store.set(levelKey, level.toString(), refreshAtMs);
|
|
111
110
|
let floodwaitMs;
|
|
112
111
|
if (Array.isArray(options.floodwaitMs)) {
|
|
113
|
-
const
|
|
114
|
-
const floodwait = options.floodwaitMs[
|
|
112
|
+
const floodIdx = Math.min(level - 1, options.floodwaitMs.length - 1);
|
|
113
|
+
const floodwait = options.floodwaitMs[floodIdx];
|
|
115
114
|
floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
|
|
116
115
|
}
|
|
117
116
|
else {
|
|
@@ -124,8 +123,6 @@ class Limiter extends tools_1.default {
|
|
|
124
123
|
api: options.keyPrefix,
|
|
125
124
|
remainMs: floodwaitMs,
|
|
126
125
|
remain: this.formatRemainingTime(floodwaitMs),
|
|
127
|
-
level,
|
|
128
|
-
limit: currentLimit,
|
|
129
126
|
})
|
|
130
127
|
: (options.messageFormat ??
|
|
131
128
|
`[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
|
package/dist/types.d.ts
CHANGED