@domain.js/main 0.1.15 → 0.1.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.
package/.husky/pre-commit CHANGED
@@ -5,5 +5,4 @@ fi
5
5
  . "$(dirname "$0")/_/husky.sh"
6
6
 
7
7
  ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
8
- npm run build
9
8
  npm tst
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
7
7
  "domain-cli": "dist/cli/index.js"
8
8
  },
9
9
  "scripts": {
10
- "build": "tsc",
10
+ "build": "rm -rf ./dist/* && tsc",
11
11
  "test": "export NODE_ENV=test && jest ./src --coverage",
12
12
  "test:watch": "export NODE_ENV=test && jest ./src --watch",
13
13
  "prepare": "husky install",
@@ -1 +0,0 @@
1
- export {};
@@ -1,37 +0,0 @@
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]);
@@ -1,6 +0,0 @@
1
- declare const _default: () => {
2
- [key: string]: (...args: any[]) => Error & {
3
- code?: string | undefined;
4
- };
5
- };
6
- export default _default;
@@ -1,36 +0,0 @@
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
- };
@@ -1,63 +0,0 @@
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 {};
@@ -1,308 +0,0 @@
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"];
@@ -1,5 +0,0 @@
1
- import { ModelBase } from "../sequelize";
2
- export interface Params {
3
- [propName: string]: any;
4
- }
5
- export declare type TModel = typeof ModelBase;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,14 +0,0 @@
1
- import * as TypeORM from "typeorm";
2
- interface Cnf {
3
- typeorm: {
4
- [propName: string]: Parameters<typeof TypeORM.createConnection>[0];
5
- };
6
- }
7
- interface Deps {
8
- TypeORM: typeof TypeORM;
9
- }
10
- export declare function Main(cnf: Cnf, deps: Deps): Promise<{
11
- [propName: string]: TypeORM.Connection;
12
- }>;
13
- export declare const Deps: string[];
14
- export {};
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = void 0;
4
- async function Main(cnf, deps) {
5
- // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
6
- const { typeorm: dbs } = cnf;
7
- const { TypeORM } = deps;
8
- const links = {};
9
- for await (const k of Object.keys(dbs)) {
10
- const db = dbs[k];
11
- links[k] = await TypeORM.createConnection(db);
12
- }
13
- return links;
14
- }
15
- exports.Main = Main;
16
- exports.Deps = ["TypeORM"];