Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying custom serializer/deserializer functions for storage #57

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type Setter<T> = ((v?: T, isHydrated?: boolean) => T) | T
export type RawKey =
| string
| {
key: string
instance: BaseStorage
}
key: string
instance: BaseStorage
}

/**
* https://docs.plasmo.com/framework/storage
Expand Down
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type StorageArea = chrome.storage.StorageArea

export type InternalStorage = typeof chrome.storage

export type SerdeOptions = {
serializer: <T>(value: T) => string,
deserializer: <T>(rawValue: string) => T
}

export abstract class BaseStorage {
#extStorageEngine: InternalStorage

Expand Down Expand Up @@ -95,20 +100,27 @@ export abstract class BaseStorage {
getNamespacedKey = (key: string) => `${this.keyNamespace}${key}`
getUnnamespacedKey = (nsKey: string) => nsKey.slice(this.keyNamespace.length)

serde: SerdeOptions = {
serializer: JSON.stringify,
deserializer: JSON.parse
}

constructor({
area = "sync" as StorageAreaName,
allCopied = false,
copiedKeyList = [] as string[]
copiedKeyList = [] as string[],
serde = {} as SerdeOptions
} = {}) {
this.setCopiedKeySet(copiedKeyList)
this.#area = area
this.#allCopied = allCopied
this.serde = { ...this.serde, ...serde }

try {
if (this.hasWebApi && (allCopied || copiedKeyList.length > 0)) {
this.#secondaryClient = window.localStorage
}
} catch {}
} catch { }

try {
if (this.hasExtensionApi) {
Expand All @@ -123,7 +135,7 @@ export abstract class BaseStorage {
this.#primaryClient = this.#extStorageEngine[this.area]
}
}
} catch {}
} catch { }
}

setCopiedKeySet(keyList: string[]) {
Expand Down Expand Up @@ -163,8 +175,8 @@ export abstract class BaseStorage {
const dataMap = this.allCopied
? await this.rawGetAll()
: await this.#primaryClient.get(
(syncAll ? [...this.copiedKeySet] : [key]).map(this.getNamespacedKey)
)
(syncAll ? [...this.copiedKeySet] : [key]).map(this.getNamespacedKey)
)

if (!dataMap) {
return false
Expand Down Expand Up @@ -345,7 +357,7 @@ export abstract class BaseStorage {
/**
* Parse the value into its original form from storage raw value.
*/
protected abstract parseValue: (rawValue: any) => Promise<any>
protected abstract parseValue: <T>(rawValue: any) => Promise<T | undefined>

/**
* Alias for get
Expand Down Expand Up @@ -378,12 +390,12 @@ export class Storage extends BaseStorage {
get = async <T = string>(key: string) => {
const nsKey = this.getNamespacedKey(key)
const rawValue = await this.rawGet(nsKey)
return this.parseValue(rawValue) as T | undefined
return this.parseValue<T>(rawValue)
}

set = async (key: string, rawValue: any) => {
const nsKey = this.getNamespacedKey(key)
const value = JSON.stringify(rawValue)
const value = this.serde.serializer(rawValue)
return this.rawSet(nsKey, value)
}

Expand All @@ -396,10 +408,10 @@ export class Storage extends BaseStorage {
this.keyNamespace = namespace
}

protected parseValue = async (rawValue: any) => {
protected parseValue = async <T>(rawValue: any) => {
try {
if (rawValue !== undefined) {
return JSON.parse(rawValue)
return this.serde.deserializer<T>(rawValue)
}
} catch (e) {
// ignore error. TODO: debug log them maybe
Expand Down
8 changes: 4 additions & 4 deletions src/secure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ export class SecureStorage extends BaseStorage {
get = async <T = string>(key: string) => {
const nsKey = this.getNamespacedKey(key)
const boxBase64 = await this.rawGet(nsKey)
return this.parseValue(boxBase64) as T | undefined
return this.parseValue<T>(boxBase64)
}

set = async (key: string, rawValue: any) => {
const nsKey = this.getNamespacedKey(key)
const value = JSON.stringify(rawValue)
const value = this.serde.serializer(rawValue)
const boxBase64 = await this.encrypt(value)
return await this.rawSet(nsKey, boxBase64)
}
Expand All @@ -177,10 +177,10 @@ export class SecureStorage extends BaseStorage {
return await this.rawRemove(nsKey)
}

protected parseValue = async (boxBase64: string | null | undefined) => {
protected parseValue = async <T>(boxBase64: string | null | undefined) => {
if (boxBase64 !== undefined && boxBase64 !== null) {
const rawValue = await this.decrypt(boxBase64)
return JSON.parse(rawValue)
return this.serde.deserializer<T>(rawValue)
}
return undefined
}
Expand Down