@runtimescope/sdk 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.global.js +5 -4
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -143,6 +143,9 @@ interface UserContext {
|
|
|
143
143
|
[key: string]: unknown;
|
|
144
144
|
}
|
|
145
145
|
interface RuntimeScopeConfig {
|
|
146
|
+
/** DSN connection string — replaces separate projectId + endpoint + appName fields.
|
|
147
|
+
* Format: runtimescope://proj_abc123@localhost:6768/my-app */
|
|
148
|
+
dsn?: string;
|
|
146
149
|
enabled?: boolean;
|
|
147
150
|
serverUrl?: string;
|
|
148
151
|
/** Alias for `serverUrl` — used by script-tag snippets */
|
|
@@ -181,6 +184,35 @@ interface RuntimeScopeConfig {
|
|
|
181
184
|
user?: UserContext;
|
|
182
185
|
}
|
|
183
186
|
|
|
187
|
+
/**
|
|
188
|
+
* DSN (Data Source Name) parser for RuntimeScope browser SDK.
|
|
189
|
+
*
|
|
190
|
+
* Format: runtimescope://proj_abc123[:token]@localhost:6768/my-app
|
|
191
|
+
* - runtimescope:// or runtimescopes:// (TLS)
|
|
192
|
+
* - projectId before @ (or before : if a token is included)
|
|
193
|
+
* - optional bearer token between : and @ (workspace-scoped API key)
|
|
194
|
+
* - host:port after @ (HTTP API port, default 6768)
|
|
195
|
+
* - WS port = HTTP port - 1 (so 6768 → ws on 6767)
|
|
196
|
+
* - Optional /appName path
|
|
197
|
+
*/
|
|
198
|
+
interface ParsedDsn {
|
|
199
|
+
projectId: string;
|
|
200
|
+
authToken?: string;
|
|
201
|
+
wsEndpoint: string;
|
|
202
|
+
httpEndpoint: string;
|
|
203
|
+
appName?: string;
|
|
204
|
+
tls: boolean;
|
|
205
|
+
}
|
|
206
|
+
declare function parseDsn(dsn: string): ParsedDsn;
|
|
207
|
+
declare function buildDsn(opts: {
|
|
208
|
+
projectId: string;
|
|
209
|
+
authToken?: string;
|
|
210
|
+
host?: string;
|
|
211
|
+
port?: number;
|
|
212
|
+
appName?: string;
|
|
213
|
+
tls?: boolean;
|
|
214
|
+
}): string;
|
|
215
|
+
|
|
184
216
|
declare class RuntimeScope {
|
|
185
217
|
private static transport;
|
|
186
218
|
private static restoreFns;
|
|
@@ -222,4 +254,4 @@ declare class RuntimeScope {
|
|
|
222
254
|
static disconnect(): void;
|
|
223
255
|
}
|
|
224
256
|
|
|
225
|
-
export { type BuildMeta, type ConsoleEvent, type CustomEvent, type DomSnapshotEvent, type NavigationEvent, type NetworkEvent, type PerformanceEvent, type RenderEvent, type RuntimeEvent, RuntimeScope, type RuntimeScopeConfig, type SessionEvent, type StateEvent, type UIInteractionEvent, type UserContext, RuntimeScope as default };
|
|
257
|
+
export { type BuildMeta, type ConsoleEvent, type CustomEvent, type DomSnapshotEvent, type NavigationEvent, type NetworkEvent, type ParsedDsn, type PerformanceEvent, type RenderEvent, type RuntimeEvent, RuntimeScope, type RuntimeScopeConfig, type SessionEvent, type StateEvent, type UIInteractionEvent, type UserContext, buildDsn, RuntimeScope as default, parseDsn };
|
package/dist/index.d.ts
CHANGED
|
@@ -143,6 +143,9 @@ interface UserContext {
|
|
|
143
143
|
[key: string]: unknown;
|
|
144
144
|
}
|
|
145
145
|
interface RuntimeScopeConfig {
|
|
146
|
+
/** DSN connection string — replaces separate projectId + endpoint + appName fields.
|
|
147
|
+
* Format: runtimescope://proj_abc123@localhost:6768/my-app */
|
|
148
|
+
dsn?: string;
|
|
146
149
|
enabled?: boolean;
|
|
147
150
|
serverUrl?: string;
|
|
148
151
|
/** Alias for `serverUrl` — used by script-tag snippets */
|
|
@@ -181,6 +184,35 @@ interface RuntimeScopeConfig {
|
|
|
181
184
|
user?: UserContext;
|
|
182
185
|
}
|
|
183
186
|
|
|
187
|
+
/**
|
|
188
|
+
* DSN (Data Source Name) parser for RuntimeScope browser SDK.
|
|
189
|
+
*
|
|
190
|
+
* Format: runtimescope://proj_abc123[:token]@localhost:6768/my-app
|
|
191
|
+
* - runtimescope:// or runtimescopes:// (TLS)
|
|
192
|
+
* - projectId before @ (or before : if a token is included)
|
|
193
|
+
* - optional bearer token between : and @ (workspace-scoped API key)
|
|
194
|
+
* - host:port after @ (HTTP API port, default 6768)
|
|
195
|
+
* - WS port = HTTP port - 1 (so 6768 → ws on 6767)
|
|
196
|
+
* - Optional /appName path
|
|
197
|
+
*/
|
|
198
|
+
interface ParsedDsn {
|
|
199
|
+
projectId: string;
|
|
200
|
+
authToken?: string;
|
|
201
|
+
wsEndpoint: string;
|
|
202
|
+
httpEndpoint: string;
|
|
203
|
+
appName?: string;
|
|
204
|
+
tls: boolean;
|
|
205
|
+
}
|
|
206
|
+
declare function parseDsn(dsn: string): ParsedDsn;
|
|
207
|
+
declare function buildDsn(opts: {
|
|
208
|
+
projectId: string;
|
|
209
|
+
authToken?: string;
|
|
210
|
+
host?: string;
|
|
211
|
+
port?: number;
|
|
212
|
+
appName?: string;
|
|
213
|
+
tls?: boolean;
|
|
214
|
+
}): string;
|
|
215
|
+
|
|
184
216
|
declare class RuntimeScope {
|
|
185
217
|
private static transport;
|
|
186
218
|
private static restoreFns;
|
|
@@ -222,4 +254,4 @@ declare class RuntimeScope {
|
|
|
222
254
|
static disconnect(): void;
|
|
223
255
|
}
|
|
224
256
|
|
|
225
|
-
export { type BuildMeta, type ConsoleEvent, type CustomEvent, type DomSnapshotEvent, type NavigationEvent, type NetworkEvent, type PerformanceEvent, type RenderEvent, type RuntimeEvent, RuntimeScope, type RuntimeScopeConfig, type SessionEvent, type StateEvent, type UIInteractionEvent, type UserContext, RuntimeScope as default };
|
|
257
|
+
export { type BuildMeta, type ConsoleEvent, type CustomEvent, type DomSnapshotEvent, type NavigationEvent, type NetworkEvent, type ParsedDsn, type PerformanceEvent, type RenderEvent, type RuntimeEvent, RuntimeScope, type RuntimeScopeConfig, type SessionEvent, type StateEvent, type UIInteractionEvent, type UserContext, buildDsn, RuntimeScope as default, parseDsn };
|
package/dist/index.global.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
"use strict";var RuntimeScope=(()=>{var M=Object.defineProperty;var
|
|
2
|
-
`).slice(3);for(let
|
|
1
|
+
"use strict";var RuntimeScope=(()=>{var M=Object.defineProperty;var pe=Object.getOwnPropertyDescriptor;var le=Object.getOwnPropertyNames;var fe=Object.prototype.hasOwnProperty;var me=(t,e,r)=>e in t?M(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var he=(t,e)=>{for(var r in e)M(t,r,{get:e[r],enumerable:!0})},ge=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of le(e))!fe.call(t,n)&&n!==r&&M(t,n,{get:()=>e[n],enumerable:!(o=pe(e,n))||o.enumerable});return t};var ve=t=>ge(M({},"__esModule",{value:!0}),t);var f=(t,e,r)=>me(t,typeof e!="symbol"?e+"":e,r);var Ve={};he(Ve,{RuntimeScope:()=>b,buildDsn:()=>ue,default:()=>We,parseDsn:()=>j});var D=console.debug.bind(console),S=class S{constructor(e){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=e}isConnected(){return this.connected}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`)},S.CONNECTION_WARNING_DELAY),typeof document<"u"&&(this.visibilityHandler=()=>{document.visibilityState==="visible"&&!this.connected&&!this.stopped&&(D("[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&&(D("[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=e=>{try{let r=JSON.parse(String(e.data));r.type==="command"&&r.payload&&this.commandHandler&&this.commandHandler(r.payload),r.type==="__server_restart"&&(D("[RuntimeScope] Server restart notice received \u2014 will reconnect immediately"),this.resetReconnectState()),r.type==="error"&&r.payload?.code==="AUTH_FAILED"&&(D("[RuntimeScope] Authentication failed \u2014 stopping reconnection"),this.stopped=!0)}catch{}},this.ws.onopen=()=>{if(this.connected=!0,this.hasEverConnected=!0,this.resetReconnectState(),D(`[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}:{},...this.config.projectId?{projectId:this.config.projectId}:{}},timestamp:Date.now(),sessionId:this.config.sessionId}),this.offlineQueue.length>0){let e=this.offlineQueue.splice(0);for(let r of e)this.batch.push(r);this.flush()}this.flushTimer=setInterval(()=>this.flush(),this.config.flushIntervalMs)},this.ws.onclose=()=>{this.connected=!1,this.clearFlushTimer(),this.stopped||(D("[RuntimeScope] Disconnected, will reconnect..."),this.scheduleReconnect())},this.ws.onerror=()=>{D(`[RuntimeScope] WebSocket error connecting to ${this.config.serverUrl}`)}}}send(e){this.connected?(this.batch.push(e),this.batch.length>=this.config.batchSize&&this.flush()):this.offlineQueue.length<S.MAX_OFFLINE_QUEUE&&this.offlineQueue.push(e)}flush(){if(this.batch.length===0||!this.connected||!this.ws)return;let e=this.batch.splice(0);this.sendRaw({type:"event",payload:{events:e},timestamp:Date.now(),sessionId:this.config.sessionId})}sendRaw(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)try{this.ws.send(JSON.stringify(e))}catch{}}scheduleReconnect(){if(this.stopped||this.reconnectTimer)return;this.reconnectAttempt++;let e;if(this.reconnectAttempt<=S.FAST_RETRY_COUNT)e=S.FAST_RETRY_DELAY;else{let r=this.reconnectDelay*.25*(Math.random()*2-1);e=Math.min(this.reconnectDelay+r,S.MAX_RECONNECT_DELAY),this.reconnectDelay=Math.min(this.reconnectDelay*2,S.MAX_RECONNECT_DELAY)}this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.doConnect()},e)}resetReconnectState(){this.reconnectDelay=500,this.reconnectAttempt=0}clearFlushTimer(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null)}onCommand(e){this.commandHandler=e}sendCommandResponse(e,r,o){this.sendRaw({type:"command_response",requestId:e,command:r,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(S,"MAX_OFFLINE_QUEUE",1e3),f(S,"MAX_RECONNECT_DELAY",3e4),f(S,"CONNECTION_WARNING_DELAY",1e4),f(S,"FAST_RETRY_COUNT",3),f(S,"FAST_RETRY_DELAY",500);var P=S;function m(){let t=new Uint8Array(8);return crypto.getRandomValues(t),Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")}function W(){let t=new Uint8Array(16);return crypto.getRandomValues(t),Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")}var we=1e3,ye=1e4,Re=5e3,L=new Map,H=null,B=0;function Ee(){B++,!H&&(H=setInterval(()=>{let t=Date.now()-ye;for(let[e,r]of L)r<t&&L.delete(e)},Re))}function Se(){B--,B<=0&&H&&(clearInterval(H),H=null,B=0)}function V(t,e,r,o){let n=window.fetch,u=new Set(r.map(i=>i.toLowerCase())),a=o?.captureBody??!1,s=o?.maxBodySize??65536;return Ee(),window.fetch=async function(i,d){let l=performance.now(),c=typeof i=="string"?i:i instanceof URL?i.href:i.url,p=(d?.method??"GET").toUpperCase(),h=be(d?.headers,u),g=Te(d?.body),_=ke(d?.body),R;a&&d?.body&&(R=Ie(d.body,s));let T=`${p}:${c}:${l}`;if(L.size>=we){let v=L.keys().next().value;v!==void 0&&L.delete(v)}L.set(T,Date.now());try{let v=await n.call(window,i,d),k=performance.now()-l,x=parseInt(v.headers.get("content-length")||"0",10),C=_e(v.headers,u),w;if(a)try{let O=await v.clone().text();w=O.length>s?O.slice(0,s):O}catch{}let E={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"network",url:c,method:p,status:v.status,requestHeaders:h,responseHeaders:C,requestBodySize:g,responseBodySize:x,duration:k,ttfb:k,graphqlOperation:_,requestBody:R,responseBody:w,source:"fetch"};if(o?.beforeSend){let I=o.beforeSend(E);I&&t(I)}else t(E);return v}catch(v){let k=performance.now()-l,x="error",C="";v instanceof DOMException&&v.name==="AbortError"?(x="abort",C="Request aborted"):v instanceof Error&&(C=v.message);let w={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"network",url:c,method:p,status:0,requestHeaders:h,responseHeaders:{},requestBodySize:g,responseBodySize:0,duration:k,ttfb:0,graphqlOperation:_,requestBody:R,errorPhase:x,errorMessage:C,source:"fetch"};if(o?.beforeSend){let E=o.beforeSend(w);E&&t(E)}else t(w);throw v}},()=>{window.fetch=n,Se()}}function be(t,e){let r={};if(!t)return r;if(t instanceof Headers)t.forEach((o,n)=>{r[n]=e.has(n.toLowerCase())?"[REDACTED]":o});else if(Array.isArray(t))for(let[o,n]of t)r[o]=e.has(o.toLowerCase())?"[REDACTED]":n;else for(let[o,n]of Object.entries(t))r[o]=e.has(o.toLowerCase())?"[REDACTED]":n;return r}function _e(t,e){let r={};return t.forEach((o,n)=>{r[n]=e.has(n.toLowerCase())?"[REDACTED]":o}),r}function Te(t){return t?typeof t=="string"?new Blob([t]).size:t instanceof Blob?t.size:t instanceof ArrayBuffer||ArrayBuffer.isView(t)?t.byteLength:t instanceof FormData?0:t instanceof URLSearchParams?new Blob([t.toString()]).size:0:0}function Ie(t,e){if(t){if(typeof t=="string")return t.length>e?t.slice(0,e):t;if(t instanceof URLSearchParams){let r=t.toString();return r.length>e?r.slice(0,e):r}if(t instanceof FormData)return"[FormData]";if(t instanceof Blob)return`[Blob ${t.size} bytes]`;if(t instanceof ArrayBuffer)return`[ArrayBuffer ${t.byteLength} bytes]`;if(ArrayBuffer.isView(t))return`[TypedArray ${t.byteLength} bytes]`}}function ke(t){if(!(!t||typeof t!="string"))try{let e=JSON.parse(t);if(typeof e.query=="string"){let r=e.query.trim(),o="query";r.startsWith("mutation")?o="mutation":r.startsWith("subscription")&&(o="subscription");let n=e.operationName||Ce(r)||"anonymous";return{type:o,name:n}}}catch{}}function Ce(t){return t.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function y(t,e=5){let r=new WeakSet;function o(n,u){if(u>e)return"[max depth]";if(n==null)return n;if(typeof n=="function")return`[Function: ${n.name||"anonymous"}]`;if(typeof n=="symbol"||typeof n=="bigint")return n.toString();if(typeof n!="object")return n;if(n instanceof Error)return{name:n.name,message:n.message,stack:n.stack};if(n instanceof Date)return n.toISOString();if(n instanceof RegExp)return n.toString();if(r.has(n))return"[Circular]";if(r.add(n),Array.isArray(n))return n.map(d=>o(d,u+1));let a={},s;try{s=Object.keys(n)}catch{return"[Object]"}let i=50;for(let d=0;d<Math.min(s.length,i);d++)try{a[s[d]]=o(n[s[d]],u+1)}catch{a[s[d]]="[Error accessing property]"}return s.length>i&&(a["..."]=`${s.length-i} more keys`),a}return o(t,0)}var G=["log","warn","error","info","debug","trace"];function Q(t){if(!t)return;let e=t.split(`
|
|
2
|
+
`).slice(3);for(let r of e){let o=r.match(/(?:at\s+)?(?:.*?\()?(.+?):(\d+):(\d+)\)?/);if(o&&!o[1].includes("interceptor")&&!o[1].includes("runtimescope"))return`${o[1]}:${o[2]}`}}function Y(t,e,r){let o={},n=new Map,u=new Map;for(let p of G)o[p]=console[p].bind(console),console[p]=(...h)=>{let g=new Error().stack,_=h.map(T=>typeof T=="string"?T:K(T)).join(" "),R={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:p,message:_,args:h.map(T=>y(T,3)),stackTrace:p==="error"||p==="trace"?g?.split(`
|
|
3
3
|
`).slice(2).join(`
|
|
4
|
-
`):void 0,sourceFile:
|
|
4
|
+
`):void 0,sourceFile:Q(g),source:"browser"};if(r){let T=r(R);T&&t(T)}else t(R);o[p](...h)};let a=console.assert?.bind(console);a&&(console.assert=(p,...h)=>{if(!p){let g=new Error().stack,_=`Assertion failed: ${h.map(R=>typeof R=="string"?R:K(R)).join(" ")}`;t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"error",message:_,args:h.map(R=>y(R,3)),stackTrace:g?.split(`
|
|
5
5
|
`).slice(2).join(`
|
|
6
|
-
`),sourceFile:G(g),source:"browser"})}i(d,...h)});let s=console.time?.bind(console),a=console.timeEnd?.bind(console);s&&(console.time=(d="default")=>{r.set(d,performance.now()),s(d)}),a&&(console.timeEnd=(d="default")=>{let h=r.get(d);if(h!==void 0){let g=performance.now()-h;r.delete(d),t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`${d}: ${g.toFixed(2)}ms`,args:[{label:d,duration:g}],source:"browser"})}a(d)});let c=console.count?.bind(console),l=console.countReset?.bind(console);c&&(console.count=(d="default")=>{let h=(u.get(d)??0)+1;u.set(d,h),t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`${d}: ${h}`,args:[{label:d,count:h}],source:"browser"}),c(d)}),l&&(console.countReset=(d="default")=>{u.delete(d),l(d)});let p=console.table?.bind(console);return p&&(console.table=(d,h)=>{t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`[table] ${Array.isArray(d)?`${d.length} rows`:typeof d}`,args:[y(d,3)],source:"browser"}),p(d,h)}),()=>{for(let d of V)console[d]=n[d];i&&(console.assert=i),s&&(console.time=s),a&&(console.timeEnd=a),c&&(console.count=c),l&&(console.countReset=l),p&&(console.table=p)}}function Q(t){try{return JSON.stringify(t)}catch{return String(t)}}function Y(t,e,o,n){let r=new Set(o.map(p=>p.toLowerCase())),u=n?.captureBody??!1,i=n?.maxBodySize??65536,s=new AbortController,a=XMLHttpRequest.prototype.open,c=XMLHttpRequest.prototype.setRequestHeader,l=XMLHttpRequest.prototype.send;return XMLHttpRequest.prototype.open=function(p,d){return this.__rs_method=p.toUpperCase(),this.__rs_url=typeof d=="string"?d:d.href,this.__rs_headers={},a.apply(this,arguments)},XMLHttpRequest.prototype.setRequestHeader=function(p,d){return this.__rs_headers&&(this.__rs_headers[p.toLowerCase()]=r.has(p.toLowerCase())?"[REDACTED]":d),c.call(this,p,d)},XMLHttpRequest.prototype.send=function(p){let d=this.__rs_method??"GET",h=this.__rs_url??"",g=`${d}:${h}`,_=!1;for(let w of L.keys())if(w.startsWith(g)){_=!0;break}if(_)return this.__rs_fetchIntercepted=!0,l.call(this,p);let R={...this.__rs_headers??{}},T=performance.now(),v,k=0;if(p){if(typeof p=="string")k=new Blob([p]).size,u&&(v=p.length>i?p.slice(0,i):p);else if(p instanceof Blob)k=p.size,u&&(v=`[Blob ${p.size} bytes]`);else if(p instanceof ArrayBuffer)k=p.byteLength,u&&(v=`[ArrayBuffer ${p.byteLength} bytes]`);else if(p instanceof FormData)u&&(v="[FormData]");else if(p instanceof URLSearchParams){let w=p.toString();k=new Blob([w]).size,u&&(v=w.length>i?w.slice(0,i):w)}}let D=ke(p),C=w=>{let E={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"network",url:h,method:d,status:0,requestHeaders:R,responseHeaders:{},requestBodySize:k,responseBodySize:0,duration:0,ttfb:0,graphqlOperation:D,requestBody:v,source:"xhr",...w};if(n?.beforeSend){let I=n.beforeSend(E);I&&t(I)}else t(E)};return this.addEventListener("loadend",()=>{let w=performance.now()-T;if(this.status>0){let E=Ie(this.getAllResponseHeaders(),r),I=parseInt(this.getResponseHeader("content-length")||"0",10),N;if(u&&(this.responseType===""||this.responseType==="text"))try{let $=this.responseText;N=$.length>i?$.slice(0,i):$}catch{}C({status:this.status,responseHeaders:E,responseBodySize:I,responseBody:N,duration:w,ttfb:w})}else{let E="error",I="Network error";this.readyState===0||this.status,this.timeout>0&&w>=this.timeout&&(E="timeout",I=`Request timed out after ${this.timeout}ms`),C({duration:w,errorPhase:E,errorMessage:I})}},{once:!0,signal:s.signal}),l.call(this,p)},()=>{s.abort(),XMLHttpRequest.prototype.open=a,XMLHttpRequest.prototype.setRequestHeader=c,XMLHttpRequest.prototype.send=l}}function Ie(t,e){let o={};if(!t)return o;for(let n of t.trim().split(/[\r\n]+/)){let r=n.indexOf(":");if(r===-1)continue;let u=n.slice(0,r).trim().toLowerCase(),i=n.slice(r+1).trim();o[u]=e.has(u)?"[REDACTED]":i}return o}function ke(t){if(!(!t||typeof t!="string"))try{let e=JSON.parse(t);if(typeof e.query=="string"){let o=e.query.trim(),n="query";o.startsWith("mutation")?n="mutation":o.startsWith("subscription")&&(n="subscription");let r=e.operationName||Ce(o)||"anonymous";return{type:n,name:r}}}catch{}}function Ce(t){return t.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function Z(t,e,o,n){let r=[],u=new Map;for(let[i,s]of Object.entries(o)){let a=Le(s);if(a==="zustand"){let c=s;q(t,e,n?.beforeSend,{storeId:i,library:a,phase:"init",state:y(c.getState(),4)});let l=c.subscribe((p,d)=>{let h=J(d,p);q(t,e,n?.beforeSend,{storeId:i,library:a,phase:"update",state:y(p,4),previousState:y(d,4),diff:h?y(h,3):void 0})});r.push(l)}else if(a==="redux"){let c=s;q(t,e,n?.beforeSend,{storeId:i,library:a,phase:"init",state:y(c.getState(),4)});let l,p=c.dispatch.bind(c);u.set(i,p),c.dispatch=g=>(g&&typeof g=="object"&&"type"in g&&(l={type:String(g.type),payload:g.payload}),p(g));let d=c.getState(),h=c.subscribe(()=>{let g=c.getState(),_=J(d,g);q(t,e,n?.beforeSend,{storeId:i,library:a,phase:"update",state:y(g,4),previousState:y(d,4),diff:_?y(_,3):void 0,action:l?y(l,3):void 0}),d=g,l=void 0});r.push(h)}}return()=>{for(let i of r)i();for(let[i,s]of u){let a=o[i];a&&(a.dispatch=s)}}}function Le(t){if(!t||typeof t!="object")return"unknown";let e=t;return typeof e.getState=="function"&&typeof e.setState=="function"&&typeof e.subscribe=="function"?"zustand":typeof e.dispatch=="function"&&typeof e.getState=="function"&&typeof e.subscribe=="function"?"redux":"unknown"}function J(t,e){if(!t||!e||typeof t!="object"||typeof e!="object")return null;let o={},n=t,r=e,u=new Set([...Object.keys(n),...Object.keys(r)]);for(let i of u)n[i]!==r[i]&&(o[i]={from:n[i],to:r[i]});return Object.keys(o).length>0?o:null}function q(t,e,o,n){let r={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"state",...n};if(o){let u=o(r);u&&t(u)}else t(r)}var Oe={LCP:[2500,4e3],FCP:[1800,3e3],CLS:[.1,.25],TTFB:[800,1800],FID:[100,300],INP:[200,500]};function De(t,e){let[o,n]=Oe[t]??[1/0,1/0];return e<=o?"good":e<=n?"needs-improvement":"poor"}var F=null;function ee(t,e,o){if(F)for(let s of F)try{s.disconnect()}catch{}let n=[];F=n;let r=(s,a,c)=>{let l={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"performance",metricName:s,value:Math.round(a*100)/100,rating:De(s,a),element:c};if(o?.beforeSend){let p=o.beforeSend(l);p&&t(p)}else t(l)};x(n,"largest-contentful-paint",s=>{let a=s[s.length-1];if(!a)return;let c=a.element;r("LCP",a.startTime,c?.tagName?.toLowerCase())}),x(n,"paint",s=>{for(let a of s)a.name==="first-contentful-paint"&&r("FCP",a.startTime)});let u=0;x(n,"layout-shift",s=>{for(let a of s){let c=a;!c.hadRecentInput&&c.value&&(u+=c.value,r("CLS",u))}}),x(n,"first-input",s=>{let a=s[0];if(!a)return;let c=a;c.processingStart&&r("FID",c.processingStart-a.startTime)}),x(n,"navigation",s=>{let a=s[0];a&&r("TTFB",a.responseStart-a.requestStart)});let i=0;return x(n,"event",s=>{for(let a of s)a.duration>i&&(i=a.duration,r("INP",i))},{durationThreshold:16}),()=>{for(let s of n)try{s.disconnect()}catch{}F===n&&(F=null)}}function x(t,e,o,n){try{let r=new PerformanceObserver(u=>{o(u.getEntries())});r.observe({type:e,buffered:!0,...n}),t.push(r)}catch{}}var Ne=0,xe=1,re=1e4,te=100,Ae=6e4,ne=500;function oe(t,e,o){let n=new Map,r=o?.snapshotIntervalMs??5e3,u=null,i=He();if(!i)return()=>{};let s=i.onCommitFiberRoot;return i._runtimescope_original=s,i.onCommitFiberRoot=(a,c)=>{if(s)try{s(a,c)}catch{}c.current&&U(c.current,n)},u=setInterval(()=>{qe(n,t,e,o?.beforeSend)},r),()=>{u&&(clearInterval(u),u=null),i&&(i._runtimescope_original?i.onCommitFiberRoot=i._runtimescope_original:delete i.onCommitFiberRoot,delete i._runtimescope_original)}}function He(){if(typeof window>"u")return null;let t=window;return t.__REACT_DEVTOOLS_GLOBAL_HOOK__||(t.__REACT_DEVTOOLS_GLOBAL_HOOK__={}),t.__REACT_DEVTOOLS_GLOBAL_HOOK__}function U(t,e){Fe(t,e),t.child&&U(t.child,e),t.sibling&&U(t.sibling,e)}function Fe(t,e){if(t.tag!==Ne&&t.tag!==xe)return;let o=Me(t);if(!o)return;let n=Date.now(),r=t.alternate===null,u=t.actualDuration??0,i=Pe(t,r),s=e.get(o);s||(s={renderCount:0,totalDuration:0,lastRenderTime:0,lastRenderPhase:"mount",lastRenderCause:"unknown",renderTimestamps:[]},e.set(o,s)),s.renderCount++,s.totalDuration+=u,s.lastRenderTime=n,s.lastRenderPhase=r?"mount":"update",s.lastRenderCause=i??"unknown",s.renderTimestamps.push(n),s.renderTimestamps.length>te&&(s.renderTimestamps=s.renderTimestamps.slice(-te))}function Me(t){if(t.type&&typeof t.type!="string")return t.type.displayName||t.type.name||void 0}function Pe(t,e){return e?"props":t.alternate?t.memoizedProps!==t.alternate.memoizedProps?"props":t.memoizedState!==t.alternate.memoizedState?"state":"parent":"unknown"}function Be(t){if(t.length<2)return 0;let e=Date.now(),o=e-re,n=t.filter(u=>u>=o);if(n.length<2)return 0;let r=e-n[0];return r===0?0:n.length/(r/1e3)}function qe(t,e,o,n){if(t.size===0)return;let r=[],u=[],i=0;for(let[c,l]of t){let p=Be(l.renderTimestamps),d=p>4||l.renderCount>20;r.push({componentName:c,renderCount:l.renderCount,totalDuration:Math.round(l.totalDuration*100)/100,avgDuration:l.renderCount>0?Math.round(l.totalDuration/l.renderCount*100)/100:0,lastRenderPhase:l.lastRenderPhase,lastRenderCause:l.lastRenderCause,renderVelocity:Math.round(p*100)/100,suspicious:d}),d&&u.push(c),i+=l.renderCount}r.sort((c,l)=>l.renderCount-c.renderCount);let s={eventId:m(),sessionId:o,timestamp:Date.now(),eventType:"render",profiles:r,snapshotWindowMs:re,totalRenders:i,suspiciousComponents:u};if(n){let c=n(s);c&&e(c)}else e(s);let a=Date.now();for(let[c,l]of t)a-l.lastRenderTime>Ae?t.delete(c):(l.renderCount=0,l.totalDuration=0);if(t.size>ne){let c=[...t.entries()].sort((p,d)=>p[1].lastRenderTime-d[1].lastRenderTime),l=t.size-ne;for(let p=0;p<l;p++)t.delete(c[p][0])}}function se(t,e,o){let n=u=>{let i,s,a;if(u instanceof ErrorEvent)i=u.message||"Uncaught error",s=u.error?.stack,a=u.filename?`${u.filename}:${u.lineno}:${u.colno}`:void 0;else{let l=u.target;if(l&&l!==window){let p=l.tagName?.toLowerCase()??"unknown",d=l.src??l.src??l.href??"unknown";i=`Failed to load resource: <${p}> ${d}`}else return}let c={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"error",message:`[Uncaught] ${i}`,args:[y(i,3)],stackTrace:s,sourceFile:a};if(o){let l=o(c);l&&t(l)}else t(c)},r=u=>{let i=u.reason,s,a;if(i instanceof Error)s=i.message,a=i.stack;else if(typeof i=="string")s=i;else try{s=JSON.stringify(i)}catch{s=String(i)}let c={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"error",message:`[Unhandled Rejection] ${s}`,args:[y(i,3)],stackTrace:a,sourceFile:void 0};if(o){let l=o(c);l&&t(l)}else t(c)};return window.addEventListener("error",n,!0),window.addEventListener("unhandledrejection",r),()=>{window.removeEventListener("error",n,!0),window.removeEventListener("unhandledrejection",r)}}function ie(t,e){let o=window.location.href;function n(a,c){let l=o;l!==a&&(o=a,t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"navigation",from:l,to:a,trigger:c}))}let r=history.pushState.bind(history),u=history.replaceState.bind(history);history.pushState=function(...a){r(...a),n(window.location.href,"pushState")},history.replaceState=function(...a){u(...a),n(window.location.href,"replaceState")};let i=()=>n(window.location.href,"popstate");window.addEventListener("popstate",i);let s=()=>n(window.location.href,"hashchange");return window.addEventListener("hashchange",s),()=>{history.pushState=r,history.replaceState=u,window.removeEventListener("popstate",i),window.removeEventListener("hashchange",s)}}function ae(t,e){let o=n=>{let r=n.target;if(!(r instanceof Element)||r.closest("[data-runtimescope]"))return;let u=ze(r),i=je(r),s={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"ui",action:"click",target:u,...i&&{text:i}};t(s)};return document.addEventListener("click",o,!0),()=>{document.removeEventListener("click",o,!0)}}function ze(t){let e=t.tagName.toLowerCase();if(t.id)return`${e}#${t.id}`;let o=t.getAttribute("data-testid")??t.getAttribute("data-test-id");if(o)return`${e}[data-testid="${o}"]`;let n=t.getAttribute("role"),r=t.getAttribute("aria-label");if(n&&r)return`${e}[role="${n}"][aria-label="${r}"]`;let u=t.className;if(typeof u=="string"&&u.trim()){let s=u.split(/\s+/).find(a=>a.length>2&&!a.startsWith("_")&&!a.includes("__"));if(s)return`${e}.${s}`}let i=t.parentElement;if(i){let s=i.children,a=Array.from(s).filter(c=>c.tagName===t.tagName);if(a.length>1){let c=a.indexOf(t)+1;return`${e}:nth-child(${c})`}}return e}function je(t){let e=t.getAttribute("aria-label");if(e)return e.slice(0,80);let o=t.innerText??t.textContent;if(!o)return;let n=o.trim().replace(/\s+/g," ");if(n.length!==0)return n.length>80?n.slice(0,77)+"...":n}var z="0.9.2",A=console.debug.bind(console),j=Symbol.for("__runtimescope_originals__");function $e(){let t=globalThis;return t[j]||(t[j]={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)}}),t[j]}var b=class{static get sessionId(){return this._sessionId}static get isConnected(){return this._state==="started"}static shouldSample(){if(this._sampleRate<1&&Math.random()>this._sampleRate)return!1;if(this._maxEventsPerSecond!==void 0){let e=Date.now();if(e-this._windowStart>=1e3&&(this._windowCount=0,this._windowStart=e),this._windowCount>=this._maxEventsPerSecond)return!1;this._windowCount++}return!0}static connect(e={}){if(e.enabled===!1)return;if(!!!(e.serverUrl||e.endpoint)&&typeof window<"u"){let s=window.location?.hostname;if(s&&s!=="localhost"&&s!=="127.0.0.1"&&!s.startsWith("192.168.")&&!s.startsWith("10.")){A("[RuntimeScope] No endpoint configured and not on localhost \u2014 SDK disabled for production");return}}this._state==="started"&&(A("[RuntimeScope] Already connected \u2014 disconnecting before re-init"),this.disconnect());let n={serverUrl:e.serverUrl??e.endpoint??"ws://localhost:9090",appName:e.appName??"unknown",captureNetwork:e.captureNetwork??!0,captureConsole:e.captureConsole??!0,captureXhr:e.captureXhr??!0,captureBody:e.captureBody??!1,maxBodySize:e.maxBodySize??65536,capturePerformance:e.capturePerformance??!0,captureRenders:e.captureRenders??!0,captureNavigation:e.captureNavigation??!0,captureClicks:e.captureClicks??!0,stores:e.stores??{},beforeSend:e.beforeSend,redactHeaders:e.redactHeaders??["authorization","cookie"],batchSize:e.batchSize??50,flushIntervalMs:e.flushIntervalMs??100};$e(),this._sessionId=X(),this._state="started",this.transport=new P({serverUrl:n.serverUrl,appName:n.appName,sessionId:this._sessionId,sdkVersion:z,authToken:e.authToken,projectId:e.projectId,batchSize:n.batchSize,flushIntervalMs:n.flushIntervalMs}),this._sampleRate=e.sampleRate??1,this._maxEventsPerSecond=e.maxEventsPerSecond,this._windowCount=0,this._windowStart=Date.now(),this._user=e.user,this.transport.connect(),A(`[RuntimeScope] SDK v${z} initializing \u2014 app: ${n.appName}, server: ${n.serverUrl}`);let r=s=>{s.eventType!=="session"&&s.eventType!=="custom"&&s.eventType!=="ui"&&!this.shouldSample()||this.transport?.send(s)};r({eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:n.appName,connectedAt:Date.now(),sdkVersion:z,buildMeta:e.buildMeta,user:this._user,projectId:e.projectId});let u=this._sessionId;this.transport.onCommand(s=>{this.handleCommand(s,r,u)}),n.captureNetwork&&this.restoreFns.push(W(r,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureXhr&&this.restoreFns.push(Y(r,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureConsole&&this.restoreFns.push(K(r,this._sessionId,n.beforeSend)),e.captureErrors!==!1&&this.restoreFns.push(se(r,this._sessionId,n.beforeSend)),Object.keys(n.stores).length>0&&this.restoreFns.push(Z(r,this._sessionId,n.stores,{beforeSend:n.beforeSend})),n.capturePerformance&&this.restoreFns.push(ee(r,this._sessionId,{beforeSend:n.beforeSend})),n.captureRenders&&this.restoreFns.push(oe(r,this._sessionId,{beforeSend:n.beforeSend})),n.captureNavigation&&this.restoreFns.push(ie(r,this._sessionId)),n.captureClicks&&this.restoreFns.push(ae(r,this._sessionId));let i=[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",n.captureClicks&&"clicks"].filter(Boolean);A(`[RuntimeScope] Interceptors active \u2014 ${i.join(", ")}`),this._sampleRate<1&&A(`[RuntimeScope] Sampling at ${(this._sampleRate*100).toFixed(0)}%`),this._maxEventsPerSecond!==void 0&&A(`[RuntimeScope] Rate limited to ${this._maxEventsPerSecond} events/sec`)}static handleCommand(e,o,n){if(e.command==="capture_dom_snapshot"){let r=e.params?.maxSize??5e5,u=document.documentElement.outerHTML,i=u.length>r;i&&(u=u.slice(0,r));let s={eventId:m(),sessionId:n,timestamp:Date.now(),eventType:"dom_snapshot",html:u,url:window.location.href,viewport:{width:window.innerWidth,height:window.innerHeight},scrollPosition:{x:window.scrollX,y:window.scrollY},elementCount:document.querySelectorAll("*").length,truncated:i};o(s),this.transport?.sendCommandResponse(e.requestId,e.command,s)}else this.transport?.sendCommandResponse(e.requestId,e.command,{error:"Unknown command"})}static init(e={}){return this.connect(e)}static track(e,o){if(!this.transport||!this._sessionId)return;let n={eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"custom",name:e,properties:o};this.transport.send(n)}static setUser(e){this._user=e??void 0,this.transport&&this._sessionId&&this.transport.send({eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:"user_update",connectedAt:Date.now(),sdkVersion:z,user:this._user})}static addBreadcrumb(e,o){if(!this.transport||!this._sessionId)return;let n={eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"ui",action:"breadcrumb",target:"manual",text:e,...o&&{data:o}};this.transport.send(n)}static disconnect(){for(let o of this.restoreFns)try{o()}catch{}this.restoreFns=[];let e=globalThis[j];if(e){window.fetch!==e.fetch&&(window.fetch=e.fetch);for(let[o,n]of Object.entries(e.consoleMethods)){let r=console;r[o]!==n&&(r[o]=n)}}this.transport?.disconnect(),this.transport=null,this._sessionId=null,this._state="stopped"}};f(b,"transport",null),f(b,"restoreFns",[]),f(b,"_sessionId",null),f(b,"_state","stopped"),f(b,"_sampleRate",1),f(b,"_maxEventsPerSecond"),f(b,"_windowCount",0),f(b,"_windowStart",0),f(b,"_user");var Ue=b;return me(Xe);})();
|
|
6
|
+
`),sourceFile:Q(g),source:"browser"})}a(p,...h)});let s=console.time?.bind(console),i=console.timeEnd?.bind(console);s&&(console.time=(p="default")=>{n.set(p,performance.now()),s(p)}),i&&(console.timeEnd=(p="default")=>{let h=n.get(p);if(h!==void 0){let g=performance.now()-h;n.delete(p),t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`${p}: ${g.toFixed(2)}ms`,args:[{label:p,duration:g}],source:"browser"})}i(p)});let d=console.count?.bind(console),l=console.countReset?.bind(console);d&&(console.count=(p="default")=>{let h=(u.get(p)??0)+1;u.set(p,h),t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`${p}: ${h}`,args:[{label:p,count:h}],source:"browser"}),d(p)}),l&&(console.countReset=(p="default")=>{u.delete(p),l(p)});let c=console.table?.bind(console);return c&&(console.table=(p,h)=>{t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"info",message:`[table] ${Array.isArray(p)?`${p.length} rows`:typeof p}`,args:[y(p,3)],source:"browser"}),c(p,h)}),()=>{for(let p of G)console[p]=o[p];a&&(console.assert=a),s&&(console.time=s),i&&(console.timeEnd=i),d&&(console.count=d),l&&(console.countReset=l),c&&(console.table=c)}}function K(t){try{return JSON.stringify(t)}catch{return String(t)}}function J(t,e,r,o){let n=new Set(r.map(c=>c.toLowerCase())),u=o?.captureBody??!1,a=o?.maxBodySize??65536,s=new AbortController,i=XMLHttpRequest.prototype.open,d=XMLHttpRequest.prototype.setRequestHeader,l=XMLHttpRequest.prototype.send;return XMLHttpRequest.prototype.open=function(c,p){return this.__rs_method=c.toUpperCase(),this.__rs_url=typeof p=="string"?p:p.href,this.__rs_headers={},i.apply(this,arguments)},XMLHttpRequest.prototype.setRequestHeader=function(c,p){return this.__rs_headers&&(this.__rs_headers[c.toLowerCase()]=n.has(c.toLowerCase())?"[REDACTED]":p),d.call(this,c,p)},XMLHttpRequest.prototype.send=function(c){let p=this.__rs_method??"GET",h=this.__rs_url??"",g=`${p}:${h}`,_=!1;for(let w of L.keys())if(w.startsWith(g)){_=!0;break}if(_)return this.__rs_fetchIntercepted=!0,l.call(this,c);let R={...this.__rs_headers??{}},T=performance.now(),v,k=0;if(c){if(typeof c=="string")k=new Blob([c]).size,u&&(v=c.length>a?c.slice(0,a):c);else if(c instanceof Blob)k=c.size,u&&(v=`[Blob ${c.size} bytes]`);else if(c instanceof ArrayBuffer)k=c.byteLength,u&&(v=`[ArrayBuffer ${c.byteLength} bytes]`);else if(c instanceof FormData)u&&(v="[FormData]");else if(c instanceof URLSearchParams){let w=c.toString();k=new Blob([w]).size,u&&(v=w.length>a?w.slice(0,a):w)}}let x=De(c),C=w=>{let E={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"network",url:h,method:p,status:0,requestHeaders:R,responseHeaders:{},requestBodySize:k,responseBodySize:0,duration:0,ttfb:0,graphqlOperation:x,requestBody:v,source:"xhr",...w};if(o?.beforeSend){let I=o.beforeSend(E);I&&t(I)}else t(E)};return this.addEventListener("loadend",()=>{let w=performance.now()-T;if(this.status>0){let E=Le(this.getAllResponseHeaders(),n),I=parseInt(this.getResponseHeader("content-length")||"0",10),O;if(u&&(this.responseType===""||this.responseType==="text"))try{let U=this.responseText;O=U.length>a?U.slice(0,a):U}catch{}C({status:this.status,responseHeaders:E,responseBodySize:I,responseBody:O,duration:w,ttfb:w})}else{let E="error",I="Network error";this.readyState===0||this.status,this.timeout>0&&w>=this.timeout&&(E="timeout",I=`Request timed out after ${this.timeout}ms`),C({duration:w,errorPhase:E,errorMessage:I})}},{once:!0,signal:s.signal}),l.call(this,c)},()=>{s.abort(),XMLHttpRequest.prototype.open=i,XMLHttpRequest.prototype.setRequestHeader=d,XMLHttpRequest.prototype.send=l}}function Le(t,e){let r={};if(!t)return r;for(let o of t.trim().split(/[\r\n]+/)){let n=o.indexOf(":");if(n===-1)continue;let u=o.slice(0,n).trim().toLowerCase(),a=o.slice(n+1).trim();r[u]=e.has(u)?"[REDACTED]":a}return r}function De(t){if(!(!t||typeof t!="string"))try{let e=JSON.parse(t);if(typeof e.query=="string"){let r=e.query.trim(),o="query";r.startsWith("mutation")?o="mutation":r.startsWith("subscription")&&(o="subscription");let n=e.operationName||Ne(r)||"anonymous";return{type:o,name:n}}}catch{}}function Ne(t){return t.match(/^(?:query|mutation|subscription)\s+(\w+)/)?.[1]}function ee(t,e,r,o){let n=[],u=new Map;for(let[a,s]of Object.entries(r)){let i=xe(s);if(i==="zustand"){let d=s;$(t,e,o?.beforeSend,{storeId:a,library:i,phase:"init",state:y(d.getState(),4)});let l=d.subscribe((c,p)=>{let h=Z(p,c);$(t,e,o?.beforeSend,{storeId:a,library:i,phase:"update",state:y(c,4),previousState:y(p,4),diff:h?y(h,3):void 0})});n.push(l)}else if(i==="redux"){let d=s;$(t,e,o?.beforeSend,{storeId:a,library:i,phase:"init",state:y(d.getState(),4)});let l,c=d.dispatch.bind(d);u.set(a,c),d.dispatch=g=>(g&&typeof g=="object"&&"type"in g&&(l={type:String(g.type),payload:g.payload}),c(g));let p=d.getState(),h=d.subscribe(()=>{let g=d.getState(),_=Z(p,g);$(t,e,o?.beforeSend,{storeId:a,library:i,phase:"update",state:y(g,4),previousState:y(p,4),diff:_?y(_,3):void 0,action:l?y(l,3):void 0}),p=g,l=void 0});n.push(h)}}return()=>{for(let a of n)a();for(let[a,s]of u){let i=r[a];i&&(i.dispatch=s)}}}function xe(t){if(!t||typeof t!="object")return"unknown";let e=t;return typeof e.getState=="function"&&typeof e.setState=="function"&&typeof e.subscribe=="function"?"zustand":typeof e.dispatch=="function"&&typeof e.getState=="function"&&typeof e.subscribe=="function"?"redux":"unknown"}function Z(t,e){if(!t||!e||typeof t!="object"||typeof e!="object")return null;let r={},o=t,n=e,u=new Set([...Object.keys(o),...Object.keys(n)]);for(let a of u)o[a]!==n[a]&&(r[a]={from:o[a],to:n[a]});return Object.keys(r).length>0?r:null}function $(t,e,r,o){let n={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"state",...o};if(r){let u=r(n);u&&t(u)}else t(n)}var Oe={LCP:[2500,4e3],FCP:[1800,3e3],CLS:[.1,.25],TTFB:[800,1800],FID:[100,300],INP:[200,500]};function Ae(t,e){let[r,o]=Oe[t]??[1/0,1/0];return e<=r?"good":e<=o?"needs-improvement":"poor"}var F=null;function te(t,e,r){if(F)for(let s of F)try{s.disconnect()}catch{}let o=[];F=o;let n=(s,i,d)=>{let l={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"performance",metricName:s,value:Math.round(i*100)/100,rating:Ae(s,i),element:d};if(r?.beforeSend){let c=r.beforeSend(l);c&&t(c)}else t(l)};A(o,"largest-contentful-paint",s=>{let i=s[s.length-1];if(!i)return;let d=i.element;n("LCP",i.startTime,d?.tagName?.toLowerCase())}),A(o,"paint",s=>{for(let i of s)i.name==="first-contentful-paint"&&n("FCP",i.startTime)});let u=0;A(o,"layout-shift",s=>{for(let i of s){let d=i;!d.hadRecentInput&&d.value&&(u+=d.value,n("CLS",u))}}),A(o,"first-input",s=>{let i=s[0];if(!i)return;let d=i;d.processingStart&&n("FID",d.processingStart-i.startTime)}),A(o,"navigation",s=>{let i=s[0];i&&n("TTFB",i.responseStart-i.requestStart)});let a=0;return A(o,"event",s=>{for(let i of s)i.duration>a&&(a=i.duration,n("INP",a))},{durationThreshold:16}),()=>{for(let s of o)try{s.disconnect()}catch{}F===o&&(F=null)}}function A(t,e,r,o){try{let n=new PerformanceObserver(u=>{r(u.getEntries())});n.observe({type:e,buffered:!0,...o}),t.push(n)}catch{}}var He=0,Fe=1,oe=1e4,ne=100,Me=6e4,re=500;function se(t,e,r){let o=new Map,n=r?.snapshotIntervalMs??5e3,u=null,a=Pe();if(!a)return()=>{};let s=a.onCommitFiberRoot;return a._runtimescope_original=s,a.onCommitFiberRoot=(i,d)=>{if(s)try{s(i,d)}catch{}d.current&&X(d.current,o)},u=setInterval(()=>{ze(o,t,e,r?.beforeSend)},n),()=>{u&&(clearInterval(u),u=null),a&&(a._runtimescope_original?a.onCommitFiberRoot=a._runtimescope_original:delete a.onCommitFiberRoot,delete a._runtimescope_original)}}function Pe(){if(typeof window>"u")return null;let t=window;return t.__REACT_DEVTOOLS_GLOBAL_HOOK__||(t.__REACT_DEVTOOLS_GLOBAL_HOOK__={}),t.__REACT_DEVTOOLS_GLOBAL_HOOK__}function X(t,e){Be(t,e),t.child&&X(t.child,e),t.sibling&&X(t.sibling,e)}function Be(t,e){if(t.tag!==He&&t.tag!==Fe)return;let r=$e(t);if(!r)return;let o=Date.now(),n=t.alternate===null,u=t.actualDuration??0,a=je(t,n),s=e.get(r);s||(s={renderCount:0,totalDuration:0,lastRenderTime:0,lastRenderPhase:"mount",lastRenderCause:"unknown",renderTimestamps:[]},e.set(r,s)),s.renderCount++,s.totalDuration+=u,s.lastRenderTime=o,s.lastRenderPhase=n?"mount":"update",s.lastRenderCause=a??"unknown",s.renderTimestamps.push(o),s.renderTimestamps.length>ne&&(s.renderTimestamps=s.renderTimestamps.slice(-ne))}function $e(t){if(t.type&&typeof t.type!="string")return t.type.displayName||t.type.name||void 0}function je(t,e){return e?"props":t.alternate?t.memoizedProps!==t.alternate.memoizedProps?"props":t.memoizedState!==t.alternate.memoizedState?"state":"parent":"unknown"}function qe(t){if(t.length<2)return 0;let e=Date.now(),r=e-oe,o=t.filter(u=>u>=r);if(o.length<2)return 0;let n=e-o[0];return n===0?0:o.length/(n/1e3)}function ze(t,e,r,o){if(t.size===0)return;let n=[],u=[],a=0;for(let[d,l]of t){let c=qe(l.renderTimestamps),p=c>4||l.renderCount>20;n.push({componentName:d,renderCount:l.renderCount,totalDuration:Math.round(l.totalDuration*100)/100,avgDuration:l.renderCount>0?Math.round(l.totalDuration/l.renderCount*100)/100:0,lastRenderPhase:l.lastRenderPhase,lastRenderCause:l.lastRenderCause,renderVelocity:Math.round(c*100)/100,suspicious:p}),p&&u.push(d),a+=l.renderCount}n.sort((d,l)=>l.renderCount-d.renderCount);let s={eventId:m(),sessionId:r,timestamp:Date.now(),eventType:"render",profiles:n,snapshotWindowMs:oe,totalRenders:a,suspiciousComponents:u};if(o){let d=o(s);d&&e(d)}else e(s);let i=Date.now();for(let[d,l]of t)i-l.lastRenderTime>Me?t.delete(d):(l.renderCount=0,l.totalDuration=0);if(t.size>re){let d=[...t.entries()].sort((c,p)=>c[1].lastRenderTime-p[1].lastRenderTime),l=t.size-re;for(let c=0;c<l;c++)t.delete(d[c][0])}}function ie(t,e,r){let o=u=>{let a,s,i;if(u instanceof ErrorEvent)a=u.message||"Uncaught error",s=u.error?.stack,i=u.filename?`${u.filename}:${u.lineno}:${u.colno}`:void 0;else{let l=u.target;if(l&&l!==window){let c=l.tagName?.toLowerCase()??"unknown",p=l.src??l.src??l.href??"unknown";a=`Failed to load resource: <${c}> ${p}`}else return}let d={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"error",message:`[Uncaught] ${a}`,args:[y(a,3)],stackTrace:s,sourceFile:i};if(r){let l=r(d);l&&t(l)}else t(d)},n=u=>{let a=u.reason,s,i;if(a instanceof Error)s=a.message,i=a.stack;else if(typeof a=="string")s=a;else try{s=JSON.stringify(a)}catch{s=String(a)}let d={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"console",level:"error",message:`[Unhandled Rejection] ${s}`,args:[y(a,3)],stackTrace:i,sourceFile:void 0};if(r){let l=r(d);l&&t(l)}else t(d)};return window.addEventListener("error",o,!0),window.addEventListener("unhandledrejection",n),()=>{window.removeEventListener("error",o,!0),window.removeEventListener("unhandledrejection",n)}}function ae(t,e){let r=window.location.href;function o(i,d){let l=r;l!==i&&(r=i,t({eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"navigation",from:l,to:i,trigger:d}))}let n=history.pushState.bind(history),u=history.replaceState.bind(history);history.pushState=function(...i){n(...i),o(window.location.href,"pushState")},history.replaceState=function(...i){u(...i),o(window.location.href,"replaceState")};let a=()=>o(window.location.href,"popstate");window.addEventListener("popstate",a);let s=()=>o(window.location.href,"hashchange");return window.addEventListener("hashchange",s),()=>{history.pushState=n,history.replaceState=u,window.removeEventListener("popstate",a),window.removeEventListener("hashchange",s)}}function ce(t,e){let r=o=>{let n=o.target;if(!(n instanceof Element)||n.closest("[data-runtimescope]"))return;let u=Ue(n),a=Xe(n),s={eventId:m(),sessionId:e,timestamp:Date.now(),eventType:"ui",action:"click",target:u,...a&&{text:a}};t(s)};return document.addEventListener("click",r,!0),()=>{document.removeEventListener("click",r,!0)}}function Ue(t){let e=t.tagName.toLowerCase();if(t.id)return`${e}#${t.id}`;let r=t.getAttribute("data-testid")??t.getAttribute("data-test-id");if(r)return`${e}[data-testid="${r}"]`;let o=t.getAttribute("role"),n=t.getAttribute("aria-label");if(o&&n)return`${e}[role="${o}"][aria-label="${n}"]`;let u=t.className;if(typeof u=="string"&&u.trim()){let s=u.split(/\s+/).find(i=>i.length>2&&!i.startsWith("_")&&!i.includes("__"));if(s)return`${e}.${s}`}let a=t.parentElement;if(a){let s=a.children,i=Array.from(s).filter(d=>d.tagName===t.tagName);if(i.length>1){let d=i.indexOf(t)+1;return`${e}:nth-child(${d})`}}return e}function Xe(t){let e=t.getAttribute("aria-label");if(e)return e.slice(0,80);let r=t.innerText??t.textContent;if(!r)return;let o=r.trim().replace(/\s+/g," ");if(o.length!==0)return o.length>80?o.slice(0,77)+"...":o}function j(t){let e=t.startsWith("runtimescopes://");if(!t.startsWith("runtimescope://")&&!e)throw new Error("Invalid RuntimeScope DSN: must start with runtimescope:// or runtimescopes://");let r=new URL(t.replace(/^runtimescopes?:\/\//,"http://")),o=r.username;if(!o||!o.startsWith("proj_"))throw new Error("Invalid RuntimeScope DSN: missing projectId (expected proj_xxx@host)");let n=r.password?decodeURIComponent(r.password):void 0,u=r.hostname,a=r.port?parseInt(r.port):6768,s=a-1,i=r.pathname.replace(/^\//,"")||void 0,d=e?"wss":"ws",l=e?"https":"http";return{projectId:o,authToken:n,wsEndpoint:`${d}://${u}:${s}`,httpEndpoint:`${l}://${u}:${a}`,appName:i,tls:e}}function ue(t){let e=t.tls?"runtimescopes":"runtimescope",r=t.host??"localhost",o=t.port??6768,n=t.appName?`/${t.appName}`:"",u=t.authToken?`:${encodeURIComponent(t.authToken)}`:"";return`${e}://${t.projectId}${u}@${r}:${o}${n}`}var q="0.10.0",N=console.debug.bind(console),z=Symbol.for("__runtimescope_originals__");function de(){let t=globalThis;return t[z]||(t[z]={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)}}),t[z]}var b=class{static get sessionId(){return this._sessionId}static get isConnected(){return this._state==="started"}static shouldSample(){if(this._sampleRate<1&&Math.random()>this._sampleRate)return!1;if(this._maxEventsPerSecond!==void 0){let e=Date.now();if(e-this._windowStart>=1e3&&(this._windowCount=0,this._windowStart=e),this._windowCount>=this._maxEventsPerSecond)return!1;this._windowCount++}return!0}static connect(e={}){if(e.enabled===!1)return;let r=!1;if(e.dsn)try{let c=j(e.dsn);e={...e,serverUrl:c.wsEndpoint,projectId:c.projectId,...c.authToken?{authToken:c.authToken}:{},...c.appName&&!e.appName?{appName:c.appName}:{}},r=!0}catch(c){N("[RuntimeScope] Invalid DSN:",c.message)}if(!(!!(e.serverUrl||e.endpoint)||r)&&typeof window<"u"){let c=window.location?.hostname;if(c&&c!=="localhost"&&c!=="127.0.0.1"&&!c.startsWith("192.168.")&&!c.startsWith("10.")){N("[RuntimeScope] No endpoint configured and not on localhost \u2014 SDK disabled for production");return}}this._state==="started"&&(N("[RuntimeScope] Already connected \u2014 disconnecting before re-init"),this.disconnect());let n={serverUrl:e.serverUrl??e.endpoint??"ws://localhost:6767",appName:e.appName??"unknown",captureNetwork:e.captureNetwork??!0,captureConsole:e.captureConsole??!0,captureXhr:e.captureXhr??!0,captureBody:e.captureBody??!1,maxBodySize:e.maxBodySize??65536,capturePerformance:e.capturePerformance??!0,captureRenders:e.captureRenders??!0,captureNavigation:e.captureNavigation??!0,captureClicks:e.captureClicks??!0,stores:e.stores??{},beforeSend:e.beforeSend,redactHeaders:e.redactHeaders??["authorization","cookie"],batchSize:e.batchSize??50,flushIntervalMs:e.flushIntervalMs??100};de(),this._sessionId=W(),this._state="started",this.transport=new P({serverUrl:n.serverUrl,appName:n.appName,sessionId:this._sessionId,sdkVersion:q,authToken:e.authToken,projectId:e.projectId,batchSize:n.batchSize,flushIntervalMs:n.flushIntervalMs}),this._sampleRate=e.sampleRate??1,this._maxEventsPerSecond=e.maxEventsPerSecond,this._windowCount=0,this._windowStart=Date.now(),this._user=e.user,this.transport.connect(),N(`[RuntimeScope] SDK v${q} initializing \u2014 app: ${n.appName}, server: ${n.serverUrl}`);let u=de().consoleMethods.warn,a=this.transport,s=n.serverUrl;setTimeout(()=>{if(this.transport!==a||a.isConnected?.())return;let p=/localhost|127\.0\.0\.1/.test(s)?"Run `npx runtimescope start` in a terminal.":`Check that ${s} is reachable and the collector is running.`;u(`[RuntimeScope] Couldn't connect to ${s} after 10s. No events are being captured.`,`
|
|
7
|
+
Fix: ${p}`)},1e4);let i=c=>{c.eventType!=="session"&&c.eventType!=="custom"&&c.eventType!=="ui"&&!this.shouldSample()||this.transport?.send(c)};i({eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:n.appName,connectedAt:Date.now(),sdkVersion:q,buildMeta:e.buildMeta,user:this._user,projectId:e.projectId});let d=this._sessionId;this.transport.onCommand(c=>{this.handleCommand(c,i,d)}),n.captureNetwork&&this.restoreFns.push(V(i,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureXhr&&this.restoreFns.push(J(i,this._sessionId,n.redactHeaders,{captureBody:n.captureBody,maxBodySize:n.maxBodySize,beforeSend:n.beforeSend})),n.captureConsole&&this.restoreFns.push(Y(i,this._sessionId,n.beforeSend)),e.captureErrors!==!1&&this.restoreFns.push(ie(i,this._sessionId,n.beforeSend)),Object.keys(n.stores).length>0&&this.restoreFns.push(ee(i,this._sessionId,n.stores,{beforeSend:n.beforeSend})),n.capturePerformance&&this.restoreFns.push(te(i,this._sessionId,{beforeSend:n.beforeSend})),n.captureRenders&&this.restoreFns.push(se(i,this._sessionId,{beforeSend:n.beforeSend})),n.captureNavigation&&this.restoreFns.push(ae(i,this._sessionId)),n.captureClicks&&this.restoreFns.push(ce(i,this._sessionId));let l=[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",n.captureClicks&&"clicks"].filter(Boolean);N(`[RuntimeScope] Interceptors active \u2014 ${l.join(", ")}`),this._sampleRate<1&&N(`[RuntimeScope] Sampling at ${(this._sampleRate*100).toFixed(0)}%`),this._maxEventsPerSecond!==void 0&&N(`[RuntimeScope] Rate limited to ${this._maxEventsPerSecond} events/sec`)}static handleCommand(e,r,o){if(e.command==="capture_dom_snapshot"){let n=e.params?.maxSize??5e5,u=document.documentElement.outerHTML,a=u.length>n;a&&(u=u.slice(0,n));let s={eventId:m(),sessionId:o,timestamp:Date.now(),eventType:"dom_snapshot",html:u,url:window.location.href,viewport:{width:window.innerWidth,height:window.innerHeight},scrollPosition:{x:window.scrollX,y:window.scrollY},elementCount:document.querySelectorAll("*").length,truncated:a};r(s),this.transport?.sendCommandResponse(e.requestId,e.command,s)}else this.transport?.sendCommandResponse(e.requestId,e.command,{error:"Unknown command"})}static init(e={}){return this.connect(e)}static track(e,r){if(!this.transport||!this._sessionId)return;let o={eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"custom",name:e,properties:r};this.transport.send(o)}static setUser(e){this._user=e??void 0,this.transport&&this._sessionId&&this.transport.send({eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"session",appName:"user_update",connectedAt:Date.now(),sdkVersion:q,user:this._user})}static addBreadcrumb(e,r){if(!this.transport||!this._sessionId)return;let o={eventId:m(),sessionId:this._sessionId,timestamp:Date.now(),eventType:"ui",action:"breadcrumb",target:"manual",text:e,...r&&{data:r}};this.transport.send(o)}static disconnect(){for(let r of this.restoreFns)try{r()}catch{}this.restoreFns=[];let e=globalThis[z];if(e){window.fetch!==e.fetch&&(window.fetch=e.fetch);for(let[r,o]of Object.entries(e.consoleMethods)){let n=console;n[r]!==o&&(n[r]=o)}}this.transport?.disconnect(),this.transport=null,this._sessionId=null,this._state="stopped"}};f(b,"transport",null),f(b,"restoreFns",[]),f(b,"_sessionId",null),f(b,"_state","stopped"),f(b,"_sampleRate",1),f(b,"_maxEventsPerSecond"),f(b,"_windowCount",0),f(b,"_windowStart",0),f(b,"_user");var We=b;return ve(Ve);})();
|
|
7
8
|
if(typeof RuntimeScope!=="undefined"&&RuntimeScope.RuntimeScope){RuntimeScope=RuntimeScope.RuntimeScope;}
|
|
8
9
|
//# sourceMappingURL=index.global.js.map
|