mm_container 1.0.2 → 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 +2 -2
- package/index.js +7 -6
- package/lib/tools/container/index.js +82 -0
- package/lib/tools/index.js +16 -0
- package/package.json +5 -4
- package/tool/container/index.js +7 -0
- package/tool/container/tool.json +81 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
## 项目信息
|
|
6
6
|
|
|
7
7
|
- **当前版本**: 1.0.0
|
|
8
|
-
- **依赖**:
|
|
8
|
+
- **依赖**: mm_admin(Admin 实例管理与全局注册表)
|
|
9
9
|
- **运行环境**: Node.js(使用私有字段 `#map`,需 Node.js 12+)
|
|
10
10
|
- **Gitee地址**: https://gitee.com/qiuwenwu91/mm_container
|
|
11
11
|
|
|
@@ -83,7 +83,7 @@ console.log(cache_container === os_container); // false
|
|
|
83
83
|
| 导出 | 签名 | 返回值 | 说明 |
|
|
84
84
|
| --- | --- | --- | --- |
|
|
85
85
|
| Container | `Container` | `Class` | 容器类,见下文 |
|
|
86
|
-
| admin | `admin` | `Admin` |
|
|
86
|
+
| admin | `admin` | `Admin` | mm_admin 全局注册表中名为 `mm_container` 的实例管理器,已绑定 `Container` |
|
|
87
87
|
| containerAdmin | `containerAdmin(scope?, config?)` | `Container` | 获取指定作用域的容器实例,`scope` 为空时使用默认作用域 `'os'`。实例按作用域缓存,**配置仅在首次创建时生效**,`config.name` 始终等于作用域名 |
|
|
88
88
|
|
|
89
89
|
> 需要跨模块共享同一个容器时,统一使用 `containerAdmin('作用域名')`,避免各自 `new Container()` 造成数据不互通。
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const Container = require('./lib/container');
|
|
4
|
-
const {
|
|
4
|
+
const { reg } = require('mm_admin');
|
|
5
5
|
|
|
6
|
-
const admin =
|
|
6
|
+
const admin = reg('mm_container');
|
|
7
7
|
admin.setClass(Container);
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -14,10 +14,7 @@ admin.setClass(Container);
|
|
|
14
14
|
* @returns {Container} 容器实例
|
|
15
15
|
*/
|
|
16
16
|
function containerAdmin(scope, config) {
|
|
17
|
-
|
|
18
|
-
scope = 'os';
|
|
19
|
-
}
|
|
20
|
-
return admin.get(scope, config);
|
|
17
|
+
return admin.get(scope || 'os', config);
|
|
21
18
|
}
|
|
22
19
|
|
|
23
20
|
module.exports = {
|
|
@@ -25,3 +22,7 @@ module.exports = {
|
|
|
25
22
|
admin,
|
|
26
23
|
containerAdmin
|
|
27
24
|
};
|
|
25
|
+
|
|
26
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
|
|
27
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
28
|
+
get: function () { return require('./lib/tools'); } });
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内存容器管理器
|
|
3
|
+
* 基于 containerAdmin 作用域单例存取对象值,进程内存活、不落盘
|
|
4
|
+
* @param {object} params 步骤参数
|
|
5
|
+
* @param {object} ctx 上下文对象
|
|
6
|
+
* @returns {object} 操作结果
|
|
7
|
+
*/
|
|
8
|
+
const { containerAdmin } = require('../../../index.js');
|
|
9
|
+
|
|
10
|
+
async function main(params, ctx, options) {
|
|
11
|
+
void options;
|
|
12
|
+
var action = params.action || 'get';
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
var container = containerAdmin(params.scope || 'os');
|
|
16
|
+
|
|
17
|
+
switch (action) {
|
|
18
|
+
case 'set': {
|
|
19
|
+
_assertKey(params.key);
|
|
20
|
+
container.set(params.key, params.value);
|
|
21
|
+
ctx.output = { key: params.key, msg: '设置成功' };
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
case 'get': {
|
|
26
|
+
_assertKey(params.key);
|
|
27
|
+
ctx.output = {
|
|
28
|
+
key: params.key,
|
|
29
|
+
value: container.get(params.key),
|
|
30
|
+
exists: container.has(params.key)
|
|
31
|
+
};
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
case 'has': {
|
|
36
|
+
_assertKey(params.key);
|
|
37
|
+
ctx.output = { key: params.key, exists: container.has(params.key) };
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
case 'del': {
|
|
42
|
+
_assertKey(params.key);
|
|
43
|
+
var deleted = container.del(params.key);
|
|
44
|
+
ctx.output = { key: params.key, deleted: deleted, msg: deleted ? '删除成功' : '键不存在' };
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case 'list':
|
|
49
|
+
ctx.output = { list: container.list(), size: container.size };
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
case 'clear':
|
|
53
|
+
container.clear();
|
|
54
|
+
ctx.output = { msg: '清空成功' };
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
default:
|
|
58
|
+
ctx.output = {
|
|
59
|
+
error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {
|
|
63
|
+
ctx.output = {
|
|
64
|
+
error: { code: 'CONTAINER_ERROR', message: e.message }
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return ctx.output;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 校验键名
|
|
73
|
+
* @param {string} key 键名
|
|
74
|
+
* @returns {void}
|
|
75
|
+
*/
|
|
76
|
+
function _assertKey(key) {
|
|
77
|
+
if (typeof key !== 'string' || key.length === 0) {
|
|
78
|
+
throw new TypeError('缺少 key 键名参数');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = { main };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
/** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
|
|
5
|
+
module.exports = new Proxy({}, {
|
|
6
|
+
get(_t, key) {
|
|
7
|
+
if (typeof key !== 'string') return undefined;
|
|
8
|
+
if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
|
|
9
|
+
return require('./' + key);
|
|
10
|
+
},
|
|
11
|
+
has(_t, key) {
|
|
12
|
+
return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
|
|
13
|
+
},
|
|
14
|
+
ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
|
|
15
|
+
getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
|
|
16
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_container",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "容器工具,基于 Map 提供语义化的键值存取(set/get/has/del/clear/list),并保存容器配置与服务器实例。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mm_mod",
|
|
@@ -11,14 +11,15 @@
|
|
|
11
11
|
"键值存储",
|
|
12
12
|
"container"
|
|
13
13
|
],
|
|
14
|
-
"layer": "
|
|
15
|
-
"group": "
|
|
14
|
+
"layer": "shared",
|
|
15
|
+
"group": "kernel",
|
|
16
16
|
"author": "邱文武",
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"main": "index.js",
|
|
19
19
|
"files": [
|
|
20
20
|
"index.js",
|
|
21
21
|
"lib",
|
|
22
|
+
"tool",
|
|
22
23
|
"example"
|
|
23
24
|
],
|
|
24
25
|
"directories": {
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
"prepublishOnly": "npm test"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"
|
|
35
|
+
"mm_admin": "^1.1.0"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
38
|
"jest": "^30.5.2"
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "container",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "内存容器管理器",
|
|
5
|
+
"description": "按作用域共享的内存键值容器,仅存储对象值,进程内存活、不落盘。适用场景:工作流步骤间传递对象引用、缓存临时句柄。工作流提示:action 选 set/get/has/del/list/clear,key 指定键名,set 需传 value 对象(不能是字符串/数字等基础值),结果 get 在 value 与 exists,list 在 list 与 size。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"params": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "操作类型: set(写入)/get(读取)/has(判断存在)/del(删除)/list(列出全部值)/clear(清空)",
|
|
13
|
+
"enum": [
|
|
14
|
+
"set",
|
|
15
|
+
"get",
|
|
16
|
+
"has",
|
|
17
|
+
"del",
|
|
18
|
+
"list",
|
|
19
|
+
"clear"
|
|
20
|
+
],
|
|
21
|
+
"default": "get"
|
|
22
|
+
},
|
|
23
|
+
"scope": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "容器实例作用域,同作用域共享同一容器,缺省 os",
|
|
26
|
+
"default": "os"
|
|
27
|
+
},
|
|
28
|
+
"key": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"description": "键名(set/get/has/del时必填)"
|
|
31
|
+
},
|
|
32
|
+
"value": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"description": "键值对象(set时必填,只能是对象,不能是基础值或函数)"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"required": [
|
|
38
|
+
"action"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"returns": {
|
|
42
|
+
"description": "get 返回 value 与 exists,has 返回 exists,del 返回 deleted,list 返回 list 与 size,set/clear 返回 msg",
|
|
43
|
+
"type": "object",
|
|
44
|
+
"properties": {
|
|
45
|
+
"key": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"description": "键名"
|
|
48
|
+
},
|
|
49
|
+
"value": {
|
|
50
|
+
"type": "object",
|
|
51
|
+
"description": "读取到的对象(get操作)"
|
|
52
|
+
},
|
|
53
|
+
"exists": {
|
|
54
|
+
"type": "boolean",
|
|
55
|
+
"description": "键是否存在(get/has操作)"
|
|
56
|
+
},
|
|
57
|
+
"deleted": {
|
|
58
|
+
"type": "boolean",
|
|
59
|
+
"description": "是否删除成功(del操作)"
|
|
60
|
+
},
|
|
61
|
+
"list": {
|
|
62
|
+
"type": "array",
|
|
63
|
+
"description": "全部值数组(list操作)"
|
|
64
|
+
},
|
|
65
|
+
"size": {
|
|
66
|
+
"type": "number",
|
|
67
|
+
"description": "键值数量(list操作)"
|
|
68
|
+
},
|
|
69
|
+
"msg": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"description": "写操作结果描述(set/clear操作)"
|
|
72
|
+
},
|
|
73
|
+
"error": {
|
|
74
|
+
"type": "object",
|
|
75
|
+
"description": "错误对象 { code, message }"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"options": {},
|
|
80
|
+
"group": "system"
|
|
81
|
+
}
|