mm_options 1.1.0 → 1.1.1

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
@@ -29,7 +29,7 @@ npm install mm_options
29
29
  ## 快速开始
30
30
 
31
31
  ```javascript
32
- const { Options, optionsAdmin } = require('mm_options');
32
+ const { Options, optionAdmin } = require('mm_options');
33
33
 
34
34
  // 直接使用存储类
35
35
  const options = new Options({ file: './data/options.json' });
@@ -38,7 +38,7 @@ options.set('theme', 'light'); // 覆盖
38
38
  options.save();
39
39
 
40
40
  // 通过管理器按作用域获取实例(相同作用域返回同一实例)
41
- const scoped = optionsAdmin('app', { file: './data/app_options.json' });
41
+ const scoped = optionAdmin('app', { file: './data/app_options.json' });
42
42
  scoped.set('lang', 'zh');
43
43
 
44
44
  // 配置修改密钥后,修改类操作必须传入正确密钥
@@ -79,7 +79,7 @@ const options = new Options(config);
79
79
 
80
80
  | 函数 | 说明 | 参数 | 返回值 |
81
81
  |---|---|---|---|
82
- | optionsAdmin | 获取指定作用域的动态配置实例,不存在时创建 | scope, config | Options |
82
+ | optionAdmin | 获取指定作用域的动态配置实例,不存在时创建 | scope, config | Options |
83
83
  | admin | 内部 Admin 管理器实例 | - | Admin |
84
84
 
85
85
  ## 使用示例
@@ -162,17 +162,17 @@ console.log(options.list()); // 获取副本
162
162
  ### 4. 作用域工厂
163
163
 
164
164
  ```javascript
165
- const { optionsAdmin } = require('mm_options');
165
+ const { optionAdmin } = require('mm_options');
166
166
 
167
167
  // 相同作用域返回同一实例
168
- const app = optionsAdmin('app', { file: './data/app_options.json', secret: 'app_secret' });
168
+ const app = optionAdmin('app', { file: './data/app_options.json', secret: 'app_secret' });
169
169
  app.set('lang', 'zh', 'app_secret');
170
170
 
171
- const appAgain = optionsAdmin('app');
171
+ const appAgain = optionAdmin('app');
172
172
  console.log(appAgain === app); // true
173
173
 
174
174
  // 缺省作用域为 os
