snappymail/dev/Common/ClientStorageDriver/Cookie.js

163 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
import { CLIENT_SIDE_STORAGE_INDEX_NAME } from 'Common/Consts';
2020-08-12 07:47:24 +08:00
var Cookies = (() => {
function extend (...args) {
var result = {};
args.forEach(attributes => {
for (var key in attributes) {
result[key] = attributes[key];
}
});
return result;
}
function decode (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}
function set (key, value, attributes) {
if (typeof document === 'undefined') {
return;
}
attributes = extend({
path: '/'
}, attributes);
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}
// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
try {
var result = JSON.stringify(value);
if (/^[{[]/.test(result)) {
value = result;
}
} catch (e) {} // eslint-disable-line no-empty
value = encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
key = encodeURIComponent(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape);
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}
return (document.cookie = key + '=' + value + stringifiedAttributes);
}
function get (key) {
if (typeof document === 'undefined') {
return;
}
var jar = {};
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var i = 0;
for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');
try {
var name = decode(parts[0]);
cookie = decode(cookie);
try {
cookie = JSON.parse(cookie);
} catch (e) {} // eslint-disable-line no-empty
jar[name] = cookie;
if (key === name) {
break;
}
} catch (e) {} // eslint-disable-line no-empty
}
return key ? jar[key] : jar;
}
return {
set: set,
getJSON: key => get(key)
};
})();
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
class CookieDriver {
2015-11-15 08:23:16 +08:00
/**
* @param {string} key
* @param {*} data
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2015-11-15 08:23:16 +08:00
*/
set(key, data) {
2019-07-05 03:19:24 +08:00
let result = false,
2016-06-30 08:02:45 +08:00
storageResult = null;
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
try {
storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-15 08:23:16 +08:00
(storageResult || (storageResult = {}))[key] = data;
2019-07-05 03:19:24 +08:00
try {
Cookies.set(CLIENT_SIDE_STORAGE_INDEX_NAME, storageResult, {
2016-04-21 01:12:51 +08:00
expires: 30
2015-11-15 08:23:16 +08:00
});
result = true;
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-15 08:23:16 +08:00
return result;
}
/**
* @param {string} key
2016-06-30 08:02:45 +08:00
* @returns {*}
2015-11-15 08:23:16 +08:00
*/
2016-04-21 01:12:51 +08:00
get(key) {
2015-11-15 08:23:16 +08:00
let result = null;
2019-07-05 03:19:24 +08:00
try {
const storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
result = storageResult && undefined !== storageResult[key] ? storageResult[key] : null;
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-15 08:23:16 +08:00
2016-04-21 01:12:51 +08:00
return result;
}
2015-11-15 08:23:16 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2015-11-15 08:23:16 +08:00
*/
static supported() {
return !!(navigator && navigator.cookieEnabled);
2015-11-15 08:23:16 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { CookieDriver, CookieDriver as default };