mm_machine 3.2.5 → 3.2.6

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
@@ -1,56 +1,7 @@
1
- const { Mod } = require('./mod');
2
- const { Drive } = require('./drive.js');
3
- const { Scanner } = require('./scanner.js');
4
- const { Registry } = require('./registry.js');
5
- const { Manager } = require('./manager.js');
6
-
7
- /**
8
- * 索引类 - 唯一入口
9
- * 作为模块系统的顶层入口,内部持有 Registry(注册表)和 Manager(运行时管理器)
10
- */
11
- class Index {
12
- /**
13
- * 构造函数
14
- * @param {object} config 配置对象
15
- * @param {object} parent 父对象
16
- */
17
- constructor(config, parent) {
18
- this.config = {
19
- name: 'default',
20
- ...config
21
- };
22
-
23
- if (parent) {
24
- this.getParent = function () {
25
- return parent;
26
- };
27
- }
28
-
29
- // 注册表:管理模块配置持久化
30
- if (!$.registry) {
31
- $.registry = new Registry({
32
- name: this.constructor.name + '_' + this.config.name
33
- });
34
- }
35
-
36
- // 运行时管理器:管理模块生命周期和方法派发
37
- this._manager = new Manager({}, this);
38
- }
39
- }
40
-
41
- /**
42
- * 加载
43
- * 读取注册数据并初始化
44
- */
45
- Index.prototype.load = function () {
46
- $.registry.load();
47
- };
1
+ const { Mod } = require('./lib/mod.js');
2
+ const { Drive } = require('./lib/drive.js');
3
+ const { Manager } = require('./lib/manager.js');
48
4
 
49
5
  module.exports = {
50
- Index,
51
- Mod,
52
- Drive,
53
- Scanner,
54
- Registry,
55
- Manager
6
+ Mod, Drive, Manager
56
7
  };
@@ -1,7 +1,6 @@
1
- const { Mod } = require('./mod');
1
+ const { Mod } = require('./mod.js');
2
2
  const { Drive } = require('./drive.js');
3
3
  const { Scanner } = require('./scanner.js');
4
- const { Registry } = require('./registry.js');
5
4
 
6
5
  /**
7
6
  * 管理器类
@@ -72,12 +71,6 @@ class Manager extends Mod {
72
71
  * @type {number}
73
72
  */
74
73
  mode: 3,
75
- /**
76
- * 是否启用注册表集成
77
- * true 时自动从 $.registry 读取/写入模块配置
78
- * @type {boolean}
79
- */
80
- use_registry: true,
81
74
  /**
82
75
  * 作用域
83
76
  * @type {string}
@@ -187,18 +180,13 @@ Manager.prototype.Drive = Drive;
187
180
  * 更新模块信息
188
181
  */
189
182
  Manager.prototype.updateInfo = function () {
190
- // 由 registry 管理模块信息,无需额外操作
191
183
  };
192
184
 
193
185
  /**
194
186
  * 获取模块配置列表
195
- * 启用注册表时从 registry 获取;否则从内部缓存 infos 读取
196
187
  * @returns {Array} 排序后的模块配置数组
197
188
  */
198
189
  Manager.prototype._getInfos = function () {
199
- if (this.config.use_registry) {
200
- return $.registry.getMods(this.getParentKey());
201
- }
202
190
  return this.infos || [];
203
191
  };
204
192
 
