mm_options 1.0.1 → 1.1.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.
package/README.md CHANGED
@@ -18,7 +18,7 @@ mm_options 是动态配置存储模块,适用于站长 / 用户配置,可在
18
18
  - 支持配置修改密钥,密钥正确才能修改配置
19
19
  - 未配置密钥时不校验,保持向后兼容
20
20
  - 密钥不进入 `config`,实例只保留 SHA-256 摘要(私有字段),不会写入持久化文件
21
- - 基于 mm_corejs 的 Admin 管理器,支持按作用域缓存实例
21
+ - 基于 mm_admin 的 Admin 管理器,支持按作用域缓存实例
22
22
 
23
23
  ## 安装
24
24
 
@@ -189,7 +189,7 @@ const os_options = optionsAdmin();
189
189
 
190
190
  ## 依赖
191
191
 
192
- - mm_corejs:提供 Admin 管理器
192
+ - mm_admin:提供 Admin 管理器
193
193
  - mm_kv_store:提供键值存储基类
194
194
 
195
195
  ## 测试
@@ -201,3 +201,12 @@ npm test
201
201
  ## 许可证
202
202
 
203
203
  ISC
204
+
205
+ ## 工具契约(LLM 可调用)
206
+
207
+ 本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
208
+ 其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
209
+
210
+ | 工具名 | 标题 | 说明 |
211
+ | --- | --- | --- |
212
+ | `option` | 动态配置存取 | 运行期可变配置读写与落盘 |
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
- const Admin = require('mm_corejs').Admin;
1
+ const { reg } = require('mm_admin');
2
2
  const Options = require('./lib/options.js');
3
3
 
4
- let admin = new Admin();
4
+ const admin = reg('options');
5
5
  admin.setClass(Options);
6
6
 
