testchimp-rum-js 0.0.6 → 0.0.7

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 +1 @@
1
- {"version":3,"file":"defaultSessionMetadata.d.ts","sourceRoot":"","sources":["../src/defaultSessionMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAwE3C;;;GAGG;AACH,wBAAgB,yBAAyB,IAAI,MAAM,CASlD"}
1
+ {"version":3,"file":"defaultSessionMetadata.d.ts","sourceRoot":"","sources":["../src/defaultSessionMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AA6D3C;;;GAGG;AACH,wBAAgB,yBAAyB,IAAI,MAAM,CASlD"}
@@ -1 +1 @@
1
- "use strict";var testchimp=(()=>{var h=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var ee=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},te=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of W(t))!Q.call(e,o)&&o!==n&&h(e,o,{get:()=>t[o],enumerable:!(r=q(t,o))||r.enumerable});return e};var ne=e=>te(h({},"__esModule",{value:!0}),e);var Se={};ee(Se,{default:()=>ye,emit:()=>Y,flush:()=>$,getSessionId:()=>J,init:()=>K,resetSession:()=>Z});function re(){if(typeof fetch<"u")return fetch;throw new Error("testchimp-rum-js: fetch is not available. Use a browser or Node 18+.")}function ie(){if(typeof window>"u")return;let t=window.__TC_CI_TEST_INFO;return typeof t=="string"&&t.length>0?t:void 0}function oe(e,t){let n=e.baseUrl.replace(/\/$/,""),r=t.startsWith("/")?t:`/${t}`;return`${n}${r}`}async function v(e,t,n,r){try{let o=oe(e,t),s={"Content-Type":"application/json","Project-Id":e.projectId,"TestChimp-Api-Key":e.apiKey,...r?.keepalive&&{"Keep-Alive":"true"}},u=ie();return u&&(s["ci-test-info"]=u),(await re()(o,{method:"POST",headers:s,body:JSON.stringify(n)})).ok}catch{return!1}}var N="0123456789ABCDEFGHJKMNPQRSTVWXYZ";function I(){let e=Date.now(),t=se(e,10),n=ae(16);return t+n}function se(e,t){let n="";for(let r=t;r>0;r--)n=N[e%32]+n,e=Math.floor(e/32);return n}function ae(e){let t=new Uint8Array(e);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(t);else for(let r=0;r<e;r++)t[r]=Math.floor(Math.random()*256);let n="";for(let r=0;r<e;r++)n+=N[t[r]%32];return n}function D(e){return e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function L(e){if(typeof e!="object"||e===null||Array.isArray(e))return{ok:!1,reason:"metadata must be a plain object"};let t=Object.keys(e);if(t.length>10)return{ok:!1,reason:"metadata has more than 10 keys"};let n={};for(let r of t){if(r.length>50)return{ok:!1,reason:"metadata key exceeds 50 chars"};let o=e[r];if(Array.isArray(o)){if(o.length>50)return{ok:!1,reason:"metadata array length exceeds 50"};for(let s=0;s<o.length;s++){let u=o[s];if(!D(u))return{ok:!1,reason:"metadata array values must be string, number, boolean, or null"};if(typeof u=="string"&&u.length>200)return{ok:!1,reason:"metadata string in array exceeds 200 chars"}}n[r]=o}else if(D(o)){if(typeof o=="string"&&o.length>200)return{ok:!1,reason:"metadata string value exceeds 200 chars"};n[r]=o}else return{ok:!1,reason:"metadata values must be primitive or array of primitives (no nested objects)"}}return{ok:!0,value:n}}function k(e){if(!e.title||typeof e.title!="string")return console.warn("[testchimp-rum] Event dropped: title is required"),null;if(e.title.length>100)return console.warn("[testchimp-rum] Event dropped: title exceeds 100 chars"),null;let t;if(e.metadata!=null&&typeof e.metadata=="object"&&!Array.isArray(e.metadata)){let o=L(e.metadata);if(!o.ok)return console.warn(`[testchimp-rum] Event dropped: ${o.reason}`),null;t=Object.keys(o.value).length>0?o.value:void 0}let n={title:e.title,timestampMillis:Date.now(),metadata:t},r=JSON.stringify(n);return new TextEncoder().encode(r).length>5120?(console.warn("[testchimp-rum] Event dropped: total size exceeds 5120 bytes"),null):n}function _(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t=L(e);if(t.ok)return Object.keys(t.value).length>0?t.value:void 0}var w="testchimp_session_id",T="testchimp_last_activity",A="testchimp_event_count",y="testchimp_event_type_counts";function l(){try{return typeof window<"u"&&window.localStorage?window.localStorage:null}catch{return null}}function C(e,t,n){if(e&&typeof e=="string"&&e.length>0)return{sessionId:e,isNew:!1};let r=l();if(!r)return{sessionId:I(),isNew:!0};let o=r.getItem(w),s=r.getItem(T),u=Date.now();if(o&&s){let c=parseInt(s,10);if(!isNaN(c)&&u-c<n)return{sessionId:o,isNew:!1}}return{sessionId:I(),isNew:!0}}function R(e,t){let n=l();if(!n)return;let r=Date.now();n.setItem(w,e),n.setItem(T,String(r)),n.setItem(A,"0");let o=_(t);if(o)try{n.setItem("testchimp_session_metadata",JSON.stringify(o))}catch{}let s={};try{n.setItem(y,JSON.stringify(s))}catch{}}function B(){let e=l();e&&e.setItem(T,String(Date.now()))}function x(){let e=l();if(!e)return 0;let t=e.getItem(A),n=parseInt(t??"0",10);return isNaN(n)?0:n}function P(e){let t=l();t&&t.setItem(A,String(e))}function M(){let e=l();if(!e)return{};try{let t=e.getItem(y);if(!t)return{};let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:{}}catch{return{}}}function V(e){let t=l();if(t)try{t.setItem(y,JSON.stringify(e))}catch{}}function O(){let e=l();if(e)try{let t=e.getItem("testchimp_session_metadata");if(!t)return;let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:void 0}catch{return}}function X(){let e=l();e&&(e.removeItem(w),e.removeItem(T),e.removeItem(A),e.removeItem(y),e.removeItem("testchimp_session_metadata"))}function E(e){return e.length<=200?e:e.slice(0,200)}function ue(){if(typeof navigator>"u")return"unknown";let e=navigator.userAgent??"",t=navigator;if(t.userAgentData?.brands?.length){let n=t.userAgentData.brands[0]?.brand;if(n){let r=n==="Not_A Brand"?t.userAgentData.brands[1]?.brand:n;if(r)return E(r==="Edg"?"Edge":r)}}return/Edg\//i.test(e)?"Edge":/Firefox\//i.test(e)?"Firefox":/Chrome\//i.test(e)&&!/Edg\//i.test(e)?"Chrome":/Safari\//i.test(e)&&!/Chrome\//i.test(e)?"Safari":/OPR\//i.test(e)?"Opera":E(e.slice(0,50))||"unknown"}function fe(){if(typeof navigator>"u"||typeof window>"u")return"unknown";let e=navigator.userAgent??"";return navigator.userAgentData?.mobile===!0?/iPad/i.test(e)||/Mac/i.test(e)&&typeof navigator.maxTouchPoints=="number"&&navigator.maxTouchPoints>0?"tablet":"mobile":/Tablet|iPad/i.test(e)?"tablet":/Mobile|Android/i.test(e)&&!/iPad/i.test(e)?"mobile":"desktop"}function de(){if(typeof navigator>"u")return"unknown";let e=navigator,t=(e.userAgentData?.platform??e.platform??navigator.userAgent??"").toLowerCase();return t.includes("win")?"windows":t.includes("mac")||t==="macintel"?"mac":t.includes("linux")||t==="linux"?"linux":t.includes("iphone")||t.includes("ipod")||t.includes("ipad")?"ios":t.includes("android")?"android":E(t||"unknown")}function ce(){if(typeof navigator>"u")return"unknown";let e=navigator.language??navigator.userLanguage;return E(e??"unknown")}function le(){try{if(typeof Intl>"u"||!Intl.DateTimeFormat)return"unknown";let e=Intl.DateTimeFormat().resolvedOptions().timeZone;return E(e??"unknown")}catch{return"unknown"}}function G(){return typeof navigator>"u"?{}:{_browser:ue(),_device_type:fe(),_os:de(),_language:ce(),_timezone:le()}}var me=100,pe=3;function H(e,t,n){let r=[],o=!1;function s(a){return!(x()>=n.maxEventsPerSession||(M()[a]??0)>=n.maxRepeatsPerEvent)}function u(a){let f=x()+1;P(f);let d=M();return d[a]=(d[a]??0)+1,V(d),f}function c(a,f=!1){a.length!==0&&v(e,"/rum/events",{session_id:t,events:a.map(d=>({title:d.title,event_index:d.eventIndex,timestamp_millis:d.timestampMillis,metadata:d.metadata??{}}))},{keepalive:f})}function m(a=!1){if(r.length===0)return;let f=[...r];r=[],c(f,a)}function S(a){if(!s(a.title))return!1;let f=u(a.title);return r.push({...a,eventIndex:f}),!0}function p(){return r.length}function b(){return r}return{add:S,flush:m,getBufferSize:p,getBuffer:b,get maxBufferSize(){return n.maxBufferSize}}}function U(){return{maxEventsPerSession:me,maxRepeatsPerEvent:pe,maxBufferSize:100}}var ge="https://ingress.testchimp.io",Ee=1e4,ve=100,_e=30*60*1e3,i=null;function Te(e){i.flushTimerId&&clearInterval(i.flushTimerId),i.flushTimerId=setInterval(()=>{i&&i.buffer.getBufferSize()>0&&i.buffer.flush(!1)},e)}function z(){document.visibilityState==="hidden"&&i&&i.buffer.flush(!0)}function F(){i&&i.buffer.flush(!0)}function j(e){e.key==="testchimp_session_id"&&e.newValue&&i&&(i.sessionId=e.newValue)}function K(e){if(!e.projectId||!e.apiKey){console.warn("[testchimp-rum] init: projectId and apiKey are required");return}let t=e.config??{},n=t.captureEnabled!==!1,r=t.testchimpEndpoint??ge,o=t.inactivityTimeoutMillis??_e,s=t.eventSendInterval??Ee,u=t.maxBufferSize??ve,c=_(e.sessionMetadata),{sessionId:m,isNew:S}=C(e.sessionId,c,o);R(m,c??e.sessionMetadata);let p={baseUrl:r,projectId:e.projectId,apiKey:e.apiKey},b={...U(),maxEventsPerSession:t.maxEventsPerSession??100,maxRepeatsPerEvent:t.maxRepeatsPerEvent??3,maxBufferSize:u},a=H(p,m,b);if(i&&i.flushTimerId&&clearInterval(i.flushTimerId),i={httpConfig:p,sessionId:m,buffer:a,captureEnabled:n,flushTimerId:null,initialized:!0},Te(s),typeof document<"u"&&(document.addEventListener("visibilitychange",z),window.addEventListener("beforeunload",F)),typeof window<"u"&&window.addEventListener("storage",j),S&&n){let f=O()??c??{},d=t.enableDefaultSessionMetadata!==!1?{...G(),...f}:f,g={session_id:m,started_at:Date.now(),metadata:d};e.environment&&(g.environment=e.environment),e.release&&(g.release=e.release),e.branchName&&(g.branch_name=e.branchName),v(p,"/rum/session/start",g)}}function Y(e){if(!i||!i.initialized){console.warn("[testchimp-rum] emit: call init() first");return}if(!i.captureEnabled)return;let t=k(e);!t||(B(),!i.buffer.add(t))||i.buffer.getBufferSize()>=i.buffer.maxBufferSize&&i.buffer.flush(!1)}function $(){i&&i.initialized&&i.buffer.flush(!1)}function J(){return!i||!i.initialized?"":i.sessionId}function Z(){i&&(i.flushTimerId&&(clearInterval(i.flushTimerId),i.flushTimerId=null),i=null),typeof document<"u"&&(document.removeEventListener("visibilitychange",z),window.removeEventListener("beforeunload",F)),typeof window<"u"&&window.removeEventListener("storage",j),X()}var Ae={init:K,emit:Y,flush:$,getSessionId:J,resetSession:Z},ye=Ae;return ne(Se);})();
1
+ "use strict";var testchimp=(()=>{var I=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var ee=(e,t)=>{for(var n in t)I(e,n,{get:t[n],enumerable:!0})},te=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of W(t))!Q.call(e,o)&&o!==n&&I(e,o,{get:()=>t[o],enumerable:!(r=q(t,o))||r.enumerable});return e};var ne=e=>te(I({},"__esModule",{value:!0}),e);var Se={};ee(Se,{default:()=>ye,emit:()=>Y,flush:()=>$,getSessionId:()=>J,init:()=>K,resetSession:()=>Z});function re(){if(typeof fetch<"u")return fetch;throw new Error("testchimp-rum-js: fetch is not available. Use a browser or Node 18+.")}function ie(){if(typeof window>"u")return;let t=window.__TC_CI_TEST_INFO;return typeof t=="string"&&t.length>0?t:void 0}function oe(e,t){let n=e.baseUrl.replace(/\/$/,""),r=t.startsWith("/")?t:`/${t}`;return`${n}${r}`}async function E(e,t,n,r){try{let o=oe(e,t),s={"Content-Type":"application/json","Project-Id":e.projectId,"TestChimp-Api-Key":e.apiKey,...r?.keepalive&&{"Keep-Alive":"true"}},u=ie();return u&&(s["ci-test-info"]=u),(await re()(o,{method:"POST",headers:s,body:JSON.stringify(n)})).ok}catch{return!1}}var N="0123456789ABCDEFGHJKMNPQRSTVWXYZ";function b(){let e=Date.now(),t=se(e,10),n=ae(16);return t+n}function se(e,t){let n="";for(let r=t;r>0;r--)n=N[e%32]+n,e=Math.floor(e/32);return n}function ae(e){let t=new Uint8Array(e);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(t);else for(let r=0;r<e;r++)t[r]=Math.floor(Math.random()*256);let n="";for(let r=0;r<e;r++)n+=N[t[r]%32];return n}function D(e){return e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function L(e){if(typeof e!="object"||e===null||Array.isArray(e))return{ok:!1,reason:"metadata must be a plain object"};let t=Object.keys(e);if(t.length>10)return{ok:!1,reason:"metadata has more than 10 keys"};let n={};for(let r of t){if(r.length>50)return{ok:!1,reason:"metadata key exceeds 50 chars"};let o=e[r];if(Array.isArray(o)){if(o.length>50)return{ok:!1,reason:"metadata array length exceeds 50"};for(let s=0;s<o.length;s++){let u=o[s];if(!D(u))return{ok:!1,reason:"metadata array values must be string, number, boolean, or null"};if(typeof u=="string"&&u.length>200)return{ok:!1,reason:"metadata string in array exceeds 200 chars"}}n[r]=o}else if(D(o)){if(typeof o=="string"&&o.length>200)return{ok:!1,reason:"metadata string value exceeds 200 chars"};n[r]=o}else return{ok:!1,reason:"metadata values must be primitive or array of primitives (no nested objects)"}}return{ok:!0,value:n}}function C(e){if(!e.title||typeof e.title!="string")return console.warn("[testchimp-rum] Event dropped: title is required"),null;if(e.title.length>100)return console.warn("[testchimp-rum] Event dropped: title exceeds 100 chars"),null;let t;if(e.metadata!=null&&typeof e.metadata=="object"&&!Array.isArray(e.metadata)){let o=L(e.metadata);if(!o.ok)return console.warn(`[testchimp-rum] Event dropped: ${o.reason}`),null;t=Object.keys(o.value).length>0?o.value:void 0}let n={title:e.title,timestampMillis:Date.now(),metadata:t},r=JSON.stringify(n);return new TextEncoder().encode(r).length>5120?(console.warn("[testchimp-rum] Event dropped: total size exceeds 5120 bytes"),null):n}function v(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t=L(e);if(t.ok)return Object.keys(t.value).length>0?t.value:void 0}var w="testchimp_session_id",T="testchimp_last_activity",_="testchimp_event_count",A="testchimp_event_type_counts";function l(){try{return typeof window<"u"&&window.localStorage?window.localStorage:null}catch{return null}}function k(e,t,n){if(e&&typeof e=="string"&&e.length>0)return{sessionId:e,isNew:!1};let r=l();if(!r)return{sessionId:b(),isNew:!0};let o=r.getItem(w),s=r.getItem(T),u=Date.now();if(o&&s){let d=parseInt(s,10);if(!isNaN(d)&&u-d<n)return{sessionId:o,isNew:!1}}return{sessionId:b(),isNew:!0}}function R(e,t){let n=l();if(!n)return;let r=Date.now();n.setItem(w,e),n.setItem(T,String(r)),n.setItem(_,"0");let o=v(t);if(o)try{n.setItem("testchimp_session_metadata",JSON.stringify(o))}catch{}let s={};try{n.setItem(A,JSON.stringify(s))}catch{}}function B(){let e=l();e&&e.setItem(T,String(Date.now()))}function x(){let e=l();if(!e)return 0;let t=e.getItem(_),n=parseInt(t??"0",10);return isNaN(n)?0:n}function P(e){let t=l();t&&t.setItem(_,String(e))}function M(){let e=l();if(!e)return{};try{let t=e.getItem(A);if(!t)return{};let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:{}}catch{return{}}}function V(e){let t=l();if(t)try{t.setItem(A,JSON.stringify(e))}catch{}}function O(){let e=l();if(e)try{let t=e.getItem("testchimp_session_metadata");if(!t)return;let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:void 0}catch{return}}function X(){let e=l();e&&(e.removeItem(w),e.removeItem(T),e.removeItem(_),e.removeItem(A),e.removeItem("testchimp_session_metadata"))}function y(e){return e.length<=200?e:e.slice(0,200)}function ue(){if(typeof navigator>"u")return"unknown";let e=navigator.userAgent??"";return/Edg\//i.test(e)?"Edge":/Firefox\//i.test(e)?"Firefox":/Chrome\//i.test(e)&&!/Edg\//i.test(e)?"Chrome":/Safari\//i.test(e)&&!/Chrome\//i.test(e)?"Safari":/OPR\//i.test(e)?"Opera":y(e.slice(0,50))||"unknown"}function fe(){if(typeof navigator>"u"||typeof window>"u")return"unknown";let e=navigator.userAgent??"";return navigator.userAgentData?.mobile===!0?/iPad/i.test(e)||/Mac/i.test(e)&&typeof navigator.maxTouchPoints=="number"&&navigator.maxTouchPoints>0?"tablet":"mobile":/Tablet|iPad/i.test(e)?"tablet":/Mobile|Android/i.test(e)&&!/iPad/i.test(e)?"mobile":"desktop"}function ce(){if(typeof navigator>"u")return"unknown";let e=navigator,t=(e.userAgentData?.platform??e.platform??navigator.userAgent??"").toLowerCase();return t.includes("win")?"windows":t.includes("mac")||t==="macintel"?"mac":t.includes("linux")||t==="linux"?"linux":t.includes("iphone")||t.includes("ipod")||t.includes("ipad")?"ios":t.includes("android")?"android":y(t||"unknown")}function de(){if(typeof navigator>"u")return"unknown";let e=navigator.language??navigator.userLanguage;return y(e??"unknown")}function le(){try{if(typeof Intl>"u"||!Intl.DateTimeFormat)return"unknown";let e=Intl.DateTimeFormat().resolvedOptions().timeZone;return y(e??"unknown")}catch{return"unknown"}}function G(){return typeof navigator>"u"?{}:{_browser:ue(),_device_type:fe(),_os:ce(),_language:de(),_timezone:le()}}var me=100,pe=3;function H(e,t,n){let r=[],o=!1;function s(a){return!(x()>=n.maxEventsPerSession||(M()[a]??0)>=n.maxRepeatsPerEvent)}function u(a){let f=x()+1;P(f);let c=M();return c[a]=(c[a]??0)+1,V(c),f}function d(a,f=!1){a.length!==0&&E(e,"/rum/events",{session_id:t,events:a.map(c=>({title:c.title,event_index:c.eventIndex,timestamp_millis:c.timestampMillis,metadata:c.metadata??{}}))},{keepalive:f})}function m(a=!1){if(r.length===0)return;let f=[...r];r=[],d(f,a)}function S(a){if(!s(a.title))return!1;let f=u(a.title);return r.push({...a,eventIndex:f}),!0}function p(){return r.length}function h(){return r}return{add:S,flush:m,getBufferSize:p,getBuffer:h,get maxBufferSize(){return n.maxBufferSize}}}function U(){return{maxEventsPerSession:me,maxRepeatsPerEvent:pe,maxBufferSize:100}}var ge="https://ingress.testchimp.io",Ee=1e4,ve=100,Te=30*60*1e3,i=null;function _e(e){i.flushTimerId&&clearInterval(i.flushTimerId),i.flushTimerId=setInterval(()=>{i&&i.buffer.getBufferSize()>0&&i.buffer.flush(!1)},e)}function z(){document.visibilityState==="hidden"&&i&&i.buffer.flush(!0)}function F(){i&&i.buffer.flush(!0)}function j(e){e.key==="testchimp_session_id"&&e.newValue&&i&&(i.sessionId=e.newValue)}function K(e){if(!e.projectId||!e.apiKey){console.warn("[testchimp-rum] init: projectId and apiKey are required");return}let t=e.config??{},n=t.captureEnabled!==!1,r=t.testchimpEndpoint??ge,o=t.inactivityTimeoutMillis??Te,s=t.eventSendInterval??Ee,u=t.maxBufferSize??ve,d=v(e.sessionMetadata),{sessionId:m,isNew:S}=k(e.sessionId,d,o);R(m,d??e.sessionMetadata);let p={baseUrl:r,projectId:e.projectId,apiKey:e.apiKey},h={...U(),maxEventsPerSession:t.maxEventsPerSession??100,maxRepeatsPerEvent:t.maxRepeatsPerEvent??3,maxBufferSize:u},a=H(p,m,h);if(i&&i.flushTimerId&&clearInterval(i.flushTimerId),i={httpConfig:p,sessionId:m,buffer:a,captureEnabled:n,flushTimerId:null,initialized:!0},_e(s),typeof document<"u"&&(document.addEventListener("visibilitychange",z),window.addEventListener("beforeunload",F)),typeof window<"u"&&window.addEventListener("storage",j),S&&n){let f=O()??d??{},c=t.enableDefaultSessionMetadata!==!1?{...G(),...f}:f,g={session_id:m,started_at:Date.now(),metadata:c};e.environment&&(g.environment=e.environment),e.release&&(g.release=e.release),e.branchName&&(g.branch_name=e.branchName),E(p,"/rum/session/start",g)}}function Y(e){if(!i||!i.initialized){console.warn("[testchimp-rum] emit: call init() first");return}if(!i.captureEnabled)return;let t=C(e);!t||(B(),!i.buffer.add(t))||i.buffer.getBufferSize()>=i.buffer.maxBufferSize&&i.buffer.flush(!1)}function $(){i&&i.initialized&&i.buffer.flush(!1)}function J(){return!i||!i.initialized?"":i.sessionId}function Z(){i&&(i.flushTimerId&&(clearInterval(i.flushTimerId),i.flushTimerId=null),i=null),typeof document<"u"&&(document.removeEventListener("visibilitychange",z),window.removeEventListener("beforeunload",F)),typeof window<"u"&&window.removeEventListener("storage",j),X()}var Ae={init:K,emit:Y,flush:$,getSessionId:J,resetSession:Z},ye=Ae;return ne(Se);})();
@@ -1 +1 @@
1
- function j(){if(typeof fetch<"u")return fetch;throw new Error("testchimp-rum-js: fetch is not available. Use a browser or Node 18+.")}function K(){if(typeof window>"u")return;let t=window.__TC_CI_TEST_INFO;return typeof t=="string"&&t.length>0?t:void 0}function Y(e,t){let n=e.baseUrl.replace(/\/$/,""),r=t.startsWith("/")?t:`/${t}`;return`${n}${r}`}async function v(e,t,n,r){try{let o=Y(e,t),s={"Content-Type":"application/json","Project-Id":e.projectId,"TestChimp-Api-Key":e.apiKey,...r?.keepalive&&{"Keep-Alive":"true"}},u=K();return u&&(s["ci-test-info"]=u),(await j()(o,{method:"POST",headers:s,body:JSON.stringify(n)})).ok}catch{return!1}}var M="0123456789ABCDEFGHJKMNPQRSTVWXYZ";function h(){let e=Date.now(),t=$(e,10),n=J(16);return t+n}function $(e,t){let n="";for(let r=t;r>0;r--)n=M[e%32]+n,e=Math.floor(e/32);return n}function J(e){let t=new Uint8Array(e);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(t);else for(let r=0;r<e;r++)t[r]=Math.floor(Math.random()*256);let n="";for(let r=0;r<e;r++)n+=M[t[r]%32];return n}function N(e){return e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function D(e){if(typeof e!="object"||e===null||Array.isArray(e))return{ok:!1,reason:"metadata must be a plain object"};let t=Object.keys(e);if(t.length>10)return{ok:!1,reason:"metadata has more than 10 keys"};let n={};for(let r of t){if(r.length>50)return{ok:!1,reason:"metadata key exceeds 50 chars"};let o=e[r];if(Array.isArray(o)){if(o.length>50)return{ok:!1,reason:"metadata array length exceeds 50"};for(let s=0;s<o.length;s++){let u=o[s];if(!N(u))return{ok:!1,reason:"metadata array values must be string, number, boolean, or null"};if(typeof u=="string"&&u.length>200)return{ok:!1,reason:"metadata string in array exceeds 200 chars"}}n[r]=o}else if(N(o)){if(typeof o=="string"&&o.length>200)return{ok:!1,reason:"metadata string value exceeds 200 chars"};n[r]=o}else return{ok:!1,reason:"metadata values must be primitive or array of primitives (no nested objects)"}}return{ok:!0,value:n}}function L(e){if(!e.title||typeof e.title!="string")return console.warn("[testchimp-rum] Event dropped: title is required"),null;if(e.title.length>100)return console.warn("[testchimp-rum] Event dropped: title exceeds 100 chars"),null;let t;if(e.metadata!=null&&typeof e.metadata=="object"&&!Array.isArray(e.metadata)){let o=D(e.metadata);if(!o.ok)return console.warn(`[testchimp-rum] Event dropped: ${o.reason}`),null;t=Object.keys(o.value).length>0?o.value:void 0}let n={title:e.title,timestampMillis:Date.now(),metadata:t},r=JSON.stringify(n);return new TextEncoder().encode(r).length>5120?(console.warn("[testchimp-rum] Event dropped: total size exceeds 5120 bytes"),null):n}function _(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t=D(e);if(t.ok)return Object.keys(t.value).length>0?t.value:void 0}var I="testchimp_session_id",T="testchimp_last_activity",A="testchimp_event_count",y="testchimp_event_type_counts";function l(){try{return typeof window<"u"&&window.localStorage?window.localStorage:null}catch{return null}}function k(e,t,n){if(e&&typeof e=="string"&&e.length>0)return{sessionId:e,isNew:!1};let r=l();if(!r)return{sessionId:h(),isNew:!0};let o=r.getItem(I),s=r.getItem(T),u=Date.now();if(o&&s){let c=parseInt(s,10);if(!isNaN(c)&&u-c<n)return{sessionId:o,isNew:!1}}return{sessionId:h(),isNew:!0}}function C(e,t){let n=l();if(!n)return;let r=Date.now();n.setItem(I,e),n.setItem(T,String(r)),n.setItem(A,"0");let o=_(t);if(o)try{n.setItem("testchimp_session_metadata",JSON.stringify(o))}catch{}let s={};try{n.setItem(y,JSON.stringify(s))}catch{}}function R(){let e=l();e&&e.setItem(T,String(Date.now()))}function w(){let e=l();if(!e)return 0;let t=e.getItem(A),n=parseInt(t??"0",10);return isNaN(n)?0:n}function B(e){let t=l();t&&t.setItem(A,String(e))}function x(){let e=l();if(!e)return{};try{let t=e.getItem(y);if(!t)return{};let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:{}}catch{return{}}}function P(e){let t=l();if(t)try{t.setItem(y,JSON.stringify(e))}catch{}}function V(){let e=l();if(e)try{let t=e.getItem("testchimp_session_metadata");if(!t)return;let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:void 0}catch{return}}function O(){let e=l();e&&(e.removeItem(I),e.removeItem(T),e.removeItem(A),e.removeItem(y),e.removeItem("testchimp_session_metadata"))}function E(e){return e.length<=200?e:e.slice(0,200)}function Z(){if(typeof navigator>"u")return"unknown";let e=navigator.userAgent??"",t=navigator;if(t.userAgentData?.brands?.length){let n=t.userAgentData.brands[0]?.brand;if(n){let r=n==="Not_A Brand"?t.userAgentData.brands[1]?.brand:n;if(r)return E(r==="Edg"?"Edge":r)}}return/Edg\//i.test(e)?"Edge":/Firefox\//i.test(e)?"Firefox":/Chrome\//i.test(e)&&!/Edg\//i.test(e)?"Chrome":/Safari\//i.test(e)&&!/Chrome\//i.test(e)?"Safari":/OPR\//i.test(e)?"Opera":E(e.slice(0,50))||"unknown"}function q(){if(typeof navigator>"u"||typeof window>"u")return"unknown";let e=navigator.userAgent??"";return navigator.userAgentData?.mobile===!0?/iPad/i.test(e)||/Mac/i.test(e)&&typeof navigator.maxTouchPoints=="number"&&navigator.maxTouchPoints>0?"tablet":"mobile":/Tablet|iPad/i.test(e)?"tablet":/Mobile|Android/i.test(e)&&!/iPad/i.test(e)?"mobile":"desktop"}function W(){if(typeof navigator>"u")return"unknown";let e=navigator,t=(e.userAgentData?.platform??e.platform??navigator.userAgent??"").toLowerCase();return t.includes("win")?"windows":t.includes("mac")||t==="macintel"?"mac":t.includes("linux")||t==="linux"?"linux":t.includes("iphone")||t.includes("ipod")||t.includes("ipad")?"ios":t.includes("android")?"android":E(t||"unknown")}function Q(){if(typeof navigator>"u")return"unknown";let e=navigator.language??navigator.userLanguage;return E(e??"unknown")}function ee(){try{if(typeof Intl>"u"||!Intl.DateTimeFormat)return"unknown";let e=Intl.DateTimeFormat().resolvedOptions().timeZone;return E(e??"unknown")}catch{return"unknown"}}function X(){return typeof navigator>"u"?{}:{_browser:Z(),_device_type:q(),_os:W(),_language:Q(),_timezone:ee()}}var te=100,ne=3;function G(e,t,n){let r=[],o=!1;function s(a){return!(w()>=n.maxEventsPerSession||(x()[a]??0)>=n.maxRepeatsPerEvent)}function u(a){let f=w()+1;B(f);let d=x();return d[a]=(d[a]??0)+1,P(d),f}function c(a,f=!1){a.length!==0&&v(e,"/rum/events",{session_id:t,events:a.map(d=>({title:d.title,event_index:d.eventIndex,timestamp_millis:d.timestampMillis,metadata:d.metadata??{}}))},{keepalive:f})}function m(a=!1){if(r.length===0)return;let f=[...r];r=[],c(f,a)}function S(a){if(!s(a.title))return!1;let f=u(a.title);return r.push({...a,eventIndex:f}),!0}function p(){return r.length}function b(){return r}return{add:S,flush:m,getBufferSize:p,getBuffer:b,get maxBufferSize(){return n.maxBufferSize}}}function H(){return{maxEventsPerSession:te,maxRepeatsPerEvent:ne,maxBufferSize:100}}var re="https://ingress.testchimp.io",ie=1e4,oe=100,se=30*60*1e3,i=null;function ae(e){i.flushTimerId&&clearInterval(i.flushTimerId),i.flushTimerId=setInterval(()=>{i&&i.buffer.getBufferSize()>0&&i.buffer.flush(!1)},e)}function U(){document.visibilityState==="hidden"&&i&&i.buffer.flush(!0)}function z(){i&&i.buffer.flush(!0)}function F(e){e.key==="testchimp_session_id"&&e.newValue&&i&&(i.sessionId=e.newValue)}function ue(e){if(!e.projectId||!e.apiKey){console.warn("[testchimp-rum] init: projectId and apiKey are required");return}let t=e.config??{},n=t.captureEnabled!==!1,r=t.testchimpEndpoint??re,o=t.inactivityTimeoutMillis??se,s=t.eventSendInterval??ie,u=t.maxBufferSize??oe,c=_(e.sessionMetadata),{sessionId:m,isNew:S}=k(e.sessionId,c,o);C(m,c??e.sessionMetadata);let p={baseUrl:r,projectId:e.projectId,apiKey:e.apiKey},b={...H(),maxEventsPerSession:t.maxEventsPerSession??100,maxRepeatsPerEvent:t.maxRepeatsPerEvent??3,maxBufferSize:u},a=G(p,m,b);if(i&&i.flushTimerId&&clearInterval(i.flushTimerId),i={httpConfig:p,sessionId:m,buffer:a,captureEnabled:n,flushTimerId:null,initialized:!0},ae(s),typeof document<"u"&&(document.addEventListener("visibilitychange",U),window.addEventListener("beforeunload",z)),typeof window<"u"&&window.addEventListener("storage",F),S&&n){let f=V()??c??{},d=t.enableDefaultSessionMetadata!==!1?{...X(),...f}:f,g={session_id:m,started_at:Date.now(),metadata:d};e.environment&&(g.environment=e.environment),e.release&&(g.release=e.release),e.branchName&&(g.branch_name=e.branchName),v(p,"/rum/session/start",g)}}function fe(e){if(!i||!i.initialized){console.warn("[testchimp-rum] emit: call init() first");return}if(!i.captureEnabled)return;let t=L(e);!t||(R(),!i.buffer.add(t))||i.buffer.getBufferSize()>=i.buffer.maxBufferSize&&i.buffer.flush(!1)}function de(){i&&i.initialized&&i.buffer.flush(!1)}function ce(){return!i||!i.initialized?"":i.sessionId}function le(){i&&(i.flushTimerId&&(clearInterval(i.flushTimerId),i.flushTimerId=null),i=null),typeof document<"u"&&(document.removeEventListener("visibilitychange",U),window.removeEventListener("beforeunload",z)),typeof window<"u"&&window.removeEventListener("storage",F),O()}var me={init:ue,emit:fe,flush:de,getSessionId:ce,resetSession:le},Le=me;export{Le as default,fe as emit,de as flush,ce as getSessionId,ue as init,le as resetSession};
1
+ function j(){if(typeof fetch<"u")return fetch;throw new Error("testchimp-rum-js: fetch is not available. Use a browser or Node 18+.")}function K(){if(typeof window>"u")return;let t=window.__TC_CI_TEST_INFO;return typeof t=="string"&&t.length>0?t:void 0}function Y(e,t){let n=e.baseUrl.replace(/\/$/,""),r=t.startsWith("/")?t:`/${t}`;return`${n}${r}`}async function E(e,t,n,r){try{let o=Y(e,t),s={"Content-Type":"application/json","Project-Id":e.projectId,"TestChimp-Api-Key":e.apiKey,...r?.keepalive&&{"Keep-Alive":"true"}},u=K();return u&&(s["ci-test-info"]=u),(await j()(o,{method:"POST",headers:s,body:JSON.stringify(n)})).ok}catch{return!1}}var M="0123456789ABCDEFGHJKMNPQRSTVWXYZ";function I(){let e=Date.now(),t=$(e,10),n=J(16);return t+n}function $(e,t){let n="";for(let r=t;r>0;r--)n=M[e%32]+n,e=Math.floor(e/32);return n}function J(e){let t=new Uint8Array(e);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(t);else for(let r=0;r<e;r++)t[r]=Math.floor(Math.random()*256);let n="";for(let r=0;r<e;r++)n+=M[t[r]%32];return n}function N(e){return e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function D(e){if(typeof e!="object"||e===null||Array.isArray(e))return{ok:!1,reason:"metadata must be a plain object"};let t=Object.keys(e);if(t.length>10)return{ok:!1,reason:"metadata has more than 10 keys"};let n={};for(let r of t){if(r.length>50)return{ok:!1,reason:"metadata key exceeds 50 chars"};let o=e[r];if(Array.isArray(o)){if(o.length>50)return{ok:!1,reason:"metadata array length exceeds 50"};for(let s=0;s<o.length;s++){let u=o[s];if(!N(u))return{ok:!1,reason:"metadata array values must be string, number, boolean, or null"};if(typeof u=="string"&&u.length>200)return{ok:!1,reason:"metadata string in array exceeds 200 chars"}}n[r]=o}else if(N(o)){if(typeof o=="string"&&o.length>200)return{ok:!1,reason:"metadata string value exceeds 200 chars"};n[r]=o}else return{ok:!1,reason:"metadata values must be primitive or array of primitives (no nested objects)"}}return{ok:!0,value:n}}function L(e){if(!e.title||typeof e.title!="string")return console.warn("[testchimp-rum] Event dropped: title is required"),null;if(e.title.length>100)return console.warn("[testchimp-rum] Event dropped: title exceeds 100 chars"),null;let t;if(e.metadata!=null&&typeof e.metadata=="object"&&!Array.isArray(e.metadata)){let o=D(e.metadata);if(!o.ok)return console.warn(`[testchimp-rum] Event dropped: ${o.reason}`),null;t=Object.keys(o.value).length>0?o.value:void 0}let n={title:e.title,timestampMillis:Date.now(),metadata:t},r=JSON.stringify(n);return new TextEncoder().encode(r).length>5120?(console.warn("[testchimp-rum] Event dropped: total size exceeds 5120 bytes"),null):n}function v(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t=D(e);if(t.ok)return Object.keys(t.value).length>0?t.value:void 0}var b="testchimp_session_id",T="testchimp_last_activity",_="testchimp_event_count",A="testchimp_event_type_counts";function l(){try{return typeof window<"u"&&window.localStorage?window.localStorage:null}catch{return null}}function C(e,t,n){if(e&&typeof e=="string"&&e.length>0)return{sessionId:e,isNew:!1};let r=l();if(!r)return{sessionId:I(),isNew:!0};let o=r.getItem(b),s=r.getItem(T),u=Date.now();if(o&&s){let d=parseInt(s,10);if(!isNaN(d)&&u-d<n)return{sessionId:o,isNew:!1}}return{sessionId:I(),isNew:!0}}function k(e,t){let n=l();if(!n)return;let r=Date.now();n.setItem(b,e),n.setItem(T,String(r)),n.setItem(_,"0");let o=v(t);if(o)try{n.setItem("testchimp_session_metadata",JSON.stringify(o))}catch{}let s={};try{n.setItem(A,JSON.stringify(s))}catch{}}function R(){let e=l();e&&e.setItem(T,String(Date.now()))}function w(){let e=l();if(!e)return 0;let t=e.getItem(_),n=parseInt(t??"0",10);return isNaN(n)?0:n}function B(e){let t=l();t&&t.setItem(_,String(e))}function x(){let e=l();if(!e)return{};try{let t=e.getItem(A);if(!t)return{};let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:{}}catch{return{}}}function P(e){let t=l();if(t)try{t.setItem(A,JSON.stringify(e))}catch{}}function V(){let e=l();if(e)try{let t=e.getItem("testchimp_session_metadata");if(!t)return;let n=JSON.parse(t);return typeof n=="object"&&n!==null?n:void 0}catch{return}}function O(){let e=l();e&&(e.removeItem(b),e.removeItem(T),e.removeItem(_),e.removeItem(A),e.removeItem("testchimp_session_metadata"))}function y(e){return e.length<=200?e:e.slice(0,200)}function Z(){if(typeof navigator>"u")return"unknown";let e=navigator.userAgent??"";return/Edg\//i.test(e)?"Edge":/Firefox\//i.test(e)?"Firefox":/Chrome\//i.test(e)&&!/Edg\//i.test(e)?"Chrome":/Safari\//i.test(e)&&!/Chrome\//i.test(e)?"Safari":/OPR\//i.test(e)?"Opera":y(e.slice(0,50))||"unknown"}function q(){if(typeof navigator>"u"||typeof window>"u")return"unknown";let e=navigator.userAgent??"";return navigator.userAgentData?.mobile===!0?/iPad/i.test(e)||/Mac/i.test(e)&&typeof navigator.maxTouchPoints=="number"&&navigator.maxTouchPoints>0?"tablet":"mobile":/Tablet|iPad/i.test(e)?"tablet":/Mobile|Android/i.test(e)&&!/iPad/i.test(e)?"mobile":"desktop"}function W(){if(typeof navigator>"u")return"unknown";let e=navigator,t=(e.userAgentData?.platform??e.platform??navigator.userAgent??"").toLowerCase();return t.includes("win")?"windows":t.includes("mac")||t==="macintel"?"mac":t.includes("linux")||t==="linux"?"linux":t.includes("iphone")||t.includes("ipod")||t.includes("ipad")?"ios":t.includes("android")?"android":y(t||"unknown")}function Q(){if(typeof navigator>"u")return"unknown";let e=navigator.language??navigator.userLanguage;return y(e??"unknown")}function ee(){try{if(typeof Intl>"u"||!Intl.DateTimeFormat)return"unknown";let e=Intl.DateTimeFormat().resolvedOptions().timeZone;return y(e??"unknown")}catch{return"unknown"}}function X(){return typeof navigator>"u"?{}:{_browser:Z(),_device_type:q(),_os:W(),_language:Q(),_timezone:ee()}}var te=100,ne=3;function G(e,t,n){let r=[],o=!1;function s(a){return!(w()>=n.maxEventsPerSession||(x()[a]??0)>=n.maxRepeatsPerEvent)}function u(a){let f=w()+1;B(f);let c=x();return c[a]=(c[a]??0)+1,P(c),f}function d(a,f=!1){a.length!==0&&E(e,"/rum/events",{session_id:t,events:a.map(c=>({title:c.title,event_index:c.eventIndex,timestamp_millis:c.timestampMillis,metadata:c.metadata??{}}))},{keepalive:f})}function m(a=!1){if(r.length===0)return;let f=[...r];r=[],d(f,a)}function S(a){if(!s(a.title))return!1;let f=u(a.title);return r.push({...a,eventIndex:f}),!0}function p(){return r.length}function h(){return r}return{add:S,flush:m,getBufferSize:p,getBuffer:h,get maxBufferSize(){return n.maxBufferSize}}}function H(){return{maxEventsPerSession:te,maxRepeatsPerEvent:ne,maxBufferSize:100}}var re="https://ingress.testchimp.io",ie=1e4,oe=100,se=30*60*1e3,i=null;function ae(e){i.flushTimerId&&clearInterval(i.flushTimerId),i.flushTimerId=setInterval(()=>{i&&i.buffer.getBufferSize()>0&&i.buffer.flush(!1)},e)}function U(){document.visibilityState==="hidden"&&i&&i.buffer.flush(!0)}function z(){i&&i.buffer.flush(!0)}function F(e){e.key==="testchimp_session_id"&&e.newValue&&i&&(i.sessionId=e.newValue)}function ue(e){if(!e.projectId||!e.apiKey){console.warn("[testchimp-rum] init: projectId and apiKey are required");return}let t=e.config??{},n=t.captureEnabled!==!1,r=t.testchimpEndpoint??re,o=t.inactivityTimeoutMillis??se,s=t.eventSendInterval??ie,u=t.maxBufferSize??oe,d=v(e.sessionMetadata),{sessionId:m,isNew:S}=C(e.sessionId,d,o);k(m,d??e.sessionMetadata);let p={baseUrl:r,projectId:e.projectId,apiKey:e.apiKey},h={...H(),maxEventsPerSession:t.maxEventsPerSession??100,maxRepeatsPerEvent:t.maxRepeatsPerEvent??3,maxBufferSize:u},a=G(p,m,h);if(i&&i.flushTimerId&&clearInterval(i.flushTimerId),i={httpConfig:p,sessionId:m,buffer:a,captureEnabled:n,flushTimerId:null,initialized:!0},ae(s),typeof document<"u"&&(document.addEventListener("visibilitychange",U),window.addEventListener("beforeunload",z)),typeof window<"u"&&window.addEventListener("storage",F),S&&n){let f=V()??d??{},c=t.enableDefaultSessionMetadata!==!1?{...X(),...f}:f,g={session_id:m,started_at:Date.now(),metadata:c};e.environment&&(g.environment=e.environment),e.release&&(g.release=e.release),e.branchName&&(g.branch_name=e.branchName),E(p,"/rum/session/start",g)}}function fe(e){if(!i||!i.initialized){console.warn("[testchimp-rum] emit: call init() first");return}if(!i.captureEnabled)return;let t=L(e);!t||(R(),!i.buffer.add(t))||i.buffer.getBufferSize()>=i.buffer.maxBufferSize&&i.buffer.flush(!1)}function ce(){i&&i.initialized&&i.buffer.flush(!1)}function de(){return!i||!i.initialized?"":i.sessionId}function le(){i&&(i.flushTimerId&&(clearInterval(i.flushTimerId),i.flushTimerId=null),i=null),typeof document<"u"&&(document.removeEventListener("visibilitychange",U),window.removeEventListener("beforeunload",z)),typeof window<"u"&&window.removeEventListener("storage",F),O()}var me={init:ue,emit:fe,flush:ce,getSessionId:de,resetSession:le},Le=me;export{Le as default,fe as emit,ce as flush,de as getSessionId,ue as init,le as resetSession};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testchimp-rum-js",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Lightweight browser library for emitting structured user interaction events to TestChimp - for test coverage analytics grounded in real user behaviour",
5
5
  "main": "dist/testchimp-rum.min.js",
6
6
  "module": "dist/testchimp-rum.mjs",