Skip to content

Commit

Permalink
Fix issue dannyklaassen#8 (setItem throws JSON Parse error)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Kalashov committed Sep 29, 2022
1 parent 730d393 commit 4521b7c
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/storage.js
Original file line number Diff line number Diff line change
@@ -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 });
}
Expand All @@ -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);
}
Expand All @@ -34,7 +38,7 @@ export const getAsync = async (key, secureStoreOptions) => {
} catch (e) {
reject(e);
}
});
});
};

export const setAsync = async (key, value, secureStoreOptions) => {
Expand All @@ -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 };
Expand All @@ -58,7 +65,7 @@ export const setAsync = async (key, value, secureStoreOptions) => {
if (currentStorageFileUri) {
await FileSystem.deleteAsync(currentStorageFileUri, { idempotent: true });
}

resolve();
} catch (e) {
reject(e);
Expand All @@ -72,15 +79,20 @@ 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);

const newStorageFileUri = await generateStorageFileUri();
await FileSystem.writeAsStringAsync(newStorageFileUri, storageString);
await SecureStore.setItemAsync(storageFileUriKey, newStorageFileUri, secureStoreOptions);
await FileSystem.deleteAsync(currentStorageFileUri, { idempotent: true });
}
}

await SecureStore.deleteItemAsync(key, secureStoreOptions);

Expand All @@ -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}`;
Expand Down

0 comments on commit 4521b7c

Please sign in to comment.