ayiin 2.0.41 → 2.0.42
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 +1 -3
- package/dist/index.js +1 -9
- package/dist/methods/limiter.js +18 -9
- package/dist/types.d.ts +2 -2
- package/package.json +2 -5
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
|
-
|
|
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
|
-
|
|
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;
|
package/dist/methods/limiter.js
CHANGED
|
@@ -44,7 +44,7 @@ class MemoryStore {
|
|
|
44
44
|
// 🔹 Class Limiter
|
|
45
45
|
class Limiter extends tools_1.default {
|
|
46
46
|
createRateLimit = (options) => {
|
|
47
|
-
const store = options.store ?? new MemoryStore();
|
|
47
|
+
const store = options.store ?? new MemoryStore();
|
|
48
48
|
return async (req, res, next) => {
|
|
49
49
|
const now = Date.now();
|
|
50
50
|
const userId = options.identifyUser
|
|
@@ -62,7 +62,9 @@ class Limiter extends tools_1.default {
|
|
|
62
62
|
api: options.keyPrefix,
|
|
63
63
|
remainMs: remainingMs,
|
|
64
64
|
remain: this.formatRemainingTime(remainingMs),
|
|
65
|
-
limit: options.limit
|
|
65
|
+
limit: Array.isArray(options.limit)
|
|
66
|
+
? options.limit[0]
|
|
67
|
+
: options.limit,
|
|
66
68
|
})
|
|
67
69
|
: (options.messageFormat ??
|
|
68
70
|
`[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`);
|
|
@@ -76,19 +78,26 @@ class Limiter extends tools_1.default {
|
|
|
76
78
|
if (hits === 1) {
|
|
77
79
|
await store.expire(rateKey, windowMs);
|
|
78
80
|
}
|
|
79
|
-
|
|
81
|
+
// ambil level sekarang
|
|
82
|
+
let level = parseInt((await store.get(levelKey)) ?? "0", 10);
|
|
83
|
+
// tentukan limit sesuai level
|
|
84
|
+
let currentLimit;
|
|
85
|
+
if (Array.isArray(options.limit)) {
|
|
86
|
+
currentLimit = options.limit[Math.min(level, options.limit.length - 1)];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
currentLimit = Number(options.limit);
|
|
90
|
+
}
|
|
91
|
+
if (hits > currentLimit) {
|
|
92
|
+
level += 1;
|
|
93
|
+
await store.set(levelKey, level.toString(), refreshAtMs);
|
|
80
94
|
let floodwaitMs;
|
|
81
|
-
let level;
|
|
82
95
|
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
96
|
const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
|
|
87
97
|
const floodwait = options.floodwaitMs[idx];
|
|
88
98
|
floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
|
|
89
99
|
}
|
|
90
100
|
else {
|
|
91
|
-
// fixed mode
|
|
92
101
|
floodwaitMs = this.toMilliseconds(options.floodwaitMs);
|
|
93
102
|
}
|
|
94
103
|
const untilDate = new Date(now + floodwaitMs).toISOString();
|
|
@@ -99,7 +108,7 @@ class Limiter extends tools_1.default {
|
|
|
99
108
|
remainMs: floodwaitMs,
|
|
100
109
|
remain: this.formatRemainingTime(floodwaitMs),
|
|
101
110
|
level,
|
|
102
|
-
limit:
|
|
111
|
+
limit: currentLimit,
|
|
103
112
|
})
|
|
104
113
|
: (options.messageFormat ??
|
|
105
114
|
`[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`);
|
package/dist/types.d.ts
CHANGED
|
@@ -12,9 +12,9 @@ export interface DurationInput {
|
|
|
12
12
|
}
|
|
13
13
|
export interface RateLimiterOptions {
|
|
14
14
|
keyPrefix: string;
|
|
15
|
-
limit: number;
|
|
15
|
+
limit: number | readonly [number, ...number[]];
|
|
16
16
|
windowMs: DurationInput;
|
|
17
|
-
floodwaitMs: DurationInput | DurationInput[];
|
|
17
|
+
floodwaitMs: DurationInput | readonly [DurationInput, ...DurationInput[]];
|
|
18
18
|
identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
|
|
19
19
|
store?: RateLimitStore;
|
|
20
20
|
refreshAt?: DurationInput;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ayiin",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.42",
|
|
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
|
}
|