@x-9lab/xlab 1.6.1 → 2.0.1

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.
Files changed (72) hide show
  1. package/@types/cluster.d.ts +15 -3
  2. package/@types/components/common/index.d.ts +1 -2
  3. package/@types/components/cron/index.d.ts +4 -4
  4. package/@types/components/log/index.d.ts +37 -2
  5. package/@types/config/index.d.ts +9 -5
  6. package/@types/custom.d.ts +25 -3
  7. package/@types/dot-file.d.ts +6 -0
  8. package/@types/global.d.ts +44 -56
  9. package/@types/index.d.ts +18 -0
  10. package/@types/init-env.d.ts +3 -1
  11. package/@types/middleware/handle-pre-dir.d.ts +1 -5
  12. package/@types/middleware/request-filter.d.ts +0 -1
  13. package/@types/middleware/service-mark.d.ts +1 -6
  14. package/@types/router.d.ts +25 -0
  15. package/@types/server.d.ts +28 -6
  16. package/MIGRATION.md +182 -0
  17. package/README.md +105 -86
  18. package/dist/@config/config.dev.js +7 -3
  19. package/dist/@config/config.js +7 -4
  20. package/dist/@config/config.sand.js +7 -3
  21. package/dist/bin/watch.js +36 -16
  22. package/dist/cluster.js +121 -44
  23. package/dist/components/assets/index.js +9 -5
  24. package/dist/components/cluster/index.js +17 -4
  25. package/dist/components/common/index.js +65 -45
  26. package/dist/components/cron/index.js +50 -27
  27. package/dist/components/header/index.js +32 -17
  28. package/dist/components/header/time.js +6 -2
  29. package/dist/components/index.js +1 -1
  30. package/dist/components/log/index.js +91 -87
  31. package/dist/components/mime/index.js +6 -2
  32. package/dist/components/return-code/index.js +33 -18
  33. package/dist/components/uuid/index.js +22 -13
  34. package/dist/config/getEnv.js +9 -5
  35. package/dist/config/index.js +98 -66
  36. package/dist/config/process-custom-config-files.js +26 -22
  37. package/dist/config/process-def-config-file.js +15 -11
  38. package/dist/custom.js +77 -28
  39. package/dist/default-x-config.js +9 -5
  40. package/dist/dot-file.js +42 -17
  41. package/dist/global.js +37 -31
  42. package/dist/index.js +100 -0
  43. package/dist/init-env.js +17 -9
  44. package/dist/middleware/@bin/html-filter.js +19 -7
  45. package/dist/middleware/bad-request.js +12 -8
  46. package/dist/middleware/compress.js +11 -7
  47. package/dist/middleware/cors.js +20 -7
  48. package/dist/middleware/fresh-filter.js +13 -7
  49. package/dist/middleware/handle-pre-dir.js +22 -17
  50. package/dist/middleware/request-filter.js +39 -25
  51. package/dist/middleware/service-mark.js +22 -25
  52. package/dist/middlewares.js +22 -20
  53. package/dist/router.js +174 -141
  54. package/dist/server.js +202 -93
  55. package/package.json +25 -29
  56. package/@types/business/utils/cookie/1.1.0/clean/index.d.ts +0 -3
  57. package/@types/business/utils/cookie/1.1.0/clean/js/index.d.ts +0 -2
  58. package/@types/components/cache/index.d.ts +0 -2
  59. package/@types/components/cache/lru.d.ts +0 -21
  60. package/@types/components/html-processor/index.d.ts +0 -9
  61. package/@types/components/injection/index.d.ts +0 -25
  62. package/@types/components/js-processor/index.d.ts +0 -5
  63. package/@types/components/md5/index.d.ts +0 -2
  64. package/@types/components/platform/index.d.ts +0 -21
  65. package/@types/components/proxy/index.d.ts +0 -26
  66. package/@types/components/querystring/index.d.ts +0 -25
  67. package/@types/components/redirect/index.d.ts +0 -11
  68. package/@types/components/request/helper.d.ts +0 -3
  69. package/@types/components/request/index.d.ts +0 -51
  70. package/@types/components/request/resolve-uri.d.ts +0 -6
  71. package/@types/components/version/index.d.ts +0 -26
  72. package/@types/env.d.ts +0 -1
