@x-9lab/xlab 1.6.1 → 2.0.0

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 -54
  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 +170 -0
  17. package/README.md +37 -17
  18. package/dist/@config/config.dev.js +7 -3
  19. package/dist/@config/config.js +7 -3
  20. package/dist/@config/config.sand.js +7 -3
  21. package/dist/bin/watch.js +13 -9
  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 -19
  53. package/dist/router.js +174 -141
  54. package/dist/server.js +195 -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,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
+ }
@@ -2,18 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.default = void 0;
6
- var _utils = require("@x-drive/utils");
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ const _utils = require("@x-drive/utils");
7
12
  /**默认业务目录 */ var businessDir = "@server";
8
13
  let Config = class Config {
9
14
  get businessDir() {
10
15
  return businessDir;
11
16
  }
12
17
  set businessDir(val) {
13
- if ((0, _utils).isString(val)) {
18
+ if ((0, _utils.isString)(val)) {
14
19
  businessDir = val;
15
20
  }
16
21
  }
17
22
  };
18
- var _default = new Config;
19
- exports.default = _default;
23
+ const _default = new Config;
package/dist/dot-file.js CHANGED
@@ -2,17 +2,37 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- exports.processDotFile = void 0;
6
- var _utils = require("@x-drive/utils");
7
- var _nodeUtil = require("node:util");
8
- var _path = _interopRequireDefault(require("path"));
9
- var _fs = _interopRequireDefault(require("fs"));
10
- 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 getXlabEnv () {
13
+ return getXlabEnv;
14
+ },
15
+ get processDotFile () {
16
+ return processDotFile;
17
+ }
18
+ });
19
+ const _utils = require("@x-drive/utils");
20
+ const _nodeutil = require("node:util");
21
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
22
+ const _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
23
+ function _interop_require_default(obj) {
11
24
  return obj && obj.__esModule ? obj : {
12
25
  default: obj
13
26
  };
14
27
  }
15
28
  /**行分割正则 */ const LINE_SPLIT_REGEXP = /[\s]?=[\s]?/;
29
+ /**`.xlab` 环境数据存储对象 */ const XLAB = {};
30
+ /**
31
+ * 获取 `.xlab` 系列文件定义的环境数据
32
+ * @return 环境数据对象
33
+ */ function getXlabEnv() {
34
+ return XLAB;
35
+ }
16
36
  /**处理根目录下可能存在的配置文件 */ function processDotFile(env, logger) {
17
37
  env = env.toLowerCase();
18
38
  if (env === "production") {
@@ -21,20 +41,25 @@ function _interopRequireDefault(obj) {
21
41
  env = `.${env}`;
22
42
  }
23
43
  const dotFile = _path.default.resolve(process.cwd(), `.xlab${env}`);
44
+ var fileStr;
24
45
  try {
25
- const fileStr = _fs.default.readFileSync(dotFile, "utf8");
26
- const lines = fileStr.split("\n");
27
- for(let i = 0; i < lines.length; i++){
28
- const line = lines[i];
29
- const linePart = line.split(LINE_SPLIT_REGEXP);
30
- if ((0, _utils).isArray(linePart) && linePart.length === 2) {
31
- global.XLAB[linePart[0]] = JSON.parse(linePart[1]);
32
- }
33
- }
46
+ fileStr = _fs.default.readFileSync(dotFile, "utf8");
34
47
  } catch (e) {
35
48
  if (e.code !== "ENOENT") {
36
- logger.error((0, _nodeUtil).inspect(e));
49
+ logger.error((0, _nodeutil.inspect)(e));
50
+ }
51
+ return;
52
+ }
53
+ const lines = fileStr.split("\n");
54
+ for(let i = 0; i < lines.length; i++){
55
+ const linePart = lines[i].split(LINE_SPLIT_REGEXP);
56
+ if ((0, _utils.isArray)(linePart) && linePart.length === 2) {
57
+ // 按行解析, 单行数据异常不影响其他行
58
+ try {
59
+ XLAB[linePart[0]] = JSON.parse(linePart[1]);
60
+ } catch (e) {
61
+ logger.error(`.xlab${env} 第 ${i + 1} 行解析失败:`, (0, _nodeutil.inspect)(e));
62
+ }
37
63
  }
38
64
  }
39
65
  }
40
- exports.processDotFile = processDotFile;
package/dist/global.js CHANGED
@@ -1,8 +1,34 @@
1
1
  "use strict";
2
- var _defaultXConfig = _interopRequireDefault(require("./default-x-config"));
3
- var _config = require("./config");
4
- var _path = _interopRequireDefault(require("path"));
5
- function _interopRequireDefault(obj) {
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
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 getSysConfig () {
13
+ return getSysConfig;
14
+ },
15
+ get requireMod () {
16
+ return requireMod;
17
+ },
18
+ get requireModel () {
19
+ return requireModel;
20
+ },
21
+ get requireService () {
22
+ return requireService;
23
+ },
24
+ get setSysConfig () {
25
+ return setSysConfig;
26
+ }
27
+ });
28
+ const _defaultxconfig = /*#__PURE__*/ _interop_require_default(require("./default-x-config"));
29
+ const _config = require("./config");
30
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
31
+ function _interop_require_default(obj) {
6
32
  return obj && obj.__esModule ? obj : {
7
33
  default: obj
8
34
  };
@@ -16,51 +42,31 @@ function _interopRequireDefault(obj) {
16
42
  return _path.default.resolve(root || __dirname, "components", name);
17
43
  }
18
44
  /**
19
- * 获取 components 模块
45
+ * 获取内置组件
20
46
  * @param name 模块名
21
47
  * @return 模块对象
22
48
  */ function requireMod(name) {
23
49
  return require(getModulePath(name));
24
50
  }
25
- global.requireMod = requireMod;
26
- /**
27
- * model 存放路径
28
- */ const MODEL_PATH = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "business", "@models");
29
- /**
30
- * 全局获取 model 的方法
31
- * @param name model 名称
32
- * @return model 模块
33
- */ function requireModel(name) {
34
- return require(_path.default.resolve(MODEL_PATH, name));
51
+ function requireModel(name) {
52
+ return require(_path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir, "business", "@models", name));
35
53
  }
36
- global.requireModel = requireModel;
37
- /**
38
- * services 存放目录
39
- */ const SERVICES_PATH = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, "business", "@services");
40
- /**
41
- * 全局获取 service 的方法
42
- * @param name service 名称
43
- * @return service 模块
44
- */ function requireService(name) {
45
- return require(_path.default.resolve(SERVICES_PATH, name));
54
+ function requireService(name) {
55
+ return require(_path.default.resolve(process.cwd(), _defaultxconfig.default.businessDir, "business", "@services", name));
46
56
  }
47
- global.requireService = requireService;
48
57
  /**
49
58
  * 获取系统配置
50
59
  * @param key 配置项
51
60
  */ function getSysConfig(key) {
52
- const conf = (0, _config).get();
61
+ const conf = (0, _config.get)();
53
62
  if (key) {
54
63
  return conf[key];
55
64
  }
56
65
  return conf;
57
66
  }
58
- global.getSysConfig = getSysConfig;
59
67
  /**
60
68
  * 更新系统配置
61
69
  * @param conf 配置项
62
70
  */ function setSysConfig(conf) {
63
- (0, _config).update(conf);
71
+ (0, _config.update)(conf);
64
72
  }
65
- global.setSysConfig = setSysConfig;
66
- global.XLAB = {}; // globalLogger.log("global")