ayiin 2.0.44 → 2.0.46
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 +18 -25
- 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.`);
|
|
@@ -62,25 +63,8 @@ class Limiter extends tools_1.default {
|
|
|
62
63
|
const userId = options.identifyUser
|
|
63
64
|
? options.identifyUser(req, res)
|
|
64
65
|
: (req.ip ?? "unknown");
|
|
65
|
-
// key storage
|
|
66
66
|
const floodKey = `${options.keyPrefix}:flood:${userId}`;
|
|
67
67
|
const levelKey = `${options.keyPrefix}:level:${userId}`;
|
|
68
|
-
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
|
-
// cek jika user terblock atau tidak
|
|
84
68
|
const blockedUntilStr = await store.get(floodKey);
|
|
85
69
|
if (blockedUntilStr) {
|
|
86
70
|
const blockedUntil = new Date(blockedUntilStr).getTime();
|
|
@@ -91,20 +75,31 @@ class Limiter extends tools_1.default {
|
|
|
91
75
|
api: options.keyPrefix,
|
|
92
76
|
remainMs: remainingMs,
|
|
93
77
|
remain: this.formatRemainingTime(remainingMs),
|
|
94
|
-
limit: currentLimit,
|
|
95
|
-
level,
|
|
96
78
|
})
|
|
97
79
|
: (options.messageFormat ??
|
|
98
80
|
`[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
|
|
99
81
|
return res.status(429).json({ success: false, error: msg });
|
|
100
82
|
}
|
|
101
83
|
}
|
|
102
|
-
|
|
84
|
+
const rateKey = `${options.keyPrefix}:rate:${userId}`;
|
|
103
85
|
const hits = await store.incr(rateKey);
|
|
86
|
+
const windowMs = this.toMilliseconds(options.windowMs);
|
|
87
|
+
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
104
88
|
if (hits === 1) {
|
|
105
89
|
await store.expire(rateKey, windowMs);
|
|
106
90
|
}
|
|
107
|
-
//
|
|
91
|
+
// ambil level sekarang
|
|
92
|
+
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
93
|
+
// tentukan limit sesuai level
|
|
94
|
+
let currentLimit;
|
|
95
|
+
if (Array.isArray(options.limit)) {
|
|
96
|
+
const limitIdx = Math.min(level, options.limit.length - 1);
|
|
97
|
+
const limit = options.limit[limitIdx];
|
|
98
|
+
currentLimit = limit;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
currentLimit = Number(options.limit);
|
|
102
|
+
}
|
|
108
103
|
if (hits > currentLimit) {
|
|
109
104
|
level += 1;
|
|
110
105
|
await store.set(levelKey, level.toString(), refreshAtMs);
|
|
@@ -124,8 +119,6 @@ class Limiter extends tools_1.default {
|
|
|
124
119
|
api: options.keyPrefix,
|
|
125
120
|
remainMs: floodwaitMs,
|
|
126
121
|
remain: this.formatRemainingTime(floodwaitMs),
|
|
127
|
-
level,
|
|
128
|
-
limit: currentLimit,
|
|
129
122
|
})
|
|
130
123
|
: (options.messageFormat ??
|
|
131
124
|
`[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
|
package/dist/types.d.ts
CHANGED