@domain.js/main 0.3.13 → 0.3.14

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,14 @@
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">;
10
+ }
11
+ export declare function Main(cnf: Cnf, deps: Deps): {
12
+ check: (filed: string, ms: number, limit: number) => Promise<void>;
13
+ };
14
+ export {};
@@ -0,0 +1,28 @@
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
+ * 检测频次控制,超过显示过抛出异常
10
+ * @param filed 控频字段key
11
+ * @param ms 控频周期长度 毫秒
12
+ * @param limit 控频极限次数
13
+ */
14
+ const check = async (filed, ms, limit) => {
15
+ const now = Date.now();
16
+ const t = Math.floor(now / ms);
17
+ const key = `${KEY}-${ms}-${t}`;
18
+ const val = await redis.hincrby(key, filed, 1);
19
+ if (val === 1)
20
+ await redis.expire(key, Math.floor((ms * (t + 1) - now) / 1000));
21
+ if (val > limit)
22
+ throw Error("Too many request");
23
+ };
24
+ return {
25
+ check,
26
+ };
27
+ }
28
+ 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.14",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {