@analytics/session-utils
Version:
Tiny session utility library
3 lines (2 loc) • 4.43 kB
JavaScript
var utilSessions=function(n){var e;e=function(n,e){n.exports=function(n,e,t,r,o){for(e=e.split?e.split("."):e,r=0;r<e.length;r++)n=n?n[e[r]]:o;return n===o?t:n}},e({exports:{}});var t="undefined",r="object";"undefined"==typeof process||process;var o="undefined"!=typeof document;function i(n,e){return e.charAt(0)[n]()+e.slice(1)}"undefined"!=typeof Deno&&Deno,o&&"nodejs"===window.name||"undefined"!=typeof navigator&&void 0!==navigator.userAgent&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom"));var u=i.bind(null,"toUpperCase"),a=i.bind(null,"toLowerCase");function s(n,e){void 0===e&&(e=!0);var t=function(n){return d(n)?u("null"):"object"==typeof n?function(n){return f(n.constructor)?n.constructor.name:null}(n):Object.prototype.toString.call(n).slice(8,-1)}(n);return e?a(t):t}function c(n,e){return typeof e===n}var f=c.bind(null,"function"),l=c.bind(null,"string");function d(n){return null===n}function v(n,e){if("object"!=typeof e||d(e))return!1;if(e instanceof n)return!0;var t=s(new n(""));if(function(n){return n instanceof Error||l(n.message)&&n.constructor&&function(n){return"number"===s(n)&&!isNaN(n)}(n.constructor.stackTraceLimit)}(e))for(;e;){if(s(e)===t)return!0;e=Object.getPrototypeOf(e)}return!1}function p(n,e){var t=n instanceof Element||n instanceof HTMLDocument;return t&&e?function(n,e){return void 0===e&&(e=""),n&&n.nodeName===e.toUpperCase()}(n,e):t}function x(n){var e=[].slice.call(arguments,1);return function(){return n.apply(void 0,[].slice.call(arguments).concat(e))}}function g(){for(var n="",e=0,t=4294967295*Math.random()|0;e++<36;){var r="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"[e-1],o=15&t;n+="-"==r||"4"==r?r:("x"==r?o:3&o|8).toString(16),t=e%8==0?4294967295*Math.random()|0:t>>4}return n}c.bind(null,"undefined"),c.bind(null,"boolean"),c.bind(null,"symbol"),v.bind(null,TypeError),v.bind(null,SyntaxError),x(p,"form"),x(p,"button"),x(p,"input"),x(p,"select");var m="__global__",b=typeof self===r&&self.self===self&&self||typeof global===r&&global.global===global&&global||void 0;function y(n){return b[m][n]}function S(n,e){return b[m][n]=e}function w(n){delete b[m][n]}function h(n,e,t){var r;try{if(D(n)){var o=window[n];r=o[e].bind(o)}}catch(n){}return r||t}b[m]||(b[m]={});var j={};function D(n){if(typeof j[n]!==t)return j[n];try{var e=window[n];e.setItem(t,t),e.removeItem(t)}catch(e){return j[n]=!1}return j[n]=!0}D.bind(null,"sessionStorage");var A=h("sessionStorage","getItem",y),I=h("sessionStorage","setItem",S);h("sessionStorage","removeItem",w);var N=function(){if(void 0!==N)return N;var n="cookiecookie";try{k(n,n),N=-1!==document.cookie.indexOf(n),T(n)}catch(n){N=!1}return N}(),O=k,_=k;function T(n){return N?k(n,"",-1):w(n)}function k(n,e,t,r,o,i){if("undefined"!=typeof window){var u=arguments.length>1;return!1===N&&(u?S(n,e):y(n)),u?document.cookie=n+"="+encodeURIComponent(e)+(t?"; expires="+new Date(+new Date+1e3*t).toUTCString()+(r?"; path="+r:"")+(o?"; domain="+o:"")+(i?"; secure":""):""):decodeURIComponent((("; "+document.cookie).split("; "+n+"=")[1]||"").split(";")[0])}}var C="__",E="session",U="__session",M="page",L=["id","createdAt","created"];function P(n){var e=n?new Date(n):new Date;return[e.toISOString(),e.getTime()]}function J(){var n=P(),e=n[0],t=n[1];return{id:g(),created:t,createdAt:e}}var R={session:[A,I],page:[y,S]};function H(n,e){for(var t=R[n],r=t[0],o=t[1],i=J(),u=!1,a={},s=0;s<L.length;s++){var c=L[s],f=C+n+C+E+C+c,l=r(f);u=e||!l,(e||!e&&!l)&&o(f,i[c]);var d=l&&!e?l:i[c],v="created"!==c?d:Number(d);a[c]=v}return q(a,u)}function q(n,e){var t=Date.now();return n.elapsed=t-n.created,n.expires&&(n.remaining=Math.max(n.expires-t,0),n.isExpired=0===n.remaining),n.isNew=e,n}function z(n,e){void 0===n&&(n=30);var t=O(U),r=t?JSON.parse(t):B(n);return e?r:q(r,!t)}function B(n,e,t){void 0===n&&(n=30);var r=t?z(n,!0):J(),o=60*n,i=r.created;if(t){var u=P(),a=u[0],s=u[1];r.modified=s,r.modifiedAt=a,i=s}var c=P(i+1e3*o),f=c[0];return r.expires=c[1],r.expiresAt=f,r.duration=1e3*o,e&&(r=Object.assign(r,e)),_(U,JSON.stringify(r),o),q(r,!t)}var F=H.bind(null,E),G=H.bind(null,E,!0),K=H.bind(null,M),Q=H.bind(null,M,!0);return n.extendSession=function(n,e){return void 0===n&&(n=30),B(n||1,e,!0)},n.getPageSession=K,n.getSession=z,n.getTabSession=F,n.removeSession=function(){return T(U)},n.sessionData=J,n.setPageSession=Q,n.setSession=B,n.setTabSession=G,n}({});
//# sourceMappingURL=session-utils.js.map