mm_machine 3.1.7 → 3.1.9

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/drive.js CHANGED
@@ -94,7 +94,7 @@ Drive.prototype._loadConfig = function (file) {
94
94
  this.setConfig(config);
95
95
  return config;
96
96
  } catch (err) {
97
- this.log('error', '加载配置失败', err);
97
+ this.log('error', '加载配置失败', this._wrapError(err));
98
98
  return null;
99
99
  }
100
100
  };
@@ -333,7 +333,7 @@ Drive.prototype.loadScript = function () {
333
333
  return cs;
334
334
  }
335
335
  } catch (err) {
336
- this.log('error', '加载脚本文件失败', err);
336
+ this.log('error', '加载脚本文件失败', this._wrapError(err));
337
337
  return null;
338
338
  }
339
339
  };
@@ -668,7 +668,7 @@ Drive.prototype.loadBefore = function () {
668
668
  this._is_loaded = true;
669
669
  }
670
670
  } catch (err) {
671
- this.log('error', `加载前处理失败`, err);
671
+ this.log('error', `加载前处理失败`, this._wrapError(err));
672
672
  this._is_loaded = false;
673
673
  }
674
674
  };
package/manager.js CHANGED
@@ -72,11 +72,6 @@ class Manager extends Mod {
72
72
  * @type {number}
73
73
  */
74
74
  mode: 3,
75
- /**
76
- * 排序项
77
- * @type {string}
78
- */
79
- sort_key: 'sort',
80
75
  /**
81
76
  * 是否启用注册表集成
82
77
  * true 时自动从 $.registry 读取/写入模块配置
@@ -350,40 +345,37 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
350
345
  if (!mod) return null;
351
346
  this.setMod(mod.config.name, mod);
352
347
 
348
+ var entry = {
349
+ name: mod.config.name,
350
+ title: mod.config.title,
351
+ description: mod.config.description,
352
+ sort: mod.config.sort,
353
+ state: mod.config.state,
354
+ config_file: config_file,
355
+ auto_start: mod.config.auto_start || 'auto',
356
+ main: mod.config.main || ''
357
+ };
358
+ // 获取触发键
359
+ let trigger = this.config.trigger;
360
+ if (trigger && trigger.length > 0) {
361
+ for (let t of trigger) {
362
+ entry.trigger[t] = mod.config[t];
363
+ }
364
+ }
353
365
  // 启用注册表时,自动写入 $.registry 供后续实例复用
354
366
  if (this.config.use_registry) {
355
367
  // 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
356
368
  if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
357
- var entry = {
358
- name: mod.config.name,
359
- title: mod.config.title,
360
- description: mod.config.description,
361
- sort: mod.config.sort,
362
- state: mod.config.state,
363
- config_file: config_file,
364
- main: mod.config.main || ''
365
- };
366
- // 获取触发键
367
- let trigger = this.config.trigger;
368
- if (trigger && trigger.length > 0) {
369
- for (let t of trigger) {
370
- entry.trigger[t] = mod.config[t];
371
- }
369
+ try {
370
+ // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
371
+ $.registry.setMod(this.getParentKey(), mod.config.name, entry);
372
+ } catch (err) {
373
+ throw new Error(
374
+ '注册模块失败: ' + config_file + ' - ' + err.message
375
+ );
372
376
  }
373
- // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
374
- $.registry.setMod(this.getParentKey(), mod.config.name, entry);
375
377
  }
376
378
  } else {
377
- // 不使用注册表时,写入内部缓存
378
- var entry = {
379
- name: mod.config.name,
380
- title: mod.config.title,
381
- description: mod.config.description,
382
- sort: mod.config.sort,
383
- state: mod.config.state,
384
- config_file: config_file,
385
- main: mod.config.main || ''
386
- };
387
379
  this.infos.push(entry);
388
380
  }
389
381
 
@@ -453,7 +445,11 @@ Manager.prototype.unregMod = async function (name) {
453
445
  */
454
446
  Manager.prototype.regMods = function (files, config = {}, script = {}) {
455
447
  for (let file of files) {
456
- this.regMod(file, config, script);
448
+ try {
449
+ this.regMod(file, config, script);
450
+ } catch (err) {
451
+ this.log('error', '批量注册模块失败: ' + file, err);
452
+ }
457
453
  }
458
454
  };
459
455
 
package/mod.js CHANGED
@@ -113,6 +113,39 @@ Mod.prototype.log = function (level, message, ...params) {
113
113
  this.getLogger()[level](`[${this.constructor.name}.${this.config.name}] ${message}`, ...params);
114
114
  };
115
115
 
116
+ /**
117
+ * 包装错误,附加模块文件路径上下文,用于溯源
118
+ * @param {Error} err 原始错误对象
119
+ * @param {object} context 上下文信息
120
+ * @param {string} context.method 方法名
121
+ * @returns {Error} 包装后的错误
122
+ */
123
+ Mod.prototype._wrapError = function (err, context = {}) {
124
+ // 已包装过的不再重复包装
125
+ if (err.__wrapped) return err;
126
+
127
+ var parts = [];
128
+ // 附加配置文件路径
129
+ if (this.config_file) {
130
+ parts.push('cfg:' + this.config_file);
131
+ }
132
+ // 附加脚本文件路径
133
+ if (this.script_file) {
134
+ parts.push('script:' + this.script_file);
135
+ }
136
+ // 附加方法名
137
+ if (context.method) {
138
+ parts.push(context.method);
139
+ }
140
+ var prefix = '[' + this.constructor.name + '.' + this.config.name + ']';
141
+ var suffix = parts.length > 0 ? ' (' + parts.join(', ') + ')' : '';
142
+ var msg = prefix + suffix + ' - ' + err.message;
143
+ var wrapped = new Error(msg);
144
+ wrapped.stack = wrapped.stack + '\n' + err.stack;
145
+ wrapped.__wrapped = true;
146
+ return wrapped;
147
+ };
148
+
116
149
  /**
117
150
  * 监听事件
118
151
  */
@@ -649,7 +682,7 @@ Mod.prototype.call = async function (method, ...params) {
649
682
  result = await this._callMain(method_func, params, before_result);
650
683
  result = await this._callAfter(method, params, result);
651
684
  } catch (err) {
652
- this.log('error', `执行 ${method} 失败: ${err.message}`, err);
685
+ this.log('error', `执行 ${method} 失败: ${err.message}`, this._wrapError(err, { method: method }));
653
686
  return null;
654
687
  }
655
688
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.1.7",
3
+ "version": "3.1.9",
4
4
  "description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/registry.js CHANGED
@@ -68,8 +68,8 @@ Registry.prototype.setMod = function (parent, name, config) {
68
68
  else if (typeof config !== 'object') {
69
69
  throw new TypeError('模块配置项必须为对象');
70
70
  }
71
- let cg = this.checkConfig(config);
72
- cg.parent = parent;
71
+ this.checkConfig(config);
72
+ let cg = this._fullValue(config);
73
73
 
74
74
  // 按 parent 分组存储到 mod_dict
75
75
  if (!this.mod_dict[parent]) {
@@ -146,22 +146,15 @@ Registry.prototype.getMods = function (parent) {
146
146
  /**
147
147
  * 检查配置项
148
148
  * @param {object} config 模块配置项
149
- * @returns {object} 模块配置项
150
149
  * @throws {Error} 模块配置项错误
151
150
  */
152
151
  Registry.prototype.checkConfig = function (config) {
153
- let cg = { ...this.newModel(), ...config };
154
152
  let {
155
153
  name,
156
154
  title,
157
- description,
158
- version,
159
- state,
160
155
  auto_start,
161
- config_file,
162
- main,
163
- dependencies
164
- } = cg;
156
+ config_file
157
+ } = config;
165
158
  if (!name) {
166
159
  throw new TypeError('模块名称不能为空');
167
160
  }
@@ -172,11 +165,27 @@ Registry.prototype.checkConfig = function (config) {
172
165
  throw new TypeError('模块配置文件不能为空');
173
166
  }
174
167
  var valid_auto = ['true', 'false', 'auto'];
175
- if (valid_auto.indexOf(auto_start) === -1 && ![true, false].includes(auto_start)) {
168
+ if (valid_auto.indexOf(auto_start) === -1) {
176
169
  throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
177
170
  }
171
+ };
172
+
173
+ /**
174
+ * 补全配置项
175
+ * @param {object} config 模块配置项
176
+ * @returns {object} 模块配置项
177
+ */
178
+ Registry.prototype._fullValue = function (config) {
179
+ let cg = { ...this.newModel(), ...config };
180
+ let {
181
+ description,
182
+ version,
183
+ state,
184
+ main,
185
+ dependencies
186
+ } = cg;
178
187
  if (!description) {
179
- description = '';
188
+ cg.description = '';
180
189
  }
181
190
  if (!version) {
182
191
  cg.version = '1.0.0';