@@ -210,8 +198,7 @@ Manager.prototype.clearMods = function () {
210
198
  for (let name in this.mods) {
211
199
  delete this.mods[name];
212
200
  }
213
- // 同步清理内部缓存
214
- if (!this.config.use_registry && this.infos) {
201
+ if (this.infos) {
215
202
  this.infos = [];
216
203
  }
217
204
  };
@@ -225,8 +212,7 @@ Manager.prototype.delMod = function (name) {
225
212
  let mod = this.mods[name];
226
213
  if (mod) {
227
214
  delete this.mods[name];
228
- // 同步清理内部缓存
229
- if (!this.config.use_registry && this.infos) {
215
+ if (this.infos) {
230
216
  this.infos = this.infos.filter((o) => { return o.name !== name; });
231
217
  }
232
218
  }
@@ -244,30 +230,11 @@ Manager.prototype.setMod = function (name, mod) {
244
230
 
245
231
  /**
246
232
  * 获取模块
247
- * 内存不存在时,若启用注册表则从注册表配置按需实例化
248
233
  * @param {string} name 模块名
249
234
  * @returns {object|null} 模块对象
250
235
  */
251
236
  Manager.prototype.getMod = function (name) {
252
- var mod = this.mods[name];
253
- if (mod) return mod;
254
-
255
- // 注册表回退:通过 _getInfos 查找配置并按需实例化
256
- if (this.config.use_registry) {
257
- var infos = this._getInfos();
258
- for (var i = 0; i < infos.length; i++) {
259
- if (infos[i].name === name) {
260
- mod = this.newMod(infos[i].config_file);
261
- // 保存到内存集合,避免后续访问时再次创建
262
- if (mod) {
263
- this.setMod(mod.config.name, mod);
264
- }
265
- return mod;
266
- }
267
- }
268
- }
269
-
270
- return null;
237
+ return this.mods[name] || null;
271
238
  };
272
239
 
273
240
  /**
@@ -299,7 +266,6 @@ Manager.prototype.newMod = function (config_file, config, script) {
299
266
  mod.mode = this.config.mode;
300
267
 
301
268
  if (config && config.name) {
302
- // config 已从 registry 缓存获取,跳过磁盘读
303
269
  mod.config_file = config_file;
304
270
  } else {
305
271
  mod.loadConfig(config_file);
@@ -357,20 +323,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
357
323
  entry[key] = mod.config[key];
358
324
  }
359
325
  }
360
- // 启用注册表时,自动写入 $.registry 供后续实例复用
361
- if (this.config.use_registry) {
362
- // 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
363
- if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
364
- try {
365
- // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
366
- $.registry.setMod(this.getParentKey(), mod.config.name, entry);
367
- } catch (err) {
368
- throw new Error('注册模块失败: ' + config_file + ' - ' + err.message);
369
- }
370
- }
371
- } else {
372
- this.infos.push(entry);
373
- }
326
+ this.infos.push(entry);
374
327
 
375
328
  return mod;
376
329
  };
@@ -524,28 +477,6 @@ Manager.prototype.update = async function (dir, clear = true) {
524
477
  }
525
478
  };
526
479
 
527
- /**
528
- * 从 registry 缓存加载模块(按 parent 字段匹配)
529
- * 从 registry.mod_dict 中按 parent 过滤并创建模块实例
530
- * @returns {boolean} 是否加载到模块
531
- */
532
- Manager.prototype._loadFromRegistry = function () {
533
- let lt = this._getInfos();
534
- if (!lt.length) return false;
535
-
536
- for (var i = 0; i < lt.length; i++) {
537
- var entry = lt[i];
538
- if (!this.config.lazy_load) {
539
- let mod = this.newMod(entry.config_file);
540
- if (mod) {
541
- this.setMod(mod.config.name, mod);
542
- }
543
- }
544
- }
545
-
546
- return true;
547
- };
548
-
549
480
  /**
550
481
  * 检索目录下所有文件
551
482
  * @private
@@ -677,26 +608,13 @@ Manager.prototype.getDir = function () {
677
608
 
678
609
  /**
679
610
  * 加载资源
680
- * 启用注册表时优先从注册表读取,快速启动;
681
- * 同时在后台异步扫描文件系统,确保注册表数据为物理最新。
682
611
  */
683
612
  Manager.prototype._loadSources = async function () {
684
- // 启用注册表且有缓存数据时,走内存快路径,快速启动;
685
- // 后台异步扫描文件系统更新注册表,确保物理最新。
686
- if (this.config.use_registry) {
687
- if (this._loadFromRegistry()) {
688
- // setImmediate(() => this._scanForUpdate(false));
689
- return;
690
- }
691
- }
692
-
693
- // 注册表无缓存,同步扫描文件系统
694
613
  await this._scanForUpdate();
695
614
  };
696
615
 
697
616
  /**
698
- * 后台扫描文件系统并同步到注册表
699
- * 不阻塞启动,让注册表始终与磁盘保持同步
617
+ * 后台扫描文件系统
700
618
  */
701
619
  Manager.prototype._scanForUpdate = async function (clear = true) {
702
620
  var base_dir = this.getBaseDir();
@@ -707,9 +625,6 @@ Manager.prototype._scanForUpdate = async function (clear = true) {
707
625
  } else {
708
626
  await this.call('update', dir, clear);
709
627
  }
710
- if (this.config.use_registry) {
711
- $.registry.save();
712
- }
713
628
  } catch (err) {
714
629
  this.log('error', '后台扫描同步失败', err);
715
630
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.5",
3
+ "version": "3.2.6",
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": {
@@ -27,12 +27,8 @@
27
27
  "node": ">=12.0.0"
28
28
  },
29
29
  "files": [
30
- "drive.js",
31
30
  "index.js",
32
- "manager.js",
33
- "mod.js",
34
- "registry.js",
35
- "scanner.js",
31
+ "lib/*",
36
32
  "README.md"
37
33
  ],
38
34
  "dependencies": {
package/registry.js DELETED
@@ -1,273 +0,0 @@
1
- require('mm_expand');
2
-
3
- /**
4
- * 注册类 - 注册表模式
5
- * 管理模块配置的持久化、模块配置的 CRUD
6
- */
7
- class Registry {
8
- /**
9
- * 构造函数
10
- * @param {object} config 配置项
11
- */
12
- constructor(config) {
13
- this.config = {
14
- name: 'default',
15
- dir: '/data/registry'.fullname(),
16
- ...config
17
- };
18
- this.mod_dict = {};
19
- this.config.dir.addDir();
20
- }
21
- }
22
-
23
- // ==== 配置项 CRUD ====
24
-
25
- /**
26
- * 创建新模块配置项
27
- * @returns {object} 模块配置项
28
- */
29
- Registry.prototype.newModel = function () {
30
- return {
31
- name: '',
32
- title: '',
33
- description: '',
34
- version: '1.0.0',
35
- dependencies: {},
36
- sort: 100,
37
- state: 1,
38
- auto_start: 'auto',
39
- config_file: '',
40
- main: ''
41
- };
42
- };
43
-
44
- /**
45
- * 设置模块
46
- * @param {string} parent 父级名称
47
- * @param {string} name 模块名称
48
- * @param {object} config 模块配置项
49
- * @returns {object} 模块配置项
50
- */
51
- Registry.prototype.setMod = function (parent, name, config) {
52
- if (!parent) {
53
- throw new TypeError('父级名称(parent)不能为空');
54
- }
55
- else if (typeof parent !== 'string') {
56
- throw new TypeError('父级名称(parent)必须为字符串');
57
- }
58
- if (!name) {
59
- throw new TypeError('模块名称(name)不能为空');
60
- }
61
- else if (typeof name !== 'string') {
62
- throw new TypeError('模块名称(name)必须为字符串');
63
- }
64
- if (!config) {
65
- throw new TypeError('模块配置项不能为空');
66
- }
67
- else if (typeof config !== 'object') {
68
- throw new TypeError('模块配置项必须为对象');
69
- }
70
- this.checkConfig(config);
71
- let cg = this.fullConfig(config);
72
-
73
- // 按 parent 分组存储到 mod_dict
74
- if (!this.mod_dict[parent]) {
75
- this.mod_dict[parent] = [];
76
- }
77
- var lt = this.mod_dict[parent];
78
-
79
- let has = false;
80
- for (let i = 0; i < lt.length; i++) {
81
- let o = lt[i];
82
- if (o.name === name) {
83
- lt[i] = cg;
84
- has = true;
85
- break;
86
- }
87
- }
88
- if (!has) {
89
- lt.push(cg);
90
- }
91
- return cg;
92
- };
93
-
94
- /**
95
- * 获取模块
96
- * @param {string} parent 父级名称
97
- * @param {string} name 模块名称
98
- * @returns {object} 模块配置项
99
- */
100
- Registry.prototype.getMod = function (parent, name) {
101
- var lt = this.mod_dict[parent];
102
- if (lt) {
103
- for (let i = 0; i < lt.length; i++) {
104
- if (lt[i].name === name) {
105
- return lt[i];
106
- }
107
- }
108
- }
109
- return null;
110
- };
111
-
112
- /**
113
- * 删除模块
114
- * @param {string} parent 父级名称
115
- * @param {string} name 模块名称
116
- * @returns {object} 模块配置项
117
- */
118
- Registry.prototype.delMod = function (parent, name) {
119
- var lt = this.mod_dict[parent];
120
- if (!lt) return null;
121
-
122
- var obj = null;
123
- for (let i = 0; i < lt.length; i++) {
124
- if (lt[i].name === name) {
125
- obj = lt[i];
126
- lt.splice(i, 1);
127
- break;
128
- }
129
- }
130
- if (lt.length === 0) {
131
- delete this.mod_dict[parent];
132
- }
133
- return obj;
134
- };
135
-
136
- /**
137
- * 获取模块列表
138
- * @param {string} parent 父级名称
139
- * @returns {Array} 模块配置项数组
140
- */
141
- Registry.prototype.getMods = function (parent) {
142
- return this.mod_dict[parent] || [];
143
- };
144
-
145
- /**
146
- * 检查配置项
147
- * @param {object} config 模块配置项
148
- * @throws {Error} 模块配置项错误
149
- */
150
- Registry.prototype.checkConfig = function (config) {
151
- let {
152
- name,
153
- title,
154
- auto_start,
155
- config_file
156
- } = config;
157
- if (!name) {
158
- throw new TypeError('模块名称不能为空');
159
- }
160
- if (!title) {
161
- throw new TypeError('模块标题不能为空');
162
- }
163
- if (!config_file) {
164
- throw new TypeError('模块配置文件不能为空');
165
- }
166
- var valid_auto = ['true', 'false', 'auto'];
167
- if (valid_auto.indexOf(auto_start) === -1) {
168
- throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
169
- }
170
- };
171
-
172
- /**
173
- * 补全配置项
174
- * @param {object} config 模块配置项
175
- * @returns {object} 模块配置项
176
- */
177
- Registry.prototype.fullConfig = function (config) {
178
- let cg = { ...this.newModel(), ...config };
179
- let {
180
- description,
181
- version,
182
- state,
183
- main,
184
- dependencies
185
- } = cg;
186
- if (!description) {
187
- cg.description = '';
188
- }
189
- if (!version) {
190
- cg.version = '1.0.0';
191
- }
192
- if (!main) {
193
- cg.main = '';
194
- }
195
- if (state) {
196
- cg.state = 1;
197
- }
198
- else {
199
- cg.state = 0;
200
- }
201
- if (!dependencies) {
202
- cg.dependencies = {};
203
- }
204
- return cg;
205
- };
206
-
207
- /**
208
- * 获取注册表目录
209
- * @returns {string} 注册表目录
210
- */
211
- Registry.prototype.getDir = function () {
212
- return this.config.dir.fullname();
213
- };
214
-
215
- /**
216
- * 获取模块文件
217
- * @returns {string} 模块文件路径
218
- */
219
- Registry.prototype.getModFile = function () {
220
- let file_name = this.config.name + '_module.json';
221
- let file = file_name.fullname(this.getDir());
222
- return file;
223
- };
224
-
225
- /**
226
- * 加载注册数据
227
- * 从 JSON 文件读取持久化数据
228
- */
229
- Registry.prototype.load = function () {
230
- this.read();
231
- };
232
-
233
- /**
234
- * 读取模块
235
- * @returns {Array} 模块配置项数组
236
- */
237
- Registry.prototype.read = function () {
238
- this.mod_dict = this.getModFile().loadJson() || {};
239
- };
240
-
241
- /**
242
- * 保存模块
243
- * @param {object} config 模块配置项
244
- * @returns {object} 模块配置项
245
- */
246
- Registry.prototype.save = function () {
247
- try {
248
- this.getModFile().saveJson(this.mod_dict);
249
- } catch (err) {
250
- console.error('保存注册数据失败: ' + err.message);
251
- }
252
- };
253
-
254
- /**
255
- * 排序模块
256
- */
257
- Registry.prototype.sort = function () {
258
- for (var parent in this.mod_dict) {
259
- this.mod_dict[parent].sort((a, b) => {
260
- if (a.sort !== b.sort) {
261
- return a.sort - b.sort;
262
- }
263
- if (a.name !== b.name) {
264
- return a.name.localeCompare(b.name);
265
- }
266
- return 0;
267
- });
268
- }
269
- };
270
-
271
- module.exports = {
272
- Registry
273
- };
File without changes
File without changes
File without changes