@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.
Files changed (2) hide show
  1. package/README.md +37 -69
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,25 +1,19 @@
1
1
  # @pubinfo/module-pinia-crypto
2
2
 
3
- 为 Pinia 持久化状态提供一层统一的加/解密能力。模块通过 `pinia:persist:*` 钩子在写入浏览器存储前后注入处理逻辑,既可以直接使用内置 AES 加密器,也支持自定义算法。
3
+ 为 Pinia 持久化状态提供统一的存储加解密能力。这个模块不直接改 Pinia,而是利用 Pubinfo 暴露的 `pinia:persist:write` / `pinia:persist:read` 钩子,在状态落盘前后处理字符串。
4
4
 
5
- ## 特性
5
+ ## 它在做什么
6
6
 
7
- - 🔐 **开箱即用的 AES-CBC/ECB 加密器**
8
- - 🎯 **按 store 维度 include/exclude,精确控制作用范围**
9
- - 🧱 **基于 Pinia 持久化插件的同步钩子,零异步依赖**
10
- - 🛠️ **自定义 Cipher 接口,可自由替换为公司内部算法**
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
- > 提醒:只有声明了 `persist` 选项的 Pinia store 才会触发该模块的加解密流程。
32
+ > 只有声明了 `persist` Pinia store 才会触发这里的读写钩子。
47
33
 
48
- ## API
34
+ ## 主要导出
49
35
 
50
- ### `piniaCrypto(options?: PiniaCryptoOptions)`
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
- ### `createPiniaAesCipher(options?: PiniaAesCipherOptions)`
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
- 返回一个符合 `PiniaCryptoCipher` 接口的 AES 加密器:
54
+ `StoreMatcher` 支持:
75
55
 
76
56
  ```ts
77
- const cipher = createPiniaAesCipher({
78
- secret: '32chars-long-secret-key----',
79
- iv: '16chars-long-iv',
80
- mode: 'cbc',
81
- });
57
+ type StoreMatcher =
58
+ | string
59
+ | RegExp
60
+ | ((payload) => boolean)
82
61
  ```
83
62
 
84
- ### 自定义 Cipher
85
-
86
- `PiniaCryptoCipher` 的签名十分简单,只需要同步地返回字符串即可:
63
+ ## 自定义 Cipher
87
64
 
88
65
  ```ts
89
66
  import type { PiniaCryptoCipher } from '@pubinfo/module-pinia-crypto';
90
67
 
91
- const rot13Cipher: PiniaCryptoCipher = {
92
- encrypt: ({ value }) => rot13(value),
93
- decrypt: ({ value }) => rot13(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
- MIT
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.234.1",
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.234.1"
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.234.1"
30
+ "pubinfo-pr": "0.237.1"
31
31
  },
32
32
  "scripts": {
33
33
  "dev": "pubinfo build -w --sourcemap",