@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.
@@ -2,6 +2,8 @@
2
2
  interface ICronJob {
3
3
  /**定时任务 */
4
4
  (): void;
5
+ /**定时任务 */
6
+ default?: () => void;
5
7
  /**获取定时任务间隔时间 */
6
8
  getDelay?: () => number;
7
9
  /**获取定时任务开启状态 */
@@ -1,3 +1,3 @@
1
1
  /**处理自定义业务 */
2
- declare function processCustom(): void;
2
+ declare function processCustom(): Promise<void>;
3
3
  export default processCustom;
@@ -120,7 +120,7 @@ declare global {
120
120
  enableCron?: boolean;
121
121
  /**
122
122
  * 开启的中间件列表
123
- * @deprecated since version 1.1.0, 1.3.0 后将完全删除
123
+ * @deprecated since version 1.1.0, 已弃用, 请使用改用 `middlewares` 配置项
124
124
  */
125
125
  middleware?: (string | (string | Record<string, any>)[])[];
126
126
  /**开启的中间件列表 */
@@ -17,7 +17,6 @@ const CONFIG = {
17
17
  "staticHtmlFileMaxage": 0,
18
18
  "enableComboCache": true,
19
19
  "enableCron": false,
20
- "middleware": [],
21
20
  "strictSSL": false,
22
21
  "root": "public",
23
22
  "timezone": "Asia/Shanghai"
@@ -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
- mod();
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
- crons.forEach(function(cron) {
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("\t>>> [ %s ] %s.", name, CRON_TIMERS[name] ? "running" : "disabled");
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
- config.custom.forEach(async (item)=>{
31
- const isStrItem = (0, _utils.isString)(item);
32
- const name = isStrItem ? item : item[0];
33
- const conf = isStrItem ? null : item[1];
34
- const mod = (0, _common.getRealDefaultMod)(require(_path.default.resolve(CustomPath, name)));
35
- if ((0, _utils.isFunction)(mod)) {
36
- mod(conf);
37
- } else if ((0, _utils.isAsyncFunction)(mod)) {
38
- await mod(conf);
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
- masterLog("custom", `[${name}] loaded`);
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) && ((0, _utils.isFunction)(mod.handler) || (0, _utils.isAsyncFunction)(mod.handler))) {
95
- modName = mod.method || "get";
96
- api = mod.api || api;
97
- apiPart = api.split("/");
98
- if (apiPart[0] !== "") {
99
- apiPart.unshift("");
100
- }
101
- // 保证一定是以 api 开头的
102
- if (!IS_API_REGEXP.test(api)) {
103
- api = buildApi(api);
104
- }
105
- if ((0, _utils.isArray)(mod.middleware)) {
106
- mod = [
107
- api,
108
- ...mod.middleware,
109
- mod.handler
110
- ];
111
- } else {
112
- mod = [
113
- api,
114
- mod.handler
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 = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "description": "9lab 服务端模块",
5
5
  "versionDesc": "静态文件服务支持跨域",
6
6
  "scripts": {