@urun-sh/core 0.1.34 → 0.1.36

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.mjs CHANGED
@@ -1 +1 @@
1
- import{a as M,b as v,c as k}from"./chunk-TSVDZX3O.mjs";var H="session-api.prod.cloud.urun.sh",K=["https://session-api.use2.prod.cloud.urun.sh","https://session-api.usw2.prod.cloud.urun.sh"];function b(n){return n.trim().replace(/\/+$/,"")}function C(n,e){let t=[],r=s=>{if(!s)return;let o=b(s);o&&!t.includes(o)&&t.push(o)};r(n);for(let s of e??[])r(s);for(let s of ee(n))r(s);return t}var q=120,V=1e3,W=3;function Y(n){if(!n)return null;try{let e=new URL(n),t=e.pathname.replace(/\/api\/sessions\/create\/?$/,"");return b(`${e.origin}${t}`)}catch{return null}}async function L(n){let e=C(n.baseUrl,n.fallbackUrls),t=null,r=e.length>1;for(let s of e)try{return await z(s,n,{pollAttempts:q,retryAfterLimitMs:r?V:void 0})}catch(o){if(t=o instanceof Error?o:new Error(String(o)),t instanceof w)throw t}throw t??new Error("[urun] session allocation failed")}async function z(n,e,t){let r,s,o,i=n,l=0,h=new Set;for(let p=0;p<t.pollAttempts;p++){s=o??await E(e),o=void 0;let d=r?await fetch(`${i}/api/session-requests/${encodeURIComponent(r)}`,{headers:A(e,s)}):await fetch(`${i}/api/sessions/create`,{method:"POST",redirect:"manual",headers:{...A(e,s),"Content-Type":"application/json","Idempotency-Key":e.idempotencyKey},body:JSON.stringify(X(e))});if(!r&&(d.status===307||d.status===308)){let u=Y(d.headers.get("location"));if(u&&u!==i&&l<W){l+=1,i=u;continue}throw new Error(`[urun] session create redirected from ${i} without a usable target`)}let a=await $(d);if(d.ok&&(a.status==="allocated"||!a.status&&!!a.session_id)&&!!a.session_id){let u=a.session_id;if(!a.ws_url)throw new w(`[urun] gateway allocated session ${u} without ws_url`);if(!D(a.ws_url))throw new w(`[urun] gateway allocated session ${u} with non-absolute ws_url`);return{baseUrl:i,sessionId:u,wsUrl:a.ws_url,response:a}}if(d.status===202||a.status==="pending"||a.status==="queued"||a.queued){if(r=a.request_id||r,!r)throw new Error("[urun] gateway queued session without request_id");await Z(Q(d,a,t.retryAfterLimitMs));continue}if((d.status===401||d.status===403)&&e.getAccessToken){let u=s??"";h.add(u);let _=await E(e,{forceRefresh:!0,reason:"unauthorized"});if(_&&!h.has(_)){o=_,r=void 0;continue}}let m=a.error_message||a.error_code||`HTTP ${d.status}`;throw new Error(`[urun] session allocation failed at ${i}: ${m}`)}throw new Error(`[urun] timed out waiting for session allocation at ${i}`)}function A(n,e){let t={"X-Tenant-Id":n.orgId,"X-Auth-Provider":n.authProvider||"default","X-User-Id":"anonymous"};return e&&(t.Authorization=`Bearer ${e}`),t}async function E(n,e){return n.getAccessToken?await n.getAccessToken(e)??void 0:n.jwt}function X(n){return{app:n.app,function:n.functionName,gpus:1,idempotency_key:n.idempotencyKey}}async function j(n){let e=C(n.baseUrl,n.fallbackUrls),t=null;for(let r of e)try{let s={baseUrl:r,app:"",functionName:"",orgId:n.orgId,jwt:n.jwt,getAccessToken:n.getAccessToken,authProvider:n.authProvider,idempotencyKey:""},o=await E(s),i=await fetch(`${b(r)}/api/sessions/${encodeURIComponent(n.sessionId)}/viewer-connect`,{headers:A(s,o)});if(!i.ok)throw new Error(`[urun] viewer-connect for ${n.sessionId} failed: HTTP ${i.status}`);let l=await $(i);if(!l.ws_url||!D(l.ws_url))throw new w(`[urun] viewer-connect returned no usable ws_url for ${n.sessionId}`);return{baseUrl:b(r),sessionId:n.sessionId,wsUrl:l.ws_url,response:l}}catch(s){if(t=s instanceof Error?s:new Error(String(s)),t instanceof w)throw t}throw t??new Error("[urun] viewer-connect failed")}async function $(n){try{let e=await n.json();return e&&typeof e=="object"?e:{}}catch{return{}}}function Q(n,e,t){let r=Number(n.headers.get("Retry-After")||""),s=Number(e.retry_after_seconds??r),o=t??1e4;return Number.isFinite(s)&&s>0?Math.min(s*1e3,o):1e3}function Z(n){return new Promise(e=>setTimeout(e,n))}var w=class extends Error{};function D(n){try{let e=new URL(n);return e.protocol==="ws:"||e.protocol==="wss:"}catch{return!1}}function ee(n){try{return new URL(n).hostname!==H?[]:K}catch{return[]}}import*as g from"yjs";var N=Symbol("urun:doc:remote"),te=Symbol("urun:doc:local");function R(n){return n!==null&&typeof n=="object"&&!Array.isArray(n)}function O(n,e){let t={...n};for(let r of Object.keys(e)){let s=e[r],o=t[r];R(s)&&R(o)?t[r]=O(o,s):t[r]=s}return t}function re(n,e){let t=e.split("."),r=n;for(let s of t){if(r==null||typeof r!="object")return;r=r[s]}return r}function B(n){return n===void 0?n:JSON.parse(JSON.stringify(n))}var I=class{_doc=new g.Doc;_root=this._doc.getMap("session");_listeners=new Set;_key;_sessionId;_transport;_transportUnsubs=[];_synced;_pendingPatches=[];_observer=()=>this.notify();_updateHandler=(e,t)=>{t!==N&&this._transport?.sendDocSync(e)};constructor(e,t,r){this._key=e,this._sessionId=t,this._transport=r??null,this._synced=this._transport===null,this._root.observeDeep(this._observer),this._doc.on("update",this._updateHandler),this._transport&&(this._transportUnsubs.push(this._transport.onDocSync(s=>this.applyRemote(s)),this._transport.onDocSyncReady(()=>this.sendHello())),this._transport.isOpen&&this.sendHello())}setSessionId(e){this._sessionId=e}set(e){if(!this._synced){this._pendingPatches.push(B(e)),this.notify();return}this.applyPatch(e)}get(e,t){let r=this.snapshot();return e?re(r,e)??t:r}on(e,t){return this._listeners.add(t),()=>this._listeners.delete(t)}dispose(){this._root.unobserveDeep(this._observer),this._doc.off("update",this._updateHandler);for(let e of this._transportUnsubs)e();this._transportUnsubs=[],this._transport=null,this._listeners.clear(),this._doc.destroy()}sendHello(){this._transport&&this._transport.sendDocSync(g.encodeStateAsUpdate(this._doc))}applyRemote(e){if(g.applyUpdate(this._doc,e,N),!this._synced){this._synced=!0;let t=this._pendingPatches.splice(0);for(let r of t)this.applyPatch(r)}}applyPatch(e){this._doc.transact(()=>{let t=this._root.toJSON();for(let[r,s]of Object.entries(e)){let o=t[r],i=R(s)&&R(o)?O(o,s):B(s);JSON.stringify(i)!==JSON.stringify(o)&&this._root.set(r,i)}},te)}snapshot(){let e=this._root.toJSON();for(let t of this._pendingPatches)e=O(e,t);return e}notify(){let e=this.snapshot();for(let t of this._listeners)t(e)}};var ne=new Set(["control"]),P=class{constructor(e,t){this._name=e;this._transport=t;this._track=this._transport.getTrackByName(this._name)??null,this._unsubscribeTransport=this._transport.on("track",()=>{let r=this._transport.getTrackByName(this._name)??null;r!==this._track&&this._setTrack(r)})}_name;_transport;_track=null;_handlers=new Map;_unsubscribeTransport=null;get track(){let e=this._transport.getTrackByName(this._name)??this._track;return e!==this._track&&this._setTrack(e),this._track}async attach(e){await this._transport.addTrack(e)}async detach(){this._transport.removeTrack()}async seek(e){if(e!=="live"&&(!Number.isFinite(e)||e<0))throw new Error('[urun] stream.seek target must be "live" or a non-negative number of seconds');this._transport.seekStream(this._name,e)}on(e,t){let r=this._handlers.get(e);return r||(r=new Set,this._handlers.set(e,r)),r.add(t),()=>{r?.delete(t),r?.size===0&&this._handlers.delete(e)}}dispose(){this._unsubscribeTransport?.(),this._unsubscribeTransport=null,this._handlers.clear()}_setTrack(e){this._track=e,e&&e.addEventListener("ended",()=>{this._track===e&&this._setTrack(null)},{once:!0});let t=this._handlers.get("track");if(t)for(let r of t)r(e)}},S=class n{_sessionId;_multiplexer;_transport;_docs=new Map;_streams=new Map;constructor(e,t,r){this._sessionId=e,this._multiplexer=t,this._transport=r}static async attach(e,t){let r=await j({baseUrl:t.baseUrl,fallbackUrls:t.fallbackUrls,sessionId:e,orgId:t.orgId,jwt:t.jwt,getAccessToken:t.getAccessToken,authProvider:t.authProvider}),s=t.getAccessToken?await t.getAccessToken()??void 0:t.jwt,o=new v,i=new k({url:r.wsUrl,orgId:t.orgId,jwt:s,authProvider:t.authProvider,sessionId:e}),l=new n(e,o,i);return i.on("connected",()=>{i.multiplexer&&o.setTarget(i.multiplexer)}),i.setConnection({url:r.wsUrl,sessionId:e}),await i.connect({role:"viewer"}),l}get id(){return this._sessionId}setSessionId(e){if(e===this._sessionId)return;this._multiplexer.rewriteSessionChannels?.call(this._multiplexer,this._sessionId,e),this._sessionId=e;for(let r of this._docs.values())r.setSessionId(e)}stream(e){let t=this._streams.get(e);return t||(t=new P(e,this._transport),this._streams.set(e,t)),t}doc(e){let t=this._docs.get(e);return t||(t=new I(e,this._sessionId,ne.has(e)?this._transport:null),this._docs.set(e,t)),t}disconnect(){for(let e of this._docs.values())e.dispose();this._docs.clear();for(let e of this._streams.values())e.dispose();this._streams.clear(),this._transport.disconnect()}};var se=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function oe(n,e){return new Proxy({},{get(t,r){if(!(se.has(r)||typeof r!="string"))return s=>{let o=s??{},i=()=>`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,l=i(),h=new v,p=new k({url:"",orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:l}),d=new S(l,h,p),a=async c=>{let m=await L({baseUrl:e.baseUrl,fallbackUrls:e.fallbackUrls,app:n,functionName:r,orgId:e.orgId,jwt:e.jwt,getAccessToken:e.getAccessToken,authProvider:e.authProvider,idempotencyKey:c}),u=e.getAccessToken?await e.getAccessToken()??void 0:e.jwt;return d.setSessionId(m.sessionId),{url:m.wsUrl,sessionId:m.sessionId,jwt:u}};return p.setConnectionResolver(()=>a(i())),p.on("connected",()=>{p.multiplexer&&h.setTarget(p.multiplexer)}),(async()=>{let c=await a(l);p.setAuth({jwt:c.jwt,authProvider:e.authProvider,orgId:e.orgId}),p.setConnection({url:c.url,sessionId:c.sessionId}),await p.connect({app:n,functionName:r,args:o})})().catch(c=>{console.error(`[urun] Failed to connect session for ${r}:`,c)}),d}}})}var G=3e4,F="stream:",x=class{_ws;_multiplexer;_pendingRpc=new Map;_rpcId=0;constructor(e,t){this._ws=e,this._multiplexer=t,this._ws.addEventListener("message",r=>{let s;try{s=JSON.parse(typeof r.data=="string"?r.data:"")}catch{return}if(s.rpcId&&this._pendingRpc.has(s.rpcId)){let o=this._pendingRpc.get(s.rpcId);this._pendingRpc.delete(s.rpcId),clearTimeout(o.timer),s.error?o.reject(new Error(s.error)):o.resolve(s.result)}})}async get(e){return this._sendRpc("store.get",{key:e})}async has(e){return await this._sendRpc("store.has",{key:e})}on(e,t){let r=`${F}${e}`;return this._multiplexer.on(r,s=>{t(s)})}emit(e,t){let r=`${F}${e}`,s=typeof t=="object"&&t!==null&&!Array.isArray(t)?t:{data:t};this._multiplexer.emit(r,s)}_getInternals(){return{multiplexer:this._multiplexer,ws:this._ws}}_sendRpc(e,t){return new Promise((r,s)=>{if(!this._ws||this._ws.readyState!==WebSocket.OPEN){s(new Error("WebSocket not open"));return}let o=String(++this._rpcId),i=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),s(new Error(`RPC request "${e}" timed out after ${G}ms`)))},G);this._pendingRpc.set(o,{resolve:r,reject:s,timer:i}),this._ws.send(JSON.stringify({rpcId:o,type:e,...t}))})}};function ie(n){let r=`${n.baseUrl.replace(/\/$/,"").replace(/^http/,"ws")}/store/${encodeURIComponent(n.orgId)}`,s=new WebSocket(r);(n.jwt||n.apiKey)&&s.addEventListener("open",()=>{s.send(JSON.stringify({type:"auth",orgId:n.orgId,jwt:n.jwt,authProvider:n.authProvider,apiKey:n.apiKey}))});let o=new M(s);return new x(s,o)}var ae='video/mp4; codecs="avc1.42E01E"';function ce(n,e){let t=e?.codec??ae,r=document.createElement("video");r.muted=!0,r.playsInline=!0;let s=new MediaSource,o=URL.createObjectURL(s);r.src=o,s.addEventListener("sourceopen",()=>{let c;try{c=s.addSourceBuffer(t)}catch(f){throw f instanceof DOMException&&f.name==="NotSupportedError"?new Error(`Codec not supported: "${t}". Ensure the codec string matches the fMP4 container format. Common values: video/mp4; codecs="avc1.42E01E" (H.264 Baseline), video/mp4; codecs="avc1.4D401F" (H.264 Main).`):f}let m=n.getReader();function u(){return c.updating?new Promise(f=>{c.addEventListener("updateend",()=>f(),{once:!0})}):Promise.resolve()}async function _(){try{for(;;){let{done:f,value:U}=await m.read();if(f){await u(),s.readyState==="open"&&s.endOfStream();return}await u();try{c.appendBuffer(U)}catch(y){if(y instanceof DOMException&&y.name==="QuotaExceededError"){let T=c.buffered;if(T.length>0){let J=T.start(T.length-1);c.remove(0,J),await u(),c.appendBuffer(U)}else throw y}else throw y}}}catch{if(s.readyState==="open")try{s.endOfStream("decode")}catch{}}}_()}),r.play().catch(()=>{});let i=r,l=i.captureStream??i.mozCaptureStream;if(!l)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let h=l.call(r),p=h.getTracks(),d=0,a=p.length;if(a===0)r.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let c of p)c.addEventListener("ended",()=>{d++,d>=a&&URL.revokeObjectURL(o)});return h}export{oe as App,S as Session,ie as createStore,ce as videoStream};
1
+ import{a as M,b as v,c as k}from"./chunk-FTLHEH7Y.mjs";var H="session-api.prod.cloud.urun.sh",K=["https://session-api.use2.prod.cloud.urun.sh","https://session-api.usw2.prod.cloud.urun.sh"];function b(n){return n.trim().replace(/\/+$/,"")}function C(n,e){let t=[],r=s=>{if(!s)return;let o=b(s);o&&!t.includes(o)&&t.push(o)};r(n);for(let s of e??[])r(s);for(let s of ee(n))r(s);return t}var q=120,V=1e3,W=3;function Y(n){if(!n)return null;try{let e=new URL(n),t=e.pathname.replace(/\/api\/sessions\/create\/?$/,"");return b(`${e.origin}${t}`)}catch{return null}}async function D(n){let e=C(n.baseUrl,n.fallbackUrls),t=null,r=e.length>1;for(let s of e)try{return await z(s,n,{pollAttempts:q,retryAfterLimitMs:r?V:void 0})}catch(o){if(t=o instanceof Error?o:new Error(String(o)),t instanceof w)throw t}throw t??new Error("[urun] session allocation failed")}async function z(n,e,t){let r,s,o,i=n,l=0,h=new Set;for(let p=0;p<t.pollAttempts;p++){s=o??await E(e),o=void 0;let d=r?await fetch(`${i}/api/session-requests/${encodeURIComponent(r)}`,{headers:A(e,s)}):await fetch(`${i}/api/sessions/create`,{method:"POST",redirect:"manual",headers:{...A(e,s),"Content-Type":"application/json","Idempotency-Key":e.idempotencyKey},body:JSON.stringify(X(e))});if(!r&&(d.status===307||d.status===308)){let u=Y(d.headers.get("location"));if(u&&u!==i&&l<W){l+=1,i=u;continue}throw new Error(`[urun] session create redirected from ${i} without a usable target`)}let a=await j(d);if(d.ok&&(a.status==="allocated"||!a.status&&!!a.session_id)&&!!a.session_id){let u=a.session_id;if(!a.ws_url)throw new w(`[urun] gateway allocated session ${u} without ws_url`);if(!$(a.ws_url))throw new w(`[urun] gateway allocated session ${u} with non-absolute ws_url`);return{baseUrl:i,sessionId:u,wsUrl:a.ws_url,response:a}}if(d.status===202||a.status==="pending"||a.status==="queued"||a.queued){if(r=a.request_id||r,!r)throw new Error("[urun] gateway queued session without request_id");await Z(Q(d,a,t.retryAfterLimitMs));continue}if((d.status===401||d.status===403)&&e.getAccessToken){let u=s??"";h.add(u);let _=await E(e,{forceRefresh:!0,reason:"unauthorized"});if(_&&!h.has(_)){o=_,r=void 0;continue}}let m=a.error_message||a.error_code||`HTTP ${d.status}`;throw new Error(`[urun] session allocation failed at ${i}: ${m}`)}throw new Error(`[urun] timed out waiting for session allocation at ${i}`)}function A(n,e){let t={"X-Tenant-Id":n.orgId,"X-Auth-Provider":n.authProvider||"default","X-User-Id":"anonymous"};return e&&(t.Authorization=`Bearer ${e}`),t}async function E(n,e){return n.getAccessToken?await n.getAccessToken(e)??void 0:n.jwt}function X(n){return{app:n.app,function:n.functionName,gpus:1,idempotency_key:n.idempotencyKey}}async function L(n){let e=C(n.baseUrl,n.fallbackUrls),t=null;for(let r of e)try{let s={baseUrl:r,app:"",functionName:"",orgId:n.orgId,jwt:n.jwt,getAccessToken:n.getAccessToken,authProvider:n.authProvider,idempotencyKey:""},o=await E(s),i=await fetch(`${b(r)}/api/sessions/${encodeURIComponent(n.sessionId)}/viewer-connect`,{headers:A(s,o)});if(!i.ok)throw new Error(`[urun] viewer-connect for ${n.sessionId} failed: HTTP ${i.status}`);let l=await j(i);if(!l.ws_url||!$(l.ws_url))throw new w(`[urun] viewer-connect returned no usable ws_url for ${n.sessionId}`);return{baseUrl:b(r),sessionId:n.sessionId,wsUrl:l.ws_url,response:l}}catch(s){if(t=s instanceof Error?s:new Error(String(s)),t instanceof w)throw t}throw t??new Error("[urun] viewer-connect failed")}async function j(n){try{let e=await n.json();return e&&typeof e=="object"?e:{}}catch{return{}}}function Q(n,e,t){let r=Number(n.headers.get("Retry-After")||""),s=Number(e.retry_after_seconds??r),o=t??1e4;return Number.isFinite(s)&&s>0?Math.min(s*1e3,o):1e3}function Z(n){return new Promise(e=>setTimeout(e,n))}var w=class extends Error{};function $(n){try{let e=new URL(n);return e.protocol==="ws:"||e.protocol==="wss:"}catch{return!1}}function ee(n){try{return new URL(n).hostname!==H?[]:K}catch{return[]}}import*as g from"yjs";var N=Symbol("urun:doc:remote"),te=Symbol("urun:doc:local");function R(n){return n!==null&&typeof n=="object"&&!Array.isArray(n)}function P(n,e){let t={...n};for(let r of Object.keys(e)){let s=e[r],o=t[r];R(s)&&R(o)?t[r]=P(o,s):t[r]=s}return t}function re(n,e){let t=e.split("."),r=n;for(let s of t){if(r==null||typeof r!="object")return;r=r[s]}return r}function B(n){return n===void 0?n:JSON.parse(JSON.stringify(n))}var I=class{_doc=new g.Doc;_root=this._doc.getMap("session");_listeners=new Set;_key;_sessionId;_transport;_transportUnsubs=[];_synced;_pendingPatches=[];_observer=()=>this.notify();_updateHandler=(e,t)=>{t!==N&&this._transport?.sendDocSync(e)};constructor(e,t,r){this._key=e,this._sessionId=t,this._transport=r??null,this._synced=this._transport===null,this._root.observeDeep(this._observer),this._doc.on("update",this._updateHandler),this._transport&&(this._transportUnsubs.push(this._transport.onDocSync(s=>this.applyRemote(s)),this._transport.onDocSyncReady(()=>this.sendHello())),this._transport.isOpen&&this.sendHello())}setSessionId(e){this._sessionId=e}set(e){if(!this._synced){this._pendingPatches.push(B(e)),this.notify();return}this.applyPatch(e)}get(e,t){let r=this.snapshot();return e?re(r,e)??t:r}on(e,t){return this._listeners.add(t),()=>this._listeners.delete(t)}dispose(){this._root.unobserveDeep(this._observer),this._doc.off("update",this._updateHandler);for(let e of this._transportUnsubs)e();this._transportUnsubs=[],this._transport=null,this._listeners.clear(),this._doc.destroy()}sendHello(){this._transport&&this._transport.sendDocSync(g.encodeStateAsUpdate(this._doc))}applyRemote(e){if(g.applyUpdate(this._doc,e,N),!this._synced){this._synced=!0;let t=this._pendingPatches.splice(0);for(let r of t)this.applyPatch(r)}}applyPatch(e){this._doc.transact(()=>{let t=this._root.toJSON();for(let[r,s]of Object.entries(e)){let o=t[r],i=R(s)&&R(o)?P(o,s):B(s);JSON.stringify(i)!==JSON.stringify(o)&&this._root.set(r,i)}},te)}snapshot(){let e=this._root.toJSON();for(let t of this._pendingPatches)e=P(e,t);return e}notify(){let e=this.snapshot();for(let t of this._listeners)t(e)}};var ne=new Set(["control"]),O=class{constructor(e,t){this._name=e;this._transport=t;this._track=this._transport.getTrackByName(this._name)??null,this._unsubscribeTransport=this._transport.on("track",()=>{let r=this._transport.getTrackByName(this._name)??null;r!==this._track&&this._setTrack(r)})}_name;_transport;_track=null;_handlers=new Map;_unsubscribeTransport=null;get track(){let e=this._transport.getTrackByName(this._name)??this._track;return e!==this._track&&this._setTrack(e),this._track}async attach(e){await this._transport.addTrack(e)}async detach(){this._transport.removeTrack()}async seek(e){if(e!=="live"&&(!Number.isFinite(e)||e<0))throw new Error('[urun] stream.seek target must be "live" or a non-negative number of seconds');this._transport.seekStream(this._name,e)}on(e,t){let r=this._handlers.get(e);return r||(r=new Set,this._handlers.set(e,r)),r.add(t),()=>{r?.delete(t),r?.size===0&&this._handlers.delete(e)}}dispose(){this._unsubscribeTransport?.(),this._unsubscribeTransport=null,this._handlers.clear()}_setTrack(e){this._track=e,e&&e.addEventListener("ended",()=>{this._track===e&&this._setTrack(null)},{once:!0});let t=this._handlers.get("track");if(t)for(let r of t)r(e)}},S=class n{_sessionId;_multiplexer;_transport;_docs=new Map;_streams=new Map;constructor(e,t,r){this._sessionId=e,this._multiplexer=t,this._transport=r,this._wireControlDocIncarnation()}_wireControlDocIncarnation(){let e=this.doc("control");this._transport.applyControlDocState(e.get()),e.on("change",t=>{this._transport.applyControlDocState(t)})}static async attach(e,t){let r=await L({baseUrl:t.baseUrl,fallbackUrls:t.fallbackUrls,sessionId:e,orgId:t.orgId,jwt:t.jwt,getAccessToken:t.getAccessToken,authProvider:t.authProvider}),s=t.getAccessToken?await t.getAccessToken()??void 0:t.jwt,o=new v,i=new k({url:r.wsUrl,orgId:t.orgId,jwt:s,authProvider:t.authProvider,sessionId:e}),l=new n(e,o,i);return i.on("connected",()=>{i.multiplexer&&o.setTarget(i.multiplexer)}),i.setConnection({url:r.wsUrl,sessionId:e}),await i.connect({role:"viewer"}),l}get id(){return this._sessionId}get phase(){return this._transport.phase}onPhase(e){return e(this._transport.phase),this._transport.on("phase",e)}setSessionId(e){if(e===this._sessionId)return;this._multiplexer.rewriteSessionChannels?.call(this._multiplexer,this._sessionId,e),this._sessionId=e;for(let r of this._docs.values())r.setSessionId(e)}stream(e){let t=this._streams.get(e);return t||(t=new O(e,this._transport),this._streams.set(e,t)),t}doc(e){let t=this._docs.get(e);return t||(t=new I(e,this._sessionId,ne.has(e)?this._transport:null),this._docs.set(e,t)),t}disconnect(){for(let e of this._docs.values())e.dispose();this._docs.clear();for(let e of this._streams.values())e.dispose();this._streams.clear(),this._transport.disconnect()}};var se=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function oe(n,e){return new Proxy({},{get(t,r){if(!(se.has(r)||typeof r!="string"))return s=>{let o=s??{},i=()=>`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,l=i(),h=new v,p=new k({url:"",orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:l}),d=new S(l,h,p),a=async c=>{let m=await D({baseUrl:e.baseUrl,fallbackUrls:e.fallbackUrls,app:n,functionName:r,orgId:e.orgId,jwt:e.jwt,getAccessToken:e.getAccessToken,authProvider:e.authProvider,idempotencyKey:c}),u=e.getAccessToken?await e.getAccessToken()??void 0:e.jwt;return d.setSessionId(m.sessionId),{url:m.wsUrl,sessionId:m.sessionId,jwt:u}};return p.setConnectionResolver(()=>a(i())),p.on("connected",()=>{p.multiplexer&&h.setTarget(p.multiplexer)}),(async()=>{let c=await a(l);p.setAuth({jwt:c.jwt,authProvider:e.authProvider,orgId:e.orgId}),p.setConnection({url:c.url,sessionId:c.sessionId}),await p.connect({app:n,functionName:r,args:o})})().catch(c=>{console.error(`[urun] Failed to connect session for ${r}:`,c)}),d}}})}var G=3e4,F="stream:",x=class{_ws;_multiplexer;_pendingRpc=new Map;_rpcId=0;constructor(e,t){this._ws=e,this._multiplexer=t,this._ws.addEventListener("message",r=>{let s;try{s=JSON.parse(typeof r.data=="string"?r.data:"")}catch{return}if(s.rpcId&&this._pendingRpc.has(s.rpcId)){let o=this._pendingRpc.get(s.rpcId);this._pendingRpc.delete(s.rpcId),clearTimeout(o.timer),s.error?o.reject(new Error(s.error)):o.resolve(s.result)}})}async get(e){return this._sendRpc("store.get",{key:e})}async has(e){return await this._sendRpc("store.has",{key:e})}on(e,t){let r=`${F}${e}`;return this._multiplexer.on(r,s=>{t(s)})}emit(e,t){let r=`${F}${e}`,s=typeof t=="object"&&t!==null&&!Array.isArray(t)?t:{data:t};this._multiplexer.emit(r,s)}_getInternals(){return{multiplexer:this._multiplexer,ws:this._ws}}_sendRpc(e,t){return new Promise((r,s)=>{if(!this._ws||this._ws.readyState!==WebSocket.OPEN){s(new Error("WebSocket not open"));return}let o=String(++this._rpcId),i=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),s(new Error(`RPC request "${e}" timed out after ${G}ms`)))},G);this._pendingRpc.set(o,{resolve:r,reject:s,timer:i}),this._ws.send(JSON.stringify({rpcId:o,type:e,...t}))})}};function ie(n){let r=`${n.baseUrl.replace(/\/$/,"").replace(/^http/,"ws")}/store/${encodeURIComponent(n.orgId)}`,s=new WebSocket(r);(n.jwt||n.apiKey)&&s.addEventListener("open",()=>{s.send(JSON.stringify({type:"auth",orgId:n.orgId,jwt:n.jwt,authProvider:n.authProvider,apiKey:n.apiKey}))});let o=new M(s);return new x(s,o)}var ae='video/mp4; codecs="avc1.42E01E"';function ce(n,e){let t=e?.codec??ae,r=document.createElement("video");r.muted=!0,r.playsInline=!0;let s=new MediaSource,o=URL.createObjectURL(s);r.src=o,s.addEventListener("sourceopen",()=>{let c;try{c=s.addSourceBuffer(t)}catch(f){throw f instanceof DOMException&&f.name==="NotSupportedError"?new Error(`Codec not supported: "${t}". Ensure the codec string matches the fMP4 container format. Common values: video/mp4; codecs="avc1.42E01E" (H.264 Baseline), video/mp4; codecs="avc1.4D401F" (H.264 Main).`):f}let m=n.getReader();function u(){return c.updating?new Promise(f=>{c.addEventListener("updateend",()=>f(),{once:!0})}):Promise.resolve()}async function _(){try{for(;;){let{done:f,value:U}=await m.read();if(f){await u(),s.readyState==="open"&&s.endOfStream();return}await u();try{c.appendBuffer(U)}catch(y){if(y instanceof DOMException&&y.name==="QuotaExceededError"){let T=c.buffered;if(T.length>0){let J=T.start(T.length-1);c.remove(0,J),await u(),c.appendBuffer(U)}else throw y}else throw y}}}catch{if(s.readyState==="open")try{s.endOfStream("decode")}catch{}}}_()}),r.play().catch(()=>{});let i=r,l=i.captureStream??i.mozCaptureStream;if(!l)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let h=l.call(r),p=h.getTracks(),d=0,a=p.length;if(a===0)r.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let c of p)c.addEventListener("ended",()=>{d++,d>=a&&URL.revokeObjectURL(o)});return h}export{oe as App,S as Session,ie as createStore,ce as videoStream};
@@ -1,6 +1,27 @@
1
1
 
