ayiin 2.0.42 → 2.0.43

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.
@@ -40,11 +40,23 @@ class MemoryStore {
40
40
  entry.expireAt = Date.now() + ttlMs;
41
41
  }
42
42
  }
43
+ refresh() {
44
+ this.store.clear();
45
+ }
43
46
  }
44
47
  // 🔹 Class Limiter
45
48
  class Limiter extends tools_1.default {
46
49
  createRateLimit = (options) => {
47
50
  const store = options.store ?? new MemoryStore();
51
+ if (options.refreshOnStart) {
52
+ if (typeof store.refresh === "function") {
53
+ store.refresh();
54
+ console.log(`[RateLimiter] ${store.constructor.name} refreshed on start.`);
55
+ }
56
+ else {
57
+ console.warn(`[RateLimiter] ${store.constructor.name} does not implement refresh() method.`);
58
+ }
59
+ }
48
60
  return async (req, res, next) => {
49
61
  const now = Date.now();
50
62
  const userId = options.identifyUser
@@ -83,7 +95,9 @@ class Limiter extends tools_1.default {
83
95
  // tentukan limit sesuai level
84
96
  let currentLimit;
85
97
  if (Array.isArray(options.limit)) {
86
- currentLimit = options.limit[Math.min(level, options.limit.length - 1)];
98
+ const idx = Math.min(level, options.limit.length - 1);
99
+ const limit = options.limit[idx];
100
+ currentLimit = limit;
87
101
  }
88
102
  else {
89
103
  currentLimit = Number(options.limit);
package/dist/types.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface RateLimitStore {
4
4
  set(key: string, value: string, ttlMs?: number): Promise<void>;
5
5
  incr(key: string): Promise<number>;
6
6
  expire(key: string, ttlMs: number): Promise<void>;
7
+ refresh?(): Promise<void> | void;
7
8
  }
8
9
  export interface DurationInput {
9
10
  hours?: number;
@@ -18,6 +19,7 @@ export interface RateLimiterOptions {
18
19
  identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
19
20
  store?: RateLimitStore;
20
21
  refreshAt?: DurationInput;
22
+ refreshOnStart?: boolean;
21
23
  messageFormat?: string | ((ctx: {
22
24
  api: string;
23
25
  remainMs: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ayiin",
3
- "version": "2.0.42",
3
+ "version": "2.0.43",
4
4
  "author": {
5
5
  "name": "AyiinXd",
6
6
  "url": "https://github.com/AyiinXd"