@pubinfo-pr/module-pinia-crypto 0.234.1 → 0.237.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 +37 -69
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
# @pubinfo/module-pinia-crypto
|
|
2
2
|
|
|
3
|
-
为 Pinia
|
|
3
|
+
为 Pinia 持久化状态提供统一的存储加解密能力。这个模块不直接改 Pinia,而是利用 Pubinfo 暴露的 `pinia:persist:write` / `pinia:persist:read` 钩子,在状态落盘前后处理字符串。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 它在做什么
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
7
|
+
- 提供 `piniaCrypto()` 模块注册函数
|
|
8
|
+
- 默认提供一个基于 `crypto-js` 的 AES 加密器
|
|
9
|
+
- 支持 `cbc` / `ecb` 两种模式
|
|
10
|
+
- 支持 `include` / `exclude` 精确匹配 store
|
|
11
|
+
- 支持完全自定义 `cipher`
|
|
12
|
+
- 失败时可按 `debug` 输出日志,但不会中断业务
|
|
12
13
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
pnpm add @pubinfo/module-pinia-crypto
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## 快速上手
|
|
14
|
+
## 基本用法
|
|
20
15
|
|
|
21
16
|
```ts
|
|
22
|
-
// src/modules/pinia-crypto.ts
|
|
23
17
|
import { createPiniaAesCipher, piniaCrypto } from '@pubinfo/module-pinia-crypto';
|
|
24
18
|
|
|
25
19
|
export function setupPiniaCrypto() {
|
|
@@ -33,76 +27,50 @@ export function setupPiniaCrypto() {
|
|
|
33
27
|
debug: import.meta.env.DEV,
|
|
34
28
|
});
|
|
35
29
|
}
|
|
36
|
-
|
|
37
|
-
// main.ts
|
|
38
|
-
createPubinfo({
|
|
39
|
-
/* ... */
|
|
40
|
-
modules: [
|
|
41
|
-
piniaCrypto(/* options */),
|
|
42
|
-
],
|
|
43
|
-
});
|
|
44
30
|
```
|
|
45
31
|
|
|
46
|
-
>
|
|
32
|
+
> 只有声明了 `persist` 的 Pinia store 才会触发这里的读写钩子。
|
|
47
33
|
|
|
48
|
-
##
|
|
34
|
+
## 主要导出
|
|
49
35
|
|
|
50
|
-
|
|
36
|
+
- `piniaCrypto`
|
|
37
|
+
- `createPiniaAesCipher`
|
|
38
|
+
- `PiniaCryptoOptions`
|
|
39
|
+
- `PiniaCryptoCipher`
|
|
40
|
+
- `PiniaAesCipherOptions`
|
|
41
|
+
- `StoreMatcher`
|
|
51
42
|
|
|
52
|
-
|
|
53
|
-
|------|------|--------|------|
|
|
54
|
-
| `enable` | `boolean` | `true` | 关闭后不做任何处理 |
|
|
55
|
-
| `include` | `StoreMatcher \| StoreMatcher[]` | `[]` | 需要加密的 store 名称/正则/函数(为空表示全部) |
|
|
56
|
-
| `exclude` | `StoreMatcher \| StoreMatcher[]` | `[]` | 需要排除的 store |
|
|
57
|
-
| `cipher` | `PiniaCryptoCipher` | - | 自定义加密器(若提供将覆盖 secret/iv 配置) |
|
|
58
|
-
| `secret` | `string` | `'pubinfo-pinia-secret'` | 默认 AES 密钥,自动补齐/截断至 32 字节 |
|
|
59
|
-
| `iv` | `string` | `'pubinfo-pinia-iv'` | CBC 模式下使用的 IV,自动补齐/截断至 16 字节 |
|
|
60
|
-
| `mode` | `'cbc' \| 'ecb'` | `'cbc'` | 默认 Cipher 模式 |
|
|
61
|
-
| `debug` | `boolean` | `false` | 控制台输出错误日志 |
|
|
62
|
-
|
|
63
|
-
`StoreMatcher` 支持三种写法:
|
|
64
|
-
|
|
65
|
-
```
|
|
66
|
-
type StoreMatcher =
|
|
67
|
-
| string // 完全匹配 storeId
|
|
68
|
-
| RegExp // 正则匹配 storeId
|
|
69
|
-
| (payload) => boolean // 自定义判断逻辑
|
|
70
|
-
```
|
|
43
|
+
## 选项
|
|
71
44
|
|
|
72
|
-
|
|
45
|
+
- `enable`: 是否启用,默认 `true`
|
|
46
|
+
- `include`: 仅对匹配的 store 加密;为空时表示不过滤
|
|
47
|
+
- `exclude`: 从加密集合里排除 store
|
|
48
|
+
- `cipher`: 自定义加密器,优先级高于 `secret` / `iv` / `mode`
|
|
49
|
+
- `secret`: 默认 AES key,会补齐或截断为 32 字节
|
|
50
|
+
- `iv`: CBC 模式使用的 IV,会补齐或截断为 16 字节
|
|
51
|
+
- `mode`: `cbc` 或 `ecb`
|
|
52
|
+
- `debug`: 是否打印错误日志
|
|
73
53
|
|
|
74
|
-
|
|
54
|
+
`StoreMatcher` 支持:
|
|
75
55
|
|
|
76
56
|
```ts
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
57
|
+
type StoreMatcher =
|
|
58
|
+
| string
|
|
59
|
+
| RegExp
|
|
60
|
+
| ((payload) => boolean)
|
|
82
61
|
```
|
|
83
62
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
`PiniaCryptoCipher` 的签名十分简单,只需要同步地返回字符串即可:
|
|
63
|
+
## 自定义 Cipher
|
|
87
64
|
|
|
88
65
|
```ts
|
|
89
66
|
import type { PiniaCryptoCipher } from '@pubinfo/module-pinia-crypto';
|
|
90
67
|
|
|
91
|
-
const
|
|
92
|
-
encrypt: ({ value }) =>
|
|
93
|
-
decrypt: ({ value }) =>
|
|
68
|
+
const customCipher: PiniaCryptoCipher = {
|
|
69
|
+
encrypt: ({ value }) => value,
|
|
70
|
+
decrypt: ({ value }) => value,
|
|
94
71
|
};
|
|
95
|
-
|
|
96
|
-
piniaCrypto({ cipher: rot13Cipher });
|
|
97
72
|
```
|
|
98
73
|
|
|
99
|
-
##
|
|
100
|
-
|
|
101
|
-
1. 先为 store 开启 `persist`。
|
|
102
|
-
2. 打开 DevTools → Application → Local Storage,找到 `storagePrefix + storeId` 的键值。
|
|
103
|
-
3. 修改值后观察 Pinia DevTools,确认状态被自动解密。
|
|
104
|
-
4. 如遇解析失败,开启 `debug` 获取详细日志。
|
|
105
|
-
|
|
106
|
-
## License
|
|
74
|
+
## 仓库关系
|
|
107
75
|
|
|
108
|
-
|
|
76
|
+
这个模块依赖 Pubinfo 核心的 Pinia 持久化钩子体系,因此它不是通用 Pinia 插件,而是 Pubinfo 生态内的增强模块。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pubinfo-pr/module-pinia-crypto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.237.1",
|
|
4
4
|
"description": "pubinfo 框架的加密集成",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
"node": "^20.19.0 || >=22.12.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"pubinfo-pr": "0.
|
|
23
|
+
"pubinfo-pr": "0.237.1"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"crypto-js": "^4.2.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/crypto-js": "^4.2.2",
|
|
30
|
-
"pubinfo-pr": "0.
|
|
30
|
+
"pubinfo-pr": "0.237.1"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"dev": "pubinfo build -w --sourcemap",
|