diff --git a/src/storage.js b/src/storage.js index 7fcfba1..fc8b600 100644 --- a/src/storage.js +++ b/src/storage.js @@ -1,15 +1,14 @@ -import * as FileSystem from 'expo-file-system'; -import * as SecureStore from 'expo-secure-store'; +import * as FileSystem from "expo-file-system"; +import * as SecureStore from "expo-secure-store"; -import uuid from './uuid'; -import * as AES from './aes'; +import uuid from "./uuid"; +import * as AES from "./aes"; -const storageFileUriKey = 'storage_file_uri'; +const storageFileUriKey = "storage_file_uri"; const storageDirectoryUri = `${FileSystem.documentDirectory}persist-storage/`; export const createDirectory = () => { - FileSystem.getInfoAsync(storageDirectoryUri) - .then(({ exists }) => { + FileSystem.getInfoAsync(storageDirectoryUri).then(({ exists }) => { if (!exists) { FileSystem.makeDirectoryAsync(storageDirectoryUri, { intermediates: true }); } @@ -25,7 +24,12 @@ export const getAsync = async (key, secureStoreOptions) => { const storageFileUri = await fixedStorageUri(secureStoreOptions); if (storageFileUri) { const storageString = await FileSystem.readAsStringAsync(storageFileUri); - const storage = JSON.parse(storageString); + + let storage = {}; + if (storageString) { + storage = JSON.parse(storageString); + } + const encryptedValue = storage[key]; value = AES.decrypt(encryptedValue, aesKey); } @@ -34,7 +38,7 @@ export const getAsync = async (key, secureStoreOptions) => { } catch (e) { reject(e); } - }); + }); }; export const setAsync = async (key, value, secureStoreOptions) => { @@ -44,8 +48,11 @@ export const setAsync = async (key, value, secureStoreOptions) => { const currentStorageFileUri = await fixedStorageUri(secureStoreOptions); if (currentStorageFileUri) { const storageString = await FileSystem.readAsStringAsync(currentStorageFileUri); - storage = JSON.parse(storageString); - } + + if (storageString) { + storage = JSON.parse(storageString); + } + } const { encryptionKey, encryptedData } = AES.encryptWithRandomKey(value); storage = { ...storage, [key]: encryptedData }; @@ -58,7 +65,7 @@ export const setAsync = async (key, value, secureStoreOptions) => { if (currentStorageFileUri) { await FileSystem.deleteAsync(currentStorageFileUri, { idempotent: true }); } - + resolve(); } catch (e) { reject(e); @@ -72,7 +79,12 @@ export const removeAsync = async (key, secureStoreOptions) => { const currentStorageFileUri = await fixedStorageUri(secureStoreOptions); if (currentStorageFileUri) { let storageString = await FileSystem.readAsStringAsync(currentStorageFileUri); - storage = JSON.parse(storageString); + + let storage = {}; + if (storageString) { + storage = JSON.parse(storageString); + } + delete storage.key; storageString = JSON.stringify(storage); @@ -80,7 +92,7 @@ export const removeAsync = async (key, secureStoreOptions) => { await FileSystem.writeAsStringAsync(newStorageFileUri, storageString); await SecureStore.setItemAsync(storageFileUriKey, newStorageFileUri, secureStoreOptions); await FileSystem.deleteAsync(currentStorageFileUri, { idempotent: true }); - } + } await SecureStore.deleteItemAsync(key, secureStoreOptions); @@ -97,10 +109,10 @@ const generateStorageFileUri = async () => { return uri; }; -const fixedStorageUri = async (secureStoreOptions) => { +const fixedStorageUri = async secureStoreOptions => { const currentStorageFileUri = await SecureStore.getItemAsync(storageFileUriKey, secureStoreOptions); if (currentStorageFileUri) { - const components = currentStorageFileUri.split('persist-storage/'); + const components = currentStorageFileUri.split("persist-storage/"); if (components.length === 2) { const fileName = components[1]; const uri = `${storageDirectoryUri}${fileName}`;