ayiin 2.0.45 → 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.
@@ -63,11 +63,8 @@ class Limiter extends tools_1.default {
63
63
  const userId = options.identifyUser
64
64
  ? options.identifyUser(req, res)
65
65
  : (req.ip ?? "unknown");
66
- // key storage
67
66
  const floodKey = `${options.keyPrefix}:flood:${userId}`;
68
67
  const levelKey = `${options.keyPrefix}:level:${userId}`;
69
- const rateKey = `${options.keyPrefix}:rate:${userId}`;
70
- // cek jika user terblock atau tidak
71
68
  const blockedUntilStr = await store.get(floodKey);
72
69
  if (blockedUntilStr) {
73
70
  const blockedUntil = new Date(blockedUntilStr).getTime();
@@ -84,33 +81,32 @@ class Limiter extends tools_1.default {
84
81
  return res.status(429).json({ success: false, error: msg });
85
82
  }
86
83
  }
84
+ const rateKey = `${options.keyPrefix}:rate:${userId}`;
85
+ const hits = await store.incr(rateKey);
87
86
  const windowMs = this.toMilliseconds(options.windowMs);
88
87
  const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
88
+ if (hits === 1) {
89
+ await store.expire(rateKey, windowMs);
90
+ }
89
91
  // ambil level sekarang
90
92
  let level = parseInt((await store.get(levelKey)) ?? "0", 10);
91
93
  // tentukan limit sesuai level
92
94
  let currentLimit;
93
95
  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];
96
+ const limitIdx = Math.min(level, options.limit.length - 1);
97
+ const limit = options.limit[limitIdx];
98
+ currentLimit = limit;
97
99
  }
98
100
  else {
99
101
  currentLimit = Number(options.limit);
100
102
  }
101
- // hitung permintaan sekarang
102
- const hits = await store.incr(rateKey);
103
- if (hits === 1) {
104
- await store.expire(rateKey, windowMs);
105
- }
106
- // cek level jika permintaan melebihi limit
107
103
  if (hits > currentLimit) {
108
104
  level += 1;
109
105
  await store.set(levelKey, level.toString(), refreshAtMs);
110
106
  let floodwaitMs;
111
107
  if (Array.isArray(options.floodwaitMs)) {
112
- const floodIdx = Math.min(level - 1, options.floodwaitMs.length - 1);
113
- const floodwait = options.floodwaitMs[floodIdx];
108
+ const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
109
+ const floodwait = options.floodwaitMs[idx];
114
110
  floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
115
111
  }
116
112
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ayiin",
3
- "version": "2.0.45",
3
+ "version": "2.0.46",
4
4
  "author": {
5
5
  "name": "AyiinXd",
6
6
  "url": "https://github.com/AyiinXd"