2
2
 
3
- type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed';
3
+ type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'renegotiating' | 'disconnected' | 'failed';
4
+
5
+ type SessionPhaseName = 'idle' | 'queued' | 'provisioning' | 'connecting' | 'live' | 'error' | 'ended';
6
+
7
+ interface SessionPhase {
8
+
9
+ name: SessionPhaseName;
10
+
11
+ queue?: {
12
+ position: number;
13
+ depth: number;
14
+ };
15
+
16
+ requestId?: string;
17
+
18
+ sessionId?: string;
19
+
20
+ error?: {
21
+ reason: string;
22
+ code?: string;
23
+ };
24
+ }
4
25
 
5
26
  interface TransportSessionOptions {
6
27
 
@@ -15,6 +36,15 @@ interface TransportSessionOptions {
15
36
  authProvider?: string;
16
37
 
17
38
  sessionId?: string;
39
+
40
+ mediaLiveness?: MediaLivenessOptions;
41
+ }
42
+
43
+ interface MediaLivenessOptions {
44
+
45
+ pollMs?: number;
46
+
47
+ stallTimeoutMs?: number;
18
48
  }
19
49
 
20
50
  type ChannelName = 'scene_graph' | 'ui_components' | 'events' | 'agent_status' | 'control';
@@ -144,10 +174,14 @@ interface Session {
144
174
 
145
175
  readonly id: string;
146
176
 
177
+ readonly phase: SessionPhase;
178
+
147
179
  stream(name: string): SessionStream;
148
180
 
149
181
  doc(key: string): SessionDocument;
150
182
 
183
+ onPhase(handler: (phase: SessionPhase) => void): () => void;
184
+
151
185
  disconnect(): void;
152
186
  }
153
187
 
@@ -230,6 +264,8 @@ interface TransportEvents {
230
264
  docSyncReady: () => void;
231
265
 
232
266
  stateChange: (state: TransportState) => void;
267
+
268
+ phase: (phase: SessionPhase) => void;
233
269
  }
234
270
  interface SessionStartOptions {
235
271
  app?: string;
@@ -273,7 +309,25 @@ declare class TransportSession {
273
309
  private _lastConsumeMsg;
274
310
  private _recvConnectWatchdog;
275
311
  private _recvTcpFailoverDone;
312
+ private _phase;
313
+ private _requestId;
314
+ private _liveReached;
315
+
316
+ private _mediaMonitorTimer;
317
+
318
+ private _lastInboundTotal;
319
+
320
+ private _mediaEverFlowed;
321
+
322
+ private _lastInboundAdvanceAt;
323
+
324
+ private _mediaDeathHandled;
325
+
326
+ private _currentEpoch;
276
327
  get state(): TransportState;
328
+
329
+ get currentIncarnation(): number;
330
+ get phase(): SessionPhase;
277
331
  get sessionId(): string | null;
278
332
  get tracks(): Map<string, MediaStreamTrack>;
279
333
  get multiplexer(): ChannelMultiplexer | null;
@@ -284,6 +338,14 @@ declare class TransportSession {
284
338
  constructor(options?: TransportSessionOptions);
285
339
  setConnection(options: Pick<TransportSessionOptions, 'url' | 'fallbackUrls' | 'sessionId'>): void;
286
340
  setConnectionResolver(resolveConnection: (() => Promise<TransportConnection>) | null): void;
341
+
342
+ applyControlDocState(state: Record<string, unknown> | null | undefined): void;
343
+
344
+ setIncarnation(epoch: number): void;
345
+
346
+ isStaleIncarnation(epoch: number): boolean;
347
+
348
+ isStaleClosedMarker(marker: Record<string, unknown> | null | undefined): boolean;
287
349
  connect(startOptions?: SessionStartOptions): Promise<void>;
288
350
  disconnect(): void;
289
351
  on<E extends keyof TransportEvents>(event: E, handler: TransportEvents[E]): () => void;
@@ -299,6 +361,8 @@ declare class TransportSession {
299
361
  requestDiagnostics(): void;
300
362
  seekStream(name: string, target: number | 'live'): void;
301
363
  private _handleMessage;
364
+
365
+ private _onStatus;
302
366
  private _onRouterCapabilities;
303
367
  private _onCreateRecvTransport;
304
368
 
@@ -308,6 +372,14 @@ declare class TransportSession {
308
372
  private _failoverRecvToTcp;
309
373
  private _onCreateSendTransport;
310
374
  private _onConsume;
375
+ private _mediaPollMs;
376
+ private _mediaStallTimeoutMs;
377
+
378
+ private _startMediaMonitor;
379
+ private _stopMediaMonitor;
380
+ private _sampleMediaLiveness;
381
+
382
+ private _renegotiate;
311
383
  private _attemptReconnect;
312
384
  private _connectUrls;
313
385
  private _connectOnce;
@@ -319,6 +391,10 @@ declare class TransportSession {
319
391
  private _cleanupMedia;
320
392
  private _send;
321
393
  private _setState;
394
+
395
+ private _setPhase;
396
+
397
+ private _emitError;
322
398
  }
323
399
 
324
- export { type App as A, type ChannelEndpoint as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, type AttachOptions as b, type ChannelMessage as c, ChannelMultiplexer as d, type ChannelName as e, type LayoutConfig as f, type LayoutContext as g, type SceneGraph as h, type Session as i, type SessionDocument as j, type SessionStartOptions as k, type SessionStream as l, type Store as m, type StoreOptions as n, type TransportSessionOptions as o, type TransportState as p };
400
+ export { type App as A, type ChannelEndpoint as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, type AttachOptions as b, type ChannelMessage as c, ChannelMultiplexer as d, type ChannelName as e, type LayoutConfig as f, type LayoutContext as g, type SceneGraph as h, type Session as i, type SessionDocument as j, type SessionPhase as k, type SessionPhaseName as l, type SessionStartOptions as m, type SessionStream as n, type Store as o, type StoreOptions as p, type TransportSessionOptions as q, type TransportState as r };
@@ -1,6 +1,27 @@
1
1
 
2
2
 
3
- type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed';
3
+ type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'renegotiating' | 'disconnected' | 'failed';
4
+
5
+ type SessionPhaseName = 'idle' | 'queued' | 'provisioning' | 'connecting' | 'live' | 'error' | 'ended';
6
+
7
+ interface SessionPhase {
8
+
9
+ name: SessionPhaseName;
10
+
11
+ queue?: {
12
+ position: number;
13
+ depth: number;
14
+ };
15
+
16
+ requestId?: string;
17
+
18
+ sessionId?: string;
19
+
20
+ error?: {
21
+ reason: string;
22
+ code?: string;
23
+ };
24
+ }
4
25
 
5
26
  interface TransportSessionOptions {
6
27
 
@@ -15,6 +36,15 @@ interface TransportSessionOptions {
15
36
  authProvider?: string;
16
37
 
17
38
  sessionId?: string;
39
+
40
+ mediaLiveness?: MediaLivenessOptions;
41
+ }
42
+
43
+ interface MediaLivenessOptions {
44
+
45
+ pollMs?: number;
46
+
47
+ stallTimeoutMs?: number;
18
48
  }
19
49
 
20
50
  type ChannelName = 'scene_graph' | 'ui_components' | 'events' | 'agent_status' | 'control';
@@ -144,10 +174,14 @@ interface Session {
144
174
 
145
175
  readonly id: string;
146
176
 
177
+ readonly phase: SessionPhase;
178
+
147
179
  stream(name: string): SessionStream;
148
180
 
149
181
  doc(key: string): SessionDocument;
150
182
 
183
+ onPhase(handler: (phase: SessionPhase) => void): () => void;
184
+
151
185
  disconnect(): void;
152
186
  }
153
187
 
@@ -230,6 +264,8 @@ interface TransportEvents {
230
264
  docSyncReady: () => void;
231
265
 
232
266
  stateChange: (state: TransportState) => void;
267
+
268
+ phase: (phase: SessionPhase) => void;
233
269
  }
234
270
  interface SessionStartOptions {
235
271
  app?: string;
@@ -273,7 +309,25 @@ declare class TransportSession {
273
309
  private _lastConsumeMsg;
274
310
  private _recvConnectWatchdog;
275
311
  private _recvTcpFailoverDone;
312
+ private _phase;
313
+ private _requestId;
314
+ private _liveReached;
315
+
316
+ private _mediaMonitorTimer;
317
+
318
+ private _lastInboundTotal;
319
+
320
+ private _mediaEverFlowed;
321
+
322
+ private _lastInboundAdvanceAt;
323
+
324
+ private _mediaDeathHandled;
325
+
326
+ private _currentEpoch;
276
327
  get state(): TransportState;
328
+
329
+ get currentIncarnation(): number;
330
+ get phase(): SessionPhase;
277
331
  get sessionId(): string | null;
278
332
  get tracks(): Map<string, MediaStreamTrack>;
279
333
  get multiplexer(): ChannelMultiplexer | null;
@@ -284,6 +338,14 @@ declare class TransportSession {
284
338
  constructor(options?: TransportSessionOptions);
285
339
  setConnection(options: Pick<TransportSessionOptions, 'url' | 'fallbackUrls' | 'sessionId'>): void;
286
340
  setConnectionResolver(resolveConnection: (() => Promise<TransportConnection>) | null): void;
341
+
342
+ applyControlDocState(state: Record<string, unknown> | null | undefined): void;
343
+
344
+ setIncarnation(epoch: number): void;
345
+
346
+ isStaleIncarnation(epoch: number): boolean;
347
+
348
+ isStaleClosedMarker(marker: Record<string, unknown> | null | undefined): boolean;
287
349
  connect(startOptions?: SessionStartOptions): Promise<void>;
288
350
  disconnect(): void;
289
351
  on<E extends keyof TransportEvents>(event: E, handler: TransportEvents[E]): () => void;
@@ -299,6 +361,8 @@ declare class TransportSession {
299
361
  requestDiagnostics(): void;
300
362
  seekStream(name: string, target: number | 'live'): void;
301
363
  private _handleMessage;
364
+
365
+ private _onStatus;
302
366
  private _onRouterCapabilities;
303
367
  private _onCreateRecvTransport;
304
368
 
@@ -308,6 +372,14 @@ declare class TransportSession {
308
372
  private _failoverRecvToTcp;
309
373
  private _onCreateSendTransport;
310
374
  private _onConsume;
375
+ private _mediaPollMs;
376
+ private _mediaStallTimeoutMs;
377
+
378
+ private _startMediaMonitor;
379
+ private _stopMediaMonitor;
380
+ private _sampleMediaLiveness;
381
+
382
+ private _renegotiate;
311
383
  private _attemptReconnect;
312
384
  private _connectUrls;
313
385
  private _connectOnce;
@@ -319,6 +391,10 @@ declare class TransportSession {
319
391
  private _cleanupMedia;
320
392
  private _send;
321
393
  private _setState;
394
+
395
+ private _setPhase;
396
+
397
+ private _emitError;
322
398
  }
323
399
 
324
- export { type App as A, type ChannelEndpoint as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, type AttachOptions as b, type ChannelMessage as c, ChannelMultiplexer as d, type ChannelName as e, type LayoutConfig as f, type LayoutContext as g, type SceneGraph as h, type Session as i, type SessionDocument as j, type SessionStartOptions as k, type SessionStream as l, type Store as m, type StoreOptions as n, type TransportSessionOptions as o, type TransportState as p };
400
+ export { type App as A, type ChannelEndpoint as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, type AttachOptions as b, type ChannelMessage as c, ChannelMultiplexer as d, type ChannelName as e, type LayoutConfig as f, type LayoutContext as g, type SceneGraph as h, type Session as i, type SessionDocument as j, type SessionPhase as k, type SessionPhaseName as l, type SessionStartOptions as m, type SessionStream as n, type Store as o, type StoreOptions as p, type TransportSessionOptions as q, type TransportState as r };
@@ -1 +1 @@
1
- export { c as ChannelMessage, d as ChannelMultiplexer, e as ChannelName, k as SessionStartOptions, T as TransportSession, o as TransportSessionOptions } from './internal-ChJcuvFH.mjs';
1
+ export { c as ChannelMessage, d as ChannelMultiplexer, e as ChannelName, m as SessionStartOptions, T as TransportSession, q as TransportSessionOptions } from './internal-BYsSyqG4.mjs';
@@ -1 +1 @@
1
- export { c as ChannelMessage, d as ChannelMultiplexer, e as ChannelName, k as SessionStartOptions, T as TransportSession, o as TransportSessionOptions } from './internal-ChJcuvFH.js';
1
+ export { c as ChannelMessage, d as ChannelMultiplexer, e as ChannelName, m as SessionStartOptions, T as TransportSession, q as TransportSessionOptions } from './internal-BYsSyqG4.js';