UNPKG

harperdb

Version:

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

2 lines (1 loc) 5.07 kB
import{V as x,ak as _,al as C,X as G,am as X,an as Q,ao as y,w as N,ap as J,aq as K,Q as W,ar as R,O as Y,l as Z,as as $}from"./index-CkEccPko.js";function ee(e,t,r,a){const u=o=>{t.notify(14,{error:o}),G("Error reported to customer",{"error.message":o.message})},d=x([e.profilingEndpointBuilder],u),f=r(a);return{async send({event:o,...E}){const p=new FormData,m=_(o);if(!m)throw new Error("Failed to serialize event");p.append("event",new Blob([m],{type:"application/json"}),"event.json");let s=m.length;for(const[v,k]of C(E)){const w=_(k);if(!w)throw new Error("Failed to serialize attachment");const g=await te(f,w);s+=g.outputBytesCount,p.append(v,new Blob([g.output]),v)}d.send({data:p,bytesCount:s})}}}function te(e,t){return new Promise(r=>{e.write(t),e.finish(a=>{r(a)})})}function ne(e){let t=0;for(const r of e)r.stackId!==void 0&&t++;return t}const se=/\/(?![vV]\d{1,2}\/)([^/\d?]*\d+[^/?]*)/g;function re(e){return e?e.replace(se,"/?"):"/"}const j=(e,t)=>e||re(t);function ae(e,t,r){const a={application:{id:t}};r&&(a.session={id:r});const{ids:u,names:d}=oe(e.views);u.length&&(a.view={id:u,name:d});const f=e.longTasks.map(o=>o.id).filter(o=>o!==void 0);return f.length&&(a.long_task={id:f}),a}function oe(e){const t={ids:[],names:[]};for(const r of e)t.ids.push(r.viewId),r.viewName&&t.names.push(r.viewName);return t.names=Array.from(new Set(t.names)),t}function ie(e,t,r){return{event:ce(e,t,r),"wall-time.json":e}}function ce(e,t,r){const a=X(t),u=ae(e,t.applicationId,r),d=le(a);return{...u,attachments:["wall-time.json"],start:new Date(e.startClocks.timeStamp).toISOString(),end:new Date(e.endClocks.timeStamp).toISOString(),family:"chrome",runtime:"chrome",format:"json",version:4,tags_profiler:d.join(","),_dd:{clock_drift:Q()}}}function le(e){return e.concat(["language:javascript","runtime:chrome","family:chrome","host:browser"])}const ue={sampleIntervalMs:10,collectIntervalMs:6e4,minProfileDurationMs:5e3,minNumberOfSamples:50};function fe(e,t,r,a,u,d,f,o=ue){const E=ee(e,t,d,6);let p;const m=[];let s={state:"stopped",stateReason:"initializing"};t.subscribe(9,()=>{w("session-expired").catch(y)}),t.subscribe(10,()=>{s.state==="stopped"&&s.stateReason==="session-expired"&&v()});function v(){if(s.state==="running")return;const n=f.findView();p=n?{startClocks:n.startClocks,viewId:n.id,viewName:j(n.name,document.location.pathname)}:void 0,m.push(N(e,window,"visibilitychange",B).stop,N(e,window,"beforeunload",L).stop),h()}async function k(){await w("stopped-by-user")}async function w(n){await M(n),m.forEach(i=>i()),a.set({status:"stopped",error_reason:void 0})}function g(n){if(n.state==="running")return{cleanupTasks:n.cleanupTasks};const i=[],l=t.subscribe(2,c=>{const b={viewId:c.id,viewName:j(c.name,document.location.pathname),startClocks:c.startClocks};T(b),p=b});return i.push(l.unsubscribe),{cleanupTasks:i}}function h(){const n=J().Profiler;if(!n)throw a.set({status:"error",error_reason:"not-supported-by-browser"}),new Error("RUM Profiler is not supported in this browser.");P(s).catch(y);const{cleanupTasks:i}=g(s);let l;try{l=new n({sampleInterval:o.sampleIntervalMs,maxBufferSize:Math.round(o.collectIntervalMs*1.5/o.sampleIntervalMs)})}catch(c){c instanceof Error&&c.message.includes("disabled by Document Policy")?(K.warn("[DD_RUM] Profiler startup failed. Ensure your server includes the `Document-Policy: js-profiling` response header when serving HTML pages.",c),a.set({status:"error",error_reason:"missing-document-policy-header"})):a.set({status:"error",error_reason:"unexpected-exception"});return}a.set({status:"running",error_reason:void 0}),s={state:"running",startClocks:R(),profiler:l,timeoutId:W(h,o.collectIntervalMs),views:[],cleanupTasks:i,longTasks:[]},T(p),l.addEventListener("samplebufferfull",S)}async function P(n){if(n.state!=="running")return;Y(n.timeoutId),n.profiler.removeEventListener("samplebufferfull",S);const{startClocks:i,views:l}=n;await n.profiler.stop().then(c=>{const b=R(),D=Z(i.timeStamp,b.timeStamp),O=u.findLongTasks(i.relative,D),H=D<o.minProfileDurationMs,q=ne(c.samples)<o.minNumberOfSamples;O.length===0&&(H||q)||A(Object.assign(c,{startClocks:i,endClocks:b,clocksOrigin:$(),longTasks:O,views:l,sampleInterval:o.sampleIntervalMs}))}).catch(y)}async function M(n){s.state==="running"&&(await I(),s={state:"stopped",stateReason:n})}async function V(){s.state==="running"&&(await I(),s={state:"paused"})}async function I(){s.state==="running"&&(s.cleanupTasks.forEach(n=>n()),await P(s))}function T(n){s.state!=="running"||!n||s.views.push(n)}function A(n){var i;const l=(i=r.findTrackedSession())===null||i===void 0?void 0:i.id,c=ie(n,e,l);E.send(c)}function S(){h()}function B(){document.visibilityState==="hidden"&&s.state==="running"?V().catch(y):document.visibilityState==="visible"&&s.state==="paused"&&h()}function L(){h()}function F(){return s.state==="stopped"}function U(){return s.state==="running"}function z(){return s.state==="paused"}return{start:v,stop:k,isStopped:F,isRunning:U,isPaused:z}}export{ue as DEFAULT_RUM_PROFILER_CONFIGURATION,fe as createRumProfiler};