// @bun
import{ak as d}from"./chunk-ss1ckkfc.js";import{mk as n}from"./chunk-y5859m2g.js";import{El as p}from"./chunk-cvshp0d0.js";import{Fq as N,aq as S,eq as D,vs as P}from"./chunk-9fn5qenm.js";import{At as f,Ct as O}from"./chunk-g2rmdpb3.js";import{NG as u}from"./chunk-xyhgvt88.js";var h={};u(h,{listen:()=>x,emit:()=>E,client:()=>J,Rpc:()=>h});function x(r){onmessage=async(e)=>{let s=JSON.parse(e.data);if(s.type==="rpc.request"){let o=await r[s.method](s.input);postMessage(JSON.stringify({type:"rpc.result",result:o,id:s.id}))}}}function E(r,e){postMessage(JSON.stringify({type:"rpc.event",event:r,data:e}))}function J(r){let e=new Map,s=new Map,o=0;return r.onmessage=async(i)=>{let t=JSON.parse(i.data);if(t.type==="rpc.result"){let a=e.get(t.id);if(a)a(t.result),e.delete(t.id)}if(t.type==="rpc.event"){let a=s.get(t.event);if(a)for(let g of a)g(t.data)}},{call(i,t){let a=o++;return new Promise((g)=>{e.set(a,g),r.postMessage(JSON.stringify({type:"rpc.request",method:i,input:t,id:a}))})},on(i,t){let a=s.get(i);if(!a)a=new Set,s.set(i,a);return a.add(t),()=>{a.delete(t)}}}}var m={};u(m,{reloadInstance:()=>H,load:()=>U,disposeInstance:()=>k,disposeAllInstances:()=>l,InstanceRuntime:()=>m});var U=(r)=>d.runPromise(p.Service.use((e)=>e.load(r))),k=(r)=>d.runPromise(p.Service.use((e)=>e.dispose(r))),l=()=>d.runPromise(p.Service.use((r)=>r.disposeAll())),H=(r)=>d.runPromise(p.Service.use((e)=>e.reload(r)));var T={};u(T,{start:()=>j,Heap:()=>T});import _ from"path";import{writeHeapSnapshot as q}from"v8";var v=S({service:"heap"}),L=60000,$=2147483648,y,M=!1,w=!0;function j(){if(!f.MUXCODE_AUTO_HEAP_SNAPSHOT)return;if(y)return;let r=async()=>{if(M)return;let e=process.memoryUsage();if(e.rss<=$){w=!0;return}if(!w)return;M=!0,w=!1;let s=_.join(O.Path.log,`heap-${process.pid}-${new Date().toISOString().replace(/[:.]/g,"")}.heapsnapshot`);v.warn("heap usage exceeded limit",{rss:e.rss,heap:e.heapUsed,file:s}),await Promise.resolve().then(()=>q(s)).catch((o)=>{v.error("failed to write heap snapshot",{error:o instanceof Error?o.message:String(o),file:s})}),M=!1};y=setInterval(()=>{r()},L),y.unref?.()}var c=(r,e)=>N.emit("event",{directory:"global",payload:{type:r,properties:{version:e}}}),W=(r)=>new Promise((e)=>setTimeout(e,r));async function R(){let r=await d.runPromise(P.Service.use((t)=>t.getGlobal())).catch(()=>({}));if(r.autoupdate===!1||f.MUXCODE_DISABLE_AUTOUPDATE)return;let e=await n.method().catch(()=>"unknown"),s=await n.latest(e).catch(()=>{return});if(!s)return;if(D===s)return;if(await W(2000),f.MUXCODE_ALWAYS_NOTIFY_UPDATE||r.autoupdate==="notify"){c(n.Event.UpdateAvailable.type,s);return}c(n.Event.UpdateAvailable.type,s);let o=e==="unknown"?"npm":e;if(await n.upgrade(o,s).then(()=>!0).catch(()=>!1))c(n.Event.Updated.type,s);else c(n.Event.UpdateFailed.type,s)}
export{h as Pj,m as Qj,T as Rj,R as Sj};
