@faststats/web 0.4.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- import{o as e,r as t}from"./send-data-Cc1nxIlg.js";const n=`faststats_anon_id`;function r(){try{return globalThis.localStorage}catch{return}}function i(){let e=r();if(!e)return``;try{let t=e.getItem(n);if(t)return t}catch{return``}let i=t();try{e.setItem(n,i)}catch{return``}return i}function a(t){return t??e()?``:i()}function o(e){if(e)return``;try{let e=r();if(!e)return``;e.removeItem(n)}catch{return``}return i()}export{o as n,a as t};
@@ -1 +0,0 @@
1
- const e=`faststats_session`;let t=!1,n=null;const r=new Set;function i(){try{return globalThis.localStorage}catch{return null}}function a(){try{return globalThis.sessionStorage}catch{return null}}function o(){return typeof crypto<`u`&&`randomUUID`in crypto?crypto.randomUUID():`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}function s(e){let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n),t|=0;return Math.abs(t)}function c(){let t=i();if(!t)return n;let r=t.getItem(e);if(r)try{let e=JSON.parse(r);if(e.id)return e}catch{}let a=t.getItem(`faststats_session_id`),o=t.getItem(`faststats_session_activity`),s=o===null?NaN:Number(o);if(!a||!Number.isFinite(s))return n;let c=t.getItem(`faststats_session_start`),u=c===null?s:Number(c),d={id:a,start:Number.isFinite(u)?u:s,activity:s};return l(d),t.removeItem(`faststats_session_id`),t.removeItem(`faststats_session_activity`),t.removeItem(`faststats_session_start`),d}function l(t){let r=i();if(!r){n=t;return}try{r.setItem(e,JSON.stringify(t))}catch{n=t}}function u(){n=null;try{i()?.removeItem(e)}catch{}}function d(e,t){return t-e.activity>=18e5||t-e.start>=864e5}function f(e,t){for(let n of r)n(e,t)}function p(e,t){let r=Date.now();if(e)return n?t&&(n.activity=r):n={id:o(),start:r,activity:r},{session:n,rotated:!1,previous:null};let i=c();if(i&&!d(i,r))return t&&(i.activity=r,l(i)),{session:i,rotated:!1,previous:null};let a=i,s={id:o(),start:r,activity:r};return l(s),{session:s,rotated:a!==null,previous:a}}function m(e,t,n){if(t)return n;let r=`faststats_window_id_${e}`,i=a();if(!i)return n;try{let e=i.getItem(r);if(e)return e;let t=o();return i.setItem(r,t),t}catch{return n}}function h(e,t,n,r){let i=r??m(e,n,t.id);return{sessionId:t.id,windowId:i,sessionStart:t.start}}function g(e){t=e,n=null}function _(){return t}function v(e,n){let r=n??t,{session:i}=p(r,!1);return h(e,i,r)}function y(e,n){let r=n??t,{session:i,rotated:a,previous:o}=p(r,!0);if(!a||!o)return null;let s=m(e,r,i.id),c={prev:h(e,o,r,s),next:h(e,i,r,s)};return f(c.prev,c.next),c}function b(e){let r=t,i=v(e,r);if(u(),r){let e=Date.now();n={id:o(),start:e,activity:e}}else try{a()?.removeItem(`faststats_window_id_${e}`)}catch{}let s=v(e,r);return i.sessionId!==s.sessionId&&f(i,s),s}function x(e){return r.add(e),()=>r.delete(e)}function S(e){return typeof e!=`number`||!Number.isFinite(e)?100:Math.max(0,Math.min(100,e))}async function C(e){let{url:t,data:n,contentType:r=`application/json`,headers:i={},debug:a=!1,debugPrefix:o=`[Analytics]`,useBeacon:s=!0,keepalive:c=!0,signal:l}=e;if(l?.aborted)return!1;if(s&&typeof globalThis.navigator?.sendBeacon==`function`)try{let e=n instanceof Blob||typeof Blob>`u`?n:new Blob([n],{type:r});if(globalThis.navigator.sendBeacon(t,e))return a&&console.log(`${o} Sent via beacon`),!0}catch{}try{let e=await fetch(t,{method:`POST`,body:n,headers:{"Content-Type":r,...i},keepalive:c,credentials:`omit`,signal:l}),s=e.ok;if(a){let t=s?`${o} Sent via fetch`:`${o} Failed: ${e.status}`;console[s?`log`:`warn`](t)}return s}catch{return a&&console.warn(`${o} Failed to send`),!1}}export{s as a,b as c,v as i,g as l,C as n,_ as o,o as r,x as s,S as t,y as u};