@x-9lab/xlab 1.5.0 → 1.5.2

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.
@@ -54,6 +54,9 @@ export { responseResult };
54
54
  */
55
55
  declare function getMD5(...args: any[]): string;
56
56
  export { getMD5 };
57
+ /**将一个输入字符串转为 md5 */
58
+ declare function md5(str: string): string;
59
+ export { md5 };
57
60
  /**
58
61
  * 对象值转化为数组
59
62
  * @param obj 待转的对象
@@ -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"
@@ -18,6 +18,7 @@ _export(exports, {
18
18
  params: ()=>params,
19
19
  responseResult: ()=>responseResult,
20
20
  getMD5: ()=>getMD5,
21
+ md5: ()=>md5,
21
22
  object2Array: ()=>object2Array,
22
23
  formatJson: ()=>formatJson,
23
24
  toJsonp: ()=>toJsonp,
@@ -100,6 +101,11 @@ function checkLogger() {
100
101
  });
101
102
  return _crypto.default.createHash("md5").update(JSON.stringify(conditions), "utf8").digest("hex");
102
103
  }
104
+ /**将一个输入字符串转为 md5 */ function md5(str) {
105
+ var md5 = _crypto.default.createHash("md5");
106
+ md5.update(str);
107
+ return md5.digest("hex");
108
+ }
103
109
  /**
104
110
  * 对象值转化为数组
105
111
  * @param obj 待转的对象
@@ -12,7 +12,9 @@ _export(exports, {
12
12
  on: ()=>on,
13
13
  kill: ()=>kill
14
14
  });
15
+ const _common = require("../common");
15
16
  const _utils = require("@x-drive/utils");
17
+ const _util = require("util");
16
18
  const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
17
19
  const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
18
20
  function _interopRequireDefault(obj) {
@@ -30,6 +32,7 @@ const config = getSysConfig("cron") || {
30
32
  /**
31
33
  * 计时器对象
32
34
  */ var CRON_TIMERS = {};
35
+ /**任务执行状态, 确保同时只会有一个定时任务在执行 */ const JOB_STATUS = {};
33
36
  /**
34
37
  * 定时刷新函数
35
38
  * @param mod 模块对象
@@ -37,6 +40,9 @@ const config = getSysConfig("cron") || {
37
40
  * @param dont 不执行
38
41
  * @return 计时器对象
39
42
  */ function fetch(mod, name, dont) {
43
+ if (JOB_STATUS[name] === true) {
44
+ return;
45
+ }
40
46
  var time = (0, _utils.isFunction)(mod.getDelay) ? mod.getDelay() : DEF_TIME;
41
47
  if (isNaN(time)) {
42
48
  logger.warn("[ %s ] Time Invalid.", name);
@@ -47,7 +53,13 @@ const config = getSysConfig("cron") || {
47
53
  return null;
48
54
  }
49
55
  if (!dont) {
50
- mod();
56
+ JOB_STATUS[name] = true;
57
+ try {
58
+ mod();
59
+ } catch (e) {
60
+ logger.error((0, _util.inspect)(e));
61
+ }
62
+ JOB_STATUS[name] = false;
51
63
  }
52
64
  return setTimeout(function() {
53
65
  CRON_TIMERS[name] = fetch(mod, name, dont);
@@ -75,7 +87,7 @@ const config = getSysConfig("cron") || {
75
87
  dirStat = _fs.default.accessSync(cronPath, _fs.default.constants.F_OK);
76
88
  } catch (e) {
77
89
  dirStat = true;
78
- logger.warn(e);
90
+ logger.warn((0, _util.inspect)(e));
79
91
  }
80
92
  // accessSync 在文件不存在的时候才会返回内容
81
93
  if (dirStat) {
@@ -85,19 +97,19 @@ const config = getSysConfig("cron") || {
85
97
  var crons = _fs.default.readdirSync(cronPath);
86
98
  logger.info("Cron online.");
87
99
  try {
88
- crons.forEach(function(cron) {
100
+ for (const cron of crons){
89
101
  if (cron.charAt(0) !== ".") {
90
102
  var tmpPath = cronPath + "/" + cron;
91
103
  var stats = _fs.default.statSync(tmpPath);
92
104
  if (stats.isFile()) {
93
105
  var name = cron.replace(".js", "");
94
- CRON_TIMERS[name] = fetch(require(_path.default.resolve(tmpPath)), name);
106
+ CRON_TIMERS[name] = fetch((0, _common.getRealDefaultMod)(require(_path.default.resolve(tmpPath))), name);
95
107
  logger.info("\t>>> [ %s ] %s.", name, CRON_TIMERS[name] ? "running" : "disabled");
96
108
  }
97
109
  }
98
- });
110
+ }
99
111
  } catch (err) {
100
- logger.error(err);
112
+ logger.error((0, _util.inspect)(err));
101
113
  }
102
114
  console.log("");
103
115
  }
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "9lab 服务端模块",
5
5
  "versionDesc": "静态文件服务支持跨域",
6
6
  "scripts": {