@brainfish-ai/web-tracker 0.0.11-alpha.1 → 0.0.11

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.js CHANGED
@@ -26,5 +26,5 @@ PERFORMANCE OF THIS SOFTWARE.
26
26
  `),n=[];for(let r=2,i=a.length;r<i;r+=2){const o=e.exec(a[r]);o&&n.push(new Le({fileName:o[2],lineNumber:parseFloat(o[1])}))}return n},parseOpera10:function(t){const e=/Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i,a=t.stacktrace.split(`
27
27
  `),n=[];for(let r=0,i=a.length;r<i;r+=2){const o=e.exec(a[r]);o&&n.push(new Le({functionName:o[3]||void 0,fileName:o[2],lineNumber:parseFloat(o[1])}))}return n},parseOpera11:function(t){return t.stack.split(`
28
28
  `).filter(function(e){return!!e.match(Ei)&&!e.match(/^Error created at/)},this).map(function(e){const a=e.split("@"),n=this.extractLocation(a.pop()),r=(a.shift()||"").replace(/<anonymous function(: (\w+))?>/,"$2").replace(/\([^)]*\)/g,"")||void 0;return new Le({functionName:r,fileName:n[0],lineNumber:n[1],columnNumber:n[2]})},this)}};function xi(t){if(!t||!t.outerHTML)return"";let e="";for(;t.parentElement;){let a=t.localName;if(!a)break;a=a.toLowerCase();const n=t.parentElement,r=[];if(n.children&&n.children.length>0)for(let i=0;i<n.children.length;i++){const o=n.children[i];o.localName&&o.localName.toLowerCase&&o.localName.toLowerCase()===a&&r.push(o)}r.length>1&&(a+=`:eq(${r.indexOf(t)})`),e=a+(e?">"+e:""),t=n}return e}function Ua(t){return Object.prototype.toString.call(t)==="[object Object]"}function Nn(t,e){if(e===0)return!0;const a=Object.keys(t);for(const n of a)if(Ua(t[n])&&Nn(t[n],e-1))return!0;return!1}function xa(t,e){const a={numOfKeysLimit:50,depthOfLimit:4};Object.assign(a,e);const n=[],r=[];return JSON.stringify(t,function(i,o){if(n.length>0){const s=n.indexOf(this);~s?n.splice(s+1):n.push(this),~s?r.splice(s,1/0,i):r.push(i),~n.indexOf(o)&&(o=n[0]===o?"[Circular ~]":"[Circular ~."+r.slice(0,n.indexOf(o)).join(".")+"]")}else n.push(o);if(o===null)return o;if(o===void 0)return"undefined";if(function(s){return!!(Ua(s)&&Object.keys(s).length>a.numOfKeysLimit||typeof s=="function"||Ua(s)&&Nn(s,a.depthOfLimit))}(o))return function(s){let l=s.toString();return a.stringLengthLimit&&l.length>a.stringLengthLimit&&(l=`${l.slice(0,a.stringLengthLimit)}...`),l}(o);if(o instanceof Event){const s={};for(const l in o){const c=o[l];Array.isArray(c)?s[l]=xi(c.length?c[0]:null):s[l]=c}return s}return o instanceof Node?o instanceof HTMLElement?o?o.outerHTML:"":o.nodeName:o instanceof Error?o.stack?o.stack+`
