mm_expand 2.1.2 → 2.1.4

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.
package/index.js CHANGED
@@ -22,6 +22,9 @@ var { Lang } = require('./lib/lang.js');
22
22
  var { Event } = require('./lib/event.js');
23
23
  var { Base } = require('./lib/base.js');
24
24
  var { Timer } = require('./lib/timer.js');
25
+ var { Errors } = require('./lib/errors.js');
26
+ var { Req } = require('./lib/req.js');
27
+ var { Ret } = require('./lib/ret.js');
25
28
  var { Validator, validator } = require('./lib/validator.js');
26
29
 
27
30
  // 初始化$对象
@@ -78,6 +81,12 @@ module.exports = {
78
81
  File,
79
82
  // 目录类
80
83
  Dir,
84
+ // 错误类
85
+ Errors,
86
+ // 请求类
87
+ Req,
88
+ // 响应类
89
+ Ret,
81
90
  // 验证器实例
82
91
  validator,
83
92
  // 事件驱动实例
package/lib/base.js CHANGED
@@ -2,12 +2,21 @@ const {
2
2
  Event
3
3
  } = require('./event.js');
4
4
 
5
- require('./global');
6
-
7
5
  /**
8
6
  * 基础类
9
7
  */
10
8
  class Base extends Event {
9
+ /**
10
+ * 配置参数
11
+ * @type {object}
12
+ */
13
+ static config = {};
14
+ /**
15
+ * 状态
16
+ * @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
17
+ */
18
+ #status = 'creating';
19
+
11
20
  /**
12
21
  * 构造函数
13
22
  * @param {object} config 配置参数
@@ -19,34 +28,36 @@ class Base extends Event {
19
28
  * @type {object}
20
29
  */
21
30
  this.config = { ...this.constructor.config };
22
-
23
- /**
24
- * 状态
25
- * @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
26
- */
27
- this.status = 'creating';
28
31
 
29
32
  // 设置配置
30
33
  this.setConfig(config);
34
+ // 监听事件
35
+ this._listen();
36
+ }
37
+
38
+ /**
39
+ * 获取当前状态
40
+ * @returns {string} 当前状态
41
+ */
42
+ getStatus() {
43
+ return this.#status;
44
+ }
31
45
 
32
- // 状态变更为 created 已完成
33
- this.status = 'created';
34
-
35
- // 监听初始化事件
36
- this._onInit();
37
- // 监听销毁事件
38
- this._onDestroy();
46
+ /**
47
+ * 设置当前状态
48
+ * @param {string} status 状态
49
+ */
50
+ setStatus(status) {
51
+ this.#status = status;
39
52
  }
40
53
  }
41
54
 
42
55
  /**
43
- * 日志输出
44
- * @param {string} level 日志级别
45
- * @param {string} message 日志消息
46
- * @param {...any} params 日志参数
56
+ * 监听事件
47
57
  */
48
- Base.prototype.log = function (level, message, ...params) {
49
- this._logger[level](`[${this.constructor.name}] ${message}`, ...params);
58
+ Base.prototype._listen = function () {
59
+ this._onInit();
60
+ this._onDestroy();
50
61
  };
51
62
 
52
63
  /**
@@ -55,11 +66,11 @@ Base.prototype.log = function (level, message, ...params) {
55
66
  Base.prototype._onInit = function () {
56
67
  // 初始化
57
68
  this.on('init:before', async (ctx) => {
58
- this.status = 'initing';
69
+ this.setStatus('initing');
59
70
  await this._initCore(...ctx.params);
60
71
  });
61
72
  this.on('init:after', (ctx) => {
62
- this.status = 'inited';
73
+ this.setStatus('inited');
63
74
  });
64
75
  };
65
76
 
@@ -67,23 +78,29 @@ Base.prototype._onInit = function () {
67
78
  * 监听销毁事件
68
79
  */
69
80
  Base.prototype._onDestroy = function () {
70
- let status_last = 'created';
81
+ let status_last = 'stopped';
71
82
  // 销毁
72
83
  this.on('destroy:before', async (ctx) => {
73
84
  status_last = this.status;
74
- this.status = 'destroying';
85
+ this.setStatus('destroying');
75
86
  });
76
87
  this.on('destroy:after', async (ctx) => {
77
88
  await this._destroyCore(...ctx.params);
78
89
  });
79
90
  this.on('destroy:success', (ctx) => {
80
- this.status = 'destroyed';
91
+ this.setStatus('destroyed');
81
92
  });
82
93
  this.on('destroy:error', (ctx) => {
83
- this.status = status_last;
94
+ this.setStatus(status_last);
84
95
  });
85
96
  };
86
97
 
98
+ /**
99
+ * 预设值
100
+ */
101
+ Base.prototype._preset = function () {
102
+ };
103
+
87
104
  /**
88
105
  * 设置配置参数
89
106
  * @param {object} config 主要配置
@@ -96,6 +113,16 @@ Base.prototype.setConfig = function (config) {
96
113
  }
97
114
  Object.assign(this.config, config);
98
115
  }
116
+ // 初始化设置
117
+ this._preset();
118
+ return this.config;
119
+ };
120
+
121
+ /**
122
+ * 获取当前配置
123
+ * @returns {Object} 配置对象
124
+ */
125
+ Base.prototype.getConfig = function () {
99
126
  return this.config;
100
127
  };
101
128
 
package/lib/errors.js ADDED
@@ -0,0 +1,160 @@
1
+ const { Base } = require('./base');
2
+
3
+ /**
4
+ * @file 错误代码管理类
5
+ * @class Errors
6
+ */
7
+ class Errors extends Base {
8
+ static config = {
9
+ /**
10
+ * @description 错误码语言
11
+ * @type {string}
12
+ * @default "zh-CN"
13
+ */
14
+ lang: "zh-CN"
15
+ };
16
+
17
+ /**
18
+ * @description 构造函数
19
+ * @constructor
20
+ * @param {Object} config 配置项
21
+ */
22
+ constructor(config) {
23
+ super(config);
24
+ /**
25
+ * @description 错误码语言包
26
+ */
27
+ this._langs = {
28
+ "zh-CN": {
29
+ "10000": "业务逻辑错误",
30
+ "0": "成功",
31
+ "100": "继续",
32
+ "101": "切换协议",
33
+ "200": "确定",
34
+ "201": "已创建",
35
+ "202": "已接受",
36
+ "204": "无内容",
37
+ "301": "已永久移动",
38
+ "302": "已找到",
39
+ "303": "见其他",
40
+ "304": "未修改",
41
+ "307": "临时重定向",
42
+ "400": "错误请求",
43
+ "401": "未授权",
44
+ "403": "禁止访问",
45
+ "404": "未找到",
46
+ "405": "方法不允许",
47
+ "408": "请求超时",
48
+ "413": "Payload Too Large",
49
+ "415": "不支持的媒体类型",
50
+ "429": "请求过多",
51
+ "500": "内部服务器错误",
52
+ "501": "未实现",
53
+ "502": "错误网关",
54
+ "503": "服务不可用",
55
+ "504": "网关超时",
56
+ "-32700": "格式错误",
57
+ "-32701": "不支持的编码",
58
+ "-32702": "编码中包含无效字符",
59
+ "-32600": "无效的json-rpc. 不符合规范",
60
+ "-32601": "请求的方法未找到",
61
+ "-32602": "无效的方法参数",
62
+ "-32603": "内部json-rpc错误",
63
+ "-32500": "应用错误",
64
+ "-32400": "系统错误",
65
+ "-32300": "传输错误",
66
+ "30000": "身份验证失败",
67
+ "40000": "数据库执行错误",
68
+ "50000": "服务端执行错误",
69
+ "70000": "参数不正确"
70
+ },
71
+ "en-US": {
72
+ "10000": "business logic error",
73
+ "0": "success",
74
+ "100": "Continue",
75
+ "101": "Switching Protocols",
76
+ "200": "OK",
77
+ "201": "Created",
78
+ "202": "Accepted",
79
+ "204": "No Content",
80
+ "301": "Moved Permanently",
81
+ "302": "Found",
82
+ "303": "See Other",
83
+ "304": "Not Modified",
84
+ "307": "Temporary Redirect",
85
+ "400": "Bad Request",
86
+ "401": "Unauthorized",
87
+ "403": "Forbidden",
88
+ "404": "Not Found",
89
+ "405": "Method Not Allowed",
90
+ "408": "Request Timeout",
91
+ "413": "Payload Too Large",
92
+ "415": "Unsupported Media Type",
93
+ "429": "Too Many Requests",
94
+ "500": "Internal Server Errors",
95
+ "501": "Not Implemented",
96
+ "502": "Bad Gateway",
97
+ "503": "Service Unavailable",
98
+ "504": "Gateway Timeout",
99
+ "-32700": "not well formed",
100
+ "-32701": "unsupported encoding",
101
+ "-32702": "invalid character for encoding",
102
+ "-32600": "invalid json-rpc. not conforming to spec",
103
+ "-32601": "requested method not found",
104
+ "-32602": "invalid method parameters",
105
+ "-32603": "internal json-rpc error",
106
+ "-32500": "application error",
107
+ "-32400": "system error",
108
+ "-32300": "transport error",
109
+ "30000": "authentication failed",
110
+ "40000": "database execution error",
111
+ "50000": "server execution error",
112
+ "70000": "parameter is incorrect"
113
+ }
114
+ };
115
+ }
116
+ }
117
+
118
+ /**
119
+ * @description 根据错误码获取错误信息
120
+ * @param {string|number} code 错误码
121
+ * @returns {string|null} 错误信息
122
+ */
123
+ Errors.prototype.get = function (code) {
124
+ const lang = this._config.lang;
125
+ const lang_pack = this._langs[lang];
126
+
127
+ if (lang_pack && lang_pack[code]) {
128
+ return lang_pack[code];
129
+ }
130
+
131
+ // 如果当前语言包没有,尝试英文包
132
+ if (lang !== "en-US" && this._langs["en-US"][code]) {
133
+ return this._langs["en-US"][code];
134
+ }
135
+
136
+ return null;
137
+ };
138
+
139
+ /**
140
+ * @description 设置语言
141
+ * @param {string} lang 语言代码
142
+ */
143
+ Errors.prototype.setLang = function (lang) {
144
+ this._config.lang = lang;
145
+ };
146
+
147
+ /**
148
+ * @description 获取当前语言
149
+ * @returns {string} 当前语言代码
150
+ */
151
+ Errors.prototype.getLang = function () {
152
+ return this._config.lang;
153
+ };
154
+
155
+ /**
156
+ * @description 导出错误管理类
157
+ */
158
+ module.exports = {
159
+ Errors
160
+ }
package/lib/event.js CHANGED
@@ -11,6 +11,15 @@ class Event {
11
11
  error_handler: null
12
12
  };
13
13
 
14
+ // 日志对象
15
+ #logger = $.log || console;
16
+ // 事件配置
17
+ #event_config = {};
18
+ // 事件存储
19
+ #events = {};
20
+ // 事件计数器
21
+ #counters = {};
22
+
14
23
  /**
15
24
  * 构造函数
16
25
  * @param {object} config 配置参数
@@ -20,13 +29,13 @@ class Event {
20
29
  * 配置参数
21
30
  * @type {object} - 配置参数
22
31
  */
23
- this.event_config = {
32
+ this.#event_config = {
24
33
  ...Event.config,
25
34
  ...config || {}
26
35
  };
27
36
 
28
37
  // 事件存储结构优化
29
- this._events = {
38
+ this.#events = {
30
39
  // 主事件(没有冒号的事件)
31
40
  main: new Map(),
32
41
  // 特殊事件分类存储
@@ -42,13 +51,10 @@ class Event {
42
51
  };
43
52
 
44
53
  // 事件计数器
45
- this._counters = {
54
+ this.#counters = {
46
55
  main: 0,
47
56
  special: 0
48
57
  };
49
-
50
- // 日志对象
51
- this._logger = $.log || console;
52
58
  }
53
59
 
54
60
  /**
@@ -84,13 +90,13 @@ class Event {
84
90
 
85
91
  if (event_type === 'main') {
86
92
  return {
87
- map: this._events.main,
93
+ map: this.#events.main,
88
94
  type: 'main'
89
95
  };
90
96
  }
91
97
 
92
98
  return {
93
- map: this._events.special[event_type],
99
+ map: this.#events.special[event_type],
94
100
  type: 'special',
95
101
  special_type: event_type
96
102
  };
@@ -116,8 +122,8 @@ class Event {
116
122
  const listeners = store.map.get(event_name);
117
123
 
118
124
  // 检查监听器数量限制
119
- if (listeners.length >= this.event_config.max_listeners) {
120
- this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.event_config.max_listeners}`);
125
+ if (listeners.length >= this.#event_config.max_listeners) {
126
+ this.#logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.#event_config.max_listeners}`);
121
127
  return this;
122
128
  }
123
129
 
@@ -125,9 +131,9 @@ class Event {
125
131
 
126
132
  // 更新计数器
127
133
  if (store.type === 'special') {
128
- this._counters.special++;
134
+ this.#counters.special++;
129
135
  } else {
130
- this._counters.main++;
136
+ this.#counters.main++;
131
137
  }
132
138
 
133
139
  return this;
@@ -182,9 +188,9 @@ class Event {
182
188
 
183
189
  // 更新计数器
184
190
  if (store.type === 'special') {
185
- this._counters.special--;
191
+ this.#counters.special--;
186
192
  } else {
187
- this._counters.main--;
193
+ this.#counters.main--;
188
194
  }
189
195
  }
190
196
  } else {
@@ -194,14 +200,14 @@ class Event {
194
200
 
195
201
  // 更新计数器
196
202
  if (store.type === 'special') {
197
- this._counters.special -= count;
203
+ this.#counters.special -= count;
198
204
  } else {
199
- this._counters.main -= count;
205
+ this.#counters.main -= count;
200
206
  }
201
207
  }
202
208
 
203
209
  // 自动清理空的事件列表
204
- if (this.event_config.auto_clean && listeners && listeners.length === 0) {
210
+ if (this.#event_config.auto_clean && listeners && listeners.length === 0) {
205
211
  store.map.delete(event_name);
206
212
  }
207
213
 
@@ -409,17 +415,17 @@ class Event {
409
415
 
410
416
  // 更新计数器
411
417
  if (store.type === 'special') {
412
- this._counters.special -= count;
418
+ this.#counters.special -= count;
413
419
  } else {
414
- this._counters.main -= count;
420
+ this.#counters.main -= count;
415
421
  }
416
422
  }
417
423
  } else {
418
424
  // 移除所有事件
419
- this._events.main.clear();
420
- Object.values(this._events.special).forEach(map => map.clear());
421
- this._counters.main = 0;
422
- this._counters.special = 0;
425
+ this.#events.main.clear();
426
+ Object.values(this.#events.special).forEach(map => map.clear());
427
+ this.#counters.main = 0;
428
+ this.#counters.special = 0;
423
429
  }
424
430
 
425
431
  return this;
@@ -433,12 +439,12 @@ class Event {
433
439
  const event_names = [];
434
440
 
435
441
  // 添加 main 类型事件
436
- for (const event_name of this._events.main.keys()) {
442
+ for (const event_name of this.#events.main.keys()) {
437
443
  event_names.push(event_name);
438
444
  }
439
445
 
440
446
  // 添加特殊类型事件
441
- for (const [type, map] of Object.entries(this._events.special)) {
447
+ for (const [type, map] of Object.entries(this.#events.special)) {
442
448
  for (const event_name of map.keys()) {
443
449
  event_names.push(event_name);
444
450
  }
@@ -467,8 +473,8 @@ class Event {
467
473
  const listeners = store.map.get(event_name);
468
474
 
469
475
  // 检查监听器数量限制
470
- if (listeners.length >= this.event_config.max_listeners) {
471
- this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.event_config.max_listeners}`);
476
+ if (listeners.length >= this.#event_config.max_listeners) {
477
+ this.#logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.#event_config.max_listeners}`);
472
478
  return this;
473
479
  }
474
480
 
@@ -477,9 +483,9 @@ class Event {
477
483
 
478
484
  // 更新计数器
479
485
  if (store.type === 'special') {
480
- this._counters.special++;
486
+ this.#counters.special++;
481
487
  } else {
482
- this._counters.main++;
488
+ this.#counters.main++;
483
489
  }
484
490
 
485
491
  return this;
@@ -522,17 +528,17 @@ class Event {
522
528
  */
523
529
  getStats() {
524
530
  return {
525
- total: this._counters.main + this._counters.special,
526
- main: this._counters.main,
527
- special: this._counters.special,
531
+ total: this.#counters.main + this.#counters.special,
532
+ main: this.#counters.main,
533
+ special: this.#counters.special,
528
534
  special_types: {
529
- before: this._events.special.before.size,
530
- check: this._events.special.check.size,
531
- after: this._events.special.after.size,
532
- error: this._events.special.error.size,
533
- render: this._events.special.render.size,
534
- success: this._events.special.success.size,
535
- other: this._events.special.other.size
535
+ before: this.#events.special.before.size,
536
+ check: this.#events.special.check.size,
537
+ after: this.#events.special.after.size,
538
+ error: this.#events.special.error.size,
539
+ render: this.#events.special.render.size,
540
+ success: this.#events.special.success.size,
541
+ other: this.#events.special.other.size
536
542
  }
537
543
  };
538
544
  }
@@ -545,9 +551,9 @@ class Event {
545
551
  * @private
546
552
  */
547
553
  _handleError(error, event_name, args) {
548
- if (this.event_config.error_handler) {
554
+ if (this.#event_config.error_handler) {
549
555
  try {
550
- this.event_config.error_handler(error, event_name, args);
556
+ this.#event_config.error_handler(error, event_name, args);
551
557
  } catch (handler_error) {
552
558
  this._logger.error('错误处理函数执行失败:', handler_error);
553
559
  }
@@ -556,15 +562,31 @@ class Event {
556
562
  }
557
563
  }
558
564
 
565
+ /**
566
+ * 设置日志记录器
567
+ * @param {object} logger 日志记录器对象
568
+ */
569
+ setLogger(logger) {
570
+ this.#logger = logger;
571
+ }
572
+
573
+ /**
574
+ * 获取日志记录器
575
+ * @returns {object} 日志记录器对象
576
+ */
577
+ getLogger() {
578
+ return this.#logger;
579
+ }
580
+
559
581
  /**
560
582
  * 日志输出
561
583
  * @param {string} level 日志级别
562
584
  * @param {string} message 日志消息
563
- * @param {...any} args 日志参数
585
+ * @param {...any} params 日志参数
564
586
  */
565
- log(level, message, ...args) {
566
- this._logger[level](`[${this.constructor.name}] ${message}`, ...args);
567
- }
587
+ log(level, message, ...params) {
588
+ this.getLogger()[level](`[${this.constructor.name}] ${message}`, ...params);
589
+ };
568
590
  }
569
591
 
570
592
  module.exports = {
package/lib/eventer.js CHANGED
@@ -12,6 +12,10 @@ class Eventer {
12
12
  error_handler: null, // 全局错误处理函数
13
13
  pause_enabled: true, // 是否启用暂停功能
14
14
  };
15
+
16
+ // 日志对象
17
+ #logger = $.log || console;
18
+
15
19
  /**
16
20
  * 构造函数
17
21
  * @param {object} config 配置参数
@@ -22,7 +26,7 @@ class Eventer {
22
26
  ...config || {}
23
27
  };
24
28
  // 是否为插件
25
- this._is_plugin = false;
29
+ this.is_plugin = false;
26
30
  // 事件字典 - 支持命名空间
27
31
  this._dict = {};
28
32
  // 事件中间件
@@ -39,20 +43,34 @@ class Eventer {
39
43
  this._paused_global = false; // 全局暂停状态
40
44
  // 初始化默认命名空间
41
45
  this._namespace[this.config.default_namespace] = true;
42
- // 日志对象
43
- this._logger = $.log || console;
44
46
  }
45
- }
46
47
 
47
- /**
48
- * 日志输出
49
- * @param {string} level 日志级别
50
- * @param {string} message 日志消息
51
- * @param {...any} args 日志参数
52
- */
53
- Eventer.prototype.log = function (level, message, ...args) {
54
- this._logger[level](`[${this.constructor.name}] ${message}`, ...args);
55
- };
48
+ /**
49
+ * 设置日志记录器
50
+ * @param {object} logger 日志记录器对象
51
+ */
52
+ setLogger(logger) {
53
+ this.#logger = logger;
54
+ }
55
+
56
+ /**
57
+ * 获取日志记录器
58
+ * @returns {object} 日志记录器对象
59
+ */
60
+ getLogger() {
61
+ return this.#logger;
62
+ }
63
+
64
+ /**
65
+ * 日志输出
66
+ * @param {string} level 日志级别
67
+ * @param {string} message 日志消息
68
+ * @param {...any} params 日志参数
69
+ */
70
+ log(level, message, ...params) {
71
+ this.getLogger()[level](`[${this.constructor.name}] ${message}`, ...params);
72
+ };
73
+ }
56
74
 
57
75
  /**
58
76
  * 初始化事件管理器
@@ -180,6 +198,14 @@ Eventer.prototype._createHandlerResult = function (namespace, index, name, full_
180
198
  };
181
199
  };
182
200
 
201
+ /**
202
+ * 监听事件(支持命名空间和优先级)
203
+ * @param {string} key 事件关键字,支持namespace:key格式
204
+ * @param {object} func 触发函数
205
+ * @param {string} name 增加名称,方便删除,当出现相同名称时会被覆盖
206
+ * @param {object} options 其他选项,如throttle、debounce、timeout等
207
+ * @returns {object} 返回事件信息对象 {namespace, index, name, remove},remove为移除该事件的函数
208
+ */
183
209
  Eventer.prototype.on = function (key, func, name, options) {
184
210
  // 处理参数
185
211
  const params = this._processOnParams(key, func, name, options);