@x-9lab/xlab 1.5.1 → 1.5.3
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/@types/components/cron/index.d.ts +2 -0
- package/@types/custom.d.ts +1 -1
- package/@types/global.d.ts +1 -1
- package/dist/@config/config.js +0 -1
- package/dist/components/cron/index.js +21 -6
- package/dist/custom.js +17 -12
- package/dist/router.js +25 -22
- package/package.json +1 -1
package/@types/custom.d.ts
CHANGED
package/@types/global.d.ts
CHANGED
|
@@ -120,7 +120,7 @@ declare global {
|
|
|
120
120
|
enableCron?: boolean;
|
|
121
121
|
/**
|
|
122
122
|
* 开启的中间件列表
|
|
123
|
-
* @deprecated since version 1.1.0,
|
|
123
|
+
* @deprecated since version 1.1.0, 已弃用, 请使用改用 `middlewares` 配置项
|
|
124
124
|
*/
|
|
125
125
|
middleware?: (string | (string | Record<string, any>)[])[];
|
|
126
126
|
/**开启的中间件列表 */
|
package/dist/@config/config.js
CHANGED
|
@@ -13,6 +13,7 @@ _export(exports, {
|
|
|
13
13
|
kill: ()=>kill
|
|
14
14
|
});
|
|
15
15
|
const _utils = require("@x-drive/utils");
|
|
16
|
+
const _util = require("util");
|
|
16
17
|
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
17
18
|
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
18
19
|
function _interopRequireDefault(obj) {
|
|
@@ -30,6 +31,7 @@ const config = getSysConfig("cron") || {
|
|
|
30
31
|
/**
|
|
31
32
|
* 计时器对象
|
|
32
33
|
*/ var CRON_TIMERS = {};
|
|
34
|
+
/**任务执行状态, 确保同时只会有一个定时任务在执行 */ const JOB_STATUS = {};
|
|
33
35
|
/**
|
|
34
36
|
* 定时刷新函数
|
|
35
37
|
* @param mod 模块对象
|
|
@@ -37,6 +39,9 @@ const config = getSysConfig("cron") || {
|
|
|
37
39
|
* @param dont 不执行
|
|
38
40
|
* @return 计时器对象
|
|
39
41
|
*/ function fetch(mod, name, dont) {
|
|
42
|
+
if (JOB_STATUS[name] === true) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
40
45
|
var time = (0, _utils.isFunction)(mod.getDelay) ? mod.getDelay() : DEF_TIME;
|
|
41
46
|
if (isNaN(time)) {
|
|
42
47
|
logger.warn("[ %s ] Time Invalid.", name);
|
|
@@ -47,7 +52,17 @@ const config = getSysConfig("cron") || {
|
|
|
47
52
|
return null;
|
|
48
53
|
}
|
|
49
54
|
if (!dont) {
|
|
50
|
-
|
|
55
|
+
JOB_STATUS[name] = true;
|
|
56
|
+
try {
|
|
57
|
+
if ((0, _utils.isExecutable)(mod)) {
|
|
58
|
+
mod();
|
|
59
|
+
} else if (mod.default && (0, _utils.isExecutable)(mod.default())) {
|
|
60
|
+
mod.default();
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {
|
|
63
|
+
logger.error((0, _util.inspect)(e));
|
|
64
|
+
}
|
|
65
|
+
JOB_STATUS[name] = false;
|
|
51
66
|
}
|
|
52
67
|
return setTimeout(function() {
|
|
53
68
|
CRON_TIMERS[name] = fetch(mod, name, dont);
|
|
@@ -75,7 +90,7 @@ const config = getSysConfig("cron") || {
|
|
|
75
90
|
dirStat = _fs.default.accessSync(cronPath, _fs.default.constants.F_OK);
|
|
76
91
|
} catch (e) {
|
|
77
92
|
dirStat = true;
|
|
78
|
-
logger.warn(e);
|
|
93
|
+
logger.warn((0, _util.inspect)(e));
|
|
79
94
|
}
|
|
80
95
|
// accessSync 在文件不存在的时候才会返回内容
|
|
81
96
|
if (dirStat) {
|
|
@@ -85,19 +100,19 @@ const config = getSysConfig("cron") || {
|
|
|
85
100
|
var crons = _fs.default.readdirSync(cronPath);
|
|
86
101
|
logger.info("Cron online.");
|
|
87
102
|
try {
|
|
88
|
-
|
|
103
|
+
for (const cron of crons){
|
|
89
104
|
if (cron.charAt(0) !== ".") {
|
|
90
105
|
var tmpPath = cronPath + "/" + cron;
|
|
91
106
|
var stats = _fs.default.statSync(tmpPath);
|
|
92
107
|
if (stats.isFile()) {
|
|
93
108
|
var name = cron.replace(".js", "");
|
|
94
109
|
CRON_TIMERS[name] = fetch(require(_path.default.resolve(tmpPath)), name);
|
|
95
|
-
logger.info("
|
|
110
|
+
logger.info("[ %s ] %s.", name, CRON_TIMERS[name] ? "running" : "disabled");
|
|
96
111
|
}
|
|
97
112
|
}
|
|
98
|
-
}
|
|
113
|
+
}
|
|
99
114
|
} catch (err) {
|
|
100
|
-
logger.error(err);
|
|
115
|
+
logger.error((0, _util.inspect)(err));
|
|
101
116
|
}
|
|
102
117
|
console.log("");
|
|
103
118
|
}
|
package/dist/custom.js
CHANGED
|
@@ -10,13 +10,14 @@ const _utils = require("@x-drive/utils");
|
|
|
10
10
|
const _common = require("./components/common");
|
|
11
11
|
const _defaultXConfig = /*#__PURE__*/ _interopRequireDefault(require("./default-x-config"));
|
|
12
12
|
const _config = require("./config");
|
|
13
|
+
const _util = require("util");
|
|
13
14
|
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
14
15
|
function _interopRequireDefault(obj) {
|
|
15
16
|
return obj && obj.__esModule ? obj : {
|
|
16
17
|
default: obj
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
|
-
/**处理自定义业务 */ function processCustom() {
|
|
20
|
+
/**处理自定义业务 */ async function processCustom() {
|
|
20
21
|
const config = (0, _config.get)();
|
|
21
22
|
const CustomPath = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "custom");
|
|
22
23
|
const hasCustom = (0, _common.checkFileStat)(CustomPath);
|
|
@@ -27,18 +28,22 @@ function _interopRequireDefault(obj) {
|
|
|
27
28
|
return;
|
|
28
29
|
}
|
|
29
30
|
if (hasCustom) {
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
try {
|
|
32
|
+
for (const item of config.custom){
|
|
33
|
+
const isStrItem = (0, _utils.isString)(item);
|
|
34
|
+
const name = isStrItem ? item : item[0];
|
|
35
|
+
const conf = isStrItem ? null : item[1];
|
|
36
|
+
const mod = (0, _common.getRealDefaultMod)(require(_path.default.resolve(CustomPath, name)));
|
|
37
|
+
if ((0, _utils.isFunction)(mod)) {
|
|
38
|
+
mod(conf);
|
|
39
|
+
} else if ((0, _utils.isAsyncFunction)(mod)) {
|
|
40
|
+
await mod(conf);
|
|
41
|
+
}
|
|
42
|
+
masterLog("custom", `[${name}] loaded`);
|
|
39
43
|
}
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
} catch (e) {
|
|
45
|
+
masterLog("custom", "error", (0, _util.inspect)(e));
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
48
|
console.log("");
|
|
44
49
|
}
|
package/dist/router.js
CHANGED
|
@@ -91,28 +91,31 @@ function _interopRequireDefault(obj) {
|
|
|
91
91
|
api = buildApi(api.join("/"));
|
|
92
92
|
}
|
|
93
93
|
// 是对象则取对应的字段
|
|
94
|
-
if ((0, _utils.isObject)(mod)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
apiPart.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
mod
|
|
115
|
-
|
|
94
|
+
if ((0, _utils.isObject)(mod)) {
|
|
95
|
+
const subjectMod = mod;
|
|
96
|
+
if ((0, _utils.isFunction)(subjectMod.handler) || (0, _utils.isAsyncFunction)(subjectMod.handler)) {
|
|
97
|
+
modName = subjectMod.method || "get";
|
|
98
|
+
api = subjectMod.api || api;
|
|
99
|
+
apiPart = api.split("/");
|
|
100
|
+
if (apiPart[0] !== "") {
|
|
101
|
+
apiPart.unshift("");
|
|
102
|
+
}
|
|
103
|
+
// 如果没有声明忽略检测则保证接口一定是以 api 开头的
|
|
104
|
+
if (!subjectMod.ignoreApiNameCheck && !IS_API_REGEXP.test(api)) {
|
|
105
|
+
api = buildApi(api);
|
|
106
|
+
}
|
|
107
|
+
if ((0, _utils.isArray)(subjectMod.middleware)) {
|
|
108
|
+
mod = [
|
|
109
|
+
api,
|
|
110
|
+
...subjectMod.middleware,
|
|
111
|
+
subjectMod.handler
|
|
112
|
+
];
|
|
113
|
+
} else {
|
|
114
|
+
mod = [
|
|
115
|
+
api,
|
|
116
|
+
subjectMod.handler
|
|
117
|
+
];
|
|
118
|
+
}
|
|
116
119
|
}
|
|
117
120
|
}
|
|
118
121
|
var dirMws = [];
|