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