UNPKG

harperdb

Version:

HarperDB is a distributed database, caching service, streaming broker, and application development platform focused on performance and ease of use.

3 lines (2 loc) 21.6 kB
import{s as x,C as Y,b as pt,N as h,a as ht,c as k,d as gt,i as mt,g as X,r as St,e as Tt,P as Et,f as yt,h as It,j as W,k as q,t as I,l as Nt,m as wt,n as J,o as K,p as j,q as _,u as M,v as y,w as Mt,x as bt,y as N,z as R,R as vt,A as Ct,B as v,D as Rt,E as xt,F as _t,G as O,H as Dt,I as Ot,J as Lt,K as At,L as Vt,M as zt,O as kt,Q as Pt,S as Ft,T as Ht,U as Ut,V as Bt,W as Gt,X as $t}from"./index-CkEccPko.js";const T={FullSnapshot:2,IncrementalSnapshot:3,Meta:4,Focus:6,ViewEnd:7,VisualViewport:8,FrustrationRecord:9},w={Document:0,DocumentType:1,Element:2,Text:3,CDATA:4,DocumentFragment:11},g={Mutation:0,MouseMove:1,MouseInteraction:2,Scroll:3,ViewportResize:4,Input:5,TouchMove:6,MediaInteraction:7,StyleSheetRule:8},m={MouseUp:0,MouseDown:1,Click:2,ContextMenu:3,DblClick:4,Focus:5,Blur:6,TouchStart:7,TouchEnd:9},P={Play:0,Pause:1};function L(t,n){const e=t.tagName,o=t.value;if(x(t,n)){const s=t.type;return e==="INPUT"&&(s==="button"||s==="submit"||s==="reset")?o:!o||e==="OPTION"?void 0:Y}if(e==="OPTION"||e==="SELECT")return t.value;if(!(e!=="INPUT"&&e!=="TEXTAREA"))return o}const Yt=/url\((?:(')([^']*)'|(")([^"]*)"|([^)]*))\)/gm,Xt=/^[A-Za-z]+:|^\/\//,Wt=/^["']?data:.*,/i;function qt(t,n){return t.replace(Yt,(e,o,s,r,u,i)=>{const a=s||u||i;if(!n||!a||Xt.test(a)||Wt.test(a))return e;const c=o||r||"";return`url(${c}${Jt(a,n)}${c})`})}function Jt(t,n){try{return pt(t,n).href}catch{return t}}const Kt=/[^a-z1-6-_]/;function jt(t){const n=t.toLowerCase().trim();return Kt.test(n)?"div":n}function Q(t){return t.tagName.toLowerCase()}function F(t,n){return`data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='${t}' height='${n}' style='background-color:silver'%3E%3C/svg%3E`}function Z(t){if(!(t===void 0||t.length===0))return t.map(n=>{const e=n.cssRules||n.rules;return{cssRules:Array.from(e,r=>r.cssText),disabled:n.disabled||void 0,media:n.media.length>0?Array.from(n.media):void 0}})}const Qt=1e6;function tt(t,n,e,o){if(n===h.HIDDEN)return null;const s=t.getAttribute(e),r=t.tagName;if(ht(r,e,s,n,o)){if(r==="IMG"){const u=t;if(u.naturalWidth>0)return F(u.naturalWidth,u.naturalHeight);const{width:i,height:a}=t.getBoundingClientRect();return i>0||a>0?F(i,a):k}return r==="SOURCE"?k:Y}return s&&gt(s,Qt)}function Zt(t,n,e){return{...te(t,n,e),...ee(t,n,e)}}function te(t,n,e){if(n===h.HIDDEN)return{};const o={},s=Q(t);for(let u=0;u<t.attributes.length;u+=1){const a=t.attributes.item(u).name,c=tt(t,n,a,e.scope.configuration);c!==null&&(o[a]=c)}if(t.value&&(s==="textarea"||s==="select"||s==="option"||s==="input")){const u=L(t,n);u!==void 0&&(o.value=u)}if(s==="option"){const u=t;u.selected&&!x(u,n)?o.selected="":delete o.selected}const r=t;return s==="input"&&(r.type==="radio"||r.type==="checkbox")&&(r.checked&&!x(r,n)?o.checked="":delete o.checked),o}function ee(t,n,e){if(n===h.HIDDEN)return{};const o={},s=t.ownerDocument,r=Q(t);if(r==="link"){const a=Array.from(s.styleSheets).find(d=>d.href===t.href),c=D(a);c&&a&&(e.addMetric("cssText",c.length),o._cssText=c)}if(r==="style"&&t.sheet){const a=D(t.sheet);a&&(e.addMetric("cssText",a.length),o._cssText=a)}if(r==="audio"||r==="video"){const a=t;o.rr_mediaState=a.paused?"paused":"played"}let u,i;switch(e.kind){case 0:u=Math.round(t.scrollTop),i=Math.round(t.scrollLeft),(u||i)&&e.scope.elementsScrollPositions.set(t,{scrollTop:u,scrollLeft:i});break;case 1:e.scope.elementsScrollPositions.has(t)&&({scrollTop:u,scrollLeft:i}=e.scope.elementsScrollPositions.get(t));break}return i&&(o.rr_scrollLeft=i),u&&(o.rr_scrollTop=u),o}function D(t){if(!t)return null;let n;try{n=t.rules||t.cssRules}catch{}if(!n)return null;const e=Array.from(n,mt()?ne:et).join("");return qt(e,t.href)}function ne(t){if(se(t)&&t.selectorText.includes(":")){const n=/(\[[\w-]+[^\\])(:[^\]]+\])/g;return t.cssText.replace(n,"$1\\$2")}return et(t)}function et(t){return oe(t)&&D(t.styleSheet)||t.cssText}function oe(t){return"styleSheet"in t}function se(t){return"selectorText"in t}function A(t,n,e){switch(t.nodeType){case t.DOCUMENT_NODE:return re(t,n,e);case t.DOCUMENT_FRAGMENT_NODE:return ie(t,n,e);case t.DOCUMENT_TYPE_NODE:return ue(t,e);case t.ELEMENT_NODE:return ae(t,n,e);case t.TEXT_NODE:return de(t,n,e);case t.CDATA_SECTION_NODE:return le(t,e);default:return null}}function V(t,n,e){const o=[];return q(t,s=>{const r=A(s,n,e);r&&o.push(r)}),o}function re(t,n,e){return{type:w.Document,id:e.assignId(t),childNodes:V(t,n,e),adoptedStyleSheets:Z(t.adoptedStyleSheets)}}function ie(t,n,e){const o=W(t);return o&&e.scope.shadowRootsController.addShadowRoot(t,e.scope),{type:w.DocumentFragment,id:e.assignId(t),childNodes:V(t,n,e),isShadowRoot:o,adoptedStyleSheets:o?Z(t.adoptedStyleSheets):void 0}}function ue(t,n){return{type:w.DocumentType,id:n.assignId(t),name:t.name,publicId:t.publicId,systemId:t.systemId}}function ae(t,n,e){const o=jt(t.tagName),s=ce(t)||void 0,r=St(Tt(t),n);if(r===h.HIDDEN){const{width:c,height:d}=t.getBoundingClientRect();return{type:w.Element,id:e.assignId(t),tagName:o,attributes:{rr_width:`${c}px`,rr_height:`${d}px`,[yt]:Et},childNodes:[],isSVG:s}}if(r===h.IGNORE)return null;const u=e.assignId(t),i=Zt(t,r,e);let a=[];return It(t)&&o!=="style"&&(a=V(t,r,e)),{type:w.Element,id:u,tagName:o,attributes:i,childNodes:a,isSVG:s}}function ce(t){return t.tagName==="svg"||t instanceof SVGElement}function de(t,n,e){const o=X(t,n);return o===void 0?null:{type:w.Text,id:e.assignId(t),textContent:o}}function le(t,n){return{type:w.CDATA,id:n.assignId(t),textContent:""}}function fe(t,n){const e=n.scope.configuration.defaultPrivacyLevel;return A(t,e,n)}function nt(){return{cssText:{count:0,max:0,sum:0},serializationDuration:{count:0,max:0,sum:0}}}function H(t,n,e){t[n].count+=1,t[n].max=Math.max(t[n].max,e),t[n].sum+=e}function pe(t,n){for(const e of["cssText","serializationDuration"])t[e].count+=n[e].count,t[e].max=Math.max(t[e].max,n[e].max),t[e].sum+=n[e].sum}function ot(t,n,e,o,s){const r=[],u=nt(),i={add(c){r.push(c)},addMetric(c,d){H(u,c,d)},assignId(c){const d=o.nodeIds.getOrInsert(c);return i.serializedNodeIds&&i.serializedNodeIds.add(d),d},kind:t,scope:o},a=I();s(i),H(u,"serializationDuration",Nt(a,I()));for(const c of r)n(c);e(u)}const U=25;function he(t){return Math.abs(t.pageTop-t.offsetTop-window.scrollY)>U||Math.abs(t.pageLeft-t.offsetLeft-window.scrollX)>U}const ge=(t,n)=>{const e=window.visualViewport,o={layoutViewportX:t,layoutViewportY:n,visualViewportX:t,visualViewportY:n};if(e)he(e)?(o.layoutViewportX=Math.round(t+e.offsetLeft),o.layoutViewportY=Math.round(n+e.offsetTop)):(o.visualViewportX=Math.round(t-e.offsetLeft),o.visualViewportY=Math.round(n-e.offsetTop));else return o;return o},st=t=>({scale:t.scale,offsetLeft:t.offsetLeft,offsetTop:t.offsetTop,pageLeft:t.pageLeft,pageTop:t.pageTop,height:t.height,width:t.width});function me(t,n,e,o,s){B(I(),0,n,e,s);const{unsubscribe:r}=t.subscribe(2,u=>{o(),B(u.startClocks.timeStamp,1,n,e,s)});return{stop:r}}function B(t,n,e,o,s){ot(n,e,o,s,r=>{const{width:u,height:i}=wt();r.add({data:{height:i,href:window.location.href,width:u},type:T.Meta,timestamp:t}),r.add({data:{has_focus:document.hasFocus()},type:T.Focus,timestamp:t}),r.add({data:{node:fe(document,r),initialOffset:{left:K(),top:J()}},type:T.FullSnapshot,timestamp:t}),window.visualViewport&&r.add({data:st(window.visualViewport),type:T.VisualViewport,timestamp:t})})}function Se(){return z(1)}function Te(){return z(0)}function Ee(){return Ie(0)}function ye(){return z(0)}function Ie(t){return rt(()=>new Map,t)}function z(t){return rt(()=>new WeakMap,t)}function rt(t,n){let e=t(),o=n;const s=r=>e.get(r);return{clear(){e=t(),o=n},get:s,getOrInsert(r){let u=s(r);return u===void 0&&(u=o++,e.set(r,u)),u},get size(){return o-n}}}function Ne(t,n,e){const o=Se(),s=Te(),r=Ee(),u=ye();return{configuration:t,elementsScrollPositions:n,eventIds:o,nodeIds:s,shadowRootsController:e,stringIds:r,styleSheetIds:u}}function we(){const t=new WeakMap;return{set(n,e){n===document&&!document.scrollingElement||t.set(n===document?document.scrollingElement:n,e)},get(n){return t.get(n)},has(n){return t.has(n)}}}function it(t){return!!t.changedTouches}function C(t){return t.composed===!0&&j(t.target)?t.composedPath()[0]:t.target}function S(t,n){return{data:{source:t,...n},type:T.IncrementalSnapshot,timestamp:I()}}const Me=50;function be(t,n){const{throttled:e,cancel:o}=_(r=>{const u=C(r),i=n.nodeIds.get(u);if(i===void 0)return;const a=ut(r);if(!a)return;const c={id:i,timeOffset:0,x:a.x,y:a.y};t(S(it(r)?g.TouchMove:g.MouseMove,{positions:[c]}))},Me,{trailing:!1}),{stop:s}=M(n.configuration,document,["mousemove","touchmove"],e,{capture:!0,passive:!0});return{stop:()=>{s(),o()}}}function ut(t){let{clientX:n,clientY:e}=it(t)?t.changedTouches[0]:t;if(window.visualViewport){const{visualViewportX:o,visualViewportY:s}=ge(n,e);n=o,e=s}if(!(!Number.isFinite(n)||!Number.isFinite(e)))return{x:n,y:e}}const G={pointerup:m.MouseUp,mousedown:m.MouseDown,click:m.Click,contextmenu:m.ContextMenu,dblclick:m.DblClick,focus:m.Focus,blur:m.Blur,touchstart:m.TouchStart,touchend:m.TouchEnd};function ve(t,n){const e=o=>{const s=C(o),r=n.nodeIds.get(s);if(r===void 0||y(s,n.configuration.defaultPrivacyLevel)===h.HIDDEN)return;const u=G[o.type];let i;if(u!==m.Blur&&u!==m.Focus){const a=ut(o);if(!a)return;i={id:r,type:u,x:a.x,y:a.y}}else i={id:r,type:u};t({id:n.eventIds.getOrInsert(o),...S(g.MouseInteraction,i)})};return M(n.configuration,document,Object.keys(G),e,{capture:!0,passive:!0})}const Ce=100;function at(t,n,e){const{throttled:o,cancel:s}=_(u=>{const i=C(u);if(!i)return;const a=e.nodeIds.get(i);if(a===void 0||y(i,e.configuration.defaultPrivacyLevel)===h.HIDDEN)return;const c=i===document?{scrollTop:J(),scrollLeft:K()}:{scrollTop:Math.round(i.scrollTop),scrollLeft:Math.round(i.scrollLeft)};e.elementsScrollPositions.set(i,c),n(S(g.Scroll,{id:a,x:c.scrollLeft,y:c.scrollTop}))},Ce),{stop:r}=Mt(e.configuration,t,"scroll",o,{capture:!0,passive:!0});return{stop:()=>{r(),s()}}}const Re=200;function xe(t,n){const e=bt(n.configuration).subscribe(o=>{t(S(g.ViewportResize,o))});return{stop:()=>{e.unsubscribe()}}}function _e(t,n){const e=window.visualViewport;if(!e)return{stop:N};const{throttled:o,cancel:s}=_(()=>{t({data:st(e),type:T.VisualViewport,timestamp:I()})},Re,{trailing:!1}),{stop:r}=M(n.configuration,e,["resize","scroll"],o,{capture:!0,passive:!0});return{stop:()=>{r(),s()}}}function De(t,n){return M(n.configuration,document,["play","pause"],e=>{const o=C(e);if(!o)return;const s=n.nodeIds.get(o);s===void 0||y(o,n.configuration.defaultPrivacyLevel)===h.HIDDEN||t(S(g.MediaInteraction,{id:s,type:e.type==="play"?P.Play:P.Pause}))},{capture:!0,passive:!0})}function Oe(t,n){function e(r,u){if(!r||!r.ownerNode)return;const i=n.nodeIds.get(r.ownerNode);i!==void 0&&u(i)}const o=[R(CSSStyleSheet.prototype,"insertRule",({target:r,parameters:[u,i]})=>{e(r,a=>t(S(g.StyleSheetRule,{id:a,adds:[{rule:u,index:i}]})))}),R(CSSStyleSheet.prototype,"deleteRule",({target:r,parameters:[u]})=>{e(r,i=>t(S(g.StyleSheetRule,{id:i,removes:[{index:u}]})))})];typeof CSSGroupingRule<"u"?s(CSSGroupingRule):(s(CSSMediaRule),s(CSSSupportsRule));function s(r){o.push(R(r.prototype,"insertRule",({target:u,parameters:[i,a]})=>{e(u.parentStyleSheet,c=>{const d=$(u);d&&(d.push(a||0),t(S(g.StyleSheetRule,{id:c,adds:[{rule:i,index:d}]})))})}),R(r.prototype,"deleteRule",({target:u,parameters:[i]})=>{e(u.parentStyleSheet,a=>{const c=$(u);c&&(c.push(i),t(S(g.StyleSheetRule,{id:a,removes:[{index:c}]})))})}))}return{stop:()=>{o.forEach(r=>r.stop())}}}function $(t){const n=[];let e=t;for(;e.parentRule;){const u=Array.from(e.parentRule.cssRules).indexOf(e);n.unshift(u),e=e.parentRule}if(!e.parentStyleSheet)return;const s=Array.from(e.parentStyleSheet.cssRules).indexOf(e);return n.unshift(s),n}function Le(t,n){return M(n.configuration,window,["focus","blur"],()=>{t({data:{has_focus:document.hasFocus()},type:T.Focus,timestamp:I()})})}function Ae(t,n,e){const o=t.subscribe(12,s=>{var r,u;s.rawRumEvent.type===vt.ACTION&&s.rawRumEvent.action.type===Ct.CLICK&&(!((u=(r=s.rawRumEvent.action.frustration)===null||r===void 0?void 0:r.type)===null||u===void 0)&&u.length)&&"events"in s.domainContext&&s.domainContext.events&&s.domainContext.events.length&&n({timestamp:s.rawRumEvent.date,type:T.FrustrationRecord,data:{frustrationTypes:s.rawRumEvent.action.frustration.type,recordIds:s.domainContext.events.map(i=>e.eventIds.getOrInsert(i))}})});return{stop:()=>{o.unsubscribe()}}}function Ve(t,n,e){const o=t.subscribe(5,()=>{e(),n({timestamp:I(),type:T.ViewEnd})});return{stop:()=>{o.unsubscribe()}}}function ct(t,n,e){const o=e.configuration.defaultPrivacyLevel,s=new WeakMap,r=t!==document,{stop:u}=M(e.configuration,t,r?["change"]:["input","change"],d=>{const l=C(d);(l instanceof HTMLInputElement||l instanceof HTMLTextAreaElement||l instanceof HTMLSelectElement)&&a(l)},{capture:!0,passive:!0});let i;if(r)i=N;else{const d=[v(HTMLInputElement.prototype,"value",a),v(HTMLInputElement.prototype,"checked",a),v(HTMLSelectElement.prototype,"value",a),v(HTMLTextAreaElement.prototype,"value",a),v(HTMLSelectElement.prototype,"selectedIndex",a)];i=()=>{d.forEach(l=>l.stop())}}return{stop:()=>{i(),u()}};function a(d){const l=y(d,o);if(l===h.HIDDEN)return;const f=d.type;let p;if(f==="radio"||f==="checkbox"){if(x(d,l))return;p={isChecked:d.checked}}else{const b=L(d,l);if(b===void 0)return;p={text:b}}c(d,p);const E=d.name;f==="radio"&&E&&d.checked&&document.querySelectorAll(`input[type="radio"][name="${CSS.escape(E)}"]`).forEach(b=>{b!==d&&c(b,{isChecked:!1})})}function c(d,l){const f=e.nodeIds.get(d);if(f===void 0)return;const p=s.get(d);(!p||p.text!==l.text||p.isChecked!==l.isChecked)&&(s.set(d,l),n(S(g.Input,{id:f,...l})))}}const ze=100,ke=16;function Pe(t){let n=N,e=[];function o(){n(),t(e),e=[]}const{throttled:s,cancel:r}=_(o,ke,{leading:!1});return{addMutations:u=>{e.length===0&&(n=Rt(s,{timeout:ze})),e.push(...u)},flush:o,stop:()=>{n(),r()}}}function dt(t,n,e,o){const s=xt();if(!s)return{stop:N,flush:N};const r=Pe(i=>{ot(2,n,e,o,a=>Fe(i.concat(u.takeRecords()),a))}),u=new s(_t(r.addMutations));return u.observe(t,{attributeOldValue:!0,attributes:!0,characterData:!0,characterDataOldValue:!0,childList:!0,subtree:!0}),{stop:()=>{u.disconnect(),r.stop()},flush:()=>{r.flush()}}}function Fe(t,n){const e=new Map;t.filter(c=>c.type==="childList").forEach(c=>{c.removedNodes.forEach(d=>{lt(d,n.scope.shadowRootsController.removeShadowRoot)})});const o=t.filter(c=>c.target.isConnected&&$e(c.target,n.scope.nodeIds)&&y(c.target,n.scope.configuration.defaultPrivacyLevel,e)!==h.HIDDEN),{adds:s,removes:r,hasBeenSerialized:u}=He(o.filter(c=>c.type==="childList"),e,n),i=Ue(o.filter(c=>c.type==="characterData"&&!u(c.target)),e,n),a=Be(o.filter(c=>c.type==="attributes"&&!u(c.target)),e,n);!i.length&&!a.length&&!r.length&&!s.length||n.add(S(g.Mutation,{adds:s,removes:r,texts:i,attributes:a}))}function He(t,n,e){const o=new Set,s=new Map;for(const d of t)d.addedNodes.forEach(l=>{o.add(l)}),d.removedNodes.forEach(l=>{o.has(l)||s.set(l,d.target),o.delete(l)});const r=Array.from(o);Ge(r),e.serializedNodeIds=new Set;const u=[];for(const d of r){if(a(d))continue;const l=y(d.parentNode,e.scope.configuration.defaultPrivacyLevel,n);if(l===h.HIDDEN||l===h.IGNORE)continue;const f=A(d,l,e);if(!f)continue;const p=O(d);u.push({nextId:c(d),parentId:e.scope.nodeIds.get(p),node:f})}const i=[];return s.forEach((d,l)=>{const f=e.scope.nodeIds.get(d),p=e.scope.nodeIds.get(l);f!==void 0&&p!==void 0&&i.push({parentId:f,id:p})}),{adds:u,removes:i,hasBeenSerialized:a};function a(d){var l;const f=e.scope.nodeIds.get(d);return f!==void 0&&((l=e.serializedNodeIds)===null||l===void 0?void 0:l.has(f))}function c(d){let l=d.nextSibling;for(;l;){const f=e.scope.nodeIds.get(l);if(f!==void 0)return f;l=l.nextSibling}return null}}function Ue(t,n,e){var o;const s=[],r=new Set,u=t.filter(i=>r.has(i.target)?!1:(r.add(i.target),!0));for(const i of u){if(i.target.textContent===i.oldValue)continue;const c=e.scope.nodeIds.get(i.target);if(c===void 0)continue;const d=y(O(i.target),e.scope.configuration.defaultPrivacyLevel,n);d===h.HIDDEN||d===h.IGNORE||s.push({id:c,value:(o=X(i.target,d))!==null&&o!==void 0?o:null})}return s}function Be(t,n,e){const o=[],s=new Map,r=t.filter(i=>{const a=s.get(i.target);return a&&a.has(i.attributeName)?!1:(a?a.add(i.attributeName):s.set(i.target,new Set([i.attributeName])),!0)}),u=new Map;for(const i of r){if(i.target.getAttribute(i.attributeName)===i.oldValue)continue;const c=e.scope.nodeIds.get(i.target);if(c===void 0)continue;const d=y(i.target,e.scope.configuration.defaultPrivacyLevel,n),l=tt(i.target,d,i.attributeName,e.scope.configuration);let f;if(i.attributeName==="value"){const E=L(i.target,d);if(E===void 0)continue;f=E}else typeof l=="string"?f=l:f=null;let p=u.get(i.target);p||(p={id:c,attributes:{}},o.push(p),u.set(i.target,p)),p.attributes[i.attributeName]=f}return o}function Ge(t){t.sort((n,e)=>{const o=n.compareDocumentPosition(e);return o&Node.DOCUMENT_POSITION_CONTAINED_BY?-1:o&Node.DOCUMENT_POSITION_CONTAINS||o&Node.DOCUMENT_POSITION_FOLLOWING?1:o&Node.DOCUMENT_POSITION_PRECEDING?-1:0})}function lt(t,n){j(t)&&n(t.shadowRoot),q(t,e=>lt(e,n))}function $e(t,n){let e=t;for(;e;){if(n.get(e)===void 0&&!W(e))return!1;e=O(e)}return!0}const Ye=(t,n)=>{const e=new Map;return{addShadowRoot:(s,r)=>{if(e.has(s))return;const u=dt(s,t,n,r),i=ct(s,t,r),a=at(s,t,r);e.set(s,{flush:()=>u.flush(),stop:()=>{u.stop(),i.stop(),a.stop()}})},removeShadowRoot:s=>{const r=e.get(s);r&&(r.stop(),e.delete(s))},stop:()=>{e.forEach(({stop:s})=>s())},flush:()=>{e.forEach(({flush:s})=>s())}}};function Xe(t){const{emitRecord:n,emitStats:e,configuration:o,lifeCycle:s}=t;if(!n||!e)throw new Error("emit functions are required");const r=f=>{n(f),Dt("record",{record:f});const p=t.viewHistory.findView();Ot(p.id)},u=Ye(r,e),i=Ne(o,we(),u),{stop:a}=me(s,r,e,c,i);function c(){u.flush(),d.flush()}const d=dt(document,r,e,i),l=[d,be(r,i),ve(r,i),at(document,r,i),xe(r,i),ct(document,r,i),De(r,i),Oe(r,i),Le(r,i),_e(r,i),Ae(s,r,i),Ve(s,r,c)];return{stop:()=>{u.stop(),l.forEach(f=>f.stop()),a()},flushMutations:c,shadowRootsController:u}}function We(t,n,e,o){const s=new FormData;s.append("segment",new Blob([t],{type:"application/octet-stream"}),`${n.session.id}-${n.start}`);const r={raw_segment_size:o,compressed_segment_size:t.byteLength,...n},u=JSON.stringify(r);return s.append("event",new Blob([u],{type:"application/json"})),{data:s,bytesCount:t.byteLength,cssText:e.cssText,isFullSnapshot:n.index_in_view===0,rawSize:o,recordCount:n.records_count,serializationDuration:e.serializationDuration}}function qe({context:t,creationReason:n,encoder:e}){let o=0;const s=t.view.id,r=Lt(s),u={start:1/0,end:-1/0,creation_reason:n,records_count:0,has_full_snapshot:!1,index_in_view:r,source:"browser",...t},i=nt();At(s);function a(l,f){u.start=Math.min(u.start,l.timestamp),u.end=Math.max(u.end,l.timestamp),u.records_count+=1,u.has_full_snapshot||(u.has_full_snapshot=l.type===T.FullSnapshot);const p=e.isEmpty?'{"records":[':",";e.write(p+JSON.stringify(l),E=>{o+=E,f(o)})}function c(l){pe(i,l)}function d(l){if(e.isEmpty)throw new Error("Empty segment flushed");e.write(`],${JSON.stringify(u).slice(1)} `),e.finish(f=>{Vt(u.view.id,f.rawBytesCount),l(u,i,f)})}return{addRecord:a,addStats:c,flush:d}}const Je=5*Ft;let ft=6e4;function Ke(t,n,e,o,s,r){return je(t,()=>Qe(n.applicationId,e,o),s,r)}function je(t,n,e,o){let s={status:0,nextSegmentCreationReason:"init"};const{unsubscribe:r}=t.subscribe(2,()=>{i("view_change")}),{unsubscribe:u}=t.subscribe(11,a=>{i(a.reason)});function i(a){s.status===1&&(s.segment.flush((c,d,l)=>{const f=We(l.output,c,d,l.rawBytesCount);zt(a)?e.sendOnExit(f):e.send(f)}),kt(s.expirationTimeoutId)),a!=="stop"?s={status:0,nextSegmentCreationReason:a}:s={status:2}}return{addRecord:a=>{if(s.status!==2){if(s.status===0){const c=n();if(!c)return;s={status:1,segment:qe({encoder:o,context:c,creationReason:s.nextSegmentCreationReason}),expirationTimeoutId:Pt(()=>{i("segment_duration_limit")},Je)}}s.segment.addRecord(a,c=>{c>ft&&i("segment_bytes_limit")})}},addStats:a=>{s.status===1&&s.segment.addStats(a)},stop:()=>{i("stop"),r(),u()}}}function Qe(t,n,e){const o=n.findTrackedSession(),s=e.findView();if(!(!o||!s))return{application:{id:t},session:{id:o.id},view:{id:s.id}}}function Ze(t,n){if(!t.metricsEnabled)return{stop:N};const{unsubscribe:e}=n.subscribe(o=>{if(o.type==="failure"||o.type==="queue-full"||o.type==="success"&&o.payload.isFullSnapshot){const s=tn(o.type,o.bandwidth,o.payload);Ht("Segment network request metrics",{metrics:s})}});return{stop:e}}function tn(t,n,e){return{cssText:{count:e.cssText.count,max:e.cssText.max,sum:e.cssText.sum},isFullSnapshot:e.isFullSnapshot,ongoingRequests:{count:n.ongoingRequestCount,totalSize:n.ongoingByteCount},recordCount:e.recordCount,result:t,serializationDuration:{count:e.serializationDuration.count,max:e.serializationDuration.max,sum:e.serializationDuration.sum},size:{compressed:e.bytesCount,raw:e.rawSize}}}function en(t){const n=Ut();return{addRecord:e=>{const o=t.findView();n.send("record",e,o.id)}}}function on(t,n,e,o,s,r,u){const i=[],a=p=>{t.notify(14,{error:p}),$t("Error reported to customer",{"error.message":p.message})},c=u||Bt([n.sessionReplayEndpointBuilder],a,ft);let d,l;if(Gt())({addRecord:d}=en(o)),l=N;else{const p=Ke(t,n,e,o,c,s);d=p.addRecord,l=p.addStats,i.push(p.stop);const E=Ze(r,c.observable);i.push(E.stop)}const{stop:f}=Xe({emitRecord:d,emitStats:l,configuration:n,lifeCycle:t,viewHistory:o});return i.push(f),{stop:()=>{i.forEach(p=>p())}}}export{on as startRecording};