universal-cookie
Version:
Universal cookies for JavaScript
8 lines (7 loc) • 5.51 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).UniversalCookie=t()}(this,(function(){"use strict";var e,t={};
/*!
* cookie
* Copyright(c) 2012-2014 Roman Shtylman
* Copyright(c) 2015 Douglas Christopher Wilson
* MIT Licensed
*/var i=function(){if(e)return t;e=1,t.parse=function(e,t){if("string"!=typeof e)throw new TypeError("argument str must be a string");var i={},n=e.length;if(n<2)return i;var r=t&&t.decode||u,s=0,a=0,f=0;do{if(-1===(a=e.indexOf("=",s)))break;if(-1===(f=e.indexOf(";",s)))f=n;else if(a>f){s=e.lastIndexOf(";",a-1)+1;continue}var l=c(e,s,a),p=h(e,a,l),g=e.slice(l,p);if(!o.call(i,g)){var O=c(e,a+1,f),m=h(e,f,O);34===e.charCodeAt(O)&&34===e.charCodeAt(m-1)&&(O++,m--);var y=e.slice(O,m);i[g]=d(y,r)}s=f+1}while(s<n);return i},t.serialize=function(e,t,o){var c=o&&o.encode||encodeURIComponent;if("function"!=typeof c)throw new TypeError("option encode is invalid");if(!n.test(e))throw new TypeError("argument name is invalid");var h=c(t);if(!r.test(h))throw new TypeError("argument val is invalid");var u=e+"="+h;if(!o)return u;if(null!=o.maxAge){var d=Math.floor(o.maxAge);if(!isFinite(d))throw new TypeError("option maxAge is invalid");u+="; Max-Age="+d}if(o.domain){if(!s.test(o.domain))throw new TypeError("option domain is invalid");u+="; Domain="+o.domain}if(o.path){if(!a.test(o.path))throw new TypeError("option path is invalid");u+="; Path="+o.path}if(o.expires){var f=o.expires;if(!function(e){return"[object Date]"===i.call(e)}(f)||isNaN(f.valueOf()))throw new TypeError("option expires is invalid");u+="; Expires="+f.toUTCString()}o.httpOnly&&(u+="; HttpOnly");o.secure&&(u+="; Secure");o.partitioned&&(u+="; Partitioned");if(o.priority){switch("string"==typeof o.priority?o.priority.toLowerCase():o.priority){case"low":u+="; Priority=Low";break;case"medium":u+="; Priority=Medium";break;case"high":u+="; Priority=High";break;default:throw new TypeError("option priority is invalid")}}if(o.sameSite){switch("string"==typeof o.sameSite?o.sameSite.toLowerCase():o.sameSite){case!0:u+="; SameSite=Strict";break;case"lax":u+="; SameSite=Lax";break;case"strict":u+="; SameSite=Strict";break;case"none":u+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}}return u};var i=Object.prototype.toString,o=Object.prototype.hasOwnProperty,n=/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/,r=/^("?)[\u0021\u0023-\u002B\u002D-\u003A\u003C-\u005B\u005D-\u007E]*\1$/,s=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,a=/^[\u0020-\u003A\u003D-\u007E]*$/;function c(e,t,i){do{var o=e.charCodeAt(t);if(32!==o&&9!==o)return t}while(++t<i);return i}function h(e,t,i){for(;t>i;){var o=e.charCodeAt(--t);if(32!==o&&9!==o)return t+1}return i}function u(e){return-1!==e.indexOf("%")?decodeURIComponent(e):e}function d(e,t){try{return t(e)}catch(t){return e}}return t}();function o(e,t={}){const i=function(e){if(e&&"j"===e[0]&&":"===e[1])return e.substr(2);return e}(e);if(!t.doNotParse)try{return JSON.parse(i)}catch(e){}return e}return class{constructor(e,t={}){this.changeListeners=[],this.HAS_DOCUMENT_COOKIE=!1,this.update=()=>{if(!this.HAS_DOCUMENT_COOKIE)return;const e=this.cookies;this.cookies=i.parse(document.cookie),this._checkChanges(e)};const o="undefined"==typeof document?"":document.cookie;this.cookies=function(e){return"string"==typeof e?i.parse(e):"object"==typeof e&&null!==e?e:{}}(e||o),this.defaultSetOptions=t,this.HAS_DOCUMENT_COOKIE=function(){const e="undefined"==typeof global?void 0:global.TEST_HAS_DOCUMENT_COOKIE;return"boolean"==typeof e?e:"object"==typeof document&&"string"==typeof document.cookie}()}_emitChange(e){for(let t=0;t<this.changeListeners.length;++t)this.changeListeners[t](e)}_checkChanges(e){new Set(Object.keys(e).concat(Object.keys(this.cookies))).forEach((t=>{e[t]!==this.cookies[t]&&this._emitChange({name:t,value:o(this.cookies[t])})}))}_startPolling(){this.pollingInterval=setInterval(this.update,300)}_stopPolling(){this.pollingInterval&&clearInterval(this.pollingInterval)}get(e,t={}){return t.doNotUpdate||this.update(),o(this.cookies[e],t)}getAll(e={}){e.doNotUpdate||this.update();const t={};for(let i in this.cookies)t[i]=o(this.cookies[i],e);return t}set(e,t,o){o=o?Object.assign(Object.assign({},this.defaultSetOptions),o):this.defaultSetOptions;const n="string"==typeof t?t:JSON.stringify(t);this.cookies=Object.assign(Object.assign({},this.cookies),{[e]:n}),this.HAS_DOCUMENT_COOKIE&&(document.cookie=i.serialize(e,n,o)),this._emitChange({name:e,value:t,options:o})}remove(e,t){const o=t=Object.assign(Object.assign(Object.assign({},this.defaultSetOptions),t),{expires:new Date(1970,1,1,0,0,1),maxAge:0});this.cookies=Object.assign({},this.cookies),delete this.cookies[e],this.HAS_DOCUMENT_COOKIE&&(document.cookie=i.serialize(e,"",o)),this._emitChange({name:e,value:void 0,options:t})}addChangeListener(e){this.changeListeners.push(e),this.HAS_DOCUMENT_COOKIE&&1===this.changeListeners.length&&("object"==typeof window&&"cookieStore"in window?window.cookieStore.addEventListener("change",this.update):this._startPolling())}removeChangeListener(e){const t=this.changeListeners.indexOf(e);t>=0&&this.changeListeners.splice(t,1),this.HAS_DOCUMENT_COOKIE&&0===this.changeListeners.length&&("object"==typeof window&&"cookieStore"in window?window.cookieStore.removeEventListener("change",this.update):this._stopPolling())}}}));