@domain.js/main 0.3.15 → 0.3.18

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,7 +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: (filed: string, ms: number, limit: number) => Promise<void>;
13
- check: (filed: string, ms: number, limit: number) => Promise<void>;
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
+ incr: (field: string, ms: number) => Promise<number>;
15
+ generate: (field: string, ms: number, limit: number, msg?: string) => {
16
+ /** 全流程控制,会自动累加次数 */
17
+ control(): Promise<number>;
18
+ /** 检测是否超限 */
19
+ check(): Promise<number>;
20
+ /** 仅做累加 */
21
+ incr(): Promise<number>;
22
+ };
14
23
  };
15
24
  export {};
@@ -8,49 +8,86 @@ function Main(cnf, deps) {
8
8
  /**
9
9
  * 计算应该的key
10
10
  * @param ms 频率控制长度,单位毫秒
11
+ * @param now 当前时间戳 毫秒值
11
12
  */
12
- const getKey = (ms) => {
13
- const now = Date.now();
13
+ const getKey = (ms, now = Date.now()) => {
14
14
  const t = Math.floor(now / ms);
15
15
  return `${KEY}-${ms}-${t}`;
16
16
  };
17
17
  /**
18
18
  * 计算过期时间
19
19
  * @param ms 频率控制长度,单位毫秒
20
+ * @param now 当前时间戳 毫秒值
20
21
  */
21
- const expire = (ms) => {
22
- const now = Date.now();
22
+ const expire = (ms, now = Date.now()) => {
23
23
  const t = Math.floor(now / ms);
24
24
  return Math.floor((ms * (t + 1) - now) / 1000);
25
25
  };
26
26
  /**
27
27
  * 频次控制,超过显示过抛出异常
28
- * @param filed 控频字段key
28
+ * @param field 控频字段key
29
29
  * @param ms 控频周期长度 毫秒
30
30
  * @param limit 控频极限次数
31
+ * @param msg 报错信息
31
32
  */
32
- const control = async (filed, ms, limit) => {
33
- const key = getKey(ms);
34
- const val = await redis.hincrby(key, filed, 1);
33
+ const control = async (field, ms, limit, msg = "Too many requests") => {
34
+ const now = Date.now();
35
+ const key = getKey(ms, now);
36
+ const val = await redis.hincrby(key, field, 1);
35
37
  if (val === 1)
36
- await redis.expire(key, expire(ms));
38
+ await redis.expire(key, expire(ms, now));
37
39
  if (val > limit)
38
40
  throw Error("Too many request");
41
+ return val;
39
42
  };
40
43
  /**
41
44
  * 检测频次控制,超过显示过抛出异常
42
45
  * @param filed 控频字段key
43
46
  * @param ms 控频周期长度 毫秒
44
47
  * @param limit 控频极限次数
48
+ * @param msg 报错信息
45
49
  */
46
- const check = async (filed, ms, limit) => {
50
+ const check = async (field, ms, limit, msg = "Too many requests") => {
47
51
  const key = getKey(ms);
48
- const val = await redis.hget(key, filed);
49
- if (!val)
50
- return;
51
- if ((Number(val) | 0) > limit)
52
- throw Error("Too many request");
52
+ const val = await redis.hget(key, field);
53
+ const value = Number(val) | 0;
54
+ if (value > limit)
55
+ throw Error(msg);
56
+ return value;
57
+ };
58
+ /**
59
+ * 控频计数+1
60
+ * @param filed 控频字段key
61
+ * @param ms 控频周期长度 毫秒
62
+ */
63
+ const incr = async (field, ms) => {
64
+ const key = getKey(ms);
65
+ const val = await redis.hincrby(key, field, 1);
66
+ if (val === 1)
67
+ await redis.expire(key, expire(ms));
68
+ return val;
53
69
  };
54
- return { control, check };
70
+ /**
71
+ * 构造一个控频对象
72
+ * @param filed 控频字段key
73
+ * @param ms 控频周期长度 毫秒
74
+ * @param limit 控频极限次数
75
+ * @param msg 报错信息
76
+ */
77
+ const generate = (field, ms, limit, msg = "Too many requests") => ({
78
+ /** 全流程控制,会自动累加次数 */
79
+ control() {
80
+ return control(field, ms, limit, msg);
81
+ },
82
+ /** 检测是否超限 */
83
+ check() {
84
+ return check(field, ms, limit, msg);
85
+ },
86
+ /** 仅做累加 */
87
+ incr() {
88
+ return incr(field, ms);
89
+ },
90
+ });
91
+ return { control, check, incr, generate };
55
92
  }
56
93
  exports.Main = Main;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.15",
3
+ "version": "0.3.18",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {