ayiin 2.0.39 → 2.0.41

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,6 +1,8 @@
1
+ import Redis from "ioredis";
1
2
  import Methods from "./methods";
2
3
  export type { DurationInput, RateLimitStore, RateLimiterOptions, } from "./types";
3
4
  export default class Ayiin extends Methods {
4
5
  version: string;
5
- constructor();
6
+ cache: Redis | undefined;
7
+ constructor(databaseUrl?: string);
6
8
  }
package/dist/index.js CHANGED
@@ -3,12 +3,20 @@ 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"));
6
7
  const package_json_1 = __importDefault(require("../package.json"));
7
8
  const methods_1 = __importDefault(require("./methods"));
8
9
  class Ayiin extends methods_1.default {
9
10
  version = package_json_1.default.version;
10
- constructor() {
11
+ cache;
12
+ constructor(databaseUrl) {
11
13
  super();
14
+ if (databaseUrl) {
15
+ this.cache = new ioredis_1.default(databaseUrl);
16
+ }
17
+ else {
18
+ this.cache = undefined;
19
+ }
12
20
  }
13
21
  }
14
22
  exports.default = Ayiin;
@@ -57,40 +57,53 @@ class Limiter extends tools_1.default {
57
57
  const blockedUntil = new Date(blockedUntilStr).getTime();
58
58
  const remainingMs = blockedUntil - now;
59
59
  if (remainingMs > 0) {
60
- return res.status(429).json({
61
- success: false,
62
- error: `Floodwait aktif untuk ${options.keyPrefix}. Tunggu ${this.formatRemainingTime(remainingMs)}.`,
63
- });
60
+ const msg = typeof options.messageFormat === "function"
61
+ ? options.messageFormat({
62
+ api: options.keyPrefix,
63
+ remainMs: remainingMs,
64
+ remain: this.formatRemainingTime(remainingMs),
65
+ limit: options.limit,
66
+ })
67
+ : (options.messageFormat ??
68
+ `[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
69
+ return res.status(429).json({ success: false, error: msg });
64
70
  }
65
71
  }
66
72
  const rateKey = `${options.keyPrefix}:rate:${userId}`;
67
73
  const hits = await store.incr(rateKey);
68
74
  const windowMs = this.toMilliseconds(options.windowMs);
69
- const baseFloodwaitMs = this.toMilliseconds(options.floodwaitMs);
70
- const levelTtlMs = this.toMilliseconds(options.levelTtl ?? { hours: 24 });
75
+ const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
71
76
  if (hits === 1) {
72
77
  await store.expire(rateKey, windowMs);
73
78
  }
74
79
  if (hits > options.limit) {
75
- let floodwaitMs = baseFloodwaitMs;
76
- if (options.levels && options.levels.length > 0) {
77
- let level = parseInt((await store.get(levelKey)) ?? "0", 10);
78
- level += 1;
79
- // simpan level dengan TTL (reset otomatis setelah levelTtlMs)
80
- await store.set(levelKey, level.toString(), levelTtlMs);
81
- // pilih durasi floodwait dari config levels
82
- const idx = Math.min(level - 1, options.levels.length - 1);
83
- const levelDuration = options.levels[idx];
84
- if (levelDuration) {
85
- floodwaitMs = this.toMilliseconds(levelDuration);
86
- }
80
+ let floodwaitMs;
81
+ let level;
82
+ 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
+ const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
87
+ const floodwait = options.floodwaitMs[idx];
88
+ floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
89
+ }
90
+ else {
91
+ // fixed mode
92
+ floodwaitMs = this.toMilliseconds(options.floodwaitMs);
87
93
  }
88
94
  const untilDate = new Date(now + floodwaitMs).toISOString();
89
95
  await store.set(floodKey, untilDate, floodwaitMs);
90
- return res.status(429).json({
91
- success: false,
92
- error: `Terlalu banyak request ${options.keyPrefix}. Floodwait aktif selama ${this.formatRemainingTime(floodwaitMs)}.`,
93
- });
96
+ const msg = typeof options.messageFormat === "function"
97
+ ? options.messageFormat({
98
+ api: options.keyPrefix,
99
+ remainMs: floodwaitMs,
100
+ remain: this.formatRemainingTime(floodwaitMs),
101
+ level,
102
+ limit: options.limit,
103
+ })
104
+ : (options.messageFormat ??
105
+ `[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
106
+ return res.status(429).json({ success: false, error: msg });
94
107
  }
95
108
  next();
96
109
  };
package/dist/types.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  import { Request, Response } from "express";
2
+ export interface RateLimitStore {
3
+ get(key: string): Promise<string | undefined>;
4
+ set(key: string, value: string, ttlMs?: number): Promise<void>;
5
+ incr(key: string): Promise<number>;
6
+ expire(key: string, ttlMs: number): Promise<void>;
7
+ }
2
8
  export interface DurationInput {
3
9
  hours?: number;
4
10
  minutes?: number;
@@ -8,15 +14,15 @@ export interface RateLimiterOptions {
8
14
  keyPrefix: string;
9
15
  limit: number;
10
16
  windowMs: DurationInput;
11
- floodwaitMs: DurationInput;
17
+ floodwaitMs: DurationInput | DurationInput[];
12
18
  identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
13
19
  store?: RateLimitStore;
14
- levels?: DurationInput[];
15
- levelTtl?: DurationInput;
16
- }
17
- export interface RateLimitStore {
18
- get(key: string): Promise<string | undefined>;
19
- set(key: string, value: string, ttlMs?: number): Promise<void>;
20
- incr(key: string): Promise<number>;
21
- expire(key: string, ttlMs: number): Promise<void>;
20
+ refreshAt?: DurationInput;
21
+ messageFormat?: string | ((ctx: {
22
+ api: string;
23
+ remainMs: number;
24
+ remain: string;
25
+ level?: number;
26
+ limit?: number;
27
+ }) => string);
22
28
  }
package/package.json CHANGED
@@ -1,30 +1,33 @@
1
1
  {
2
2
  "name": "ayiin",
3
- "version": "2.0.39",
4
- "description": "Library Pribadi Yang Gak Ada Isinya",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "bin": {
8
- "axd": "./dist/bin/cli.js"
9
- },
10
- "scripts": {
11
- "build": "tsc"
3
+ "version": "2.0.41",
4
+ "author": {
5
+ "name": "AyiinXd",
6
+ "url": "https://github.com/AyiinXd"
12
7
  },
13
8
  "repository": {
14
9
  "type": "git",
15
10
  "url": "git+https://github.com/AyiinXd/ayiin.git"
16
11
  },
17
- "author": {
18
- "name": "AyiinXd",
19
- "url": "https://github.com/AyiinXd"
12
+ "main": "./dist/index.js",
13
+ "devDependencies": {
14
+ "@types/express": "^5.0.6",
15
+ "@types/node": "^26.3.0",
16
+ "typescript": "^7"
17
+ },
18
+ "bin": {
19
+ "axd": "./dist/bin/cli.js"
20
20
  },
21
+ "description": "Library Pribadi Yang Gak Ada Isinya",
21
22
  "keywords": [
22
23
  "ayiin"
23
24
  ],
24
25
  "license": "GPL-3.0-only",
25
- "devDependencies": {
26
- "@types/express": "^5.0.6",
27
- "@types/node": "^26.3.0",
28
- "typescript": "^7"
26
+ "scripts": {
27
+ "build": "tsc"
28
+ },
29
+ "types": "./dist/index.d.ts",
30
+ "dependencies": {
31
+ "ioredis": "^6.0.0"
29
32
  }
30
33
  }