mm_machine 3.5.4 → 3.5.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/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.5.4",
3
+ "version": "3.5.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": {
7
- "test": "node ./test.js",
7
+ "test": "jest --coverage",
8
+ "demo": "node example/index.js",
8
9
  "dev": "nodemon ./test.js"
9
10
  },
10
11
  "repository": {
@@ -12,6 +13,9 @@
12
13
  "url": "git+https://gitee.com/qiuwenwu91/mm_machine.git"
13
14
  },
14
15
  "keywords": [
16
+ "mm_mod",
17
+ "mm_infra",
18
+ "mm_runtime",
15
19
  "plugin-system",
16
20
  "module-management",
17
21
  "hot-reload",
@@ -21,6 +25,8 @@
21
25
  "framework",
22
26
  "mechanism"
23
27
  ],
28
+ "layer": "infra",
29
+ "group": "runtime",
24
30
  "author": "邱文武",
25
31
  "license": "ISC",
26
32
  "engines": {
@@ -29,17 +35,22 @@
29
35
  "files": [
30
36
  "index.js",
31
37
  "lib/*",
38
+ "example",
32
39
  "README.md"
33
40
  ],
34
41
  "dependencies": {
35
42
  "@prettier/sync": "^0.6.1",
36
- "mm_config": "^2.5.2",
37
- "mm_hot_reload": "^1.5.1",
38
- "mm_tpl": "^2.7.1"
43
+ "mm_config": "^2.5.3",
44
+ "mm_hot_reload": "^1.5.3",
45
+ "mm_helper": "^1.0.3",
46
+ "mm_loader": "^1.1.1",
47
+ "mm_mod": "^1.0.6",
48
+ "mm_tpl": "^2.7.3"
39
49
  },
40
50
  "devDependencies": {
41
- "eslint": "^10.9.1",
42
- "eslint-plugin-jsdoc": "^64.2.1",
43
- "mm_eslint": "^1.7.6"
51
+ "eslint": "^10.13.0",
52
+ "eslint-plugin-jsdoc": "^65.4.2",
53
+ "jest": "^30.5.2",
54
+ "mm_eslint": "^1.7.7"
44
55
  }
45
56
  }