7
7
  module.exports = {
@@ -14,9 +14,19 @@ module.exports = {
14
14
  * @returns {Options} 动态配置实例
15
15
  */
16
16
  optionsAdmin(scope, config) {
17
- if (!scope) {
18
- scope = 'os';
17
+ const name = scope || 'os';
18
+ const existed = admin.has(name);
19
+ const options = admin.get(name, config);
20
+ if (!existed) {
21
+ options.restore();
19
22
  }
20
- return admin.get(scope, config);
23
+ return options;
24
+ },
25
+ /**
26
+ * 获取缺省动态配置实例
27
+ * @returns {Options} 动态配置实例
28
+ */
29
+ getOptions() {
30
+ return admin.get('os');
21
31
  }
22
32
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_options",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "动态配置存储模块:适用于站长/用户配置,可在运行时动态修改",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -15,7 +15,7 @@
15
15
  "配置",
16
16
  "存储"
17
17
  ],
18
- "layer": "infra",
18
+ "layer": "store",
19
19
  "group": "data",
20
20
  "author": "邱文武",
21
21
  "license": "ISC",
@@ -23,7 +23,7 @@
23
23
  "test": "jest --coverage"
24
24
  },
25
25
  "dependencies": {
26
- "mm_corejs": "^1.1.4",
26
+ "mm_admin": "^1.1.0",
27
27
  "mm_kv_store": "^1.0.0"
28
28
  },
29
29
  "devDependencies": {
@@ -37,6 +37,7 @@
37
37
  "lib",
38
38
  "example",
39
39
  "README.md",
40
- "LICENSE"
40
+ "LICENSE",
41
+ "tool"
41
42
  ]
42
43
  }
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+ /**
3
+ * option —— 动态配置存取工具
4
+ * 复用模块 Options 实例的读写与落盘能力。
5
+ */
6
+ const { optionsAdmin } = require('../../index.js');
7
+
8
+ /**
9
+ * 落盘:未配置持久化文件时只保留内存结果
10
+ * @param {object} store 存储实例
11
+ * @returns {boolean} 是否已落盘
12
+ */
13
+ function _save(store) {
14
+ const file = (store.config && store.config.file) || '';
15
+ if (!file) return false;
16
+ store.save();
17
+ return true;
18
+ }
19
+
20
+ async function main(params, ctx, options) {
21
+ void options;
22
+ const p = params || {};
23
+ const action = p.action || 'list';
24
+ const done = (data) => { ctx.output = data; return data; };
25
+
26
+ try {
27
+ const store = optionsAdmin(p.scope || 'os');
28
+
29
+ if (action === 'set') {
30
+ if (!p.key) throw new TypeError('key 不能为空');
31
+ store.set(p.key, p.value);
32
+ _save(store);
33
+ return done({ code: 0, msg: '写入成功', key: p.key });
34
+ }
35
+
36
+ if (action === 'get') {
37
+ if (!p.key) throw new TypeError('key 不能为空');
38
+ return done({ code: 0, msg: '成功', key: p.key, value: store.get(p.key), exists: store.has(p.key) });
39
+ }
40
+
41
+ if (action === 'del') {
42
+ if (!p.key) throw new TypeError('key 不能为空');
43
+ const deleted = store.del(p.key);
44
+ _save(store);
45
+ return done({ code: 0, msg: deleted ? '删除成功' : '配置不存在', key: p.key, deleted: !!deleted });
46
+ }
47
+
48
+ if (action === 'list') {
49
+ const list = store.list();
50
+ return done({ code: 0, msg: '成功', list, count: Object.keys(list || {}).length });
51
+ }
52
+
53
+ if (action === 'clear') {
54
+ store.clear();
55
+ _save(store);
56
+ return done({ code: 0, msg: '已清空' });
57
+ }
58
+
59
+ if (action === 'save') {
60
+ _save(store);
61
+ return done({ code: 0, msg: '已落盘', ok: true });
62
+ }
63
+
64
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
65
+ } catch (error) {
66
+ return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
67
+ }
68
+ }
69
+
70
+ module.exports = { main };
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "option",
3
+ "title": "动态配置存取",
4
+ "description": "基于 mm_options 的动态配置能力:运行期读写站长/用户配置项、删除、清空并落盘。适用场景:把可变配置(阈值、开关文案、分页大小)落到配置文件并在运行期改。",
5
+ "category": "状态存储",
6
+ "group": "state",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": {
10
+ "type": "object",
11
+ "properties": {
12
+ "action": {
13
+ "type": "string",
14
+ "description": "操作类型: set(写入配置)/get(读取配置)/del(删除配置)/list(列出全部)/clear(清空)/save(落盘)",
15
+ "enum": [
16
+ "set",
17
+ "get",
18
+ "del",
19
+ "list",
20
+ "clear",
21
+ "save"
22
+ ],
23
+ "default": "set"
24
+ },
25
+ "key": {
26
+ "type": "string",
27
+ "description": "配置名(get/del/set 必填)"
28
+ },
29
+ "value": {
30
+ "description": "配置值(set 必填,任意 JSON)"
31
+ },
32
+ "scope": {
33
+ "type": "string",
34
+ "description": "实例作用域,缺省 os",
35
+ "default": "os"
36
+ }
37
+ },
38
+ "required": [
39
+ "action"
40
+ ]
41
+ },
42
+ "returns": {
43
+ "type": "object",
44
+ "properties": {
45
+ "code": {
46
+ "type": "number",
47
+ "description": "0 表示成功,1xxxx 表示业务/执行异常"
48
+ },
49
+ "msg": {
50
+ "type": "string",
51
+ "description": "结果描述"
52
+ },
53
+ "value": {
54
+ "description": "读取到的值(get)"
55
+ },
56
+ "exists": {
57
+ "type": "boolean",
58
+ "description": "是否存在"
59
+ },
60
+ "deleted": {
61
+ "type": "boolean",
62
+ "description": "是否删除成功(del)"
63
+ },
64
+ "list": {
65
+ "type": "object",
66
+ "description": "全部配置(list)"
67
+ },
68
+ "count": {
69
+ "type": "number",
70
+ "description": "数量"
71
+ },
72
+ "error": {
73
+ "type": "object",
74
+ "description": "异常对象 { name, message }"
75
+ }
76
+ }
77
+ },
78
+ "handler": "manager"
79
+ }
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+ const { admin } = require('../../index.js');
3
+
4
+ /**
5
+ * 管理选项存储实例池
6
+ * @param {object} params 参数:action/scope
7
+ * @param {object} ctx 执行上下文
8
+ * @returns {Promise<object>} 执行结果
9
+ */
10
+ async function main(params, ctx) {
11
+ const p = params || {};
12
+ const action = p.action || 'list';
13
+ const scope = p.scope || 'os';
14
+ const done = (data) => {
15
+ if (ctx) ctx.output = data;
16
+ return data;
17
+ };
18
+ try {
19
+ if (action === 'list') {
20
+ const scopes = [...admin.keys()];
21
+ return done({ code: 0, msg: '获取成功', action, count: scopes.length, scopes });
22
+ }
23
+ if (action === 'inspect') {
24
+ const exists = admin.has(scope);
25
+ const instance = exists ? admin.get(scope) : null;
26
+ return done({ code: 0, msg: exists ? '获取成功' : '实例不存在', action, scope, exists, config: instance && instance.config ? instance.config : null });
27
+ }
28
+ if (action === 'del') return done({ code: 0, msg: '删除成功', action, scope, ok: admin.del(scope) });
29
+ if (action === 'clear') { admin.clear(); return done({ code: 0, msg: '实例池已清空', action }); }
30
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
31
+ } catch (error) {
32
+ return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
33
+ }
34
+ }
35
+
36
+ module.exports = { main };
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "option_admin",
3
+ "title": "选项存储实例管理",
4
+ "description": "管理 mm_options 的实例池:list 列出实例、inspect 查看配置、del 删除实例、clear 清空实例池。",
5
+ "category": "选项存储",
6
+ "group": "state",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": { "type": "object", "properties": { "action": { "type": "string", "description": "操作类型: list/inspect/del/clear", "default": "list" }, "scope": { "type": "string", "description": "实例作用域,缺省 os", "default": "os" } }, "required": ["action"] },
10
+ "handler": "manager"
11
+ }