@@ -2,46 +2,65 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.ErrorTypes = exports.set = exports.getCodeDetail = exports.getReturnCode = void 0;
6
- var _utils = require("@x-drive/utils");
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get ErrorTypes () {
13
+ return ErrorTypes;
14
+ },
15
+ get getCodeDetail () {
16
+ return getCodeDetail;
17
+ },
18
+ get getReturnCode () {
19
+ return getReturnCode;
20
+ },
21
+ get set () {
22
+ return set;
23
+ }
24
+ });
25
+ const _utils = require("@x-drive/utils");
7
26
  /**
8
27
  * 状态详细信息
9
28
  */ const CODE_DETAIL = {
10
29
  "FAIL": {
11
30
  "errorcode": -1,
12
- "msg": "\u7CFB\u7EDF\u7E41\u5FD9\uFF0C\u8BF7\u7A0D\u5019\u518D\u8BD5"
31
+ "msg": "系统繁忙,请稍候再试"
13
32
  },
14
33
  "SUCCESS": {
15
34
  "errorcode": 0,
16
- "msg": "\u8BF7\u6C42\u6210\u529F"
35
+ "msg": "请求成功"
17
36
  },
18
37
  "BUSY": {
19
38
  "errorcode": 1001,
20
- "msg": "\u7CFB\u7EDF\u7E41\u5FD9\uFF0C\u8BF7\u7A0D\u5019\u518D\u8BD5"
39
+ "msg": "系统繁忙,请稍候再试"
21
40
  },
22
41
  "WRONG_PARAMETER": {
23
42
  "errorcode": 400,
24
- "msg": "\u53C2\u6570\u9519\u8BEF"
43
+ "msg": "参数错误"
25
44
  },
26
45
  "NOT_FOUND": {
27
46
  "errorcode": 404,
28
- "msg": "\u6570\u636E\u4E0D\u5B58\u5728"
47
+ "msg": "数据不存在"
29
48
  },
30
49
  "PERMISSION_DENIED": {
31
50
  "errorcode": 403,
32
- "msg": "\u6CA1\u6709\u6743\u9650"
51
+ "msg": "没有权限"
33
52
  },
34
53
  "SERVER_BUSY": {
35
54
  "errorcode": 503,
36
- "msg": "\u7CFB\u7EDF\u7E41\u5FD9\uFF0C\u8BF7\u7A0D\u5019\u518D\u8BD5"
55
+ "msg": "系统繁忙,请稍候再试"
37
56
  },
38
57
  "SERVER_TIMEOUT": {
39
58
  "errorcode": 504,
40
- "msg": "\u7CFB\u7EDF\u7E41\u5FD9\uFF0C\u8BF7\u7A0D\u5019\u518D\u8BD5"
59
+ "msg": "系统繁忙,请稍候再试"
41
60
  },
42
61
  "UNCHECK_ERROR": {
43
62
  "errorcode": 9999999,
44
- "msg": "\u7CFB\u7EDF\u8FDB\u5165\u4E86\u5F02\u6B21\u5143..."
63
+ "msg": "系统进入了异次元..."
45
64
  }
46
65
  };
47
66
  /**
@@ -59,15 +78,15 @@ buildCodeMap();
59
78
  * @param msg 返回信息<可选>
60
79
  * @return 状态对象
61
80
  */ function getReturnCode(code, msg) {
62
- if ((0, _utils).isBoolean(code)) {
81
+ if ((0, _utils.isBoolean)(code)) {
63
82
  code = code ? 0 : -1;
64
83
  }
65
84
  var re;
66
85
  switch(true){
67
- case (0, _utils).isString(code):
86
+ case (0, _utils.isString)(code):
68
87
  re = CODE_DETAIL[code];
69
88
  break;
70
- case (0, _utils).isNumber(code):
89
+ case (0, _utils.isNumber)(code):
71
90
  re = CODE_DETAIL[CODE_MAP[code]] || CODE_DETAIL.UNCHECK_ERROR;
72
91
  break;
73
92
  default:
@@ -104,7 +123,3 @@ buildCodeMap();
104
123
  return code;
105
124
  }
106
125
  });
107
- exports.getReturnCode = getReturnCode;
108
- exports.getCodeDetail = getCodeDetail;
109
- exports.set = set;
110
- exports.ErrorTypes = ErrorTypes;
@@ -2,27 +2,37 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.uuid = exports.X_MAC = void 0;
6
- var _utils = require("@x-drive/utils");
7
- var _os = require("os");
8
- var _common = require("../common");
9
- var ref;
10
- var netInterfaces = (0, _os).networkInterfaces();
11
- const X_MAC = ((ref = Object.keys(netInterfaces).sort().map((key)=>netInterfaces[key]
12
- ).flat(1).find((en)=>{
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get X_MAC () {
13
+ return X_MAC;
14
+ },
15
+ get uuid () {
16
+ return uuid;
17
+ }
18
+ });
19
+ const _utils = require("@x-drive/utils");
20
+ const _os = require("os");
21
+ const _common = require("../common");
22
+ var netInterfaces = (0, _os.networkInterfaces)();
23
+ const X_MAC = Object.keys(netInterfaces).sort().map((key)=>netInterfaces[key]).flat(1).find((en)=>{
13
24
  return en && en.family && en.family.toLowerCase() === "ipv4" && en.mac !== "00:00:00:00:00:00" ? true : false;
14
- })) === null || ref === void 0 ? void 0 : ref.mac) || null;
15
- exports.X_MAC = X_MAC;
25
+ })?.mac || null;
16
26
  netInterfaces = null;