@@ -1,174 +0,0 @@
1
- require('mm_hot_reload');
2
-
3
- const _AUTO_INST = {};
4
- // 内置模块名集合
5
- const BUILTIN_MODULES = new Set(require('module').builtinModules);
6
-
7
- /**
8
- * 从模块标识符中提取npm包名
9
- * @param {string} id 模块标识符
10
- * @returns {string} npm包名
11
- */
12
- function _extractPkgName(id) {
13
- if (id.startsWith('@')) {
14
- let parts = id.split('/');
15
- if (parts.length >= 2) {
16
- return parts[0] + '/' + parts[1];
17
- }
18
- }
19
- return id.split('/')[0];
20
- }
21
-
22
- /**
23
- * 从MODULE_NOT_FOUND错误消息中提取模块名
24
- * @param {Error} e 错误对象
25
- * @returns {string} 模块名
26
- */
27
- function _extractErrName(e) {
28
- let match = e.message.match(/'([^']+)'/);
29
- return match ? match[1] : '';
30
- }
31
-
32
- /**
33
- * 是否为本地路径引用(非npm包)
34
- * @param {string} name 模块名
35
- * @returns {boolean} 是否为本地路径
36
- */
37
- function _isLocal(name) {
38
- return name.startsWith('.') || name.startsWith('/') || /^[a-zA-Z]:/.test(name);
39
- }
40
-
41
- /**
42
- * 是否为Node内置模块
43
- * @param {string} name 模块名
44
- * @returns {boolean} 是否为内置模块
45
- */
46
- function _isBuiltin(name) {
47
- let raw_name = name.startsWith('node:') ? name.slice(5) : name;
48
- return BUILTIN_MODULES.has(raw_name);
49
- }
50
-
51
- /**
52
- * 清除指定包的require缓存
53
- * @param {string} name 包名
54
- */
55
- function _clearPkgCache(name) {
56
- try {
57
- let main_file = require.resolve(name);
58
- delete require.cache[main_file];
59
- } catch (e) {
60
- void e;
61
- }
62
- let sep = require('path').sep;
63
- let marker = sep + 'node_modules' + sep + name + sep;
64
- for (let key in require.cache) {
65
- if (key.indexOf(marker) !== -1) {
66
- delete require.cache[key];
67
- }
68
- }
69
- }
70
-
71
- /**
72
- * 同步安装npm包
73
- * @param {string} name 包名
74
- * @param {string} cwd 安装目录
75
- */
76
- function _npmInstall(name, cwd) {
77
- let install_dir = cwd || process.cwd();
78
- let cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
79
- let exec_sync = require('child_process').execSync;
80
- exec_sync(cmd + ' install ' + name + ' --save-prod',
81
- { cwd: install_dir, stdio: 'pipe' });
82
- }
83
-
84
- /**
85
- * 安装指定npm包
86
- * @param {string} name 包名或模块标识符
87
- * @returns {boolean} 是否已安装
88
- */
89
- function _installByName(name) {
90
- let pkg_name = _extractPkgName(name);
91
- if (!pkg_name || _AUTO_INST[pkg_name]) {
92
- return false;
93
- }
94
- _AUTO_INST[pkg_name] = true;
95
- try {
96
- _npmInstall(pkg_name);
97
- _clearPkgCache(pkg_name);
98
- return true;
99
- } catch (e) {
100
- _AUTO_INST[pkg_name] = false;
101
- return false;
102
- }
103
- }
104
-
105
- /**
106
- * 分析 $.mod.load 失败原因,若是缺少npm依赖则安装
107
- * @param {string} file 文件路径
108
- * @returns {boolean} 是否已安装依赖
109
- */
110
- function _tryInstall(file) {
111
- try {
112
- require(require('path').resolve(file));
113
- return false;
114
- } catch (e) {
115
- if (e.code !== 'MODULE_NOT_FOUND') {
116
- return false;
117
- }
118
- let raw_name = _extractErrName(e);
119
- if (!raw_name || _isLocal(raw_name) || _isBuiltin(raw_name)) {
120
- return false;
121
- }
122
- return _installByName(raw_name);
123
- }
124
- }
125
-
126
- /**
127
- * 增强require函数,支持脚本文件(.js)热重载、内置模块和npm包加载
128
- * 当引用的目标模块缺少npm依赖时,自动npm install并重试加载
129
- * @param {string} file 文件路径、内置模块名或npm包名
130
- * @param {Function} func 热更新回调函数,文件变化时触发
131
- * @returns {object} 加载的模块对象
132
- * @throws {TypeError} 参数类型错误时抛出
133
- */
134
- $.require = function (file, func) {
135
- if (typeof file !== 'string') {
136
- throw new TypeError('第一个参数(file)必须是字符串');
137
- }
138
- if (func && typeof func !== 'function') {
139
- throw new TypeError('第二个参数(func)必须是函数');
140
- }
141
-
142
- if (_isBuiltin(file)) {
143
- return require(file);
144
- }
145
-
146
- // 本地文件路径:热重载加载
147
- if (_isLocal(file)) {
148
- let mod = $.mod.load(file, func);
149
- if (mod !== null) {
150
- return mod;
151
- }
152
- if (_tryInstall(file)) {
153
- return $.mod.load(file, func);
154
- }
155
- return null;
156
- }
157
-
158
- // npm包名:原生加载,失败时先按文件再自动安装
159
- try {
160
- return require(file);
161
- } catch (e) {
162
- if (e.code !== 'MODULE_NOT_FOUND') {
163
- return null;
164
- }
165
- let mod = $.mod.load(file, func);
166
- if (mod !== null) {
167
- return mod;
168
- }
169
- if (_installByName(file)) {
170
- return require(file);
171
- }
172
- return null;
173
- }
174
- };
package/lib/scanner.js DELETED
@@ -1,225 +0,0 @@
1
- const fs = require('fs');
2
- require('mm_expand');
3
-
4
- // 扫描结果缓存, 按目录mtime与文件mtime校验失效
5
- // 避免多个Manager实例重复遍历同一目录树, 显著提升批量加载速度
6
- const _scanCache = new Map();
7
- const _CACHE_MAX = 200;
8
- // 快速复用窗口: 启动阶段文件树刚扫描过, 短时间内的命中直接复用, 避免每次全量校验目录mtime
9
- const _CACHE_TTL = 2000;
10
-
11
- /**
12
- * 生成扫描缓存键
13
- * @param {string} dir 根目录
14
- * @param {string} keyword 文件搜索关键词
15
- * @param {string} keyword_dir 目录搜索关键词
16
- * @param {Array} ignore 忽略目录数组
17
- * @param {number} max_depth 最大搜索深度
18
- * @returns {string} 缓存键
19
- */
20
- function _cacheKey(dir, keyword, keyword_dir, ignore, max_depth) {
21
- let depth = max_depth === undefined || max_depth === null ? -1 : max_depth;
22
- return (
23
- (dir || '') +
24
- '\u0001' +
25
- (keyword || '') +
26
- '\u0001' +
27
- (keyword_dir || '') +
28
- '\u0001' +
29
- (ignore || []).join(',') +
30
- '\u0001' +
31
- depth
32
- );
33
- }
34
-
35
- /**
36
- * 获取路径mtime毫秒值, 失败返回-1
37
- * @param {string} f 路径
38
- * @returns {number} mtime毫秒值
39
- */
40
- function _getMtime(f) {
41
- try {
42
- return fs.statSync(f).mtimeMs;
43
- } catch {
44
- return -1;
45
- }
46
- }
47
-
48
- /**
49
- * 校验扫描缓存是否有效
50
- * 目录mtime变化表示有新增或删除, 文件mtime变化表示内容修改
51
- * @param {object} cache 缓存对象
52
- * @returns {boolean} 有效返回true
53
- */
54
- function _isCacheValid(cache) {
55
- for (let i = 0; i < cache.dirs.length; i++) {
56
- if (_getMtime(cache.dirs[i]) !== cache.dir_mtimes[i]) {
57
- return false;
58
- }
59
- }
60
- for (let i = 0; i < cache.list.length; i++) {
61
- if (_getMtime(cache.list[i]) !== cache.list_mtimes[i]) {
62
- return false;
63
- }
64
- }
65
- return true;
66
- }
67
-
68
- /**
69
- * 获取扫描缓存
70
- * @param {string} key 缓存键
71
- * @returns {object|null} 有效的缓存对象, 无效返回null
72
- */
73
- function _getCache(key) {
74
- let cache = _scanCache.get(key);
75
- if (!cache) {
76
- return null;
77
- }
78
- let now = Date.now();
79
- // 启动窗口内直接复用, 避免对大量目录做全量mtime校验
80
- if (now - cache.time <= _CACHE_TTL) {
81
- return cache;
82
- }
83
- // 窗口外做mtime校验, 文件或目录变化时重新扫描
84
- if (_isCacheValid(cache)) {
85
- cache.time = now;
86
- return cache;
87
- }
88
- return null;
89
- }
90
-
91
- /**
92
- * 保存扫描缓存
93
- * @param {string} key 缓存键
94
- * @param {Array} list 结果路径数组
95
- * @param {Array} dirs 遍历的目录数组
96
- */
97
- function _setCache(key, list, dirs) {
98
- let dir_mtimes = [];
99
- for (let i = 0; i < dirs.length; i++) {
100
- dir_mtimes.push(_getMtime(dirs[i]));
101
- }
102
- let list_mtimes = [];
103
- for (let i = 0; i < list.length; i++) {
104
- list_mtimes.push(_getMtime(list[i]));
105
- }
106
- if (_scanCache.size >= _CACHE_MAX) {
107
- _scanCache.clear();
108
- }
109
- _scanCache.set(key, {
110
- list: list,
111
- dirs: dirs,
112
- dir_mtimes: dir_mtimes,
113
- list_mtimes: list_mtimes,
114
- time: Date.now()
115
- });
116
- }
117
-
118
- /**
119
- * 扫描类
120
- * 负责按目录或文件名模式检索文件
121
- * @param {Array} ignore 忽略目录数组
122
- * @param {number} max_depth 最大搜索深度
123
- */
124
- class Scanner {
125
- /**
126
- * 构造函数
127
- * @param {Array} ignore 忽略目录数组
128
- * @param {number} max_depth 最大搜索深度
129
- */
130
- constructor(ignore, max_depth = 3) {
131
- this.ignore = ignore || ['node_modules', '.git'];
132
- this.max_depth = max_depth;
133
- }
134
- }
135
-
136
- /**
137
- * 扫描模块(按目录搜索)
138
- * 先找所有匹配 dirname 的子目录,再在每个子目录中找匹配 filename 的文件
139
- * @param {string} dir 根目录
140
- * @param {string} filename 目标文件名
141
- * @param {string} dirname 目标目录名
142
- * @param {Array} ignore 忽略目录数组
143
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
144
- * @returns {Array} 文件路径数组
145
- */
146
- Scanner.prototype.scan = async function (
147
- dir,
148
- filename,
149
- dirname,
150
- ignore = this.ignore,
151
- max_depth = this.max_depth
152
- ) {
153
- let dirs = await this.scanDir(dir, dirname, ignore, max_depth);
154
- let results = await Promise.all(
155
- dirs.map((d) => {
156
- return this.scanFile(d, filename, ignore, max_depth);
157
- })
158
- );
159
- return results.flat();
160
- };
161
-
162
- /**
163
- * 扫描模块目录(按目录搜索)
164
- * 在指定目录下找所有匹配 dirname 的子目录
165
- * @param {string} dir 根目录
166
- * @param {string} dirname 目标目录名
167
- * @param {Array} ignore 忽略目录数组
168
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
169
- * @returns {Array} 目录路径数组
170
- */
171
- Scanner.prototype.scanDir = async function (
172
- dir,
173
- dirname,
174
- ignore = this.ignore,
175
- max_depth = this.max_depth
176
- ) {
177
- let key = _cacheKey(dir, undefined, dirname, ignore, max_depth);
178
- let cache = _getCache(key);
179
- if (cache) {
180
- return cache.list.slice();
181
- }
182
- let dirs = $.dir.getAll(dir, dirname, ignore, max_depth);
183
- // 目录快照需包含全部目录(忽略关键词), 才能检测新增目录
184
- let all_dirs = $.dir.getAll(dir, undefined, ignore, max_depth);
185
- _setCache(key, dirs, all_dirs);
186
- return dirs;
187
- };
188
-
189
- /**
190
- * 扫描模块文件(按文件名搜索)
191
- * 在指定目录下找所有匹配 filename 的文件
192
- * @param {string} dir 根目录
193
- * @param {string} filename 目标文件名
194
- * @param {Array} ignore 忽略目录数组
195
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
196
- * @returns {Array} 文件路径数组
197
- */
198
- Scanner.prototype.scanFile = async function (
199
- dir,
200
- filename,
201
- ignore = this.ignore,
202
- max_depth = this.max_depth
203
- ) {
204
- let key = _cacheKey(dir, filename, undefined, ignore, max_depth);
205
- let cache = _getCache(key);
206
- if (cache) {
207
- return cache.list.slice();
208
- }
209
- let files = $.file.getAll(dir, filename, undefined, ignore, max_depth);
210
- // 目录快照用于检测新增或删除文件
211
- let all_dirs = $.dir.getAll(dir, undefined, ignore, max_depth);
212
- _setCache(key, files, all_dirs);
213
- return files;
214
- };
215
-
216
- /**
217
- * 清除扫描缓存
218
- */
219
- Scanner.clearCache = function () {
220
- _scanCache.clear();
221
- };
222
-
223
- module.exports = {
224
- Scanner
225
- };