@domain.js/main 0.3.13 → 0.3.16

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.
@@ -3,6 +3,7 @@ import * as cache from "./cache";
3
3
  import * as checker from "./checker";
4
4
  import * as counter from "./counter";
5
5
  import * as cron from "./cron";
6
+ import * as frequency from "./frequency";
6
7
  import * as graceful from "./graceful";
7
8
  import * as hash from "./hash";
8
9
  import * as logger from "./logger";
@@ -20,6 +21,7 @@ declare const _default: {
20
21
  checker: typeof checker;
21
22
  counter: typeof counter;
22
23
  cron: typeof cron;
24
+ frequency: typeof frequency;
23
25
  graceful: typeof graceful;
24
26
  hash: typeof hash;
25
27
  logger: typeof logger;
@@ -24,6 +24,7 @@ const cache = __importStar(require("./cache"));
24
24
  const checker = __importStar(require("./checker"));
25
25
  const counter = __importStar(require("./counter"));
26
26
  const cron = __importStar(require("./cron"));
27
+ const frequency = __importStar(require("./frequency"));
27
28
  const graceful = __importStar(require("./graceful"));
28
29
  const hash = __importStar(require("./hash"));
29
30
  const logger = __importStar(require("./logger"));
@@ -41,6 +42,7 @@ module.exports = {
41
42
  checker: checker,
42
43
  counter: counter,
43
44
  cron: cron,
45
+ frequency: frequency,
44
46
  graceful: graceful,
45
47
  hash: hash,
46
48
  logger: logger,
@@ -0,0 +1,24 @@
1
+ import { Redis } from "ioredis";
2
+ interface Cnf {
3
+ frequency: {
4
+ key: string;
5
+ };
6
+ }
7
+ export declare const Deps: readonly ["redis"];
8
+ interface Deps {
9
+ redis: Pick<Redis, "hincrby" | "expire" | "hget" | "hdel">;
10
+ }
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>;
14
+ incr: (field: string, ms: number) => Promise<number>;
15
+ generate: (field: string, ms: number, limit: number) => Promise<{
16
+ /** 全流程控制,会自动累加次数 */
17
+ control(): Promise<number>;
18
+ /** 检测是否超限 */
19
+ check(): Promise<number>;
20
+ /** 仅做累加 */
21
+ incr(): Promise<number>;
22
+ }>;
23
+ };
24
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Main = exports.Deps = void 0;
4
+ exports.Deps = ["redis"];
5
+ function Main(cnf, deps) {
6
+ const { redis } = deps;
7
+ const { frequency: { key: KEY }, } = cnf;
8
+ /**
9
+ * 计算应该的key
10
+ * @param ms 频率控制长度,单位毫秒
11
+ * @param now 当前时间戳 毫秒值
12
+ */
13
+ const getKey = (ms, now = Date.now()) => {
14
+ const t = Math.floor(now / ms);
15
+ return `${KEY}-${ms}-${t}`;
16
+ };
17
+ /**
18
+ * 计算过期时间
19
+ * @param ms 频率控制长度,单位毫秒
20
+ * @param now 当前时间戳 毫秒值
21
+ */
22
+ const expire = (ms, now = Date.now()) => {
23
+ const t = Math.floor(now / ms);
24
+ return Math.floor((ms * (t + 1) - now) / 1000);
25
+ };
26
+ /**
27
+ * 频次控制,超过显示过抛出异常
28
+ * @param field 控频字段key
29
+ * @param ms 控频周期长度 毫秒
30
+ * @param limit 控频极限次数
31
+ */
32
+ const control = async (field, ms, limit) => {
33
+ const now = Date.now();
34
+ const key = getKey(ms, now);
35
+ const val = await redis.hincrby(key, field, 1);
36
+ if (val === 1)
37
+ await redis.expire(key, expire(ms, now));
38
+ if (val > limit)
39
+ throw Error("Too many request");
40
+ return val;
41
+ };
42
+ /**
43
+ * 检测频次控制,超过显示过抛出异常
44
+ * @param filed 控频字段key
45
+ * @param ms 控频周期长度 毫秒
46
+ * @param limit 控频极限次数
47
+ */
48
+ const check = async (field, ms, limit) => {
49
+ const key = getKey(ms);
50
+ const val = await redis.hget(key, field);
51
+ const value = Number(val) | 0;
52
+ if (value > limit)
53
+ throw Error("Too many request");
54
+ return value;
55
+ };
56
+ /**
57
+ * 控频计数+1
58
+ * @param filed 控频字段key
59
+ * @param ms 控频周期长度 毫秒
60
+ */
61
+ const incr = async (field, ms) => {
62
+ const key = getKey(ms);
63
+ const val = await redis.hincrby(key, field, 1);
64
+ if (val === 1)
65
+ await redis.expire(key, expire(ms));
66
+ return val;
67
+ };
68
+ /**
69
+ * 构造一个控频对象
70
+ * @param filed 控频字段key
71
+ * @param ms 控频周期长度 毫秒
72
+ * @param limit 控频极限次数
73
+ */
74
+ const generate = async (field, ms, limit) => ({
75
+ /** 全流程控制,会自动累加次数 */
76
+ control() {
77
+ return control(field, ms, limit);
78
+ },
79
+ /** 检测是否超限 */
80
+ check() {
81
+ return check(field, ms, limit);
82
+ },
83
+ /** 仅做累加 */
84
+ incr() {
85
+ return incr(field, ms);
86
+ },
87
+ });
88
+ return { control, check, incr, generate };
89
+ }
90
+ exports.Main = Main;
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
21
21
  checker: typeof import("./deps/checker");
22
22
  counter: typeof import("./deps/counter");
23
23
  cron: typeof import("./deps/cron");
24
+ frequency: typeof import("./deps/frequency");
24
25
  graceful: typeof import("./deps/graceful");
25
26
  hash: typeof import("./deps/hash");
26
27
  logger: typeof import("./deps/logger");
@@ -32,12 +33,13 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
32
33
  schema: typeof import("./deps/schema");
33
34
  sequelize: typeof import("./deps/sequelize");
34
35
  signer: typeof import("./deps/signer");
35
- }[Include<"schema", RemoveReadonlyArray<T>> | Include<"logger", RemoveReadonlyArray<T>> | Include<"aes", RemoveReadonlyArray<T>> | Include<"request", RemoveReadonlyArray<T>> | Include<"sequelize", RemoveReadonlyArray<T>> | Include<"cache", RemoveReadonlyArray<T>> | Include<"redis", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"myCia", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"rest", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
36
+ }[Include<"frequency", RemoveReadonlyArray<T>> | Include<"schema", RemoveReadonlyArray<T>> | Include<"logger", RemoveReadonlyArray<T>> | Include<"aes", RemoveReadonlyArray<T>> | Include<"request", RemoveReadonlyArray<T>> | Include<"sequelize", RemoveReadonlyArray<T>> | Include<"cache", RemoveReadonlyArray<T>> | Include<"redis", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"myCia", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"rest", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
36
37
  aes: typeof import("./deps/aes");
37
38
  cache: typeof import("./deps/cache");
38
39
  checker: typeof import("./deps/checker");
39
40
  counter: typeof import("./deps/counter");
40
41
  cron: typeof import("./deps/cron");
42
+ frequency: typeof import("./deps/frequency");
41
43
  graceful: typeof import("./deps/graceful");
42
44
  hash: typeof import("./deps/hash");
43
45
  logger: typeof import("./deps/logger");
@@ -55,6 +57,7 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
55
57
  checker: typeof import("./deps/checker");
56
58
  counter: typeof import("./deps/counter");
57
59
  cron: typeof import("./deps/cron");
60
+ frequency: typeof import("./deps/frequency");
58
61
  graceful: typeof import("./deps/graceful");
59
62
  hash: typeof import("./deps/hash");
60
63
  logger: typeof import("./deps/logger");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.13",
3
+ "version": "0.3.16",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {