mm_machine 3.0.3 → 3.0.5

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
@@ -84,14 +84,19 @@ Drive.prototype._loadConfig = function (file) {
84
84
  if (file) {
85
85
  this.config_file = file;
86
86
  }
87
- // 加载配置
88
- let config = $.loadJson(this.config_file);
89
- if (!config) {
90
- this.newConfig(this.config_file);
91
- config = $.loadJson(this.config_file);
87
+ try {
88
+ // 加载配置
89
+ let config = $.loadJson(this.config_file);
90
+ if (!config) {
91
+ this.newConfig(this.config_file);
92
+ config = $.loadJson(this.config_file);
93
+ }
94
+ this.setConfig(config);
95
+ return config;
96
+ } catch (err) {
97
+ this.log('error', '加载配置失败', err);
98
+ return null;
92
99
  }
93
- this.setConfig(config);
94
- return config;
95
100
  };
96
101
 
97
102
  /**
@@ -315,16 +320,21 @@ Drive.prototype._getScriptTpl = function () {
315
320
  Drive.prototype.loadScript = function () {
316
321
  let file = this._getScriptFile();
317
322
  if (!file) return null;
318
- if (this.mode >= 3) {
319
- var cs = $.require(file, (mod) => {
320
- this._setMainMethod(mod);
321
- });
322
- this._setMainMethod(cs);
323
- return cs;
324
- } else {
325
- var cs = require(file);
326
- this._setMainMethod(cs);
327
- return cs;
323
+ try {
324
+ if (this.mode >= 3) {
325
+ var cs = $.require(file, (mod) => {
326
+ this._setMainMethod(mod);
327
+ });
328
+ this._setMainMethod(cs);
329
+ return cs;
330
+ } else {
331
+ var cs = require(file);
332
+ this._setMainMethod(cs);
333
+ return cs;
334
+ }
335
+ } catch (err) {
336
+ this.log('error', '加载脚本文件失败', err);
337
+ return null;
328
338
  }
329
339
  };
330
340
 
package/manager.js CHANGED
@@ -314,38 +314,43 @@ Manager.prototype.getMods = function () {
314
314
  * @returns {object} 模块对象
315
315
  */
316
316
  Manager.prototype.newMod = function (config_file, config, script) {
317
- var mod;
318
- switch (this.config.mod_type) {
319
- case 'script':
320
- mod = new Mod(config, this);
321
- break;
322
- default:
323
- mod = new this.Drive(config, this);
324
- break;
325
- }
326
- mod.mode = this.config.mode;
317
+ try {
318
+ var mod;
319
+ switch (this.config.mod_type) {
320
+ case 'script':
321
+ mod = new Mod(config, this);
322
+ break;
323
+ default:
324
+ mod = new this.Drive(config, this);
325
+ break;
326
+ }
327
+ mod.mode = this.config.mode;
327
328
 
328
- if (config && config.name) {
329
- // config 已从 registry 缓存获取,跳过磁盘读
330
- mod.config_file = config_file;
331
- if (mod._preset) {
332
- mod._preset();
329
+ if (config && config.name) {
330
+ // config 已从 registry 缓存获取,跳过磁盘读
331
+ mod.config_file = config_file;
332
+ if (mod._preset) {
333
+ mod._preset();
334
+ }
335
+ } else {
336
+ mod.loadConfig(config_file);
333
337
  }
334
- } else {
335
- mod.loadConfig(config_file);
336
- }
337
338
 
338
- if (script) {
339
- for (var key in script) {
340
- var obj = script[key];
341
- if (typeof obj == 'function') {
342
- mod.setMethod(key, obj);
343
- } else {
344
- mod.setData(key, obj);
339
+ if (script) {
340
+ for (var key in script) {
341
+ var obj = script[key];
342
+ if (typeof obj == 'function') {
343
+ mod.setMethod(key, obj);
344
+ } else {
345
+ mod.setData(key, obj);
346
+ }
345
347
  }
346
348
  }
349
+ return mod;
350
+ } catch (err) {
351
+ this.log('error', '创建模块失败: 配置文件 ' + config_file, err);
352
+ return null;
347
353
  }
348
- return mod;
349
354
  };
350
355
 
351
356
  /**
@@ -517,20 +522,24 @@ Manager.prototype.updateAfter = function () {};
517
522
  * @param {boolean} clear 是否清除缓存
518
523
  */
519
524
  Manager.prototype.update = async function (dir, clear = true) {
520
- if (clear) {
521
- this.clearMods();
522
- this.infos = [];
523
- }
525
+ try {
526
+ if (clear) {
527
+ this.clearMods();
528
+ this.infos = [];
529
+ }
524
530
 
525
- var search_dir = dir || this.config.dir;
526
- var files = await this._findFiles(search_dir);
527
- this.registerMods(files);
528
- this.updateInfo();
531
+ var search_dir = dir || this.config.dir;
532
+ var files = await this._findFiles(search_dir);
533
+ this.registerMods(files);
534
+ this.updateInfo();
529
535
 
530
- if (!this.config.lazy_load) {
531
- await this.loadScripts();
536
+ if (!this.config.lazy_load) {
537
+ await this.loadScripts();
538
+ }
539
+ this._is_loaded = true;
540
+ } catch (err) {
541
+ this.log('error', '更新配置失败', err);
532
542
  }
533
- this._is_loaded = true;
534
543
  };
535
544
 
536
545
  /**
@@ -743,12 +752,14 @@ Manager.prototype._loadSources = async function () {
743
752
  $.registry.module_list &&
744
753
  $.registry.module_list.length > 0
745
754
  ) {
746
- this._loadFromRegistry(this.config.name);
747
- this.updateInfo();
748
- return;
755
+ // 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
756
+ if (this._loadFromRegistry(this.config.name)) {
757
+ this.updateInfo();
758
+ return;
759
+ }
749
760
  }
750
761
 
751
- // 无注册表缓存,走文件系统扫描
762
+ // 无注册表缓存或注册表无匹配模块,走文件系统扫描
752
763
  if (base_dir) {
753
764
  // base_dir 和 dir 并行扫描,互不依赖
754
765
  await Promise.all([this.call('update', base_dir, true), this.call('update', dir, false)]);
@@ -768,12 +779,16 @@ Manager.prototype._initSources = async function () {
768
779
  * 初始化公共属性
769
780
  */
770
781
  Manager.prototype._initCore = async function () {
771
- // 初始化管理器
772
- this._initManager();
773
- // 加载资源
774
- await this._loadSources();
775
- // 初始化AI客户端
776
- await this._initSources();
782
+ try {
783
+ // 初始化管理器
784
+ this._initManager();
785
+ // 加载资源
786
+ await this._loadSources();
787
+ // 初始化AI客户端
788
+ await this._initSources();
789
+ } catch (err) {
790
+ this.log('error', '管理器初始化失败', err);
791
+ }
777
792
  };
778
793
 
779
794
  /**
package/mod.js CHANGED
@@ -366,16 +366,24 @@ Mod.prototype._unloadCore = async function () {
366
366
  * 重载
367
367
  */
368
368
  Mod.prototype.reload = async function () {
369
- await this.do('unload');
370
- await this.do('load');
369
+ try {
370
+ await this.do('unload');
371
+ await this.do('load');
372
+ } catch (err) {
373
+ this.log('error', '重载失败', err);
374
+ }
371
375
  };
372
376
 
373
377
  /**
374
378
  * 重启
375
379
  */
376
380
  Mod.prototype.restart = async function () {
377
- await this.do('stop');
378
- await this.do('start');
381
+ try {
382
+ await this.do('stop');
383
+ await this.do('start');
384
+ } catch (err) {
385
+ this.log('error', '重启失败', err);
386
+ }
379
387
  };
380
388
 
381
389
  /**
@@ -390,9 +398,13 @@ Mod.prototype.isRunning = function () {
390
398
  * 结束
391
399
  */
392
400
  Mod.prototype.end = async function () {
393
- await this.do('stop');
394
- await this.do('unload');
395
- await this.do('destroy');
401
+ try {
402
+ await this.do('stop');
403
+ await this.do('unload');
404
+ await this.do('destroy');
405
+ } catch (err) {
406
+ this.log('error', '结束失败', err);
407
+ }
396
408
  };
397
409
 
398
410
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
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
@@ -133,9 +133,15 @@ Registry.prototype.getModule = function (name) {
133
133
  var class_name = entry.config.class_name || 'Drive';
134
134
  const CS = this._class_cache[class_name];
135
135
  if (!CS) {
136
- throw new Error('模块类 ' + class_name + ' 未注册');
136
+ console.error('模块类 ' + class_name + ' 未注册');
137
+ return null;
138
+ }
139
+ try {
140
+ entry.instance = new CS(entry.config);
141
+ } catch (err) {
142
+ console.error('实例化模块 ' + name + ' 失败: ' + err.message);
143
+ return null;
137
144
  }
138
- entry.instance = new CS(entry.config);
139
145
  }
140
146
  return entry.instance;
141
147
  };
@@ -182,9 +188,13 @@ Registry.prototype.load = function () {
182
188
  // 注册模块类(幂等-safe,Node require 返回同一引用)
183
189
  for (var key in this.class_dict) {
184
190
  var file = this.class_dict[key];
185
- var exports = require(file);
186
- var class_ref = exports[key] || exports;
187
- this.registerClass(key, class_ref);
191
+ try {
192
+ var exports = require(file);
193
+ var class_ref = exports[key] || exports;
194
+ this.registerClass(key, class_ref);
195
+ } catch (err) {
196
+ console.error('注册模块类 ' + key + ' 失败: ' + err.message);
197
+ }
188
198
  }
189
199
 
190
200
  // 注册模块配置(仅新增不覆盖,保留已有实例)
@@ -388,8 +398,12 @@ Registry.prototype.read = function () {
388
398
  * @returns {object} 模块配置项
389
399
  */
390
400
  Registry.prototype.save = function () {
391
- this.getClassFile().saveJson(this.class_dict);
392
- this.getModuleFile().saveJson(this.module_list);
401
+ try {
402
+ this.getClassFile().saveJson(this.class_dict);
403
+ this.getModuleFile().saveJson(this.module_list);
404
+ } catch (err) {
405
+ console.error('保存注册数据失败: ' + err.message);
406
+ }
393
407
  };
394
408
 
395
409
  /**