1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/utils/storage.js

67 lines
2.1 KiB
JavaScript
Raw Normal View History

/* @flow */
// Copy-paste from mytrezor (old wallet)
// https://github.com/satoshilabs/mytrezor/blob/develop/app/scripts/storage/BackendLocalStorage.js
export const getStoragePath = (): string => {
const regexHash = /^([^#]*)#.*$/;
const path = window.location.href.replace(regexHash, '$1');
const regexStart = /^[^:]*:\/\/[^/]*\//;
return path.replace(regexStart, '/');
};
export const STORAGE_PATH: string = getStoragePath();
type StorageType = 'local' | 'session';
export const get: <T>(type: StorageType, key: string) => T | null = (type, key) => {
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
try {
return storage.getItem(key);
} catch (error) {
console.warn(`Get ${type} storage: ${error}`);
return null;
}
};
export const set = (type: StorageType, key: string, value: string | number | boolean): void => {
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
try {
storage.setItem(key, value);
} catch (error) {
console.warn(`Save ${type} storage: ${error}`);
}
};
export const remove = (type: StorageType, key: string): void => {
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
try {
storage.removeItem(key);
} catch (error) {
console.warn(`Remove ${type} storage: ${error}`);
}
};
export const clearAll = (type: ?StorageType): void => {
let clearLocal: boolean = true;
let clearSession: boolean = true;
if (typeof type === 'string') {
clearLocal = type === 'local';
clearSession = !clearLocal;
}
try {
if (clearLocal) {
2019-03-04 12:33:02 +00:00
Object.keys(window.localStorage).forEach(
key => key.indexOf(STORAGE_PATH) >= 0 && window.localStorage.removeItem(key)
);
}
if (clearSession) {
2019-03-04 12:33:02 +00:00
Object.keys(window.sessionStorage).forEach(
key => key.indexOf(STORAGE_PATH) >= 0 && window.sessionStorage.removeItem(key)
);
}
} catch (error) {
console.error(`Clearing sessionStorage error: ${error}`);
}
2019-03-04 12:33:02 +00:00
};