@groundcover/browser 0.0.77-rc.0 → 0.0.77-rc.2

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.
package/README.md CHANGED
@@ -140,7 +140,16 @@ export interface SDKOptions {
140
140
  }
141
141
  ```
142
142
 
143
- `sessionMaxDuration` caps wall-clock session length (default 4 hours, must be between 1 minute and 8 hours). The cap is enforced lazily: when the next user/business event is produced after the cap has elapsed, the SDK flushes pending events under the current session id, generates a fresh id, and resumes replay recording if it had been active. A dormant tab that produces no further events keeps its session id (session replay batches don't count, so a backgrounded recording won't spawn empty sessions). The flush is best-effort and the rotation always proceeds regardless of delivery success. Values outside that range or otherwise invalid fall back to the default with a `console.warn`.
143
+ `sessionMaxDuration` sets a **target** maximum wall-clock session length (default 4 hours, must be between 1 minute and 8 hours).
144
+
145
+ It is enforced **lazily, on activity — not by a background timer**, so it is **not a hard upper bound**. Once the cap has elapsed, the *next* user/business event (click, navigation, log, custom, network, exception, …) flushes pending events under the current session id, generates a fresh id, and resumes replay recording if it had been active.
146
+
147
+ Because rotation is activity-gated, a session that goes idle keeps its id past the cap until the next qualifying event:
148
+
149
+ - A dormant or backgrounded tab that produces no further events stays on the same session id — by design, so the SDK doesn't mint "phantom" sessions for tabs nobody is using.
150
+ - **Session-replay batches don't count as activity** (rrweb emits a snapshot heartbeat every ~30s regardless of interaction), so a tab that's only recording replay won't rotate on that alone. Such a tab is ultimately bounded by the 30-minute replay-inactivity stop, not by this cap.
151
+
152
+ The flush is best-effort and the rotation always proceeds regardless of delivery success. Values outside the allowed range or otherwise invalid fall back to the default with a `console.warn`.
144
153
 
145
154
  You can pass the values by calling the init function:
146
155
 
package/dist/index.d.mts CHANGED
@@ -205,16 +205,41 @@ interface SDKOptions {
205
205
  blockedSelectors?: string[];
206
206
  };
207
207
  /**
208
- * Maximum wall-clock duration of a session, in milliseconds. The cap is
209
- * enforced lazily: the next user/business event after the cap has elapsed
210
- * flushes buffered events under the current session id, then generates a
211
- * fresh session id and restarts replay recording if it was active. Session
212
- * replay batches never trigger this on their own. The flush is best-effort
213
- * and the rotation always proceeds it does not wait for or depend on
214
- * successful delivery, so if that network request fails the batch may be
215
- * lost while the new session id still takes effect. Defaults to 4 hours when
216
- * omitted; must be between 1 minute and 8 hours. Values outside that range
217
- * or otherwise invalid fall back to the 4-hour default with a `console.warn`.
208
+ * Target maximum wall-clock duration of a session, in milliseconds.
209
+ *
210
+ * Enforced LAZILY, on activity not by a background timer. Once the cap has
211
+ * elapsed, the next user/business event (click, navigation, log, custom,
212
+ * network, exception, …) flushes buffered events under the current session
213
+ * id, then mints a fresh session id and restarts replay recording if it was
214
+ * active.
215
+ *
216
+ * Because it is activity-gated, this is NOT a hard upper bound: a session
217
+ * only rotates when the next qualifying event arrives. An idle or
218
+ * backgrounded tab that produces no such events — including one that is only
219
+ * emitting session-replay heartbeats, which never trigger rotation on their
220
+ * own — keeps the same session id past the cap until activity resumes. This
221
+ * is deliberate: it avoids minting "phantom" sessions for dormant tabs. (An
222
+ * idle tab with replay on is ultimately bounded by the 30-minute user-idle
223
+ * pause — recording stops when no user presence is detected — not by this
224
+ * cap.)
225
+ *
226
+ * Sessions are also bounded by a 30-minute inactivity gap, enforced the same
227
+ * lazy way: when a user-interaction event (click, navigation, pageload,
228
+ * custom — not background network/log/performance traffic) arrives after 30+
229
+ * minutes without one, the session rotates first and replay recording
230
+ * restarts for the returning user. Raw user input (scroll, pointer, key,
231
+ * touch) also counts as presence even when it produces no SDK event, so
232
+ * passive engagement — reading, watching — doesn't end the session. The
233
+ * same gap check runs at init, so a reload after a long break also starts a
234
+ * fresh session.
235
+ *
236
+ * The flush is best-effort and the rotation always proceeds — it does not
237
+ * wait for or depend on successful delivery, so if that network request
238
+ * fails the batch may be lost while the new session id still takes effect.
239
+ *
240
+ * Defaults to 4 hours when omitted; must be between 1 minute and 8 hours.
241
+ * Values outside that range or otherwise invalid fall back to the 4-hour
242
+ * default with a `console.warn`.
218
243
  */
219
244
  sessionMaxDuration?: number;
220
245
  }
package/dist/index.d.ts CHANGED
@@ -205,16 +205,41 @@ interface SDKOptions {
205
205
  blockedSelectors?: string[];
206
206
  };
207
207
  /**
208
- * Maximum wall-clock duration of a session, in milliseconds. The cap is
209
- * enforced lazily: the next user/business event after the cap has elapsed
210
- * flushes buffered events under the current session id, then generates a
211
- * fresh session id and restarts replay recording if it was active. Session
212
- * replay batches never trigger this on their own. The flush is best-effort
213
- * and the rotation always proceeds it does not wait for or depend on
214
- * successful delivery, so if that network request fails the batch may be
215
- * lost while the new session id still takes effect. Defaults to 4 hours when
216
- * omitted; must be between 1 minute and 8 hours. Values outside that range
217
- * or otherwise invalid fall back to the 4-hour default with a `console.warn`.
208
+ * Target maximum wall-clock duration of a session, in milliseconds.
209
+ *
210
+ * Enforced LAZILY, on activity not by a background timer. Once the cap has
211
+ * elapsed, the next user/business event (click, navigation, log, custom,
212
+ * network, exception, …) flushes buffered events under the current session
213
+ * id, then mints a fresh session id and restarts replay recording if it was
214
+ * active.
215
+ *
216
+ * Because it is activity-gated, this is NOT a hard upper bound: a session
217
+ * only rotates when the next qualifying event arrives. An idle or
218
+ * backgrounded tab that produces no such events — including one that is only
219
+ * emitting session-replay heartbeats, which never trigger rotation on their
220
+ * own — keeps the same session id past the cap until activity resumes. This
221
+ * is deliberate: it avoids minting "phantom" sessions for dormant tabs. (An
222
+ * idle tab with replay on is ultimately bounded by the 30-minute user-idle
223
+ * pause — recording stops when no user presence is detected — not by this
224
+ * cap.)
225
+ *
226
+ * Sessions are also bounded by a 30-minute inactivity gap, enforced the same
227
+ * lazy way: when a user-interaction event (click, navigation, pageload,
228
+ * custom — not background network/log/performance traffic) arrives after 30+
229
+ * minutes without one, the session rotates first and replay recording
230
+ * restarts for the returning user. Raw user input (scroll, pointer, key,
231
+ * touch) also counts as presence even when it produces no SDK event, so
232
+ * passive engagement — reading, watching — doesn't end the session. The
233
+ * same gap check runs at init, so a reload after a long break also starts a
234
+ * fresh session.
235
+ *
236
+ * The flush is best-effort and the rotation always proceeds — it does not
237
+ * wait for or depend on successful delivery, so if that network request
238
+ * fails the batch may be lost while the new session id still takes effect.
239
+ *
240
+ * Defaults to 4 hours when omitted; must be between 1 minute and 8 hours.
241
+ * Values outside that range or otherwise invalid fall back to the 4-hour
242
+ * default with a `console.warn`.
218
243
  */
219
244
  sessionMaxDuration?: number;
220
245
  }
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("fflate"),t=require("error-stack-parser"),n=require("web-vitals"),i=require("rrweb"),s=require("@rrweb/packer");function r(e){return e&&e.__esModule?e:{default:e}}var a=r(t),o=console.log.bind(console),l=class e{constructor(){this.isDebugEnabled=!1,this.prefix=""}static initialize(t){return e.instance||(e.instance=new e),t&&(e.instance.isDebugEnabled=t.debug??!1,e.instance.prefix=t.prefix??""),e.instance}static getInstance(t){return e?.instance||e.initialize(t)}formatMessage(e){return`[${(new Date).toISOString()}] ${this.prefix} ${e}`}log(e,...t){this.isDebugEnabled&&o(this.formatMessage(e),...t)}updateConfig(e){this.isDebugEnabled=e.debug??this.isDebugEnabled,this.prefix=e.prefix??this.prefix}},c=l.getInstance();function d(e){c.log("[error-handler.handleError] called",e,{groundcoverIgnore:!0})}var h="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:{},g=1e6;function u(e){return e*g}function p(e){return e/g}var m=class{constructor(){this.generateTraceId=function(){for(let e=0;e<16;e++)f[e]=Math.floor(16*Math.random())+48,f[e]>=58&&(f[e]+=39);return y+String.fromCharCode.apply(null,f.slice(0,16))},this.generateSpanId=v(8),this.generateId=v(16)}},f=Array(32);function v(e){return function(){for(let t=0;t<2*e;t++)f[t]=Math.floor(16*Math.random())+48,f[t]>=58&&(f[t]+=39);return String.fromCharCode.apply(null,f.slice(0,2*e))}}var y="0000000000000000";var b=144e5,E=288e5,I={batchSize:10,batchTimeout:1e4,eventSampleRate:1,sessionSampleRate:1,environment:"development",debug:!1,enableCompression:!0,maskFields:[],enableMasking:!1,enabledEvents:[],tracePropagationUrls:[],tracePropagationHeaders:[],tracePropagationTraceIdHeaderName:"",tracePropagationSpanIdHeaderName:"",traceOrigin:{name:"",value:""},sessionMaxDuration:b},S=class e{constructor(t){this.dsn=t?.dsn||"",this.appId=t?.appId||"",this.cluster=t?.cluster||"",this.apiKey=t?.apiKey||"",this.environment=t?.environment||"",this.namespace=t?.namespace||"",this.releaseId=t?.releaseId,this.userIdentifier=t?.userIdentifier||null,this.options={...I,...t?.options||{}},this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t?.options?.sessionMaxDuration)}static normalizeSessionMaxDuration(e){return void 0===e?b:"number"!=typeof e||!Number.isFinite(e)||e<6e4||e>E?(console.warn("[groundcover] sessionMaxDuration must be a finite number between 60000 ms (1 minute) and 28800000 ms (8 hours); falling back to the 4-hour default"),b):e}getEndpoint(){return this.dsn?.startsWith("http")?this.dsn:`https://${this.dsn}`}updateConfig(t){t.options&&(this.options={...this.options,...t.options},void 0!==t.options.sessionMaxDuration&&(this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t.options.sessionMaxDuration))),t.userIdentifier&&(this.userIdentifier={...this.userIdentifier,...t.userIdentifier}),t.appId&&(this.appId=t.appId),t.dsn&&(this.dsn=t.dsn),t.apiKey&&(this.apiKey=t.apiKey),t.cluster&&(this.cluster=t.cluster),t.environment&&(this.environment=t.environment),t.namespace&&(this.namespace=t.namespace),t.releaseId&&(this.releaseId=t.releaseId)}};function T(e){try{return globalThis?.sessionStorage?.getItem(e)??null}catch(e){return d(e),null}}function w(e,t){try{globalThis?.sessionStorage?.setItem(e,t)}catch(e){d(e)}}function R(e){try{globalThis?.sessionStorage?.removeItem(e)}catch(e){d(e)}}var N=class e{constructor(){this.config=null,this.logger=l.getInstance(),this.sessionId=null,this.sessionStartTime=null}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(e){this.config||(this.config=new S(e))}getConfig(){return this.config?this.config:(this.logger.log("[config-manager] configuration not initialized"),null)}getSessionId(){if(this.sessionId)return this.sessionId;return T("gcId")||""}setSessionId(e){this.sessionId=e,w("gcId",e)}getSessionStartTime(){return this.sessionStartTime?this.sessionStartTime:function(e,t=0){const n=T(e);if(null===n)return t;const i=Number(n);return Number.isFinite(i)?i:t}("gcStartTime",0)}setSessionStartTime(e){this.sessionStartTime=e,w("gcStartTime",String(e))}getSessionMaxDuration(){return this.config?.options?.sessionMaxDuration??b}getSessionElapsedMs(e=Date.now()){const t=this.getSessionStartTime();return t?e-p(t):0}isSessionExpired(e=Date.now()){return this.getSessionElapsedMs(e)>=this.getSessionMaxDuration()}updateConfig(e){this.logger.log("[config-manager] updateConfig called"),this.config?(e&&(this.logger.log("[config-manager] updating options"),this.config.updateConfig(e)),e?.userIdentifier&&(this.logger.log("[config-manager] updating user identifier"),this.config.userIdentifier={...this.config.userIdentifier,...e.userIdentifier})):this.logger.log("[config-manager] configuration not initialized")}},L=N.getInstance(),z=new m,C=l.getInstance();function q(){const e=L?.getConfig()?.options?.eventSampleRate;return!(void 0!==e&&!Number.isNaN(e))||Math.random()<=e}function H(e,t="",n=new WeakSet,i=0){try{return i>=10?{[t||"depth_limit"]:"[Depth Limit]"}:null!=(s=e)&&"number"==typeof s.nodeType&&"string"==typeof s.nodeName?{[t||"dom_node"]:`[${e?.constructor?.name||"DomNode"}]`}:n.has(e)?{[t||"circular_reference"]:"[Circular]"}:(n.add(e),Object.entries(e).reduce(((e,[s,r])=>{const a=t?`${t}.${s}`:s;return"object"==typeof r&&null!==r?Array.isArray(r)?e[a]=r.map(((e,t)=>"object"==typeof e&&null!==e?H(e,`${a}[${t}]`,n,i+1):e)):Object.assign(e,H(r,a,n,i+1)):e[a]=r,e}),{}))}catch(e){return C.log("[events-processors.formatEventObject] Error formatting event object",e),{[t||"format_error"]:"[Error formatting object]"}}finally{n.delete(e)}var s}function _(e){const t=u(Date.now()),n=z.generateId(),i=e.spanId||z.generateSpanId(),s=e.traceId||z.generateTraceId(),r=e.parentSpanId||"",a=H(e.attributes||{}),o=function(){const e=h?.location;return e?{path:e.hash&&!e.hash.match(/^#[a-z0-9-]+$/i)&&e.hash.startsWith("#/")?e.hash:e.pathname,url:e.href,title:h?.document?.title||""}:{path:"",url:"",title:h?.document?.title||""}}(),l={type:e.type,id:n,spanId:i,parentSpanId:r,traceId:s,attributes:{...a,location:o}};return e.span_name&&(l.span_name=e.span_name),"network"===e.type?(l.start_timestamp=e.timestamp||t,l.end_timestamp=e?.end_timestamp):l.timestamp=t,l}function x({text:e,maxLength:t=1e4}){return e?e.length<=t||"string"!=typeof e?e:e.substring(0,t):""}var k=class{constructor(){this.logger=l.getInstance(),this.config=N.getInstance()}compress(t){const n=this.config.getConfig();if(!n?.options?.enableCompression)return{data:(new TextEncoder).encode(t),isCompressed:!1};try{const n=e.strToU8(t);return{data:e.gzipSync(n),isCompressed:!0}}catch(e){return{data:(new TextEncoder).encode(t),isCompressed:!1}}}async send(e){const t=this.config.getConfig();if(t)try{t?.options?.debug&&this.logger.log("Sending batch:",e,{groundcoverIgnore:!0});const n=t.apiKey,i=this.buildEndpoint();if(!n)return void this.logger.log("No API key found");const s=JSON.stringify(e),{data:r,isCompressed:a}=this.compress(s);fetch(i,{method:"POST",headers:{"Content-Type":"application/json",apikey:n,"Content-Encoding":a?"gzip":""},body:r}).catch((e=>{this.logger.log("Network error while sending batch:",e,{groundcoverIgnore:!0})}))}catch(e){t.options.debug&&this.logger.log("Failed to send batch:",e,{groundcoverIgnore:!0})}}buildEndpoint(){const e=this.config.getConfig();if(!e)return"";const{dsn:t}=e;return`${t}/json/rum`}},M=["navigation","dom.event"],D=class e{constructor(){this.events=[],this.timeoutId=null,this.config=N.getInstance(),this.logger=l.getInstance(),this.initialized=!1,this.onBeforeEnqueue=null,this.transporter=new k}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(){try{if(this.initialized)return;this.scheduleFlush(),"undefined"!=typeof document&&document.addEventListener("visibilitychange",(()=>{"hidden"===document.visibilityState&&this.flush()})),this.initialized=!0}catch(e){d(e)}}setBeforeEnqueueHook(e){this.onBeforeEnqueue=e}addEvent(e){if(e&&this.initialized)try{const t=this.config.getConfig()?.options?.beforeSend,n=this.config.getConfig()?.options?.enrichEvent;if(t&&!t(e))return;if(this.onBeforeEnqueue)try{this.onBeforeEnqueue(e)}catch(e){d(e)}n&&(e=n(e)),this.events.push(e);const i=this.config.getConfig()?.options?.batchSize||100;this.events.length>=i&&!M.includes(e.type)&&this.flush()}catch(e){d(e)}}flush(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[],this.scheduleFlush()}catch(e){d(e)}}async flushSync(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");await this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[]}catch(e){d(e)}}scheduleFlush(){try{null!==this.timeoutId&&(h.clearTimeout(this.timeoutId),this.timeoutId=null);const e=this.config.getConfig()?.options?.batchTimeout;if(!e)return;this.timeoutId=h.setTimeout((()=>{this.timeoutId=null,this.flush()}),e)}catch(e){d(e)}}detectBrowser(){const e=navigator.userAgent;let t="unknown",n="unknown";const i=navigator.platform||"unknown",s=/Mobile|Android|iPhone|iPad|iPod/i.test(e);return/Edg/.test(e)?(t="Edge",n=e.match(/Edg\/([\d.]+)/)?.[1]||n):/Chrome/.test(e)&&!/Chromium/.test(e)?(t="Chrome",n=e.match(/Chrome\/([\d.]+)/)?.[1]||n):/Firefox/.test(e)?(t="Firefox",n=e.match(/Firefox\/([\d.]+)/)?.[1]||n):/Safari/.test(e)&&!/Chrome/.test(e)?(t="Safari",n=e.match(/Version\/([\d.]+)/)?.[1]||n):/Trident/.test(e)?(t="Internet Explorer",n=/rv:([^)]+)\)/i.test(e)?e.match(/rv:([^)]+)\)/i)?.[1]||n:e.match(/MSIE ([^;]+)/)?.[1]||n):/OPR/.test(e)&&(t="Opera",n=e.match(/OPR\/([\d.]+)/)?.[1]||n),{name:t,version:n,platform:i,language:navigator.language,mobile:s}}getSessionAttributes(){const e=this.detectBrowser(),t=this.config.getConfig();return{cluster:t?.cluster||"",env:t?.environment||"",namespace:t?.namespace||"",releaseId:t?.releaseId||"",session_id:this.config.getSessionId(),session_start_time:this.config.getSessionStartTime(),user:t?.userIdentifier||{},"service.name":t?.appId,userAgent:navigator.userAgent,browser:e}}};D.instance=null;var P=D,A=class{constructor(){this.logger=l.getInstance(),this.eventsPool=P.getInstance()}},O="undefined"!=typeof Element?Object.getOwnPropertyDescriptor(Element.prototype,"tagName")?.get:void 0;function j(e,t){try{return e.getAttribute(t)||""}catch{return""}}function F(e){try{const t=O?.call(e);return"string"==typeof t?t:""}catch{return""}}var U=class extends A{constructor(){super(...arguments),this.eventHandlers=[],this.capturedEvents=["click","change","keydown","select","submit"],this.config=N.getInstance(),this.getShouldMaskText=e=>{const t=this.config.getConfig(),n=t?.options?.maskFields||[],i=t?.options?.enableMasking||!1;return(["password"===e.target?.type,e.target?.id?.toLowerCase().includes("password"),e.target?.id?.toLowerCase().includes("credit-card"),e.target?.id?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.hasAttribute("data-private")].some((e=>e))||n.some((t=>e.target?.id?.toLowerCase().includes(t))))&&i},this.getKeyCode=e=>{if(e instanceof KeyboardEvent&&e.code&&"Dead"!==e.key){const t=this.getShouldMaskText({target:e.target});return t||t?"*":e.key}return""},this.getText=e=>{const t=e.target,n=this.config.getConfig();return n?.options?.enableMasking||!1?"*":x({text:t.innerText||""})},this.getSelector=e=>e.target instanceof Element&&this.generateSelector(e.target)||"",this.getCoordinates=e=>{if({mouseup:!0,mousedown:!0,mousemove:!0,mouseover:!0}[e.type]){const{clientX:t,clientY:n}=e||{};return{clientX:t,clientY:n}}return null}}initialize(){try{this.logger.log("[dom-events-listener.initialize] called"),this.capturedEvents?.forEach((e=>{const t=e=>{try{this.handleEvent(e)}catch(e){d(e)}};this.eventHandlers.push({type:e,handler:t}),h?.addEventListener(e,t)}))}catch(e){d(e)}}destroy(){this.eventHandlers.forEach((({type:e,handler:t})=>{global?.removeEventListener?.(e,t)})),this.eventHandlers=[]}handleEvent(e){try{if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}}buildEvent(e){const t=this.getSelector(e),n=this.getCoordinates(e),i=e.target,s=this.getKeyCode(e),r=this.getText(e);this.logger.log("[dom-events-listener.buildEvent] called");return _({type:"dom.event",attributes:{dom_event_selector:t,dom_event_key_code:s,dom_event_type:e.type,dom_event_coordinates:n||{clientX:0,clientY:0},dom_event_target:{id:j(i,"id"),tagName:F(i),className:j(i,"class"),text:r}}})}queueEvent(e){try{this.logger.log("[dom-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}generateSelector(e,t={}){if(!e||!e.nodeType||e.nodeType!==Node.ELEMENT_NODE)return"";const n=t.root||document.body||document.documentElement,i=t.maxAttempts||10,s=t.priorityAttributes||["id","class","name","aria-label","type","title","alt"];if("html"===F(e).toLowerCase())return"html";let r="",a=0,o=e;const l=[];for(;o&&o!==n&&o!==document.documentElement&&a<i;){let e=F(o).toLowerCase();try{const t=o.getAttribute("id");if(t&&/^[a-zA-Z][\w-]*$/.test(t))return`#${t}`;if(o.classList?.length){const t=Array.from(o.classList).filter((e=>e&&"string"==typeof e)).map((e=>`.${e}`));t.length&&(e+=t.join(""))}for(const t of s)if("id"!==t&&"class"!==t)try{const n=o.getAttribute(t);n&&(e+=`[${t}="${n.replace(/"/g,'\\"')}"]`)}catch(e){}}catch(t){e=F(o).toLowerCase()||"*"}l.unshift(e||"*"),r=l.join(" > ");try{if(1===n.querySelectorAll(r).length)return r}catch(e){l.shift(),r=l.join(" > ")}try{o=o.parentElement}catch(e){break}a++}return r||"*"}},B=["log","info","warn","error","debug","assert","trace"],$=class extends A{constructor(){super(...arguments),this.originalConsole=null,this.isInitialized=!1}initialize(){if(!this.isInitialized)try{this.originalConsole=this.captureConsoleMethods(),B.forEach((e=>{e in console&&(console[e]=(...t)=>{try{this.handleEvent(t,e)}catch(e){d(e)}finally{this.originalConsole?.[e]&&this.originalConsole[e](...t)}})})),this.isInitialized=!0}catch(e){d(e)}}destroy(){try{if(!this.isInitialized)return;B.forEach((e=>{this.originalConsole&&this.originalConsole[e]&&(console[e]=this.originalConsole[e])})),this.isInitialized=!1}catch(e){d(e)}}handleEvent(e,t){try{if(!q())return;const n=Array.isArray(e)?e:[e],i=this.formatMessage(n,t);if(!i||i?.includes("groundcoverIgnore"))return;const s=this.extractAttributes(n),r=this.buildEvent({message:i,level:t,attributes:s});this.queueEvent(r)}catch(e){d(e)}}buildEvent({message:e,level:t,attributes:n}){return _({type:"log",attributes:{...n,message:x({text:e}),level:t}})}queueEvent(e){try{this.logger.log("[logs-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}extractAttributes(e){if(Array.isArray(e)&&0!==e.length)try{const t={};for(const n of e)null!==n&&"object"==typeof n&&"[object Object]"===Object.prototype.toString.call(n)&&Object.assign(t,n);return delete t.message,delete t.level,delete t.location,t}catch(e){return void d(e)}}formatMessage(e,t){if(!Array.isArray(e))return String(e);try{return e.map((e=>{if(void 0===e)return"undefined";if(null===e)return"null";if("object"==typeof e){if(e instanceof Error)try{return"error"===t&&e.stack||e.toString()}catch{try{const t={name:e.name,message:e.message,stack:e.stack};return JSON.stringify(t)}catch{return Object.prototype.toString.call(e)}}try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return String(e)})).join(" ")}catch(e){return d(e),"[Error formatting console message]"}}captureConsoleMethods(){const e={};try{B.forEach((t=>{console&&"function"==typeof console[t]&&(e[t]=console[t].bind(console))}))}catch(e){d(e)}return e}};function K(e){try{if(null==e)return"";if("string"==typeof e)return e;if(e instanceof FormData)return"[form data]";if(e instanceof Blob)return"[blob data]";if(e instanceof ArrayBuffer)return"[arraybuffer data]";if("undefined"!=typeof Document){if(e instanceof Document)return"[document data]";const t=e;if(9===t?.nodeType)return"[document data]"}if(e instanceof URLSearchParams)try{return e.toString()}catch{return"[unreadable body]"}return Array.isArray(e)||"object"==typeof e?JSON.stringify(e):String(e)}catch(e){return d(`Failed to format body: ${e}`),"[unreadable body]"}}var X,V=class extends A{constructor(){super(...arguments),this.maxBodyLength=5e3,this.config=N.getInstance(),this.idGenerator=new m,this.handleEvent=e=>{try{if(this.shouldIgnoreRequest(e.url))return;if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}},this.buildEvent=e=>{let t=e.url,n=e.url;this.logger.log("[network-events-listener.buildEvent] called",{event:e});try{t=new URL(e.url).pathname}catch(t){n=new URL(e.url,globalThis.location.href).href}this.logger.log("[network-events-listener.buildEvent] fullUrl",{fullUrl:n});const i=this.formatHeaders(e.request.headers),s=this.formatHeaders(e.response.headers),r=e.method?.toUpperCase()||"UNKNOWN",{traceId:a,spanId:o}=this.extractTraceIds(e.request.headers),l=x({text:e.request.body,maxLength:this.maxBodyLength}),c=x({text:e.response.body,maxLength:this.maxBodyLength}),d={type:"HTTP",operation:{name:r},resource_name:t,status:e.status?.toString(),subType:r,url:{full:n},http:{route:t,path:t,method:e.method,status:e.status?.toString(),request:{headers:i,method:e.method},response:{headers:s,status_code:e.status?.toString()}},error:e?.error?.type?{type:e.error?.type||"Unknown error"}:void 0,gc:{request:{body:l},response:{body:c}}},h={type:"network",timestamp:e?.timestamp&&!Number.isNaN(e.timestamp)?u(e.timestamp):void 0,end_timestamp:e?.end_time&&!Number.isNaN(e.end_time)?u(e.end_time):void 0,span_name:`${e.method} ${t}`,attributes:d};return a&&(h.traceId=a),o&&(h.spanId=o),_(h)}}async getResponseBody(e){let t;try{const n=e.clone().body;if(n){let e,i=n.getReader(),s=new TextDecoder,r="";for(;!(e=await i.read()).done;){let t=e.value;r+=s.decode(t)}t=r}else t=""}catch(e){t=`Unable to clone response: ${e}`}return t}initialize(){this.logger.log("[network-events-listener.initialize] called"),this.patchXHR(),this.patchFetch()}destroy(){globalThis.XMLHttpRequest.prototype.open=globalThis.XMLHttpRequest.prototype.open,globalThis.XMLHttpRequest.prototype.send=globalThis.XMLHttpRequest.prototype.send,globalThis.fetch=globalThis.fetch}shouldIgnoreRequest(e){try{const t=this.config.getConfig()?.getEndpoint();if(t&&e.includes(t))return!0;if([".tsx",".jsx",".css"].some((t=>e.toLowerCase().endsWith(t))))return!0;return(this.config.getConfig()?.options?.excludedUrls||[]).some((t=>{if(t instanceof RegExp)return t.test(e);if("string"==typeof t&&(t.includes("*")||t.includes("?"))){const n=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*").replace(/\?/g,".");return new RegExp(n).test(e)}return e===t}))}catch(e){return d(e),!1}}queueEvent(e){try{this.logger.log("[network-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}patchXHR(){const e=this,t=globalThis.XMLHttpRequest.prototype.open,n=globalThis.XMLHttpRequest.prototype.send,i=globalThis.XMLHttpRequest.prototype.setRequestHeader;let s;globalThis.XMLHttpRequest.prototype.open=function(n,i,r=!0,a,o){if(s=Date.now(),e.shouldIgnoreRequest(i.toString()))return t.apply(this,[n,i,r,a,o]);const l=new URL(i.toString(),globalThis.location.href).href;return this._requestMethod=n,this._requestUrl=l,this._requestHeaders={},t.apply(this,[n,i,r,a,o])},globalThis.XMLHttpRequest.prototype.setRequestHeader=function(e,t){return this._requestHeaders&&(this._requestHeaders[e]=t),i.apply(this,[e,t])},globalThis.XMLHttpRequest.prototype.send=function(...t){const i=this._requestUrl?.toString();if(!i||e.shouldIgnoreRequest(i))return n.apply(this,t);if(e.shouldAddTraceHeader(i)){const t=e.config.getConfig(),n=e.getTraceIds();t?.options?.traceOrigin?.name&&e.addHeaderToXhrRequest({request:this,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),n&&(t?.options?.tracePropagationTraceIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:n.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:n.decimalSpanId}),Boolean(t?.options?.tracePropagationHeaders)&&t?.options?.tracePropagationHeaders?.forEach((t=>{e.addHeaderToXhrRequest({request:this,headerName:t,headerValue:n.traceparent})})),t?.options?.tracePropagationHeaders?.length||e.addHeaderToXhrRequest({request:this,headerName:"traceparent",headerValue:n.traceparent}))}return this._requestBody=t[0]||"",this.addEventListener("load",(()=>{const t=Date.now(),n={};this.getAllResponseHeaders().split(/\r?\n/).forEach((e=>{const[t,i]=e.split(": ");t&&i&&(n[t.trim()]=i.trim())}));const i=K(this._requestBody),r=K(""===this.responseType||"text"===this.responseType?this.responseText:this.response),a={timestamp:s,end_time:t,method:this._requestMethod,url:this._requestUrl,body:i,status:this.status,request:{headers:this._requestHeaders||{},body:i},response:{headers:n,body:r}};e.handleEvent(a)})),n.apply(this,t)}}normalizeHeaders(e,t){try{const n=e?new Headers(e):new Headers;return t?(t instanceof Headers?t.forEach(((e,t)=>n.set(t,e))):Array.isArray(t)?new Headers(t).forEach(((e,t)=>n.set(t,e))):"object"==typeof t&&Object.entries(t).forEach((([e,t])=>n.set(e,t))),n):n}catch{return new Headers}}addTraceHeaders(e){const t=this.config.getConfig(),n=this.getTraceIds();if(t?.options?.traceOrigin?.name&&this.addHeaderToFetchRequest({init:e,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),!n)return;t?.options?.tracePropagationTraceIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:n.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:n.decimalSpanId});const i=t?.options?.tracePropagationHeaders;(i?.length?i:["traceparent"]).forEach((t=>{this.addHeaderToFetchRequest({init:e,headerName:t,headerValue:n.traceparent})}))}extractHeadersRecord(e){const t={};return e instanceof Headers&&e.forEach(((e,n)=>{t[n]=e})),t}patchFetch(){const e=globalThis.fetch;globalThis.fetch=(...t)=>{const n=Date.now();let[i,s]=t;s={...s||{}},t[1]=s;const r=i instanceof Request,a=s?.method||"GET",o=s?.body||"",l=i instanceof Request?i.url:i.toString();if(this.shouldIgnoreRequest(l))return e.apply(globalThis,t);const c=this.shouldAddTraceHeader(l);s||(t[1]={},s=t[1]),s.headers=this.normalizeHeaders(r?i.headers:void 0,s.headers),c&&this.addTraceHeaders(s);const d=this.extractHeadersRecord(s.headers),h=K(o);return e.apply(globalThis,t).then((e=>{const t=Date.now(),i=e.clone(),s={};i.headers.forEach(((e,t)=>{s[t]=e}));const r={method:a,url:l,timestamp:n,body:h,status:i.status,end_time:t,request:{headers:d,body:h},response:{headers:s,body:""}};try{s["content-type"]?.includes("text/event-stream")?(r.response.body="[unreadable response body]",this.handleEvent(r)):this.getResponseBody(i).then((e=>{r.response.body=e,this.handleEvent(r)})).catch((e=>{r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}))}catch(e){r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}return e})).catch((e=>{const t=Date.now();let n="NetworkError";const i=e.message||"Unknown network error";e instanceof TypeError&&i.includes("Failed to fetch")?n="ERR_NETWORK_FAILURE":i.includes("Name not resolved")?n="ERR_NAME_NOT_RESOLVED":i.includes("Connection refused")&&(n="ERR_CONNECTION_REFUSED");const s={method:a,url:l,body:h,status:0,end_time:t,request:{headers:d,body:h},response:{headers:{},body:""},error:{type:n}};throw this.handleEvent(s),e}))}}formatHeaders(e){const t=["authorization","cookie","set-cookie"];return Object.entries(e)?.reduce(((e,[n,i])=>{const s=n.toLowerCase();return t.includes(s)||/(token|key|secret|password)/i.test(s)?e[n]="[REDACTED]":e[n]=x({text:i}),e}),{})}extractTraceIds(e){const t=this.config.getConfig();let n="",i="";if(t?.options?.tracePropagationTraceIdHeaderName){const i=e[t.options.tracePropagationTraceIdHeaderName];i&&"string"==typeof i&&(n=i)}if(t?.options?.tracePropagationSpanIdHeaderName){const n=e[t.options.tracePropagationSpanIdHeaderName];n&&"string"==typeof n&&(i=n)}if(!n||!i){const t=this.extractTraceparentHeader(e);if(t){const e=t.split("-");4===e.length&&"00"===e[0]&&(!n&&e[1]&&32===e[1].length&&(n=e[1]),!i&&e[2]&&16===e[2].length&&(i=e[2]))}}return{traceId:n,spanId:i}}extractTraceparentHeader(e){const t=this.config.getConfig();if(t?.options?.tracePropagationHeaders?.length)for(const n of t.options.tracePropagationHeaders){const t=e[n];if(t&&"string"==typeof t)return t}const n=e.traceparent;return n&&"string"==typeof n?n:""}getTraceIds(){const e=this.idGenerator.generateSpanId(),t=this.idGenerator.generateTraceId();let n,i;try{n=BigInt("0x"+t).toString(10),i=BigInt("0x"+e).toString(10)}catch(s){this.logger.log("[network-events-listener.getTraceIds] error",{error:s}),n=t,i=e}return{traceparent:`00-${t}-${e}-01`,traceId:t,spanId:e,decimalTraceId:n,decimalSpanId:i}}addHeaderToXhrRequest({request:e,headerName:t,headerValue:n}){if(this.logger.log("[network-events-listener.addHeaderToXhrRequest] called",{request:e,headerName:t,headerValue:n}),this.isValidHeaderName(t))try{e.setRequestHeader(t,n),e._requestHeaders[t]=n}catch(e){this.logger.log("[network-events-listener.addHeaderToXhrRequest] Error setting header",{error:e,headerName:t,headerValue:n})}else this.logger.log("[network-events-listener.addHeaderToXhrRequest] Invalid header name",{headerName:t})}addHeaderToFetchRequest({init:e,headerName:t,headerValue:n}){if(this.logger.log("[network-events-listener.addHeaderToFetchRequest] called",{init:e,headerName:t,headerValue:n}),this.isValidHeaderName(t))try{e.headers instanceof Headers?e.headers.set(t,n):e.headers={...e.headers,[t]:n}}catch(e){this.logger.log("[network-events-listener.addHeaderToFetchRequest] Error setting header",{error:e,headerName:t,headerValue:n})}else this.logger.log("[network-events-listener.addHeaderToFetchRequest] Invalid header name",{headerName:t})}isValidHeaderName(e){if(!e||"string"!=typeof e)return!1;return/^[a-zA-Z0-9!#$&'*+\-.^_`|~]+$/.test(e)}shouldAddTraceHeader(e){const t=this.config.getConfig();if(this.logger.log("[network-events-listener.shouldAddTraceHeader] called",{url:e,config:t}),!t?.options?.tracePropagationUrls?.length)return!1;const n=t.options.tracePropagationUrls?.some((t=>{const n=t.replace(/\*/g,".*"),i=new RegExp(`^${n}$`),s=e.startsWith("/")?new URL(e,globalThis.location.href).pathname:e;return i.test(s)}));return this.logger.log("[network-events-listener.shouldAddTraceHeader] result",{url:e,result:n}),n}},G=class extends A{constructor(){super(...arguments),this.handleEvent=(e,t)=>{this.logger.log("[errors-events-listener.handleEvent] called");try{let n;if(e instanceof Error)n=e,this.enhanceError(n);else if(e instanceof ErrorEvent){if(n=e.error||new Error(e.message||"Unknown error"),/Script error\.?/.test(n.message))return;this.enhanceError(n,e)}else{if(!(e instanceof PromiseRejectionEvent))return;n=this.createUnhandledRejectionError(e)}const i=this.buildEvent(n,t);this.queueEvent(i)}catch(e){d(e)}}}initialize(){h?.addEventListener("error",this.handleEvent),h?.addEventListener("unhandledrejection",this.handleEvent)}destroy(){h?.removeEventListener("error",this.handleEvent),h?.removeEventListener("unhandledrejection",this.handleEvent)}buildEvent(e,t){return _({type:"exception",attributes:{error_type:e.name||"Error",error_message:e.message||"Unknown error",error_stacktrace:this.buildStackTrace(e),error_fingerprint:`${e.name}:${x({text:e.message,maxLength:400})}`,error_handled:t?.handled||!1,error_metadata:t?.metadata}})}queueEvent(e){try{this.logger.log("[errors-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}enhanceError(e,t){const{filename:n,lineno:i,colno:s}=t||{};n&&!e.fileName&&Object.defineProperty(e,"fileName",{value:n}),i&&!e.lineNumber&&Object.defineProperty(e,"lineNumber",{value:i}),s&&!e.columnNumber&&Object.defineProperty(e,"columnNumber",{value:s})}createUnhandledRejectionError(e){let t;if(e.reason instanceof Error)t=e.reason;else{const n="object"==typeof e.reason?JSON.stringify(e.reason,null,2):String(e.reason);t=new Error(n),t.name="UnhandledRejection",Object.defineProperty(t,"originalReason",{value:e.reason,enumerable:!1})}return t}buildStackTrace(e){if(!e)return[];try{return a.default.parse(e).map((e=>({filename:e.fileName||"unknown",function:e.functionName||"anonymous",lineno:e.lineNumber||0,colno:e.columnNumber||0})))}catch(e){return[]}}},W=class extends A{constructor({isEnabled:e}){if(super(),this.currentUrl="",this.originalPushState=null,this.originalReplaceState=null,this.isInitialized=!1,this.isAutomaticNavigationEnabled=!1,this.activeNavigation=null,this.popStateHandler=()=>{this.handleLocationChange()},this.hashChangeHandler=()=>{this.handleLocationChange()},this.isInBrowserEnvironment()&&e)try{this.isAutomaticNavigationEnabled=e,this.currentUrl=h.location.href,this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState}catch(e){d(e)}}isInBrowserEnvironment(){try{return void 0!==h&&void 0!==h.location&&void 0!==h.history&&"function"==typeof h.addEventListener}catch(e){return d(e),!1}}initialize(){try{if(this.logger.log("[navigation-listener.initialize] called"),!this.isInBrowserEnvironment()||!this.originalPushState||!this.originalReplaceState)return void this.logger.log("[navigation-listener.initialize] Browser environment not available, skipping initialization");if(!this.isAutomaticNavigationEnabled)return void this.logger.log("[navigation-listener.initialize] Automatic navigation is disabled, skipping initialization");const e=this.originalPushState,t=this.originalReplaceState;history.pushState=(...t)=>{const n=e.apply(history,t);return this.handleLocationChange(),n},history.replaceState=(...e)=>{const n=t.apply(history,e);return this.handleLocationChange(),n},h&&"function"==typeof h.addEventListener&&(h.addEventListener("popstate",this.popStateHandler),h.addEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!0}catch(e){d(e)}}destroy(){try{this.logger.log("[navigation-listener.destroy] called"),this.isInitialized&&this.originalPushState&&this.originalReplaceState&&(history.pushState=this.originalPushState,history.replaceState=this.originalReplaceState),h&&"function"==typeof h.removeEventListener&&(h.removeEventListener("popstate",this.popStateHandler),h.removeEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!1}catch(e){d(e)}}handleEvent(){try{this.logger.log("[navigation-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){d(e)}}buildEvent(){this.logger.log("[navigation-listener.buildEvent] called",h.location.href);return _({type:"navigation",attributes:{page_url:h.location.href,metadata:this.activeNavigation?.metadata,duration_ms:this.activeNavigation?.duration_ms||0,start_timestamp:this.activeNavigation?.startTime||0,end_timestamp:this.activeNavigation?.endTime||0}})}queueEvent(e){try{this.logger.log("[navigation-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}startNavigation(e){this.logger.log("[navigation-listener.startNavigation] called",e),this.activeNavigation={page_url:h.location.href,metadata:e,startTime:Date.now(),endTime:0,duration_ms:0}}endNavigation(e){this.logger.log("[navigation-listener.endNavigation] called",e),this.activeNavigation?(this.logger.log("[navigation-listener.endNavigation] active navigation",this.activeNavigation),this.activeNavigation.endTime=Date.now(),this.activeNavigation.duration_ms=this.activeNavigation.endTime-this.activeNavigation.startTime,this.handleEvent()):this.logger.log("[navigation-listener.endNavigation] no active navigation found")}handleLocationChange(){try{if(!this.isInitialized)return;if(this.logger.log("[navigation-listener.handleLocationChange] called"),this.currentUrl===h.location.href)return;let e,t;try{e=new URL(h.location.href),t=new URL(this.currentUrl)}catch(e){return void d(e)}const n=e=>e.startsWith("#/"),i=n(e.hash)||n(t.hash);(e.pathname!==t.pathname||i&&e.hash!==t.hash)&&(this.currentUrl=h.location.href,this.handleEvent())}catch(e){d(e)}}},J=class extends A{constructor(){super(),this.loadEventHandler=()=>{this.logger.log("load event"),this.handleEvent()}}initialize(){try{this.logger.log("[page-load-listener.initialize] called"),this.logger.log("globalThis",h),"complete"===h?.document?.readyState?(this.logger.log("Page already loaded, triggering event immediately"),this.handleEvent()):h?.addEventListener("load",this.loadEventHandler)}catch(e){this.logger.log("[page-load-listener.initialize] error",e),d(e)}}destroy(){h?.removeEventListener("load",this.loadEventHandler)}handleEvent(){try{this.logger.log("[page-load-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){d(e)}}buildEvent(){const e=performance.getEntriesByType("navigation")[0],t=e?e.loadEventEnd-e.startTime:0,n=performance.getEntriesByType("resource"),i={count:n.length,totalSize:0,totalDuration:0,byType:{}};n.forEach((e=>{const t=e,n=t.transferSize||0,s=t.duration,r=t.initiatorType;i.totalSize+=n,i.totalDuration+=s,i.byType[r]||(i.byType[r]={count:0,size:0,duration:0}),i.byType[r].count++,i.byType[r].size+=n,i.byType[r].duration+=s}));return _({type:"pageload",attributes:{page_url:h?.location?.href||"",page_load_time:t,page_referrer:h?.document?.referrer||"",page_resources:i}})}queueEvent(e){try{this.logger.log("[page-load-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}},Y=class extends A{constructor(){super(...arguments),this.handleEvent=e=>{try{if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}}}initialize(){n.onCLS(this.handleEvent),n.onLCP(this.handleEvent),n.onFCP(this.handleEvent),n.onTTFB(this.handleEvent),n.onINP(this.handleEvent)}destroy(){}buildEvent(e){return _({type:"performance",attributes:{performance_metric_name:e.name,performance_metric_value:e.value,performance_metric_id:e.id,performance_metric_navigation_type:e.navigationType||""}})}queueEvent(e){try{this.logger.log("[performance-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}},Z=class{constructor(e){this.logger=l.getInstance(),this.eventsPool=P.getInstance(),this.configManager=N.getInstance(),this.stopFn=null,this.isRecording=!1,this.events=[],this.startTime=0,this.batchSize=3,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.inactivityTimer=null,this.sendTimerId=null,this.onInactivityStop=e??null}initialize(){this.logger.log("[session-replay-listener] initialize called"),this.isRecording?this.logger.log("[session-replay-listener] already recording"):this.startRecording()}startRecording(){try{this.startTime=Date.now(),this.events=[],this.isRecording=!0,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0;const e=this.configManager.getConfig()?.options?.sessionReplay?.blockedSelectors?.filter(Boolean),t=e?.length?e.join(", "):void 0;this.stopFn=i.record({...t?{blockSelector:t}:{},slimDOMOptions:{script:!1,comment:!1,headWhitespace:!1},sampling:{mousemove:100,input:"last",scroll:150},checkoutEveryNms:3e4,emit:(e,t)=>{if(this.isRecording)try{t||this.resetInactivityTimer(),this.events.push(e),t&&(this.batchContainsFullSnapshot=!0,this.fullSnapshotTimestamp=Date.now(),this.logger.log("[session-replay-listener] full snapshot captured at",this.fullSnapshotTimestamp)),this.events.length>=this.batchSize&&this.scheduleSendBatch()}catch(e){d(e),this.logger.log("[session-replay-listener] failed to process event in emit callback:",e)}}})||null,this.resetInactivityTimer(),this.logger.log("[session-replay-listener] started recording with full snapshot interval:",3e4,"ms")}catch(e){d(e),this.logger.log("[session-replay-listener] failed to start recording:",e),this.isRecording=!1}}scheduleSendBatch(){null===this.sendTimerId&&(this.sendTimerId=h.setTimeout((()=>{if(this.sendTimerId=null,this.isRecording)try{this.sendBatch()}catch(e){d(e),this.logger.log("[session-replay-listener] failed to send deferred batch:",e)}}),0))}clearScheduledSend(){null!==this.sendTimerId&&(h.clearTimeout(this.sendTimerId),this.sendTimerId=null)}resetInactivityTimer(){this.inactivityTimer&&clearTimeout(this.inactivityTimer),this.inactivityTimer=setTimeout((()=>{this.logger.log("[session-replay-listener] stopping recording due to inactivity");try{this.onInactivityStop?.()}catch(e){d(e),this.logger.log("[session-replay-listener] onInactivityStop error:",e)}this.stopRecording()}),18e5)}clearInactivityTimer(){this.inactivityTimer&&(clearTimeout(this.inactivityTimer),this.inactivityTimer=null)}sendBatch(){if(0!==this.events.length)try{const e=this.events.map((e=>s.pack(e))),t=this.batchContainsFullSnapshot,n=this.fullSnapshotTimestamp,i={_gc_replay_data:{events:e}};t&&(i.replay_is_full_snapshot=!0,i.replay_full_snapshot_timestamp=n);const r=_({type:"replay",attributes:i});this.eventsPool.addEvent(r),this.events=[],this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.logger.log("[session-replay-listener] sent batch with",e.length,"events",t?"(includes full snapshot)":"")}catch(e){d(e),this.logger.log("[session-replay-listener] failed to send batch:",e)}}stopRecording(){if(this.isRecording)try{this.clearInactivityTimer(),this.clearScheduledSend(),this.stopFn?.(),this.isRecording=!1,this.events.length>0&&this.sendBatch(),this.stopFn=null,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0}catch(e){d(e),this.logger.log("[session-replay-listener] failed to stop recording:",e)}}destroy(){this.stopRecording()}},Q=class{constructor(){this.logger=l.getInstance(),this.eventsPool=P.getInstance(),this.configManager=N.getInstance(),this.idGenerator=new m,this.logEventsListener=null,this.domEventsListener=null,this.errorsEventsListener=null,this.networkEventsListener=null,this.navigationListener=null,this.performanceListener=null,this.pageLoadListener=null,this.sessionReplayListener=null,this.isRotating=!1}instrument(){this.logger.log("[instrumentation-manager.instrument] called"),this.eventsPool.setBeforeEnqueueHook((e=>this.maybeRotateOnEnqueue(e)));const e=this.configManager.getConfig(),t=e?.options?.enabledEvents,n=!t||0===t.length,i=[];(n||t?.includes("pageload"))&&(this.pageLoadListener=new J,this.pageLoadListener.initialize(),i.push("pageload")),(n||t?.includes("dom"))&&(this.domEventsListener=new U,this.domEventsListener.initialize(),i.push("dom")),(n||t?.includes("logs"))&&(this.logEventsListener=new $,this.logEventsListener.initialize(),i.push("logs")),(n||t?.includes("exceptions"))&&(this.errorsEventsListener=new G,this.errorsEventsListener.initialize(),i.push("exceptions")),(n||t?.includes("network"))&&(this.networkEventsListener=new V,this.networkEventsListener.initialize(),i.push("network")),(n||t?.includes("performance"))&&(this.performanceListener=new Y,this.performanceListener.initialize(),i.push("performance")),this.navigationListener=new W({isEnabled:n||t?.includes("navigation")}),this.navigationListener.initialize(),i.push("navigation"),this.logger.log("[instrumentation-manager.instrument] initialized listeners based on config:",i)}sendCustomEvent(e){this.logger.log("[instrumentation-manager.sendCustomEvent] called",e);try{const t=_({type:"custom",attributes:{custom_event_name:e?.event,custom_event_attributes:e?.attributes}});this.eventsPool.addEvent(t)}catch(e){d(e)}}emitLog(e,t,n){if(this.logEventsListener){this.logger.log("[instrumentation-manager.emitLog] called",{message:e,level:t,attributes:n});try{const i={...n||{}};delete i.message,delete i.level,delete i.location;const s=_({type:"log",attributes:{...i,message:x({text:e}),level:t}});this.eventsPool.addEvent(s)}catch(e){d(e)}}}captureException(e,t){try{this.logger.log("[instrumentation-manager.captureException] called",e),this.errorsEventsListener?.handleEvent(e,{handled:!0,metadata:t})}catch(e){d(e)}}isNavigationTrackingEnabled(){const e=this.configManager.getConfig();return e?.options?.enabledEvents?.includes("navigation")||0===e?.options?.enabledEvents?.length}startNavigation(e){this.logger.log("[instrumentation-manager.startNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.startNavigation] startNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.startNavigation(e)}endNavigation(e){this.logger.log("[instrumentation-manager.endNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.endNavigation] endNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.endNavigation(e)}isReplayRecording(){return null!==this.sessionReplayListener}startReplayRecording(){if(this.sessionReplayListener)this.logger.log("[instrumentation-manager] replay recording already started");else try{const e=new Z((()=>{this.rotateSession({restartReplay:!1,reason:"replay inactivity"})}));e.initialize(),this.sessionReplayListener=e,this.logger.log("[instrumentation-manager] started replay recording")}catch(e){d(e),this.logger.log("[instrumentation-manager] failed to start replay recording:",e)}}maybeRotateOnEnqueue(e){if(this.isRotating)return;if("replay"===e.type)return void this.logger.log("[instrumentation-manager] enqueue rotation check: ignoring replay event");const t=Date.now(),n=this.configManager.getSessionElapsedMs(t),i=this.configManager.getSessionMaxDuration(),s=n>=i;this.logger.log("[instrumentation-manager] enqueue rotation check",{event:e.type,elapsedMs:n,maxDurationMs:i,expired:s}),s&&this.rotateSession({restartReplay:!0,reason:"max session duration"})}rotateSession(e){if(this.isRotating)return;this.isRotating=!0;const t=this.configManager.getSessionId(),n=this.isReplayRecording();this.logger.log("[instrumentation-manager] session rotation start",{reason:e.reason,previousId:t,wasReplayActive:n,restartReplay:e.restartReplay});const i=(e,t)=>{try{this.logger.log(`[instrumentation-manager] session rotation: ${e}`),t()}catch(t){d(t),this.logger.log(`[instrumentation-manager] failed to ${e} during session rotation:`,t)}};try{i("stop replay",(()=>this.stopReplayRecording())),i("flush events",(()=>this.eventsPool.flush())),i("rotate session ids",(()=>{const e=this.idGenerator.generateId();this.configManager.setSessionId(e),this.configManager.setSessionStartTime(u(Date.now())),this.logger.log("[instrumentation-manager] session id rotated",{from:t,to:e})})),e.restartReplay&&n&&i("restart replay",(()=>this.startReplayRecording()))}finally{this.isRotating=!1,this.logger.log("[instrumentation-manager] session rotation complete",{reason:e.reason})}}stopReplayRecording(){this.sessionReplayListener?(this.sessionReplayListener.stopRecording(),this.sessionReplayListener=null):this.logger.log("[instrumentation-manager] cannot stop replay recording: replay listener not initialized")}uninstrument(){this.eventsPool.setBeforeEnqueueHook(null),this.domEventsListener?.destroy(),this.logEventsListener?.destroy(),this.errorsEventsListener?.destroy(),this.networkEventsListener?.destroy(),this.navigationListener?.destroy(),this.performanceListener?.destroy(),this.pageLoadListener?.destroy(),this.sessionReplayListener?.destroy()}},ee="gcSample",te=class{constructor(e){this.initialized=!1,this.logger=l.initialize({debug:e.options?.debug||!1,prefix:"[groundcover]"}),this.logger.log("[session-manager] initialize called"),this.instrumentationManager=new Q,this.idGenerator=new m;if(!this.getSamplingDecision(e.options?.sessionSampleRate))return void this.logger.log("[session-manager] session is not sampled");if(this.initialized)return void this.logger.log("[session-manager] SDK already initialized");const t=N.getInstance();t.initialize(e);P.getInstance().initialize();const n=e.sessionId,i=t.getSessionId();n&&n!==i?(t.setSessionId(n),t.setSessionStartTime(u(Date.now())),this.logger.log("[session-manager] using provided sessionId:",n)):n?(t.setSessionId(n),this.logger.log("[session-manager] continuing provided sessionId:",n)):i||t.setSessionId(this.idGenerator.generateId());const s=Date.now(),r=p(t.getSessionStartTime());if(r>0&&s-r>=-6e4){if(t.isSessionExpired(s)){const e=t.getSessionId(),n=this.idGenerator.generateId();t.setSessionId(n),t.setSessionStartTime(u(s)),this.logger.log("[session-manager] init-time rotation: persisted session past cap",{from:e,to:n})}}else t.setSessionStartTime(u(s));this.instrumentationManager.instrument(),this.initialized=!0}getSamplingDecision(e){try{if(!e||1===e)return!0;const t=T(ee);if(t){const e="1"===t;return this.logger.log("[session-manager] using stored sampling decision:",e),e}const n=!e||Math.random()<e;return w(ee,n?"1":"0"),this.logger.log("[session-manager] stored new sampling decision:",n),n}catch(t){this.logger.log("[session-manager] session storage access failed:",t);const n=!e||Math.random()<e;return this.logger.log("[session-manager] using fallback sampling decision:",n),n}}identifyUser(e){if(this.logger.log("[session-manager] identifyUser called"),!this.initialized)return void this.logger.log("[session-manager] cannot identify user: SDK not initialized");N.getInstance().updateConfig({userIdentifier:e})}updateConfig(e){if(!this.initialized)return void this.logger.log("[session-manager] cannot update config: SDK not initialized");N.getInstance().updateConfig(e)}sendCustomEvent(e){this.initialized?this.instrumentationManager.sendCustomEvent(e):this.logger.log("[session-manager] cannot send custom event: SDK not initialized")}startNavigation(e){this.initialized?this.instrumentationManager.startNavigation(e):this.logger.log("[session-manager] cannot start navigation: SDK not initialized")}endNavigation(e){this.initialized?this.instrumentationManager.endNavigation(e):this.logger.log("[session-manager] cannot end navigation: SDK not initialized")}captureException(e,t){this.initialized?this.instrumentationManager.captureException(e,t):this.logger.log("[session-manager] Cannot capture exception: SDK not initialized")}emitLog(e,t,n){this.initialized?this.instrumentationManager.emitLog(e,t,n):this.logger.log("[session-manager] Cannot emit log: SDK not initialized")}getSessionId(){return this.initialized?N.getInstance().getSessionId():(this.logger.log("[session-manager] cannot get session ID: SDK not initialized"),"")}async setSessionId(e){if(this.logger.log("[session-manager] setSessionId called"),this.initialized)try{const t=P.getInstance();await t.flushSync();const n=e||this.idGenerator.generateId(),i=N.getInstance();i.setSessionId(n),i.setSessionStartTime(u(Date.now())),this.logger.log("[session-manager] session ID set successfully:",n)}catch(e){this.logger.log("[session-manager] failed to set session ID:",e)}else this.logger.log("[session-manager] cannot set session ID: SDK not initialized")}startReplayRecording(){this.initialized?this.instrumentationManager.startReplayRecording():this.logger.log("[session-manager] cannot start replay recording: SDK not initialized")}stopReplayRecording(){this.initialized?this.instrumentationManager.stopReplayRecording():this.logger.log("[session-manager] cannot stop replay recording: SDK not initialized")}destroy(){this.initialized&&(this.instrumentationManager.uninstrument(),R("gcId"),R("gcStartTime"),this.initialized=!1)}},ne=new class{constructor(e){this.manager=e}emit(e,t,n){this.manager&&this.manager.emitLog(t,e,n)}log(e,t){this.emit("log",e,t)}info(e,t){this.emit("info",e,t)}warn(e,t){this.emit("warn",e,t)}error(e,t){this.emit("error",e,t)}debug(e,t){this.emit("debug",e,t)}trace(e,t){this.emit("trace",e,t)}};var ie={init:function(e){try{X=new te({cluster:e?.cluster||"",environment:e?.environment||"",namespace:e?.namespace,releaseId:e?.releaseId,dsn:e?.dsn||"",appId:e?.appId||"",userIdentifier:e?.userIdentifier,apiKey:e?.apiKey||"",options:e?.options,sessionId:e?.sessionId}),ne.manager=X}catch(e){d(e)}},identifyUser:function(e){X?X.identifyUser(e):console.warn("[groundcover] identifyUser: groundcover is not initialized. please call init() first")},sendCustomEvent:function(e){X&&X.sendCustomEvent(e)},captureException:function(e,t){X&&X.captureException(e,t)},logger:ne,updateConfig:function(e){X&&X.updateConfig(e)},startNavigation:function(e){X?X.startNavigation(e):console.warn("[groundcover] startNavigation: groundcover is not initialized. please call init() first")},endNavigation:function(e){X?X.endNavigation(e):console.warn("[groundcover] endNavigation: groundcover is not initialized. please call init() first")},getSessionId:function(){return X?X.getSessionId():(console.warn("[groundcover] getSessionId: groundcover is not initialized. please call init() first"),"")},setSessionId:async function(e){if(X)try{await X.setSessionId(e)}catch(e){console.error("[groundcover] setSessionId: failed to set session ID:",e)}else console.warn("[groundcover] setSessionId: groundcover is not initialized. please call init() first")},startReplayRecording:function(){X?X.startReplayRecording():console.warn("[groundcover] startReplayRecording: groundcover is not initialized. please call init() first")},stopReplayRecording:function(){X?X.stopReplayRecording():console.warn("[groundcover] stopReplayRecording: groundcover is not initialized. please call init() first")}};if("undefined"!=typeof window&&!window.groundcover)try{window.groundcover=ie}catch(e){console.warn("[groundcover] Failed to expose groundcover on window:",e)}var se=ie;module.exports=se;
1
+ "use strict";var e=require("fflate"),t=require("error-stack-parser"),i=require("web-vitals"),n=require("rrweb"),s=require("@rrweb/packer");function r(e){return e&&e.__esModule?e:{default:e}}var a=r(t),o=console.log.bind(console),l=class e{constructor(){this.isDebugEnabled=!1,this.prefix=""}static initialize(t){return e.instance||(e.instance=new e),t&&(e.instance.isDebugEnabled=t.debug??!1,e.instance.prefix=t.prefix??""),e.instance}static getInstance(t){return e?.instance||e.initialize(t)}formatMessage(e){return`[${(new Date).toISOString()}] ${this.prefix} ${e}`}log(e,...t){this.isDebugEnabled&&o(this.formatMessage(e),...t)}updateConfig(e){this.isDebugEnabled=e.debug??this.isDebugEnabled,this.prefix=e.prefix??this.prefix}},c=l.getInstance();function d(e){c.log("[error-handler.handleError] called",e,{groundcoverIgnore:!0})}var h="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:{},g=1e6;function u(e){return e*g}function p(e){return e/g}var m=class{constructor(){this.generateTraceId=function(){for(let e=0;e<16;e++)f[e]=Math.floor(16*Math.random())+48,f[e]>=58&&(f[e]+=39);return y+String.fromCharCode.apply(null,f.slice(0,16))},this.generateSpanId=v(8),this.generateId=v(16)}},f=Array(32);function v(e){return function(){for(let t=0;t<2*e;t++)f[t]=Math.floor(16*Math.random())+48,f[t]>=58&&(f[t]+=39);return String.fromCharCode.apply(null,f.slice(0,2*e))}}var y="0000000000000000";var b=144e5,E=288e5,S=18e5,I={batchSize:10,batchTimeout:1e4,eventSampleRate:1,sessionSampleRate:1,environment:"development",debug:!1,enableCompression:!0,maskFields:[],enableMasking:!1,enabledEvents:[],tracePropagationUrls:[],tracePropagationHeaders:[],tracePropagationTraceIdHeaderName:"",tracePropagationSpanIdHeaderName:"",traceOrigin:{name:"",value:""},sessionMaxDuration:b},T=class e{constructor(t){this.dsn=t?.dsn||"",this.appId=t?.appId||"",this.cluster=t?.cluster||"",this.apiKey=t?.apiKey||"",this.environment=t?.environment||"",this.namespace=t?.namespace||"",this.releaseId=t?.releaseId,this.userIdentifier=t?.userIdentifier||null,this.options={...I,...t?.options||{}},this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t?.options?.sessionMaxDuration)}static normalizeSessionMaxDuration(e){return void 0===e?b:"number"!=typeof e||!Number.isFinite(e)||e<6e4||e>E?(console.warn("[groundcover] sessionMaxDuration must be a finite number between 60000 ms (1 minute) and 28800000 ms (8 hours); falling back to the 4-hour default"),b):e}getEndpoint(){return this.dsn?.startsWith("http")?this.dsn:`https://${this.dsn}`}updateConfig(t){t.options&&(this.options={...this.options,...t.options},void 0!==t.options.sessionMaxDuration&&(this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t.options.sessionMaxDuration))),t.userIdentifier&&(this.userIdentifier={...this.userIdentifier,...t.userIdentifier}),t.appId&&(this.appId=t.appId),t.dsn&&(this.dsn=t.dsn),t.apiKey&&(this.apiKey=t.apiKey),t.cluster&&(this.cluster=t.cluster),t.environment&&(this.environment=t.environment),t.namespace&&(this.namespace=t.namespace),t.releaseId&&(this.releaseId=t.releaseId)}};function w(e){try{return globalThis?.sessionStorage?.getItem(e)??null}catch(e){return d(e),null}}function R(e,t){try{globalThis?.sessionStorage?.setItem(e,t)}catch(e){d(e)}}function L(e){try{globalThis?.sessionStorage?.removeItem(e)}catch(e){d(e)}}function N(e,t=0){const i=w(e);if(null===i)return t;const n=Number(i);return Number.isFinite(n)?n:t}var H=class e{constructor(){this.config=null,this.logger=l.getInstance(),this.sessionId=null,this.sessionStartTime=null,this.lastActivityMs=null,this.lastPersistedActivityMs=0,this.flushActivityOnHide=()=>{try{if("undefined"!=typeof document&&"hidden"!==document.visibilityState)return;this.persistLastActivity()}catch(e){d(e)}},this.activityPersistHooked=!1}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(e){if(!this.activityPersistHooked&&"undefined"!=typeof document){this.activityPersistHooked=!0;try{document.addEventListener("visibilitychange",this.flushActivityOnHide)}catch(e){d(e)}}this.config||(this.config=new T(e))}getConfig(){return this.config?this.config:(this.logger.log("[config-manager] configuration not initialized"),null)}getSessionId(){if(this.sessionId)return this.sessionId;return w("gcId")||""}setSessionId(e){this.sessionId=e,R("gcId",e)}getSessionStartTime(){return this.sessionStartTime?this.sessionStartTime:N("gcStartTime",0)}setSessionStartTime(e){this.sessionStartTime=e,R("gcStartTime",String(e)),this.setLastActivityMs(Date.now())}getLastActivityMs(){return null!==this.lastActivityMs?this.lastActivityMs:N("gcLastActivity",0)}setLastActivityMs(e){this.lastActivityMs=e,e-this.lastPersistedActivityMs>=6e4&&this.persistLastActivity()}persistLastActivity(){null!==this.lastActivityMs&&(this.lastPersistedActivityMs=this.lastActivityMs,R("gcLastActivity",String(this.lastActivityMs)))}isSessionInactive(e=Date.now()){const t=this.getLastActivityMs();return t>0&&e-t>=S}clearSessionState(){if(this.sessionId=null,this.sessionStartTime=null,this.lastActivityMs=null,this.lastPersistedActivityMs=0,this.activityPersistHooked&&"undefined"!=typeof document){this.activityPersistHooked=!1;try{document.removeEventListener("visibilitychange",this.flushActivityOnHide)}catch(e){d(e)}}L("gcId"),L("gcStartTime"),L("gcLastActivity")}getSessionMaxDuration(){return this.config?.options?.sessionMaxDuration??b}getSessionElapsedMs(e=Date.now()){const t=this.getSessionStartTime();return t?e-p(t):0}isSessionExpired(e=Date.now()){return this.getSessionElapsedMs(e)>=this.getSessionMaxDuration()}updateConfig(e){this.logger.log("[config-manager] updateConfig called"),this.config?(e&&(this.logger.log("[config-manager] updating options"),this.config.updateConfig(e)),e?.userIdentifier&&(this.logger.log("[config-manager] updating user identifier"),this.config.userIdentifier={...this.config.userIdentifier,...e.userIdentifier})):this.logger.log("[config-manager] configuration not initialized")}},z=H.getInstance(),C=new m,M=l.getInstance();function q(){const e=z?.getConfig()?.options?.eventSampleRate;return!(void 0!==e&&!Number.isNaN(e))||Math.random()<=e}function _(e,t="",i=new WeakSet,n=0){try{return n>=10?{[t||"depth_limit"]:"[Depth Limit]"}:null!=(s=e)&&"number"==typeof s.nodeType&&"string"==typeof s.nodeName?{[t||"dom_node"]:`[${e?.constructor?.name||"DomNode"}]`}:i.has(e)?{[t||"circular_reference"]:"[Circular]"}:(i.add(e),Object.entries(e).reduce(((e,[s,r])=>{const a=t?`${t}.${s}`:s;return"object"==typeof r&&null!==r?Array.isArray(r)?e[a]=r.map(((e,t)=>"object"==typeof e&&null!==e?_(e,`${a}[${t}]`,i,n+1):e)):Object.assign(e,_(r,a,i,n+1)):e[a]=r,e}),{}))}catch(e){return M.log("[events-processors.formatEventObject] Error formatting event object",e),{[t||"format_error"]:"[Error formatting object]"}}finally{i.delete(e)}var s}function x(e){const t=u(Date.now()),i=C.generateId(),n=e.spanId||C.generateSpanId(),s=e.traceId||C.generateTraceId(),r=e.parentSpanId||"",a=_(e.attributes||{}),o=function(){const e=h?.location;return e?{path:e.hash&&!e.hash.match(/^#[a-z0-9-]+$/i)&&e.hash.startsWith("#/")?e.hash:e.pathname,url:e.href,title:h?.document?.title||""}:{path:"",url:"",title:h?.document?.title||""}}(),l={type:e.type,id:i,spanId:n,parentSpanId:r,traceId:s,attributes:{...a,location:o}};return e.span_name&&(l.span_name=e.span_name),"network"===e.type?(l.start_timestamp=e.timestamp||t,l.end_timestamp=e?.end_timestamp):l.timestamp=t,l}function k({text:e,maxLength:t=1e4}){return e?e.length<=t||"string"!=typeof e?e:e.substring(0,t):""}var D=class{constructor(){this.logger=l.getInstance(),this.config=H.getInstance()}compress(t){const i=this.config.getConfig();if(!i?.options?.enableCompression)return{data:(new TextEncoder).encode(t),isCompressed:!1};try{const i=e.strToU8(t);return{data:e.gzipSync(i),isCompressed:!0}}catch(e){return{data:(new TextEncoder).encode(t),isCompressed:!1}}}async send(e){const t=this.config.getConfig();if(t)try{t?.options?.debug&&this.logger.log("Sending batch:",e,{groundcoverIgnore:!0});const i=t.apiKey,n=this.buildEndpoint();if(!i)return void this.logger.log("No API key found");const s=JSON.stringify(e),{data:r,isCompressed:a}=this.compress(s);fetch(n,{method:"POST",headers:{"Content-Type":"application/json",apikey:i,"Content-Encoding":a?"gzip":""},body:r}).catch((e=>{this.logger.log("Network error while sending batch:",e,{groundcoverIgnore:!0})}))}catch(e){t.options.debug&&this.logger.log("Failed to send batch:",e,{groundcoverIgnore:!0})}}buildEndpoint(){const e=this.config.getConfig();if(!e)return"";const{dsn:t}=e;return`${t}/json/rum`}},P=["navigation","dom.event"],A=class e{constructor(){this.events=[],this.timeoutId=null,this.config=H.getInstance(),this.logger=l.getInstance(),this.initialized=!1,this.noClusterWarned=!1,this.onBeforeEnqueue=null,this.visibilityHandler=()=>{"hidden"===document.visibilityState&&this.flush()},this.transporter=new D}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(){try{if(this.initialized)return;this.scheduleFlush(),"undefined"!=typeof document&&document.addEventListener("visibilitychange",this.visibilityHandler),this.initialized=!0}catch(e){d(e)}}destroy(){try{null!==this.timeoutId&&(h.clearTimeout(this.timeoutId),this.timeoutId=null),"undefined"!=typeof document&&document.removeEventListener("visibilitychange",this.visibilityHandler),this.events=[],this.initialized=!1}catch(e){d(e)}}setBeforeEnqueueHook(e){this.onBeforeEnqueue=e}addEvent(e){if(e&&this.initialized)try{const t=this.config.getConfig()?.options?.beforeSend,i=this.config.getConfig()?.options?.enrichEvent;if(t&&!t(e))return;if(this.onBeforeEnqueue)try{this.onBeforeEnqueue(e)}catch(e){d(e)}i&&(e=i(e)),this.events.push(e);const n=this.config.getConfig()?.options?.batchSize||100;this.events.length>=n&&!P.includes(e.type)&&this.flush()}catch(e){d(e)}}flush(){try{if(!this.initialized)return;if(!this.events?.length)return void this.scheduleFlush();if(!this.config.getConfig()?.cluster)return this.noClusterWarned||(this.noClusterWarned=!0,this.logger.log("[events-pool.flush] skipping flush: no cluster")),void this.scheduleFlush();this.noClusterWarned=!1,this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[],this.scheduleFlush()}catch(e){d(e)}}async flushSync(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");await this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[]}catch(e){d(e)}}scheduleFlush(){try{null!==this.timeoutId&&(h.clearTimeout(this.timeoutId),this.timeoutId=null);const e=this.config.getConfig()?.options?.batchTimeout;if(!e)return;this.timeoutId=h.setTimeout((()=>{this.timeoutId=null,this.flush()}),e)}catch(e){d(e)}}detectBrowser(){const e=navigator.userAgent;let t="unknown",i="unknown";const n=navigator.platform||"unknown",s=/Mobile|Android|iPhone|iPad|iPod/i.test(e);return/Edg/.test(e)?(t="Edge",i=e.match(/Edg\/([\d.]+)/)?.[1]||i):/Chrome/.test(e)&&!/Chromium/.test(e)?(t="Chrome",i=e.match(/Chrome\/([\d.]+)/)?.[1]||i):/Firefox/.test(e)?(t="Firefox",i=e.match(/Firefox\/([\d.]+)/)?.[1]||i):/Safari/.test(e)&&!/Chrome/.test(e)?(t="Safari",i=e.match(/Version\/([\d.]+)/)?.[1]||i):/Trident/.test(e)?(t="Internet Explorer",i=/rv:([^)]+)\)/i.test(e)?e.match(/rv:([^)]+)\)/i)?.[1]||i:e.match(/MSIE ([^;]+)/)?.[1]||i):/OPR/.test(e)&&(t="Opera",i=e.match(/OPR\/([\d.]+)/)?.[1]||i),{name:t,version:i,platform:n,language:navigator.language,mobile:s}}getSessionAttributes(){const e=this.detectBrowser(),t=this.config.getConfig();return{cluster:t?.cluster||"",env:t?.environment||"",namespace:t?.namespace||"",releaseId:t?.releaseId||"",session_id:this.config.getSessionId(),session_start_time:this.config.getSessionStartTime(),user:t?.userIdentifier||{},"service.name":t?.appId,userAgent:navigator.userAgent,browser:e}}};A.instance=null;var O=A,j=class{constructor(){this.logger=l.getInstance(),this.eventsPool=O.getInstance()}},F="undefined"!=typeof Element?Object.getOwnPropertyDescriptor(Element.prototype,"tagName")?.get:void 0;function U(e,t){try{return e.getAttribute(t)||""}catch{return""}}function B(e){try{const t=F?.call(e);return"string"==typeof t?t:""}catch{return""}}var $=class extends j{constructor(){super(...arguments),this.eventHandlers=[],this.capturedEvents=["click","change","keydown","select","submit"],this.config=H.getInstance(),this.getShouldMaskText=e=>{const t=this.config.getConfig(),i=t?.options?.maskFields||[],n=t?.options?.enableMasking||!1;return(["password"===e.target?.type,e.target?.id?.toLowerCase().includes("password"),e.target?.id?.toLowerCase().includes("credit-card"),e.target?.id?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.hasAttribute("data-private")].some((e=>e))||i.some((t=>e.target?.id?.toLowerCase().includes(t))))&&n},this.getKeyCode=e=>{if(e instanceof KeyboardEvent&&e.code&&"Dead"!==e.key){const t=this.getShouldMaskText({target:e.target});return t||t?"*":e.key}return""},this.getText=e=>{const t=e.target,i=this.config.getConfig();return i?.options?.enableMasking||!1?"*":k({text:t.innerText||""})},this.getSelector=e=>e.target instanceof Element&&this.generateSelector(e.target)||"",this.getCoordinates=e=>{if({mouseup:!0,mousedown:!0,mousemove:!0,mouseover:!0}[e.type]){const{clientX:t,clientY:i}=e||{};return{clientX:t,clientY:i}}return null}}initialize(){try{this.logger.log("[dom-events-listener.initialize] called"),this.capturedEvents?.forEach((e=>{const t=e=>{try{this.handleEvent(e)}catch(e){d(e)}};this.eventHandlers.push({type:e,handler:t}),h?.addEventListener(e,t)}))}catch(e){d(e)}}destroy(){this.eventHandlers.forEach((({type:e,handler:t})=>{global?.removeEventListener?.(e,t)})),this.eventHandlers=[]}handleEvent(e){try{if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}}buildEvent(e){const t=this.getSelector(e),i=this.getCoordinates(e),n=e.target,s=this.getKeyCode(e),r=this.getText(e);this.logger.log("[dom-events-listener.buildEvent] called");return x({type:"dom.event",attributes:{dom_event_selector:t,dom_event_key_code:s,dom_event_type:e.type,dom_event_coordinates:i||{clientX:0,clientY:0},dom_event_target:{id:U(n,"id"),tagName:B(n),className:U(n,"class"),text:r}}})}queueEvent(e){try{this.logger.log("[dom-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}generateSelector(e,t={}){if(!e||!e.nodeType||e.nodeType!==Node.ELEMENT_NODE)return"";const i=t.root||document.body||document.documentElement,n=t.maxAttempts||10,s=t.priorityAttributes||["id","class","name","aria-label","type","title","alt"];if("html"===B(e).toLowerCase())return"html";let r="",a=0,o=e;const l=[];for(;o&&o!==i&&o!==document.documentElement&&a<n;){let e=B(o).toLowerCase();try{const t=o.getAttribute("id");if(t&&/^[a-zA-Z][\w-]*$/.test(t))return`#${t}`;if(o.classList?.length){const t=Array.from(o.classList).filter((e=>e&&"string"==typeof e)).map((e=>`.${e}`));t.length&&(e+=t.join(""))}for(const t of s)if("id"!==t&&"class"!==t)try{const i=o.getAttribute(t);i&&(e+=`[${t}="${i.replace(/"/g,'\\"')}"]`)}catch(e){}}catch(t){e=B(o).toLowerCase()||"*"}l.unshift(e||"*"),r=l.join(" > ");try{if(1===i.querySelectorAll(r).length)return r}catch(e){l.shift(),r=l.join(" > ")}try{o=o.parentElement}catch(e){break}a++}return r||"*"}},K=["log","info","warn","error","debug","assert","trace"],X=class extends j{constructor(){super(...arguments),this.originalConsole=null,this.isInitialized=!1}initialize(){if(!this.isInitialized)try{this.originalConsole=this.captureConsoleMethods(),K.forEach((e=>{e in console&&(console[e]=(...t)=>{try{this.handleEvent(t,e)}catch(e){d(e)}finally{this.originalConsole?.[e]&&this.originalConsole[e](...t)}})})),this.isInitialized=!0}catch(e){d(e)}}destroy(){try{if(!this.isInitialized)return;K.forEach((e=>{this.originalConsole&&this.originalConsole[e]&&(console[e]=this.originalConsole[e])})),this.isInitialized=!1}catch(e){d(e)}}handleEvent(e,t){try{if(!q())return;const i=Array.isArray(e)?e:[e],n=this.formatMessage(i,t);if(!n||n?.includes("groundcoverIgnore"))return;const s=this.extractAttributes(i),r=this.buildEvent({message:n,level:t,attributes:s});this.queueEvent(r)}catch(e){d(e)}}buildEvent({message:e,level:t,attributes:i}){return x({type:"log",attributes:{...i,message:k({text:e}),level:t}})}queueEvent(e){try{this.logger.log("[logs-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}extractAttributes(e){if(Array.isArray(e)&&0!==e.length)try{const t={};for(const i of e)null!==i&&"object"==typeof i&&"[object Object]"===Object.prototype.toString.call(i)&&Object.assign(t,i);return delete t.message,delete t.level,delete t.location,t}catch(e){return void d(e)}}formatMessage(e,t){if(!Array.isArray(e))return String(e);try{return e.map((e=>{if(void 0===e)return"undefined";if(null===e)return"null";if("object"==typeof e){if(e instanceof Error)try{return"error"===t&&e.stack||e.toString()}catch{try{const t={name:e.name,message:e.message,stack:e.stack};return JSON.stringify(t)}catch{return Object.prototype.toString.call(e)}}try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return String(e)})).join(" ")}catch(e){return d(e),"[Error formatting console message]"}}captureConsoleMethods(){const e={};try{K.forEach((t=>{console&&"function"==typeof console[t]&&(e[t]=console[t].bind(console))}))}catch(e){d(e)}return e}};function V(e){try{if(null==e)return"";if("string"==typeof e)return e;if(e instanceof FormData)return"[form data]";if(e instanceof Blob)return"[blob data]";if(e instanceof ArrayBuffer)return"[arraybuffer data]";if("undefined"!=typeof Document){if(e instanceof Document)return"[document data]";const t=e;if(9===t?.nodeType)return"[document data]"}if(e instanceof URLSearchParams)try{return e.toString()}catch{return"[unreadable body]"}return Array.isArray(e)||"object"==typeof e?JSON.stringify(e):String(e)}catch(e){return d(`Failed to format body: ${e}`),"[unreadable body]"}}var W,G=class extends j{constructor(){super(...arguments),this.maxBodyLength=5e3,this.config=H.getInstance(),this.idGenerator=new m,this.handleEvent=e=>{try{if(this.shouldIgnoreRequest(e.url))return;if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}},this.buildEvent=e=>{let t=e.url,i=e.url;this.logger.log("[network-events-listener.buildEvent] called",{event:e});try{t=new URL(e.url).pathname}catch(t){i=new URL(e.url,globalThis.location.href).href}this.logger.log("[network-events-listener.buildEvent] fullUrl",{fullUrl:i});const n=this.formatHeaders(e.request.headers),s=this.formatHeaders(e.response.headers),r=e.method?.toUpperCase()||"UNKNOWN",{traceId:a,spanId:o}=this.extractTraceIds(e.request.headers),l=k({text:e.request.body,maxLength:this.maxBodyLength}),c=k({text:e.response.body,maxLength:this.maxBodyLength}),d={type:"HTTP",operation:{name:r},resource_name:t,status:e.status?.toString(),subType:r,url:{full:i},http:{route:t,path:t,method:e.method,status:e.status?.toString(),request:{headers:n,method:e.method},response:{headers:s,status_code:e.status?.toString()}},error:e?.error?.type?{type:e.error?.type||"Unknown error"}:void 0,gc:{request:{body:l},response:{body:c}}},h={type:"network",timestamp:e?.timestamp&&!Number.isNaN(e.timestamp)?u(e.timestamp):void 0,end_timestamp:e?.end_time&&!Number.isNaN(e.end_time)?u(e.end_time):void 0,span_name:`${e.method} ${t}`,attributes:d};return a&&(h.traceId=a),o&&(h.spanId=o),x(h)}}async getResponseBody(e){let t;try{const i=e.clone().body;if(i){let e,n=i.getReader(),s=new TextDecoder,r="";for(;!(e=await n.read()).done;){let t=e.value;r+=s.decode(t)}t=r}else t=""}catch(e){t=`Unable to clone response: ${e}`}return t}initialize(){this.logger.log("[network-events-listener.initialize] called"),this.patchXHR(),this.patchFetch()}destroy(){globalThis.XMLHttpRequest.prototype.open=globalThis.XMLHttpRequest.prototype.open,globalThis.XMLHttpRequest.prototype.send=globalThis.XMLHttpRequest.prototype.send,globalThis.fetch=globalThis.fetch}shouldIgnoreRequest(e){try{const t=this.config.getConfig()?.getEndpoint();if(t&&e.includes(t))return!0;if([".tsx",".jsx",".css"].some((t=>e.toLowerCase().endsWith(t))))return!0;return(this.config.getConfig()?.options?.excludedUrls||[]).some((t=>{if(t instanceof RegExp)return t.test(e);if("string"==typeof t&&(t.includes("*")||t.includes("?"))){const i=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*").replace(/\?/g,".");return new RegExp(i).test(e)}return e===t}))}catch(e){return d(e),!1}}queueEvent(e){try{this.logger.log("[network-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}patchXHR(){const e=this,t=globalThis.XMLHttpRequest.prototype.open,i=globalThis.XMLHttpRequest.prototype.send,n=globalThis.XMLHttpRequest.prototype.setRequestHeader;let s;globalThis.XMLHttpRequest.prototype.open=function(i,n,r=!0,a,o){if(s=Date.now(),e.shouldIgnoreRequest(n.toString()))return t.apply(this,[i,n,r,a,o]);const l=new URL(n.toString(),globalThis.location.href).href;return this._requestMethod=i,this._requestUrl=l,this._requestHeaders={},t.apply(this,[i,n,r,a,o])},globalThis.XMLHttpRequest.prototype.setRequestHeader=function(e,t){return this._requestHeaders&&(this._requestHeaders[e]=t),n.apply(this,[e,t])},globalThis.XMLHttpRequest.prototype.send=function(...t){const n=this._requestUrl?.toString();if(!n||e.shouldIgnoreRequest(n))return i.apply(this,t);if(e.shouldAddTraceHeader(n)){const t=e.config.getConfig(),i=e.getTraceIds();t?.options?.traceOrigin?.name&&e.addHeaderToXhrRequest({request:this,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),i&&(t?.options?.tracePropagationTraceIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:i.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:i.decimalSpanId}),Boolean(t?.options?.tracePropagationHeaders)&&t?.options?.tracePropagationHeaders?.forEach((t=>{e.addHeaderToXhrRequest({request:this,headerName:t,headerValue:i.traceparent})})),t?.options?.tracePropagationHeaders?.length||e.addHeaderToXhrRequest({request:this,headerName:"traceparent",headerValue:i.traceparent}))}return this._requestBody=t[0]||"",this.addEventListener("load",(()=>{const t=Date.now(),i={};this.getAllResponseHeaders().split(/\r?\n/).forEach((e=>{const[t,n]=e.split(": ");t&&n&&(i[t.trim()]=n.trim())}));const n=V(this._requestBody),r=V(""===this.responseType||"text"===this.responseType?this.responseText:this.response),a={timestamp:s,end_time:t,method:this._requestMethod,url:this._requestUrl,body:n,status:this.status,request:{headers:this._requestHeaders||{},body:n},response:{headers:i,body:r}};e.handleEvent(a)})),i.apply(this,t)}}normalizeHeaders(e,t){try{const i=e?new Headers(e):new Headers;return t?(t instanceof Headers?t.forEach(((e,t)=>i.set(t,e))):Array.isArray(t)?new Headers(t).forEach(((e,t)=>i.set(t,e))):"object"==typeof t&&Object.entries(t).forEach((([e,t])=>i.set(e,t))),i):i}catch{return new Headers}}addTraceHeaders(e){const t=this.config.getConfig(),i=this.getTraceIds();if(t?.options?.traceOrigin?.name&&this.addHeaderToFetchRequest({init:e,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),!i)return;t?.options?.tracePropagationTraceIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:i.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:i.decimalSpanId});const n=t?.options?.tracePropagationHeaders;(n?.length?n:["traceparent"]).forEach((t=>{this.addHeaderToFetchRequest({init:e,headerName:t,headerValue:i.traceparent})}))}extractHeadersRecord(e){const t={};return e instanceof Headers&&e.forEach(((e,i)=>{t[i]=e})),t}patchFetch(){const e=globalThis.fetch;globalThis.fetch=(...t)=>{const i=Date.now();let[n,s]=t;s={...s||{}},t[1]=s;const r=n instanceof Request,a=s?.method||"GET",o=s?.body||"",l=n instanceof Request?n.url:n.toString();if(this.shouldIgnoreRequest(l))return e.apply(globalThis,t);const c=this.shouldAddTraceHeader(l);s||(t[1]={},s=t[1]),s.headers=this.normalizeHeaders(r?n.headers:void 0,s.headers),c&&this.addTraceHeaders(s);const d=this.extractHeadersRecord(s.headers),h=V(o);return e.apply(globalThis,t).then((e=>{const t=Date.now(),n=e.clone(),s={};n.headers.forEach(((e,t)=>{s[t]=e}));const r={method:a,url:l,timestamp:i,body:h,status:n.status,end_time:t,request:{headers:d,body:h},response:{headers:s,body:""}};try{s["content-type"]?.includes("text/event-stream")?(r.response.body="[unreadable response body]",this.handleEvent(r)):this.getResponseBody(n).then((e=>{r.response.body=e,this.handleEvent(r)})).catch((e=>{r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}))}catch(e){r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}return e})).catch((e=>{const t=Date.now();let i="NetworkError";const n=e.message||"Unknown network error";e instanceof TypeError&&n.includes("Failed to fetch")?i="ERR_NETWORK_FAILURE":n.includes("Name not resolved")?i="ERR_NAME_NOT_RESOLVED":n.includes("Connection refused")&&(i="ERR_CONNECTION_REFUSED");const s={method:a,url:l,body:h,status:0,end_time:t,request:{headers:d,body:h},response:{headers:{},body:""},error:{type:i}};throw this.handleEvent(s),e}))}}formatHeaders(e){const t=["authorization","cookie","set-cookie"];return Object.entries(e)?.reduce(((e,[i,n])=>{const s=i.toLowerCase();return t.includes(s)||/(token|key|secret|password)/i.test(s)?e[i]="[REDACTED]":e[i]=k({text:n}),e}),{})}extractTraceIds(e){const t=this.config.getConfig();let i="",n="";if(t?.options?.tracePropagationTraceIdHeaderName){const n=e[t.options.tracePropagationTraceIdHeaderName];n&&"string"==typeof n&&(i=n)}if(t?.options?.tracePropagationSpanIdHeaderName){const i=e[t.options.tracePropagationSpanIdHeaderName];i&&"string"==typeof i&&(n=i)}if(!i||!n){const t=this.extractTraceparentHeader(e);if(t){const e=t.split("-");4===e.length&&"00"===e[0]&&(!i&&e[1]&&32===e[1].length&&(i=e[1]),!n&&e[2]&&16===e[2].length&&(n=e[2]))}}return{traceId:i,spanId:n}}extractTraceparentHeader(e){const t=this.config.getConfig();if(t?.options?.tracePropagationHeaders?.length)for(const i of t.options.tracePropagationHeaders){const t=e[i];if(t&&"string"==typeof t)return t}const i=e.traceparent;return i&&"string"==typeof i?i:""}getTraceIds(){const e=this.idGenerator.generateSpanId(),t=this.idGenerator.generateTraceId();let i,n;try{i=BigInt("0x"+t).toString(10),n=BigInt("0x"+e).toString(10)}catch(s){this.logger.log("[network-events-listener.getTraceIds] error",{error:s}),i=t,n=e}return{traceparent:`00-${t}-${e}-01`,traceId:t,spanId:e,decimalTraceId:i,decimalSpanId:n}}addHeaderToXhrRequest({request:e,headerName:t,headerValue:i}){if(this.logger.log("[network-events-listener.addHeaderToXhrRequest] called",{request:e,headerName:t,headerValue:i}),this.isValidHeaderName(t))try{e.setRequestHeader(t,i),e._requestHeaders[t]=i}catch(e){this.logger.log("[network-events-listener.addHeaderToXhrRequest] Error setting header",{error:e,headerName:t,headerValue:i})}else this.logger.log("[network-events-listener.addHeaderToXhrRequest] Invalid header name",{headerName:t})}addHeaderToFetchRequest({init:e,headerName:t,headerValue:i}){if(this.logger.log("[network-events-listener.addHeaderToFetchRequest] called",{init:e,headerName:t,headerValue:i}),this.isValidHeaderName(t))try{e.headers instanceof Headers?e.headers.set(t,i):e.headers={...e.headers,[t]:i}}catch(e){this.logger.log("[network-events-listener.addHeaderToFetchRequest] Error setting header",{error:e,headerName:t,headerValue:i})}else this.logger.log("[network-events-listener.addHeaderToFetchRequest] Invalid header name",{headerName:t})}isValidHeaderName(e){if(!e||"string"!=typeof e)return!1;return/^[a-zA-Z0-9!#$&'*+\-.^_`|~]+$/.test(e)}shouldAddTraceHeader(e){const t=this.config.getConfig();if(this.logger.log("[network-events-listener.shouldAddTraceHeader] called",{url:e,config:t}),!t?.options?.tracePropagationUrls?.length)return!1;const i=t.options.tracePropagationUrls?.some((t=>{const i=t.replace(/\*/g,".*"),n=new RegExp(`^${i}$`),s=e.startsWith("/")?new URL(e,globalThis.location.href).pathname:e;return n.test(s)}));return this.logger.log("[network-events-listener.shouldAddTraceHeader] result",{url:e,result:i}),i}},J=class extends j{constructor(){super(...arguments),this.handleEvent=(e,t)=>{this.logger.log("[errors-events-listener.handleEvent] called");try{let i;if(e instanceof Error)i=e,this.enhanceError(i);else if(e instanceof ErrorEvent){if(i=e.error||new Error(e.message||"Unknown error"),/Script error\.?/.test(i.message))return;this.enhanceError(i,e)}else{if(!(e instanceof PromiseRejectionEvent))return;i=this.createUnhandledRejectionError(e)}const n=this.buildEvent(i,t);this.queueEvent(n)}catch(e){d(e)}}}initialize(){h?.addEventListener("error",this.handleEvent),h?.addEventListener("unhandledrejection",this.handleEvent)}destroy(){h?.removeEventListener("error",this.handleEvent),h?.removeEventListener("unhandledrejection",this.handleEvent)}buildEvent(e,t){return x({type:"exception",attributes:{error_type:e.name||"Error",error_message:e.message||"Unknown error",error_stacktrace:this.buildStackTrace(e),error_fingerprint:`${e.name}:${k({text:e.message,maxLength:400})}`,error_handled:t?.handled||!1,error_metadata:t?.metadata}})}queueEvent(e){try{this.logger.log("[errors-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}enhanceError(e,t){const{filename:i,lineno:n,colno:s}=t||{};i&&!e.fileName&&Object.defineProperty(e,"fileName",{value:i}),n&&!e.lineNumber&&Object.defineProperty(e,"lineNumber",{value:n}),s&&!e.columnNumber&&Object.defineProperty(e,"columnNumber",{value:s})}createUnhandledRejectionError(e){let t;if(e.reason instanceof Error)t=e.reason;else{const i="object"==typeof e.reason?JSON.stringify(e.reason,null,2):String(e.reason);t=new Error(i),t.name="UnhandledRejection",Object.defineProperty(t,"originalReason",{value:e.reason,enumerable:!1})}return t}buildStackTrace(e){if(!e)return[];try{return a.default.parse(e).map((e=>({filename:e.fileName||"unknown",function:e.functionName||"anonymous",lineno:e.lineNumber||0,colno:e.columnNumber||0})))}catch(e){return[]}}},Y=class extends j{constructor({isEnabled:e}){if(super(),this.currentUrl="",this.originalPushState=null,this.originalReplaceState=null,this.isInitialized=!1,this.isAutomaticNavigationEnabled=!1,this.activeNavigation=null,this.popStateHandler=()=>{this.handleLocationChange()},this.hashChangeHandler=()=>{this.handleLocationChange()},this.isInBrowserEnvironment()&&e)try{this.isAutomaticNavigationEnabled=e,this.currentUrl=h.location.href,this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState}catch(e){d(e)}}isInBrowserEnvironment(){try{return void 0!==h&&void 0!==h.location&&void 0!==h.history&&"function"==typeof h.addEventListener}catch(e){return d(e),!1}}initialize(){try{if(this.logger.log("[navigation-listener.initialize] called"),!this.isInBrowserEnvironment()||!this.originalPushState||!this.originalReplaceState)return void this.logger.log("[navigation-listener.initialize] Browser environment not available, skipping initialization");if(!this.isAutomaticNavigationEnabled)return void this.logger.log("[navigation-listener.initialize] Automatic navigation is disabled, skipping initialization");const e=this.originalPushState,t=this.originalReplaceState;history.pushState=(...t)=>{const i=e.apply(history,t);return this.handleLocationChange(),i},history.replaceState=(...e)=>{const i=t.apply(history,e);return this.handleLocationChange(),i},h&&"function"==typeof h.addEventListener&&(h.addEventListener("popstate",this.popStateHandler),h.addEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!0}catch(e){d(e)}}destroy(){try{this.logger.log("[navigation-listener.destroy] called"),this.isInitialized&&this.originalPushState&&this.originalReplaceState&&(history.pushState=this.originalPushState,history.replaceState=this.originalReplaceState),h&&"function"==typeof h.removeEventListener&&(h.removeEventListener("popstate",this.popStateHandler),h.removeEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!1}catch(e){d(e)}}handleEvent(){try{this.logger.log("[navigation-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){d(e)}}buildEvent(){this.logger.log("[navigation-listener.buildEvent] called",h.location.href);return x({type:"navigation",attributes:{page_url:h.location.href,metadata:this.activeNavigation?.metadata,duration_ms:this.activeNavigation?.duration_ms||0,start_timestamp:this.activeNavigation?.startTime||0,end_timestamp:this.activeNavigation?.endTime||0}})}queueEvent(e){try{this.logger.log("[navigation-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}startNavigation(e){this.logger.log("[navigation-listener.startNavigation] called",e),this.activeNavigation={page_url:h.location.href,metadata:e,startTime:Date.now(),endTime:0,duration_ms:0}}endNavigation(e){this.logger.log("[navigation-listener.endNavigation] called",e),this.activeNavigation?(this.logger.log("[navigation-listener.endNavigation] active navigation",this.activeNavigation),this.activeNavigation.endTime=Date.now(),this.activeNavigation.duration_ms=this.activeNavigation.endTime-this.activeNavigation.startTime,this.handleEvent()):this.logger.log("[navigation-listener.endNavigation] no active navigation found")}handleLocationChange(){try{if(!this.isInitialized)return;if(this.logger.log("[navigation-listener.handleLocationChange] called"),this.currentUrl===h.location.href)return;let e,t;try{e=new URL(h.location.href),t=new URL(this.currentUrl)}catch(e){return void d(e)}const i=e=>e.startsWith("#/"),n=i(e.hash)||i(t.hash);(e.pathname!==t.pathname||n&&e.hash!==t.hash)&&(this.currentUrl=h.location.href,this.handleEvent())}catch(e){d(e)}}},Z=class extends j{constructor(){super(),this.loadEventHandler=()=>{this.logger.log("load event"),this.handleEvent()}}initialize(){try{this.logger.log("[page-load-listener.initialize] called"),this.logger.log("globalThis",h),"complete"===h?.document?.readyState?(this.logger.log("Page already loaded, triggering event immediately"),this.handleEvent()):h?.addEventListener("load",this.loadEventHandler)}catch(e){this.logger.log("[page-load-listener.initialize] error",e),d(e)}}destroy(){h?.removeEventListener("load",this.loadEventHandler)}handleEvent(){try{this.logger.log("[page-load-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){d(e)}}buildEvent(){const e=performance.getEntriesByType("navigation")[0],t=e?e.loadEventEnd-e.startTime:0,i=performance.getEntriesByType("resource"),n={count:i.length,totalSize:0,totalDuration:0,byType:{}};i.forEach((e=>{const t=e,i=t.transferSize||0,s=t.duration,r=t.initiatorType;n.totalSize+=i,n.totalDuration+=s,n.byType[r]||(n.byType[r]={count:0,size:0,duration:0}),n.byType[r].count++,n.byType[r].size+=i,n.byType[r].duration+=s}));return x({type:"pageload",attributes:{page_url:h?.location?.href||"",page_load_time:t,page_referrer:h?.document?.referrer||"",page_resources:n}})}queueEvent(e){try{this.logger.log("[page-load-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}},Q=class extends j{constructor(){super(...arguments),this.handleEvent=e=>{try{if(!q())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){d(e)}}}initialize(){i.onCLS(this.handleEvent),i.onLCP(this.handleEvent),i.onFCP(this.handleEvent),i.onTTFB(this.handleEvent),i.onINP(this.handleEvent)}destroy(){}buildEvent(e){return x({type:"performance",attributes:{performance_metric_name:e.name,performance_metric_value:e.value,performance_metric_id:e.id,performance_metric_navigation_type:e.navigationType||""}})}queueEvent(e){try{this.logger.log("[performance-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){d(e)}}},ee=class{constructor(){this.logger=l.getInstance(),this.eventsPool=O.getInstance(),this.configManager=H.getInstance(),this.stopFn=null,this.isRecording=!1,this.events=[],this.startTime=0,this.batchSize=3,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.sendTimerId=null}initialize(){this.logger.log("[session-replay-listener] initialize called"),this.isRecording?this.logger.log("[session-replay-listener] already recording"):this.startRecording()}startRecording(){try{this.startTime=Date.now(),this.events=[],this.isRecording=!0,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0;const e=this.configManager.getConfig()?.options?.sessionReplay?.blockedSelectors?.filter(Boolean),t=e?.length?e.join(", "):void 0;this.stopFn=n.record({...t?{blockSelector:t}:{},slimDOMOptions:{script:!1,comment:!1,headWhitespace:!1},sampling:{mousemove:100,input:"last",scroll:150},checkoutEveryNms:3e4,emit:(e,t)=>{if(this.isRecording)try{this.events.push(e),t&&(this.batchContainsFullSnapshot=!0,this.fullSnapshotTimestamp=Date.now(),this.logger.log("[session-replay-listener] full snapshot captured at",this.fullSnapshotTimestamp)),this.events.length>=this.batchSize&&this.scheduleSendBatch()}catch(e){d(e),this.logger.log("[session-replay-listener] failed to process event in emit callback:",e)}}})||null,this.logger.log("[session-replay-listener] started recording with full snapshot interval:",3e4,"ms")}catch(e){d(e),this.logger.log("[session-replay-listener] failed to start recording:",e),this.isRecording=!1}}scheduleSendBatch(){null===this.sendTimerId&&(this.sendTimerId=h.setTimeout((()=>{if(this.sendTimerId=null,this.isRecording)try{this.sendBatch()}catch(e){d(e),this.logger.log("[session-replay-listener] failed to send deferred batch:",e)}}),0))}clearScheduledSend(){null!==this.sendTimerId&&(h.clearTimeout(this.sendTimerId),this.sendTimerId=null)}sendBatch(){if(0!==this.events.length)try{const e=this.events.map((e=>s.pack(e))),t=this.batchContainsFullSnapshot,i=this.fullSnapshotTimestamp,n={_gc_replay_data:{events:e}};t&&(n.replay_is_full_snapshot=!0,n.replay_full_snapshot_timestamp=i);const r=x({type:"replay",attributes:n});this.eventsPool.addEvent(r),this.events=[],this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.logger.log("[session-replay-listener] sent batch with",e.length,"events",t?"(includes full snapshot)":"")}catch(e){d(e),this.logger.log("[session-replay-listener] failed to send batch:",e)}}stopRecording(){if(this.isRecording)try{this.clearScheduledSend(),this.stopFn?.(),this.isRecording=!1,this.events.length>0&&this.sendBatch(),this.stopFn=null,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0}catch(e){d(e),this.logger.log("[session-replay-listener] failed to stop recording:",e)}}destroy(){this.stopRecording()}},te=["pointerdown","keydown","scroll","touchstart","mousemove"],ie=class{constructor(e){this.logger=l.getInstance(),this.lastSignalMs=0,this.idleTimer=null,this.installed=!1,this.handleInput=()=>{const e=Date.now();if(!(e-this.lastSignalMs<3e4)){this.notePresence(e);try{this.callbacks.onPresence(e)}catch(e){d(e)}}},this.callbacks=e}initialize(){this.installed||(this.installed=!0,te.forEach((e=>{try{h.addEventListener?.(e,this.handleInput,{capture:!0,passive:!0})}catch(e){d(e)}})),this.armIdleTimer(),this.logger.log("[user-presence-monitor] installed"))}notePresence(e){this.lastSignalMs=e,this.armIdleTimer()}resetIdleTimer(){this.armIdleTimer()}armIdleTimer(){try{this.idleTimer&&clearTimeout(this.idleTimer),this.idleTimer=null,this.idleTimer=setTimeout((()=>{this.idleTimer=null,this.logger.log("[user-presence-monitor] user idle for",S,"ms");try{this.callbacks.onIdle()}catch(e){d(e)}}),S)}catch(e){d(e)}}destroy(){if(this.installed){this.installed=!1,te.forEach((e=>{try{h.removeEventListener?.(e,this.handleInput,{capture:!0})}catch(e){d(e)}}));try{this.idleTimer&&clearTimeout(this.idleTimer)}catch(e){d(e)}finally{this.idleTimer=null}}}},ne=new Set(["dom.event","navigation","pageload","custom"]),se=class{constructor(){this.logger=l.getInstance(),this.eventsPool=O.getInstance(),this.configManager=H.getInstance(),this.idGenerator=new m,this.logEventsListener=null,this.domEventsListener=null,this.errorsEventsListener=null,this.networkEventsListener=null,this.navigationListener=null,this.performanceListener=null,this.pageLoadListener=null,this.sessionReplayListener=null,this.isRotating=!1,this.replayDesired=!1,this.presenceMonitor=null}instrument(){this.logger.log("[instrumentation-manager.instrument] called"),this.eventsPool.setBeforeEnqueueHook((e=>this.reconcileSessionOnEnqueue(e))),this.presenceMonitor?.destroy(),this.presenceMonitor=new ie({onPresence:e=>this.reconcileSession(e,!0),onIdle:()=>this.pauseReplayOnIdle()}),this.presenceMonitor.initialize();const e=this.configManager.getConfig(),t=e?.options?.enabledEvents,i=!t||0===t.length,n=[];(i||t?.includes("pageload"))&&(this.pageLoadListener=new Z,this.pageLoadListener.initialize(),n.push("pageload")),(i||t?.includes("dom"))&&(this.domEventsListener=new $,this.domEventsListener.initialize(),n.push("dom")),(i||t?.includes("logs"))&&(this.logEventsListener=new X,this.logEventsListener.initialize(),n.push("logs")),(i||t?.includes("exceptions"))&&(this.errorsEventsListener=new J,this.errorsEventsListener.initialize(),n.push("exceptions")),(i||t?.includes("network"))&&(this.networkEventsListener=new G,this.networkEventsListener.initialize(),n.push("network")),(i||t?.includes("performance"))&&(this.performanceListener=new Q,this.performanceListener.initialize(),n.push("performance")),this.navigationListener=new Y({isEnabled:i||t?.includes("navigation")}),this.navigationListener.initialize(),n.push("navigation"),this.logger.log("[instrumentation-manager.instrument] initialized listeners based on config:",n)}sendCustomEvent(e){this.logger.log("[instrumentation-manager.sendCustomEvent] called",e);try{const t=x({type:"custom",attributes:{custom_event_name:e?.event,custom_event_attributes:e?.attributes}});this.eventsPool.addEvent(t)}catch(e){d(e)}}emitLog(e,t,i){if(this.logEventsListener){this.logger.log("[instrumentation-manager.emitLog] called",{message:e,level:t,attributes:i});try{const n={...i||{}};delete n.message,delete n.level,delete n.location;const s=x({type:"log",attributes:{...n,message:k({text:e}),level:t}});this.eventsPool.addEvent(s)}catch(e){d(e)}}}captureException(e,t){try{this.logger.log("[instrumentation-manager.captureException] called",e),this.errorsEventsListener?.handleEvent(e,{handled:!0,metadata:t})}catch(e){d(e)}}isNavigationTrackingEnabled(){const e=this.configManager.getConfig();return e?.options?.enabledEvents?.includes("navigation")||0===e?.options?.enabledEvents?.length}startNavigation(e){this.logger.log("[instrumentation-manager.startNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.startNavigation] startNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.startNavigation(e)}endNavigation(e){this.logger.log("[instrumentation-manager.endNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.endNavigation] endNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.endNavigation(e)}isReplayRecording(){return null!==this.sessionReplayListener}startReplayRecording(){if(this.replayDesired=!0,this.isReplayRecording())return void this.logger.log("[instrumentation-manager] replay recording already started");const e=Date.now();(this.configManager.isSessionInactive(e)||this.configManager.isSessionExpired(e))&&this.rotateSession({reason:"replay start into stale session"}),this.startReplayListener(),this.presenceMonitor?.resetIdleTimer()}stopReplayRecording(){this.replayDesired=!1,this.stopReplayListener()}syncReplayState(e){this.replayDesired&&e&&!this.isReplayRecording()&&(this.logger.log("[instrumentation-manager] user present and replay desired — starting replay"),this.startReplayListener())}pauseReplayOnIdle(){this.isReplayRecording()&&(this.logger.log("[instrumentation-manager] pausing replay: user idle window elapsed"),this.stopReplayListener())}startReplayListener(){if(this.sessionReplayListener)this.logger.log("[instrumentation-manager] replay recording already started");else try{const e=new ee;e.initialize(),this.sessionReplayListener=e,this.logger.log("[instrumentation-manager] started replay recording")}catch(e){d(e),this.logger.log("[instrumentation-manager] failed to start replay recording:",e)}}reconcileSession(e,t){if(this.isRotating)return;t&&this.configManager.isSessionInactive(e)&&this.rotateSession({reason:"inactivity gap"});const i=this.configManager.getSessionElapsedMs(e),n=this.configManager.getSessionMaxDuration(),s=i>=n;this.logger.log("[instrumentation-manager] session reconcile",{userPresent:t,elapsedMs:i,maxDurationMs:n,expired:s}),s&&this.rotateSession({reason:"max session duration"}),this.syncReplayState(t),t&&(this.configManager.setLastActivityMs(e),this.presenceMonitor?.notePresence(e))}reconcileSessionOnEnqueue(e){this.isRotating||("replay"!==e.type?this.reconcileSession(Date.now(),ne.has(e.type)):this.logger.log("[instrumentation-manager] enqueue rotation check: ignoring replay event"))}rotateSession(e){if(this.isRotating)return;this.isRotating=!0;const t=this.configManager.getSessionId(),i=this.isReplayRecording();this.logger.log("[instrumentation-manager] session rotation start",{reason:e.reason,previousId:t,wasReplayActive:i});const n=(e,t)=>{try{this.logger.log(`[instrumentation-manager] session rotation: ${e}`),t()}catch(t){d(t),this.logger.log(`[instrumentation-manager] failed to ${e} during session rotation:`,t)}};try{n("stop replay",(()=>this.stopReplayListener())),n("flush events",(()=>this.eventsPool.flush())),n("rotate session ids",(()=>{const e=this.idGenerator.generateId();this.configManager.setSessionId(e),this.configManager.setSessionStartTime(u(Date.now())),this.logger.log("[instrumentation-manager] session id rotated",{from:t,to:e})})),n("sync replay",(()=>this.syncReplayState(i)))}finally{this.isRotating=!1,this.logger.log("[instrumentation-manager] session rotation complete",{reason:e.reason})}}stopReplayListener(){this.sessionReplayListener?(this.sessionReplayListener.stopRecording(),this.sessionReplayListener=null):this.logger.log("[instrumentation-manager] cannot stop replay recording: replay listener not initialized")}uninstrument(){this.eventsPool.setBeforeEnqueueHook(null),this.presenceMonitor?.destroy(),this.presenceMonitor=null,this.domEventsListener?.destroy(),this.logEventsListener?.destroy(),this.errorsEventsListener?.destroy(),this.networkEventsListener?.destroy(),this.navigationListener?.destroy(),this.performanceListener?.destroy(),this.pageLoadListener?.destroy(),this.sessionReplayListener?.destroy(),this.sessionReplayListener=null,this.replayDesired=!1}},re="gcSample",ae=class{constructor(e){this.initialized=!1,this.logger=l.initialize({debug:e.options?.debug||!1,prefix:"[groundcover]"}),this.logger.log("[session-manager] initialize called"),this.instrumentationManager=new se,this.idGenerator=new m;if(!this.getSamplingDecision(e.options?.sessionSampleRate))return void this.logger.log("[session-manager] session is not sampled");if(this.initialized)return void this.logger.log("[session-manager] SDK already initialized");const t=H.getInstance();t.initialize(e);O.getInstance().initialize();const i=e.sessionId,n=t.getSessionId();i&&i!==n?(t.setSessionId(i),t.setSessionStartTime(u(Date.now())),this.logger.log("[session-manager] using provided sessionId:",i)):i?(t.setSessionId(i),this.logger.log("[session-manager] continuing provided sessionId:",i)):n||t.setSessionId(this.idGenerator.generateId());const s=Date.now(),r=p(t.getSessionStartTime());if(r>0&&s-r>=-6e4){if(t.isSessionExpired(s)||t.isSessionInactive(s)){const e=t.isSessionExpired(s)?"persisted session past cap":"inactivity gap across reload",i=t.getSessionId(),n=this.idGenerator.generateId();t.setSessionId(n),t.setSessionStartTime(u(s)),this.logger.log(`[session-manager] init-time rotation: ${e}`,{from:i,to:n})}}else t.setSessionStartTime(u(s));this.instrumentationManager.instrument(),this.initialized=!0}getSamplingDecision(e){try{if(!e||1===e)return!0;const t=w(re);if(t){const e="1"===t;return this.logger.log("[session-manager] using stored sampling decision:",e),e}const i=!e||Math.random()<e;return R(re,i?"1":"0"),this.logger.log("[session-manager] stored new sampling decision:",i),i}catch(t){this.logger.log("[session-manager] session storage access failed:",t);const i=!e||Math.random()<e;return this.logger.log("[session-manager] using fallback sampling decision:",i),i}}identifyUser(e){if(this.logger.log("[session-manager] identifyUser called"),!this.initialized)return void this.logger.log("[session-manager] cannot identify user: SDK not initialized");H.getInstance().updateConfig({userIdentifier:e})}updateConfig(e){if(!this.initialized)return void this.logger.log("[session-manager] cannot update config: SDK not initialized");H.getInstance().updateConfig(e)}sendCustomEvent(e){this.initialized?this.instrumentationManager.sendCustomEvent(e):this.logger.log("[session-manager] cannot send custom event: SDK not initialized")}startNavigation(e){this.initialized?this.instrumentationManager.startNavigation(e):this.logger.log("[session-manager] cannot start navigation: SDK not initialized")}endNavigation(e){this.initialized?this.instrumentationManager.endNavigation(e):this.logger.log("[session-manager] cannot end navigation: SDK not initialized")}captureException(e,t){this.initialized?this.instrumentationManager.captureException(e,t):this.logger.log("[session-manager] Cannot capture exception: SDK not initialized")}emitLog(e,t,i){this.initialized?this.instrumentationManager.emitLog(e,t,i):this.logger.log("[session-manager] Cannot emit log: SDK not initialized")}getSessionId(){return this.initialized?H.getInstance().getSessionId():(this.logger.log("[session-manager] cannot get session ID: SDK not initialized"),"")}async setSessionId(e){if(this.logger.log("[session-manager] setSessionId called"),this.initialized)try{const t=O.getInstance();await t.flushSync();const i=e||this.idGenerator.generateId(),n=H.getInstance();n.setSessionId(i),n.setSessionStartTime(u(Date.now())),this.logger.log("[session-manager] session ID set successfully:",i)}catch(e){this.logger.log("[session-manager] failed to set session ID:",e)}else this.logger.log("[session-manager] cannot set session ID: SDK not initialized")}startReplayRecording(){this.initialized?this.instrumentationManager.startReplayRecording():this.logger.log("[session-manager] cannot start replay recording: SDK not initialized")}stopReplayRecording(){this.initialized?this.instrumentationManager.stopReplayRecording():this.logger.log("[session-manager] cannot stop replay recording: SDK not initialized")}destroy(){this.initialized&&(this.instrumentationManager.uninstrument(),O.getInstance().destroy(),H.getInstance().clearSessionState(),this.initialized=!1)}},oe=new class{constructor(e){this.manager=e}emit(e,t,i){this.manager&&this.manager.emitLog(t,e,i)}log(e,t){this.emit("log",e,t)}info(e,t){this.emit("info",e,t)}warn(e,t){this.emit("warn",e,t)}error(e,t){this.emit("error",e,t)}debug(e,t){this.emit("debug",e,t)}trace(e,t){this.emit("trace",e,t)}};var le={init:function(e){try{W=new ae({cluster:e?.cluster||"",environment:e?.environment||"",namespace:e?.namespace,releaseId:e?.releaseId,dsn:e?.dsn||"",appId:e?.appId||"",userIdentifier:e?.userIdentifier,apiKey:e?.apiKey||"",options:e?.options,sessionId:e?.sessionId}),oe.manager=W}catch(e){d(e)}},identifyUser:function(e){W?W.identifyUser(e):console.warn("[groundcover] identifyUser: groundcover is not initialized. please call init() first")},sendCustomEvent:function(e){W&&W.sendCustomEvent(e)},captureException:function(e,t){W&&W.captureException(e,t)},logger:oe,updateConfig:function(e){W&&W.updateConfig(e)},startNavigation:function(e){W?W.startNavigation(e):console.warn("[groundcover] startNavigation: groundcover is not initialized. please call init() first")},endNavigation:function(e){W?W.endNavigation(e):console.warn("[groundcover] endNavigation: groundcover is not initialized. please call init() first")},getSessionId:function(){return W?W.getSessionId():(console.warn("[groundcover] getSessionId: groundcover is not initialized. please call init() first"),"")},setSessionId:async function(e){if(W)try{await W.setSessionId(e)}catch(e){console.error("[groundcover] setSessionId: failed to set session ID:",e)}else console.warn("[groundcover] setSessionId: groundcover is not initialized. please call init() first")},startReplayRecording:function(){W?W.startReplayRecording():console.warn("[groundcover] startReplayRecording: groundcover is not initialized. please call init() first")},stopReplayRecording:function(){W?W.stopReplayRecording():console.warn("[groundcover] stopReplayRecording: groundcover is not initialized. please call init() first")}};if("undefined"!=typeof window&&!window.groundcover)try{window.groundcover=le}catch(e){console.warn("[groundcover] Failed to expose groundcover on window:",e)}var ce=le;module.exports=ce;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{strToU8 as e,gzipSync as t}from"fflate";import n from"error-stack-parser";import{onCLS as i,onLCP as s,onFCP as r,onTTFB as a,onINP as o}from"web-vitals";import{record as l}from"rrweb";import{pack as c}from"@rrweb/packer";var d=console.log.bind(console),h=class e{constructor(){this.isDebugEnabled=!1,this.prefix=""}static initialize(t){return e.instance||(e.instance=new e),t&&(e.instance.isDebugEnabled=t.debug??!1,e.instance.prefix=t.prefix??""),e.instance}static getInstance(t){return e?.instance||e.initialize(t)}formatMessage(e){return`[${(new Date).toISOString()}] ${this.prefix} ${e}`}log(e,...t){this.isDebugEnabled&&d(this.formatMessage(e),...t)}updateConfig(e){this.isDebugEnabled=e.debug??this.isDebugEnabled,this.prefix=e.prefix??this.prefix}},g=h.getInstance();function u(e){g.log("[error-handler.handleError] called",e,{groundcoverIgnore:!0})}var p="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:{},m=1e6;function f(e){return e*m}function v(e){return e/m}var y=class{constructor(){this.generateTraceId=function(){for(let e=0;e<16;e++)b[e]=Math.floor(16*Math.random())+48,b[e]>=58&&(b[e]+=39);return I+String.fromCharCode.apply(null,b.slice(0,16))},this.generateSpanId=E(8),this.generateId=E(16)}},b=Array(32);function E(e){return function(){for(let t=0;t<2*e;t++)b[t]=Math.floor(16*Math.random())+48,b[t]>=58&&(b[t]+=39);return String.fromCharCode.apply(null,b.slice(0,2*e))}}var I="0000000000000000";var S=144e5,T=288e5,w={batchSize:10,batchTimeout:1e4,eventSampleRate:1,sessionSampleRate:1,environment:"development",debug:!1,enableCompression:!0,maskFields:[],enableMasking:!1,enabledEvents:[],tracePropagationUrls:[],tracePropagationHeaders:[],tracePropagationTraceIdHeaderName:"",tracePropagationSpanIdHeaderName:"",traceOrigin:{name:"",value:""},sessionMaxDuration:S},R=class e{constructor(t){this.dsn=t?.dsn||"",this.appId=t?.appId||"",this.cluster=t?.cluster||"",this.apiKey=t?.apiKey||"",this.environment=t?.environment||"",this.namespace=t?.namespace||"",this.releaseId=t?.releaseId,this.userIdentifier=t?.userIdentifier||null,this.options={...w,...t?.options||{}},this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t?.options?.sessionMaxDuration)}static normalizeSessionMaxDuration(e){return void 0===e?S:"number"!=typeof e||!Number.isFinite(e)||e<6e4||e>T?(console.warn("[groundcover] sessionMaxDuration must be a finite number between 60000 ms (1 minute) and 28800000 ms (8 hours); falling back to the 4-hour default"),S):e}getEndpoint(){return this.dsn?.startsWith("http")?this.dsn:`https://${this.dsn}`}updateConfig(t){t.options&&(this.options={...this.options,...t.options},void 0!==t.options.sessionMaxDuration&&(this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t.options.sessionMaxDuration))),t.userIdentifier&&(this.userIdentifier={...this.userIdentifier,...t.userIdentifier}),t.appId&&(this.appId=t.appId),t.dsn&&(this.dsn=t.dsn),t.apiKey&&(this.apiKey=t.apiKey),t.cluster&&(this.cluster=t.cluster),t.environment&&(this.environment=t.environment),t.namespace&&(this.namespace=t.namespace),t.releaseId&&(this.releaseId=t.releaseId)}};function N(e){try{return globalThis?.sessionStorage?.getItem(e)??null}catch(e){return u(e),null}}function L(e,t){try{globalThis?.sessionStorage?.setItem(e,t)}catch(e){u(e)}}function z(e){try{globalThis?.sessionStorage?.removeItem(e)}catch(e){u(e)}}var C=class e{constructor(){this.config=null,this.logger=h.getInstance(),this.sessionId=null,this.sessionStartTime=null}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(e){this.config||(this.config=new R(e))}getConfig(){return this.config?this.config:(this.logger.log("[config-manager] configuration not initialized"),null)}getSessionId(){if(this.sessionId)return this.sessionId;return N("gcId")||""}setSessionId(e){this.sessionId=e,L("gcId",e)}getSessionStartTime(){return this.sessionStartTime?this.sessionStartTime:function(e,t=0){const n=N(e);if(null===n)return t;const i=Number(n);return Number.isFinite(i)?i:t}("gcStartTime",0)}setSessionStartTime(e){this.sessionStartTime=e,L("gcStartTime",String(e))}getSessionMaxDuration(){return this.config?.options?.sessionMaxDuration??S}getSessionElapsedMs(e=Date.now()){const t=this.getSessionStartTime();return t?e-v(t):0}isSessionExpired(e=Date.now()){return this.getSessionElapsedMs(e)>=this.getSessionMaxDuration()}updateConfig(e){this.logger.log("[config-manager] updateConfig called"),this.config?(e&&(this.logger.log("[config-manager] updating options"),this.config.updateConfig(e)),e?.userIdentifier&&(this.logger.log("[config-manager] updating user identifier"),this.config.userIdentifier={...this.config.userIdentifier,...e.userIdentifier})):this.logger.log("[config-manager] configuration not initialized")}},H=C.getInstance(),q=new y,_=h.getInstance();function x(){const e=H?.getConfig()?.options?.eventSampleRate;return!(void 0!==e&&!Number.isNaN(e))||Math.random()<=e}function k(e,t="",n=new WeakSet,i=0){try{return i>=10?{[t||"depth_limit"]:"[Depth Limit]"}:null!=(s=e)&&"number"==typeof s.nodeType&&"string"==typeof s.nodeName?{[t||"dom_node"]:`[${e?.constructor?.name||"DomNode"}]`}:n.has(e)?{[t||"circular_reference"]:"[Circular]"}:(n.add(e),Object.entries(e).reduce(((e,[s,r])=>{const a=t?`${t}.${s}`:s;return"object"==typeof r&&null!==r?Array.isArray(r)?e[a]=r.map(((e,t)=>"object"==typeof e&&null!==e?k(e,`${a}[${t}]`,n,i+1):e)):Object.assign(e,k(r,a,n,i+1)):e[a]=r,e}),{}))}catch(e){return _.log("[events-processors.formatEventObject] Error formatting event object",e),{[t||"format_error"]:"[Error formatting object]"}}finally{n.delete(e)}var s}function M(e){const t=f(Date.now()),n=q.generateId(),i=e.spanId||q.generateSpanId(),s=e.traceId||q.generateTraceId(),r=e.parentSpanId||"",a=k(e.attributes||{}),o=function(){const e=p?.location;return e?{path:e.hash&&!e.hash.match(/^#[a-z0-9-]+$/i)&&e.hash.startsWith("#/")?e.hash:e.pathname,url:e.href,title:p?.document?.title||""}:{path:"",url:"",title:p?.document?.title||""}}(),l={type:e.type,id:n,spanId:i,parentSpanId:r,traceId:s,attributes:{...a,location:o}};return e.span_name&&(l.span_name=e.span_name),"network"===e.type?(l.start_timestamp=e.timestamp||t,l.end_timestamp=e?.end_timestamp):l.timestamp=t,l}function D({text:e,maxLength:t=1e4}){return e?e.length<=t||"string"!=typeof e?e:e.substring(0,t):""}var P=class{constructor(){this.logger=h.getInstance(),this.config=C.getInstance()}compress(n){const i=this.config.getConfig();if(!i?.options?.enableCompression)return{data:(new TextEncoder).encode(n),isCompressed:!1};try{const i=e(n);return{data:t(i),isCompressed:!0}}catch(e){return{data:(new TextEncoder).encode(n),isCompressed:!1}}}async send(e){const t=this.config.getConfig();if(t)try{t?.options?.debug&&this.logger.log("Sending batch:",e,{groundcoverIgnore:!0});const n=t.apiKey,i=this.buildEndpoint();if(!n)return void this.logger.log("No API key found");const s=JSON.stringify(e),{data:r,isCompressed:a}=this.compress(s);fetch(i,{method:"POST",headers:{"Content-Type":"application/json",apikey:n,"Content-Encoding":a?"gzip":""},body:r}).catch((e=>{this.logger.log("Network error while sending batch:",e,{groundcoverIgnore:!0})}))}catch(e){t.options.debug&&this.logger.log("Failed to send batch:",e,{groundcoverIgnore:!0})}}buildEndpoint(){const e=this.config.getConfig();if(!e)return"";const{dsn:t}=e;return`${t}/json/rum`}},A=["navigation","dom.event"],O=class e{constructor(){this.events=[],this.timeoutId=null,this.config=C.getInstance(),this.logger=h.getInstance(),this.initialized=!1,this.onBeforeEnqueue=null,this.transporter=new P}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(){try{if(this.initialized)return;this.scheduleFlush(),"undefined"!=typeof document&&document.addEventListener("visibilitychange",(()=>{"hidden"===document.visibilityState&&this.flush()})),this.initialized=!0}catch(e){u(e)}}setBeforeEnqueueHook(e){this.onBeforeEnqueue=e}addEvent(e){if(e&&this.initialized)try{const t=this.config.getConfig()?.options?.beforeSend,n=this.config.getConfig()?.options?.enrichEvent;if(t&&!t(e))return;if(this.onBeforeEnqueue)try{this.onBeforeEnqueue(e)}catch(e){u(e)}n&&(e=n(e)),this.events.push(e);const i=this.config.getConfig()?.options?.batchSize||100;this.events.length>=i&&!A.includes(e.type)&&this.flush()}catch(e){u(e)}}flush(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[],this.scheduleFlush()}catch(e){u(e)}}async flushSync(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");await this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[]}catch(e){u(e)}}scheduleFlush(){try{null!==this.timeoutId&&(p.clearTimeout(this.timeoutId),this.timeoutId=null);const e=this.config.getConfig()?.options?.batchTimeout;if(!e)return;this.timeoutId=p.setTimeout((()=>{this.timeoutId=null,this.flush()}),e)}catch(e){u(e)}}detectBrowser(){const e=navigator.userAgent;let t="unknown",n="unknown";const i=navigator.platform||"unknown",s=/Mobile|Android|iPhone|iPad|iPod/i.test(e);return/Edg/.test(e)?(t="Edge",n=e.match(/Edg\/([\d.]+)/)?.[1]||n):/Chrome/.test(e)&&!/Chromium/.test(e)?(t="Chrome",n=e.match(/Chrome\/([\d.]+)/)?.[1]||n):/Firefox/.test(e)?(t="Firefox",n=e.match(/Firefox\/([\d.]+)/)?.[1]||n):/Safari/.test(e)&&!/Chrome/.test(e)?(t="Safari",n=e.match(/Version\/([\d.]+)/)?.[1]||n):/Trident/.test(e)?(t="Internet Explorer",n=/rv:([^)]+)\)/i.test(e)?e.match(/rv:([^)]+)\)/i)?.[1]||n:e.match(/MSIE ([^;]+)/)?.[1]||n):/OPR/.test(e)&&(t="Opera",n=e.match(/OPR\/([\d.]+)/)?.[1]||n),{name:t,version:n,platform:i,language:navigator.language,mobile:s}}getSessionAttributes(){const e=this.detectBrowser(),t=this.config.getConfig();return{cluster:t?.cluster||"",env:t?.environment||"",namespace:t?.namespace||"",releaseId:t?.releaseId||"",session_id:this.config.getSessionId(),session_start_time:this.config.getSessionStartTime(),user:t?.userIdentifier||{},"service.name":t?.appId,userAgent:navigator.userAgent,browser:e}}};O.instance=null;var j=O,F=class{constructor(){this.logger=h.getInstance(),this.eventsPool=j.getInstance()}},U="undefined"!=typeof Element?Object.getOwnPropertyDescriptor(Element.prototype,"tagName")?.get:void 0;function B(e,t){try{return e.getAttribute(t)||""}catch{return""}}function $(e){try{const t=U?.call(e);return"string"==typeof t?t:""}catch{return""}}var K=class extends F{constructor(){super(...arguments),this.eventHandlers=[],this.capturedEvents=["click","change","keydown","select","submit"],this.config=C.getInstance(),this.getShouldMaskText=e=>{const t=this.config.getConfig(),n=t?.options?.maskFields||[],i=t?.options?.enableMasking||!1;return(["password"===e.target?.type,e.target?.id?.toLowerCase().includes("password"),e.target?.id?.toLowerCase().includes("credit-card"),e.target?.id?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.hasAttribute("data-private")].some((e=>e))||n.some((t=>e.target?.id?.toLowerCase().includes(t))))&&i},this.getKeyCode=e=>{if(e instanceof KeyboardEvent&&e.code&&"Dead"!==e.key){const t=this.getShouldMaskText({target:e.target});return t||t?"*":e.key}return""},this.getText=e=>{const t=e.target,n=this.config.getConfig();return n?.options?.enableMasking||!1?"*":D({text:t.innerText||""})},this.getSelector=e=>e.target instanceof Element&&this.generateSelector(e.target)||"",this.getCoordinates=e=>{if({mouseup:!0,mousedown:!0,mousemove:!0,mouseover:!0}[e.type]){const{clientX:t,clientY:n}=e||{};return{clientX:t,clientY:n}}return null}}initialize(){try{this.logger.log("[dom-events-listener.initialize] called"),this.capturedEvents?.forEach((e=>{const t=e=>{try{this.handleEvent(e)}catch(e){u(e)}};this.eventHandlers.push({type:e,handler:t}),p?.addEventListener(e,t)}))}catch(e){u(e)}}destroy(){this.eventHandlers.forEach((({type:e,handler:t})=>{global?.removeEventListener?.(e,t)})),this.eventHandlers=[]}handleEvent(e){try{if(!x())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}}buildEvent(e){const t=this.getSelector(e),n=this.getCoordinates(e),i=e.target,s=this.getKeyCode(e),r=this.getText(e);this.logger.log("[dom-events-listener.buildEvent] called");return M({type:"dom.event",attributes:{dom_event_selector:t,dom_event_key_code:s,dom_event_type:e.type,dom_event_coordinates:n||{clientX:0,clientY:0},dom_event_target:{id:B(i,"id"),tagName:$(i),className:B(i,"class"),text:r}}})}queueEvent(e){try{this.logger.log("[dom-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}generateSelector(e,t={}){if(!e||!e.nodeType||e.nodeType!==Node.ELEMENT_NODE)return"";const n=t.root||document.body||document.documentElement,i=t.maxAttempts||10,s=t.priorityAttributes||["id","class","name","aria-label","type","title","alt"];if("html"===$(e).toLowerCase())return"html";let r="",a=0,o=e;const l=[];for(;o&&o!==n&&o!==document.documentElement&&a<i;){let e=$(o).toLowerCase();try{const t=o.getAttribute("id");if(t&&/^[a-zA-Z][\w-]*$/.test(t))return`#${t}`;if(o.classList?.length){const t=Array.from(o.classList).filter((e=>e&&"string"==typeof e)).map((e=>`.${e}`));t.length&&(e+=t.join(""))}for(const t of s)if("id"!==t&&"class"!==t)try{const n=o.getAttribute(t);n&&(e+=`[${t}="${n.replace(/"/g,'\\"')}"]`)}catch(e){}}catch(t){e=$(o).toLowerCase()||"*"}l.unshift(e||"*"),r=l.join(" > ");try{if(1===n.querySelectorAll(r).length)return r}catch(e){l.shift(),r=l.join(" > ")}try{o=o.parentElement}catch(e){break}a++}return r||"*"}},X=["log","info","warn","error","debug","assert","trace"],V=class extends F{constructor(){super(...arguments),this.originalConsole=null,this.isInitialized=!1}initialize(){if(!this.isInitialized)try{this.originalConsole=this.captureConsoleMethods(),X.forEach((e=>{e in console&&(console[e]=(...t)=>{try{this.handleEvent(t,e)}catch(e){u(e)}finally{this.originalConsole?.[e]&&this.originalConsole[e](...t)}})})),this.isInitialized=!0}catch(e){u(e)}}destroy(){try{if(!this.isInitialized)return;X.forEach((e=>{this.originalConsole&&this.originalConsole[e]&&(console[e]=this.originalConsole[e])})),this.isInitialized=!1}catch(e){u(e)}}handleEvent(e,t){try{if(!x())return;const n=Array.isArray(e)?e:[e],i=this.formatMessage(n,t);if(!i||i?.includes("groundcoverIgnore"))return;const s=this.extractAttributes(n),r=this.buildEvent({message:i,level:t,attributes:s});this.queueEvent(r)}catch(e){u(e)}}buildEvent({message:e,level:t,attributes:n}){return M({type:"log",attributes:{...n,message:D({text:e}),level:t}})}queueEvent(e){try{this.logger.log("[logs-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}extractAttributes(e){if(Array.isArray(e)&&0!==e.length)try{const t={};for(const n of e)null!==n&&"object"==typeof n&&"[object Object]"===Object.prototype.toString.call(n)&&Object.assign(t,n);return delete t.message,delete t.level,delete t.location,t}catch(e){return void u(e)}}formatMessage(e,t){if(!Array.isArray(e))return String(e);try{return e.map((e=>{if(void 0===e)return"undefined";if(null===e)return"null";if("object"==typeof e){if(e instanceof Error)try{return"error"===t&&e.stack||e.toString()}catch{try{const t={name:e.name,message:e.message,stack:e.stack};return JSON.stringify(t)}catch{return Object.prototype.toString.call(e)}}try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return String(e)})).join(" ")}catch(e){return u(e),"[Error formatting console message]"}}captureConsoleMethods(){const e={};try{X.forEach((t=>{console&&"function"==typeof console[t]&&(e[t]=console[t].bind(console))}))}catch(e){u(e)}return e}};function G(e){try{if(null==e)return"";if("string"==typeof e)return e;if(e instanceof FormData)return"[form data]";if(e instanceof Blob)return"[blob data]";if(e instanceof ArrayBuffer)return"[arraybuffer data]";if("undefined"!=typeof Document){if(e instanceof Document)return"[document data]";const t=e;if(9===t?.nodeType)return"[document data]"}if(e instanceof URLSearchParams)try{return e.toString()}catch{return"[unreadable body]"}return Array.isArray(e)||"object"==typeof e?JSON.stringify(e):String(e)}catch(e){return u(`Failed to format body: ${e}`),"[unreadable body]"}}var W,J=class extends F{constructor(){super(...arguments),this.maxBodyLength=5e3,this.config=C.getInstance(),this.idGenerator=new y,this.handleEvent=e=>{try{if(this.shouldIgnoreRequest(e.url))return;if(!x())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}},this.buildEvent=e=>{let t=e.url,n=e.url;this.logger.log("[network-events-listener.buildEvent] called",{event:e});try{t=new URL(e.url).pathname}catch(t){n=new URL(e.url,globalThis.location.href).href}this.logger.log("[network-events-listener.buildEvent] fullUrl",{fullUrl:n});const i=this.formatHeaders(e.request.headers),s=this.formatHeaders(e.response.headers),r=e.method?.toUpperCase()||"UNKNOWN",{traceId:a,spanId:o}=this.extractTraceIds(e.request.headers),l=D({text:e.request.body,maxLength:this.maxBodyLength}),c=D({text:e.response.body,maxLength:this.maxBodyLength}),d={type:"HTTP",operation:{name:r},resource_name:t,status:e.status?.toString(),subType:r,url:{full:n},http:{route:t,path:t,method:e.method,status:e.status?.toString(),request:{headers:i,method:e.method},response:{headers:s,status_code:e.status?.toString()}},error:e?.error?.type?{type:e.error?.type||"Unknown error"}:void 0,gc:{request:{body:l},response:{body:c}}},h={type:"network",timestamp:e?.timestamp&&!Number.isNaN(e.timestamp)?f(e.timestamp):void 0,end_timestamp:e?.end_time&&!Number.isNaN(e.end_time)?f(e.end_time):void 0,span_name:`${e.method} ${t}`,attributes:d};return a&&(h.traceId=a),o&&(h.spanId=o),M(h)}}async getResponseBody(e){let t;try{const n=e.clone().body;if(n){let e,i=n.getReader(),s=new TextDecoder,r="";for(;!(e=await i.read()).done;){let t=e.value;r+=s.decode(t)}t=r}else t=""}catch(e){t=`Unable to clone response: ${e}`}return t}initialize(){this.logger.log("[network-events-listener.initialize] called"),this.patchXHR(),this.patchFetch()}destroy(){globalThis.XMLHttpRequest.prototype.open=globalThis.XMLHttpRequest.prototype.open,globalThis.XMLHttpRequest.prototype.send=globalThis.XMLHttpRequest.prototype.send,globalThis.fetch=globalThis.fetch}shouldIgnoreRequest(e){try{const t=this.config.getConfig()?.getEndpoint();if(t&&e.includes(t))return!0;if([".tsx",".jsx",".css"].some((t=>e.toLowerCase().endsWith(t))))return!0;return(this.config.getConfig()?.options?.excludedUrls||[]).some((t=>{if(t instanceof RegExp)return t.test(e);if("string"==typeof t&&(t.includes("*")||t.includes("?"))){const n=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*").replace(/\?/g,".");return new RegExp(n).test(e)}return e===t}))}catch(e){return u(e),!1}}queueEvent(e){try{this.logger.log("[network-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}patchXHR(){const e=this,t=globalThis.XMLHttpRequest.prototype.open,n=globalThis.XMLHttpRequest.prototype.send,i=globalThis.XMLHttpRequest.prototype.setRequestHeader;let s;globalThis.XMLHttpRequest.prototype.open=function(n,i,r=!0,a,o){if(s=Date.now(),e.shouldIgnoreRequest(i.toString()))return t.apply(this,[n,i,r,a,o]);const l=new URL(i.toString(),globalThis.location.href).href;return this._requestMethod=n,this._requestUrl=l,this._requestHeaders={},t.apply(this,[n,i,r,a,o])},globalThis.XMLHttpRequest.prototype.setRequestHeader=function(e,t){return this._requestHeaders&&(this._requestHeaders[e]=t),i.apply(this,[e,t])},globalThis.XMLHttpRequest.prototype.send=function(...t){const i=this._requestUrl?.toString();if(!i||e.shouldIgnoreRequest(i))return n.apply(this,t);if(e.shouldAddTraceHeader(i)){const t=e.config.getConfig(),n=e.getTraceIds();t?.options?.traceOrigin?.name&&e.addHeaderToXhrRequest({request:this,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),n&&(t?.options?.tracePropagationTraceIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:n.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:n.decimalSpanId}),Boolean(t?.options?.tracePropagationHeaders)&&t?.options?.tracePropagationHeaders?.forEach((t=>{e.addHeaderToXhrRequest({request:this,headerName:t,headerValue:n.traceparent})})),t?.options?.tracePropagationHeaders?.length||e.addHeaderToXhrRequest({request:this,headerName:"traceparent",headerValue:n.traceparent}))}return this._requestBody=t[0]||"",this.addEventListener("load",(()=>{const t=Date.now(),n={};this.getAllResponseHeaders().split(/\r?\n/).forEach((e=>{const[t,i]=e.split(": ");t&&i&&(n[t.trim()]=i.trim())}));const i=G(this._requestBody),r=G(""===this.responseType||"text"===this.responseType?this.responseText:this.response),a={timestamp:s,end_time:t,method:this._requestMethod,url:this._requestUrl,body:i,status:this.status,request:{headers:this._requestHeaders||{},body:i},response:{headers:n,body:r}};e.handleEvent(a)})),n.apply(this,t)}}normalizeHeaders(e,t){try{const n=e?new Headers(e):new Headers;return t?(t instanceof Headers?t.forEach(((e,t)=>n.set(t,e))):Array.isArray(t)?new Headers(t).forEach(((e,t)=>n.set(t,e))):"object"==typeof t&&Object.entries(t).forEach((([e,t])=>n.set(e,t))),n):n}catch{return new Headers}}addTraceHeaders(e){const t=this.config.getConfig(),n=this.getTraceIds();if(t?.options?.traceOrigin?.name&&this.addHeaderToFetchRequest({init:e,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),!n)return;t?.options?.tracePropagationTraceIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:n.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:n.decimalSpanId});const i=t?.options?.tracePropagationHeaders;(i?.length?i:["traceparent"]).forEach((t=>{this.addHeaderToFetchRequest({init:e,headerName:t,headerValue:n.traceparent})}))}extractHeadersRecord(e){const t={};return e instanceof Headers&&e.forEach(((e,n)=>{t[n]=e})),t}patchFetch(){const e=globalThis.fetch;globalThis.fetch=(...t)=>{const n=Date.now();let[i,s]=t;s={...s||{}},t[1]=s;const r=i instanceof Request,a=s?.method||"GET",o=s?.body||"",l=i instanceof Request?i.url:i.toString();if(this.shouldIgnoreRequest(l))return e.apply(globalThis,t);const c=this.shouldAddTraceHeader(l);s||(t[1]={},s=t[1]),s.headers=this.normalizeHeaders(r?i.headers:void 0,s.headers),c&&this.addTraceHeaders(s);const d=this.extractHeadersRecord(s.headers),h=G(o);return e.apply(globalThis,t).then((e=>{const t=Date.now(),i=e.clone(),s={};i.headers.forEach(((e,t)=>{s[t]=e}));const r={method:a,url:l,timestamp:n,body:h,status:i.status,end_time:t,request:{headers:d,body:h},response:{headers:s,body:""}};try{s["content-type"]?.includes("text/event-stream")?(r.response.body="[unreadable response body]",this.handleEvent(r)):this.getResponseBody(i).then((e=>{r.response.body=e,this.handleEvent(r)})).catch((e=>{r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}))}catch(e){r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}return e})).catch((e=>{const t=Date.now();let n="NetworkError";const i=e.message||"Unknown network error";e instanceof TypeError&&i.includes("Failed to fetch")?n="ERR_NETWORK_FAILURE":i.includes("Name not resolved")?n="ERR_NAME_NOT_RESOLVED":i.includes("Connection refused")&&(n="ERR_CONNECTION_REFUSED");const s={method:a,url:l,body:h,status:0,end_time:t,request:{headers:d,body:h},response:{headers:{},body:""},error:{type:n}};throw this.handleEvent(s),e}))}}formatHeaders(e){const t=["authorization","cookie","set-cookie"];return Object.entries(e)?.reduce(((e,[n,i])=>{const s=n.toLowerCase();return t.includes(s)||/(token|key|secret|password)/i.test(s)?e[n]="[REDACTED]":e[n]=D({text:i}),e}),{})}extractTraceIds(e){const t=this.config.getConfig();let n="",i="";if(t?.options?.tracePropagationTraceIdHeaderName){const i=e[t.options.tracePropagationTraceIdHeaderName];i&&"string"==typeof i&&(n=i)}if(t?.options?.tracePropagationSpanIdHeaderName){const n=e[t.options.tracePropagationSpanIdHeaderName];n&&"string"==typeof n&&(i=n)}if(!n||!i){const t=this.extractTraceparentHeader(e);if(t){const e=t.split("-");4===e.length&&"00"===e[0]&&(!n&&e[1]&&32===e[1].length&&(n=e[1]),!i&&e[2]&&16===e[2].length&&(i=e[2]))}}return{traceId:n,spanId:i}}extractTraceparentHeader(e){const t=this.config.getConfig();if(t?.options?.tracePropagationHeaders?.length)for(const n of t.options.tracePropagationHeaders){const t=e[n];if(t&&"string"==typeof t)return t}const n=e.traceparent;return n&&"string"==typeof n?n:""}getTraceIds(){const e=this.idGenerator.generateSpanId(),t=this.idGenerator.generateTraceId();let n,i;try{n=BigInt("0x"+t).toString(10),i=BigInt("0x"+e).toString(10)}catch(s){this.logger.log("[network-events-listener.getTraceIds] error",{error:s}),n=t,i=e}return{traceparent:`00-${t}-${e}-01`,traceId:t,spanId:e,decimalTraceId:n,decimalSpanId:i}}addHeaderToXhrRequest({request:e,headerName:t,headerValue:n}){if(this.logger.log("[network-events-listener.addHeaderToXhrRequest] called",{request:e,headerName:t,headerValue:n}),this.isValidHeaderName(t))try{e.setRequestHeader(t,n),e._requestHeaders[t]=n}catch(e){this.logger.log("[network-events-listener.addHeaderToXhrRequest] Error setting header",{error:e,headerName:t,headerValue:n})}else this.logger.log("[network-events-listener.addHeaderToXhrRequest] Invalid header name",{headerName:t})}addHeaderToFetchRequest({init:e,headerName:t,headerValue:n}){if(this.logger.log("[network-events-listener.addHeaderToFetchRequest] called",{init:e,headerName:t,headerValue:n}),this.isValidHeaderName(t))try{e.headers instanceof Headers?e.headers.set(t,n):e.headers={...e.headers,[t]:n}}catch(e){this.logger.log("[network-events-listener.addHeaderToFetchRequest] Error setting header",{error:e,headerName:t,headerValue:n})}else this.logger.log("[network-events-listener.addHeaderToFetchRequest] Invalid header name",{headerName:t})}isValidHeaderName(e){if(!e||"string"!=typeof e)return!1;return/^[a-zA-Z0-9!#$&'*+\-.^_`|~]+$/.test(e)}shouldAddTraceHeader(e){const t=this.config.getConfig();if(this.logger.log("[network-events-listener.shouldAddTraceHeader] called",{url:e,config:t}),!t?.options?.tracePropagationUrls?.length)return!1;const n=t.options.tracePropagationUrls?.some((t=>{const n=t.replace(/\*/g,".*"),i=new RegExp(`^${n}$`),s=e.startsWith("/")?new URL(e,globalThis.location.href).pathname:e;return i.test(s)}));return this.logger.log("[network-events-listener.shouldAddTraceHeader] result",{url:e,result:n}),n}},Y=class extends F{constructor(){super(...arguments),this.handleEvent=(e,t)=>{this.logger.log("[errors-events-listener.handleEvent] called");try{let n;if(e instanceof Error)n=e,this.enhanceError(n);else if(e instanceof ErrorEvent){if(n=e.error||new Error(e.message||"Unknown error"),/Script error\.?/.test(n.message))return;this.enhanceError(n,e)}else{if(!(e instanceof PromiseRejectionEvent))return;n=this.createUnhandledRejectionError(e)}const i=this.buildEvent(n,t);this.queueEvent(i)}catch(e){u(e)}}}initialize(){p?.addEventListener("error",this.handleEvent),p?.addEventListener("unhandledrejection",this.handleEvent)}destroy(){p?.removeEventListener("error",this.handleEvent),p?.removeEventListener("unhandledrejection",this.handleEvent)}buildEvent(e,t){return M({type:"exception",attributes:{error_type:e.name||"Error",error_message:e.message||"Unknown error",error_stacktrace:this.buildStackTrace(e),error_fingerprint:`${e.name}:${D({text:e.message,maxLength:400})}`,error_handled:t?.handled||!1,error_metadata:t?.metadata}})}queueEvent(e){try{this.logger.log("[errors-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}enhanceError(e,t){const{filename:n,lineno:i,colno:s}=t||{};n&&!e.fileName&&Object.defineProperty(e,"fileName",{value:n}),i&&!e.lineNumber&&Object.defineProperty(e,"lineNumber",{value:i}),s&&!e.columnNumber&&Object.defineProperty(e,"columnNumber",{value:s})}createUnhandledRejectionError(e){let t;if(e.reason instanceof Error)t=e.reason;else{const n="object"==typeof e.reason?JSON.stringify(e.reason,null,2):String(e.reason);t=new Error(n),t.name="UnhandledRejection",Object.defineProperty(t,"originalReason",{value:e.reason,enumerable:!1})}return t}buildStackTrace(e){if(!e)return[];try{return n.parse(e).map((e=>({filename:e.fileName||"unknown",function:e.functionName||"anonymous",lineno:e.lineNumber||0,colno:e.columnNumber||0})))}catch(e){return[]}}},Z=class extends F{constructor({isEnabled:e}){if(super(),this.currentUrl="",this.originalPushState=null,this.originalReplaceState=null,this.isInitialized=!1,this.isAutomaticNavigationEnabled=!1,this.activeNavigation=null,this.popStateHandler=()=>{this.handleLocationChange()},this.hashChangeHandler=()=>{this.handleLocationChange()},this.isInBrowserEnvironment()&&e)try{this.isAutomaticNavigationEnabled=e,this.currentUrl=p.location.href,this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState}catch(e){u(e)}}isInBrowserEnvironment(){try{return void 0!==p&&void 0!==p.location&&void 0!==p.history&&"function"==typeof p.addEventListener}catch(e){return u(e),!1}}initialize(){try{if(this.logger.log("[navigation-listener.initialize] called"),!this.isInBrowserEnvironment()||!this.originalPushState||!this.originalReplaceState)return void this.logger.log("[navigation-listener.initialize] Browser environment not available, skipping initialization");if(!this.isAutomaticNavigationEnabled)return void this.logger.log("[navigation-listener.initialize] Automatic navigation is disabled, skipping initialization");const e=this.originalPushState,t=this.originalReplaceState;history.pushState=(...t)=>{const n=e.apply(history,t);return this.handleLocationChange(),n},history.replaceState=(...e)=>{const n=t.apply(history,e);return this.handleLocationChange(),n},p&&"function"==typeof p.addEventListener&&(p.addEventListener("popstate",this.popStateHandler),p.addEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!0}catch(e){u(e)}}destroy(){try{this.logger.log("[navigation-listener.destroy] called"),this.isInitialized&&this.originalPushState&&this.originalReplaceState&&(history.pushState=this.originalPushState,history.replaceState=this.originalReplaceState),p&&"function"==typeof p.removeEventListener&&(p.removeEventListener("popstate",this.popStateHandler),p.removeEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!1}catch(e){u(e)}}handleEvent(){try{this.logger.log("[navigation-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){u(e)}}buildEvent(){this.logger.log("[navigation-listener.buildEvent] called",p.location.href);return M({type:"navigation",attributes:{page_url:p.location.href,metadata:this.activeNavigation?.metadata,duration_ms:this.activeNavigation?.duration_ms||0,start_timestamp:this.activeNavigation?.startTime||0,end_timestamp:this.activeNavigation?.endTime||0}})}queueEvent(e){try{this.logger.log("[navigation-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}startNavigation(e){this.logger.log("[navigation-listener.startNavigation] called",e),this.activeNavigation={page_url:p.location.href,metadata:e,startTime:Date.now(),endTime:0,duration_ms:0}}endNavigation(e){this.logger.log("[navigation-listener.endNavigation] called",e),this.activeNavigation?(this.logger.log("[navigation-listener.endNavigation] active navigation",this.activeNavigation),this.activeNavigation.endTime=Date.now(),this.activeNavigation.duration_ms=this.activeNavigation.endTime-this.activeNavigation.startTime,this.handleEvent()):this.logger.log("[navigation-listener.endNavigation] no active navigation found")}handleLocationChange(){try{if(!this.isInitialized)return;if(this.logger.log("[navigation-listener.handleLocationChange] called"),this.currentUrl===p.location.href)return;let e,t;try{e=new URL(p.location.href),t=new URL(this.currentUrl)}catch(e){return void u(e)}const n=e=>e.startsWith("#/"),i=n(e.hash)||n(t.hash);(e.pathname!==t.pathname||i&&e.hash!==t.hash)&&(this.currentUrl=p.location.href,this.handleEvent())}catch(e){u(e)}}},Q=class extends F{constructor(){super(),this.loadEventHandler=()=>{this.logger.log("load event"),this.handleEvent()}}initialize(){try{this.logger.log("[page-load-listener.initialize] called"),this.logger.log("globalThis",p),"complete"===p?.document?.readyState?(this.logger.log("Page already loaded, triggering event immediately"),this.handleEvent()):p?.addEventListener("load",this.loadEventHandler)}catch(e){this.logger.log("[page-load-listener.initialize] error",e),u(e)}}destroy(){p?.removeEventListener("load",this.loadEventHandler)}handleEvent(){try{this.logger.log("[page-load-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){u(e)}}buildEvent(){const e=performance.getEntriesByType("navigation")[0],t=e?e.loadEventEnd-e.startTime:0,n=performance.getEntriesByType("resource"),i={count:n.length,totalSize:0,totalDuration:0,byType:{}};n.forEach((e=>{const t=e,n=t.transferSize||0,s=t.duration,r=t.initiatorType;i.totalSize+=n,i.totalDuration+=s,i.byType[r]||(i.byType[r]={count:0,size:0,duration:0}),i.byType[r].count++,i.byType[r].size+=n,i.byType[r].duration+=s}));return M({type:"pageload",attributes:{page_url:p?.location?.href||"",page_load_time:t,page_referrer:p?.document?.referrer||"",page_resources:i}})}queueEvent(e){try{this.logger.log("[page-load-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}},ee=class extends F{constructor(){super(...arguments),this.handleEvent=e=>{try{if(!x())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}}}initialize(){i(this.handleEvent),s(this.handleEvent),r(this.handleEvent),a(this.handleEvent),o(this.handleEvent)}destroy(){}buildEvent(e){return M({type:"performance",attributes:{performance_metric_name:e.name,performance_metric_value:e.value,performance_metric_id:e.id,performance_metric_navigation_type:e.navigationType||""}})}queueEvent(e){try{this.logger.log("[performance-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}},te=class{constructor(e){this.logger=h.getInstance(),this.eventsPool=j.getInstance(),this.configManager=C.getInstance(),this.stopFn=null,this.isRecording=!1,this.events=[],this.startTime=0,this.batchSize=3,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.inactivityTimer=null,this.sendTimerId=null,this.onInactivityStop=e??null}initialize(){this.logger.log("[session-replay-listener] initialize called"),this.isRecording?this.logger.log("[session-replay-listener] already recording"):this.startRecording()}startRecording(){try{this.startTime=Date.now(),this.events=[],this.isRecording=!0,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0;const e=this.configManager.getConfig()?.options?.sessionReplay?.blockedSelectors?.filter(Boolean),t=e?.length?e.join(", "):void 0;this.stopFn=l({...t?{blockSelector:t}:{},slimDOMOptions:{script:!1,comment:!1,headWhitespace:!1},sampling:{mousemove:100,input:"last",scroll:150},checkoutEveryNms:3e4,emit:(e,t)=>{if(this.isRecording)try{t||this.resetInactivityTimer(),this.events.push(e),t&&(this.batchContainsFullSnapshot=!0,this.fullSnapshotTimestamp=Date.now(),this.logger.log("[session-replay-listener] full snapshot captured at",this.fullSnapshotTimestamp)),this.events.length>=this.batchSize&&this.scheduleSendBatch()}catch(e){u(e),this.logger.log("[session-replay-listener] failed to process event in emit callback:",e)}}})||null,this.resetInactivityTimer(),this.logger.log("[session-replay-listener] started recording with full snapshot interval:",3e4,"ms")}catch(e){u(e),this.logger.log("[session-replay-listener] failed to start recording:",e),this.isRecording=!1}}scheduleSendBatch(){null===this.sendTimerId&&(this.sendTimerId=p.setTimeout((()=>{if(this.sendTimerId=null,this.isRecording)try{this.sendBatch()}catch(e){u(e),this.logger.log("[session-replay-listener] failed to send deferred batch:",e)}}),0))}clearScheduledSend(){null!==this.sendTimerId&&(p.clearTimeout(this.sendTimerId),this.sendTimerId=null)}resetInactivityTimer(){this.inactivityTimer&&clearTimeout(this.inactivityTimer),this.inactivityTimer=setTimeout((()=>{this.logger.log("[session-replay-listener] stopping recording due to inactivity");try{this.onInactivityStop?.()}catch(e){u(e),this.logger.log("[session-replay-listener] onInactivityStop error:",e)}this.stopRecording()}),18e5)}clearInactivityTimer(){this.inactivityTimer&&(clearTimeout(this.inactivityTimer),this.inactivityTimer=null)}sendBatch(){if(0!==this.events.length)try{const e=this.events.map((e=>c(e))),t=this.batchContainsFullSnapshot,n=this.fullSnapshotTimestamp,i={_gc_replay_data:{events:e}};t&&(i.replay_is_full_snapshot=!0,i.replay_full_snapshot_timestamp=n);const s=M({type:"replay",attributes:i});this.eventsPool.addEvent(s),this.events=[],this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.logger.log("[session-replay-listener] sent batch with",e.length,"events",t?"(includes full snapshot)":"")}catch(e){u(e),this.logger.log("[session-replay-listener] failed to send batch:",e)}}stopRecording(){if(this.isRecording)try{this.clearInactivityTimer(),this.clearScheduledSend(),this.stopFn?.(),this.isRecording=!1,this.events.length>0&&this.sendBatch(),this.stopFn=null,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0}catch(e){u(e),this.logger.log("[session-replay-listener] failed to stop recording:",e)}}destroy(){this.stopRecording()}},ne=class{constructor(){this.logger=h.getInstance(),this.eventsPool=j.getInstance(),this.configManager=C.getInstance(),this.idGenerator=new y,this.logEventsListener=null,this.domEventsListener=null,this.errorsEventsListener=null,this.networkEventsListener=null,this.navigationListener=null,this.performanceListener=null,this.pageLoadListener=null,this.sessionReplayListener=null,this.isRotating=!1}instrument(){this.logger.log("[instrumentation-manager.instrument] called"),this.eventsPool.setBeforeEnqueueHook((e=>this.maybeRotateOnEnqueue(e)));const e=this.configManager.getConfig(),t=e?.options?.enabledEvents,n=!t||0===t.length,i=[];(n||t?.includes("pageload"))&&(this.pageLoadListener=new Q,this.pageLoadListener.initialize(),i.push("pageload")),(n||t?.includes("dom"))&&(this.domEventsListener=new K,this.domEventsListener.initialize(),i.push("dom")),(n||t?.includes("logs"))&&(this.logEventsListener=new V,this.logEventsListener.initialize(),i.push("logs")),(n||t?.includes("exceptions"))&&(this.errorsEventsListener=new Y,this.errorsEventsListener.initialize(),i.push("exceptions")),(n||t?.includes("network"))&&(this.networkEventsListener=new J,this.networkEventsListener.initialize(),i.push("network")),(n||t?.includes("performance"))&&(this.performanceListener=new ee,this.performanceListener.initialize(),i.push("performance")),this.navigationListener=new Z({isEnabled:n||t?.includes("navigation")}),this.navigationListener.initialize(),i.push("navigation"),this.logger.log("[instrumentation-manager.instrument] initialized listeners based on config:",i)}sendCustomEvent(e){this.logger.log("[instrumentation-manager.sendCustomEvent] called",e);try{const t=M({type:"custom",attributes:{custom_event_name:e?.event,custom_event_attributes:e?.attributes}});this.eventsPool.addEvent(t)}catch(e){u(e)}}emitLog(e,t,n){if(this.logEventsListener){this.logger.log("[instrumentation-manager.emitLog] called",{message:e,level:t,attributes:n});try{const i={...n||{}};delete i.message,delete i.level,delete i.location;const s=M({type:"log",attributes:{...i,message:D({text:e}),level:t}});this.eventsPool.addEvent(s)}catch(e){u(e)}}}captureException(e,t){try{this.logger.log("[instrumentation-manager.captureException] called",e),this.errorsEventsListener?.handleEvent(e,{handled:!0,metadata:t})}catch(e){u(e)}}isNavigationTrackingEnabled(){const e=this.configManager.getConfig();return e?.options?.enabledEvents?.includes("navigation")||0===e?.options?.enabledEvents?.length}startNavigation(e){this.logger.log("[instrumentation-manager.startNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.startNavigation] startNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.startNavigation(e)}endNavigation(e){this.logger.log("[instrumentation-manager.endNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.endNavigation] endNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.endNavigation(e)}isReplayRecording(){return null!==this.sessionReplayListener}startReplayRecording(){if(this.sessionReplayListener)this.logger.log("[instrumentation-manager] replay recording already started");else try{const e=new te((()=>{this.rotateSession({restartReplay:!1,reason:"replay inactivity"})}));e.initialize(),this.sessionReplayListener=e,this.logger.log("[instrumentation-manager] started replay recording")}catch(e){u(e),this.logger.log("[instrumentation-manager] failed to start replay recording:",e)}}maybeRotateOnEnqueue(e){if(this.isRotating)return;if("replay"===e.type)return void this.logger.log("[instrumentation-manager] enqueue rotation check: ignoring replay event");const t=Date.now(),n=this.configManager.getSessionElapsedMs(t),i=this.configManager.getSessionMaxDuration(),s=n>=i;this.logger.log("[instrumentation-manager] enqueue rotation check",{event:e.type,elapsedMs:n,maxDurationMs:i,expired:s}),s&&this.rotateSession({restartReplay:!0,reason:"max session duration"})}rotateSession(e){if(this.isRotating)return;this.isRotating=!0;const t=this.configManager.getSessionId(),n=this.isReplayRecording();this.logger.log("[instrumentation-manager] session rotation start",{reason:e.reason,previousId:t,wasReplayActive:n,restartReplay:e.restartReplay});const i=(e,t)=>{try{this.logger.log(`[instrumentation-manager] session rotation: ${e}`),t()}catch(t){u(t),this.logger.log(`[instrumentation-manager] failed to ${e} during session rotation:`,t)}};try{i("stop replay",(()=>this.stopReplayRecording())),i("flush events",(()=>this.eventsPool.flush())),i("rotate session ids",(()=>{const e=this.idGenerator.generateId();this.configManager.setSessionId(e),this.configManager.setSessionStartTime(f(Date.now())),this.logger.log("[instrumentation-manager] session id rotated",{from:t,to:e})})),e.restartReplay&&n&&i("restart replay",(()=>this.startReplayRecording()))}finally{this.isRotating=!1,this.logger.log("[instrumentation-manager] session rotation complete",{reason:e.reason})}}stopReplayRecording(){this.sessionReplayListener?(this.sessionReplayListener.stopRecording(),this.sessionReplayListener=null):this.logger.log("[instrumentation-manager] cannot stop replay recording: replay listener not initialized")}uninstrument(){this.eventsPool.setBeforeEnqueueHook(null),this.domEventsListener?.destroy(),this.logEventsListener?.destroy(),this.errorsEventsListener?.destroy(),this.networkEventsListener?.destroy(),this.navigationListener?.destroy(),this.performanceListener?.destroy(),this.pageLoadListener?.destroy(),this.sessionReplayListener?.destroy()}},ie="gcSample",se=class{constructor(e){this.initialized=!1,this.logger=h.initialize({debug:e.options?.debug||!1,prefix:"[groundcover]"}),this.logger.log("[session-manager] initialize called"),this.instrumentationManager=new ne,this.idGenerator=new y;if(!this.getSamplingDecision(e.options?.sessionSampleRate))return void this.logger.log("[session-manager] session is not sampled");if(this.initialized)return void this.logger.log("[session-manager] SDK already initialized");const t=C.getInstance();t.initialize(e);j.getInstance().initialize();const n=e.sessionId,i=t.getSessionId();n&&n!==i?(t.setSessionId(n),t.setSessionStartTime(f(Date.now())),this.logger.log("[session-manager] using provided sessionId:",n)):n?(t.setSessionId(n),this.logger.log("[session-manager] continuing provided sessionId:",n)):i||t.setSessionId(this.idGenerator.generateId());const s=Date.now(),r=v(t.getSessionStartTime());if(r>0&&s-r>=-6e4){if(t.isSessionExpired(s)){const e=t.getSessionId(),n=this.idGenerator.generateId();t.setSessionId(n),t.setSessionStartTime(f(s)),this.logger.log("[session-manager] init-time rotation: persisted session past cap",{from:e,to:n})}}else t.setSessionStartTime(f(s));this.instrumentationManager.instrument(),this.initialized=!0}getSamplingDecision(e){try{if(!e||1===e)return!0;const t=N(ie);if(t){const e="1"===t;return this.logger.log("[session-manager] using stored sampling decision:",e),e}const n=!e||Math.random()<e;return L(ie,n?"1":"0"),this.logger.log("[session-manager] stored new sampling decision:",n),n}catch(t){this.logger.log("[session-manager] session storage access failed:",t);const n=!e||Math.random()<e;return this.logger.log("[session-manager] using fallback sampling decision:",n),n}}identifyUser(e){if(this.logger.log("[session-manager] identifyUser called"),!this.initialized)return void this.logger.log("[session-manager] cannot identify user: SDK not initialized");C.getInstance().updateConfig({userIdentifier:e})}updateConfig(e){if(!this.initialized)return void this.logger.log("[session-manager] cannot update config: SDK not initialized");C.getInstance().updateConfig(e)}sendCustomEvent(e){this.initialized?this.instrumentationManager.sendCustomEvent(e):this.logger.log("[session-manager] cannot send custom event: SDK not initialized")}startNavigation(e){this.initialized?this.instrumentationManager.startNavigation(e):this.logger.log("[session-manager] cannot start navigation: SDK not initialized")}endNavigation(e){this.initialized?this.instrumentationManager.endNavigation(e):this.logger.log("[session-manager] cannot end navigation: SDK not initialized")}captureException(e,t){this.initialized?this.instrumentationManager.captureException(e,t):this.logger.log("[session-manager] Cannot capture exception: SDK not initialized")}emitLog(e,t,n){this.initialized?this.instrumentationManager.emitLog(e,t,n):this.logger.log("[session-manager] Cannot emit log: SDK not initialized")}getSessionId(){return this.initialized?C.getInstance().getSessionId():(this.logger.log("[session-manager] cannot get session ID: SDK not initialized"),"")}async setSessionId(e){if(this.logger.log("[session-manager] setSessionId called"),this.initialized)try{const t=j.getInstance();await t.flushSync();const n=e||this.idGenerator.generateId(),i=C.getInstance();i.setSessionId(n),i.setSessionStartTime(f(Date.now())),this.logger.log("[session-manager] session ID set successfully:",n)}catch(e){this.logger.log("[session-manager] failed to set session ID:",e)}else this.logger.log("[session-manager] cannot set session ID: SDK not initialized")}startReplayRecording(){this.initialized?this.instrumentationManager.startReplayRecording():this.logger.log("[session-manager] cannot start replay recording: SDK not initialized")}stopReplayRecording(){this.initialized?this.instrumentationManager.stopReplayRecording():this.logger.log("[session-manager] cannot stop replay recording: SDK not initialized")}destroy(){this.initialized&&(this.instrumentationManager.uninstrument(),z("gcId"),z("gcStartTime"),this.initialized=!1)}},re=new class{constructor(e){this.manager=e}emit(e,t,n){this.manager&&this.manager.emitLog(t,e,n)}log(e,t){this.emit("log",e,t)}info(e,t){this.emit("info",e,t)}warn(e,t){this.emit("warn",e,t)}error(e,t){this.emit("error",e,t)}debug(e,t){this.emit("debug",e,t)}trace(e,t){this.emit("trace",e,t)}};var ae={init:function(e){try{W=new se({cluster:e?.cluster||"",environment:e?.environment||"",namespace:e?.namespace,releaseId:e?.releaseId,dsn:e?.dsn||"",appId:e?.appId||"",userIdentifier:e?.userIdentifier,apiKey:e?.apiKey||"",options:e?.options,sessionId:e?.sessionId}),re.manager=W}catch(e){u(e)}},identifyUser:function(e){W?W.identifyUser(e):console.warn("[groundcover] identifyUser: groundcover is not initialized. please call init() first")},sendCustomEvent:function(e){W&&W.sendCustomEvent(e)},captureException:function(e,t){W&&W.captureException(e,t)},logger:re,updateConfig:function(e){W&&W.updateConfig(e)},startNavigation:function(e){W?W.startNavigation(e):console.warn("[groundcover] startNavigation: groundcover is not initialized. please call init() first")},endNavigation:function(e){W?W.endNavigation(e):console.warn("[groundcover] endNavigation: groundcover is not initialized. please call init() first")},getSessionId:function(){return W?W.getSessionId():(console.warn("[groundcover] getSessionId: groundcover is not initialized. please call init() first"),"")},setSessionId:async function(e){if(W)try{await W.setSessionId(e)}catch(e){console.error("[groundcover] setSessionId: failed to set session ID:",e)}else console.warn("[groundcover] setSessionId: groundcover is not initialized. please call init() first")},startReplayRecording:function(){W?W.startReplayRecording():console.warn("[groundcover] startReplayRecording: groundcover is not initialized. please call init() first")},stopReplayRecording:function(){W?W.stopReplayRecording():console.warn("[groundcover] stopReplayRecording: groundcover is not initialized. please call init() first")}};if("undefined"!=typeof window&&!window.groundcover)try{window.groundcover=ae}catch(e){console.warn("[groundcover] Failed to expose groundcover on window:",e)}var oe=ae;export{oe as default};
1
+ import{strToU8 as e,gzipSync as t}from"fflate";import i from"error-stack-parser";import{onCLS as n,onLCP as s,onFCP as r,onTTFB as a,onINP as o}from"web-vitals";import{record as l}from"rrweb";import{pack as c}from"@rrweb/packer";var d=console.log.bind(console),h=class e{constructor(){this.isDebugEnabled=!1,this.prefix=""}static initialize(t){return e.instance||(e.instance=new e),t&&(e.instance.isDebugEnabled=t.debug??!1,e.instance.prefix=t.prefix??""),e.instance}static getInstance(t){return e?.instance||e.initialize(t)}formatMessage(e){return`[${(new Date).toISOString()}] ${this.prefix} ${e}`}log(e,...t){this.isDebugEnabled&&d(this.formatMessage(e),...t)}updateConfig(e){this.isDebugEnabled=e.debug??this.isDebugEnabled,this.prefix=e.prefix??this.prefix}},g=h.getInstance();function u(e){g.log("[error-handler.handleError] called",e,{groundcoverIgnore:!0})}var p="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:{},m=1e6;function f(e){return e*m}function v(e){return e/m}var y=class{constructor(){this.generateTraceId=function(){for(let e=0;e<16;e++)b[e]=Math.floor(16*Math.random())+48,b[e]>=58&&(b[e]+=39);return S+String.fromCharCode.apply(null,b.slice(0,16))},this.generateSpanId=E(8),this.generateId=E(16)}},b=Array(32);function E(e){return function(){for(let t=0;t<2*e;t++)b[t]=Math.floor(16*Math.random())+48,b[t]>=58&&(b[t]+=39);return String.fromCharCode.apply(null,b.slice(0,2*e))}}var S="0000000000000000";var I=144e5,T=288e5,w=18e5,R={batchSize:10,batchTimeout:1e4,eventSampleRate:1,sessionSampleRate:1,environment:"development",debug:!1,enableCompression:!0,maskFields:[],enableMasking:!1,enabledEvents:[],tracePropagationUrls:[],tracePropagationHeaders:[],tracePropagationTraceIdHeaderName:"",tracePropagationSpanIdHeaderName:"",traceOrigin:{name:"",value:""},sessionMaxDuration:I},L=class e{constructor(t){this.dsn=t?.dsn||"",this.appId=t?.appId||"",this.cluster=t?.cluster||"",this.apiKey=t?.apiKey||"",this.environment=t?.environment||"",this.namespace=t?.namespace||"",this.releaseId=t?.releaseId,this.userIdentifier=t?.userIdentifier||null,this.options={...R,...t?.options||{}},this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t?.options?.sessionMaxDuration)}static normalizeSessionMaxDuration(e){return void 0===e?I:"number"!=typeof e||!Number.isFinite(e)||e<6e4||e>T?(console.warn("[groundcover] sessionMaxDuration must be a finite number between 60000 ms (1 minute) and 28800000 ms (8 hours); falling back to the 4-hour default"),I):e}getEndpoint(){return this.dsn?.startsWith("http")?this.dsn:`https://${this.dsn}`}updateConfig(t){t.options&&(this.options={...this.options,...t.options},void 0!==t.options.sessionMaxDuration&&(this.options.sessionMaxDuration=e.normalizeSessionMaxDuration(t.options.sessionMaxDuration))),t.userIdentifier&&(this.userIdentifier={...this.userIdentifier,...t.userIdentifier}),t.appId&&(this.appId=t.appId),t.dsn&&(this.dsn=t.dsn),t.apiKey&&(this.apiKey=t.apiKey),t.cluster&&(this.cluster=t.cluster),t.environment&&(this.environment=t.environment),t.namespace&&(this.namespace=t.namespace),t.releaseId&&(this.releaseId=t.releaseId)}};function N(e){try{return globalThis?.sessionStorage?.getItem(e)??null}catch(e){return u(e),null}}function H(e,t){try{globalThis?.sessionStorage?.setItem(e,t)}catch(e){u(e)}}function z(e){try{globalThis?.sessionStorage?.removeItem(e)}catch(e){u(e)}}function C(e,t=0){const i=N(e);if(null===i)return t;const n=Number(i);return Number.isFinite(n)?n:t}var M=class e{constructor(){this.config=null,this.logger=h.getInstance(),this.sessionId=null,this.sessionStartTime=null,this.lastActivityMs=null,this.lastPersistedActivityMs=0,this.flushActivityOnHide=()=>{try{if("undefined"!=typeof document&&"hidden"!==document.visibilityState)return;this.persistLastActivity()}catch(e){u(e)}},this.activityPersistHooked=!1}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(e){if(!this.activityPersistHooked&&"undefined"!=typeof document){this.activityPersistHooked=!0;try{document.addEventListener("visibilitychange",this.flushActivityOnHide)}catch(e){u(e)}}this.config||(this.config=new L(e))}getConfig(){return this.config?this.config:(this.logger.log("[config-manager] configuration not initialized"),null)}getSessionId(){if(this.sessionId)return this.sessionId;return N("gcId")||""}setSessionId(e){this.sessionId=e,H("gcId",e)}getSessionStartTime(){return this.sessionStartTime?this.sessionStartTime:C("gcStartTime",0)}setSessionStartTime(e){this.sessionStartTime=e,H("gcStartTime",String(e)),this.setLastActivityMs(Date.now())}getLastActivityMs(){return null!==this.lastActivityMs?this.lastActivityMs:C("gcLastActivity",0)}setLastActivityMs(e){this.lastActivityMs=e,e-this.lastPersistedActivityMs>=6e4&&this.persistLastActivity()}persistLastActivity(){null!==this.lastActivityMs&&(this.lastPersistedActivityMs=this.lastActivityMs,H("gcLastActivity",String(this.lastActivityMs)))}isSessionInactive(e=Date.now()){const t=this.getLastActivityMs();return t>0&&e-t>=w}clearSessionState(){if(this.sessionId=null,this.sessionStartTime=null,this.lastActivityMs=null,this.lastPersistedActivityMs=0,this.activityPersistHooked&&"undefined"!=typeof document){this.activityPersistHooked=!1;try{document.removeEventListener("visibilitychange",this.flushActivityOnHide)}catch(e){u(e)}}z("gcId"),z("gcStartTime"),z("gcLastActivity")}getSessionMaxDuration(){return this.config?.options?.sessionMaxDuration??I}getSessionElapsedMs(e=Date.now()){const t=this.getSessionStartTime();return t?e-v(t):0}isSessionExpired(e=Date.now()){return this.getSessionElapsedMs(e)>=this.getSessionMaxDuration()}updateConfig(e){this.logger.log("[config-manager] updateConfig called"),this.config?(e&&(this.logger.log("[config-manager] updating options"),this.config.updateConfig(e)),e?.userIdentifier&&(this.logger.log("[config-manager] updating user identifier"),this.config.userIdentifier={...this.config.userIdentifier,...e.userIdentifier})):this.logger.log("[config-manager] configuration not initialized")}},q=M.getInstance(),x=new y,_=h.getInstance();function k(){const e=q?.getConfig()?.options?.eventSampleRate;return!(void 0!==e&&!Number.isNaN(e))||Math.random()<=e}function D(e,t="",i=new WeakSet,n=0){try{return n>=10?{[t||"depth_limit"]:"[Depth Limit]"}:null!=(s=e)&&"number"==typeof s.nodeType&&"string"==typeof s.nodeName?{[t||"dom_node"]:`[${e?.constructor?.name||"DomNode"}]`}:i.has(e)?{[t||"circular_reference"]:"[Circular]"}:(i.add(e),Object.entries(e).reduce(((e,[s,r])=>{const a=t?`${t}.${s}`:s;return"object"==typeof r&&null!==r?Array.isArray(r)?e[a]=r.map(((e,t)=>"object"==typeof e&&null!==e?D(e,`${a}[${t}]`,i,n+1):e)):Object.assign(e,D(r,a,i,n+1)):e[a]=r,e}),{}))}catch(e){return _.log("[events-processors.formatEventObject] Error formatting event object",e),{[t||"format_error"]:"[Error formatting object]"}}finally{i.delete(e)}var s}function P(e){const t=f(Date.now()),i=x.generateId(),n=e.spanId||x.generateSpanId(),s=e.traceId||x.generateTraceId(),r=e.parentSpanId||"",a=D(e.attributes||{}),o=function(){const e=p?.location;return e?{path:e.hash&&!e.hash.match(/^#[a-z0-9-]+$/i)&&e.hash.startsWith("#/")?e.hash:e.pathname,url:e.href,title:p?.document?.title||""}:{path:"",url:"",title:p?.document?.title||""}}(),l={type:e.type,id:i,spanId:n,parentSpanId:r,traceId:s,attributes:{...a,location:o}};return e.span_name&&(l.span_name=e.span_name),"network"===e.type?(l.start_timestamp=e.timestamp||t,l.end_timestamp=e?.end_timestamp):l.timestamp=t,l}function A({text:e,maxLength:t=1e4}){return e?e.length<=t||"string"!=typeof e?e:e.substring(0,t):""}var O=class{constructor(){this.logger=h.getInstance(),this.config=M.getInstance()}compress(i){const n=this.config.getConfig();if(!n?.options?.enableCompression)return{data:(new TextEncoder).encode(i),isCompressed:!1};try{const n=e(i);return{data:t(n),isCompressed:!0}}catch(e){return{data:(new TextEncoder).encode(i),isCompressed:!1}}}async send(e){const t=this.config.getConfig();if(t)try{t?.options?.debug&&this.logger.log("Sending batch:",e,{groundcoverIgnore:!0});const i=t.apiKey,n=this.buildEndpoint();if(!i)return void this.logger.log("No API key found");const s=JSON.stringify(e),{data:r,isCompressed:a}=this.compress(s);fetch(n,{method:"POST",headers:{"Content-Type":"application/json",apikey:i,"Content-Encoding":a?"gzip":""},body:r}).catch((e=>{this.logger.log("Network error while sending batch:",e,{groundcoverIgnore:!0})}))}catch(e){t.options.debug&&this.logger.log("Failed to send batch:",e,{groundcoverIgnore:!0})}}buildEndpoint(){const e=this.config.getConfig();if(!e)return"";const{dsn:t}=e;return`${t}/json/rum`}},j=["navigation","dom.event"],F=class e{constructor(){this.events=[],this.timeoutId=null,this.config=M.getInstance(),this.logger=h.getInstance(),this.initialized=!1,this.noClusterWarned=!1,this.onBeforeEnqueue=null,this.visibilityHandler=()=>{"hidden"===document.visibilityState&&this.flush()},this.transporter=new O}static getInstance(){return e.instance||(e.instance=new e),e.instance}initialize(){try{if(this.initialized)return;this.scheduleFlush(),"undefined"!=typeof document&&document.addEventListener("visibilitychange",this.visibilityHandler),this.initialized=!0}catch(e){u(e)}}destroy(){try{null!==this.timeoutId&&(p.clearTimeout(this.timeoutId),this.timeoutId=null),"undefined"!=typeof document&&document.removeEventListener("visibilitychange",this.visibilityHandler),this.events=[],this.initialized=!1}catch(e){u(e)}}setBeforeEnqueueHook(e){this.onBeforeEnqueue=e}addEvent(e){if(e&&this.initialized)try{const t=this.config.getConfig()?.options?.beforeSend,i=this.config.getConfig()?.options?.enrichEvent;if(t&&!t(e))return;if(this.onBeforeEnqueue)try{this.onBeforeEnqueue(e)}catch(e){u(e)}i&&(e=i(e)),this.events.push(e);const n=this.config.getConfig()?.options?.batchSize||100;this.events.length>=n&&!j.includes(e.type)&&this.flush()}catch(e){u(e)}}flush(){try{if(!this.initialized)return;if(!this.events?.length)return void this.scheduleFlush();if(!this.config.getConfig()?.cluster)return this.noClusterWarned||(this.noClusterWarned=!0,this.logger.log("[events-pool.flush] skipping flush: no cluster")),void this.scheduleFlush();this.noClusterWarned=!1,this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[],this.scheduleFlush()}catch(e){u(e)}}async flushSync(){try{if(!this.events?.length||!this.initialized)return;if(!this.config.getConfig()?.cluster)return void this.logger.log("[events-pool.flush] skipping flush: no cluster");await this.transporter.send({sessionAttributes:this.getSessionAttributes(),events:this.events}),this.events=[]}catch(e){u(e)}}scheduleFlush(){try{null!==this.timeoutId&&(p.clearTimeout(this.timeoutId),this.timeoutId=null);const e=this.config.getConfig()?.options?.batchTimeout;if(!e)return;this.timeoutId=p.setTimeout((()=>{this.timeoutId=null,this.flush()}),e)}catch(e){u(e)}}detectBrowser(){const e=navigator.userAgent;let t="unknown",i="unknown";const n=navigator.platform||"unknown",s=/Mobile|Android|iPhone|iPad|iPod/i.test(e);return/Edg/.test(e)?(t="Edge",i=e.match(/Edg\/([\d.]+)/)?.[1]||i):/Chrome/.test(e)&&!/Chromium/.test(e)?(t="Chrome",i=e.match(/Chrome\/([\d.]+)/)?.[1]||i):/Firefox/.test(e)?(t="Firefox",i=e.match(/Firefox\/([\d.]+)/)?.[1]||i):/Safari/.test(e)&&!/Chrome/.test(e)?(t="Safari",i=e.match(/Version\/([\d.]+)/)?.[1]||i):/Trident/.test(e)?(t="Internet Explorer",i=/rv:([^)]+)\)/i.test(e)?e.match(/rv:([^)]+)\)/i)?.[1]||i:e.match(/MSIE ([^;]+)/)?.[1]||i):/OPR/.test(e)&&(t="Opera",i=e.match(/OPR\/([\d.]+)/)?.[1]||i),{name:t,version:i,platform:n,language:navigator.language,mobile:s}}getSessionAttributes(){const e=this.detectBrowser(),t=this.config.getConfig();return{cluster:t?.cluster||"",env:t?.environment||"",namespace:t?.namespace||"",releaseId:t?.releaseId||"",session_id:this.config.getSessionId(),session_start_time:this.config.getSessionStartTime(),user:t?.userIdentifier||{},"service.name":t?.appId,userAgent:navigator.userAgent,browser:e}}};F.instance=null;var U=F,B=class{constructor(){this.logger=h.getInstance(),this.eventsPool=U.getInstance()}},$="undefined"!=typeof Element?Object.getOwnPropertyDescriptor(Element.prototype,"tagName")?.get:void 0;function K(e,t){try{return e.getAttribute(t)||""}catch{return""}}function X(e){try{const t=$?.call(e);return"string"==typeof t?t:""}catch{return""}}var V=class extends B{constructor(){super(...arguments),this.eventHandlers=[],this.capturedEvents=["click","change","keydown","select","submit"],this.config=M.getInstance(),this.getShouldMaskText=e=>{const t=this.config.getConfig(),i=t?.options?.maskFields||[],n=t?.options?.enableMasking||!1;return(["password"===e.target?.type,e.target?.id?.toLowerCase().includes("password"),e.target?.id?.toLowerCase().includes("credit-card"),e.target?.id?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("cc"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.className?.toLowerCase().includes("credit-card"),e.target?.hasAttribute("data-private")].some((e=>e))||i.some((t=>e.target?.id?.toLowerCase().includes(t))))&&n},this.getKeyCode=e=>{if(e instanceof KeyboardEvent&&e.code&&"Dead"!==e.key){const t=this.getShouldMaskText({target:e.target});return t||t?"*":e.key}return""},this.getText=e=>{const t=e.target,i=this.config.getConfig();return i?.options?.enableMasking||!1?"*":A({text:t.innerText||""})},this.getSelector=e=>e.target instanceof Element&&this.generateSelector(e.target)||"",this.getCoordinates=e=>{if({mouseup:!0,mousedown:!0,mousemove:!0,mouseover:!0}[e.type]){const{clientX:t,clientY:i}=e||{};return{clientX:t,clientY:i}}return null}}initialize(){try{this.logger.log("[dom-events-listener.initialize] called"),this.capturedEvents?.forEach((e=>{const t=e=>{try{this.handleEvent(e)}catch(e){u(e)}};this.eventHandlers.push({type:e,handler:t}),p?.addEventListener(e,t)}))}catch(e){u(e)}}destroy(){this.eventHandlers.forEach((({type:e,handler:t})=>{global?.removeEventListener?.(e,t)})),this.eventHandlers=[]}handleEvent(e){try{if(!k())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}}buildEvent(e){const t=this.getSelector(e),i=this.getCoordinates(e),n=e.target,s=this.getKeyCode(e),r=this.getText(e);this.logger.log("[dom-events-listener.buildEvent] called");return P({type:"dom.event",attributes:{dom_event_selector:t,dom_event_key_code:s,dom_event_type:e.type,dom_event_coordinates:i||{clientX:0,clientY:0},dom_event_target:{id:K(n,"id"),tagName:X(n),className:K(n,"class"),text:r}}})}queueEvent(e){try{this.logger.log("[dom-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}generateSelector(e,t={}){if(!e||!e.nodeType||e.nodeType!==Node.ELEMENT_NODE)return"";const i=t.root||document.body||document.documentElement,n=t.maxAttempts||10,s=t.priorityAttributes||["id","class","name","aria-label","type","title","alt"];if("html"===X(e).toLowerCase())return"html";let r="",a=0,o=e;const l=[];for(;o&&o!==i&&o!==document.documentElement&&a<n;){let e=X(o).toLowerCase();try{const t=o.getAttribute("id");if(t&&/^[a-zA-Z][\w-]*$/.test(t))return`#${t}`;if(o.classList?.length){const t=Array.from(o.classList).filter((e=>e&&"string"==typeof e)).map((e=>`.${e}`));t.length&&(e+=t.join(""))}for(const t of s)if("id"!==t&&"class"!==t)try{const i=o.getAttribute(t);i&&(e+=`[${t}="${i.replace(/"/g,'\\"')}"]`)}catch(e){}}catch(t){e=X(o).toLowerCase()||"*"}l.unshift(e||"*"),r=l.join(" > ");try{if(1===i.querySelectorAll(r).length)return r}catch(e){l.shift(),r=l.join(" > ")}try{o=o.parentElement}catch(e){break}a++}return r||"*"}},W=["log","info","warn","error","debug","assert","trace"],G=class extends B{constructor(){super(...arguments),this.originalConsole=null,this.isInitialized=!1}initialize(){if(!this.isInitialized)try{this.originalConsole=this.captureConsoleMethods(),W.forEach((e=>{e in console&&(console[e]=(...t)=>{try{this.handleEvent(t,e)}catch(e){u(e)}finally{this.originalConsole?.[e]&&this.originalConsole[e](...t)}})})),this.isInitialized=!0}catch(e){u(e)}}destroy(){try{if(!this.isInitialized)return;W.forEach((e=>{this.originalConsole&&this.originalConsole[e]&&(console[e]=this.originalConsole[e])})),this.isInitialized=!1}catch(e){u(e)}}handleEvent(e,t){try{if(!k())return;const i=Array.isArray(e)?e:[e],n=this.formatMessage(i,t);if(!n||n?.includes("groundcoverIgnore"))return;const s=this.extractAttributes(i),r=this.buildEvent({message:n,level:t,attributes:s});this.queueEvent(r)}catch(e){u(e)}}buildEvent({message:e,level:t,attributes:i}){return P({type:"log",attributes:{...i,message:A({text:e}),level:t}})}queueEvent(e){try{this.logger.log("[logs-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}extractAttributes(e){if(Array.isArray(e)&&0!==e.length)try{const t={};for(const i of e)null!==i&&"object"==typeof i&&"[object Object]"===Object.prototype.toString.call(i)&&Object.assign(t,i);return delete t.message,delete t.level,delete t.location,t}catch(e){return void u(e)}}formatMessage(e,t){if(!Array.isArray(e))return String(e);try{return e.map((e=>{if(void 0===e)return"undefined";if(null===e)return"null";if("object"==typeof e){if(e instanceof Error)try{return"error"===t&&e.stack||e.toString()}catch{try{const t={name:e.name,message:e.message,stack:e.stack};return JSON.stringify(t)}catch{return Object.prototype.toString.call(e)}}try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return String(e)})).join(" ")}catch(e){return u(e),"[Error formatting console message]"}}captureConsoleMethods(){const e={};try{W.forEach((t=>{console&&"function"==typeof console[t]&&(e[t]=console[t].bind(console))}))}catch(e){u(e)}return e}};function J(e){try{if(null==e)return"";if("string"==typeof e)return e;if(e instanceof FormData)return"[form data]";if(e instanceof Blob)return"[blob data]";if(e instanceof ArrayBuffer)return"[arraybuffer data]";if("undefined"!=typeof Document){if(e instanceof Document)return"[document data]";const t=e;if(9===t?.nodeType)return"[document data]"}if(e instanceof URLSearchParams)try{return e.toString()}catch{return"[unreadable body]"}return Array.isArray(e)||"object"==typeof e?JSON.stringify(e):String(e)}catch(e){return u(`Failed to format body: ${e}`),"[unreadable body]"}}var Y,Z=class extends B{constructor(){super(...arguments),this.maxBodyLength=5e3,this.config=M.getInstance(),this.idGenerator=new y,this.handleEvent=e=>{try{if(this.shouldIgnoreRequest(e.url))return;if(!k())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}},this.buildEvent=e=>{let t=e.url,i=e.url;this.logger.log("[network-events-listener.buildEvent] called",{event:e});try{t=new URL(e.url).pathname}catch(t){i=new URL(e.url,globalThis.location.href).href}this.logger.log("[network-events-listener.buildEvent] fullUrl",{fullUrl:i});const n=this.formatHeaders(e.request.headers),s=this.formatHeaders(e.response.headers),r=e.method?.toUpperCase()||"UNKNOWN",{traceId:a,spanId:o}=this.extractTraceIds(e.request.headers),l=A({text:e.request.body,maxLength:this.maxBodyLength}),c=A({text:e.response.body,maxLength:this.maxBodyLength}),d={type:"HTTP",operation:{name:r},resource_name:t,status:e.status?.toString(),subType:r,url:{full:i},http:{route:t,path:t,method:e.method,status:e.status?.toString(),request:{headers:n,method:e.method},response:{headers:s,status_code:e.status?.toString()}},error:e?.error?.type?{type:e.error?.type||"Unknown error"}:void 0,gc:{request:{body:l},response:{body:c}}},h={type:"network",timestamp:e?.timestamp&&!Number.isNaN(e.timestamp)?f(e.timestamp):void 0,end_timestamp:e?.end_time&&!Number.isNaN(e.end_time)?f(e.end_time):void 0,span_name:`${e.method} ${t}`,attributes:d};return a&&(h.traceId=a),o&&(h.spanId=o),P(h)}}async getResponseBody(e){let t;try{const i=e.clone().body;if(i){let e,n=i.getReader(),s=new TextDecoder,r="";for(;!(e=await n.read()).done;){let t=e.value;r+=s.decode(t)}t=r}else t=""}catch(e){t=`Unable to clone response: ${e}`}return t}initialize(){this.logger.log("[network-events-listener.initialize] called"),this.patchXHR(),this.patchFetch()}destroy(){globalThis.XMLHttpRequest.prototype.open=globalThis.XMLHttpRequest.prototype.open,globalThis.XMLHttpRequest.prototype.send=globalThis.XMLHttpRequest.prototype.send,globalThis.fetch=globalThis.fetch}shouldIgnoreRequest(e){try{const t=this.config.getConfig()?.getEndpoint();if(t&&e.includes(t))return!0;if([".tsx",".jsx",".css"].some((t=>e.toLowerCase().endsWith(t))))return!0;return(this.config.getConfig()?.options?.excludedUrls||[]).some((t=>{if(t instanceof RegExp)return t.test(e);if("string"==typeof t&&(t.includes("*")||t.includes("?"))){const i=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*").replace(/\?/g,".");return new RegExp(i).test(e)}return e===t}))}catch(e){return u(e),!1}}queueEvent(e){try{this.logger.log("[network-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}patchXHR(){const e=this,t=globalThis.XMLHttpRequest.prototype.open,i=globalThis.XMLHttpRequest.prototype.send,n=globalThis.XMLHttpRequest.prototype.setRequestHeader;let s;globalThis.XMLHttpRequest.prototype.open=function(i,n,r=!0,a,o){if(s=Date.now(),e.shouldIgnoreRequest(n.toString()))return t.apply(this,[i,n,r,a,o]);const l=new URL(n.toString(),globalThis.location.href).href;return this._requestMethod=i,this._requestUrl=l,this._requestHeaders={},t.apply(this,[i,n,r,a,o])},globalThis.XMLHttpRequest.prototype.setRequestHeader=function(e,t){return this._requestHeaders&&(this._requestHeaders[e]=t),n.apply(this,[e,t])},globalThis.XMLHttpRequest.prototype.send=function(...t){const n=this._requestUrl?.toString();if(!n||e.shouldIgnoreRequest(n))return i.apply(this,t);if(e.shouldAddTraceHeader(n)){const t=e.config.getConfig(),i=e.getTraceIds();t?.options?.traceOrigin?.name&&e.addHeaderToXhrRequest({request:this,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),i&&(t?.options?.tracePropagationTraceIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:i.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&e.addHeaderToXhrRequest({request:this,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:i.decimalSpanId}),Boolean(t?.options?.tracePropagationHeaders)&&t?.options?.tracePropagationHeaders?.forEach((t=>{e.addHeaderToXhrRequest({request:this,headerName:t,headerValue:i.traceparent})})),t?.options?.tracePropagationHeaders?.length||e.addHeaderToXhrRequest({request:this,headerName:"traceparent",headerValue:i.traceparent}))}return this._requestBody=t[0]||"",this.addEventListener("load",(()=>{const t=Date.now(),i={};this.getAllResponseHeaders().split(/\r?\n/).forEach((e=>{const[t,n]=e.split(": ");t&&n&&(i[t.trim()]=n.trim())}));const n=J(this._requestBody),r=J(""===this.responseType||"text"===this.responseType?this.responseText:this.response),a={timestamp:s,end_time:t,method:this._requestMethod,url:this._requestUrl,body:n,status:this.status,request:{headers:this._requestHeaders||{},body:n},response:{headers:i,body:r}};e.handleEvent(a)})),i.apply(this,t)}}normalizeHeaders(e,t){try{const i=e?new Headers(e):new Headers;return t?(t instanceof Headers?t.forEach(((e,t)=>i.set(t,e))):Array.isArray(t)?new Headers(t).forEach(((e,t)=>i.set(t,e))):"object"==typeof t&&Object.entries(t).forEach((([e,t])=>i.set(e,t))),i):i}catch{return new Headers}}addTraceHeaders(e){const t=this.config.getConfig(),i=this.getTraceIds();if(t?.options?.traceOrigin?.name&&this.addHeaderToFetchRequest({init:e,headerName:t.options.traceOrigin.name,headerValue:t.options.traceOrigin.value}),!i)return;t?.options?.tracePropagationTraceIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationTraceIdHeaderName,headerValue:i.decimalTraceId}),t?.options?.tracePropagationSpanIdHeaderName&&this.addHeaderToFetchRequest({init:e,headerName:t.options.tracePropagationSpanIdHeaderName,headerValue:i.decimalSpanId});const n=t?.options?.tracePropagationHeaders;(n?.length?n:["traceparent"]).forEach((t=>{this.addHeaderToFetchRequest({init:e,headerName:t,headerValue:i.traceparent})}))}extractHeadersRecord(e){const t={};return e instanceof Headers&&e.forEach(((e,i)=>{t[i]=e})),t}patchFetch(){const e=globalThis.fetch;globalThis.fetch=(...t)=>{const i=Date.now();let[n,s]=t;s={...s||{}},t[1]=s;const r=n instanceof Request,a=s?.method||"GET",o=s?.body||"",l=n instanceof Request?n.url:n.toString();if(this.shouldIgnoreRequest(l))return e.apply(globalThis,t);const c=this.shouldAddTraceHeader(l);s||(t[1]={},s=t[1]),s.headers=this.normalizeHeaders(r?n.headers:void 0,s.headers),c&&this.addTraceHeaders(s);const d=this.extractHeadersRecord(s.headers),h=J(o);return e.apply(globalThis,t).then((e=>{const t=Date.now(),n=e.clone(),s={};n.headers.forEach(((e,t)=>{s[t]=e}));const r={method:a,url:l,timestamp:i,body:h,status:n.status,end_time:t,request:{headers:d,body:h},response:{headers:s,body:""}};try{s["content-type"]?.includes("text/event-stream")?(r.response.body="[unreadable response body]",this.handleEvent(r)):this.getResponseBody(n).then((e=>{r.response.body=e,this.handleEvent(r)})).catch((e=>{r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}))}catch(e){r.response.body="[unreadable response body]",this.handleEvent(r),this.logger.log("[network-events-listener.patchFetch] error",{error:e})}return e})).catch((e=>{const t=Date.now();let i="NetworkError";const n=e.message||"Unknown network error";e instanceof TypeError&&n.includes("Failed to fetch")?i="ERR_NETWORK_FAILURE":n.includes("Name not resolved")?i="ERR_NAME_NOT_RESOLVED":n.includes("Connection refused")&&(i="ERR_CONNECTION_REFUSED");const s={method:a,url:l,body:h,status:0,end_time:t,request:{headers:d,body:h},response:{headers:{},body:""},error:{type:i}};throw this.handleEvent(s),e}))}}formatHeaders(e){const t=["authorization","cookie","set-cookie"];return Object.entries(e)?.reduce(((e,[i,n])=>{const s=i.toLowerCase();return t.includes(s)||/(token|key|secret|password)/i.test(s)?e[i]="[REDACTED]":e[i]=A({text:n}),e}),{})}extractTraceIds(e){const t=this.config.getConfig();let i="",n="";if(t?.options?.tracePropagationTraceIdHeaderName){const n=e[t.options.tracePropagationTraceIdHeaderName];n&&"string"==typeof n&&(i=n)}if(t?.options?.tracePropagationSpanIdHeaderName){const i=e[t.options.tracePropagationSpanIdHeaderName];i&&"string"==typeof i&&(n=i)}if(!i||!n){const t=this.extractTraceparentHeader(e);if(t){const e=t.split("-");4===e.length&&"00"===e[0]&&(!i&&e[1]&&32===e[1].length&&(i=e[1]),!n&&e[2]&&16===e[2].length&&(n=e[2]))}}return{traceId:i,spanId:n}}extractTraceparentHeader(e){const t=this.config.getConfig();if(t?.options?.tracePropagationHeaders?.length)for(const i of t.options.tracePropagationHeaders){const t=e[i];if(t&&"string"==typeof t)return t}const i=e.traceparent;return i&&"string"==typeof i?i:""}getTraceIds(){const e=this.idGenerator.generateSpanId(),t=this.idGenerator.generateTraceId();let i,n;try{i=BigInt("0x"+t).toString(10),n=BigInt("0x"+e).toString(10)}catch(s){this.logger.log("[network-events-listener.getTraceIds] error",{error:s}),i=t,n=e}return{traceparent:`00-${t}-${e}-01`,traceId:t,spanId:e,decimalTraceId:i,decimalSpanId:n}}addHeaderToXhrRequest({request:e,headerName:t,headerValue:i}){if(this.logger.log("[network-events-listener.addHeaderToXhrRequest] called",{request:e,headerName:t,headerValue:i}),this.isValidHeaderName(t))try{e.setRequestHeader(t,i),e._requestHeaders[t]=i}catch(e){this.logger.log("[network-events-listener.addHeaderToXhrRequest] Error setting header",{error:e,headerName:t,headerValue:i})}else this.logger.log("[network-events-listener.addHeaderToXhrRequest] Invalid header name",{headerName:t})}addHeaderToFetchRequest({init:e,headerName:t,headerValue:i}){if(this.logger.log("[network-events-listener.addHeaderToFetchRequest] called",{init:e,headerName:t,headerValue:i}),this.isValidHeaderName(t))try{e.headers instanceof Headers?e.headers.set(t,i):e.headers={...e.headers,[t]:i}}catch(e){this.logger.log("[network-events-listener.addHeaderToFetchRequest] Error setting header",{error:e,headerName:t,headerValue:i})}else this.logger.log("[network-events-listener.addHeaderToFetchRequest] Invalid header name",{headerName:t})}isValidHeaderName(e){if(!e||"string"!=typeof e)return!1;return/^[a-zA-Z0-9!#$&'*+\-.^_`|~]+$/.test(e)}shouldAddTraceHeader(e){const t=this.config.getConfig();if(this.logger.log("[network-events-listener.shouldAddTraceHeader] called",{url:e,config:t}),!t?.options?.tracePropagationUrls?.length)return!1;const i=t.options.tracePropagationUrls?.some((t=>{const i=t.replace(/\*/g,".*"),n=new RegExp(`^${i}$`),s=e.startsWith("/")?new URL(e,globalThis.location.href).pathname:e;return n.test(s)}));return this.logger.log("[network-events-listener.shouldAddTraceHeader] result",{url:e,result:i}),i}},Q=class extends B{constructor(){super(...arguments),this.handleEvent=(e,t)=>{this.logger.log("[errors-events-listener.handleEvent] called");try{let i;if(e instanceof Error)i=e,this.enhanceError(i);else if(e instanceof ErrorEvent){if(i=e.error||new Error(e.message||"Unknown error"),/Script error\.?/.test(i.message))return;this.enhanceError(i,e)}else{if(!(e instanceof PromiseRejectionEvent))return;i=this.createUnhandledRejectionError(e)}const n=this.buildEvent(i,t);this.queueEvent(n)}catch(e){u(e)}}}initialize(){p?.addEventListener("error",this.handleEvent),p?.addEventListener("unhandledrejection",this.handleEvent)}destroy(){p?.removeEventListener("error",this.handleEvent),p?.removeEventListener("unhandledrejection",this.handleEvent)}buildEvent(e,t){return P({type:"exception",attributes:{error_type:e.name||"Error",error_message:e.message||"Unknown error",error_stacktrace:this.buildStackTrace(e),error_fingerprint:`${e.name}:${A({text:e.message,maxLength:400})}`,error_handled:t?.handled||!1,error_metadata:t?.metadata}})}queueEvent(e){try{this.logger.log("[errors-events-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}enhanceError(e,t){const{filename:i,lineno:n,colno:s}=t||{};i&&!e.fileName&&Object.defineProperty(e,"fileName",{value:i}),n&&!e.lineNumber&&Object.defineProperty(e,"lineNumber",{value:n}),s&&!e.columnNumber&&Object.defineProperty(e,"columnNumber",{value:s})}createUnhandledRejectionError(e){let t;if(e.reason instanceof Error)t=e.reason;else{const i="object"==typeof e.reason?JSON.stringify(e.reason,null,2):String(e.reason);t=new Error(i),t.name="UnhandledRejection",Object.defineProperty(t,"originalReason",{value:e.reason,enumerable:!1})}return t}buildStackTrace(e){if(!e)return[];try{return i.parse(e).map((e=>({filename:e.fileName||"unknown",function:e.functionName||"anonymous",lineno:e.lineNumber||0,colno:e.columnNumber||0})))}catch(e){return[]}}},ee=class extends B{constructor({isEnabled:e}){if(super(),this.currentUrl="",this.originalPushState=null,this.originalReplaceState=null,this.isInitialized=!1,this.isAutomaticNavigationEnabled=!1,this.activeNavigation=null,this.popStateHandler=()=>{this.handleLocationChange()},this.hashChangeHandler=()=>{this.handleLocationChange()},this.isInBrowserEnvironment()&&e)try{this.isAutomaticNavigationEnabled=e,this.currentUrl=p.location.href,this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState}catch(e){u(e)}}isInBrowserEnvironment(){try{return void 0!==p&&void 0!==p.location&&void 0!==p.history&&"function"==typeof p.addEventListener}catch(e){return u(e),!1}}initialize(){try{if(this.logger.log("[navigation-listener.initialize] called"),!this.isInBrowserEnvironment()||!this.originalPushState||!this.originalReplaceState)return void this.logger.log("[navigation-listener.initialize] Browser environment not available, skipping initialization");if(!this.isAutomaticNavigationEnabled)return void this.logger.log("[navigation-listener.initialize] Automatic navigation is disabled, skipping initialization");const e=this.originalPushState,t=this.originalReplaceState;history.pushState=(...t)=>{const i=e.apply(history,t);return this.handleLocationChange(),i},history.replaceState=(...e)=>{const i=t.apply(history,e);return this.handleLocationChange(),i},p&&"function"==typeof p.addEventListener&&(p.addEventListener("popstate",this.popStateHandler),p.addEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!0}catch(e){u(e)}}destroy(){try{this.logger.log("[navigation-listener.destroy] called"),this.isInitialized&&this.originalPushState&&this.originalReplaceState&&(history.pushState=this.originalPushState,history.replaceState=this.originalReplaceState),p&&"function"==typeof p.removeEventListener&&(p.removeEventListener("popstate",this.popStateHandler),p.removeEventListener("hashchange",this.hashChangeHandler)),this.isInitialized=!1}catch(e){u(e)}}handleEvent(){try{this.logger.log("[navigation-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){u(e)}}buildEvent(){this.logger.log("[navigation-listener.buildEvent] called",p.location.href);return P({type:"navigation",attributes:{page_url:p.location.href,metadata:this.activeNavigation?.metadata,duration_ms:this.activeNavigation?.duration_ms||0,start_timestamp:this.activeNavigation?.startTime||0,end_timestamp:this.activeNavigation?.endTime||0}})}queueEvent(e){try{this.logger.log("[navigation-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}startNavigation(e){this.logger.log("[navigation-listener.startNavigation] called",e),this.activeNavigation={page_url:p.location.href,metadata:e,startTime:Date.now(),endTime:0,duration_ms:0}}endNavigation(e){this.logger.log("[navigation-listener.endNavigation] called",e),this.activeNavigation?(this.logger.log("[navigation-listener.endNavigation] active navigation",this.activeNavigation),this.activeNavigation.endTime=Date.now(),this.activeNavigation.duration_ms=this.activeNavigation.endTime-this.activeNavigation.startTime,this.handleEvent()):this.logger.log("[navigation-listener.endNavigation] no active navigation found")}handleLocationChange(){try{if(!this.isInitialized)return;if(this.logger.log("[navigation-listener.handleLocationChange] called"),this.currentUrl===p.location.href)return;let e,t;try{e=new URL(p.location.href),t=new URL(this.currentUrl)}catch(e){return void u(e)}const i=e=>e.startsWith("#/"),n=i(e.hash)||i(t.hash);(e.pathname!==t.pathname||n&&e.hash!==t.hash)&&(this.currentUrl=p.location.href,this.handleEvent())}catch(e){u(e)}}},te=class extends B{constructor(){super(),this.loadEventHandler=()=>{this.logger.log("load event"),this.handleEvent()}}initialize(){try{this.logger.log("[page-load-listener.initialize] called"),this.logger.log("globalThis",p),"complete"===p?.document?.readyState?(this.logger.log("Page already loaded, triggering event immediately"),this.handleEvent()):p?.addEventListener("load",this.loadEventHandler)}catch(e){this.logger.log("[page-load-listener.initialize] error",e),u(e)}}destroy(){p?.removeEventListener("load",this.loadEventHandler)}handleEvent(){try{this.logger.log("[page-load-listener.handleEvent] called");const e=this.buildEvent();this.queueEvent(e)}catch(e){u(e)}}buildEvent(){const e=performance.getEntriesByType("navigation")[0],t=e?e.loadEventEnd-e.startTime:0,i=performance.getEntriesByType("resource"),n={count:i.length,totalSize:0,totalDuration:0,byType:{}};i.forEach((e=>{const t=e,i=t.transferSize||0,s=t.duration,r=t.initiatorType;n.totalSize+=i,n.totalDuration+=s,n.byType[r]||(n.byType[r]={count:0,size:0,duration:0}),n.byType[r].count++,n.byType[r].size+=i,n.byType[r].duration+=s}));return P({type:"pageload",attributes:{page_url:p?.location?.href||"",page_load_time:t,page_referrer:p?.document?.referrer||"",page_resources:n}})}queueEvent(e){try{this.logger.log("[page-load-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}},ie=class extends B{constructor(){super(...arguments),this.handleEvent=e=>{try{if(!k())return;const t=this.buildEvent(e);this.queueEvent(t)}catch(e){u(e)}}}initialize(){n(this.handleEvent),s(this.handleEvent),r(this.handleEvent),a(this.handleEvent),o(this.handleEvent)}destroy(){}buildEvent(e){return P({type:"performance",attributes:{performance_metric_name:e.name,performance_metric_value:e.value,performance_metric_id:e.id,performance_metric_navigation_type:e.navigationType||""}})}queueEvent(e){try{this.logger.log("[performance-listener.queueEvent] called"),e&&this.eventsPool.addEvent(e)}catch(e){u(e)}}},ne=class{constructor(){this.logger=h.getInstance(),this.eventsPool=U.getInstance(),this.configManager=M.getInstance(),this.stopFn=null,this.isRecording=!1,this.events=[],this.startTime=0,this.batchSize=3,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.sendTimerId=null}initialize(){this.logger.log("[session-replay-listener] initialize called"),this.isRecording?this.logger.log("[session-replay-listener] already recording"):this.startRecording()}startRecording(){try{this.startTime=Date.now(),this.events=[],this.isRecording=!0,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0;const e=this.configManager.getConfig()?.options?.sessionReplay?.blockedSelectors?.filter(Boolean),t=e?.length?e.join(", "):void 0;this.stopFn=l({...t?{blockSelector:t}:{},slimDOMOptions:{script:!1,comment:!1,headWhitespace:!1},sampling:{mousemove:100,input:"last",scroll:150},checkoutEveryNms:3e4,emit:(e,t)=>{if(this.isRecording)try{this.events.push(e),t&&(this.batchContainsFullSnapshot=!0,this.fullSnapshotTimestamp=Date.now(),this.logger.log("[session-replay-listener] full snapshot captured at",this.fullSnapshotTimestamp)),this.events.length>=this.batchSize&&this.scheduleSendBatch()}catch(e){u(e),this.logger.log("[session-replay-listener] failed to process event in emit callback:",e)}}})||null,this.logger.log("[session-replay-listener] started recording with full snapshot interval:",3e4,"ms")}catch(e){u(e),this.logger.log("[session-replay-listener] failed to start recording:",e),this.isRecording=!1}}scheduleSendBatch(){null===this.sendTimerId&&(this.sendTimerId=p.setTimeout((()=>{if(this.sendTimerId=null,this.isRecording)try{this.sendBatch()}catch(e){u(e),this.logger.log("[session-replay-listener] failed to send deferred batch:",e)}}),0))}clearScheduledSend(){null!==this.sendTimerId&&(p.clearTimeout(this.sendTimerId),this.sendTimerId=null)}sendBatch(){if(0!==this.events.length)try{const e=this.events.map((e=>c(e))),t=this.batchContainsFullSnapshot,i=this.fullSnapshotTimestamp,n={_gc_replay_data:{events:e}};t&&(n.replay_is_full_snapshot=!0,n.replay_full_snapshot_timestamp=i);const s=P({type:"replay",attributes:n});this.eventsPool.addEvent(s),this.events=[],this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0,this.logger.log("[session-replay-listener] sent batch with",e.length,"events",t?"(includes full snapshot)":"")}catch(e){u(e),this.logger.log("[session-replay-listener] failed to send batch:",e)}}stopRecording(){if(this.isRecording)try{this.clearScheduledSend(),this.stopFn?.(),this.isRecording=!1,this.events.length>0&&this.sendBatch(),this.stopFn=null,this.batchContainsFullSnapshot=!1,this.fullSnapshotTimestamp=void 0}catch(e){u(e),this.logger.log("[session-replay-listener] failed to stop recording:",e)}}destroy(){this.stopRecording()}},se=["pointerdown","keydown","scroll","touchstart","mousemove"],re=class{constructor(e){this.logger=h.getInstance(),this.lastSignalMs=0,this.idleTimer=null,this.installed=!1,this.handleInput=()=>{const e=Date.now();if(!(e-this.lastSignalMs<3e4)){this.notePresence(e);try{this.callbacks.onPresence(e)}catch(e){u(e)}}},this.callbacks=e}initialize(){this.installed||(this.installed=!0,se.forEach((e=>{try{p.addEventListener?.(e,this.handleInput,{capture:!0,passive:!0})}catch(e){u(e)}})),this.armIdleTimer(),this.logger.log("[user-presence-monitor] installed"))}notePresence(e){this.lastSignalMs=e,this.armIdleTimer()}resetIdleTimer(){this.armIdleTimer()}armIdleTimer(){try{this.idleTimer&&clearTimeout(this.idleTimer),this.idleTimer=null,this.idleTimer=setTimeout((()=>{this.idleTimer=null,this.logger.log("[user-presence-monitor] user idle for",w,"ms");try{this.callbacks.onIdle()}catch(e){u(e)}}),w)}catch(e){u(e)}}destroy(){if(this.installed){this.installed=!1,se.forEach((e=>{try{p.removeEventListener?.(e,this.handleInput,{capture:!0})}catch(e){u(e)}}));try{this.idleTimer&&clearTimeout(this.idleTimer)}catch(e){u(e)}finally{this.idleTimer=null}}}},ae=new Set(["dom.event","navigation","pageload","custom"]),oe=class{constructor(){this.logger=h.getInstance(),this.eventsPool=U.getInstance(),this.configManager=M.getInstance(),this.idGenerator=new y,this.logEventsListener=null,this.domEventsListener=null,this.errorsEventsListener=null,this.networkEventsListener=null,this.navigationListener=null,this.performanceListener=null,this.pageLoadListener=null,this.sessionReplayListener=null,this.isRotating=!1,this.replayDesired=!1,this.presenceMonitor=null}instrument(){this.logger.log("[instrumentation-manager.instrument] called"),this.eventsPool.setBeforeEnqueueHook((e=>this.reconcileSessionOnEnqueue(e))),this.presenceMonitor?.destroy(),this.presenceMonitor=new re({onPresence:e=>this.reconcileSession(e,!0),onIdle:()=>this.pauseReplayOnIdle()}),this.presenceMonitor.initialize();const e=this.configManager.getConfig(),t=e?.options?.enabledEvents,i=!t||0===t.length,n=[];(i||t?.includes("pageload"))&&(this.pageLoadListener=new te,this.pageLoadListener.initialize(),n.push("pageload")),(i||t?.includes("dom"))&&(this.domEventsListener=new V,this.domEventsListener.initialize(),n.push("dom")),(i||t?.includes("logs"))&&(this.logEventsListener=new G,this.logEventsListener.initialize(),n.push("logs")),(i||t?.includes("exceptions"))&&(this.errorsEventsListener=new Q,this.errorsEventsListener.initialize(),n.push("exceptions")),(i||t?.includes("network"))&&(this.networkEventsListener=new Z,this.networkEventsListener.initialize(),n.push("network")),(i||t?.includes("performance"))&&(this.performanceListener=new ie,this.performanceListener.initialize(),n.push("performance")),this.navigationListener=new ee({isEnabled:i||t?.includes("navigation")}),this.navigationListener.initialize(),n.push("navigation"),this.logger.log("[instrumentation-manager.instrument] initialized listeners based on config:",n)}sendCustomEvent(e){this.logger.log("[instrumentation-manager.sendCustomEvent] called",e);try{const t=P({type:"custom",attributes:{custom_event_name:e?.event,custom_event_attributes:e?.attributes}});this.eventsPool.addEvent(t)}catch(e){u(e)}}emitLog(e,t,i){if(this.logEventsListener){this.logger.log("[instrumentation-manager.emitLog] called",{message:e,level:t,attributes:i});try{const n={...i||{}};delete n.message,delete n.level,delete n.location;const s=P({type:"log",attributes:{...n,message:A({text:e}),level:t}});this.eventsPool.addEvent(s)}catch(e){u(e)}}}captureException(e,t){try{this.logger.log("[instrumentation-manager.captureException] called",e),this.errorsEventsListener?.handleEvent(e,{handled:!0,metadata:t})}catch(e){u(e)}}isNavigationTrackingEnabled(){const e=this.configManager.getConfig();return e?.options?.enabledEvents?.includes("navigation")||0===e?.options?.enabledEvents?.length}startNavigation(e){this.logger.log("[instrumentation-manager.startNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.startNavigation] startNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.startNavigation(e)}endNavigation(e){this.logger.log("[instrumentation-manager.endNavigation] called",e),this.isNavigationTrackingEnabled()?this.logger.log("[instrumentation-manager.endNavigation] endNavigation called while navigation tracking is enabled. Ignoring. If you wish to track navigations manually, please disable navigation tracking via the enabledEvents array in the config."):this.navigationListener?.endNavigation(e)}isReplayRecording(){return null!==this.sessionReplayListener}startReplayRecording(){if(this.replayDesired=!0,this.isReplayRecording())return void this.logger.log("[instrumentation-manager] replay recording already started");const e=Date.now();(this.configManager.isSessionInactive(e)||this.configManager.isSessionExpired(e))&&this.rotateSession({reason:"replay start into stale session"}),this.startReplayListener(),this.presenceMonitor?.resetIdleTimer()}stopReplayRecording(){this.replayDesired=!1,this.stopReplayListener()}syncReplayState(e){this.replayDesired&&e&&!this.isReplayRecording()&&(this.logger.log("[instrumentation-manager] user present and replay desired — starting replay"),this.startReplayListener())}pauseReplayOnIdle(){this.isReplayRecording()&&(this.logger.log("[instrumentation-manager] pausing replay: user idle window elapsed"),this.stopReplayListener())}startReplayListener(){if(this.sessionReplayListener)this.logger.log("[instrumentation-manager] replay recording already started");else try{const e=new ne;e.initialize(),this.sessionReplayListener=e,this.logger.log("[instrumentation-manager] started replay recording")}catch(e){u(e),this.logger.log("[instrumentation-manager] failed to start replay recording:",e)}}reconcileSession(e,t){if(this.isRotating)return;t&&this.configManager.isSessionInactive(e)&&this.rotateSession({reason:"inactivity gap"});const i=this.configManager.getSessionElapsedMs(e),n=this.configManager.getSessionMaxDuration(),s=i>=n;this.logger.log("[instrumentation-manager] session reconcile",{userPresent:t,elapsedMs:i,maxDurationMs:n,expired:s}),s&&this.rotateSession({reason:"max session duration"}),this.syncReplayState(t),t&&(this.configManager.setLastActivityMs(e),this.presenceMonitor?.notePresence(e))}reconcileSessionOnEnqueue(e){this.isRotating||("replay"!==e.type?this.reconcileSession(Date.now(),ae.has(e.type)):this.logger.log("[instrumentation-manager] enqueue rotation check: ignoring replay event"))}rotateSession(e){if(this.isRotating)return;this.isRotating=!0;const t=this.configManager.getSessionId(),i=this.isReplayRecording();this.logger.log("[instrumentation-manager] session rotation start",{reason:e.reason,previousId:t,wasReplayActive:i});const n=(e,t)=>{try{this.logger.log(`[instrumentation-manager] session rotation: ${e}`),t()}catch(t){u(t),this.logger.log(`[instrumentation-manager] failed to ${e} during session rotation:`,t)}};try{n("stop replay",(()=>this.stopReplayListener())),n("flush events",(()=>this.eventsPool.flush())),n("rotate session ids",(()=>{const e=this.idGenerator.generateId();this.configManager.setSessionId(e),this.configManager.setSessionStartTime(f(Date.now())),this.logger.log("[instrumentation-manager] session id rotated",{from:t,to:e})})),n("sync replay",(()=>this.syncReplayState(i)))}finally{this.isRotating=!1,this.logger.log("[instrumentation-manager] session rotation complete",{reason:e.reason})}}stopReplayListener(){this.sessionReplayListener?(this.sessionReplayListener.stopRecording(),this.sessionReplayListener=null):this.logger.log("[instrumentation-manager] cannot stop replay recording: replay listener not initialized")}uninstrument(){this.eventsPool.setBeforeEnqueueHook(null),this.presenceMonitor?.destroy(),this.presenceMonitor=null,this.domEventsListener?.destroy(),this.logEventsListener?.destroy(),this.errorsEventsListener?.destroy(),this.networkEventsListener?.destroy(),this.navigationListener?.destroy(),this.performanceListener?.destroy(),this.pageLoadListener?.destroy(),this.sessionReplayListener?.destroy(),this.sessionReplayListener=null,this.replayDesired=!1}},le="gcSample",ce=class{constructor(e){this.initialized=!1,this.logger=h.initialize({debug:e.options?.debug||!1,prefix:"[groundcover]"}),this.logger.log("[session-manager] initialize called"),this.instrumentationManager=new oe,this.idGenerator=new y;if(!this.getSamplingDecision(e.options?.sessionSampleRate))return void this.logger.log("[session-manager] session is not sampled");if(this.initialized)return void this.logger.log("[session-manager] SDK already initialized");const t=M.getInstance();t.initialize(e);U.getInstance().initialize();const i=e.sessionId,n=t.getSessionId();i&&i!==n?(t.setSessionId(i),t.setSessionStartTime(f(Date.now())),this.logger.log("[session-manager] using provided sessionId:",i)):i?(t.setSessionId(i),this.logger.log("[session-manager] continuing provided sessionId:",i)):n||t.setSessionId(this.idGenerator.generateId());const s=Date.now(),r=v(t.getSessionStartTime());if(r>0&&s-r>=-6e4){if(t.isSessionExpired(s)||t.isSessionInactive(s)){const e=t.isSessionExpired(s)?"persisted session past cap":"inactivity gap across reload",i=t.getSessionId(),n=this.idGenerator.generateId();t.setSessionId(n),t.setSessionStartTime(f(s)),this.logger.log(`[session-manager] init-time rotation: ${e}`,{from:i,to:n})}}else t.setSessionStartTime(f(s));this.instrumentationManager.instrument(),this.initialized=!0}getSamplingDecision(e){try{if(!e||1===e)return!0;const t=N(le);if(t){const e="1"===t;return this.logger.log("[session-manager] using stored sampling decision:",e),e}const i=!e||Math.random()<e;return H(le,i?"1":"0"),this.logger.log("[session-manager] stored new sampling decision:",i),i}catch(t){this.logger.log("[session-manager] session storage access failed:",t);const i=!e||Math.random()<e;return this.logger.log("[session-manager] using fallback sampling decision:",i),i}}identifyUser(e){if(this.logger.log("[session-manager] identifyUser called"),!this.initialized)return void this.logger.log("[session-manager] cannot identify user: SDK not initialized");M.getInstance().updateConfig({userIdentifier:e})}updateConfig(e){if(!this.initialized)return void this.logger.log("[session-manager] cannot update config: SDK not initialized");M.getInstance().updateConfig(e)}sendCustomEvent(e){this.initialized?this.instrumentationManager.sendCustomEvent(e):this.logger.log("[session-manager] cannot send custom event: SDK not initialized")}startNavigation(e){this.initialized?this.instrumentationManager.startNavigation(e):this.logger.log("[session-manager] cannot start navigation: SDK not initialized")}endNavigation(e){this.initialized?this.instrumentationManager.endNavigation(e):this.logger.log("[session-manager] cannot end navigation: SDK not initialized")}captureException(e,t){this.initialized?this.instrumentationManager.captureException(e,t):this.logger.log("[session-manager] Cannot capture exception: SDK not initialized")}emitLog(e,t,i){this.initialized?this.instrumentationManager.emitLog(e,t,i):this.logger.log("[session-manager] Cannot emit log: SDK not initialized")}getSessionId(){return this.initialized?M.getInstance().getSessionId():(this.logger.log("[session-manager] cannot get session ID: SDK not initialized"),"")}async setSessionId(e){if(this.logger.log("[session-manager] setSessionId called"),this.initialized)try{const t=U.getInstance();await t.flushSync();const i=e||this.idGenerator.generateId(),n=M.getInstance();n.setSessionId(i),n.setSessionStartTime(f(Date.now())),this.logger.log("[session-manager] session ID set successfully:",i)}catch(e){this.logger.log("[session-manager] failed to set session ID:",e)}else this.logger.log("[session-manager] cannot set session ID: SDK not initialized")}startReplayRecording(){this.initialized?this.instrumentationManager.startReplayRecording():this.logger.log("[session-manager] cannot start replay recording: SDK not initialized")}stopReplayRecording(){this.initialized?this.instrumentationManager.stopReplayRecording():this.logger.log("[session-manager] cannot stop replay recording: SDK not initialized")}destroy(){this.initialized&&(this.instrumentationManager.uninstrument(),U.getInstance().destroy(),M.getInstance().clearSessionState(),this.initialized=!1)}},de=new class{constructor(e){this.manager=e}emit(e,t,i){this.manager&&this.manager.emitLog(t,e,i)}log(e,t){this.emit("log",e,t)}info(e,t){this.emit("info",e,t)}warn(e,t){this.emit("warn",e,t)}error(e,t){this.emit("error",e,t)}debug(e,t){this.emit("debug",e,t)}trace(e,t){this.emit("trace",e,t)}};var he={init:function(e){try{Y=new ce({cluster:e?.cluster||"",environment:e?.environment||"",namespace:e?.namespace,releaseId:e?.releaseId,dsn:e?.dsn||"",appId:e?.appId||"",userIdentifier:e?.userIdentifier,apiKey:e?.apiKey||"",options:e?.options,sessionId:e?.sessionId}),de.manager=Y}catch(e){u(e)}},identifyUser:function(e){Y?Y.identifyUser(e):console.warn("[groundcover] identifyUser: groundcover is not initialized. please call init() first")},sendCustomEvent:function(e){Y&&Y.sendCustomEvent(e)},captureException:function(e,t){Y&&Y.captureException(e,t)},logger:de,updateConfig:function(e){Y&&Y.updateConfig(e)},startNavigation:function(e){Y?Y.startNavigation(e):console.warn("[groundcover] startNavigation: groundcover is not initialized. please call init() first")},endNavigation:function(e){Y?Y.endNavigation(e):console.warn("[groundcover] endNavigation: groundcover is not initialized. please call init() first")},getSessionId:function(){return Y?Y.getSessionId():(console.warn("[groundcover] getSessionId: groundcover is not initialized. please call init() first"),"")},setSessionId:async function(e){if(Y)try{await Y.setSessionId(e)}catch(e){console.error("[groundcover] setSessionId: failed to set session ID:",e)}else console.warn("[groundcover] setSessionId: groundcover is not initialized. please call init() first")},startReplayRecording:function(){Y?Y.startReplayRecording():console.warn("[groundcover] startReplayRecording: groundcover is not initialized. please call init() first")},stopReplayRecording:function(){Y?Y.stopReplayRecording():console.warn("[groundcover] stopReplayRecording: groundcover is not initialized. please call init() first")}};if("undefined"!=typeof window&&!window.groundcover)try{window.groundcover=he}catch(e){console.warn("[groundcover] Failed to expose groundcover on window:",e)}var ge=he;export{ge as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@groundcover/browser",
3
- "version": "0.0.77-rc.0",
3
+ "version": "0.0.77-rc.2",
4
4
  "description": "groundcover browser SDK",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -59,11 +59,11 @@
59
59
  "size-limit": [
60
60
  {
61
61
  "path": "dist/index.js",
62
- "limit": "82 kB"
62
+ "limit": "83 kB"
63
63
  },
64
64
  {
65
65
  "path": "dist/index.mjs",
66
- "limit": "82 kB"
66
+ "limit": "83 kB"
67
67
  }
68
68
  ]
69
- }
69
+ }