Use msgpackr

This commit is contained in:
Jim Hamill
2025-02-02 07:08:30 +00:00
parent a604363bec
commit e64d964e6e
5 changed files with 152 additions and 30 deletions
+9 -12
View File
@@ -3,10 +3,10 @@ import { StoreInMemorySynced } from './store-inmemory-synced.js';
import { Encrypter } from '../utils';
import {
encodeExtendedJsonObject,
decodeExtendedJsonObject,
} from '@xocash/primitives';
// TODO: Only use pack/unpack on encrypted content.
// Unsure why you were doing it on non-encrypted content (as it's not needed for IDB).
// Did you bump into an object that couldn't be structuredClone'd?
import { pack, unpack } from 'msgpackr';
import { openDB, deleteDB, IDBPDatabase } from 'idb';
@@ -156,7 +156,7 @@ export class StoreIndexedDB extends BaseStore {
return keys.reduce(
(acc, key, index) => {
acc[key.toString()] = decodeExtendedJsonObject(values[index]);
acc[key.toString()] = unpack(values[index]);
return acc;
},
{} as { [key: string]: T },
@@ -177,11 +177,11 @@ export class StoreIndexedDB extends BaseStore {
? this.encryption.decrypt(encodedValue)
: encodedValue;
return decodeExtendedJsonObject(decryptedValue) as T;
return unpack(decryptedValue) as T;
}
async set<T>(key: string, value: T) {
const encodedValue = encodeExtendedJsonObject(value);
const encodedValue = pack(value);
const encryptedValue = this.encryption
? this.encryption.encrypt(encodedValue)
@@ -203,10 +203,7 @@ export class StoreIndexedDB extends BaseStore {
const results = await Promise.all(
keys.map(async (key) => {
const value = await store.get(key);
return [
key,
value ? decodeExtendedJsonObject(value) : undefined,
] as const;
return [key, value ? unpack(value) : undefined] as const;
}),
);
@@ -223,7 +220,7 @@ export class StoreIndexedDB extends BaseStore {
await Promise.all(
Array.from(items).map(([key, value]) => {
const encodedValue = encodeExtendedJsonObject(value);
const encodedValue = pack(value);
return store.put(encodedValue, key);
}),
);