snappymail/dev/Storage/RainLoop.js

113 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-06-17 07:23:49 +08:00
const STORAGE_KEY = '__rlA';
const TIME_KEY = '__rlT';
2016-05-06 23:14:40 +08:00
/**
* @param {string} storageName
* @returns {boolean}
*/
2019-07-05 03:19:24 +08:00
export function isStorageSupported(storageName) {
let storageIsAvailable = false;
2019-07-05 03:19:24 +08:00
try {
2019-03-28 07:27:52 +08:00
// at: window[storageName] firefox throws SecurityError: The operation is insecure. when in iframe
storageIsAvailable = storageName in window && window[storageName] && window[storageName].setItem;
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2019-03-28 07:27:52 +08:00
2019-07-05 03:19:24 +08:00
if (storageIsAvailable) {
const s = window[storageName],
key = 'testLocalStorage_' + Math.random();
2019-07-05 03:19:24 +08:00
try {
s.setItem(key, key);
2019-07-05 03:19:24 +08:00
if (key === s.getItem(key)) {
s.removeItem(key);
return true;
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
}
return false;
}
2019-07-05 03:19:24 +08:00
const SESS_STORAGE = isStorageSupported('sessionStorage') ? window.sessionStorage || null : null;
2016-06-17 07:23:49 +08:00
const WIN_STORAGE = window.top || window || null;
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const __get = (key) => {
let result = null;
2019-07-05 03:19:24 +08:00
if (SESS_STORAGE) {
2016-06-17 07:23:49 +08:00
result = SESS_STORAGE.getItem(key) || null;
} else if (WIN_STORAGE) {
2019-07-05 03:19:24 +08:00
const data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString()[0]
? JSON.parse(WIN_STORAGE.name.toString())
2019-07-05 03:19:24 +08:00
: null;
result = data ? data[key] || null : null;
2016-05-22 08:09:20 +08:00
}
2016-06-17 07:23:49 +08:00
return result;
};
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const __set = (key, value) => {
2019-07-05 03:19:24 +08:00
if (SESS_STORAGE) {
2016-06-17 07:23:49 +08:00
SESS_STORAGE.setItem(key, value);
} else if (WIN_STORAGE) {
2019-07-05 03:19:24 +08:00
let data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString()[0]
? JSON.parse(WIN_STORAGE.name.toString())
2019-07-05 03:19:24 +08:00
: null;
2016-06-17 07:23:49 +08:00
data = data || {};
data[key] = value;
2016-05-22 20:27:50 +08:00
WIN_STORAGE.name = JSON.stringify(data);
2016-05-22 08:09:20 +08:00
}
2016-06-17 07:23:49 +08:00
};
2016-05-22 08:09:20 +08:00
2020-08-18 03:35:03 +08:00
const timestamp = () => Math.round(Date.now() / 1000);
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const setTimestamp = () => __set(TIME_KEY, timestamp());
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const getTimestamp = () => {
2016-06-30 08:02:45 +08:00
const time = __get(TIME_KEY, 0);
return time ? parseInt(time, 10) || 0 : 0;
2016-06-17 07:23:49 +08:00
};
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getHash() {
2016-06-17 07:23:49 +08:00
return __get(STORAGE_KEY);
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function setHash() {
const key = 'AuthAccountHash',
2016-06-30 08:02:45 +08:00
appData = window.__rlah_data();
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
__set(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
setTimestamp();
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function clearHash() {
2016-06-17 07:23:49 +08:00
__set(STORAGE_KEY, '');
setTimestamp();
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
2019-07-05 03:19:24 +08:00
export function checkTimestamp() {
if (timestamp() > getTimestamp() + 3600000) {
2019-07-05 03:19:24 +08:00
// 60m
2016-06-17 07:23:49 +08:00
clearHash();
return true;
2016-05-06 23:14:40 +08:00
}
2016-06-17 07:23:49 +08:00
return false;
2016-05-06 23:14:40 +08:00
}
2016-06-17 07:23:49 +08:00
// init section
setInterval(setTimestamp, 60000); // 1m