feat: pinia persist plugin custom serializer (#3244)

This commit is contained in:
Kirk Lin 2023-11-07 10:13:24 +08:00 committed by GitHub
parent dccc8f625d
commit ea51c492c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 5 deletions

View File

@ -10,4 +10,4 @@ export const cacheCipher = {
};
// Whether the system cache is encrypted using aes
export const enableStorageEncryption = !isDevMode();
export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !isDevMode();

View File

@ -5,13 +5,49 @@
*
*/
import type { Pinia } from 'pinia';
import { createPersistedState } from 'pinia-plugin-persistedstate';
import { createPersistedState, Serializer } from 'pinia-plugin-persistedstate';
import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate';
import { getCommonStoragePrefix } from '@/utils/env';
import { Encryption, EncryptionFactory } from '@/utils/cipher';
import { cacheCipher, SHOULD_ENABLE_STORAGE_ENCRYPTION } 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 +70,6 @@ export function createPersistedStateOptions(keyPrefix: string): PersistedStateFa
return {
storage: localStorage,
key: (id) => `${keyPrefix}__${id}`,
serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION),
};
}

View File

@ -1,13 +1,16 @@
import { getStorageShortName } from '/@/utils/env';
import { createStorage as create, CreateStorageParams } from './storageCache';
import { enableStorageEncryption, DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import {
SHOULD_ENABLE_STORAGE_ENCRYPTION,
DEFAULT_CACHE_TIME,
} from '/@/settings/encryptionSetting';
export type Options = Partial<CreateStorageParams>;
const createOptions = (storage: Storage, options: Options = {}): Options => {
return {
// No encryption in debug mode
hasEncrypt: enableStorageEncryption,
hasEncrypt: SHOULD_ENABLE_STORAGE_ENCRYPTION,
storage,
prefixKey: getStorageShortName(),
...options,

View File

@ -9,6 +9,7 @@ export interface CreateStorageParams extends EncryptionParams {
hasEncrypt: boolean;
timeout?: Nullable<number>;
}
// TODO 移除此文件夹下全部代码
export const createStorage = ({
prefixKey = '',
storage = sessionStorage,