mm_machine 2.9.8 → 2.9.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.
Files changed (3) hide show
  1. package/index.js +8 -9
  2. package/package.json +1 -1
  3. package/registry.js +412 -0
package/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  const { Mod } = require('./mod');
2
2
  const { Drive } = require('./drive.js');
3
3
  const { Scanner } = require('./scanner.js');
4
- const { Register } = require('./register.js');
4
+ const { Registry } = require('./registry.js');
5
5
  const { Manager } = require('./manager.js');
6
6
 
7
7
  /**
8
8
  * 索引类 - 唯一入口
9
- * 作为模块系统的顶层入口,内部持有 Register(注册表)和 Manager(运行时管理器)
9
+ * 作为模块系统的顶层入口,内部持有 Registry(注册表)和 Manager(运行时管理器)
10
10
  */
11
11
  class Index {
12
12
  /**
@@ -29,12 +29,11 @@ class Index {
29
29
  }
30
30
 
31
31
  // 注册表:管理类注册、模块配置持久化、模块实例懒加载
32
- if (!$.register) {
33
- $.register = new Register({
32
+ if (!$.registry) {
33
+ $.registry = new Registry({
34
34
  name: this.constructor.name + '_' + this.config.name
35
35
  });
36
36
  }
37
- this.register = $.register;
38
37
 
39
38
  // 运行时管理器:管理模块生命周期和方法派发
40
39
  this._manager = new Manager({}, this);
@@ -46,7 +45,7 @@ class Index {
46
45
  * 读取注册数据并初始化
47
46
  */
48
47
  Index.prototype.load = function () {
49
- this.register.load();
48
+ $.registry.load();
50
49
  };
51
50
 
52
51
  /**
@@ -55,7 +54,7 @@ Index.prototype.load = function () {
55
54
  * @returns {object|null} 模块实例
56
55
  */
57
56
  Index.prototype.getModule = function (name) {
58
- return this.register.getModule(name);
57
+ return $.registry.getModule(name);
59
58
  };
60
59
 
61
60
  /**
@@ -64,7 +63,7 @@ Index.prototype.getModule = function (name) {
64
63
  * @returns {Array} 模块实例数组
65
64
  */
66
65
  Index.prototype.getModules = function (class_name) {
67
- return this.register.getModules(class_name);
66
+ return $.registry.getModules(class_name);
68
67
  };
69
68
 
70
69
  module.exports = {
@@ -72,6 +71,6 @@ module.exports = {
72
71
  Mod,
73
72
  Drive,
74
73
  Scanner,
75
- Register,
74
+ Registry,
76
75
  Manager
77
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.9.8",
3
+ "version": "2.9.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 ADDED
@@ -0,0 +1,412 @@
1
+ /**
2
+ * 注册类 - 注册表模式
3
+ * 管理模块类的注册、模块配置的持久化、模块实例的懒加载
4
+ */
5
+ class Registry {
6
+ /**
7
+ * 构造函数
8
+ * @param {object} config 配置项
9
+ */
10
+ constructor(config) {
11
+ this.config = {
12
+ name: 'default',
13
+ dir: '/data/registry'.fullname(__dirname),
14
+ ...config
15
+ };
16
+ this.class_dict = this.getDefaultClassDict();
17
+ this.module_list = [];
18
+
19
+ // 注册表缓存:类名 -> 类构造函数
20
+ this._class_cache = {};
21
+ // 注册表缓存:模块名 -> { config, instance }
22
+ this._module_cache = {};
23
+ // 类名索引:类名 -> [模块名]
24
+ this._class_index = {};
25
+ this._preset();
26
+ }
27
+ }
28
+
29
+ /**
30
+ * 预设默认模块类
31
+ */
32
+ Registry.prototype._preset = function () {
33
+ this.config.dir.addDir();
34
+ };
35
+
36
+ /**
37
+ * 获取模块类字典
38
+ * @returns {object} 模块类字典
39
+ */
40
+ Registry.prototype.getClassDict = function () {
41
+ return this.class_dict;
42
+ };
43
+
44
+ /**
45
+ * 获取默认模块类
46
+ * @returns {object} 默认模块类
47
+ */
48
+ Registry.prototype.getDefaultClassDict = function () {
49
+ return {
50
+ Mod: './mod.js'.fullname(__dirname),
51
+ Drive: './drive.js'.fullname(__dirname),
52
+ Manager: './manager.js'.fullname(__dirname)
53
+ };
54
+ };
55
+
56
+ /**
57
+ * 设置模块类
58
+ * @param {string} class_name 模块类名
59
+ * @param {string} file 模块类文件路径
60
+ */
61
+ Registry.prototype.setClass = function (class_name, file) {
62
+ this.class_dict[class_name] = file;
63
+ };
64
+
65
+ /**
66
+ * 删除模块类
67
+ * @param {string} class_name 模块类名
68
+ */
69
+ Registry.prototype.delClass = function (class_name) {
70
+ delete this.class_dict[class_name];
71
+ };
72
+
73
+ // ==== 注册表缓存方法 ====
74
+
75
+ /**
76
+ * 注册模块类到缓存
77
+ * @param {string} class_name 模块类名
78
+ * @param {Function} class_ref 模块类构造函数
79
+ */
80
+ Registry.prototype.registerClass = function (class_name, class_ref) {
81
+ if (!class_name || !class_ref) {
82
+ throw new TypeError('模块类名和类引用不能为空');
83
+ }
84
+ this._class_cache[class_name] = class_ref;
85
+ };
86
+
87
+ /**
88
+ * 获取模块类构造函数
89
+ * @param {string} class_name 模块类名
90
+ * @returns {Function} 模块类构造函数
91
+ */
92
+ Registry.prototype.getClass = function (class_name) {
93
+ return this._class_cache[class_name];
94
+ };
95
+
96
+ /**
97
+ * 注册模块配置到缓存(不实例化)
98
+ * @param {object} config 模块配置项
99
+ * @returns {object} 注册的配置项
100
+ * @throws {Error} 模块配置项错误
101
+ */
102
+ Registry.prototype.registerModule = function (config) {
103
+ if (!config || !config.name) {
104
+ throw new TypeError('模块配置和模块名称不能为空');
105
+ }
106
+ var class_name = config.class_name || 'Drive';
107
+ this._module_cache[config.name] = {
108
+ config: config,
109
+ instance: null
110
+ };
111
+ // 按类名建立索引
112
+ if (!this._class_index[class_name]) {
113
+ this._class_index[class_name] = [];
114
+ }
115
+ this._class_index[class_name].push(config.name);
116
+ return config;
117
+ };
118
+
119
+ /**
120
+ * 获取模块实例(懒加载)
121
+ * 首次调用时根据 config 和已注册的类构造函数实例化
122
+ * @param {string} name 模块名
123
+ * @returns {object|null} 模块实例
124
+ */
125
+ Registry.prototype.getModule = function (name) {
126
+ var entry = this._module_cache[name];
127
+ if (!entry) return null;
128
+
129
+ if (!entry.instance) {
130
+ var class_name = entry.config.class_name || 'Drive';
131
+ const CS = this._class_cache[class_name];
132
+ if (!CS) {
133
+ throw new Error('模块类 ' + class_name + ' 未注册');
134
+ }
135
+ entry.instance = new CS(entry.config);
136
+ }
137
+ return entry.instance;
138
+ };
139
+
140
+ /**
141
+ * 按类名获取模块实例列表
142
+ * @param {string} class_name 模块类名
143
+ * @returns {Array} 模块实例数组
144
+ */
145
+ Registry.prototype.getModules = function (class_name) {
146
+ var names = this._class_index[class_name];
147
+ if (!names || names.length === 0) return [];
148
+ return names.map(function (name) {
149
+ return this.getModule(name);
150
+ }, this);
151
+ };
152
+
153
+ /**
154
+ * 注销模块
155
+ * @param {string} name 模块名
156
+ */
157
+ Registry.prototype.unregisterModule = function (name) {
158
+ var entry = this._module_cache[name];
159
+ if (!entry) return;
160
+
161
+ var class_name = entry.config.class_name || 'Drive';
162
+ var names = this._class_index[class_name];
163
+ if (names) {
164
+ var idx = names.indexOf(name);
165
+ if (idx !== -1) {
166
+ names.splice(idx, 1);
167
+ }
168
+ }
169
+ delete this._module_cache[name];
170
+ };
171
+
172
+ /**
173
+ * 加载注册数据
174
+ * 从 JSON 文件读取持久化数据,注册所有类和模块到缓存
175
+ */
176
+ Registry.prototype.load = function () {
177
+ this.read();
178
+
179
+ // 注册模块类(幂等-safe,Node require 返回同一引用)
180
+ for (var key in this.class_dict) {
181
+ var file = this.class_dict[key];
182
+ var exports = require(file);
183
+ var class_ref = exports[key] || exports;
184
+ this.registerClass(key, class_ref);
185
+ }
186
+
187
+ // 注册模块配置(仅新增不覆盖,保留已有实例)
188
+ for (var i = 0; i < this.module_list.length; i++) {
189
+ var mod_cfg = this.module_list[i];
190
+ if (!this._module_cache[mod_cfg.name]) {
191
+ this.registerModule(mod_cfg);
192
+ }
193
+ }
194
+ };
195
+
196
+ // ==== 配置项 CRUD ====
197
+
198
+ /**
199
+ * 创建新模块配置项
200
+ * @returns {object} 模块配置项
201
+ */
202
+ Registry.prototype.newModel = function () {
203
+ return {
204
+ parent: '',
205
+ name: '',
206
+ title: '',
207
+ description: '',
208
+ version: '1.0.0',
209
+ dependencies: {},
210
+ sort: 100,
211
+ enabled: true,
212
+ auto_start: 'auto',
213
+ class_name: 'Drive',
214
+ config_file: '',
215
+ main: '',
216
+ values: {}
217
+ };
218
+ };
219
+
220
+ /**
221
+ * 设置模块
222
+ * @param {object} config 模块配置项
223
+ * @returns {object} 模块配置项
224
+ */
225
+ Registry.prototype.set = function (config) {
226
+ let cg = this.checkConfig(config);
227
+ let has = false;
228
+ for (let i = 0; i < this.module_list.length; i++) {
229
+ let o = this.module_list[i];
230
+ if (o.name === cg.name) {
231
+ if (o.class_name !== cg.class_name) {
232
+ throw new Error('模块类名与配置项类名不一致');
233
+ }
234
+ this.module_list[i] = cg;
235
+ has = true;
236
+ break;
237
+ }
238
+ }
239
+ if (!has) {
240
+ this.module_list.push(cg);
241
+ }
242
+ // 清除外部缓存索引
243
+ this._config_map = null;
244
+ return cg;
245
+ };
246
+
247
+ /**
248
+ * 获取模块
249
+ * @param {string} name 模块名称
250
+ * @returns {object} 模块配置项
251
+ */
252
+ Registry.prototype.get = function (name) {
253
+ // 先查缓存索引,无命中再回退线性扫描
254
+ var entry = this._module_cache[name];
255
+ if (entry) return entry.config;
256
+ for (let i = 0; i < this.module_list.length; i++) {
257
+ if (this.module_list[i].name === name) {
258
+ return this.module_list[i];
259
+ }
260
+ }
261
+ return null;
262
+ };
263
+
264
+ /**
265
+ * 删除模块
266
+ * @param {string} name 模块名称
267
+ * @returns {object} 模块配置项
268
+ */
269
+ Registry.prototype.del = function (name) {
270
+ let obj = null;
271
+ for (let i = 0; i < this.module_list.length; i++) {
272
+ if (this.module_list[i].name === name) {
273
+ obj = this.module_list[i];
274
+ this.module_list.splice(i, 1);
275
+ break;
276
+ }
277
+ }
278
+ // 清除外部缓存索引
279
+ this._config_map = null;
280
+ return obj;
281
+ };
282
+
283
+ /**
284
+ * 获取模块列表
285
+ * @returns {Array} 模块配置项数组
286
+ */
287
+ Registry.prototype.getList = function () {
288
+ return this.module_list;
289
+ };
290
+
291
+ /**
292
+ * 检查配置项
293
+ * @param {object} config 模块配置项
294
+ * @returns {object} 模块配置项
295
+ * @throws {Error} 模块配置项错误
296
+ */
297
+ Registry.prototype.checkConfig = function (config) {
298
+ let cg = { ...this.newModel(), ...config };
299
+ let {
300
+ parent,
301
+ name,
302
+ title,
303
+ description,
304
+ version,
305
+ enabled,
306
+ auto_start,
307
+ class_name,
308
+ config_file,
309
+ main,
310
+ dependencies
311
+ } = cg;
312
+ if (!parent) {
313
+ cg.parent = '';
314
+ }
315
+ if (!name) {
316
+ throw new Error('模块名称不能为空');
317
+ }
318
+ if (!title) {
319
+ throw new Error('模块标题不能为空');
320
+ }
321
+ if (!description) {
322
+ throw new Error('模块描述不能为空');
323
+ }
324
+ if (!config_file) {
325
+ throw new Error('模块配置文件不能为空');
326
+ }
327
+ if (!['true', 'false', 'auto'].includes(auto_start)) {
328
+ throw new Error('模块自动启动配置错误,只能为true、false、auto');
329
+ }
330
+ if (!this.class_dict[class_name]) {
331
+ throw new Error('模块类不存在');
332
+ }
333
+ if (!enabled) {
334
+ cg.enabled = false;
335
+ } else if (enabled === 'true') {
336
+ cg.enabled = true;
337
+ }
338
+ if (!version) {
339
+ cg.version = '1.0.0';
340
+ }
341
+ if (!main) {
342
+ cg.main = '';
343
+ }
344
+ if (!dependencies) {
345
+ cg.dependencies = {};
346
+ }
347
+ return cg;
348
+ };
349
+
350
+ /**
351
+ * 获取模块类文件
352
+ * @param {string} class_name 模块类名
353
+ * @returns {string} 模块类文件路径
354
+ */
355
+ Registry.prototype.getClassFile = function (class_name) {
356
+ let file_name = this.config.name + '_class.json';
357
+ let file = file_name.fullname(this.config.dir);
358
+ file.addDir();
359
+ return file;
360
+ };
361
+
362
+ /**
363
+ * 获取模块文件
364
+ * @returns {string} 模块文件路径
365
+ */
366
+ Registry.prototype.getModuleFile = function () {
367
+ let file_name = this.config.name + '_module.json';
368
+ let file = file_name.fullname(this.config.dir);
369
+ file.addDir();
370
+ return file;
371
+ };
372
+
373
+ /**
374
+ * 读取模块
375
+ * @returns {Array} 模块配置项数组
376
+ */
377
+ Registry.prototype.read = function () {
378
+ this.class_dict = this.getClassFile().loadJson() || this.getDefaultClassDict();
379
+ this.module_list = this.getModuleFile().loadJson() || [];
380
+ };
381
+
382
+ /**
383
+ * 保存模块
384
+ * @param {object} config 模块配置项
385
+ * @returns {object} 模块配置项
386
+ */
387
+ Registry.prototype.save = function () {
388
+ this.getClassFile().saveJson(this.class_dict);
389
+ this.getModuleFile().saveJson(this.module_list);
390
+ };
391
+
392
+ /**
393
+ * 排序模块
394
+ */
395
+ Registry.prototype.sort = function () {
396
+ this.module_list.sort((a, b) => {
397
+ if (a.parent !== b.parent) {
398
+ return a.parent.localeCompare(b.parent);
399
+ }
400
+ if (a.sort !== b.sort) {
401
+ return a.sort - b.sort;
402
+ }
403
+ if (a.name !== b.name) {
404
+ return a.name.localeCompare(b.name);
405
+ }
406
+ return 0;
407
+ });
408
+ };
409
+
410
+ module.exports = {
411
+ Registry
412
+ };