@runtimescope/sdk 0.7.1 → 0.7.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/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
- "use strict";var N=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var te=Object.prototype.hasOwnProperty;var ne=(e,t,n)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var re=(e,t)=>{for(var n in t)N(e,n,{get:t[n],enumerable:!0})},oe=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ee(t))!te.call(e,r)&&r!==n&&N(e,r,{get:()=>t[r],enumerable:!(o=Z(t,r))||o.enumerable});return e};var se=e=>oe(N({},"__esModule",{value:!0}),e);var m=(e,t,n)=>ne(e,typeof t!="symbol"?t+"":t,n);var _e={};re(_e,{RuntimeScope:()=>_,default:()=>Ie});module.exports=se(_e);var F=console.debug.bind(console),k=class k{constructor(t){m(this,"ws",null);m(this,"batch",[]);m(this,"offlineQueue",[]);m(this,"flushTimer",null);m(this,"reconnectTimer",null);m(this,"reconnectDelay",1e3);m(this,"connected",!1);m(this,"stopped",!1);m(this,"config");m(this,"commandHandler",null);m(this,"hasEverConnected",!1);m(this,"connectionWarningTimer",null);this.config=t}connect(){this.stopped=!1,this.doConnect(),this.connectionWarningTimer=setTimeout(()=>{!this.hasEverConnected&&!this.stopped&&console.warn(`[RuntimeScope] SDK has not connected to ${this.config.serverUrl} after 10s. Is the collector running? Start it with: npx @runtimescope/collector`)},k.CONNECTION_WARNING_DELAY)}doConnect(){if(!this.stopped){try{this.ws=new WebSocket(this.config.serverUrl)}catch{this.scheduleReconnect();return}this.ws.onmessage=t=>{try{let n=JSON.parse(String(t.data));n.type==="command"&&n.payload&&this.commandHandler&&this.commandHandler(n.payload),n.type==="error"&&n.payload?.code==="AUTH_FAILED"&&(F("[RuntimeScope] Authentication failed \u2014 stopping reconnection"),this.stopped=!0)}catch{}},this.ws.onopen=()=>{if(this.connected=!0,this.hasEverConnected=!0,this.reconnectDelay=1e3,F(`[RuntimeScope] Connected to ${this.config.serverUrl}`),this.sendRaw({type:"handshake",payload:{appName:this.config.appName,sdkVersion:this.config.sdkVersion,sessionId:this.config.sessionId,...this.config.authToken?{authToken:this.config.authToken}:{}},timestamp:Date.now(),sessionId:this.config.sessionId}),this.offlineQueue.length>0){let t=this.offlineQueue.splice(0);for(let n of t)this.batch.push(n);this.flush()}this.flushTimer=setInterval(()=>this.flush(),this.config.flushIntervalMs)},this.ws.onclose=()=>{this.connected=!1,this.clearFlushTimer(),this.stopped||(F("[RuntimeScope] Disconnected, will reconnect..."),this.scheduleReconnect())},this.ws.onerror=()=>{F(`[RuntimeScope] WebSocket error connecting to ${this.config.serverUrl}`)}}}send(t){this.connected?(this.batch.push(t),this.batch.length>=this.config.batchSize&&this.flush()):this.offlineQueue.length<k.MAX_OFFLINE_QUEUE&&this.offlineQueue.push(t)}flush(){if(this.batch.length===0||!this.connected||!this.ws)return;let t=this.batch.splice(0);this.sendRaw({type:"event",payload:{events:t},timestamp:Date.now(),sessionId:this.config.sessionId})}sendRaw(t){if(this.ws&&this.ws.readyState===WebSocket.OPEN)try{this.ws.send(JSON.stringify(t))}catch{}}scheduleReconnect(){if(this.stopped||this.reconnectTimer)return;let t=this.reconnectDelay*.25*(Math.random()*2-1),n=Math.min(this.reconnectDelay+t,k.MAX_RECONNECT_DELAY);this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.reconnectDelay=Math.min(this.reconnectDelay*2,k.MAX_RECONNECT_DELAY),this.doConnect()},n)}clearFlushTimer(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null)}onCommand(t){this.commandHandler=t}sendCommandResponse(t,n,o){this.sendRaw({type:"command_response",requestId:t,command:n,payload:o,timestamp:Date.now(),sessionId:this.config.sessionId})}disconnect(){this.stopped=!0,this.clearFlushTimer(),this.connectionWarningTimer&&(clearTimeout(this.connectionWarningTimer),this.connectionWarningTimer=null),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.flush(),this.ws&&(this.ws.onclose=null,this.ws.onerror=null,this.ws.close(),this.ws=null),this.connected=!1,this.batch=[],this.offlineQueue=[]}};m(k,"MAX_OFFLINE_QUEUE",1e3),m(k,"MAX_RECONNECT_DELAY",3e4),m(k,"CONNECTION_WARNING_DELAY",1e4);var B=k;function l(){let e=new Uint8Array(4);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}function A(){let e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}var M=new Set;function q(e,t,n,o){let r=window.fetch,c=new Set(n.map(s=>s.toLowerCase())),i=o?.captureBody??!1,a=o?.maxBodySize??65536;return window.fetch=async function(s,u){let d=performance.now(),p=typeof s=="string"?s:s instanceof URL?s.href:s.url,g=(u?.method??"GET").toUpperCase(),R=ie(u?.headers,c),y=ce(u?.body),T=de(u?.body),C;i&&u?.body&&(C=ue(u.body,a));let S=`${g}:${p}:${d}`;M.add(S),setTimeout(()=>M.delete(S),5e3);try{let h=await r.call(window,s,u),L=performance.now()-d,b=parseInt(h.headers.get("content-length")||"0",10),f=ae(h.headers,c),w;if(i)try{let I=await h.clone().text();w=I.length>a?I.slice(0,a):I}catch{}let E={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"network",url:p,method:g,status:h.status,requestHeaders:R,responseHeaders:f,requestBodySize:y,responseBodySize:b,duration:L,ttfb:L,graphqlOperation:T,requestBody:C,responseBody:w,source:"fetch"};if(o?.beforeSend){let O=o.beforeSend(E);O&&e(O)}else e(E);return h}catch(h){let L=performance.now()-d,b="error",f="";h instanceof DOMException&&h.name==="AbortError"?(b="abort",f="Request aborted"):h instanceof Error&&(f=h.message);let w={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"network",url:p,method:g,status:0,requestHeaders:R,responseHeaders:{},requestBodySize:y,responseBodySize:0,duration:L,ttfb:0,graphqlOperation:T,requestBody:C,errorPhase:b,errorMessage:f,source:"fetch"};if(o?.beforeSend){let E=o.beforeSend(w);E&&e(E)}else e(w);throw h}},()=>{window.fetch=r}}function ie(e,t){let n={};if(!e)return n;if(e instanceof Headers)e.forEach((o,r)=>{n[r]=t.has(r.toLowerCase())?"[REDACTED]":o});else if(Array.isArray(e))for(let[o,r]of e)n[o]=t.has(o.toLowerCase())?"[REDACTED]":r;else for(let[o,r]of Object.entries(e))n[o]=t.has(o.toLowerCase())?"[REDACTED]":r;return n}function ae(e,t){let n={};return e.forEach((o,r)=>{n[r]=t.has(r.toLowerCase())?"[REDACTED]":o}),n}function ce(e){return e?typeof e=="string"?new Blob([e]).size:e instanceof Blob?e.size:e instanceof ArrayBuffer||ArrayBuffer.isView(e)?e.byteLength:e instanceof FormData?0:e instanceof URLSearchParams?new Blob([e.toString()]).size:0:0}function ue(e,t){if(e){if(typeof e=="string")return e.length>t?e.slice(0,t):e;if(e instanceof URLSearchParams){let n=e.toString();return n.length>t?n.slice(0,t):n}if(e instanceof FormData)return"[FormData]";if(e instanceof Blob)return`[Blob ${e.size} bytes]`;if(e instanceof ArrayBuffer)return`[ArrayBuffer ${e.byteLength} bytes]`;if(ArrayBuffer.isView(e))return`[TypedArray ${e.byteLength} bytes]`}}function de(e){if(!(!e||typeof e!="string"))try{let t=JSON.parse(e);if(typeof t.query=="string"){let n=t.query.trim(),o="query";n.startsWith("mutation")?o="mutation":n.startsWith("subscription")&&(o="subscription");let r=t.operationName||pe(n)||"anonymous";return{type:o,name:r}}}catch{}}function pe(e){return e.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function v(e,t=5){let n=new WeakSet;function o(r,c){if(c>t)return"[max depth]";if(r==null)return r;if(typeof r=="function")return`[Function: ${r.name||"anonymous"}]`;if(typeof r=="symbol"||typeof r=="bigint")return r.toString();if(typeof r!="object")return r;if(r instanceof Error)return{name:r.name,message:r.message,stack:r.stack};if(r instanceof Date)return r.toISOString();if(r instanceof RegExp)return r.toString();if(n.has(r))return"[Circular]";if(n.add(r),Array.isArray(r))return r.map(u=>o(u,c+1));let i={},a;try{a=Object.keys(r)}catch{return"[Object]"}let s=50;for(let u=0;u<Math.min(a.length,s);u++)try{i[a[u]]=o(r[a[u]],c+1)}catch{i[a[u]]="[Error accessing property]"}return a.length>s&&(i["..."]=`${a.length-s} more keys`),i}return o(e,0)}var z=["log","warn","error","info","debug","trace"];function j(e,t,n){let o={};for(let r of z)o[r]=console[r].bind(console),console[r]=(...c)=>{let i=c.map(s=>typeof s=="string"?s:fe(s)).join(" "),a={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"console",level:r,message:i,args:c.map(s=>v(s,3)),stackTrace:r==="error"||r==="trace"?new Error().stack?.split(`
1
+ "use strict";var M=Object.defineProperty;var se=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var ae=Object.prototype.hasOwnProperty;var ce=(e,t,n)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var ue=(e,t)=>{for(var n in t)M(e,n,{get:t[n],enumerable:!0})},de=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ie(t))!ae.call(e,r)&&r!==n&&M(e,r,{get:()=>t[r],enumerable:!(o=se(t,r))||o.enumerable});return e};var le=e=>de(M({},"__esModule",{value:!0}),e);var f=(e,t,n)=>ce(e,typeof t!="symbol"?t+"":t,n);var ze={};ue(ze,{RuntimeScope:()=>C,default:()=>qe});module.exports=le(ze);var I=console.debug.bind(console),R=class R{constructor(t){f(this,"ws",null);f(this,"batch",[]);f(this,"offlineQueue",[]);f(this,"flushTimer",null);f(this,"reconnectTimer",null);f(this,"reconnectDelay",500);f(this,"reconnectAttempt",0);f(this,"connected",!1);f(this,"stopped",!1);f(this,"config");f(this,"commandHandler",null);f(this,"hasEverConnected",!1);f(this,"connectionWarningTimer",null);f(this,"visibilityHandler",null);f(this,"onlineHandler",null);this.config=t}connect(){this.stopped=!1,this.doConnect(),this.connectionWarningTimer=setTimeout(()=>{!this.hasEverConnected&&!this.stopped&&console.warn(`[RuntimeScope] SDK has not connected to ${this.config.serverUrl} after 10s. Is the collector running? Start it with: npx @runtimescope/collector`)},R.CONNECTION_WARNING_DELAY),typeof document<"u"&&(this.visibilityHandler=()=>{document.visibilityState==="visible"&&!this.connected&&!this.stopped&&(I("[RuntimeScope] Tab visible \u2014 attempting immediate reconnect"),this.resetReconnectState(),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.doConnect())},document.addEventListener("visibilitychange",this.visibilityHandler)),typeof window<"u"&&(this.onlineHandler=()=>{!this.connected&&!this.stopped&&(I("[RuntimeScope] Network online \u2014 attempting immediate reconnect"),this.resetReconnectState(),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.doConnect())},window.addEventListener("online",this.onlineHandler))}doConnect(){if(!this.stopped){try{this.ws=new WebSocket(this.config.serverUrl)}catch{this.scheduleReconnect();return}this.ws.onmessage=t=>{try{let n=JSON.parse(String(t.data));n.type==="command"&&n.payload&&this.commandHandler&&this.commandHandler(n.payload),n.type==="__server_restart"&&(I("[RuntimeScope] Server restart notice received \u2014 will reconnect immediately"),this.resetReconnectState()),n.type==="error"&&n.payload?.code==="AUTH_FAILED"&&(I("[RuntimeScope] Authentication failed \u2014 stopping reconnection"),this.stopped=!0)}catch{}},this.ws.onopen=()=>{if(this.connected=!0,this.hasEverConnected=!0,this.resetReconnectState(),I(`[RuntimeScope] Connected to ${this.config.serverUrl}`),this.sendRaw({type:"handshake",payload:{appName:this.config.appName,sdkVersion:this.config.sdkVersion,sessionId:this.config.sessionId,...this.config.authToken?{authToken:this.config.authToken}:{}},timestamp:Date.now(),sessionId:this.config.sessionId}),this.offlineQueue.length>0){let t=this.offlineQueue.splice(0);for(let n of t)this.batch.push(n);this.flush()}this.flushTimer=setInterval(()=>this.flush(),this.config.flushIntervalMs)},this.ws.onclose=()=>{this.connected=!1,this.clearFlushTimer(),this.stopped||(I("[RuntimeScope] Disconnected, will reconnect..."),this.scheduleReconnect())},this.ws.onerror=()=>{I(`[RuntimeScope] WebSocket error connecting to ${this.config.serverUrl}`)}}}send(t){this.connected?(this.batch.push(t),this.batch.length>=this.config.batchSize&&this.flush()):this.offlineQueue.length<R.MAX_OFFLINE_QUEUE&&this.offlineQueue.push(t)}flush(){if(this.batch.length===0||!this.connected||!this.ws)return;let t=this.batch.splice(0);this.sendRaw({type:"event",payload:{events:t},timestamp:Date.now(),sessionId:this.config.sessionId})}sendRaw(t){if(this.ws&&this.ws.readyState===WebSocket.OPEN)try{this.ws.send(JSON.stringify(t))}catch{}}scheduleReconnect(){if(this.stopped||this.reconnectTimer)return;this.reconnectAttempt++;let t;if(this.reconnectAttempt<=R.FAST_RETRY_COUNT)t=R.FAST_RETRY_DELAY;else{let n=this.reconnectDelay*.25*(Math.random()*2-1);t=Math.min(this.reconnectDelay+n,R.MAX_RECONNECT_DELAY),this.reconnectDelay=Math.min(this.reconnectDelay*2,R.MAX_RECONNECT_DELAY)}this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.doConnect()},t)}resetReconnectState(){this.reconnectDelay=500,this.reconnectAttempt=0}clearFlushTimer(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null)}onCommand(t){this.commandHandler=t}sendCommandResponse(t,n,o){this.sendRaw({type:"command_response",requestId:t,command:n,payload:o,timestamp:Date.now(),sessionId:this.config.sessionId})}disconnect(){this.stopped=!0,this.clearFlushTimer(),this.connectionWarningTimer&&(clearTimeout(this.connectionWarningTimer),this.connectionWarningTimer=null),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.visibilityHandler&&typeof document<"u"&&(document.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=null),this.onlineHandler&&typeof window<"u"&&(window.removeEventListener("online",this.onlineHandler),this.onlineHandler=null),this.flush(),this.ws&&(this.ws.onclose=null,this.ws.onerror=null,this.ws.close(),this.ws=null),this.connected=!1,this.batch=[],this.offlineQueue=[]}};f(R,"MAX_OFFLINE_QUEUE",1e3),f(R,"MAX_RECONNECT_DELAY",3e4),f(R,"CONNECTION_WARNING_DELAY",1e4),f(R,"FAST_RETRY_COUNT",3),f(R,"FAST_RETRY_DELAY",500);var A=R;function m(){let e=new Uint8Array(4);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}function $(){let e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}var pe=1e3,fe=1e4,me=5e3,k=new Map,H=null,x=0;function he(){x++,!H&&(H=setInterval(()=>{let e=Date.now()-fe;for(let[t,n]of k)n<e&&k.delete(t)},me))}function ge(){x--,x<=0&&H&&(clearInterval(H),H=null,x=0)}function W(e,t,n,o){let r=window.fetch,c=new Set(n.map(a=>a.toLowerCase())),s=o?.captureBody??!1,i=o?.maxBodySize??65536;return he(),window.fetch=async function(a,u){let d=performance.now(),l=typeof a=="string"?a:a instanceof URL?a.href:a.url,p=(u?.method??"GET").toUpperCase(),E=ve(u?.headers,c),v=we(u?.body),T=Se(u?.body),D;s&&u?.body&&(D=Re(u.body,i));let q=`${p}:${l}:${d}`;if(k.size>=pe){let h=k.keys().next().value;h!==void 0&&k.delete(h)}k.set(q,Date.now());try{let h=await r.call(window,a,u),b=performance.now()-d,L=parseInt(h.headers.get("content-length")||"0",10),_=ye(h.headers,c),g;if(s)try{let O=await h.clone().text();g=O.length>i?O.slice(0,i):O}catch{}let w={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"network",url:l,method:p,status:h.status,requestHeaders:E,responseHeaders:_,requestBodySize:v,responseBodySize:L,duration:b,ttfb:b,graphqlOperation:T,requestBody:D,responseBody:g,source:"fetch"};if(o?.beforeSend){let S=o.beforeSend(w);S&&e(S)}else e(w);return h}catch(h){let b=performance.now()-d,L="error",_="";h instanceof DOMException&&h.name==="AbortError"?(L="abort",_="Request aborted"):h instanceof Error&&(_=h.message);let g={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"network",url:l,method:p,status:0,requestHeaders:E,responseHeaders:{},requestBodySize:v,responseBodySize:0,duration:b,ttfb:0,graphqlOperation:T,requestBody:D,errorPhase:L,errorMessage:_,source:"fetch"};if(o?.beforeSend){let w=o.beforeSend(g);w&&e(w)}else e(g);throw h}},()=>{window.fetch=r,ge()}}function ve(e,t){let n={};if(!e)return n;if(e instanceof Headers)e.forEach((o,r)=>{n[r]=t.has(r.toLowerCase())?"[REDACTED]":o});else if(Array.isArray(e))for(let[o,r]of e)n[o]=t.has(o.toLowerCase())?"[REDACTED]":r;else for(let[o,r]of Object.entries(e))n[o]=t.has(o.toLowerCase())?"[REDACTED]":r;return n}function ye(e,t){let n={};return e.forEach((o,r)=>{n[r]=t.has(r.toLowerCase())?"[REDACTED]":o}),n}function we(e){return e?typeof e=="string"?new Blob([e]).size:e instanceof Blob?e.size:e instanceof ArrayBuffer||ArrayBuffer.isView(e)?e.byteLength:e instanceof FormData?0:e instanceof URLSearchParams?new Blob([e.toString()]).size:0:0}function Re(e,t){if(e){if(typeof e=="string")return e.length>t?e.slice(0,t):e;if(e instanceof URLSearchParams){let n=e.toString();return n.length>t?n.slice(0,t):n}if(e instanceof FormData)return"[FormData]";if(e instanceof Blob)return`[Blob ${e.size} bytes]`;if(e instanceof ArrayBuffer)return`[ArrayBuffer ${e.byteLength} bytes]`;if(ArrayBuffer.isView(e))return`[TypedArray ${e.byteLength} bytes]`}}function Se(e){if(!(!e||typeof e!="string"))try{let t=JSON.parse(e);if(typeof t.query=="string"){let n=t.query.trim(),o="query";n.startsWith("mutation")?o="mutation":n.startsWith("subscription")&&(o="subscription");let r=t.operationName||Ee(n)||"anonymous";return{type:o,name:r}}}catch{}}function Ee(e){return e.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function y(e,t=5){let n=new WeakSet;function o(r,c){if(c>t)return"[max depth]";if(r==null)return r;if(typeof r=="function")return`[Function: ${r.name||"anonymous"}]`;if(typeof r=="symbol"||typeof r=="bigint")return r.toString();if(typeof r!="object")return r;if(r instanceof Error)return{name:r.name,message:r.message,stack:r.stack};if(r instanceof Date)return r.toISOString();if(r instanceof RegExp)return r.toString();if(n.has(r))return"[Circular]";if(n.add(r),Array.isArray(r))return r.map(u=>o(u,c+1));let s={},i;try{i=Object.keys(r)}catch{return"[Object]"}let a=50;for(let u=0;u<Math.min(i.length,a);u++)try{s[i[u]]=o(r[i[u]],c+1)}catch{s[i[u]]="[Error accessing property]"}return i.length>a&&(s["..."]=`${i.length-a} more keys`),s}return o(e,0)}var V=["log","warn","error","info","debug","trace"];function G(e,t,n){let o={};for(let r of V)o[r]=console[r].bind(console),console[r]=(...c)=>{let s=c.map(a=>typeof a=="string"?a:be(a)).join(" "),i={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"console",level:r,message:s,args:c.map(a=>y(a,3)),stackTrace:r==="error"||r==="trace"?new Error().stack?.split(`
2
2
  `).slice(2).join(`
3
- `):void 0,sourceFile:void 0};if(n){let s=n(a);s&&e(s)}else e(a);o[r](...c)};return()=>{for(let r of z)console[r]=o[r]}}function fe(e){try{return JSON.stringify(e)}catch{return String(e)}}function U(e,t,n,o){let r=new Set(n.map(d=>d.toLowerCase())),c=o?.captureBody??!1,i=o?.maxBodySize??65536,a=XMLHttpRequest.prototype.open,s=XMLHttpRequest.prototype.setRequestHeader,u=XMLHttpRequest.prototype.send;return XMLHttpRequest.prototype.open=function(d,p){return this.__rs_method=d.toUpperCase(),this.__rs_url=typeof p=="string"?p:p.href,this.__rs_headers={},a.apply(this,arguments)},XMLHttpRequest.prototype.setRequestHeader=function(d,p){return this.__rs_headers&&(this.__rs_headers[d.toLowerCase()]=r.has(d.toLowerCase())?"[REDACTED]":p),s.call(this,d,p)},XMLHttpRequest.prototype.send=function(d){let p=this.__rs_method??"GET",g=this.__rs_url??"",R=`${p}:${g}`;if(Array.from(M).some(f=>f.startsWith(R)))return this.__rs_fetchIntercepted=!0,u.call(this,d);let T={...this.__rs_headers??{}},C=performance.now(),S,h=0;if(d){if(typeof d=="string")h=new Blob([d]).size,c&&(S=d.length>i?d.slice(0,i):d);else if(d instanceof Blob)h=d.size,c&&(S=`[Blob ${d.size} bytes]`);else if(d instanceof ArrayBuffer)h=d.byteLength,c&&(S=`[ArrayBuffer ${d.byteLength} bytes]`);else if(d instanceof FormData)c&&(S="[FormData]");else if(d instanceof URLSearchParams){let f=d.toString();h=new Blob([f]).size,c&&(S=f.length>i?f.slice(0,i):f)}}let L=me(d),b=f=>{let w={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"network",url:g,method:p,status:0,requestHeaders:T,responseHeaders:{},requestBodySize:h,responseBodySize:0,duration:0,ttfb:0,graphqlOperation:L,requestBody:S,source:"xhr",...f};if(o?.beforeSend){let E=o.beforeSend(w);E&&e(E)}else e(w)};return this.addEventListener("load",()=>{let f=performance.now()-C,w=le(this.getAllResponseHeaders(),r),E=parseInt(this.getResponseHeader("content-length")||"0",10),O;if(c&&this.responseType===""||this.responseType==="text")try{let I=this.responseText;O=I.length>i?I.slice(0,i):I}catch{}b({status:this.status,responseHeaders:w,responseBodySize:E,responseBody:O,duration:f,ttfb:f})}),this.addEventListener("error",()=>{let f=performance.now()-C;b({duration:f,errorPhase:"error",errorMessage:"Network error"})}),this.addEventListener("abort",()=>{let f=performance.now()-C;b({duration:f,errorPhase:"abort",errorMessage:"Request aborted"})}),this.addEventListener("timeout",()=>{let f=performance.now()-C;b({duration:f,errorPhase:"timeout",errorMessage:`Request timed out after ${this.timeout}ms`})}),u.call(this,d)},()=>{XMLHttpRequest.prototype.open=a,XMLHttpRequest.prototype.setRequestHeader=s,XMLHttpRequest.prototype.send=u}}function le(e,t){let n={};if(!e)return n;for(let o of e.trim().split(/[\r\n]+/)){let r=o.indexOf(":");if(r===-1)continue;let c=o.slice(0,r).trim().toLowerCase(),i=o.slice(r+1).trim();n[c]=t.has(c)?"[REDACTED]":i}return n}function me(e){if(!(!e||typeof e!="string"))try{let t=JSON.parse(e);if(typeof t.query=="string"){let n=t.query.trim(),o="query";n.startsWith("mutation")?o="mutation":n.startsWith("subscription")&&(o="subscription");let r=t.operationName||he(n)||"anonymous";return{type:o,name:r}}}catch{}}function he(e){return e.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function $(e,t,n,o){let r=[],c=new Map;for(let[i,a]of Object.entries(n)){let s=ge(a);if(s==="zustand"){let u=a;x(e,t,o?.beforeSend,{storeId:i,library:s,phase:"init",state:v(u.getState(),4)});let d=u.subscribe((p,g)=>{let R=X(g,p);x(e,t,o?.beforeSend,{storeId:i,library:s,phase:"update",state:v(p,4),previousState:v(g,4),diff:R?v(R,3):void 0})});r.push(d)}else if(s==="redux"){let u=a;x(e,t,o?.beforeSend,{storeId:i,library:s,phase:"init",state:v(u.getState(),4)});let d,p=u.dispatch.bind(u);c.set(i,p),u.dispatch=y=>(y&&typeof y=="object"&&"type"in y&&(d={type:String(y.type),payload:y.payload}),p(y));let g=u.getState(),R=u.subscribe(()=>{let y=u.getState(),T=X(g,y);x(e,t,o?.beforeSend,{storeId:i,library:s,phase:"update",state:v(y,4),previousState:v(g,4),diff:T?v(T,3):void 0,action:d?v(d,3):void 0}),g=y,d=void 0});r.push(R)}}return()=>{for(let i of r)i();for(let[i,a]of c){let s=n[i];s&&(s.dispatch=a)}}}function ge(e){if(!e||typeof e!="object")return"unknown";let t=e;return typeof t.getState=="function"&&typeof t.setState=="function"&&typeof t.subscribe=="function"?"zustand":typeof t.dispatch=="function"&&typeof t.getState=="function"&&typeof t.subscribe=="function"?"redux":"unknown"}function X(e,t){if(!e||!t||typeof e!="object"||typeof t!="object")return null;let n={},o=e,r=t,c=new Set([...Object.keys(o),...Object.keys(r)]);for(let i of c)o[i]!==r[i]&&(n[i]={from:o[i],to:r[i]});return Object.keys(n).length>0?n:null}function x(e,t,n,o){let r={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"state",...o};if(n){let c=n(r);c&&e(c)}else e(r)}var ve={LCP:[2500,4e3],FCP:[1800,3e3],CLS:[.1,.25],TTFB:[800,1800],FID:[100,300],INP:[200,500]};function ye(e,t){let[n,o]=ve[e]??[1/0,1/0];return t<=n?"good":t<=o?"needs-improvement":"poor"}function W(e,t,n){let o=[],r=(a,s,u)=>{let d={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"performance",metricName:a,value:Math.round(s*100)/100,rating:ye(a,s),element:u};if(n?.beforeSend){let p=n.beforeSend(d);p&&e(p)}else e(d)};D(o,"largest-contentful-paint",a=>{let s=a[a.length-1];if(!s)return;let u=s.element;r("LCP",s.startTime,u?.tagName?.toLowerCase())}),D(o,"paint",a=>{for(let s of a)s.name==="first-contentful-paint"&&r("FCP",s.startTime)});let c=0;D(o,"layout-shift",a=>{for(let s of a){let u=s;!u.hadRecentInput&&u.value&&(c+=u.value,r("CLS",c))}}),D(o,"first-input",a=>{let s=a[0];if(!s)return;let u=s;u.processingStart&&r("FID",u.processingStart-s.startTime)}),D(o,"navigation",a=>{let s=a[0];s&&r("TTFB",s.responseStart-s.requestStart)});let i=0;return D(o,"event",a=>{for(let s of a)s.duration>i&&(i=s.duration,r("INP",i))},{durationThreshold:16}),()=>{for(let a of o)try{a.disconnect()}catch{}}}function D(e,t,n,o){try{let r=new PerformanceObserver(c=>{n(c.getEntries())});r.observe({type:t,buffered:!0,...o}),e.push(r)}catch{}}var we=0,Ee=1,G=1e4,V=100;function Q(e,t,n){let o=new Map,r=n?.snapshotIntervalMs??5e3,c=null,i=Re();if(!i)return()=>{};let a=i.onCommitFiberRoot;return i._runtimescope_original=a,i.onCommitFiberRoot=(s,u)=>{if(a)try{a(s,u)}catch{}u.current&&H(u.current,o)},c=setInterval(()=>{Te(o,e,t,n?.beforeSend)},r),()=>{c&&(clearInterval(c),c=null),i&&(i._runtimescope_original?i.onCommitFiberRoot=i._runtimescope_original:delete i.onCommitFiberRoot,delete i._runtimescope_original)}}function Re(){if(typeof window>"u")return null;let e=window;return e.__REACT_DEVTOOLS_GLOBAL_HOOK__||(e.__REACT_DEVTOOLS_GLOBAL_HOOK__={}),e.__REACT_DEVTOOLS_GLOBAL_HOOK__}function H(e,t){Se(e,t),e.child&&H(e.child,t),e.sibling&&H(e.sibling,t)}function Se(e,t){if(e.tag!==we&&e.tag!==Ee)return;let n=be(e);if(!n)return;let o=Date.now(),r=e.alternate===null,c=e.actualDuration??0,i=ke(e,r),a=t.get(n);a||(a={renderCount:0,totalDuration:0,lastRenderTime:0,lastRenderPhase:"mount",lastRenderCause:"unknown",renderTimestamps:[]},t.set(n,a)),a.renderCount++,a.totalDuration+=c,a.lastRenderTime=o,a.lastRenderPhase=r?"mount":"update",a.lastRenderCause=i??"unknown",a.renderTimestamps.push(o),a.renderTimestamps.length>V&&(a.renderTimestamps=a.renderTimestamps.slice(-V))}function be(e){if(e.type&&typeof e.type!="string")return e.type.displayName||e.type.name||void 0}function ke(e,t){return t?"props":e.alternate?e.memoizedProps!==e.alternate.memoizedProps?"props":e.memoizedState!==e.alternate.memoizedState?"state":"parent":"unknown"}function Ce(e){if(e.length<2)return 0;let t=Date.now(),n=t-G,o=e.filter(c=>c>=n);if(o.length<2)return 0;let r=t-o[0];return r===0?0:o.length/(r/1e3)}function Te(e,t,n,o){if(e.size===0)return;let r=[],c=[],i=0;for(let[s,u]of e){let d=Ce(u.renderTimestamps),p=d>4||u.renderCount>20;r.push({componentName:s,renderCount:u.renderCount,totalDuration:Math.round(u.totalDuration*100)/100,avgDuration:u.renderCount>0?Math.round(u.totalDuration/u.renderCount*100)/100:0,lastRenderPhase:u.lastRenderPhase,lastRenderCause:u.lastRenderCause,renderVelocity:Math.round(d*100)/100,suspicious:p}),p&&c.push(s),i+=u.renderCount}r.sort((s,u)=>u.renderCount-s.renderCount);let a={eventId:l(),sessionId:n,timestamp:Date.now(),eventType:"render",profiles:r,snapshotWindowMs:G,totalRenders:i,suspiciousComponents:c};if(o){let s=o(a);s&&t(s)}else t(a);for(let s of e.values())s.renderCount=0,s.totalDuration=0}function K(e,t,n){let o=c=>{let i,a,s;if(c instanceof ErrorEvent)i=c.message||"Uncaught error",a=c.error?.stack,s=c.filename?`${c.filename}:${c.lineno}:${c.colno}`:void 0;else{let d=c.target;if(d&&d!==window){let p=d.tagName?.toLowerCase()??"unknown",g=d.src??d.src??d.href??"unknown";i=`Failed to load resource: <${p}> ${g}`}else return}let u={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"console",level:"error",message:`[Uncaught] ${i}`,args:[v(i,3)],stackTrace:a,sourceFile:s};if(n){let d=n(u);d&&e(d)}else e(u)},r=c=>{let i=c.reason,a,s;if(i instanceof Error)a=i.message,s=i.stack;else if(typeof i=="string")a=i;else try{a=JSON.stringify(i)}catch{a=String(i)}let u={eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"console",level:"error",message:`[Unhandled Rejection] ${a}`,args:[v(i,3)],stackTrace:s,sourceFile:void 0};if(n){let d=n(u);d&&e(d)}else e(u)};return window.addEventListener("error",o,!0),window.addEventListener("unhandledrejection",r),()=>{window.removeEventListener("error",o,!0),window.removeEventListener("unhandledrejection",r)}}function J(e,t){let n=window.location.href;function o(s,u){let d=n;d!==s&&(n=s,e({eventId:l(),sessionId:t,timestamp:Date.now(),eventType:"navigation",from:d,to:s,trigger:u}))}let r=history.pushState.bind(history),c=history.replaceState.bind(history);history.pushState=function(...s){r(...s),o(window.location.href,"pushState")},history.replaceState=function(...s){c(...s),o(window.location.href,"replaceState")};let i=()=>o(window.location.href,"popstate");window.addEventListener("popstate",i);let a=()=>o(window.location.href,"hashchange");return window.addEventListener("hashchange",a),()=>{history.pushState=r,history.replaceState=c,window.removeEventListener("popstate",i),window.removeEventListener("hashchange",a)}}var P="0.7.1",Y=console.debug.bind(console),_=class{static get sessionId(){return this._sessionId}static connect(t={}){if(t.enabled===!1)return;this.transport&&this.disconnect();let n={serverUrl:t.serverUrl??t.endpoint??"ws://localhost:9090",appName:t.appName??"unknown",captureNetwork:t.captureNetwork??!0,captureConsole:t.captureConsole??!0,captureXhr:t.captureXhr??!0,captureBody:t.captureBody??!1,maxBodySize:t.maxBodySize??65536,capturePerformance:t.capturePerformance??!0,captureRenders:t.captureRenders??!0,captureNavigation:t.captureNavigation??!0,stores:t.stores??{},beforeSend:t.beforeSend,redactHeaders:t.redactHeaders??["authorization","cookie"],batchSize:t.batchSize??50,flushIntervalMs:t.flushIntervalMs??100};this._sessionId=A(),this.transport=new B({serverUrl:n.serverUrl,appName:n.appName,sessionId:this._sessionId,sdkVersion:P,authToken:t.authToken,batchSize:n.batchSize,flushIntervalMs:n.flushIntervalMs}),this.transport.connect(),Y(`[RuntimeScope] SDK v${P} initializing \u2014 app: ${n.appName}, server: ${n.serverUrl}`);let o=i=>this.transport?.send(i);o({eventId:l(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:n.appName,connectedAt:Date.now(),sdkVersion:P,buildMeta:t.buildMeta});let r=this._sessionId;this.transport.onCommand(i=>{this.handleCommand(i,o,r)}),n.captureNetwork&&this.restoreFns.push(q(o,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureXhr&&this.restoreFns.push(U(o,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureConsole&&(this.restoreFns.push(j(o,this._sessionId,n.beforeSend)),this.restoreFns.push(K(o,this._sessionId,n.beforeSend))),Object.keys(n.stores).length>0&&this.restoreFns.push($(o,this._sessionId,n.stores,{beforeSend:n.beforeSend})),n.capturePerformance&&this.restoreFns.push(W(o,this._sessionId,{beforeSend:n.beforeSend})),n.captureRenders&&this.restoreFns.push(Q(o,this._sessionId,{beforeSend:n.beforeSend})),n.captureNavigation&&this.restoreFns.push(J(o,this._sessionId));let c=[n.captureNetwork&&"fetch",n.captureXhr&&"xhr",n.captureConsole&&"console, errors",Object.keys(n.stores).length>0&&"state",n.capturePerformance&&"performance",n.captureRenders&&"renders",n.captureNavigation&&"navigation"].filter(Boolean);Y(`[RuntimeScope] Interceptors active \u2014 ${c.join(", ")}`)}static handleCommand(t,n,o){if(t.command==="capture_dom_snapshot"){let r=t.params?.maxSize??5e5,c=document.documentElement.outerHTML,i=c.length>r;i&&(c=c.slice(0,r));let a={eventId:l(),sessionId:o,timestamp:Date.now(),eventType:"dom_snapshot",html:c,url:window.location.href,viewport:{width:window.innerWidth,height:window.innerHeight},scrollPosition:{x:window.scrollX,y:window.scrollY},elementCount:document.querySelectorAll("*").length,truncated:i};n(a),this.transport?.sendCommandResponse(t.requestId,t.command,a)}else this.transport?.sendCommandResponse(t.requestId,t.command,{error:"Unknown command"})}static init(t={}){return this.connect(t)}static track(t,n){if(!this.transport||!this._sessionId)return;let o={eventId:l(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"custom",name:t,properties:n};this.transport.send(o)}static disconnect(){for(let t of this.restoreFns)t();this.restoreFns=[],this.transport?.disconnect(),this.transport=null,this._sessionId=null}};m(_,"transport",null),m(_,"restoreFns",[]),m(_,"_sessionId",null);var Ie=_;0&&(module.exports={RuntimeScope});
3
+ `):void 0,sourceFile:void 0};if(n){let a=n(i);a&&e(a)}else e(i);o[r](...c)};return()=>{for(let r of V)console[r]=o[r]}}function be(e){try{return JSON.stringify(e)}catch{return String(e)}}function Q(e,t,n,o){let r=new Set(n.map(l=>l.toLowerCase())),c=o?.captureBody??!1,s=o?.maxBodySize??65536,i=new AbortController,a=XMLHttpRequest.prototype.open,u=XMLHttpRequest.prototype.setRequestHeader,d=XMLHttpRequest.prototype.send;return XMLHttpRequest.prototype.open=function(l,p){return this.__rs_method=l.toUpperCase(),this.__rs_url=typeof p=="string"?p:p.href,this.__rs_headers={},a.apply(this,arguments)},XMLHttpRequest.prototype.setRequestHeader=function(l,p){return this.__rs_headers&&(this.__rs_headers[l.toLowerCase()]=r.has(l.toLowerCase())?"[REDACTED]":p),u.call(this,l,p)},XMLHttpRequest.prototype.send=function(l){let p=this.__rs_method??"GET",E=this.__rs_url??"",v=`${p}:${E}`,T=!1;for(let g of k.keys())if(g.startsWith(v)){T=!0;break}if(T)return this.__rs_fetchIntercepted=!0,d.call(this,l);let D={...this.__rs_headers??{}},q=performance.now(),h,b=0;if(l){if(typeof l=="string")b=new Blob([l]).size,c&&(h=l.length>s?l.slice(0,s):l);else if(l instanceof Blob)b=l.size,c&&(h=`[Blob ${l.size} bytes]`);else if(l instanceof ArrayBuffer)b=l.byteLength,c&&(h=`[ArrayBuffer ${l.byteLength} bytes]`);else if(l instanceof FormData)c&&(h="[FormData]");else if(l instanceof URLSearchParams){let g=l.toString();b=new Blob([g]).size,c&&(h=g.length>s?g.slice(0,s):g)}}let L=_e(l),_=g=>{let w={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"network",url:E,method:p,status:0,requestHeaders:D,responseHeaders:{},requestBodySize:b,responseBodySize:0,duration:0,ttfb:0,graphqlOperation:L,requestBody:h,source:"xhr",...g};if(o?.beforeSend){let S=o.beforeSend(w);S&&e(S)}else e(w)};return this.addEventListener("loadend",()=>{let g=performance.now()-q;if(this.status>0){let w=Te(this.getAllResponseHeaders(),r),S=parseInt(this.getResponseHeader("content-length")||"0",10),O;if(c&&(this.responseType===""||this.responseType==="text"))try{let z=this.responseText;O=z.length>s?z.slice(0,s):z}catch{}_({status:this.status,responseHeaders:w,responseBodySize:S,responseBody:O,duration:g,ttfb:g})}else{let w="error",S="Network error";this.readyState===0||this.status,this.timeout>0&&g>=this.timeout&&(w="timeout",S=`Request timed out after ${this.timeout}ms`),_({duration:g,errorPhase:w,errorMessage:S})}},{once:!0,signal:i.signal}),d.call(this,l)},()=>{i.abort(),XMLHttpRequest.prototype.open=a,XMLHttpRequest.prototype.setRequestHeader=u,XMLHttpRequest.prototype.send=d}}function Te(e,t){let n={};if(!e)return n;for(let o of e.trim().split(/[\r\n]+/)){let r=o.indexOf(":");if(r===-1)continue;let c=o.slice(0,r).trim().toLowerCase(),s=o.slice(r+1).trim();n[c]=t.has(c)?"[REDACTED]":s}return n}function _e(e){if(!(!e||typeof e!="string"))try{let t=JSON.parse(e);if(typeof t.query=="string"){let n=t.query.trim(),o="query";n.startsWith("mutation")?o="mutation":n.startsWith("subscription")&&(o="subscription");let r=t.operationName||ke(n)||"anonymous";return{type:o,name:r}}}catch{}}function ke(e){return e.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function Y(e,t,n,o){let r=[],c=new Map;for(let[s,i]of Object.entries(n)){let a=Ce(i);if(a==="zustand"){let u=i;B(e,t,o?.beforeSend,{storeId:s,library:a,phase:"init",state:y(u.getState(),4)});let d=u.subscribe((l,p)=>{let E=K(p,l);B(e,t,o?.beforeSend,{storeId:s,library:a,phase:"update",state:y(l,4),previousState:y(p,4),diff:E?y(E,3):void 0})});r.push(d)}else if(a==="redux"){let u=i;B(e,t,o?.beforeSend,{storeId:s,library:a,phase:"init",state:y(u.getState(),4)});let d,l=u.dispatch.bind(u);c.set(s,l),u.dispatch=v=>(v&&typeof v=="object"&&"type"in v&&(d={type:String(v.type),payload:v.payload}),l(v));let p=u.getState(),E=u.subscribe(()=>{let v=u.getState(),T=K(p,v);B(e,t,o?.beforeSend,{storeId:s,library:a,phase:"update",state:y(v,4),previousState:y(p,4),diff:T?y(T,3):void 0,action:d?y(d,3):void 0}),p=v,d=void 0});r.push(E)}}return()=>{for(let s of r)s();for(let[s,i]of c){let a=n[s];a&&(a.dispatch=i)}}}function Ce(e){if(!e||typeof e!="object")return"unknown";let t=e;return typeof t.getState=="function"&&typeof t.setState=="function"&&typeof t.subscribe=="function"?"zustand":typeof t.dispatch=="function"&&typeof t.getState=="function"&&typeof t.subscribe=="function"?"redux":"unknown"}function K(e,t){if(!e||!t||typeof e!="object"||typeof t!="object")return null;let n={},o=e,r=t,c=new Set([...Object.keys(o),...Object.keys(r)]);for(let s of c)o[s]!==r[s]&&(n[s]={from:o[s],to:r[s]});return Object.keys(n).length>0?n:null}function B(e,t,n,o){let r={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"state",...o};if(n){let c=n(r);c&&e(c)}else e(r)}var Ie={LCP:[2500,4e3],FCP:[1800,3e3],CLS:[.1,.25],TTFB:[800,1800],FID:[100,300],INP:[200,500]};function Le(e,t){let[n,o]=Ie[e]??[1/0,1/0];return t<=n?"good":t<=o?"needs-improvement":"poor"}var F=null;function J(e,t,n){if(F)for(let i of F)try{i.disconnect()}catch{}let o=[];F=o;let r=(i,a,u)=>{let d={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"performance",metricName:i,value:Math.round(a*100)/100,rating:Le(i,a),element:u};if(n?.beforeSend){let l=n.beforeSend(d);l&&e(l)}else e(d)};N(o,"largest-contentful-paint",i=>{let a=i[i.length-1];if(!a)return;let u=a.element;r("LCP",a.startTime,u?.tagName?.toLowerCase())}),N(o,"paint",i=>{for(let a of i)a.name==="first-contentful-paint"&&r("FCP",a.startTime)});let c=0;N(o,"layout-shift",i=>{for(let a of i){let u=a;!u.hadRecentInput&&u.value&&(c+=u.value,r("CLS",c))}}),N(o,"first-input",i=>{let a=i[0];if(!a)return;let u=a;u.processingStart&&r("FID",u.processingStart-a.startTime)}),N(o,"navigation",i=>{let a=i[0];a&&r("TTFB",a.responseStart-a.requestStart)});let s=0;return N(o,"event",i=>{for(let a of i)a.duration>s&&(s=a.duration,r("INP",s))},{durationThreshold:16}),()=>{for(let i of o)try{i.disconnect()}catch{}F===o&&(F=null)}}function N(e,t,n,o){try{let r=new PerformanceObserver(c=>{n(c.getEntries())});r.observe({type:t,buffered:!0,...o}),e.push(r)}catch{}}var Oe=0,Ne=1,te=1e4,Z=100,De=6e4,ee=500;function ne(e,t,n){let o=new Map,r=n?.snapshotIntervalMs??5e3,c=null,s=He();if(!s)return()=>{};let i=s.onCommitFiberRoot;return s._runtimescope_original=i,s.onCommitFiberRoot=(a,u)=>{if(i)try{i(a,u)}catch{}u.current&&j(u.current,o)},c=setInterval(()=>{Be(o,e,t,n?.beforeSend)},r),()=>{c&&(clearInterval(c),c=null),s&&(s._runtimescope_original?s.onCommitFiberRoot=s._runtimescope_original:delete s.onCommitFiberRoot,delete s._runtimescope_original)}}function He(){if(typeof window>"u")return null;let e=window;return e.__REACT_DEVTOOLS_GLOBAL_HOOK__||(e.__REACT_DEVTOOLS_GLOBAL_HOOK__={}),e.__REACT_DEVTOOLS_GLOBAL_HOOK__}function j(e,t){Fe(e,t),e.child&&j(e.child,t),e.sibling&&j(e.sibling,t)}function Fe(e,t){if(e.tag!==Oe&&e.tag!==Ne)return;let n=Me(e);if(!n)return;let o=Date.now(),r=e.alternate===null,c=e.actualDuration??0,s=Ae(e,r),i=t.get(n);i||(i={renderCount:0,totalDuration:0,lastRenderTime:0,lastRenderPhase:"mount",lastRenderCause:"unknown",renderTimestamps:[]},t.set(n,i)),i.renderCount++,i.totalDuration+=c,i.lastRenderTime=o,i.lastRenderPhase=r?"mount":"update",i.lastRenderCause=s??"unknown",i.renderTimestamps.push(o),i.renderTimestamps.length>Z&&(i.renderTimestamps=i.renderTimestamps.slice(-Z))}function Me(e){if(e.type&&typeof e.type!="string")return e.type.displayName||e.type.name||void 0}function Ae(e,t){return t?"props":e.alternate?e.memoizedProps!==e.alternate.memoizedProps?"props":e.memoizedState!==e.alternate.memoizedState?"state":"parent":"unknown"}function xe(e){if(e.length<2)return 0;let t=Date.now(),n=t-te,o=e.filter(c=>c>=n);if(o.length<2)return 0;let r=t-o[0];return r===0?0:o.length/(r/1e3)}function Be(e,t,n,o){if(e.size===0)return;let r=[],c=[],s=0;for(let[u,d]of e){let l=xe(d.renderTimestamps),p=l>4||d.renderCount>20;r.push({componentName:u,renderCount:d.renderCount,totalDuration:Math.round(d.totalDuration*100)/100,avgDuration:d.renderCount>0?Math.round(d.totalDuration/d.renderCount*100)/100:0,lastRenderPhase:d.lastRenderPhase,lastRenderCause:d.lastRenderCause,renderVelocity:Math.round(l*100)/100,suspicious:p}),p&&c.push(u),s+=d.renderCount}r.sort((u,d)=>d.renderCount-u.renderCount);let i={eventId:m(),sessionId:n,timestamp:Date.now(),eventType:"render",profiles:r,snapshotWindowMs:te,totalRenders:s,suspiciousComponents:c};if(o){let u=o(i);u&&t(u)}else t(i);let a=Date.now();for(let[u,d]of e)a-d.lastRenderTime>De?e.delete(u):(d.renderCount=0,d.totalDuration=0);if(e.size>ee){let u=[...e.entries()].sort((l,p)=>l[1].lastRenderTime-p[1].lastRenderTime),d=e.size-ee;for(let l=0;l<d;l++)e.delete(u[l][0])}}function re(e,t,n){let o=c=>{let s,i,a;if(c instanceof ErrorEvent)s=c.message||"Uncaught error",i=c.error?.stack,a=c.filename?`${c.filename}:${c.lineno}:${c.colno}`:void 0;else{let d=c.target;if(d&&d!==window){let l=d.tagName?.toLowerCase()??"unknown",p=d.src??d.src??d.href??"unknown";s=`Failed to load resource: <${l}> ${p}`}else return}let u={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"console",level:"error",message:`[Uncaught] ${s}`,args:[y(s,3)],stackTrace:i,sourceFile:a};if(n){let d=n(u);d&&e(d)}else e(u)},r=c=>{let s=c.reason,i,a;if(s instanceof Error)i=s.message,a=s.stack;else if(typeof s=="string")i=s;else try{i=JSON.stringify(s)}catch{i=String(s)}let u={eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"console",level:"error",message:`[Unhandled Rejection] ${i}`,args:[y(s,3)],stackTrace:a,sourceFile:void 0};if(n){let d=n(u);d&&e(d)}else e(u)};return window.addEventListener("error",o,!0),window.addEventListener("unhandledrejection",r),()=>{window.removeEventListener("error",o,!0),window.removeEventListener("unhandledrejection",r)}}function oe(e,t){let n=window.location.href;function o(a,u){let d=n;d!==a&&(n=a,e({eventId:m(),sessionId:t,timestamp:Date.now(),eventType:"navigation",from:d,to:a,trigger:u}))}let r=history.pushState.bind(history),c=history.replaceState.bind(history);history.pushState=function(...a){r(...a),o(window.location.href,"pushState")},history.replaceState=function(...a){c(...a),o(window.location.href,"replaceState")};let s=()=>o(window.location.href,"popstate");window.addEventListener("popstate",s);let i=()=>o(window.location.href,"hashchange");return window.addEventListener("hashchange",i),()=>{history.pushState=r,history.replaceState=c,window.removeEventListener("popstate",s),window.removeEventListener("hashchange",i)}}var X="0.7.2",U=console.debug.bind(console),P=Symbol.for("__runtimescope_originals__");function Pe(){let e=globalThis;return e[P]||(e[P]={fetch:window.fetch,xhrOpen:XMLHttpRequest.prototype.open,xhrSend:XMLHttpRequest.prototype.send,xhrSetRequestHeader:XMLHttpRequest.prototype.setRequestHeader,consoleMethods:{log:console.log.bind(console),warn:console.warn.bind(console),error:console.error.bind(console),info:console.info.bind(console),debug:console.debug.bind(console),trace:console.trace.bind(console)}}),e[P]}var C=class{static get sessionId(){return this._sessionId}static get isConnected(){return this._state==="started"}static connect(t={}){if(t.enabled===!1)return;this._state==="started"&&(U("[RuntimeScope] Already connected \u2014 disconnecting before re-init"),this.disconnect());let n={serverUrl:t.serverUrl??t.endpoint??"ws://localhost:9090",appName:t.appName??"unknown",captureNetwork:t.captureNetwork??!0,captureConsole:t.captureConsole??!0,captureXhr:t.captureXhr??!0,captureBody:t.captureBody??!1,maxBodySize:t.maxBodySize??65536,capturePerformance:t.capturePerformance??!0,captureRenders:t.captureRenders??!0,captureNavigation:t.captureNavigation??!0,stores:t.stores??{},beforeSend:t.beforeSend,redactHeaders:t.redactHeaders??["authorization","cookie"],batchSize:t.batchSize??50,flushIntervalMs:t.flushIntervalMs??100};Pe(),this._sessionId=$(),this._state="started",this.transport=new A({serverUrl:n.serverUrl,appName:n.appName,sessionId:this._sessionId,sdkVersion:X,authToken:t.authToken,batchSize:n.batchSize,flushIntervalMs:n.flushIntervalMs}),this.transport.connect(),U(`[RuntimeScope] SDK v${X} initializing \u2014 app: ${n.appName}, server: ${n.serverUrl}`);let o=s=>this.transport?.send(s);o({eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:n.appName,connectedAt:Date.now(),sdkVersion:X,buildMeta:t.buildMeta});let r=this._sessionId;this.transport.onCommand(s=>{this.handleCommand(s,o,r)}),n.captureNetwork&&this.restoreFns.push(W(o,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureXhr&&this.restoreFns.push(Q(o,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureConsole&&(this.restoreFns.push(G(o,this._sessionId,n.beforeSend)),this.restoreFns.push(re(o,this._sessionId,n.beforeSend))),Object.keys(n.stores).length>0&&this.restoreFns.push(Y(o,this._sessionId,n.stores,{beforeSend:n.beforeSend})),n.capturePerformance&&this.restoreFns.push(J(o,this._sessionId,{beforeSend:n.beforeSend})),n.captureRenders&&this.restoreFns.push(ne(o,this._sessionId,{beforeSend:n.beforeSend})),n.captureNavigation&&this.restoreFns.push(oe(o,this._sessionId));let c=[n.captureNetwork&&"fetch",n.captureXhr&&"xhr",n.captureConsole&&"console, errors",Object.keys(n.stores).length>0&&"state",n.capturePerformance&&"performance",n.captureRenders&&"renders",n.captureNavigation&&"navigation"].filter(Boolean);U(`[RuntimeScope] Interceptors active \u2014 ${c.join(", ")}`)}static handleCommand(t,n,o){if(t.command==="capture_dom_snapshot"){let r=t.params?.maxSize??5e5,c=document.documentElement.outerHTML,s=c.length>r;s&&(c=c.slice(0,r));let i={eventId:m(),sessionId:o,timestamp:Date.now(),eventType:"dom_snapshot",html:c,url:window.location.href,viewport:{width:window.innerWidth,height:window.innerHeight},scrollPosition:{x:window.scrollX,y:window.scrollY},elementCount:document.querySelectorAll("*").length,truncated:s};n(i),this.transport?.sendCommandResponse(t.requestId,t.command,i)}else this.transport?.sendCommandResponse(t.requestId,t.command,{error:"Unknown command"})}static init(t={}){return this.connect(t)}static track(t,n){if(!this.transport||!this._sessionId)return;let o={eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"custom",name:t,properties:n};this.transport.send(o)}static disconnect(){for(let n of this.restoreFns)try{n()}catch{}this.restoreFns=[];let t=globalThis[P];if(t){window.fetch!==t.fetch&&(window.fetch=t.fetch);for(let[n,o]of Object.entries(t.consoleMethods)){let r=console;r[n]!==o&&(r[n]=o)}}this.transport?.disconnect(),this.transport=null,this._sessionId=null,this._state="stopped"}};f(C,"transport",null),f(C,"restoreFns",[]),f(C,"_sessionId",null),f(C,"_state","stopped");var qe=C;0&&(module.exports={RuntimeScope});
4
4
  if(typeof RuntimeScope!=="undefined"&&RuntimeScope.RuntimeScope){RuntimeScope=RuntimeScope.RuntimeScope;}
5
5
  //# sourceMappingURL=index.cjs.map