From f95d30129b7042f4173090bb9b1eafcc845a349f Mon Sep 17 00:00:00 2001 From: xingyu Date: Thu, 9 Nov 2023 11:18:57 +0800 Subject: [PATCH] feat: pinia persist plugin custom serializer --- src/settings/encryptionSetting.ts | 2 +- src/store/plugin/persist.ts | 43 +++++++++++++++++++++++++++++-- src/utils/cache/index.ts | 4 +-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/settings/encryptionSetting.ts b/src/settings/encryptionSetting.ts index d72ad7ba..2f7b0c83 100644 --- a/src/settings/encryptionSetting.ts +++ b/src/settings/encryptionSetting.ts @@ -10,4 +10,4 @@ export const cacheCipher = { } // 是否加密缓存,默认生产环境加密 -export const enableStorageEncryption = !isDevMode() +export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !isDevMode() diff --git a/src/store/plugin/persist.ts b/src/store/plugin/persist.ts index 25be1ae9..b86c53dd 100644 --- a/src/store/plugin/persist.ts +++ b/src/store/plugin/persist.ts @@ -6,12 +6,50 @@ */ import type { Pinia } from 'pinia' import { createPersistedState } from 'pinia-plugin-persistedstate' -import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate' +import type { PersistedStateFactoryOptions, Serializer } from 'pinia-plugin-persistedstate' import { getCommonStoragePrefix } from '@/utils/env' +import type { Encryption } from '@/utils/cipher' +import { EncryptionFactory } from '@/utils/cipher' +import { SHOULD_ENABLE_STORAGE_ENCRYPTION, cacheCipher } from '@/settings/encryptionSetting' export const PERSIST_KEY_PREFIX = getCommonStoragePrefix() -// TODO customSerializer +const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({ + key: cacheCipher.key, + iv: cacheCipher.iv, +}) + +/** + * Custom serializer for serialization and deserialization of storage data + * 自定义序列化器,用于序列化和反序列化存储数据 + * + * @param shouldEnableEncryption whether to enable encryption for storage data 是否启用存储数据加密 + * @returns serializer + */ +function customSerializer(shouldEnableEncryption: boolean): Serializer { + if (shouldEnableEncryption) { + return { + deserialize: (value) => { + const decrypted = persistEncryption.decrypt(value) + return JSON.parse(decrypted) + }, + serialize: (value) => { + const serialized = JSON.stringify(value) + return persistEncryption.encrypt(serialized) + }, + } + } + else { + return { + deserialize: (value) => { + return JSON.parse(value) + }, + serialize: (value) => { + return JSON.stringify(value) + }, + } + } +} /** * Register Pinia Persist Plugin @@ -34,5 +72,6 @@ export function createPersistedStateOptions(keyPrefix: string): PersistedStateFa return { storage: localStorage, key: id => `${keyPrefix}__${id}`, + serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION), } } diff --git a/src/utils/cache/index.ts b/src/utils/cache/index.ts index 1916394c..a3d180c4 100644 --- a/src/utils/cache/index.ts +++ b/src/utils/cache/index.ts @@ -1,14 +1,14 @@ import type { CreateStorageParams } from './storageCache' import { createStorage as create } from './storageCache' import { getStorageShortName } from '@/utils/env' -import { DEFAULT_CACHE_TIME, enableStorageEncryption } from '@/settings/encryptionSetting' +import { DEFAULT_CACHE_TIME, SHOULD_ENABLE_STORAGE_ENCRYPTION } from '@/settings/encryptionSetting' export type Options = Partial function createOptions(storage: Storage, options: Options = {}): Options { return { // No encryption in debug mode - hasEncrypt: enableStorageEncryption, + hasEncrypt: SHOULD_ENABLE_STORAGE_ENCRYPTION, storage, prefixKey: getStorageShortName(), ...options,