@domain.js/main 0.1.1 → 0.1.6
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/.husky/pre-commit +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +5 -5
- package/dist/cli/schema2ts.d.ts +1 -0
- package/dist/cli/schema2ts.js +37 -0
- package/dist/deps/cia/index.d.ts +2 -0
- package/dist/deps/cia/index.js +21 -1
- package/dist/deps/cron/index.js +1 -1
- package/dist/deps/defines.d.ts +2 -2
- package/dist/deps/defines.js +2 -2
- package/dist/deps/myCia/errors.d.ts +6 -0
- package/dist/deps/myCia/errors.js +36 -0
- package/dist/deps/myCia/index.d.ts +63 -0
- package/dist/deps/myCia/index.js +308 -0
- package/dist/deps/signer/index.d.ts +2 -2
- package/dist/deps/signer/index.js +1 -1
- package/dist/dm/index.d.ts +1 -1
- package/dist/http/defines.d.ts +5 -6
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +2 -2
- package/dist/http/router.d.ts +2 -1
- package/dist/http/router.js +3 -51
- package/dist/http/utils.d.ts +0 -1
- package/dist/http/utils.js +0 -35
- package/dist/index.d.ts +7 -7
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/package.json +5 -3
- package/dist/deps/axios/index.d.ts +0 -22
- package/dist/deps/axios/index.js +0 -56
- package/dist/deps/errors/index.d.ts +0 -1
- package/dist/deps/errors/index.js +0 -10
- package/dist/dm/dm.d.ts +0 -21
- package/dist/dm/dm.js +0 -57
- package/dist/errors/index.d.ts +0 -16
- package/dist/errors/index.js +0 -27
- package/dist/errors.d.ts +0 -1
- package/dist/errors.js +0 -10
- package/dist/npms.d.ts +0 -88
- package/dist/npms.js +0 -31
package/.husky/pre-commit
CHANGED
package/dist/cli/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#! /usr/bin/env
|
|
1
|
+
#! /usr/bin/env node
|
|
2
2
|
export {};
|
package/dist/cli/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#! /usr/bin/env
|
|
1
|
+
#! /usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const fs = require("fs");
|
|
@@ -79,7 +79,7 @@ const checkHookExport = (_dir) => {
|
|
|
79
79
|
const loadDeps = async (rootDir = process.cwd(), ext = "js") => {
|
|
80
80
|
const isTS = ext === "ts";
|
|
81
81
|
const modules = [];
|
|
82
|
-
const dir = path.resolve(rootDir, "
|
|
82
|
+
const dir = path.resolve(rootDir, "./");
|
|
83
83
|
for (const x of fs.readdirSync(dir)) {
|
|
84
84
|
// 忽略隐藏目录
|
|
85
85
|
if (x[0] === ".")
|
|
@@ -93,7 +93,7 @@ const loadDeps = async (rootDir = process.cwd(), ext = "js") => {
|
|
|
93
93
|
modules.push(x);
|
|
94
94
|
}
|
|
95
95
|
// 按字典排序,后续有变动的时候不容易冲突
|
|
96
|
-
const targetFile = path.resolve(rootDir,
|
|
96
|
+
const targetFile = path.resolve(rootDir, `./defines.${ext}`);
|
|
97
97
|
await makeDefineFile(modules.sort(), targetFile, isTS);
|
|
98
98
|
};
|
|
99
99
|
const checkService = (_dir) => {
|
|
@@ -106,7 +106,7 @@ const checkService = (_dir) => {
|
|
|
106
106
|
const loadServices = async (rootDir = process.cwd(), ext = "js") => {
|
|
107
107
|
const isTS = ext === "ts";
|
|
108
108
|
const modules = [];
|
|
109
|
-
const dir = path.resolve(rootDir, "
|
|
109
|
+
const dir = path.resolve(rootDir, "domain/services/");
|
|
110
110
|
for (const x of fs.readdirSync(dir)) {
|
|
111
111
|
// 忽略隐藏目录, 忽略私有目录
|
|
112
112
|
if (x[0] === "." || x[0] === "_")
|
|
@@ -120,7 +120,7 @@ const loadServices = async (rootDir = process.cwd(), ext = "js") => {
|
|
|
120
120
|
modules.push(x);
|
|
121
121
|
}
|
|
122
122
|
// 按字典排序,后续有变动的时候不容易冲突
|
|
123
|
-
const targetFile = path.resolve(rootDir, `
|
|
123
|
+
const targetFile = path.resolve(rootDir, `domain/services/defines.${ext}`);
|
|
124
124
|
await makeDefineFile(modules.sort(), targetFile, isTS);
|
|
125
125
|
};
|
|
126
126
|
const deepLoadDir = (root, parent, files = []) => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
6
|
+
const _require = require;
|
|
7
|
+
async function main(file, single = false, name = "Params") {
|
|
8
|
+
const stats = fs.statSync(file);
|
|
9
|
+
if (stats.isFile()) {
|
|
10
|
+
const arr = file.split(".");
|
|
11
|
+
if (arr.pop() !== "js")
|
|
12
|
+
return;
|
|
13
|
+
const obj = _require(file);
|
|
14
|
+
if (!single) {
|
|
15
|
+
if (!Array.isArray(obj))
|
|
16
|
+
return;
|
|
17
|
+
if (typeof obj[1] !== "object")
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const ts = await (0, json_schema_to_typescript_1.compile)(single ? obj : obj[1], name);
|
|
22
|
+
arr.push("d.ts");
|
|
23
|
+
fs.writeFileSync(arr.join("."), ts);
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
console.error(file, e);
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const files = fs.readdirSync(file);
|
|
31
|
+
for await (const x of files) {
|
|
32
|
+
if (x === "." || x === "..")
|
|
33
|
+
continue;
|
|
34
|
+
await main(path.resolve(file, x), single, name);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
main(process.argv[2], process.argv[3] === "single", process.argv[4]);
|
package/dist/deps/cia/index.d.ts
CHANGED
|
@@ -56,6 +56,8 @@ export declare function Main(cnf: Cnf, deps: Deps): {
|
|
|
56
56
|
link: (name: string, type: string, waiter: Function) => void;
|
|
57
57
|
submit: (name: string, data: any, callback?: Function | undefined) => void;
|
|
58
58
|
setFn: (type: "error" | "timeout", fn: (...args: any[]) => any) => void;
|
|
59
|
+
domainPaths: Set<string>;
|
|
60
|
+
modelHooks: Set<string>;
|
|
59
61
|
};
|
|
60
62
|
export declare const Deps: string[];
|
|
61
63
|
export {};
|
package/dist/deps/cia/index.js
CHANGED
|
@@ -282,7 +282,27 @@ function Main(cnf, deps) {
|
|
|
282
282
|
const isExiting = () => Boolean(exiting);
|
|
283
283
|
// 进程是否已经退出
|
|
284
284
|
const isExited = () => Boolean(exited);
|
|
285
|
-
|
|
285
|
+
/**
|
|
286
|
+
* 领域方法注册到 cia 上的路径集合
|
|
287
|
+
*/
|
|
288
|
+
const domainPaths = new Set();
|
|
289
|
+
/**
|
|
290
|
+
* Model hook 注册到 cia 上的路径集合
|
|
291
|
+
*/
|
|
292
|
+
const modelHooks = new Set();
|
|
293
|
+
return {
|
|
294
|
+
isExiting,
|
|
295
|
+
isExited,
|
|
296
|
+
checkReady,
|
|
297
|
+
getStats,
|
|
298
|
+
getUnlinks,
|
|
299
|
+
regist,
|
|
300
|
+
link,
|
|
301
|
+
submit,
|
|
302
|
+
setFn,
|
|
303
|
+
domainPaths,
|
|
304
|
+
modelHooks,
|
|
305
|
+
};
|
|
286
306
|
}
|
|
287
307
|
exports.Main = Main;
|
|
288
308
|
exports.Deps = ["_", "async", "logger", "utils", "redis", "graceful", "uuid"];
|
package/dist/deps/cron/index.js
CHANGED
package/dist/deps/defines.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as aes from "./aes";
|
|
2
2
|
import * as cache from "./cache";
|
|
3
3
|
import * as checker from "./checker";
|
|
4
|
-
import * as cia from "./cia";
|
|
5
4
|
import * as counter from "./counter";
|
|
6
5
|
import * as cron from "./cron";
|
|
7
6
|
import * as graceful from "./graceful";
|
|
8
7
|
import * as hash from "./hash";
|
|
9
8
|
import * as logger from "./logger";
|
|
9
|
+
import * as myCia from "./myCia";
|
|
10
10
|
import * as parallel from "./parallel";
|
|
11
11
|
import * as redis from "./redis";
|
|
12
12
|
import * as request from "./request";
|
|
@@ -18,12 +18,12 @@ declare const _default: {
|
|
|
18
18
|
aes: typeof aes;
|
|
19
19
|
cache: typeof cache;
|
|
20
20
|
checker: typeof checker;
|
|
21
|
-
cia: typeof cia;
|
|
22
21
|
counter: typeof counter;
|
|
23
22
|
cron: typeof cron;
|
|
24
23
|
graceful: typeof graceful;
|
|
25
24
|
hash: typeof hash;
|
|
26
25
|
logger: typeof logger;
|
|
26
|
+
myCia: typeof myCia;
|
|
27
27
|
parallel: typeof parallel;
|
|
28
28
|
redis: typeof redis;
|
|
29
29
|
request: typeof request;
|
package/dist/deps/defines.js
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
const aes = require("./aes");
|
|
4
4
|
const cache = require("./cache");
|
|
5
5
|
const checker = require("./checker");
|
|
6
|
-
const cia = require("./cia");
|
|
7
6
|
const counter = require("./counter");
|
|
8
7
|
const cron = require("./cron");
|
|
9
8
|
const graceful = require("./graceful");
|
|
10
9
|
const hash = require("./hash");
|
|
11
10
|
const logger = require("./logger");
|
|
11
|
+
const myCia = require("./myCia");
|
|
12
12
|
const parallel = require("./parallel");
|
|
13
13
|
const redis = require("./redis");
|
|
14
14
|
const request = require("./request");
|
|
@@ -20,12 +20,12 @@ module.exports = {
|
|
|
20
20
|
aes,
|
|
21
21
|
cache,
|
|
22
22
|
checker,
|
|
23
|
-
cia,
|
|
24
23
|
counter,
|
|
25
24
|
cron,
|
|
26
25
|
graceful,
|
|
27
26
|
hash,
|
|
28
27
|
logger,
|
|
28
|
+
myCia,
|
|
29
29
|
parallel,
|
|
30
30
|
redis,
|
|
31
31
|
request,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util = require("util");
|
|
4
|
+
exports.default = () => {
|
|
5
|
+
const defines = [
|
|
6
|
+
["duplicatRegistMessage", "The message has been registed: %s"],
|
|
7
|
+
["registWhenReadyAfter", "The message dont registed when mcenter be ready after: %s"],
|
|
8
|
+
[
|
|
9
|
+
"submitUnregistedMessage",
|
|
10
|
+
"The message has not been registed: %s, data: %o, when will submit",
|
|
11
|
+
],
|
|
12
|
+
["linkUnregistedMessage", "The message has not been registed: %s, when will link"],
|
|
13
|
+
[
|
|
14
|
+
"linkUnknowTypes",
|
|
15
|
+
"The message link type unknown, message name is: %s, type is: %s, when will link",
|
|
16
|
+
],
|
|
17
|
+
[
|
|
18
|
+
"linkDuplicateType",
|
|
19
|
+
"The message link type duplicate, message name is: %s, type is: %s, when will link",
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
"linkListernerMustBeFunctionType",
|
|
23
|
+
"The message link waiter must be a function, message name is: %s, type is: %s, when will link",
|
|
24
|
+
],
|
|
25
|
+
["setFnNotAllowed", "Set function but unknown type: %s"],
|
|
26
|
+
];
|
|
27
|
+
const fns = {};
|
|
28
|
+
for (const [code, message] of defines) {
|
|
29
|
+
fns[code] = (...args) => {
|
|
30
|
+
const error = Error(util.format(message, ...args));
|
|
31
|
+
error.code = code;
|
|
32
|
+
return error;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return fns;
|
|
36
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
import * as async from "async";
|
|
3
|
+
import { v4 } from "uuid";
|
|
4
|
+
interface Cnf {
|
|
5
|
+
cia?: {
|
|
6
|
+
concurrency?: number;
|
|
7
|
+
storeKey?: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
interface Deps {
|
|
11
|
+
_: Pick<typeof _, "pick" | "map" | "isFunction">;
|
|
12
|
+
async: Pick<typeof async, "eachSeries" | "queue">;
|
|
13
|
+
uuid: {
|
|
14
|
+
v4: typeof v4;
|
|
15
|
+
};
|
|
16
|
+
logger: {
|
|
17
|
+
info: (...args: any[]) => void;
|
|
18
|
+
error: (...args: any[]) => void;
|
|
19
|
+
};
|
|
20
|
+
redis: {
|
|
21
|
+
hset: Function;
|
|
22
|
+
hdel: Function;
|
|
23
|
+
hgetall: Function;
|
|
24
|
+
};
|
|
25
|
+
graceful: {
|
|
26
|
+
exit: (fn: () => Promise<void>) => void;
|
|
27
|
+
};
|
|
28
|
+
U: {
|
|
29
|
+
tryCatchLog<Fn extends (...args: any[]) => any>(fn: Fn, errorFn: (...args: any[]) => any): Fn;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export interface Stats {
|
|
33
|
+
pendings: number;
|
|
34
|
+
doings: number;
|
|
35
|
+
errors: number;
|
|
36
|
+
dones: number;
|
|
37
|
+
}
|
|
38
|
+
declare type Type = {
|
|
39
|
+
type: string;
|
|
40
|
+
timeout?: number;
|
|
41
|
+
validator?: Function;
|
|
42
|
+
};
|
|
43
|
+
export declare function Main(cnf: Cnf, deps: Deps): {
|
|
44
|
+
isExiting: () => boolean;
|
|
45
|
+
isExited: () => boolean;
|
|
46
|
+
checkReady: () => boolean;
|
|
47
|
+
getStats: () => {
|
|
48
|
+
[name: string]: Stats & {
|
|
49
|
+
_types: ({
|
|
50
|
+
type: string;
|
|
51
|
+
} & Stats)[];
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
getUnlinks: () => string[];
|
|
55
|
+
regist: (name: string, validator: Function | undefined, types: Type[]) => number;
|
|
56
|
+
link: (name: string, type: string, waiter: Function) => void;
|
|
57
|
+
submit: (name: string, data: any, callback?: Function | undefined) => void;
|
|
58
|
+
setFn: (type: "error" | "timeout", fn: (...args: any[]) => any) => void;
|
|
59
|
+
domainPaths: Set<string>;
|
|
60
|
+
modelHooks: Set<string>;
|
|
61
|
+
};
|
|
62
|
+
export declare const Deps: string[];
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Deps = exports.Main = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
function Main(cnf, deps) {
|
|
6
|
+
const { _, async, uuid: { v4: uuid }, logger, redis, graceful, U: { tryCatchLog }, } = deps;
|
|
7
|
+
const { cia } = cnf;
|
|
8
|
+
const concurrency = Math.max(1, ((cia && cia.concurrency) || 10) | 0);
|
|
9
|
+
const storeKey = (cia && cia.storeKey) || "cia-store";
|
|
10
|
+
const errors = (0, errors_1.default)();
|
|
11
|
+
let doingCount = 0; // 正在执行的消息数量
|
|
12
|
+
let exited = false; // 是否已经完成退出
|
|
13
|
+
let exiting = false; // 是否正在退出
|
|
14
|
+
let readyToExitFn; // 完成退出前准备后执行函数
|
|
15
|
+
let unlinkdCount = 0; // 未被订阅的数量, 基于 {name}::{type} 判断
|
|
16
|
+
let isReady = false; // 系统是否已准备妥当
|
|
17
|
+
const registeds = {};
|
|
18
|
+
// 默认通知函数
|
|
19
|
+
const fns = {
|
|
20
|
+
error: logger.error,
|
|
21
|
+
timeout: logger.info,
|
|
22
|
+
};
|
|
23
|
+
// 记录监听回调函数
|
|
24
|
+
// { [${name}::${type}]: { [type]: fn } }
|
|
25
|
+
const waiters = new Map();
|
|
26
|
+
// 更新等待数量
|
|
27
|
+
const updatePendings = (registed) => {
|
|
28
|
+
const { result = {}, types } = registed;
|
|
29
|
+
const withouts = new Set(Object.keys(result));
|
|
30
|
+
registed.pendings += 1;
|
|
31
|
+
types.forEach((x) => {
|
|
32
|
+
if (!withouts.has(x.type))
|
|
33
|
+
x.pendings += 1;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
// 更新 doings 统计信息
|
|
37
|
+
const updateDoings = (item) => {
|
|
38
|
+
item.pendings -= 1;
|
|
39
|
+
item.doings += 1;
|
|
40
|
+
};
|
|
41
|
+
// 更新 errors 统计信息
|
|
42
|
+
const updateErrors = (item) => {
|
|
43
|
+
item.doings -= 1;
|
|
44
|
+
item.errors += 1;
|
|
45
|
+
};
|
|
46
|
+
// 更新 dones 统计信息
|
|
47
|
+
const updateDones = (item) => {
|
|
48
|
+
item.doings -= 1;
|
|
49
|
+
item.dones += 1;
|
|
50
|
+
};
|
|
51
|
+
// 消息分发函数,分发到对应的订阅函数上
|
|
52
|
+
const dispatch = async (item) => {
|
|
53
|
+
const { id, name, data, result = {}, callback } = item;
|
|
54
|
+
const registed = registeds[name];
|
|
55
|
+
const { types } = registed;
|
|
56
|
+
const withouts = new Set(Object.keys(result));
|
|
57
|
+
updateDoings(registed);
|
|
58
|
+
doingCount += 1;
|
|
59
|
+
let errorCount = 0;
|
|
60
|
+
await async.eachSeries(types, async (_type) => {
|
|
61
|
+
const { type, timeout, validator } = _type;
|
|
62
|
+
// 看看是否有设置要忽略掉某些订阅者
|
|
63
|
+
// 这个功能主要是留给应用无故中断后系统自动恢复的任务执行
|
|
64
|
+
if (withouts && withouts.has(type))
|
|
65
|
+
return;
|
|
66
|
+
if (exiting)
|
|
67
|
+
return;
|
|
68
|
+
const fn = waiters.get(`${name}::${type}`);
|
|
69
|
+
const startAt = Date.now();
|
|
70
|
+
let err = null;
|
|
71
|
+
let ret = null;
|
|
72
|
+
try {
|
|
73
|
+
updateDoings(_type);
|
|
74
|
+
ret = await fn(data);
|
|
75
|
+
if (validator)
|
|
76
|
+
validator(ret);
|
|
77
|
+
updateDones(_type);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
updateErrors(_type);
|
|
81
|
+
fns.error(e, id, name, type, data);
|
|
82
|
+
errorCount += 1;
|
|
83
|
+
err = e;
|
|
84
|
+
}
|
|
85
|
+
const consumedMS = Date.now() - startAt;
|
|
86
|
+
if (timeout && timeout < consumedMS)
|
|
87
|
+
fns.timeout(consumedMS, id, name, type);
|
|
88
|
+
result[type] = [err, ret, consumedMS];
|
|
89
|
+
// 记录执行结果
|
|
90
|
+
logger.info(`cia.dispatch\t${id}\t${type}`, result[type]);
|
|
91
|
+
});
|
|
92
|
+
doingCount -= 1;
|
|
93
|
+
if (errorCount) {
|
|
94
|
+
updateErrors(registed);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
updateDones(registed);
|
|
98
|
+
}
|
|
99
|
+
// submit 设置了callback 要记得执行回调函数
|
|
100
|
+
if (callback)
|
|
101
|
+
callback(result);
|
|
102
|
+
// 正在退出,且完成的不等于总共的,则需要储存, 以备下次启动后执行
|
|
103
|
+
if (exiting) {
|
|
104
|
+
if (Object.keys(result).length !== types.length) {
|
|
105
|
+
item.result = result;
|
|
106
|
+
// 存储以备下次启动恢复执行
|
|
107
|
+
await redis.hset(storeKey, item.id, JSON.stringify(item));
|
|
108
|
+
}
|
|
109
|
+
// 全部处理完毕后,执行退出
|
|
110
|
+
if (!doingCount) {
|
|
111
|
+
exited = true;
|
|
112
|
+
exiting = false;
|
|
113
|
+
readyToExitFn();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const statsFields = Object.freeze(["pendings", "doings", "dones", "errors"]);
|
|
118
|
+
// 获取统计信息
|
|
119
|
+
const getStats = () => {
|
|
120
|
+
const stats = {};
|
|
121
|
+
for (const name of Object.keys(registeds)) {
|
|
122
|
+
const { types } = registeds[name];
|
|
123
|
+
stats[name] = {
|
|
124
|
+
..._.pick(registeds[name], statsFields),
|
|
125
|
+
_types: types.map((x) => _.pick(x, "type", ...statsFields)),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return stats;
|
|
129
|
+
};
|
|
130
|
+
// 内部消息队列, 初始化后立即暂定,等待 regist, link 都准备好了在开始执行
|
|
131
|
+
// 这样就不会有未成功订阅函数执行遗漏的问题了
|
|
132
|
+
// 例如: A 函数要监听 1 好消息的 save 类型,结果在完成订阅前,已经有某个区域 submit 了 1 号事件
|
|
133
|
+
// 如果队列一开始不暂停就会出现A函数遗漏执行
|
|
134
|
+
const queue = async.queue(dispatch, concurrency);
|
|
135
|
+
queue.pause();
|
|
136
|
+
graceful.exit(async () => {
|
|
137
|
+
exiting = true;
|
|
138
|
+
await new Promise((resolve) => {
|
|
139
|
+
// 如果队列已经清空,且没有正在执行的消息,则直接退出
|
|
140
|
+
if (!queue.length() && !doingCount) {
|
|
141
|
+
exited = true;
|
|
142
|
+
exiting = false;
|
|
143
|
+
resolve();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
readyToExitFn = resolve;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
// 恢复上次残留的消息订阅执行
|
|
151
|
+
const recover = async () => {
|
|
152
|
+
const items = await redis.hgetall(storeKey);
|
|
153
|
+
if (!items)
|
|
154
|
+
return;
|
|
155
|
+
for await (const id of Object.keys(items)) {
|
|
156
|
+
const item = items[id];
|
|
157
|
+
const ok = await redis.hdel(storeKey, id);
|
|
158
|
+
if (ok !== 1)
|
|
159
|
+
continue;
|
|
160
|
+
try {
|
|
161
|
+
const data = JSON.parse(item);
|
|
162
|
+
const { name } = data;
|
|
163
|
+
queue.push(data);
|
|
164
|
+
updatePendings(registeds[name]);
|
|
165
|
+
logger.info("cia-recover: %s", item);
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
logger.error(e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
// regist 消息注册,提前注册好需要submit和link的消息
|
|
173
|
+
// 这么做的目的是可以随时检测是否所有的消息都消费者,消费者类型是否正确
|
|
174
|
+
// 同时在submit的时候也可以检测发送的数据是否符合规定的格式
|
|
175
|
+
// name: String 消息名称
|
|
176
|
+
// validator?: Function 消息体数据格式验证函数
|
|
177
|
+
// types: [{
|
|
178
|
+
// type: 'updateUser', // 类型名称
|
|
179
|
+
// timeout?: 100, // 执行超时限定, 单位毫秒,可选 默认为 0, 不限制
|
|
180
|
+
// validator?: fn, // 返回值格式验证函数, 可选
|
|
181
|
+
// }]
|
|
182
|
+
const regist = (name, validator, types) => {
|
|
183
|
+
if (isReady)
|
|
184
|
+
throw errors.registWhenReadyAfter(name);
|
|
185
|
+
if (registeds[name])
|
|
186
|
+
throw errors.duplicatRegistMessage(name);
|
|
187
|
+
const typeNames = new Set(_.map(types, "type"));
|
|
188
|
+
types.forEach((x) => {
|
|
189
|
+
Object.assign(x, { pendings: 0, dones: 0, doings: 0, errors: 0 });
|
|
190
|
+
});
|
|
191
|
+
const item = {
|
|
192
|
+
validator,
|
|
193
|
+
types: types,
|
|
194
|
+
typeNames,
|
|
195
|
+
pendings: 0,
|
|
196
|
+
dones: 0,
|
|
197
|
+
doings: 0,
|
|
198
|
+
errors: 0,
|
|
199
|
+
};
|
|
200
|
+
unlinkdCount += typeNames.size;
|
|
201
|
+
registeds[name] = item;
|
|
202
|
+
return Object.keys(registeds).length;
|
|
203
|
+
};
|
|
204
|
+
// start 启动系统执行, 这之前一定要regist 和 link 都准备好
|
|
205
|
+
const start = async () => {
|
|
206
|
+
queue.resume();
|
|
207
|
+
await recover();
|
|
208
|
+
};
|
|
209
|
+
// check 消息注册、监听检测
|
|
210
|
+
// 检查是否存在注册了的消息,但没有人监听消费
|
|
211
|
+
const checkReady = () => {
|
|
212
|
+
if (unlinkdCount !== 0)
|
|
213
|
+
return false;
|
|
214
|
+
if (!isReady) {
|
|
215
|
+
isReady = true;
|
|
216
|
+
start();
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
};
|
|
220
|
+
// link 消息订阅
|
|
221
|
+
const link = (name, type, waiter) => {
|
|
222
|
+
if (!registeds[name])
|
|
223
|
+
throw errors.linkUnregistedMessage(name);
|
|
224
|
+
const { typeNames } = registeds[name];
|
|
225
|
+
if (!typeNames.has(type))
|
|
226
|
+
throw errors.linkUnknowTypes(name, type);
|
|
227
|
+
if (!_.isFunction(waiter))
|
|
228
|
+
throw errors.linkListernerMustBeFunctionType(name, type);
|
|
229
|
+
const key = `${name}::${type}`;
|
|
230
|
+
if (waiters.get(key))
|
|
231
|
+
throw errors.linkDuplicateType(name, type);
|
|
232
|
+
waiters.set(key, waiter);
|
|
233
|
+
unlinkdCount -= 1;
|
|
234
|
+
checkReady();
|
|
235
|
+
};
|
|
236
|
+
// submit 消息发布
|
|
237
|
+
// name string 消息名称
|
|
238
|
+
// data any 消息数据
|
|
239
|
+
// callback function 消息执行完毕回调
|
|
240
|
+
const submit = (name, data, callback) => {
|
|
241
|
+
if (!registeds[name]) {
|
|
242
|
+
// 这里记录error就可以了。throw没有意义,因为submit是异步的
|
|
243
|
+
// throw error并没有被捕获,还会导致调用方的后续代码不执行
|
|
244
|
+
logger.error(errors.submitUnregistedMessage(name, data));
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (callback && !_.isFunction(callback))
|
|
248
|
+
callback = undefined;
|
|
249
|
+
const { validator } = registeds[name];
|
|
250
|
+
if (validator)
|
|
251
|
+
validator(data);
|
|
252
|
+
const id = uuid();
|
|
253
|
+
queue.push({ id, name, data, callback });
|
|
254
|
+
updatePendings(registeds[name]);
|
|
255
|
+
logger.info(`cia.submit\t${id}`, { name, data });
|
|
256
|
+
};
|
|
257
|
+
// 设置通知函数,错误通知,超时通知
|
|
258
|
+
// 在消息分发执行的时候遇到错误会调用错误通知函数
|
|
259
|
+
// 在消息分发执行的时候遇到超时会调用超时通知函数
|
|
260
|
+
// type string 类型,error or timeout
|
|
261
|
+
// fn function 通知函数
|
|
262
|
+
const setFn = (type, fn) => {
|
|
263
|
+
if (!fns[type])
|
|
264
|
+
throw errors.setFnNotAllowed(type);
|
|
265
|
+
// 这里之所以会用 tryCatchLog 封装函数,是不想让这些函数的执行影响主流程
|
|
266
|
+
// 这些函数内部抛出的异常不会导致主流程执行中断
|
|
267
|
+
fns[type] = tryCatchLog(fn, logger.error);
|
|
268
|
+
};
|
|
269
|
+
// 获取未被连接的任务消息
|
|
270
|
+
const getUnlinks = () => {
|
|
271
|
+
const losts = [];
|
|
272
|
+
for (const name of Object.keys(registeds)) {
|
|
273
|
+
for (const { type } of registeds[name].types) {
|
|
274
|
+
const key = `${name}::${type}`;
|
|
275
|
+
if (!waiters.has(key))
|
|
276
|
+
losts.push(key);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return losts;
|
|
280
|
+
};
|
|
281
|
+
// 进程是否正在退出
|
|
282
|
+
const isExiting = () => Boolean(exiting);
|
|
283
|
+
// 进程是否已经退出
|
|
284
|
+
const isExited = () => Boolean(exited);
|
|
285
|
+
/**
|
|
286
|
+
* 领域方法注册到 cia 上的路径集合
|
|
287
|
+
*/
|
|
288
|
+
const domainPaths = new Set();
|
|
289
|
+
/**
|
|
290
|
+
* Model hook 注册到 cia 上的路径集合
|
|
291
|
+
*/
|
|
292
|
+
const modelHooks = new Set();
|
|
293
|
+
return {
|
|
294
|
+
isExiting,
|
|
295
|
+
isExited,
|
|
296
|
+
checkReady,
|
|
297
|
+
getStats,
|
|
298
|
+
getUnlinks,
|
|
299
|
+
regist,
|
|
300
|
+
link,
|
|
301
|
+
submit,
|
|
302
|
+
setFn,
|
|
303
|
+
domainPaths,
|
|
304
|
+
modelHooks,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
exports.Main = Main;
|
|
308
|
+
exports.Deps = ["_", "async", "logger", "utils", "redis", "graceful", "uuid"];
|
|
@@ -5,7 +5,7 @@ export interface Opt {
|
|
|
5
5
|
/** Key for signature calculation */
|
|
6
6
|
key: string;
|
|
7
7
|
/** Second timestamp */
|
|
8
|
-
timestamp: number;
|
|
8
|
+
timestamp: number | string;
|
|
9
9
|
/** Signature algorithm, fixed as hmacsha256 */
|
|
10
10
|
signMethod: "HmacSHA256";
|
|
11
11
|
/** Signature version, fixed as 1 */
|
|
@@ -37,7 +37,7 @@ export declare function Main(): {
|
|
|
37
37
|
readonly "x-auth-signature": string;
|
|
38
38
|
readonly "x-auth-key": string;
|
|
39
39
|
readonly "x-auth-method": string;
|
|
40
|
-
readonly "x-auth-timestamp":
|
|
40
|
+
readonly "x-auth-timestamp": string;
|
|
41
41
|
readonly "x-auth-sign-method": "HmacSHA256";
|
|
42
42
|
readonly "x-auth-sign-version": "1";
|
|
43
43
|
};
|
package/dist/dm/index.d.ts
CHANGED
package/dist/http/defines.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export interface Cnf {
|
|
|
4
4
|
host?: string;
|
|
5
5
|
bodyMaxBytes?: number;
|
|
6
6
|
apisRoute?: string;
|
|
7
|
-
swaggerApiPath?: string;
|
|
8
7
|
[propName: string]: any;
|
|
9
8
|
}
|
|
10
9
|
interface Sign {
|
|
@@ -31,12 +30,12 @@ export interface Profile {
|
|
|
31
30
|
export interface HttpCodes {
|
|
32
31
|
[propName: string]: number;
|
|
33
32
|
}
|
|
34
|
-
interface
|
|
35
|
-
[propName: string]: (profile: Profile, params: any) => any |
|
|
33
|
+
export interface Domain {
|
|
34
|
+
[propName: string]: (profile: Profile, params: any) => any | Domain;
|
|
35
|
+
}
|
|
36
|
+
export interface GetSchemaByPath {
|
|
37
|
+
(methodPath: string): [any, any];
|
|
36
38
|
}
|
|
37
|
-
export declare type Domain = _Domain & {
|
|
38
|
-
_getSchemaByPath(methodPath: string): [any, any];
|
|
39
|
-
};
|
|
40
39
|
export interface Err {
|
|
41
40
|
message: string;
|
|
42
41
|
code?: number | string;
|