@domain.js/main 0.3.12 → 0.3.15
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/deps/defines.d.ts +2 -0
- package/dist/deps/defines.js +2 -0
- package/dist/deps/frequency/index.d.ts +15 -0
- package/dist/deps/frequency/index.js +56 -0
- package/dist/deps/sequelize/index.d.ts +2 -0
- package/dist/deps/sequelize/index.js +2 -0
- package/dist/index.d.ts +4 -1
- package/package.json +1 -1
package/dist/deps/defines.d.ts
CHANGED
|
@@ -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;
|
package/dist/deps/defines.js
CHANGED
|
@@ -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,15 @@
|
|
|
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: (filed: string, ms: number, limit: number) => Promise<void>;
|
|
13
|
+
check: (filed: string, ms: number, limit: number) => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
*/
|
|
12
|
+
const getKey = (ms) => {
|
|
13
|
+
const now = Date.now();
|
|
14
|
+
const t = Math.floor(now / ms);
|
|
15
|
+
return `${KEY}-${ms}-${t}`;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 计算过期时间
|
|
19
|
+
* @param ms 频率控制长度,单位毫秒
|
|
20
|
+
*/
|
|
21
|
+
const expire = (ms) => {
|
|
22
|
+
const now = Date.now();
|
|
23
|
+
const t = Math.floor(now / ms);
|
|
24
|
+
return Math.floor((ms * (t + 1) - now) / 1000);
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 频次控制,超过显示过抛出异常
|
|
28
|
+
* @param filed 控频字段key
|
|
29
|
+
* @param ms 控频周期长度 毫秒
|
|
30
|
+
* @param limit 控频极限次数
|
|
31
|
+
*/
|
|
32
|
+
const control = async (filed, ms, limit) => {
|
|
33
|
+
const key = getKey(ms);
|
|
34
|
+
const val = await redis.hincrby(key, filed, 1);
|
|
35
|
+
if (val === 1)
|
|
36
|
+
await redis.expire(key, expire(ms));
|
|
37
|
+
if (val > limit)
|
|
38
|
+
throw Error("Too many request");
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* 检测频次控制,超过显示过抛出异常
|
|
42
|
+
* @param filed 控频字段key
|
|
43
|
+
* @param ms 控频周期长度 毫秒
|
|
44
|
+
* @param limit 控频极限次数
|
|
45
|
+
*/
|
|
46
|
+
const check = async (filed, ms, limit) => {
|
|
47
|
+
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");
|
|
53
|
+
};
|
|
54
|
+
return { control, check };
|
|
55
|
+
}
|
|
56
|
+
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");
|