@x-9lab/xlab 1.0.12 → 1.1.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.
@@ -3,7 +3,7 @@
3
3
  * @param cat 业务类型
4
4
  * @param type 日志等级
5
5
  */
6
- declare function commonLog(cat: string, type: string): void;
6
+ declare function commonLog(cat: string, type: string, ...rest: any[]): void;
7
7
  export { commonLog as log };
8
8
  /**
9
9
  * 默认配置
@@ -83,6 +83,15 @@ declare global {
83
83
  /**操作结果 */
84
84
  success?: boolean;
85
85
  }
86
+ /**中间件配置 */
87
+ type MiddlewareConfig = {
88
+ /**中间件名称 */
89
+ name?: string;
90
+ /**中间件位置 */
91
+ index?: number;
92
+ /**中间件配置 */
93
+ config?: Record<string, JsonValue>;
94
+ };
86
95
  interface IConfig {
87
96
  /**服务(应用)名称 */
88
97
  name?: string;
@@ -110,8 +119,13 @@ declare global {
110
119
  enableComboCache?: boolean;
111
120
  /**是否开启定时任务 */
112
121
  enableCron?: boolean;
113
- /**开启的中间件列表 */
122
+ /**
123
+ * 开启的中间件列表
124
+ * @deprecated since version 1.1.0, 1.3.0 后将完全删除
125
+ */
114
126
  middleware?: (string | (string | Record<string, any>)[])[];
127
+ /**开启的中间件列表 */
128
+ middlewares?: Record<string, boolean | MiddlewareConfig>;
115
129
  /**自定模块配置 */
116
130
  custom?: string[];
117
131
  /**是否启用严格 ssl */
package/README.md CHANGED
@@ -14,6 +14,10 @@ X-9lab 通用服务端
14
14
  }
15
15
  }
