Replaced dev/Common/ClientStorageDriver/* with webstorage polyfill

Cleanup some other code
This commit is contained in:
djmaze 2020-09-03 12:51:15 +02:00
parent 0e8bf13d5d
commit b837013cfb
13 changed files with 201 additions and 594 deletions

View file

@ -1,11 +1,6 @@
import { CookieDriver } from 'Common/ClientStorageDriver/Cookie';
import { LocalStorageDriver } from 'Common/ClientStorageDriver/LocalStorage';
import { CLIENT_SIDE_STORAGE_INDEX_NAME } from 'Common/Consts';
const SupportedStorageDriver = [LocalStorageDriver, CookieDriver].find(
StorageDriver => StorageDriver && StorageDriver.supported()
);
const driver = SupportedStorageDriver ? new SupportedStorageDriver() : null;
const storage = window.localStorage;
/**
* @param {number} key
@ -13,7 +8,20 @@ const driver = SupportedStorageDriver ? new SupportedStorageDriver() : null;
* @returns {boolean}
*/
export function set(key, data) {
return driver ? driver.set('p' + key, data) : false;
let storageResult = null;
try {
const storageValue = storage.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null;
storageResult = null === storageValue ? null : JSON.parse(storageValue);
} catch (e) {} // eslint-disable-line no-empty
(storageResult || (storageResult = {}))['p' + key] = data;
try {
storage.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, JSON.stringify(storageResult));
return true;
} catch (e) {} // eslint-disable-line no-empty
return false;
}
/**
@ -21,5 +29,13 @@ export function set(key, data) {
* @returns {*}
*/
export function get(key) {
return driver ? driver.get('p' + key) : null;
try {
key = 'p' + key;
const storageValue = storage.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null,
storageResult = null === storageValue ? null : JSON.parse(storageValue);
return storageResult && null != storageResult[key] ? storageResult[key] : null;
} catch (e) {} // eslint-disable-line no-empty
return null;
}

View file

@ -1,80 +1,17 @@
const STORAGE_KEY = '__rlA';
const TIME_KEY = '__rlT';
/**
* @param {string} storageName
* @returns {boolean}
*/
export function isStorageSupported(storageName) {
let storageIsAvailable = false;
try {
// at: window[storageName] firefox throws SecurityError: The operation is insecure. when in iframe
storageIsAvailable = storageName in window && window[storageName] && window[storageName].setItem;
} catch (e) {} // eslint-disable-line no-empty
if (storageIsAvailable) {
const s = window[storageName],
key = 'testLocalStorage_' + Math.random();
try {
s.setItem(key, key);
if (key === s.getItem(key)) {
s.removeItem(key);
return true;
}
} catch (e) {} // eslint-disable-line no-empty
}
return false;
}
const SESS_STORAGE = isStorageSupported('sessionStorage') ? window.sessionStorage || null : null;
const WIN_STORAGE = window.top || window || null;
const __get = (key) => {
let result = null;
if (SESS_STORAGE) {
result = SESS_STORAGE.getItem(key) || null;
} else if (WIN_STORAGE) {
const data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString()[0]
? JSON.parse(WIN_STORAGE.name.toString())
: null;
result = data ? data[key] || null : null;
}
return result;
};
const __set = (key, value) => {
if (SESS_STORAGE) {
SESS_STORAGE.setItem(key, value);
} else if (WIN_STORAGE) {
let data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString()[0]
? JSON.parse(WIN_STORAGE.name.toString())
: null;
data = data || {};
data[key] = value;
WIN_STORAGE.name = JSON.stringify(data);
}
};
const storage = ()=>window.sessionStorage;
const timestamp = () => Math.round(Date.now() / 1000);
const setTimestamp = () => __set(TIME_KEY, timestamp());
const getTimestamp = () => {
const time = __get(TIME_KEY, 0);
return time ? parseInt(time, 10) || 0 : 0;
};
const setTimestamp = () => storage().setItem(TIME_KEY, timestamp());
/**
* @returns {string}
*/
export function getHash() {
return __get(STORAGE_KEY);
return storage().getItem(STORAGE_KEY) || null;
}
/**
@ -84,7 +21,7 @@ export function setHash() {
const key = 'AuthAccountHash',
appData = window.__rlah_data();
__set(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
storage().setItem(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
setTimestamp();
}
@ -92,7 +29,7 @@ export function setHash() {
* @returns {void}
*/
export function clearHash() {
__set(STORAGE_KEY, '');
storage().setItem(STORAGE_KEY, '');
setTimestamp();
}
@ -100,7 +37,7 @@ export function clearHash() {
* @returns {boolean}
*/
export function checkTimestamp() {
if (timestamp() > getTimestamp() + 3600000) {
if (timestamp() > (parseInt(storage().getItem(TIME_KEY) || 0, 10) || 0) + 3600000) {
// 60m
clearHash();
return true;