ayiin 2.0.41 → 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.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,6 @@
1
- import Redis from "ioredis";
2
1
  import Methods from "./methods";
3
2
  export type { DurationInput, RateLimitStore, RateLimiterOptions, } from "./types";
4
3
  export default class Ayiin extends Methods {
5
4
  version: string;
6
- cache: Redis | undefined;
7
- constructor(databaseUrl?: string);
5
+ constructor();
8
6
  }
package/dist/index.js CHANGED
@@ -3,20 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const ioredis_1 = __importDefault(require("ioredis"));
7
6
  const package_json_1 = __importDefault(require("../package.json"));
8
7
  const methods_1 = __importDefault(require("./methods"));
9
8
  class Ayiin extends methods_1.default {
10
9
  version = package_json_1.default.version;
11
- cache;
12
- constructor(databaseUrl) {
10
+ constructor() {
13
11
  super();
14
- if (databaseUrl) {
15
- this.cache = new ioredis_1.default(databaseUrl);
16
- }
17
- else {
18
- this.cache = undefined;
19
- }
20
12
  }
21
13
  }
22
14
  exports.default = Ayiin;
@@ -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
- const store = options.store ?? new MemoryStore(); // default ke MemoryStore
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
@@ -62,7 +74,9 @@ class Limiter extends tools_1.default {
62
74
  api: options.keyPrefix,
63
75
  remainMs: remainingMs,
64
76
  remain: this.formatRemainingTime(remainingMs),
65
- limit: options.limit,
77
+ limit: Array.isArray(options.limit)
78
+ ? options.limit[0]
79
+ : options.limit,
66
80
  })
67
81
  : (options.messageFormat ??
68
82
  `[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
@@ -76,19 +90,28 @@ class Limiter extends tools_1.default {
76
90
  if (hits === 1) {
77
91
  await store.expire(rateKey, windowMs);
78
92
  }
79
- if (hits > options.limit) {
93
+ // ambil level sekarang
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
+ }
105
+ if (hits > currentLimit) {
106
+ level += 1;
107
+ await store.set(levelKey, level.toString(), refreshAtMs);
80
108
  let floodwaitMs;
81
- let level;
82
109
  if (Array.isArray(options.floodwaitMs)) {
83
- // escalation mode
84
- level = parseInt((await store.get(levelKey)) ?? "0", 10) + 1;
85
- await store.set(levelKey, level.toString(), refreshAtMs);
86
110
  const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
87
111
  const floodwait = options.floodwaitMs[idx];
88
112
  floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
89
113
  }
90
114
  else {
91
- // fixed mode
92
115
  floodwaitMs = this.toMilliseconds(options.floodwaitMs);
93
116
  }
94
117
  const untilDate = new Date(now + floodwaitMs).toISOString();
@@ -99,7 +122,7 @@ class Limiter extends tools_1.default {
99
122
  remainMs: floodwaitMs,
100
123
  remain: this.formatRemainingTime(floodwaitMs),
101
124
  level,
102
- limit: options.limit,
125
+ limit: currentLimit,
103
126
  })
104
127
  : (options.messageFormat ??
105
128
  `[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
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;
@@ -12,12 +13,13 @@ export interface DurationInput {
12
13
  }
13
14
  export interface RateLimiterOptions {
14
15
  keyPrefix: string;
15
- limit: number;
16
+ limit: number | readonly [number, ...number[]];
16
17
  windowMs: DurationInput;
17
- floodwaitMs: DurationInput | DurationInput[];
18
+ floodwaitMs: DurationInput | readonly [DurationInput, ...DurationInput[]];
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.41",
3
+ "version": "2.0.43",
4
4
  "author": {
5
5
  "name": "AyiinXd",
6
6
  "url": "https://github.com/AyiinXd"
@@ -26,8 +26,5 @@
26
26
  "scripts": {
27
27
  "build": "tsc"
28
28
  },
29
- "types": "./dist/index.d.ts",
30
- "dependencies": {
31
- "ioredis": "^6.0.0"
32
- }
29
+ "types": "./dist/index.d.ts"
33
30
  }