@pubinfo-nightly/module-pinia-crypto 2025.11.14
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 +21 -0
- package/README.md +108 -0
- package/dist/cipher.d.ts +2 -0
- package/dist/index.css +0 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5126 -0
- package/dist/runtime.d.ts +12 -0
- package/dist/types.d.ts +26 -0
- package/package.json +37 -0
- package/src/cipher.ts +66 -0
- package/src/index.ts +20 -0
- package/src/runtime.ts +105 -0
- package/src/types.ts +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 PUBINFO 腾龙框架
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @pubinfo/module-pinia-crypto
|
|
2
|
+
|
|
3
|
+
为 Pinia 持久化状态提供一层统一的加/解密能力。模块通过 `pinia:persist:*` 钩子在写入浏览器存储前后注入处理逻辑,既可以直接使用内置 AES 加密器,也支持自定义算法。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🔐 **开箱即用的 AES-CBC/ECB 加密器**
|
|
8
|
+
- 🎯 **按 store 维度 include/exclude,精确控制作用范围**
|
|
9
|
+
- 🧱 **基于 Pinia 持久化插件的同步钩子,零异步依赖**
|
|
10
|
+
- 🛠️ **自定义 Cipher 接口,可自由替换为公司内部算法**
|
|
11
|
+
- 🧪 **完善的调试日志,便于排查存储数据**
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @pubinfo/module-pinia-crypto
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 快速上手
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// src/modules/pinia-crypto.ts
|
|
23
|
+
import { createPiniaAesCipher, piniaCrypto } from '@pubinfo/module-pinia-crypto';
|
|
24
|
+
|
|
25
|
+
export function setupPiniaCrypto() {
|
|
26
|
+
return piniaCrypto({
|
|
27
|
+
cipher: createPiniaAesCipher({
|
|
28
|
+
secret: 'demo-secret-key',
|
|
29
|
+
iv: 'demo-secret-iv',
|
|
30
|
+
mode: 'cbc',
|
|
31
|
+
}),
|
|
32
|
+
include: ['user', /^settings/],
|
|
33
|
+
debug: import.meta.env.DEV,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// main.ts
|
|
38
|
+
createPubinfo({
|
|
39
|
+
/* ... */
|
|
40
|
+
modules: [
|
|
41
|
+
piniaCrypto(/* options */),
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> 提醒:只有声明了 `persist` 选项的 Pinia store 才会触发该模块的加解密流程。
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### `piniaCrypto(options?: PiniaCryptoOptions)`
|
|
51
|
+
|
|
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
|
+
```
|
|
71
|
+
|
|
72
|
+
### `createPiniaAesCipher(options?: PiniaAesCipherOptions)`
|
|
73
|
+
|
|
74
|
+
返回一个符合 `PiniaCryptoCipher` 接口的 AES 加密器:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const cipher = createPiniaAesCipher({
|
|
78
|
+
secret: '32chars-long-secret-key----',
|
|
79
|
+
iv: '16chars-long-iv',
|
|
80
|
+
mode: 'cbc',
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 自定义 Cipher
|
|
85
|
+
|
|
86
|
+
`PiniaCryptoCipher` 的签名十分简单,只需要同步地返回字符串即可:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import type { PiniaCryptoCipher } from '@pubinfo/module-pinia-crypto';
|
|
90
|
+
|
|
91
|
+
const rot13Cipher: PiniaCryptoCipher = {
|
|
92
|
+
encrypt: ({ value }) => rot13(value),
|
|
93
|
+
decrypt: ({ value }) => rot13(value),
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
piniaCrypto({ cipher: rot13Cipher });
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 调试技巧
|
|
100
|
+
|
|
101
|
+
1. 先为 store 开启 `persist`。
|
|
102
|
+
2. 打开 DevTools → Application → Local Storage,找到 `storagePrefix + storeId` 的键值。
|
|
103
|
+
3. 修改值后观察 Pinia DevTools,确认状态被自动解密。
|
|
104
|
+
4. 如遇解析失败,开启 `debug` 获取详细日志。
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
MIT
|
package/dist/cipher.d.ts
ADDED
package/dist/index.css
ADDED
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ModuleOptions } from 'pubinfo-nightly';
|
|
2
|
+
import { PiniaCryptoOptions } from './types';
|
|
3
|
+
import 'uno.css';
|
|
4
|
+
export declare function piniaCrypto(options?: PiniaCryptoOptions): ModuleOptions;
|
|
5
|
+
export { createPiniaAesCipher } from './cipher';
|
|
6
|
+
export type { PiniaAesCipherOptions, PiniaCryptoCipher, PiniaCryptoOptions, StoreMatcher } from './types';
|