17
27
  /**
18
28
  * 生成一个 uuid
19
29
  * @param one 第一节数据
20
30
  * @param tow 第二节数据
21
31
  */ function uuid(one, tow) {
22
- var r = (0, _utils).random(2333333, 23333).toString(16);
32
+ var r = (0, _utils.random)(2333333, 23333).toString(16);
23
33
  r = r.length < 6 ? r += "0" : r;
24
34
  return [
25
- (0, _common).getMD5([
35
+ (0, _common.getMD5)([
26
36
  one,
27
37
  X_MAC,
28
38
  tow
@@ -31,4 +41,3 @@ netInterfaces = null;
31
41
  r
32
42
  ].join("-");
33
43
  }
34
- exports.uuid = uuid;
@@ -2,9 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = void 0;
6
- var _path = _interopRequireDefault(require("path"));
7
- function _interopRequireDefault(obj) {
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
12
+ function _interop_require_default(obj) {
8
13
  return obj && obj.__esModule ? obj : {
9
14
  default: obj
10
15
  };
@@ -15,7 +20,7 @@ function _interopRequireDefault(obj) {
15
20
  function getEnv(argv) {
16
21
  return argv.ENV !== undefined ? argv.ENV : process.env && process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() || "development";
17
22
  }
18
- var _default = (argv)=>{
23
+ const _default = (argv)=>{
19
24
  switch(getEnv(argv)){
20
25
  case "0":
21
26
  env = "DEVELOPMENT";
@@ -56,4 +61,3 @@ var _default = (argv)=>{
56
61
  envAddConfPath
57
62
  };
58
63
  };
59
- exports.default = _default;
@@ -2,29 +2,40 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.update = exports.get = exports.default = exports.log = void 0;
6
- var _processCustomConfigFiles = _interopRequireDefault(require("./process-custom-config-files"));
7
- var _processDefConfigFile = _interopRequireDefault(require("./process-def-config-file"));
8
- var _utils = require("@x-drive/utils");
9
- var _log = require("../components/log");
10
- var _cluster = _interopRequireDefault(require("cluster"));
11
- var _getEnv = _interopRequireDefault(require("./getEnv"));
12
- var _path = _interopRequireDefault(require("path"));
13
- function _interopRequireDefault(obj) {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get default () {
13
+ return _default;
14
+ },
15
+ get get () {
16
+ return get;
17
+ },
18
+ get log () {
19
+ return commonLog;
20
+ },
21
+ get update () {
22
+ return update;
23
+ }
24
+ });
25
+ const _processcustomconfigfiles = /*#__PURE__*/ _interop_require_default(require("./process-custom-config-files"));
26
+ const _processdefconfigfile = /*#__PURE__*/ _interop_require_default(require("./process-def-config-file"));
27
+ const _utils = require("@x-drive/utils");
28
+ const _log = require("../components/log");
29
+ const _cluster = /*#__PURE__*/ _interop_require_default(require("cluster"));
30
+ const _getEnv = /*#__PURE__*/ _interop_require_default(require("./getEnv"));
31
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
32
+ function _interop_require_default(obj) {
14
33
  return obj && obj.__esModule ? obj : {
15
34
  default: obj
16
35
  };
17
36
  }
18
- const argv = {};
19
- process.argv.forEach(function(item) {
20
- if (item.indexOf("=") !== -1) {
21
- const argPart = item.split("=");
22
- argv[argPart[0]] = argPart[1] || undefined;
23
- }
24
- });
25
- const meta = require(_path.default.resolve(process.cwd(), "package.json"));
26
37
  // 是否正处于 master 进程
27
- const IS_MASTER = parseInt(process.versions.node) >= 16 ? _cluster.default.isPrimary : _cluster.default.isMaster;
38
+ const IS_MASTER = _cluster.default.isPrimary;
28
39
  const logger = _log.globalLog.getLogger("config");
29
40
  /**
30
41
  * 日志类型存储对象
@@ -32,7 +43,7 @@ const logger = _log.globalLog.getLogger("config");
32
43
  "config": logger
33
44
  };
34
45
  /**
35
- * 通用日志方法
46
+ * 通用日志方法, 只在 master 进程上输出
36
47
  * @param cat 业务类型
37
48
  * @param type 日志等级
38
49
  */ function commonLog(cat, type, ...rest) {
@@ -59,7 +70,7 @@ const logger = _log.globalLog.getLogger("config");
59
70
  logArr = [].slice.call(arguments, setp);
60
71
  if (IS_MASTER) {
61
72
  if (!LOGERS[cat]) {
62
- LOGERS[cat] = log.getLogger(cat);
73
+ LOGERS[cat] = _log.globalLog.getLogger(cat);
63
74
  }
64
75
  LOGERS[cat][type].apply(LOGERS[cat], logArr);
65
76
  }
@@ -71,60 +82,81 @@ const logger = _log.globalLog.getLogger("config");
71
82
  logger.info.apply(logger, rest);
72
83
  }
73
84
  }
74
- const { env , envAddConfPath , isProd } = (0, _getEnv).default(argv);
75
- const conf = (0, _processDefConfigFile).default(argv);
76
- theLog(`A [ ${env} ] server starting.`);
77
- (0, _processCustomConfigFiles).default(conf, env, envAddConfPath);
78
85
  /**
79
- * 默认配置
80
- */ const CONFIG = {
81
- "name": meta.name,
82
- "version": meta.version,
83
- "env": env,
84
- "debug": !isProd,
85
- "port": 5000,
86
- "ip": argv.IP || null,
87
- "isProd": isProd,
88
- "workers": 3,
89
- "allowCache": true,
90
- "root": _path.default.resolve(process.cwd(), "public"),
91
- "isMaster": IS_MASTER
92
- };
93
- try {
94
- (0, _utils).extend(CONFIG, conf);
95
- if (argv.PORT) {
96
- const port = Number(argv.PORT);
97
- if (!isNaN(port)) {
98
- CONFIG.port = port;
99
- }
100
- }
101
- } catch (e) {
102
- logger.error("ASSIGN CONFIG ERROR.\n", e);
103
- }
104
- const NOT_SHOW = [
86
+ * 系统配置存储对象
87
+
88
+ * 模块加载即持有稳定引用, 内容在 loadConfig 执行后可用
89
+ */ const CONFIG = {};
90
+ /**是否已装载 */ var loaded = false;
91
+ /**配置打印时需要打码的字段 */ const NOT_SHOW = [
105
92
  "root",
106
93
  "sessionStore",
107
94
  "mongodb"
108
95
  ];
109
- Object.keys(CONFIG).forEach(function(key) {
110
- var val = JSON.stringify(CONFIG[key]);
111
- if (NOT_SHOW.indexOf(key) !== -1 && isProd) {
112
- val = "*".repeat(20);
96
+ /**
97
+ * 装载系统配置
98
+
99
+ * 解析命令行参数与环境, 按「内置默认 -> 业务生产配置 -> 环境配置 -> 本地配置 -> 命令行参数」
100
+ * 顺序合并配置, 重复调用直接返回已装载的配置
101
+ */ function loadConfig() {
102
+ if (loaded) {
103
+ return CONFIG;
113
104
  }
114
- theLog(`${key} : ${val}`);
115
- });
116
- console.log("");
117
- var _default = CONFIG;
118
- exports.default = _default;
119
- function get() {
105
+ loaded = true;
106
+ const argv = {};
107
+ process.argv.forEach(function(item) {
108
+ if (item.indexOf("=") !== -1) {
109
+ const argPart = item.split("=");
110
+ argv[argPart[0]] = argPart[1] || undefined;
111
+ }
112
+ });
113
+ const meta = require(_path.default.resolve(process.cwd(), "package.json"));
114
+ const { env, envAddConfPath, isProd } = (0, _getEnv.default)(argv);
115
+ const conf = (0, _processdefconfigfile.default)(argv);
116
+ theLog(`A [ ${env} ] server starting.`);
117
+ (0, _processcustomconfigfiles.default)(conf, env, envAddConfPath);
118
+ // 默认配置
119
+ (0, _utils.extend)(CONFIG, {
120
+ "name": meta.name,
121
+ "version": meta.version,
122
+ "env": env,
123
+ "debug": !isProd,
124
+ "port": 5000,
125
+ "ip": argv.IP || null,
126
+ "isProd": isProd,
127
+ "workers": 3,
128
+ "allowCache": true,
129
+ "root": _path.default.resolve(process.cwd(), "public"),
130
+ "isMaster": IS_MASTER
131
+ });
132
+ try {
133
+ (0, _utils.extend)(CONFIG, conf);
134
+ if (argv.PORT) {
135
+ const port = Number(argv.PORT);
136
+ if (!isNaN(port)) {
137
+ CONFIG.port = port;
138
+ }
139
+ }
140
+ } catch (e) {
141
+ logger.error("ASSIGN CONFIG ERROR.\n", e);
142
+ }
143
+ Object.keys(CONFIG).forEach(function(key) {
144
+ var val = JSON.stringify(CONFIG[key]);
145
+ if (NOT_SHOW.indexOf(key) !== -1 && isProd) {
146
+ val = "*".repeat(20);
147
+ }
148
+ theLog(`${key} : ${val}`);
149
+ });
150
+ console.log("");
151
+ return CONFIG;
152
+ }
153
+ const _default = loadConfig;
154
+ /**获取系统配置对象 */ function get() {
120
155
  return CONFIG;
121
156
  }
122
- /**更新配置 */ function update(conf1) {
123
- if ((0, _utils).isObject(conf1)) {
124
- (0, _utils).extend(CONFIG, conf1);
157
+ /**更新配置 */ function update(conf) {
158
+ if ((0, _utils.isObject)(conf)) {
159
+ (0, _utils.extend)(CONFIG, conf);
125
160
  }
126
161
  return CONFIG;
127
162
  }
128
- exports.log = commonLog;
129
- exports.get = get;
130
- exports.update = update;
@@ -2,57 +2,62 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = void 0;
6
- var _common = require("../components/common");
7
- var _defaultXConfig = _interopRequireDefault(require("../default-x-config"));
8
- var _utils = require("@x-drive/utils");
9
- var _path = _interopRequireDefault(require("path"));
10
- var _fs = _interopRequireDefault(require("fs"));
11
- function _interopRequireDefault(obj) {
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _common = require("../components/common");
12
+ const _defaultxconfig = /*#__PURE__*/ _interop_require_default(require("../default-x-config"));
13
+ const _utils = require("@x-drive/utils");
14
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
15
+ const _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
16
+ function _interop_require_default(obj) {
12
17
  return obj && obj.__esModule ? obj : {
13
18
  default: obj
14
19
  };
15
20
  }
16
21
  function customConfigProcessor(conf, env, envAddConfPath) {
17
22
  // 业务自定义服务端业务文件夹
18
- const customDir = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir);
23
+ const customDir = _path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir);
19
24
  // 是否存在业务自定义服务端业务文件夹
20
- const hasCustom = (0, _common).checkFileStat(customDir);
25
+ const hasCustom = (0, _common.checkFileStat)(customDir);
21
26
  if (hasCustom) {
22
27
  /**业务生产环境配置文件地址 */ let customConfPath = _path.default.resolve(customDir, "@config", "config.js");
23
- /**是否有业务自定义配置 */ let hasCustomConf = (0, _common).checkFileStat(customConfPath);
28
+ /**是否有业务自定义配置 */ let hasCustomConf = (0, _common.checkFileStat)(customConfPath);
24
29
  if (hasCustomConf) {
25
- (0, _utils).merge(conf, (0, _common).getRealDefaultMod(require(customConfPath)));
30
+ (0, _utils.merge)(conf, (0, _common.getRealDefaultMod)(require(customConfPath)));
26
31
  }
27
32
  }
28
33
  if (envAddConfPath) {
29
- const devConf = (0, _common).getRealDefaultMod(require(_path.default.join("..", envAddConfPath)));
34
+ const devConf = (0, _common.getRealDefaultMod)(require(_path.default.join("..", envAddConfPath)));
30
35
  let extConf = {};
31
36
  let customDevConf = {};
32
37
  if (hasCustom) {
33
38
  /**对应环境配置文件地址 */ let customConfPath = _path.default.resolve(customDir, `${envAddConfPath}.js`);
34
- if ((0, _common).checkFileStat(customConfPath)) {
35
- customDevConf = (0, _common).getRealDefaultMod(require(customConfPath));
39
+ if ((0, _common.checkFileStat)(customConfPath)) {
40
+ customDevConf = (0, _common.getRealDefaultMod)(require(customConfPath));
36
41
  }
37
42
  if (env === "DEVELOPMENT") {
38
43
  let localConfPath = _path.default.resolve(customDir, "@config", "config.lo.js");
39
- if ((0, _common).checkFileStat(localConfPath)) {
40
- extConf = (0, _common).getRealDefaultMod(require(localConfPath));
44
+ if ((0, _common.checkFileStat)(localConfPath)) {
45
+ extConf = (0, _common.getRealDefaultMod)(require(localConfPath));
41
46
  extConf.hasLo = true;
42
47
  }
43
48
  }
44
49
  }
45
50
  // 合并
46
- (0, _utils).merge(conf, devConf, customDevConf, extConf);
51
+ (0, _utils.merge)(conf, devConf, customDevConf, extConf);
47
52
  }
48
53
  // apis文件夹配置处理
49
54
  if (hasCustom) {
50
- const APIS_PATH = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "@config", "@apis");
51
- if ((0, _common).checkFileStat(APIS_PATH) && conf.passExtApis !== true) {
55
+ const APIS_PATH = _path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir, "@config", "@apis");
56
+ if ((0, _common.checkFileStat)(APIS_PATH) && conf.passExtApis !== true) {
52
57
  let apis = [];
53
58
  _fs.default.readdirSync(APIS_PATH).forEach((file)=>{
54
59
  if (!file.startsWith(".") && _path.default.extname(file) === ".js") {
55
- apis.push((0, _common).getRealDefaultMod(require(`${APIS_PATH}/${file}`)));
60
+ apis.push((0, _common.getRealDefaultMod)(require(`${APIS_PATH}/${file}`)));
56
61
  }
57
62
  });
58
63
  if (apis.length) {
@@ -62,5 +67,4 @@ function customConfigProcessor(conf, env, envAddConfPath) {
62
67
  }
63
68
  }
64
69
  }
65
- var _default = customConfigProcessor;
66
- exports.default = _default;
70
+ const _default = customConfigProcessor;
@@ -2,11 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = void 0;
6
- var _common = require("../components/common");
7
- var _config = _interopRequireDefault(require("../@config/config"));
8
- var _path = require("path");
9
- function _interopRequireDefault(obj) {
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _common = require("../components/common");
12
+ const _config = /*#__PURE__*/ _interop_require_default(require("../@config/config"));
13
+ const _path = require("path");
14
+ function _interop_require_default(obj) {
10
15
  return obj && obj.__esModule ? obj : {
11
16
  default: obj
12
17
  };
@@ -14,16 +19,15 @@ function _interopRequireDefault(obj) {
14
19
  function defConfigProcessor(argv) {
15
20
  if (argv.IP) {
16
21
  // 绑定 IP
17
- (_config.default).ip = argv.IP;
22
+ _config.default.ip = argv.IP;
18
23
  }
19
- const apiPath = (0, _path).resolve("..", "@config", "@apis", "index.js");
24
+ const apiPath = (0, _path.resolve)("..", "@config", "@apis", "index.js");
20
25
  // api 配置文件处理
21
- var hasDefApiConf = (0, _common).checkFileStat(apiPath, true);
26
+ var hasDefApiConf = (0, _common.checkFileStat)(apiPath, true);
22
27
  if (hasDefApiConf) {
23
- _config.default.apis = Object.assign(_config.default.apis || {}, (0, _common).getRealDefaultMod(require(apiPath)));
28
+ _config.default.apis = Object.assign(_config.default.apis || {}, (0, _common.getRealDefaultMod)(require(apiPath)));
24
29
  }
25
30
  hasDefApiConf = null;
26
31
  return _config.default;
27
32
  }
28
- var _default = defConfigProcessor;
29
- exports.default = _default;
33
+ const _default = defConfigProcessor;
package/dist/custom.js CHANGED
@@ -2,47 +2,96 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = void 0;
6
- var _utils = require("@x-drive/utils");
7
- var _common = require("./components/common");
8
- var _defaultXConfig = _interopRequireDefault(require("./default-x-config"));
9
- var _config = require("./config");
10
- var _util = require("util");
11
- var _path = _interopRequireDefault(require("path"));
12
- function _interopRequireDefault(obj) {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get loadCustom () {
13
+ return loadCustom;
14
+ },
15
+ get runReady () {
16
+ return runReady;
17
+ },
18
+ get runShutdown () {
19
+ return runShutdown;
20
+ }
21
+ });
22
+ const _utils = require("@x-drive/utils");
23
+ const _common = require("./components/common");
24
+ const _defaultxconfig = /*#__PURE__*/ _interop_require_default(require("./default-x-config"));
25
+ const _config = require("./config");
26
+ const _util = require("util");
27
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
28
+ function _interop_require_default(obj) {
13
29
  return obj && obj.__esModule ? obj : {
14
30
  default: obj
15
31
  };
16
32
  }
17
- /**处理自定义业务 */ async function processCustom() {
18
- const config = (0, _config).get();
19
- const CustomPath = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "custom");
20
- const hasCustom = (0, _common).checkFileStat(CustomPath);
21
- if (!hasCustom) {
33
+ /**已装载的自定义模块钩子 */ const HOOKS = [];
34
+ /**
35
+ * 装载自定义业务模块并执行 setup 阶段
36
+
37
+ * 模块支持两种导出形态:
38
+ * 1. 导出函数, 视为 setup 钩子
39
+ * 2. 导出 `{ setup?, ready?, shutdown? }` 钩子对象
40
+ */ async function loadCustom() {
41
+ const config = (0, _config.get)();
42
+ const CustomPath = _path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir, "custom");
43
+ if (!(0, _common.checkFileStat)(CustomPath)) {
22
44
  return;
23
45
  }
24
- if (!(0, _utils).isArray(config.custom) || !config.custom.length) {
46
+ if (!(0, _utils.isArray)(config.custom) || !config.custom.length) {
25
47
  return;
26
48
  }
27
- if (hasCustom) {
49
+ for (const item of config.custom){
50
+ const isStrItem = (0, _utils.isString)(item);
51
+ const name = isStrItem ? item : item[0];
28
52
  try {
29
- for (const item of config.custom){
30
- const isStrItem = (0, _utils).isString(item);
31
- const name = isStrItem ? item : item[0];
32
- const conf = isStrItem ? null : item[1];
33
- const mod = (0, _common).getRealDefaultMod(require(_path.default.resolve(CustomPath, name)));
34
- if ((0, _utils).isFunction(mod)) {
35
- mod(conf);
36
- } else if ((0, _utils).isAsyncFunction(mod)) {
37
- await mod(conf);
53
+ const conf = isStrItem ? null : item[1];
54
+ const mod = (0, _common.getRealDefaultMod)(require(_path.default.resolve(CustomPath, String(name))));
55
+ var hooks = null;
56
+ if ((0, _utils.isExecutable)(mod)) {
57
+ hooks = {
58
+ "setup": mod
59
+ };
60
+ } else if ((0, _utils.isObject)(mod)) {
61
+ hooks = mod;
62
+ }
63
+ if (hooks) {
64
+ HOOKS.push(hooks);
65
+ if ((0, _utils.isExecutable)(hooks.setup)) {
66
+ await hooks.setup(conf);
38
67
  }
39
- masterLog("custom", `[${name}] loaded`);
68
+ (0, _config.log)("custom", `[${name}] loaded`);
40
69
  }
41
70
  } catch (e) {
42
- masterLog("custom", "error", (0, _util).inspect(e));
71
+ (0, _config.log)("custom", "error", `[${name}]`, (0, _util.inspect)(e));
43
72
  }
44
73
  }
45
74
  console.log("");
46
75
  }
47
- var _default = processCustom;
48
- exports.default = _default;
76
+ /**执行全部 ready 钩子 */ async function runReady() {
77
+ for (const hooks of HOOKS){
78
+ if ((0, _utils.isExecutable)(hooks.ready)) {
79
+ try {
80
+ await hooks.ready();
81
+ } catch (e) {
82
+ (0, _config.log)("custom", "error", (0, _util.inspect)(e));
83
+ }
84
+ }
85
+ }
86
+ }
87
+ /**执行全部 shutdown 钩子 */ async function runShutdown() {
88
+ for (const hooks of HOOKS){
89
+ if ((0, _utils.isExecutable)(hooks.shutdown)) {
90
+ try {
91
+ await hooks.shutdown();
92
+ } catch (e) {
93
+ (0, _config.log)("custom", "error", (0, _util.inspect)(e));
94
+ }
95
+ }
96
+ }
97
+ }