snappymail/dev/Storage/RainLoop.js

115 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-05-06 23:14:40 +08:00
import window from 'window';
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_' + window.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;
2019-07-05 03:19:24 +08:00
} else if (WIN_STORAGE && window.JSON) {
const data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1)
? window.JSON.parse(WIN_STORAGE.name.toString())
: 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);
2019-07-05 03:19:24 +08:00
} else if (WIN_STORAGE && window.JSON) {
let data =
WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1)
? window.JSON.parse(WIN_STORAGE.name.toString())
: 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 = window.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
2019-07-05 03:19:24 +08:00
const timestamp = () => window.Math.round(new window.Date().getTime() / 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);
2019-07-05 03:19:24 +08:00
return time ? window.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() + 1000 * 60 * 60) {
// 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
window.setInterval(setTimestamp, 1000 * 60); // 1m