29
- End of stack for Error object`:o.name+": "+o.message:o})}const Dt={level:["assert","clear","count","countReset","debug","dir","dirxml","error","group","groupCollapsed","groupEnd","info","log","table","time","timeEnd","timeLog","trace","warn"],lengthThreshold:1e3,logger:"console"};function Ri(t,e,a){const n=a?Object.assign({},Dt,a):Dt,r=n.logger;if(!r)return()=>{};let i;i=typeof r=="string"?e[r]:r;let o=0;const s=[];if(n.level.includes("error")&&window){const c=h=>{const d=h.message,u=h.error,b=Ft.parse(u).map(T=>T.toString()),v=[xa(d,n.stringifyOptions)];t({level:"error",trace:b,payload:v})};window.addEventListener("error",c),s.push(()=>{window&&window.removeEventListener("error",c)})}for(const c of n.level)s.push(l(i,c));return()=>{s.forEach(c=>c())};function l(c,h){return c[h]?Ne(c,h,d=>(...u)=>{d.apply(this,u);try{const b=Ft.parse(new Error).map(T=>T.toString()).splice(1),v=u.map(T=>xa(T,n.stringifyOptions));o++,o<n.lengthThreshold?t({level:h,trace:b,payload:v}):o===n.lengthThreshold&&t({level:"warn",trace:[],payload:[xa("The number of log records reached the threshold.")]})}catch(b){d("rrweb logger error:",b,...u)}}):()=>{}}}class Ni extends Array{constructor(){super()}isEmpty(){return this.length===0}isFull(){return this.length===10}flush(){return this.splice(0,this.length)}}class Mi{constructor(e){M(this,"agent");M(this,"eventBuffer");this.agent=e,this.eventBuffer=new Ni}start(){document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.send()})}handle(e){this.eventBuffer.push(e),this.eventBuffer.isFull()&&this.send()}send(){return O(this,null,function*(){if(!this.eventBuffer.isEmpty())if(this.agent.sendEvents){const e=this.eventBuffer.flush();yield this.agent.sendEvents(e)}else console.warn("No send function provided to Agent")})}}var Va=(t=>(t[t.DomContentLoaded=0]="DomContentLoaded",t[t.Load=1]="Load",t[t.FullSnapshot=2]="FullSnapshot",t[t.IncrementalSnapshot=3]="IncrementalSnapshot",t[t.Meta=4]="Meta",t[t.Custom=5]="Custom",t[t.Plugin=6]="Plugin",t))(Va||{}),Mn=(t=>(t[t.Mutation=0]="Mutation",t[t.MouseMove=1]="MouseMove",t[t.MouseInteraction=2]="MouseInteraction",t[t.Scroll=3]="Scroll",t[t.ViewportResize=4]="ViewportResize",t[t.Input=5]="Input",t[t.TouchMove=6]="TouchMove",t[t.MediaInteraction=7]="MediaInteraction",t[t.StyleSheetRule=8]="StyleSheetRule",t[t.CanvasMutation=9]="CanvasMutation",t[t.Font=10]="Font",t[t.Log=11]="Log",t[t.Drag=12]="Drag",t[t.StyleDeclaration=13]="StyleDeclaration",t[t.Selection=14]="Selection",t[t.AdoptedStyleSheet=15]="AdoptedStyleSheet",t[t.CustomElement=16]="CustomElement",t))(Mn||{}),On=(t=>(t[t.MouseUp=0]="MouseUp",t[t.MouseDown=1]="MouseDown",t[t.Click=2]="Click",t[t.ContextMenu=3]="ContextMenu",t[t.DblClick=4]="DblClick",t[t.Focus=5]="Focus",t[t.Blur=6]="Blur",t[t.TouchStart=7]="TouchStart",t[t.TouchMove_Departed=8]="TouchMove_Departed",t[t.TouchEnd=9]="TouchEnd",t[t.TouchCancel=10]="TouchCancel",t))(On||{});const Oi={recordOptions:{blockClass:"rr-block",blockSelector:null,ignoreClass:"rr-ignore",ignoreCSSAttributes:null,maskTextClass:"rr-mask",maskTextSelector:null,maskAllInputs:!0,maskInputOptions:{password:!0},maskInputFn:void 0,maskTextFn:void 0},recordConsolePlugin:{level:["error"],lengthThreshold:Number.POSITIVE_INFINITY,stringifyOptions:{stringLengthLimit:Number.POSITIVE_INFINITY,numOfKeysLimit:Number.POSITIVE_INFINITY,depthOfLimit:Number.POSITIVE_INFINITY}}};class Bt{constructor(e){M(this,"options");M(this,"stop");var a;this.options=Oe(de({},Oi),{emit:e,plugins:[{name:"rrweb/console@1",observer:Ri,options:a}]}),this.stop=null}static mockImplementation(e){throw new Error("Method not implemented.")}start(){var e;this.stop=(e=Ce(this.options))!=null?e:null}getMirror(){return Ce.mirror}takeFullSnapshot(){return Ce.takeFullSnapshot()}}class _i{constructor(e){M(this,"recordingManager");M(this,"isRunning");M(this,"events");this.recordingManager=e,this.isRunning=!1,this.events=[]}start(){this.isRunning=!0}handle(e){this.isInitializingEvent(e)?this.events.push(e):this.stop(e)}isInitializingEvent(e){return[2,4].includes(e.type)||this.isSelectionEvent(e)||this.isConsoleEvent(e)}isSelectionEvent(e){return e.type===3&&e.data.source===14}isConsoleEvent(e){return e.type===6}stop(e){this.stamp(this.events,e.timestamp-1),this.publish(...this.events,e),this.events=[],this.isRunning=!1}stamp(e,a){e.forEach(n=>n.timestamp=a)}publish(...e){e.forEach(a=>{this.recordingManager.publishEvent(a)})}}function Li(t,e){e===void 0&&(e={});for(var a=function(m){for(var w=[],y=0;y<m.length;){var j=m[y];if(j!=="*"&&j!=="+"&&j!=="?")if(j!=="\\")if(j!=="{")if(j!=="}")if(j!==":")if(j!=="(")w.push({type:"CHAR",index:y,value:m[y++]});else{var S=1,E="";if(m[f=y+1]==="?")throw new TypeError('Pattern cannot start with "?" at '.concat(f));for(;f<m.length;)if(m[f]!=="\\"){if(m[f]===")"){if(--S==0){f++;break}}else if(m[f]==="("&&(S++,m[f+1]!=="?"))throw new TypeError("Capturing groups are not allowed at ".concat(f));E+=m[f++]}else E+=m[f++]+m[f++];if(S)throw new TypeError("Unbalanced pattern at ".concat(y));if(!E)throw new TypeError("Missing pattern at ".concat(y));w.push({type:"PATTERN",index:y,value:E}),y=f}else{for(var x="",f=y+1;f<m.length;){var F=m.charCodeAt(f);if(!(F>=48&&F<=57||F>=65&&F<=90||F>=97&&F<=122||F===95))break;x+=m[f++]}if(!x)throw new TypeError("Missing parameter name at ".concat(y));w.push({type:"NAME",index:y,value:x}),y=f}else w.push({type:"CLOSE",index:y,value:m[y++]});else w.push({type:"OPEN",index:y,value:m[y++]});else w.push({type:"ESCAPED_CHAR",index:y++,value:m[y++]});else w.push({type:"MODIFIER",index:y,value:m[y++]})}return w.push({type:"END",index:y,value:""}),w}(t),n=e.prefixes,r=n===void 0?"./":n,i=e.delimiter,o=i===void 0?"/#?":i,s=[],l=0,c=0,h="",d=function(m){if(c<a.length&&a[c].type===m)return a[c++].value},u=function(m){var w=d(m);if(w!==void 0)return w;var y=a[c],j=y.type,S=y.index;throw new TypeError("Unexpected ".concat(j," at ").concat(S,", expected ").concat(m))},b=function(){for(var m,w="";m=d("CHAR")||d("ESCAPED_CHAR");)w+=m;return w},v=function(m){var w=s[s.length-1],y=m||(w&&typeof w=="string"?w:"");if(w&&!y)throw new TypeError('Must have text between two parameters, missing text after "'.concat(w.name,'"'));return!y||function(j){for(var S=0,E=o;S<E.length;S++){var x=E[S];if(j.indexOf(x)>-1)return!0}return!1}(y)?"[^".concat(Ee(o),"]+?"):"(?:(?!".concat(Ee(y),")[^").concat(Ee(o),"])+?")};c<a.length;){var T=d("CHAR"),p=d("NAME"),g=d("PATTERN");if(p||g){var C=T||"";r.indexOf(C)===-1&&(h+=C,C=""),h&&(s.push(h),h=""),s.push({name:p||l++,prefix:C,suffix:"",pattern:g||v(C),modifier:d("MODIFIER")||""})}else{var z=T||d("ESCAPED_CHAR");if(z)h+=z;else if(h&&(s.push(h),h=""),d("OPEN")){C=b();var N=d("NAME")||"",k=d("PATTERN")||"",I=b();u("CLOSE"),s.push({name:N||(k?l++:""),pattern:N&&!k?v(C):k,prefix:C,suffix:I,modifier:d("MODIFIER")||""})}else u("END")}}return s}function Fi(t,e){var a=[];return function(n,r,i){i===void 0&&(i={});var o=i.decode,s=o===void 0?function(l){return l}:o;return function(l){var c=n.exec(l);if(!c)return!1;for(var h=c[0],d=c.index,u=Object.create(null),b=function(T){if(c[T]===void 0)return"continue";var p=r[T-1];p.modifier==="*"||p.modifier==="+"?u[p.name]=c[T].split(p.prefix+p.suffix).map(function(g){return s(g,p)}):u[p.name]=s(c[T],p)},v=1;v<c.length;v++)b(v);return{path:h,index:d,params:u}}}(Ln(t,a,e),a,e)}function Ee(t){return t.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function _n(t){return t&&t.sensitive?"":"i"}function Di(t,e,a){return function(n,r,i){i===void 0&&(i={});for(var o=i.strict,s=o!==void 0&&o,l=i.start,c=l===void 0||l,h=i.end,d=h===void 0||h,u=i.encode,b=u===void 0?function(E){return E}:u,v=i.delimiter,T=v===void 0?"/#?":v,p=i.endsWith,g="[".concat(Ee(p===void 0?"":p),"]|$"),C="[".concat(Ee(T),"]"),z=c?"^":"",N=0,k=n;N<k.length;N++){var I=k[N];if(typeof I=="string")z+=Ee(b(I));else{var m=Ee(b(I.prefix)),w=Ee(b(I.suffix));if(I.pattern)if(r&&r.push(I),m||w)if(I.modifier==="+"||I.modifier==="*"){var y=I.modifier==="*"?"?":"";z+="(?:".concat(m,"((?:").concat(I.pattern,")(?:").concat(w).concat(m,"(?:").concat(I.pattern,"))*)").concat(w,")").concat(y)}else z+="(?:".concat(m,"(").concat(I.pattern,")").concat(w,")").concat(I.modifier);else{if(I.modifier==="+"||I.modifier==="*")throw new TypeError('Can not repeat "'.concat(I.name,'" without a prefix and suffix'));z+="(".concat(I.pattern,")").concat(I.modifier)}else z+="(?:".concat(m).concat(w,")").concat(I.modifier)}}if(d)s||(z+="".concat(C,"?")),z+=i.endsWith?"(?=".concat(g,")"):"$";else{var j=n[n.length-1],S=typeof j=="string"?C.indexOf(j[j.length-1])>-1:j===void 0;s||(z+="(?:".concat(C,"(?=").concat(g,"))?")),S||(z+="(?=".concat(C,"|").concat(g,")"))}return new RegExp(z,_n(i))}(Li(t,a),e,a)}function Ln(t,e,a){return t instanceof RegExp?function(n,r){if(!r)return n;for(var i=/\((?:\?<(.*?)>)?(?!\?)/g,o=0,s=i.exec(n.source);s;)r.push({name:s[1]||o++,prefix:"",suffix:"",modifier:"",pattern:""}),s=i.exec(n.source);return n}(t,e):Array.isArray(t)?function(n,r,i){var o=n.map(function(s){return Ln(s,r,i).source});return new RegExp("(?:".concat(o.join("|"),")"),_n(i))}(t,e,a):Di(t,e,a)}function Fn(){if(typeof window=="undefined")return!1;const t=window.location.hostname;return t==="localhost"||t==="127.0.0.1"}function Dn(t,e){if(Fn()&&!e)return!1;const a=window.location.pathname;for(const n of t)try{if(Fi(n,{decode:decodeURIComponent})(a))return!1}catch(r){if(n===a)return!1}return!0}class Bi{constructor(e){M(this,"agent");M(this,"recorder");M(this,"stasher");this.agent=e,this.recorder=new Bt(this.handleEvent.bind(this)),this.stasher=new _i(this)}start(){this.recorder.start()}handleEvent(e){var r;const a=new URL(window.location.href),n=a.origin+a.pathname;if(Dn(this.agent.recordingBlocklist,this.agent._allowLocalhostRecording)){if(this.isClick(e)){let i=Ce.mirror.getNode(e.data.id);i&&this.nodeIsInteresting(i)&&(e.conversionData={},e.conversionData.eventType="click",e.conversionData.textContent=i.textContent,e.conversionData.htmlElement=(r=i.nodeName)==null?void 0:r.toLowerCase(),e.conversionData.location=n)}this.isCustom(e)&&(e.conversionData={},e.conversionData.eventType="custom",e.conversionData.customEventType=e.data.tag,e.conversionData.location=n),this.stasher.isRunning?this.stasher.handle(e):this.publishEvent(e)}}publishEvent(e){this.agent.sender.handle(e),this.agent.timer&&this.agent.timer.restart()}startRecorderWithStasher(){this.stasher.start(),this.recorder=new Bt(this.handleEvent.bind(this)),this.recorder.start()}isClick(e){return e.type===Va.IncrementalSnapshot&&e.data.source===Mn.MouseInteraction&&e.data.type===On.Click}nodeIsInteresting(e){return e.nodeName==="BUTTON"||e.nodeName==="ANCHOR"}isCustom(e){return e.type===Va.Custom}}class qi{constructor(e,a){M(this,"agent");M(this,"MAX_IDLE_TIME");M(this,"timeoutId");this.agent=e,this.MAX_IDLE_TIME=a,this.timeoutId=0}restart(){this.stop(),this.start()}stop(){clearTimeout(this.timeoutId)}start(){this.timeoutId=window.setTimeout(this.agent.handleTimeout.bind(this.agent),this.MAX_IDLE_TIME)}}class Wi{constructor(){M(this,"recordingManager");M(this,"sender");M(this,"endpoint");M(this,"MAX_IDLE_TIME");M(this,"timer");M(this,"sendEvents");M(this,"recordingBlocklist",[]);M(this,"_allowLocalhostRecording");this.recordingManager=new Bi(this),this.sender=new Mi(this)}start(i){return O(this,arguments,function*({endpoint:e,sendEvents:a,recordingBlocklist:n,_allowLocalhostRecording:r}){this.endpoint=e,this.sendEvents=a,this.recordingBlocklist=n,this._allowLocalhostRecording=r,this.MAX_IDLE_TIME=6e4,this.timer=new qi(this,this.MAX_IDLE_TIME),this.sender.start(),this.recordingManager.start()})}handleTimeout(){this.recordingManager.recorder.stop&&this.recordingManager.recorder.stop(),this.sender.send(),this.recordingManager.startRecorderWithStasher()}takeFullSnapshot(){return this.recordingManager.recorder.takeFullSnapshot()}static handleCustomEvent(e){Ce.addCustomEvent(e,null)}}const xe=class xe{constructor(){M(this,"sessionId",null)}static getInstance(){return xe.instance||(xe.instance=new xe),xe.instance}determineSessionState(e){return!this.sessionId&&e?"new":this.sessionId&&e&&this.sessionId!==e?"changed":this.sessionId===e?"active":"expired"}handleSessionTransition(e,a,n){var r;switch(e){case"new":this.sessionId=a;break;case"changed":this.sessionId=a,(r=n.onSessionChanged)==null||r.call(n)}}updateSession(e,a={}){const n=this.determineSessionState(e);this.handleSessionTransition(n,e,a)}getCurrentSessionId(){return this.sessionId}};M(xe,"instance");let Pa=xe;const qt="0.0.11-alpha.1";exports.Tracker=class e extends Mr{constructor(a){super(de({sdk:"web",sdkVersion:qt},a));M(this,"lastPath","");M(this,"debounceTimer",null);M(this,"agent");M(this,"sessionManager",Pa.getInstance());M(this,"recordingBlocklist",[]);this.options=a,this.agent=new Wi,this.recordingBlocklist=a.recordingBlocklist||[],this.isServer()||(this.setGlobalProperties({__referrer:document.referrer}),this.options.trackOutgoingLinks&&this.trackOutgoingLinks(),this.options.trackScreenViews&&this.trackScreenViews(),this.options.trackAttributes&&this.trackAttributes(),this.options.enableRecording&&Fn()&&this.startRecording())}debounce(a,n){this.debounceTimer&&clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(a,n)}isServer(){return typeof document=="undefined"}trackOutgoingLinks(){this.isServer()||document.addEventListener("click",a=>{const n=a.target,r=n.closest("a");if(r&&n){const i=r.getAttribute("href");i!=null&&i.startsWith("http")&&super.track("link_out",{href:i,text:r.innerText||r.getAttribute("title")||n.getAttribute("alt")||n.getAttribute("title")})}})}trackScreenViews(){if(this.isServer())return;this.screenView();const a=history.pushState;history.pushState=function(...i){const o=a.apply(this,i);return window.dispatchEvent(new Event("pushstate")),window.dispatchEvent(new Event("locationchange")),o};const n=history.replaceState;history.replaceState=function(...i){const o=n.apply(this,i);return window.dispatchEvent(new Event("replacestate")),window.dispatchEvent(new Event("locationchange")),o},window.addEventListener("popstate",function(){window.dispatchEvent(new Event("locationchange"))});const r=()=>this.debounce(()=>this.screenView(),100);this.options.trackHashChanges?window.addEventListener("hashchange",r):window.addEventListener("locationchange",r)}trackAttributes(){this.isServer()||document.addEventListener("click",a=>{const n=a.target,r=n.closest("button"),i=n.closest("a"),o=r!=null&&r.getAttribute("data-track")?r:i!=null&&i.getAttribute("data-track")?i:null;if(o){const l={};for(const h of o.attributes)h.name.startsWith("data-")&&h.name!=="data-track"&&(l[s=h.name.replace(/^data-/,""),s.replace(/([-_][a-z])/gi,d=>d.toUpperCase().replace("-","").replace("_",""))]=h.value);const c=o.getAttribute("data-track");c&&super.track(c,l)}var s})}startRecording(){if(this.isServer())return;const{apiUrl:a}=this.options;if(!a)throw new Error("API URL is required");this.agent.start({endpoint:a,sendEvents:this.recordEventsToServer.bind(this),recordingBlocklist:this.recordingBlocklist,_allowLocalhostRecording:this.options._allowLocalhostRecording})}screenView(a,n){return O(this,null,function*(){if(this.isServer())return;let r,i;if(typeof a=="string"?(r=a,i=n):(r=window.location.href,i=a),this.lastPath===r)return;this.lastPath=r;const o=Oe(de({},i!=null?i:{}),{__path:r,__title:document.title});if(this.options.enableRecording&&Dn(this.recordingBlocklist,this.options._allowLocalhostRecording)){const s=yield function(l){return O(this,null,function*(){try{yield ai(l);const c=document.body.cloneNode(!0);return ei(c),yield Hr(c)}catch(c){console.error("An error occurred:",c)}})}();o.__screenshot=s}ot(e.prototype,this,"track").call(this,"screen_view",o)})}recordEventsToServer(a){return O(this,null,function*(){this.record(a,n=>{this.sessionManager.updateSession(n.sessionId,{onSessionChanged:()=>{this.agent.takeFullSnapshot()}})})})}},exports.VERSION=qt;
29
+ End of stack for Error object`:o.name+": "+o.message:o})}const Dt={level:["assert","clear","count","countReset","debug","dir","dirxml","error","group","groupCollapsed","groupEnd","info","log","table","time","timeEnd","timeLog","trace","warn"],lengthThreshold:1e3,logger:"console"};function Ri(t,e,a){const n=a?Object.assign({},Dt,a):Dt,r=n.logger;if(!r)return()=>{};let i;i=typeof r=="string"?e[r]:r;let o=0;const s=[];if(n.level.includes("error")&&window){const c=h=>{const d=h.message,u=h.error,b=Ft.parse(u).map(T=>T.toString()),v=[xa(d,n.stringifyOptions)];t({level:"error",trace:b,payload:v})};window.addEventListener("error",c),s.push(()=>{window&&window.removeEventListener("error",c)})}for(const c of n.level)s.push(l(i,c));return()=>{s.forEach(c=>c())};function l(c,h){return c[h]?Ne(c,h,d=>(...u)=>{d.apply(this,u);try{const b=Ft.parse(new Error).map(T=>T.toString()).splice(1),v=u.map(T=>xa(T,n.stringifyOptions));o++,o<n.lengthThreshold?t({level:h,trace:b,payload:v}):o===n.lengthThreshold&&t({level:"warn",trace:[],payload:[xa("The number of log records reached the threshold.")]})}catch(b){d("rrweb logger error:",b,...u)}}):()=>{}}}class Ni extends Array{constructor(){super()}isEmpty(){return this.length===0}isFull(){return this.length===10}flush(){return this.splice(0,this.length)}}class Mi{constructor(e){M(this,"agent");M(this,"eventBuffer");this.agent=e,this.eventBuffer=new Ni}start(){document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.send()})}handle(e){this.eventBuffer.push(e),this.eventBuffer.isFull()&&this.send()}send(){return O(this,null,function*(){if(!this.eventBuffer.isEmpty())if(this.agent.sendEvents){const e=this.eventBuffer.flush();yield this.agent.sendEvents(e)}else console.warn("No send function provided to Agent")})}}var Va=(t=>(t[t.DomContentLoaded=0]="DomContentLoaded",t[t.Load=1]="Load",t[t.FullSnapshot=2]="FullSnapshot",t[t.IncrementalSnapshot=3]="IncrementalSnapshot",t[t.Meta=4]="Meta",t[t.Custom=5]="Custom",t[t.Plugin=6]="Plugin",t))(Va||{}),Mn=(t=>(t[t.Mutation=0]="Mutation",t[t.MouseMove=1]="MouseMove",t[t.MouseInteraction=2]="MouseInteraction",t[t.Scroll=3]="Scroll",t[t.ViewportResize=4]="ViewportResize",t[t.Input=5]="Input",t[t.TouchMove=6]="TouchMove",t[t.MediaInteraction=7]="MediaInteraction",t[t.StyleSheetRule=8]="StyleSheetRule",t[t.CanvasMutation=9]="CanvasMutation",t[t.Font=10]="Font",t[t.Log=11]="Log",t[t.Drag=12]="Drag",t[t.StyleDeclaration=13]="StyleDeclaration",t[t.Selection=14]="Selection",t[t.AdoptedStyleSheet=15]="AdoptedStyleSheet",t[t.CustomElement=16]="CustomElement",t))(Mn||{}),On=(t=>(t[t.MouseUp=0]="MouseUp",t[t.MouseDown=1]="MouseDown",t[t.Click=2]="Click",t[t.ContextMenu=3]="ContextMenu",t[t.DblClick=4]="DblClick",t[t.Focus=5]="Focus",t[t.Blur=6]="Blur",t[t.TouchStart=7]="TouchStart",t[t.TouchMove_Departed=8]="TouchMove_Departed",t[t.TouchEnd=9]="TouchEnd",t[t.TouchCancel=10]="TouchCancel",t))(On||{});const Oi={recordOptions:{blockClass:"rr-block",blockSelector:null,ignoreClass:"rr-ignore",ignoreCSSAttributes:null,maskTextClass:"rr-mask",maskTextSelector:null,maskAllInputs:!0,maskInputOptions:{password:!0},maskInputFn:void 0,maskTextFn:void 0},recordConsolePlugin:{level:["error"],lengthThreshold:Number.POSITIVE_INFINITY,stringifyOptions:{stringLengthLimit:Number.POSITIVE_INFINITY,numOfKeysLimit:Number.POSITIVE_INFINITY,depthOfLimit:Number.POSITIVE_INFINITY}}};class Bt{constructor(e){M(this,"options");M(this,"stop");var a;this.options=Oe(de({},Oi),{emit:e,plugins:[{name:"rrweb/console@1",observer:Ri,options:a}]}),this.stop=null}static mockImplementation(e){throw new Error("Method not implemented.")}start(){var e;this.stop=(e=Ce(this.options))!=null?e:null}getMirror(){return Ce.mirror}takeFullSnapshot(){return Ce.takeFullSnapshot()}}class _i{constructor(e){M(this,"recordingManager");M(this,"isRunning");M(this,"events");this.recordingManager=e,this.isRunning=!1,this.events=[]}start(){this.isRunning=!0}handle(e){this.isInitializingEvent(e)?this.events.push(e):this.stop(e)}isInitializingEvent(e){return[2,4].includes(e.type)||this.isSelectionEvent(e)||this.isConsoleEvent(e)}isSelectionEvent(e){return e.type===3&&e.data.source===14}isConsoleEvent(e){return e.type===6}stop(e){this.stamp(this.events,e.timestamp-1),this.publish(...this.events,e),this.events=[],this.isRunning=!1}stamp(e,a){e.forEach(n=>n.timestamp=a)}publish(...e){e.forEach(a=>{this.recordingManager.publishEvent(a)})}}function Li(t,e){e===void 0&&(e={});for(var a=function(m){for(var w=[],y=0;y<m.length;){var j=m[y];if(j!=="*"&&j!=="+"&&j!=="?")if(j!=="\\")if(j!=="{")if(j!=="}")if(j!==":")if(j!=="(")w.push({type:"CHAR",index:y,value:m[y++]});else{var S=1,E="";if(m[f=y+1]==="?")throw new TypeError('Pattern cannot start with "?" at '.concat(f));for(;f<m.length;)if(m[f]!=="\\"){if(m[f]===")"){if(--S==0){f++;break}}else if(m[f]==="("&&(S++,m[f+1]!=="?"))throw new TypeError("Capturing groups are not allowed at ".concat(f));E+=m[f++]}else E+=m[f++]+m[f++];if(S)throw new TypeError("Unbalanced pattern at ".concat(y));if(!E)throw new TypeError("Missing pattern at ".concat(y));w.push({type:"PATTERN",index:y,value:E}),y=f}else{for(var x="",f=y+1;f<m.length;){var F=m.charCodeAt(f);if(!(F>=48&&F<=57||F>=65&&F<=90||F>=97&&F<=122||F===95))break;x+=m[f++]}if(!x)throw new TypeError("Missing parameter name at ".concat(y));w.push({type:"NAME",index:y,value:x}),y=f}else w.push({type:"CLOSE",index:y,value:m[y++]});else w.push({type:"OPEN",index:y,value:m[y++]});else w.push({type:"ESCAPED_CHAR",index:y++,value:m[y++]});else w.push({type:"MODIFIER",index:y,value:m[y++]})}return w.push({type:"END",index:y,value:""}),w}(t),n=e.prefixes,r=n===void 0?"./":n,i=e.delimiter,o=i===void 0?"/#?":i,s=[],l=0,c=0,h="",d=function(m){if(c<a.length&&a[c].type===m)return a[c++].value},u=function(m){var w=d(m);if(w!==void 0)return w;var y=a[c],j=y.type,S=y.index;throw new TypeError("Unexpected ".concat(j," at ").concat(S,", expected ").concat(m))},b=function(){for(var m,w="";m=d("CHAR")||d("ESCAPED_CHAR");)w+=m;return w},v=function(m){var w=s[s.length-1],y=m||(w&&typeof w=="string"?w:"");if(w&&!y)throw new TypeError('Must have text between two parameters, missing text after "'.concat(w.name,'"'));return!y||function(j){for(var S=0,E=o;S<E.length;S++){var x=E[S];if(j.indexOf(x)>-1)return!0}return!1}(y)?"[^".concat(Ee(o),"]+?"):"(?:(?!".concat(Ee(y),")[^").concat(Ee(o),"])+?")};c<a.length;){var T=d("CHAR"),p=d("NAME"),g=d("PATTERN");if(p||g){var C=T||"";r.indexOf(C)===-1&&(h+=C,C=""),h&&(s.push(h),h=""),s.push({name:p||l++,prefix:C,suffix:"",pattern:g||v(C),modifier:d("MODIFIER")||""})}else{var z=T||d("ESCAPED_CHAR");if(z)h+=z;else if(h&&(s.push(h),h=""),d("OPEN")){C=b();var N=d("NAME")||"",k=d("PATTERN")||"",I=b();u("CLOSE"),s.push({name:N||(k?l++:""),pattern:N&&!k?v(C):k,prefix:C,suffix:I,modifier:d("MODIFIER")||""})}else u("END")}}return s}function Fi(t,e){var a=[];return function(n,r,i){i===void 0&&(i={});var o=i.decode,s=o===void 0?function(l){return l}:o;return function(l){var c=n.exec(l);if(!c)return!1;for(var h=c[0],d=c.index,u=Object.create(null),b=function(T){if(c[T]===void 0)return"continue";var p=r[T-1];p.modifier==="*"||p.modifier==="+"?u[p.name]=c[T].split(p.prefix+p.suffix).map(function(g){return s(g,p)}):u[p.name]=s(c[T],p)},v=1;v<c.length;v++)b(v);return{path:h,index:d,params:u}}}(Ln(t,a,e),a,e)}function Ee(t){return t.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function _n(t){return t&&t.sensitive?"":"i"}function Di(t,e,a){return function(n,r,i){i===void 0&&(i={});for(var o=i.strict,s=o!==void 0&&o,l=i.start,c=l===void 0||l,h=i.end,d=h===void 0||h,u=i.encode,b=u===void 0?function(E){return E}:u,v=i.delimiter,T=v===void 0?"/#?":v,p=i.endsWith,g="[".concat(Ee(p===void 0?"":p),"]|$"),C="[".concat(Ee(T),"]"),z=c?"^":"",N=0,k=n;N<k.length;N++){var I=k[N];if(typeof I=="string")z+=Ee(b(I));else{var m=Ee(b(I.prefix)),w=Ee(b(I.suffix));if(I.pattern)if(r&&r.push(I),m||w)if(I.modifier==="+"||I.modifier==="*"){var y=I.modifier==="*"?"?":"";z+="(?:".concat(m,"((?:").concat(I.pattern,")(?:").concat(w).concat(m,"(?:").concat(I.pattern,"))*)").concat(w,")").concat(y)}else z+="(?:".concat(m,"(").concat(I.pattern,")").concat(w,")").concat(I.modifier);else{if(I.modifier==="+"||I.modifier==="*")throw new TypeError('Can not repeat "'.concat(I.name,'" without a prefix and suffix'));z+="(".concat(I.pattern,")").concat(I.modifier)}else z+="(?:".concat(m).concat(w,")").concat(I.modifier)}}if(d)s||(z+="".concat(C,"?")),z+=i.endsWith?"(?=".concat(g,")"):"$";else{var j=n[n.length-1],S=typeof j=="string"?C.indexOf(j[j.length-1])>-1:j===void 0;s||(z+="(?:".concat(C,"(?=").concat(g,"))?")),S||(z+="(?=".concat(C,"|").concat(g,")"))}return new RegExp(z,_n(i))}(Li(t,a),e,a)}function Ln(t,e,a){return t instanceof RegExp?function(n,r){if(!r)return n;for(var i=/\((?:\?<(.*?)>)?(?!\?)/g,o=0,s=i.exec(n.source);s;)r.push({name:s[1]||o++,prefix:"",suffix:"",modifier:"",pattern:""}),s=i.exec(n.source);return n}(t,e):Array.isArray(t)?function(n,r,i){var o=n.map(function(s){return Ln(s,r,i).source});return new RegExp("(?:".concat(o.join("|"),")"),_n(i))}(t,e,a):Di(t,e,a)}function Fn(){if(typeof window=="undefined")return!1;const t=window.location.hostname;return t==="localhost"||t==="127.0.0.1"}function Dn(t,e){if(Fn()&&!e)return!1;const a=window.location.pathname;for(const n of t)try{if(Fi(n,{decode:decodeURIComponent})(a))return!1}catch(r){if(n===a)return!1}return!0}class Bi{constructor(e){M(this,"agent");M(this,"recorder");M(this,"stasher");this.agent=e,this.recorder=new Bt(this.handleEvent.bind(this)),this.stasher=new _i(this)}start(){this.recorder.start()}handleEvent(e){var r;const a=new URL(window.location.href),n=a.origin+a.pathname;if(Dn(this.agent.recordingBlocklist,this.agent._allowLocalhostRecording)){if(this.isClick(e)){let i=Ce.mirror.getNode(e.data.id);i&&this.nodeIsInteresting(i)&&(e.conversionData={},e.conversionData.eventType="click",e.conversionData.textContent=i.textContent,e.conversionData.htmlElement=(r=i.nodeName)==null?void 0:r.toLowerCase(),e.conversionData.location=n)}this.isCustom(e)&&(e.conversionData={},e.conversionData.eventType="custom",e.conversionData.customEventType=e.data.tag,e.conversionData.location=n),this.stasher.isRunning?this.stasher.handle(e):this.publishEvent(e)}}publishEvent(e){this.agent.sender.handle(e),this.agent.timer&&this.agent.timer.restart()}startRecorderWithStasher(){this.stasher.start(),this.recorder=new Bt(this.handleEvent.bind(this)),this.recorder.start()}isClick(e){return e.type===Va.IncrementalSnapshot&&e.data.source===Mn.MouseInteraction&&e.data.type===On.Click}nodeIsInteresting(e){return e.nodeName==="BUTTON"||e.nodeName==="ANCHOR"}isCustom(e){return e.type===Va.Custom}}class qi{constructor(e,a){M(this,"agent");M(this,"MAX_IDLE_TIME");M(this,"timeoutId");this.agent=e,this.MAX_IDLE_TIME=a,this.timeoutId=0}restart(){this.stop(),this.start()}stop(){clearTimeout(this.timeoutId)}start(){this.timeoutId=window.setTimeout(this.agent.handleTimeout.bind(this.agent),this.MAX_IDLE_TIME)}}class Wi{constructor(){M(this,"recordingManager");M(this,"sender");M(this,"endpoint");M(this,"MAX_IDLE_TIME");M(this,"timer");M(this,"sendEvents");M(this,"recordingBlocklist",[]);M(this,"_allowLocalhostRecording");this.recordingManager=new Bi(this),this.sender=new Mi(this)}start(i){return O(this,arguments,function*({endpoint:e,sendEvents:a,recordingBlocklist:n,_allowLocalhostRecording:r}){this.endpoint=e,this.sendEvents=a,this.recordingBlocklist=n,this._allowLocalhostRecording=r,this.MAX_IDLE_TIME=6e4,this.timer=new qi(this,this.MAX_IDLE_TIME),this.sender.start(),this.recordingManager.start()})}handleTimeout(){this.recordingManager.recorder.stop&&this.recordingManager.recorder.stop(),this.sender.send(),this.recordingManager.startRecorderWithStasher()}takeFullSnapshot(){return this.recordingManager.recorder.takeFullSnapshot()}static handleCustomEvent(e){Ce.addCustomEvent(e,null)}}const xe=class xe{constructor(){M(this,"sessionId",null)}static getInstance(){return xe.instance||(xe.instance=new xe),xe.instance}determineSessionState(e){return!this.sessionId&&e?"new":this.sessionId&&e&&this.sessionId!==e?"changed":this.sessionId===e?"active":"expired"}handleSessionTransition(e,a,n){var r;switch(e){case"new":this.sessionId=a;break;case"changed":this.sessionId=a,(r=n.onSessionChanged)==null||r.call(n)}}updateSession(e,a={}){const n=this.determineSessionState(e);this.handleSessionTransition(n,e,a)}getCurrentSessionId(){return this.sessionId}};M(xe,"instance");let Pa=xe;const qt="0.0.11";exports.Tracker=class e extends Mr{constructor(a){super(de({sdk:"web",sdkVersion:qt},a));M(this,"lastPath","");M(this,"debounceTimer",null);M(this,"agent");M(this,"sessionManager",Pa.getInstance());M(this,"recordingBlocklist",[]);this.options=a,this.agent=new Wi,this.recordingBlocklist=a.recordingBlocklist||[],this.isServer()||(this.setGlobalProperties({__referrer:document.referrer}),this.options.trackOutgoingLinks&&this.trackOutgoingLinks(),this.options.trackScreenViews&&this.trackScreenViews(),this.options.trackAttributes&&this.trackAttributes(),this.options.enableRecording&&Fn()&&this.startRecording())}debounce(a,n){this.debounceTimer&&clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(a,n)}isServer(){return typeof document=="undefined"}trackOutgoingLinks(){this.isServer()||document.addEventListener("click",a=>{const n=a.target,r=n.closest("a");if(r&&n){const i=r.getAttribute("href");i!=null&&i.startsWith("http")&&super.track("link_out",{href:i,text:r.innerText||r.getAttribute("title")||n.getAttribute("alt")||n.getAttribute("title")})}})}trackScreenViews(){if(this.isServer())return;this.screenView();const a=history.pushState;history.pushState=function(...i){const o=a.apply(this,i);return window.dispatchEvent(new Event("pushstate")),window.dispatchEvent(new Event("locationchange")),o};const n=history.replaceState;history.replaceState=function(...i){const o=n.apply(this,i);return window.dispatchEvent(new Event("replacestate")),window.dispatchEvent(new Event("locationchange")),o},window.addEventListener("popstate",function(){window.dispatchEvent(new Event("locationchange"))});const r=()=>this.debounce(()=>this.screenView(),100);this.options.trackHashChanges?window.addEventListener("hashchange",r):window.addEventListener("locationchange",r)}trackAttributes(){this.isServer()||document.addEventListener("click",a=>{const n=a.target,r=n.closest("button"),i=n.closest("a"),o=r!=null&&r.getAttribute("data-track")?r:i!=null&&i.getAttribute("data-track")?i:null;if(o){const l={};for(const h of o.attributes)h.name.startsWith("data-")&&h.name!=="data-track"&&(l[s=h.name.replace(/^data-/,""),s.replace(/([-_][a-z])/gi,d=>d.toUpperCase().replace("-","").replace("_",""))]=h.value);const c=o.getAttribute("data-track");c&&super.track(c,l)}var s})}startRecording(){if(this.isServer())return;const{apiUrl:a}=this.options;if(!a)throw new Error("API URL is required");this.agent.start({endpoint:a,sendEvents:this.recordEventsToServer.bind(this),recordingBlocklist:this.recordingBlocklist,_allowLocalhostRecording:this.options._allowLocalhostRecording})}screenView(a,n){return O(this,null,function*(){if(this.isServer())return;let r,i;if(typeof a=="string"?(r=a,i=n):(r=window.location.href,i=a),this.lastPath===r)return;this.lastPath=r;const o=Oe(de({},i!=null?i:{}),{__path:r,__title:document.title});if(this.options.enableRecording&&Dn(this.recordingBlocklist,this.options._allowLocalhostRecording)){const s=yield function(l){return O(this,null,function*(){try{yield ai(l);const c=document.body.cloneNode(!0);return ei(c),yield Hr(c)}catch(c){console.error("An error occurred:",c)}})}();o.__screenshot=s}ot(e.prototype,this,"track").call(this,"screen_view",o)})}recordEventsToServer(a){return O(this,null,function*(){this.record(a,n=>{this.sessionManager.updateSession(n.sessionId,{onSessionChanged:()=>{this.agent.takeFullSnapshot()}})})})}},exports.VERSION=qt;
30
30
  //# sourceMappingURL=index.cjs.js.map
Binary file