@arc-publishing/sdk-identity
Version:
JS Identity SDK for working with Identity API
67 lines • 3.45 kB
JavaScript
import { logPrefix, PROFILE_STORAGE_KEY } from '../sdk/constants';
var CookieStorage = (function () {
function CookieStorage(baseDomain, isSession, expDate) {
if (baseDomain === void 0) { baseDomain = ''; }
this.sessionIndicator = '.session';
this.length = 1;
this.baseDomain = baseDomain;
this.cookieString = isSession ? this.sessionIndicator : '';
this.doc = document;
this.expDate = expDate;
}
CookieStorage.prototype.isSession = function (key) {
var sessionKey = "".concat(key).concat(this.sessionIndicator);
return (document.cookie.indexOf(sessionKey) >= 0 ||
this.cookieString.indexOf(this.sessionIndicator) >= 0);
};
CookieStorage.prototype.getItem = function (key) {
if (!key || !this._has(key)) {
return null;
}
var regexpStr = '(?:^|.*;\\s*)' +
encodeURIComponent("".concat(key).concat(this.isSession(key) ? this.sessionIndicator : '')).replace(/[-.+*]/g, '\\$&') +
'\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*';
return JSON.parse(decodeURIComponent(this.doc.cookie.replace(new RegExp(regexpStr), '$1')));
};
CookieStorage.prototype.setItem = function (key, data) {
if (!key) {
return;
}
var escapedData = encodeURIComponent(JSON.stringify(data));
var escapedKey = encodeURIComponent("".concat(key).concat(this.isSession(key) ? this.sessionIndicator : ''));
var expDate = this.expDate && key === PROFILE_STORAGE_KEY
? new Date(this.expDate).toUTCString()
: 'Tue, 19 Jan 2038 03:14:07 GMT';
this.cookieString = "".concat(escapedKey, "=").concat(escapedData, "; ").concat(this.isSession(key) ? '' : 'expires=' + expDate + '; ', ";").concat(this.baseDomain ? "domain=".concat(this.baseDomain, ";") : '', " path=/");
if (data && escapedData.length > 4000) {
console.warn("".concat(logPrefix, " Unable to save data to key \"").concat(key, "\" due to the size of the data (").concat(this.cookieString.length, " bytes is greater than maximum allowed 4 KB)"));
return;
}
this.doc.cookie = this.cookieString;
return data;
};
CookieStorage.prototype.removeItem = function (key) {
if (!key || !this._has(key)) {
return;
}
this.doc.cookie =
encodeURIComponent("".concat(key).concat(this.isSession(key) ? this.sessionIndicator : '')) +
"=; Max-Age=-99999999;".concat(this.baseDomain ? "domain=".concat(this.baseDomain, ";") : '', " path=/");
};
CookieStorage.prototype.clear = function () {
console.warn("".concat(logPrefix, " unsupported method, please use .removeItem(key) instead."));
};
CookieStorage.prototype.key = function (_) {
console.warn("".concat(logPrefix, " unsupported method, please use getItem(key) instead."));
return 'N/A';
};
CookieStorage.prototype._has = function (key) {
return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=').test(this.doc.cookie) ||
new RegExp('(?:^|;\\s*)' +
encodeURIComponent(key + this.sessionIndicator).replace(/[-.+*]/g, '\\$&') +
'\\s*\\=').test(this.doc.cookie));
};
return CookieStorage;
}());
export default CookieStorage;
//# sourceMappingURL=cookieStorage.js.map