16
16
  ```
17
+ - 支持传入 `APP_NAME` 参数,可用于解决诸如同个服务器上有多个实例运行时的识别问题
18
+ ```bash
19
+ xlab --APP_NAME=nice
20
+ ```
17
21
  - 配置文件 `xlab.config.js`
18
22
  模块会自动检测执行根目录中是否存在 `xlab.config.js` 文件。如存在该文件则会使用该文件为模块的启动配置文件。支持的配置项:
19
23
  1. `watch` 是否开启 watch 模式
@@ -171,7 +175,9 @@ X-9lab 通用服务端
171
175
  |staticHtmlFileMaxage|`number`|0|静态 html 文件缓存时间|
172
176
  |enableComboCache|`boolean`|true|是否开启 Combo 缓存|
173
177
  |enableCron|`boolean`|false|是否开启定时任务|
174
- |middleware|`Array<string | (string | Record<string, any>)[]>`|[]|开启的中间件列表 |
178
+ |middleware|`Array<string | (string | Record<string, any>)[]>`|[]|开启的中间件列表。因不方便合并替换,v1.1.0 开始建议使用 `middlewares` 来配置 |
179
+ |middlewares|`Record<string, MiddlewareConfig>`|{}|开启的中间件列表 |
180
+ ||MiddlewareConfig||`index` 用于定义中间件位置,不提供将使用配置对象中的默认顺序<br/> `name` 中间件名称,不提供将使用配置对象的键名<br/>`config` 中间件配置|
175
181
  |custom|`string[]`||自定模块配置|
176
182
  |strictSSL|`boolean`||是否启用严格 ssl|
177
183
  |apis|`Record<string, string>`|{}|页端注入的 api 设置|
@@ -55,7 +55,7 @@ Object.keys(returnCode).forEach(function(key) {
55
55
  }
56
56
  processorData[key] = dat;
57
57
  });
58
- return (0, _utils.labelReplace)(html, processorData);
58
+ return (0, _utils.labelReplace)(html, processorData, true);
59
59
  }
60
60
  return html;
61
61
  }
@@ -46,7 +46,7 @@ const logger = _log.globalLog.getLogger("config");
46
46
  * 通用日志方法
47
47
  * @param cat 业务类型
48
48
  * @param type 日志等级
49
- */ function commonLog(cat, type) {
49
+ */ function commonLog(cat, type, ...rest) {
50
50
  var logArr;
51
51
  var setp = 0;
52
52
  switch(arguments.length){
@@ -25,20 +25,64 @@ const cwd = process.cwd();
25
25
  "compress": true,
26
26
  "cors": true
27
27
  };
28
+ /**兼容老的中间件声明 */ function compatibleWithOldVer(list) {
29
+ const middlewares = {};
30
+ list.forEach((item, index)=>{
31
+ var subject;
32
+ var name;
33
+ subject = {
34
+ index
35
+ };
36
+ if ((0, _utils.isString)(item)) {
37
+ name = item;
38
+ } else if ((0, _utils.isArray)(item)) {
39
+ name = item[0];
40
+ if (item[1]) {
41
+ subject.config = item[1];
42
+ }
43
+ }
44
+ middlewares[name] = subject;
45
+ });
46
+ return middlewares;
47
+ }
48
+ /**获取中间件加载执行列表 */ function getExeList() {
49
+ const config = getSysConfig();
50
+ var mConfig;
51
+ if (config.middlewares) {
52
+ mConfig = config.middlewares;
53
+ } else if ((0, _utils.isArray)(config.middleware) && config.middleware.length) {
54
+ masterLog("middlewares", "warn", "middleware 配置已弃用并将在 v1.3.0 之后删除,请使用 middlewares 配置中间件");
55
+ mConfig = compatibleWithOldVer(config.middleware);
56
+ } else {
57
+ mConfig = null;
58
+ }
59
+ if (mConfig) {
60
+ return Object.keys(mConfig).filter((key)=>(0, _utils.isBoolean)(mConfig[key]) ? mConfig[key] : true).map((key, index)=>{
61
+ if ((0, _utils.isBoolean)(mConfig[key])) {
62
+ mConfig[key] = {};
63
+ }
64
+ if (!(0, _utils.isString)(mConfig[key].name)) {
65
+ mConfig[key].name = key;
66
+ }
67
+ if (!(0, _utils.isNumber)(mConfig[key].index)) {
68
+ mConfig[key].index = index;
69
+ }
70
+ return mConfig[key];
71
+ }).sort((now, next)=>now.index - next.index);
72
+ }
73
+ return [];
74
+ }
28
75
  // [最后3个中间件固定是 系统路由,静态文件服务,错误请求处理模块]
29
76
  /**加载中间件 */ function loadMiddleware(app) {
30
- const config = getSysConfig();
31
- if ((0, _utils.isArray)(config.middleware) && config.middleware.length) {
32
- config.middleware.forEach((item)=>{
33
- const isSrtItem = (0, _utils.isString)(item);
34
- let name = isSrtItem ? item : item[0];
35
- const conf = isSrtItem ? null : item[1];
36
- if (INTERNAL_MIDDLEWARE[name]) {
37
- app.use((0, _common.getRealDefaultMod)(require(`./middleware/${name}`))(conf));
77
+ const list = getExeList();
78
+ if ((0, _utils.isArray)(list) && list.length) {
79
+ list.forEach((item)=>{
80
+ if (INTERNAL_MIDDLEWARE[item.name]) {
81
+ app.use((0, _common.getRealDefaultMod)(require(`./middleware/${item.name}`))(item.config));
38
82
  } else {
39
- app.use((0, _common.getRealDefaultMod)(require(`${cwd}/${_defaultXConfig.default.businessDir}/middleware/${name}`))(conf));
83
+ app.use((0, _common.getRealDefaultMod)(require(`${cwd}/${_defaultXConfig.default.businessDir}/middleware/${item.name}`))(item.config));
40
84
  }
41
- masterLog(`MIDDLEWARE >>> [${name}] launch`);
85
+ masterLog(`MIDDLEWARE >>> [${item.name}] launch`);
42
86
  });
43
87
  console.log("");
44
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.0.12",
3
+ "version": "1.1.0",
4
4
  "description": "9lab 服务端模块",
5
5
  "versionDesc": "originHost 现在所有请求都会带上",
6
6
  "scripts": {