feat: pinia persist plugin custom serializer

pull/40/MERGE
xingyu 2023-11-09 11:18:57 +08:00
parent ca8dc1ab48
commit f95d30129b
3 changed files with 44 additions and 5 deletions

View File

@ -10,4 +10,4 @@ export const cacheCipher = {
} }
// 是否加密缓存,默认生产环境加密 // 是否加密缓存,默认生产环境加密
export const enableStorageEncryption = !isDevMode() export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !isDevMode()

View File

@ -6,12 +6,50 @@
*/ */
import type { Pinia } from 'pinia' import type { Pinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' 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 { 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() 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 * Register Pinia Persist Plugin
@ -34,5 +72,6 @@ export function createPersistedStateOptions(keyPrefix: string): PersistedStateFa
return { return {
storage: localStorage, storage: localStorage,
key: id => `${keyPrefix}__${id}`, key: id => `${keyPrefix}__${id}`,
serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION),
} }
} }

View File

@ -1,14 +1,14 @@
import type { CreateStorageParams } from './storageCache' import type { CreateStorageParams } from './storageCache'
import { createStorage as create } from './storageCache' import { createStorage as create } from './storageCache'
import { getStorageShortName } from '@/utils/env' 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<CreateStorageParams> export type Options = Partial<CreateStorageParams>
function createOptions(storage: Storage, options: Options = {}): Options { function createOptions(storage: Storage, options: Options = {}): Options {
return { return {
// No encryption in debug mode // No encryption in debug mode
hasEncrypt: enableStorageEncryption, hasEncrypt: SHOULD_ENABLE_STORAGE_ENCRYPTION,
storage, storage,
prefixKey: getStorageShortName(), prefixKey: getStorageShortName(),
...options, ...options,