mm_machine 3.4.9 → 3.5.0

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 (2) hide show
  1. package/lib/scanner.js +169 -9
  2. package/package.json +6 -6
package/lib/scanner.js CHANGED
@@ -1,12 +1,131 @@
1
+ const fs = require('fs');
1
2
  require('mm_expand');
2
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
+
3
118
  /**
4
119
  * 扫描类
5
120
  * 负责按目录或文件名模式检索文件
121
+ * @param {Array} ignore 忽略目录数组
122
+ * @param {number} max_depth 最大搜索深度
6
123
  */
7
124
  class Scanner {
8
125
  /**
9
126
  * 构造函数
127
+ * @param {Array} ignore 忽略目录数组
128
+ * @param {number} max_depth 最大搜索深度
10
129
  */
11
130
  constructor(ignore, max_depth = 3) {
12
131
  this.ignore = ignore || ['node_modules', '.git'];
@@ -20,15 +139,21 @@ class Scanner {
20
139
  * @param {string} dir 根目录
21
140
  * @param {string} filename 目标文件名
22
141
  * @param {string} dirname 目标目录名
23
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
24
142
  * @param {Array} ignore 忽略目录数组
143
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
25
144
  * @returns {Array} 文件路径数组
26
145
  */
27
- Scanner.prototype.scan = async function (dir, filename, dirname, ignore = this.ignore, max_depth = this.max_depth) {
146
+ Scanner.prototype.scan = async function (
147
+ dir,
148
+ filename,
149
+ dirname,
150
+ ignore = this.ignore,
151
+ max_depth = this.max_depth
152
+ ) {
28
153
  let dirs = await this.scanDir(dir, dirname, ignore, max_depth);
29
154
  let results = await Promise.all(
30
155
  dirs.map((d) => {
31
- return $.file.getAllAsync(d, filename, undefined, ignore, max_depth);
156
+ return this.scanFile(d, filename, ignore, max_depth);
32
157
  })
33
158
  );
34
159
  return results.flat();
@@ -39,12 +164,26 @@ Scanner.prototype.scan = async function (dir, filename, dirname, ignore = this.i
39
164
  * 在指定目录下找所有匹配 dirname 的子目录
40
165
  * @param {string} dir 根目录
41
166
  * @param {string} dirname 目标目录名
42
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
43
167
  * @param {Array} ignore 忽略目录数组
168
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
44
169
  * @returns {Array} 目录路径数组
45
170
  */
46
- Scanner.prototype.scanDir = async function (dir, dirname, ignore = this.ignore, max_depth = this.max_depth) {
47
- return await $.dir.getAllAsync(dir, dirname, ignore, max_depth);
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 = await $.dir.getAllAsync(dir, dirname, ignore, max_depth);
183
+ // 目录快照需包含全部目录(忽略关键词), 才能检测新增目录
184
+ let all_dirs = await $.dir.getAllAsync(dir, undefined, ignore, max_depth);
185
+ _setCache(key, dirs, all_dirs);
186
+ return dirs;
48
187
  };
49
188
 
50
189
  /**
@@ -52,12 +191,33 @@ Scanner.prototype.scanDir = async function (dir, dirname, ignore = this.ignore,
52
191
  * 在指定目录下找所有匹配 filename 的文件
53
192
  * @param {string} dir 根目录
54
193
  * @param {string} filename 目标文件名
55
- * @param {number} max_depth 最大搜索深度 -1表示不限制深度
56
194
  * @param {Array} ignore 忽略目录数组
195
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
57
196
  * @returns {Array} 文件路径数组
58
197
  */
59
- Scanner.prototype.scanFile = async function (dir, filename, ignore = this.ignore, max_depth = this.max_depth) {
60
- return await $.file.getAllAsync(dir, filename, undefined, ignore, max_depth);
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 = await $.file.getAllAsync(dir, filename, undefined, ignore, max_depth);
210
+ // 目录快照用于检测新增或删除文件
211
+ let all_dirs = await $.dir.getAllAsync(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();
61
221
  };
62
222
 
63
223
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.4.9",
3
+ "version": "3.5.0",
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": {
@@ -33,13 +33,13 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@prettier/sync": "^0.6.1",
36
- "mm_config": "^2.4.8",
37
- "mm_hot_reload": "^1.4.7",
38
- "mm_tpl": "^2.6.8"
36
+ "mm_config": "^2.5.0",
37
+ "mm_hot_reload": "^1.5.0",
38
+ "mm_tpl": "^2.7.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "eslint": "^10.8.1",
42
- "eslint-plugin-jsdoc": "^64.0.1",
43
- "mm_eslint": "^1.7.5"
42
+ "eslint-plugin-jsdoc": "^64.1.0",
43
+ "mm_eslint": "^1.7.6"
44
44
  }
45
45
  }