175
- const os_options = optionsAdmin();
175
+ const os_options = optionAdmin();
176
176
  ```
177
177
 
178
178
  ## 注意事项
@@ -182,7 +182,7 @@ const os_options = optionsAdmin();
182
182
  3. 配置了 `secret` 后,`set` / `del` / `clear` / `save` 必须传入正确密钥,否则抛出 `密钥错误,无权修改配置`。
183
183
  4. 未配置 `secret`(默认)时不校验密钥,旧有调用无需改动。
184
184
  5. 密钥校验先于参数校验,密钥错误时优先抛出密钥异常。
185
- 6. `secret` 只在首次创建实例时生效;`optionsAdmin` 对相同作用域复用已有实例,后续传入的 `config` 不会覆盖。
185
+ 6. `secret` 只在首次创建实例时生效;`optionAdmin` 对相同作用域复用已有实例,后续传入的 `config` 不会覆盖。
186
186
  7. 实例仅保存密钥的 SHA-256 摘要且不可逆,密钥遗失后无法从实例还原,只能重新创建实例。
187
187
  8. 持久化文件只包含配置键值,不包含密钥或密钥摘要。
188
188
  9. 密钥以 SHA-256 摘要比对,适用于防误改与权限隔离,不等同于加密存储,敏感配置请另行加密。
package/example/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * mm_options 动态配置存储示例
3
- * 演示 optionsAdmin() 作用域工厂,以及 Options 类运行时
3
+ * 演示 optionAdmin() 作用域工厂,以及 Options 类运行时
4
4
  * 可修改的键值配置存取与 JSON 文件持久化(save / load)。
5
5
  * 运行:node example/index.js
6
6
  */
7
7
  const fs = require('fs');
8
8
  const os = require('os');
9
9
  const path = require('path');
10
- const { Options, optionsAdmin } = require('../index.js');
10
+ const { Options, optionAdmin } = require('../index.js');
11
11
 
12
12
  /** 示例临时目录 */
13
13
  const TMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-options-example-'));
@@ -17,13 +17,13 @@ const TMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-options-example-'));
17
17
  */
18
18
  function main() {
19
19
  // 方式1:作用域工厂,获取 os 作用域的动态配置实例
20
- const store = optionsAdmin('os');
20
+ let store = optionAdmin('os');
21
21
  store.set('theme', 'dark');
22
22
  console.log('工厂实例读取 theme:', store.get('theme'));
23
23
 
24
24
  // 方式2:类实例 + 文件持久化
25
- const file = path.join(TMP_DIR, 'options.json');
26
- const options = new Options({ file });
25
+ let file = path.join(TMP_DIR, 'options.json');
26
+ let options = new Options({ file });
27
27
  options.set('theme', 'dark').set('language', 'zh_cn');
28
28
  console.log('配置列表:', options.list());
29
29
  console.log('保存到文件:', options.save());
@@ -33,7 +33,7 @@ function main() {
33
33
  console.log('动态修改后 theme:', options.get('theme'));
34
34
 
35
35
  // 从文件重新加载(文件里是保存时的 dark)
36
- const loaded = new Options({ file });
36
+ let loaded = new Options({ file });
37
37
  loaded.load();
38
38
  console.log('重新加载后 theme:', loaded.get('theme'));
39
39
 
package/index.js CHANGED
@@ -1,32 +1,13 @@
1
1
  const { reg } = require('mm_admin');
2
2
  const Options = require('./lib/options.js');
3
+ const { optionAdmin, getOptions } = require('./lib/admin.js');
3
4
 
4
- const admin = reg('options');
5
+ let admin = reg('options');
5
6
  admin.setClass(Options);
6
7
 
7
8
  module.exports = {
8
9
  Options,
9
10
  admin,
10
- /**
11
- * 获取指定作用域的动态配置实例,不存在时创建
12
- * @param {string} [scope] 作用域名称,缺省时使用 os
13
- * @param {{file?: string, secret?: string}} [config] 配置对象
14
- * @returns {Options} 动态配置实例
15
- */
16
- optionsAdmin(scope, config) {
17
- const name = scope || 'os';
18
- const existed = admin.has(name);
19
- const options = admin.get(name, config);
20
- if (!existed) {
21
- options.restore();
22
- }
23
- return options;
24
- },
25
- /**
26
- * 获取缺省动态配置实例
27
- * @returns {Options} 动态配置实例
28
- */
29
- getOptions() {
30
- return admin.get('os');
31
- }
11
+ optionAdmin,
12
+ getOptions
32
13
  };
package/lib/admin.js ADDED
@@ -0,0 +1,36 @@
1
+ const { reg } = require('mm_admin');
2
+
3
+ /**
4
+ * 动态配置实例管理(非类文件)
5
+ * optionAdmin / getOptions 获取 mm_admin 'options' 注册池中的动态配置实例。
6
+ */
7
+
8
+ /**
9
+ * 获取指定作用域的动态配置实例,不存在时创建
10
+ * @param {string} [scope] 作用域名称,缺省时使用 os
11
+ * @param {{file?: string, secret?: string}} [config] 配置对象
12
+ * @returns {object} 动态配置实例
13
+ */
14
+ function optionAdmin(scope, config) {
15
+ let admin = reg('options');
16
+ let name = scope || 'os';
17
+ let existed = admin.has(name);
18
+ let options = admin.get(name, config);
19
+ if (!existed) {
20
+ options.restore();
21
+ }
22
+ return options;
23
+ }
24
+
25
+ /**
26
+ * 获取缺省动态配置实例
27
+ * @returns {object} 动态配置实例
28
+ */
29
+ function getOptions() {
30
+ return reg('options').get('os');
31
+ }
32
+
33
+ module.exports = {
34
+ optionAdmin,
35
+ getOptions
36
+ };
package/lib/options.js CHANGED
@@ -8,7 +8,7 @@ const KvStore = require('mm_kv_store').KvStore;
8
8
  */
9
9
  function _hashSecret(secret) {
10
10
  if (typeof secret !== 'string' || secret.length === 0) {
11
- return '';
11
+ return null;
12
12
  }
13
13
  return crypto.createHash('sha256').update(secret).digest('hex');
14
14
  }
@@ -31,18 +31,22 @@ class Options extends KvStore {
31
31
  }
32
32
 
33
33
  /**
34
- * 密钥摘要
35
- * @type {string}
34
+ * 密钥摘要,null 表示未配置密钥
35
+ * @type {string|null}
36
36
  */
37
- #secret_hash = '';
37
+ #secret_hash = null;
38
38
 
39
39
  /**
40
40
  * 构造函数
41
41
  * @param {{file?: string, secret?: string}} [config] 配置对象
42
42
  */
43
43
  constructor(config = {}) {
44
- const { secret, ...rest_config } = { ...Options.config, ...config };
44
+ let { secret, ...rest_config } = { ...Options.config, ...config };
45
+ if (secret !== undefined && secret !== null && typeof secret !== 'string') {
46
+ throw new TypeError('secret 必须是字符串');
47
+ }
45
48
  super(rest_config);
49
+ // 空串密钥视为未配置(与缺省一致),内部以 null 为"无密钥"哨兵
46
50
  this.#secret_hash = _hashSecret(secret);
47
51
  }
48
52
 
@@ -53,7 +57,7 @@ class Options extends KvStore {
53
57
  * @throws {Error} 已配置密钥且传入密钥不一致时抛出
54
58
  */
55
59
  _checkSecret(secret) {
56
- if (!this.#secret_hash) {
60
+ if (this.#secret_hash === null) {
57
61
  return;
58
62
  }
59
63
  if (_hashSecret(secret) !== this.#secret_hash) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_options",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "动态配置存储模块:适用于站长/用户配置,可在运行时动态修改",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -3,7 +3,7 @@
3
3
  * option —— 动态配置存取工具
4
4
  * 复用模块 Options 实例的读写与落盘能力。
5
5
  */
6
- const { optionsAdmin } = require('../../index.js');
6
+ const { optionAdmin } = require('../../index.js');
7
7
 
8
8
  /**
9
9
  * 落盘:未配置持久化文件时只保留内存结果
@@ -11,7 +11,7 @@ const { optionsAdmin } = require('../../index.js');
11
11
  * @returns {boolean} 是否已落盘
12
12
  */
13
13
  function _save(store) {
14
- const file = (store.config && store.config.file) || '';
14
+ let file = (store.config && store.config.file) || '';
15
15
  if (!file) return false;
16
16
  store.save();
17
17
  return true;
@@ -19,12 +19,12 @@ function _save(store) {
19
19
 
20
20
  async function main(params, ctx, options) {
21
21
  void options;
22
- const p = params || {};
23
- const action = p.action || 'list';
22
+ let p = params || {};
23
+ let action = p.action || 'list';
24
24
  const done = (data) => { ctx.output = data; return data; };
25
25
 
26
26
  try {
27
- const store = optionsAdmin(p.scope || 'os');
27
+ let store = optionAdmin(p.scope || 'os');
28
28
 
29
29
  if (action === 'set') {
30
30
  if (!p.key) throw new TypeError('key 不能为空');
@@ -40,13 +40,13 @@ async function main(params, ctx, options) {
40
40
 
41
41
  if (action === 'del') {
42
42
  if (!p.key) throw new TypeError('key 不能为空');
43
- const deleted = store.del(p.key);
43
+ let deleted = store.del(p.key);
44
44
  _save(store);
45
45
  return done({ code: 0, msg: deleted ? '删除成功' : '配置不存在', key: p.key, deleted: !!deleted });
46
46
  }
47
47
 
48
48
  if (action === 'list') {
49
- const list = store.list();
49
+ let list = store.list();
50
50
  return done({ code: 0, msg: '成功', list, count: Object.keys(list || {}).length });
51
51
  }
52
52
 
@@ -8,21 +8,21 @@ const { admin } = require('../../index.js');
8
8
  * @returns {Promise<object>} 执行结果
9
9
  */
10
10
  async function main(params, ctx) {
11
- const p = params || {};
12
- const action = p.action || 'list';
13
- const scope = p.scope || 'os';
11
+ let p = params || {};
12
+ let action = p.action || 'list';
13
+ let scope = p.scope || 'os';
14
14
  const done = (data) => {
15
15
  if (ctx) ctx.output = data;
16
16
  return data;
17
17
  };
18
18
  try {
19
19
  if (action === 'list') {
20
- const scopes = [...admin.keys()];
20
+ let scopes = [...admin.keys()];
21
21
  return done({ code: 0, msg: '获取成功', action, count: scopes.length, scopes });
22
22
  }
23
23
  if (action === 'inspect') {
24
- const exists = admin.has(scope);
25
- const instance = exists ? admin.get(scope) : null;
24
+ let exists = admin.has(scope);
25
+ let instance = exists ? admin.get(scope) : null;
26
26
  return done({ code: 0, msg: exists ? '获取成功' : '实例不存在', action, scope, exists, config: instance && instance.config ? instance.config : null });
27
27
  }
28
28
  if (action === 'del') return done({ code: 0, msg: '删除成功', action, scope, ok: admin.del(scope) });