mm_options 1.0.0 → 1.0.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/LICENSE +15 -0
- package/README.md +169 -13
- package/example/index.js +44 -0
- package/index.js +1 -1
- package/lib/options.js +105 -3
- package/package.json +18 -3
- package/test/options.test.js +0 -169
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 qww
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,47 +1,203 @@
|
|
|
1
1
|
# 动态配置管理器(mm_options)
|
|
2
|
+
|
|
2
3
|
## 说明
|
|
4
|
+
|
|
3
5
|
mm_options 是动态配置存储模块,适用于站长 / 用户配置,可在运行时动态修改。
|
|
4
6
|
与固定配置(mm_config)不同,其配置项可随时覆盖、删除。
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
模块基于 `mm_kv_store` 的键值存储基类实现,
|
|
9
|
+
在保留 `set` / `get` / `del` / `has` / `list` / `clear` / `save` / `load` 统一调用方式的基础上,
|
|
10
|
+
增加了修改密钥校验,避免配置被非授权改写。
|
|
11
|
+
|
|
12
|
+
## 功能特点
|
|
13
|
+
|
|
14
|
+
- 键名为非空字符串,值可为任意类型
|
|
8
15
|
- 支持覆盖已有值、删除、判断存在、获取副本、清空
|
|
16
|
+
- 支持链式调用 `set`、`clear`
|
|
9
17
|
- 支持保存到 JSON 文件与从 JSON 文件加载
|
|
18
|
+
- 支持配置修改密钥,密钥正确才能修改配置
|
|
19
|
+
- 未配置密钥时不校验,保持向后兼容
|
|
20
|
+
- 密钥不进入 `config`,实例只保留 SHA-256 摘要(私有字段),不会写入持久化文件
|
|
21
|
+
- 基于 mm_corejs 的 Admin 管理器,支持按作用域缓存实例
|
|
10
22
|
|
|
11
23
|
## 安装
|
|
24
|
+
|
|
12
25
|
```bash
|
|
13
26
|
npm install mm_options
|
|
14
27
|
```
|
|
15
28
|
|
|
16
29
|
## 快速开始
|
|
30
|
+
|
|
17
31
|
```javascript
|
|
18
32
|
const { Options, optionsAdmin } = require('mm_options');
|
|
19
33
|
|
|
20
34
|
// 直接使用存储类
|
|
21
|
-
const options = new Options('./data/options.json');
|
|
35
|
+
const options = new Options({ file: './data/options.json' });
|
|
22
36
|
options.set('theme', 'dark').set('size', 12);
|
|
23
37
|
options.set('theme', 'light'); // 覆盖
|
|
24
38
|
options.save();
|
|
25
39
|
|
|
26
40
|
// 通过管理器按作用域获取实例(相同作用域返回同一实例)
|
|
27
|
-
const scoped = optionsAdmin('app', './data/app_options.json');
|
|
41
|
+
const scoped = optionsAdmin('app', { file: './data/app_options.json' });
|
|
28
42
|
scoped.set('lang', 'zh');
|
|
43
|
+
|
|
44
|
+
// 配置修改密钥后,修改类操作必须传入正确密钥
|
|
45
|
+
const secured = new Options({ file: './data/options.json', secret: 'my_secret' });
|
|
46
|
+
secured.set('theme', 'dark', 'my_secret'); // 成功
|
|
47
|
+
secured.set('theme', 'light', 'wrong'); // 抛出:密钥错误,无权修改配置
|
|
48
|
+
secured.save('my_secret');
|
|
29
49
|
```
|
|
30
50
|
|
|
31
51
|
## API
|
|
32
|
-
|
|
52
|
+
|
|
53
|
+
### 构造函数
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
/**
|
|
57
|
+
* 创建动态配置实例
|
|
58
|
+
* @param {object} [config] 配置对象
|
|
59
|
+
* @param {string} [config.file] 持久化文件路径,默认空字符串
|
|
60
|
+
* @param {string} [config.secret] 修改密钥,默认空字符串(不校验)
|
|
61
|
+
*/
|
|
62
|
+
const options = new Options(config);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 类方法
|
|
66
|
+
|
|
67
|
+
| 方法 | 说明 | 参数 | 返回值 | 需密钥 |
|
|
68
|
+
|---|---|---|---|---|
|
|
69
|
+
| set | 设置配置,可覆盖 | key, value, secret? | Options | 是 |
|
|
70
|
+
| get | 获取配置 | key | * | 否 |
|
|
71
|
+
| del | 删除配置 | key, secret? | boolean | 是 |
|
|
72
|
+
| has | 判断配置是否存在 | key | boolean | 否 |
|
|
73
|
+
| list | 获取配置表副本 | - | object | 否 |
|
|
74
|
+
| clear | 清空所有配置 | secret? | Options | 是 |
|
|
75
|
+
| save | 保存到持久化文件 | secret? | boolean | 是 |
|
|
76
|
+
| load | 从持久化文件加载 | - | boolean | 否 |
|
|
77
|
+
|
|
78
|
+
### 工厂函数
|
|
79
|
+
|
|
80
|
+
| 函数 | 说明 | 参数 | 返回值 |
|
|
33
81
|
|---|---|---|---|
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
82
|
+
| optionsAdmin | 获取指定作用域的动态配置实例,不存在时创建 | scope, config | Options |
|
|
83
|
+
| admin | 内部 Admin 管理器实例 | - | Admin |
|
|
84
|
+
|
|
85
|
+
## 使用示例
|
|
86
|
+
|
|
87
|
+
### 1. 基本读写
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
const { Options } = require('mm_options');
|
|
91
|
+
|
|
92
|
+
const options = new Options();
|
|
93
|
+
options.set('theme', 'dark');
|
|
94
|
+
options.set('size', 12);
|
|
95
|
+
|
|
96
|
+
console.log(options.get('theme')); // "dark"
|
|
97
|
+
console.log(options.has('size')); // true
|
|
98
|
+
console.log(options.list()); // { theme: 'dark', size: 12 }
|
|
99
|
+
console.log(options.del('size')); // true
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`set` 与 `clear` 支持链式调用:
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
const options = new Options();
|
|
106
|
+
|
|
107
|
+
options.set('a', 1).set('b', 2);
|
|
108
|
+
console.log(options.list()); // { a: 1, b: 2 }
|
|
109
|
+
options.clear();
|
|
110
|
+
console.log(options.list()); // {}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 2. 文件持久化
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
const { Options } = require('mm_options');
|
|
117
|
+
|
|
118
|
+
const file = './data/options.json';
|
|
119
|
+
const options = new Options({ file });
|
|
120
|
+
options.set('theme', 'dark').set('language', 'zh_cn');
|
|
121
|
+
options.save(); // true
|
|
122
|
+
|
|
123
|
+
// 从文件重新加载
|
|
124
|
+
const loaded = new Options({ file });
|
|
125
|
+
loaded.load();
|
|
126
|
+
console.log(loaded.get('theme')); // "dark"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 3. 修改密钥保护
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
const { Options } = require('mm_options');
|
|
133
|
+
|
|
134
|
+
const options = new Options({ file: './data/options.json', secret: 'my_secret' });
|
|
135
|
+
|
|
136
|
+
// 密钥正确,修改成功
|
|
137
|
+
options.set('theme', 'dark', 'my_secret');
|
|
138
|
+
|
|
139
|
+
// 密钥缺失或错误,抛出异常且数据不变
|
|
140
|
+
try {
|
|
141
|
+
options.set('theme', 'light', 'wrong');
|
|
142
|
+
} catch (err) {
|
|
143
|
+
console.error(err.message); // 密钥错误,无权修改配置
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 删除、清空、保存同样需要密钥
|
|
147
|
+
options.del('theme', 'my_secret');
|
|
148
|
+
options.save('my_secret');
|
|
149
|
+
options.clear('my_secret');
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
只读操作无需密钥,可在无密钥场景下安全暴露:
|
|
153
|
+
|
|
154
|
+
```javascript
|
|
155
|
+
const options = new Options({ secret: 'my_secret' });
|
|
156
|
+
|
|
157
|
+
console.log(options.get('theme')); // 直接读取
|
|
158
|
+
console.log(options.has('theme')); // 判断存在
|
|
159
|
+
console.log(options.list()); // 获取副本
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 4. 作用域工厂
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const { optionsAdmin } = require('mm_options');
|
|
166
|
+
|
|
167
|
+
// 相同作用域返回同一实例
|
|
168
|
+
const app = optionsAdmin('app', { file: './data/app_options.json', secret: 'app_secret' });
|
|
169
|
+
app.set('lang', 'zh', 'app_secret');
|
|
170
|
+
|
|
171
|
+
const appAgain = optionsAdmin('app');
|
|
172
|
+
console.log(appAgain === app); // true
|
|
173
|
+
|
|
174
|
+
// 缺省作用域为 os
|
|
175
|
+
const os_options = optionsAdmin();
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## 注意事项
|
|
179
|
+
|
|
180
|
+
1. `save` 前必须配置 `file`,否则抛出 `未配置持久化文件路径`。
|
|
181
|
+
2. `load` 时文件不存在返回 `false`,不会抛错。
|
|
182
|
+
3. 配置了 `secret` 后,`set` / `del` / `clear` / `save` 必须传入正确密钥,否则抛出 `密钥错误,无权修改配置`。
|
|
183
|
+
4. 未配置 `secret`(默认)时不校验密钥,旧有调用无需改动。
|
|
184
|
+
5. 密钥校验先于参数校验,密钥错误时优先抛出密钥异常。
|
|
185
|
+
6. `secret` 只在首次创建实例时生效;`optionsAdmin` 对相同作用域复用已有实例,后续传入的 `config` 不会覆盖。
|
|
186
|
+
7. 实例仅保存密钥的 SHA-256 摘要且不可逆,密钥遗失后无法从实例还原,只能重新创建实例。
|
|
187
|
+
8. 持久化文件只包含配置键值,不包含密钥或密钥摘要。
|
|
188
|
+
9. 密钥以 SHA-256 摘要比对,适用于防误改与权限隔离,不等同于加密存储,敏感配置请另行加密。
|
|
42
189
|
|
|
43
190
|
## 依赖
|
|
191
|
+
|
|
44
192
|
- mm_corejs:提供 Admin 管理器
|
|
193
|
+
- mm_kv_store:提供键值存储基类
|
|
194
|
+
|
|
195
|
+
## 测试
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
npm test
|
|
199
|
+
```
|
|
45
200
|
|
|
46
201
|
## 许可证
|
|
202
|
+
|
|
47
203
|
ISC
|
package/example/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mm_options 动态配置存储示例
|
|
3
|
+
* 演示 optionsAdmin() 作用域工厂,以及 Options 类运行时
|
|
4
|
+
* 可修改的键值配置存取与 JSON 文件持久化(save / load)。
|
|
5
|
+
* 运行:node example/index.js
|
|
6
|
+
*/
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const os = require('os');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const { Options, optionsAdmin } = require('../index.js');
|
|
11
|
+
|
|
12
|
+
/** 示例临时目录 */
|
|
13
|
+
const TMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-options-example-'));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 主流程
|
|
17
|
+
*/
|
|
18
|
+
function main() {
|
|
19
|
+
// 方式1:作用域工厂,获取 os 作用域的动态配置实例
|
|
20
|
+
const store = optionsAdmin('os');
|
|
21
|
+
store.set('theme', 'dark');
|
|
22
|
+
console.log('工厂实例读取 theme:', store.get('theme'));
|
|
23
|
+
|
|
24
|
+
// 方式2:类实例 + 文件持久化
|
|
25
|
+
const file = path.join(TMP_DIR, 'options.json');
|
|
26
|
+
const options = new Options({ file });
|
|
27
|
+
options.set('theme', 'dark').set('language', 'zh_cn');
|
|
28
|
+
console.log('配置列表:', options.list());
|
|
29
|
+
console.log('保存到文件:', options.save());
|
|
30
|
+
|
|
31
|
+
// 运行时动态修改
|
|
32
|
+
options.set('theme', 'light');
|
|
33
|
+
console.log('动态修改后 theme:', options.get('theme'));
|
|
34
|
+
|
|
35
|
+
// 从文件重新加载(文件里是保存时的 dark)
|
|
36
|
+
const loaded = new Options({ file });
|
|
37
|
+
loaded.load();
|
|
38
|
+
console.log('重新加载后 theme:', loaded.get('theme'));
|
|
39
|
+
|
|
40
|
+
// 清理临时目录
|
|
41
|
+
fs.rmSync(TMP_DIR, { recursive: true, force: true });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
main();
|
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ module.exports = {
|
|
|
10
10
|
/**
|
|
11
11
|
* 获取指定作用域的动态配置实例,不存在时创建
|
|
12
12
|
* @param {string} [scope] 作用域名称,缺省时使用 os
|
|
13
|
-
* @param {string} [config]
|
|
13
|
+
* @param {{file?: string, secret?: string}} [config] 配置对象
|
|
14
14
|
* @returns {Options} 动态配置实例
|
|
15
15
|
*/
|
|
16
16
|
optionsAdmin(scope, config) {
|
package/lib/options.js
CHANGED
|
@@ -1,10 +1,112 @@
|
|
|
1
|
-
const
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const KvStore = require('mm_kv_store').KvStore;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 计算密钥摘要,空值返回空字符串
|
|
6
|
+
* @param {*} secret 原始密钥
|
|
7
|
+
* @returns {string} SHA-256 十六进制摘要,密钥为空时返回空字符串
|
|
8
|
+
*/
|
|
9
|
+
function _hashSecret(secret) {
|
|
10
|
+
if (typeof secret !== 'string' || secret.length === 0) {
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
return crypto.createHash('sha256').update(secret).digest('hex');
|
|
14
|
+
}
|
|
2
15
|
|
|
3
16
|
/**
|
|
4
17
|
* 动态配置存储类,适用于站长或用户配置,可在运行时动态修改
|
|
18
|
+
* 配置密钥 secret 后,修改类操作(set / del / clear / save)必须传入正确密钥
|
|
19
|
+
* 密钥仅以摘要形式保存在私有字段中,不会出现在 config 或持久化数据里
|
|
5
20
|
* @class
|
|
6
21
|
* @augments {KvStore}
|
|
7
22
|
*/
|
|
8
|
-
class Options extends KvStore {
|
|
23
|
+
class Options extends KvStore {
|
|
24
|
+
/**
|
|
25
|
+
* 配置对象
|
|
26
|
+
* @type {{file: string, secret: string}}
|
|
27
|
+
*/
|
|
28
|
+
static config = {
|
|
29
|
+
file: '',
|
|
30
|
+
secret: ''
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 密钥摘要
|
|
35
|
+
* @type {string}
|
|
36
|
+
*/
|
|
37
|
+
#secret_hash = '';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 构造函数
|
|
41
|
+
* @param {{file?: string, secret?: string}} [config] 配置对象
|
|
42
|
+
*/
|
|
43
|
+
constructor(config = {}) {
|
|
44
|
+
const { secret, ...rest_config } = { ...Options.config, ...config };
|
|
45
|
+
super(rest_config);
|
|
46
|
+
this.#secret_hash = _hashSecret(secret);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 校验修改密钥,未配置密钥时跳过校验
|
|
51
|
+
* @param {string} [secret] 修改密钥
|
|
52
|
+
* @returns {void}
|
|
53
|
+
* @throws {Error} 已配置密钥且传入密钥不一致时抛出
|
|
54
|
+
*/
|
|
55
|
+
_checkSecret(secret) {
|
|
56
|
+
if (!this.#secret_hash) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (_hashSecret(secret) !== this.#secret_hash) {
|
|
60
|
+
throw new Error('密钥错误,无权修改配置');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 设置键值项,需通过密钥校验
|
|
66
|
+
* @param {*} key 键名
|
|
67
|
+
* @param {*} value 值
|
|
68
|
+
* @param {string} [secret] 修改密钥
|
|
69
|
+
* @returns {Options} 当前实例
|
|
70
|
+
* @throws {Error} 密钥不正确时抛出
|
|
71
|
+
*/
|
|
72
|
+
set(key, value, secret) {
|
|
73
|
+
this._checkSecret(secret);
|
|
74
|
+
return super.set(key, value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 删除键值项,需通过密钥校验
|
|
79
|
+
* @param {*} key 键名
|
|
80
|
+
* @param {string} [secret] 修改密钥
|
|
81
|
+
* @returns {boolean} 是否删除成功
|
|
82
|
+
* @throws {Error} 密钥不正确时抛出
|
|
83
|
+
*/
|
|
84
|
+
del(key, secret) {
|
|
85
|
+
this._checkSecret(secret);
|
|
86
|
+
return super.del(key);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 清空所有键值项,需通过密钥校验
|
|
91
|
+
* @param {string} [secret] 修改密钥
|
|
92
|
+
* @returns {Options} 当前实例
|
|
93
|
+
* @throws {Error} 密钥不正确时抛出
|
|
94
|
+
*/
|
|
95
|
+
clear(secret) {
|
|
96
|
+
this._checkSecret(secret);
|
|
97
|
+
return super.clear();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 将键值表保存到持久化文件,需通过密钥校验
|
|
102
|
+
* @param {string} [secret] 修改密钥
|
|
103
|
+
* @returns {boolean} 是否保存成功
|
|
104
|
+
* @throws {Error} 密钥不正确或未配置持久化文件路径时抛出
|
|
105
|
+
*/
|
|
106
|
+
save(secret) {
|
|
107
|
+
this._checkSecret(secret);
|
|
108
|
+
return super.save();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
9
111
|
|
|
10
|
-
module.exports = Options;
|
|
112
|
+
module.exports = Options;
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_options",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "动态配置存储模块:适用于站长/用户配置,可在运行时动态修改",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_options.git"
|
|
9
|
+
},
|
|
6
10
|
"keywords": [
|
|
7
11
|
"mm_mod",
|
|
8
12
|
"mm_infra",
|
|
@@ -19,9 +23,20 @@
|
|
|
19
23
|
"test": "jest --coverage"
|
|
20
24
|
},
|
|
21
25
|
"dependencies": {
|
|
22
|
-
"mm_corejs": "^1.1.4"
|
|
26
|
+
"mm_corejs": "^1.1.4",
|
|
27
|
+
"mm_kv_store": "^1.0.0"
|
|
23
28
|
},
|
|
24
29
|
"devDependencies": {
|
|
25
30
|
"jest": "^30.5.2"
|
|
26
|
-
}
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14.0.0"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"index.js",
|
|
37
|
+
"lib",
|
|
38
|
+
"example",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
]
|
|
27
42
|
}
|
package/test/options.test.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const os = require('os');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const mod = require('../index');
|
|
7
|
-
const Options = require('../lib/options');
|
|
8
|
-
|
|
9
|
-
describe('mm_options 模块入口', () => {
|
|
10
|
-
it('导出 Options 类与管理器', () => {
|
|
11
|
-
expect(mod.Options).toBe(Options);
|
|
12
|
-
expect(mod.admin).toBeDefined();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('optionsAdmin 按作用域缓存并返回实例', () => {
|
|
16
|
-
const first = mod.optionsAdmin('options_scope');
|
|
17
|
-
expect(first).toBeInstanceOf(Options);
|
|
18
|
-
expect(mod.optionsAdmin('options_scope')).toBe(first);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('optionsAdmin 缺省作用域为 os', () => {
|
|
22
|
-
expect(mod.optionsAdmin()).toBeInstanceOf(Options);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('Options', () => {
|
|
27
|
-
let tmp_dir;
|
|
28
|
-
|
|
29
|
-
beforeEach(() => {
|
|
30
|
-
tmp_dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-options-'));
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
afterEach(() => {
|
|
34
|
-
fs.rmSync(tmp_dir, { recursive: true, force: true });
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe('构造', () => {
|
|
38
|
-
it('默认不传文件路径时 file 为空字符串', () => {
|
|
39
|
-
const options = new Options();
|
|
40
|
-
expect(options._file).toBe('');
|
|
41
|
-
expect(options.list()).toEqual({});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('传入文件路径时保存路径', () => {
|
|
45
|
-
const file = path.join(tmp_dir, 'options.json');
|
|
46
|
-
const options = new Options(file);
|
|
47
|
-
expect(options._file).toBe(file);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('set / get', () => {
|
|
52
|
-
it('set 后可通过 get 读取,并支持链式调用', () => {
|
|
53
|
-
const options = new Options();
|
|
54
|
-
expect(options.set('theme', 'dark')).toBe(options);
|
|
55
|
-
expect(options.get('theme')).toBe('dark');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('set 可覆盖已有值', () => {
|
|
59
|
-
const options = new Options();
|
|
60
|
-
options.set('theme', 'dark');
|
|
61
|
-
options.set('theme', 'light');
|
|
62
|
-
expect(options.get('theme')).toBe('light');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('get 不存在的键返回 undefined', () => {
|
|
66
|
-
const options = new Options();
|
|
67
|
-
expect(options.get('nope')).toBeUndefined();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('set 非字符串键抛 TypeError', () => {
|
|
71
|
-
const options = new Options();
|
|
72
|
-
expect(() => options.set(123, 'x')).toThrow('键名必须是非空字符串');
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('set 空字符串键抛 TypeError', () => {
|
|
76
|
-
const options = new Options();
|
|
77
|
-
expect(() => options.set('', 'x')).toThrow('键名必须是非空字符串');
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('get 非字符串键抛 TypeError', () => {
|
|
81
|
-
const options = new Options();
|
|
82
|
-
expect(() => options.get(null)).toThrow('键名必须是非空字符串');
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('del / has', () => {
|
|
87
|
-
it('del 删除存在的键返回 true', () => {
|
|
88
|
-
const options = new Options();
|
|
89
|
-
options.set('a', 1);
|
|
90
|
-
expect(options.del('a')).toBe(true);
|
|
91
|
-
expect(options.has('a')).toBe(false);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('del 不存在的键返回 false', () => {
|
|
95
|
-
const options = new Options();
|
|
96
|
-
expect(options.del('nope')).toBe(false);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('del 非字符串键抛 TypeError', () => {
|
|
100
|
-
const options = new Options();
|
|
101
|
-
expect(() => options.del('')).toThrow('键名必须是非空字符串');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('has 存在返回 true,不存在返回 false', () => {
|
|
105
|
-
const options = new Options();
|
|
106
|
-
options.set('a', 1);
|
|
107
|
-
expect(options.has('a')).toBe(true);
|
|
108
|
-
expect(options.has('b')).toBe(false);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('has 非字符串键抛 TypeError', () => {
|
|
112
|
-
const options = new Options();
|
|
113
|
-
expect(() => options.has(undefined)).toThrow('键名必须是非空字符串');
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('list / clear', () => {
|
|
118
|
-
it('list 返回副本,不引用内部对象', () => {
|
|
119
|
-
const options = new Options();
|
|
120
|
-
options.set('a', 1);
|
|
121
|
-
const list = options.list();
|
|
122
|
-
expect(list).toEqual({ a: 1 });
|
|
123
|
-
list.a = 99;
|
|
124
|
-
expect(options.get('a')).toBe(1);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('clear 清空所有配置项', () => {
|
|
128
|
-
const options = new Options();
|
|
129
|
-
options.set('a', 1);
|
|
130
|
-
options.set('b', 2);
|
|
131
|
-
expect(options.clear()).toBe(options);
|
|
132
|
-
expect(options.list()).toEqual({});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe('save / load', () => {
|
|
137
|
-
it('未配置文件路径时 save 抛 Error', () => {
|
|
138
|
-
const options = new Options();
|
|
139
|
-
expect(() => options.save()).toThrow('未配置持久化文件路径');
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('save 写入文件返回 true', () => {
|
|
143
|
-
const file = path.join(tmp_dir, 'options.json');
|
|
144
|
-
const options = new Options(file);
|
|
145
|
-
options.set('a', 1);
|
|
146
|
-
expect(options.save()).toBe(true);
|
|
147
|
-
expect(JSON.parse(fs.readFileSync(file, 'utf8'))).toEqual({ a: 1 });
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('未配置文件路径时 load 返回 false', () => {
|
|
151
|
-
const options = new Options();
|
|
152
|
-
expect(options.load()).toBe(false);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('文件不存在时 load 返回 false', () => {
|
|
156
|
-
const options = new Options(path.join(tmp_dir, 'nope.json'));
|
|
157
|
-
expect(options.load()).toBe(false);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('文件存在时 load 加载数据返回 true', () => {
|
|
161
|
-
const file = path.join(tmp_dir, 'options.json');
|
|
162
|
-
fs.writeFileSync(file, JSON.stringify({ a: 1, b: 'x' }));
|
|
163
|
-
const options = new Options(file);
|
|
164
|
-
expect(options.load()).toBe(true);
|
|
165
|
-
expect(options.get('a')).toBe(1);
|
|
166
|
-
expect(options.get('b')).toBe('x');
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
});
|