whistle.mockbubu 2.0.0-beta.2 → 2.0.0-beta.3

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/README.md CHANGED
@@ -59,59 +59,6 @@ pattern mockbubu:// or pattern mockbubu://pathname
59
59
 
60
60
  [![step2.png](https://i.postimg.cc/K8wsg8xv/step2.png)](https://postimg.cc/Js3Qfms9)
61
61
 
62
- ---
63
-
64
- # Performance & Memory Monitoring
65
-
66
- For developers and advanced users, we provide performance monitoring tools:
67
-
68
- ## Memory Usage Monitoring
69
-
70
- Monitor real-time memory usage of the mockbubu plugin:
71
-
72
- ```bash
73
- $ node scripts/monitor-memory.js
74
- ```
75
-
76
- This will display:
77
- - Heap memory usage (New Space & Old Space)
78
- - Process memory (RSS, external, array buffers)
79
- - GC statistics
80
- - Warnings when memory usage exceeds 80%
81
-
82
- ## Stress Testing
83
-
84
- Test plugin performance with large datasets:
85
-
86
- ```bash
87
- $ ./scripts/stress-test-memory.sh [file_count] [group_name]
88
-
89
- # Example: Create 10,000 test files
90
- $ ./scripts/stress-test-memory.sh 10000 stress-test-group
91
- ```
92
-
93
- The test will:
94
- 1. Create specified number of mock files
95
- 2. Measure API list query performance (cold start, hot cache, concurrent)
96
- 3. Test batch deletion performance
97
- 4. Generate a detailed performance report
98
-
99
- ## Memory Limit Analysis
100
-
101
- See [docs/MEMORY-LIMIT-ANALYSIS.md](/docs/MEMORY-LIMIT-ANALYSIS.md) for:
102
- - Whistle's `--max-semi-space-size=64` parameter explanation
103
- - Memory architecture analysis (New Space vs Old Space)
104
- - Impact assessment for different file counts (10K, 100K, 1M files)
105
- - Optimization recommendations
106
- - Action plan for scaling
107
-
108
- **Key Findings**:
109
- - Current implementation is safe for up to 10,000 files without any optimization
110
- - For 100,000+ files, consider implementing pagination or index sharding
111
- - The plugin's file-system based V3 architecture provides excellent memory efficiency
112
-
113
- ---
114
-
115
62
  # whistle.mockbubu
116
63
 
117
64
  whistle.mockbubu 是[whistle](https://github.com/avwo/whistle)的一个扩展脚本插件,包括以下功能
@@ -170,56 +117,3 @@ pattern mockbubu:// 或者 pattern mockbubu://pathname
170
117
  - mock 返回内容使用当前选中的版本
171
118
 
172
119
  [![step2.png](https://i.postimg.cc/K8wsg8xv/step2.png)](https://postimg.cc/Js3Qfms9)
173
-
174
- ---
175
-
176
- # 性能与内存监控
177
-
178
- 为开发者和高级用户提供性能监控工具:
179
-
180
- ## 内存使用监控
181
-
182
- 实时监控 mockbubu 插件的内存使用情况:
183
-
184
- ```bash
185
- $ node scripts/monitor-memory.js
186
- ```
187
-
188
- 将显示以下信息:
189
- - 堆内存使用情况(New Space 和 Old Space)
190
- - 进程内存(RSS、外部内存、数组缓冲区)
191
- - GC 统计
192
- - 内存使用超过 80% 时的警告
193
-
194
- ## 压力测试
195
-
196
- 使用大数据集测试插件性能:
197
-
198
- ```bash
199
- $ ./scripts/stress-test-memory.sh [文件数量] [组名]
200
-
201
- # 示例:创建 10,000 个测试文件
202
- $ ./scripts/stress-test-memory.sh 10000 stress-test-group
203
- ```
204
-
205
- 测试将执行:
206
- 1. 创建指定数量的 mock 文件
207
- 2. 测量 API 列表查询性能(冷启动、热缓存、并发)
208
- 3. 测试批量删除性能
209
- 4. 生成详细的性能报告
210
-
211
- ## 内存限制分析
212
-
213
- 查看 [docs/MEMORY-LIMIT-ANALYSIS.md](/docs/MEMORY-LIMIT-ANALYSIS.md) 了解:
214
- - Whistle 的 `--max-semi-space-size=64` 参数说明
215
- - 内存架构分析(New Space vs Old Space)
216
- - 不同文件数量(1万、10万、100万)的影响评估
217
- - 优化建议
218
- - 扩展性行动计划
219
-
220
- **关键结论**:
221
- - 当前实现在 10,000 文件以下完全安全,无需任何优化
222
- - 对于 100,000+ 文件,建议实施分页或索引分片
223
- - 插件的基于文件系统的 V3 架构提供了出色的内存效率
224
-
225
- ---
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 规则收集器 - 收集和管理生效的 Whistle 规则
3
+ * 用于在 UI 中显示帮助提示
4
+ */
5
+
6
+ class RuleCollector {
7
+ constructor() {
8
+ this.activeRules = new Set()
9
+ }
10
+
11
+ /**
12
+ * 添加一条生效的规则
13
+ * @param {string} rule - 规则字符串,如 "*.shein.com mockbubu://pathname"
14
+ */
15
+ addRule(rule) {
16
+ this.activeRules.add(rule)
17
+ }
18
+
19
+ /**
20
+ * 获取所有生效的规则列表
21
+ * @returns {Array<string>} 规则数组
22
+ */
23
+ getRules() {
24
+ return Array.from(this.activeRules).sort()
25
+ }
26
+
27
+ /**
28
+ * 清空所有规则
29
+ */
30
+ clear() {
31
+ this.activeRules.clear()
32
+ }
33
+
34
+ /**
35
+ * 获取规则数量
36
+ * @returns {number}
37
+ */
38
+ getCount() {
39
+ return this.activeRules.size
40
+ }
41
+ }
42
+
43
+ // 导出单例
44
+ module.exports = new RuleCollector()
package/lib/server.js CHANGED
@@ -8,6 +8,7 @@ const {
8
8
  } = require('./utils')
9
9
  const GroupManager = require('./group-manager')
10
10
  const StorageAdapter = require('./storage-adapter')
11
+ const ruleCollector = require('./rule-collector')
11
12
 
12
13
  module.exports = (server, options) => {
13
14
  console.log('[mockbubu] 🚀 server.js 模块已加载!')
@@ -47,6 +48,9 @@ module.exports = (server, options) => {
47
48
  const filename = getFilename(originalReq)
48
49
  const rule = getRule(originalReq)
49
50
 
51
+ // 收集生效的规则(用于 UI 帮助提示)
52
+ ruleCollector.addRule(rule)
53
+
50
54
  // 调试日志:记录所有请求
51
55
  if (url.includes('trend_card')) {
52
56
  console.log('[mockbubu] 🔍 [trend_card] 收到请求')
package/lib/storage-v3.js CHANGED
@@ -53,6 +53,58 @@ class FileSystemStorage {
53
53
  isDefault: true,
54
54
  })
55
55
  }
56
+
57
+ // 清理老旧临时目录(启动时执行)
58
+ this._cleanupOldTempDirs().catch((err) => {
59
+ console.error('[storage-v3] 清理临时目录失败:', err.message)
60
+ })
61
+ }
62
+
63
+ /**
64
+ * 清理7天前的临时目录
65
+ * @private
66
+ */
67
+ async _cleanupOldTempDirs() {
68
+ const tmpDir = path.join(this.baseDir, '.tmp')
69
+
70
+ try {
71
+ await fs.access(tmpDir)
72
+ } catch {
73
+ // .tmp 目录不存在,无需清理
74
+ return
75
+ }
76
+
77
+ try {
78
+ const entries = await fs.readdir(tmpDir, { withFileTypes: true })
79
+ const now = Date.now()
80
+ const SEVEN_DAYS = 7 * 24 * 60 * 60 * 1000
81
+ let cleanedCount = 0
82
+
83
+ for (const entry of entries) {
84
+ if (entry.isDirectory()) {
85
+ const dirPath = path.join(tmpDir, entry.name)
86
+ try {
87
+ const stat = await fs.stat(dirPath)
88
+ const age = now - stat.mtimeMs
89
+
90
+ // 删除7天前的临时目录
91
+ if (age > SEVEN_DAYS) {
92
+ await fs.rm(dirPath, { recursive: true, force: true })
93
+ cleanedCount++
94
+ console.log(`[storage-v3] ✓ 已清理老旧临时目录: ${entry.name} (${Math.floor(age / (24 * 60 * 60 * 1000))}天前)`)
95
+ }
96
+ } catch (err) {
97
+ console.error(`[storage-v3] 清理临时目录失败: ${entry.name}`, err.message)
98
+ }
99
+ }
100
+ }
101
+
102
+ if (cleanedCount > 0) {
103
+ console.log(`[storage-v3] ✓ 临时目录清理完成,共清理 ${cleanedCount} 个`)
104
+ }
105
+ } catch (err) {
106
+ console.error('[storage-v3] 扫描临时目录失败:', err.message)
107
+ }
56
108
  }
57
109
 
58
110
  /**