accounts
Version:
Tempo Accounts SDK
61 lines • 2.03 kB
JavaScript
import { Json } from 'ox';
import { createMMKV } from 'react-native-mmkv';
import * as Storage from '../core/Storage.js';
import { getOrCreateMmkvEncryptionKey } from './expoSecureStore.js';
/** Creates an encrypted storage adapter backed by `react-native-mmkv`. */
export function secureMmkv(options = {}) {
let mmkv;
let pending;
async function getMmkv() {
if (mmkv)
return mmkv;
pending ??= create(options)
.then((value) => {
mmkv = value;
return value;
})
.catch((error) => {
pending = undefined;
throw error;
});
return pending;
}
return Storage.from({
async getItem(name) {
const mmkv = await getMmkv();
const raw = mmkv.getString(name);
if (raw == null)
return null;
try {
return Json.parse(raw);
}
catch {
return null;
}
},
async setItem(name, value) {
const mmkv = await getMmkv();
mmkv.set(name, Json.stringify(value));
},
async removeItem(name) {
const mmkv = await getMmkv();
mmkv.remove(name);
},
}, options);
}
async function create(options) {
const encryption_key = options.encryptionKey ?? (await getOrCreateMmkvEncryptionKey());
const configuration = { id: options.id ?? 'tempo-secure' };
if (options.path)
configuration.path = options.path;
configuration.encryptionKey = encryption_key;
configuration.encryptionType = options.encryptionType ?? 'AES-256';
if (options.mode)
configuration.mode = options.mode;
if (options.readOnly !== undefined)
configuration.readOnly = options.readOnly;
if (options.compareBeforeSet !== undefined)
configuration.compareBeforeSet = options.compareBeforeSet;
return createMMKV(configuration);
}
//# sourceMappingURL=secureMmkv.js.map