@domain.js/main 0.3.16 → 0.3.19
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.
|
@@ -9,16 +9,16 @@ interface Deps {
|
|
|
9
9
|
redis: Pick<Redis, "hincrby" | "expire" | "hget" | "hdel">;
|
|
10
10
|
}
|
|
11
11
|
export declare function Main(cnf: Cnf, deps: Deps): {
|
|
12
|
-
control: (field: string, ms: number, limit: number) => Promise<number>;
|
|
13
|
-
check: (field: string, ms: number, limit: number) => Promise<number>;
|
|
12
|
+
control: (field: string, ms: number, limit: number, msg?: string) => Promise<number>;
|
|
13
|
+
check: (field: string, ms: number, limit: number, msg?: string) => Promise<number>;
|
|
14
14
|
incr: (field: string, ms: number) => Promise<number>;
|
|
15
|
-
generate: (field: string, ms: number, limit: number) =>
|
|
15
|
+
generate: (field: string, ms: number, limit: number, msg?: string) => {
|
|
16
16
|
/** 全流程控制,会自动累加次数 */
|
|
17
17
|
control(): Promise<number>;
|
|
18
18
|
/** 检测是否超限 */
|
|
19
19
|
check(): Promise<number>;
|
|
20
20
|
/** 仅做累加 */
|
|
21
21
|
incr(): Promise<number>;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
};
|
|
24
24
|
export {};
|
|
@@ -28,15 +28,16 @@ function Main(cnf, deps) {
|
|
|
28
28
|
* @param field 控频字段key
|
|
29
29
|
* @param ms 控频周期长度 毫秒
|
|
30
30
|
* @param limit 控频极限次数
|
|
31
|
+
* @param msg 报错信息
|
|
31
32
|
*/
|
|
32
|
-
const control = async (field, ms, limit) => {
|
|
33
|
+
const control = async (field, ms, limit, msg = "Too many requests") => {
|
|
33
34
|
const now = Date.now();
|
|
34
35
|
const key = getKey(ms, now);
|
|
35
36
|
const val = await redis.hincrby(key, field, 1);
|
|
36
37
|
if (val === 1)
|
|
37
38
|
await redis.expire(key, expire(ms, now));
|
|
38
39
|
if (val > limit)
|
|
39
|
-
throw Error(
|
|
40
|
+
throw Error(msg);
|
|
40
41
|
return val;
|
|
41
42
|
};
|
|
42
43
|
/**
|
|
@@ -44,13 +45,14 @@ function Main(cnf, deps) {
|
|
|
44
45
|
* @param filed 控频字段key
|
|
45
46
|
* @param ms 控频周期长度 毫秒
|
|
46
47
|
* @param limit 控频极限次数
|
|
48
|
+
* @param msg 报错信息
|
|
47
49
|
*/
|
|
48
|
-
const check = async (field, ms, limit) => {
|
|
50
|
+
const check = async (field, ms, limit, msg = "Too many requests") => {
|
|
49
51
|
const key = getKey(ms);
|
|
50
52
|
const val = await redis.hget(key, field);
|
|
51
53
|
const value = Number(val) | 0;
|
|
52
54
|
if (value > limit)
|
|
53
|
-
throw Error(
|
|
55
|
+
throw Error(msg);
|
|
54
56
|
return value;
|
|
55
57
|
};
|
|
56
58
|
/**
|
|
@@ -70,15 +72,16 @@ function Main(cnf, deps) {
|
|
|
70
72
|
* @param filed 控频字段key
|
|
71
73
|
* @param ms 控频周期长度 毫秒
|
|
72
74
|
* @param limit 控频极限次数
|
|
75
|
+
* @param msg 报错信息
|
|
73
76
|
*/
|
|
74
|
-
const generate =
|
|
77
|
+
const generate = (field, ms, limit, msg = "Too many requests") => ({
|
|
75
78
|
/** 全流程控制,会自动累加次数 */
|
|
76
79
|
control() {
|
|
77
|
-
return control(field, ms, limit);
|
|
80
|
+
return control(field, ms, limit, msg);
|
|
78
81
|
},
|
|
79
82
|
/** 检测是否超限 */
|
|
80
83
|
check() {
|
|
81
|
-
return check(field, ms, limit);
|
|
84
|
+
return check(field, ms, limit, msg);
|
|
82
85
|
},
|
|
83
86
|
/** 仅做累加 */
|
|
84
87
|
incr() {
|