ayiin 2.0.38 → 2.0.40
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 -0
- package/dist/methods/limiter.d.ts +1 -0
- package/dist/methods/limiter.js +44 -9
- package/dist/types.d.ts +9 -11
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/methods/limiter.js
CHANGED
|
@@ -51,34 +51,69 @@ class Limiter extends tools_1.default {
|
|
|
51
51
|
? options.identifyUser(req, res)
|
|
52
52
|
: (req.ip ?? "unknown");
|
|
53
53
|
const floodKey = `${options.keyPrefix}:flood:${userId}`;
|
|
54
|
+
const levelKey = `${options.keyPrefix}:level:${userId}`;
|
|
54
55
|
const blockedUntilStr = await store.get(floodKey);
|
|
55
56
|
if (blockedUntilStr) {
|
|
56
57
|
const blockedUntil = new Date(blockedUntilStr).getTime();
|
|
57
58
|
const remainingMs = blockedUntil - now;
|
|
58
59
|
if (remainingMs > 0) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const msg = options.messageFormat
|
|
61
|
+
? this.formatMessage(options.messageFormat, {
|
|
62
|
+
api: options.keyPrefix,
|
|
63
|
+
remainMs: remainingMs,
|
|
64
|
+
remain: this.formatRemainingTime(remainingMs),
|
|
65
|
+
limit: options.limit,
|
|
66
|
+
})
|
|
67
|
+
: `[FloodWait] - Silakan coba lagi dalam ${this.formatRemainingTime(remainingMs)}.`;
|
|
68
|
+
return res.status(429).json({ success: false, error: msg });
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
71
|
const rateKey = `${options.keyPrefix}:rate:${userId}`;
|
|
66
72
|
const hits = await store.incr(rateKey);
|
|
67
73
|
const windowMs = this.toMilliseconds(options.windowMs);
|
|
68
|
-
const
|
|
74
|
+
const refreshAtMs = this.toMilliseconds(options.refreshAt ?? { hours: 24 });
|
|
69
75
|
if (hits === 1) {
|
|
70
76
|
await store.expire(rateKey, windowMs);
|
|
71
77
|
}
|
|
72
78
|
if (hits > options.limit) {
|
|
79
|
+
let floodwaitMs;
|
|
80
|
+
let level;
|
|
81
|
+
if (Array.isArray(options.floodwaitMs)) {
|
|
82
|
+
// escalation mode
|
|
83
|
+
level = parseInt((await store.get(levelKey)) ?? "0", 10) + 1;
|
|
84
|
+
await store.set(levelKey, level.toString(), refreshAtMs);
|
|
85
|
+
const idx = Math.min(level - 1, options.floodwaitMs.length - 1);
|
|
86
|
+
const floodwait = options.floodwaitMs[idx];
|
|
87
|
+
floodwaitMs = this.toMilliseconds(floodwait ?? options.floodwaitMs[0]);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
// fixed mode
|
|
91
|
+
floodwaitMs = this.toMilliseconds(options.floodwaitMs);
|
|
92
|
+
}
|
|
73
93
|
const untilDate = new Date(now + floodwaitMs).toISOString();
|
|
74
94
|
await store.set(floodKey, untilDate, floodwaitMs);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
95
|
+
const msg = options.messageFormat
|
|
96
|
+
? this.formatMessage(options.messageFormat, {
|
|
97
|
+
api: options.keyPrefix,
|
|
98
|
+
remainMs: floodwaitMs,
|
|
99
|
+
remain: this.formatRemainingTime(floodwaitMs),
|
|
100
|
+
level,
|
|
101
|
+
limit: options.limit,
|
|
102
|
+
})
|
|
103
|
+
: `[FloodWait] - Terlalu banyak permintaan. Silakan coba lagi dalam ${this.formatRemainingTime(floodwaitMs)}.`;
|
|
104
|
+
return res.status(429).json({ success: false, error: msg });
|
|
79
105
|
}
|
|
80
106
|
next();
|
|
81
107
|
};
|
|
82
108
|
};
|
|
109
|
+
// internal formatter
|
|
110
|
+
formatMessage(template, ctx) {
|
|
111
|
+
return template
|
|
112
|
+
.replace("%(api)s", ctx.api)
|
|
113
|
+
.replace("%(remainMs)s", ctx.remainMs.toString())
|
|
114
|
+
.replace("%(remain)s", ctx.remain)
|
|
115
|
+
.replace("%(level)s", ctx.level?.toString() ?? "1")
|
|
116
|
+
.replace("%(limit)s", ctx.limit?.toString() ?? "");
|
|
117
|
+
}
|
|
83
118
|
}
|
|
84
119
|
exports.Limiter = Limiter;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Request, Response } from "express";
|
|
2
|
-
export interface
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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>;
|
|
6
7
|
}
|
|
7
8
|
export interface DurationInput {
|
|
8
9
|
hours?: number;
|
|
@@ -13,13 +14,10 @@ export interface RateLimiterOptions {
|
|
|
13
14
|
keyPrefix: string;
|
|
14
15
|
limit: number;
|
|
15
16
|
windowMs: DurationInput;
|
|
16
|
-
floodwaitMs: DurationInput;
|
|
17
|
+
floodwaitMs: DurationInput | DurationInput[];
|
|
17
18
|
identifyUser?: (req: Request, res: Response) => string | Response<any, Record<string, any>>;
|
|
18
19
|
store?: RateLimitStore;
|
|
20
|
+
refreshAt?: DurationInput;
|
|
21
|
+
messageFormat?: string extends FloodwaitPlaceholder ? string : never;
|
|
19
22
|
}
|
|
20
|
-
export
|
|
21
|
-
get(key: string): Promise<string | undefined>;
|
|
22
|
-
set(key: string, value: string, ttlMs?: number): Promise<void>;
|
|
23
|
-
incr(key: string): Promise<number>;
|
|
24
|
-
expire(key: string, ttlMs: number): Promise<void>;
|
|
25
|
-
}
|
|
23
|
+
export type FloodwaitPlaceholder = "%(api)s" | "%(remainMs)s" | "%(remain)s" | "%(level)s" | "%(limit)s";
|