@urun-sh/core 0.1.30 → 0.1.33
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/CHANGELOG.md +16 -0
- package/dist/{chunk-5SICKIGZ.mjs → chunk-ELCOC6B4.mjs} +5 -5
- package/dist/index.d.mts +34 -8
- package/dist/index.d.ts +34 -8
- package/dist/index.js +5 -5
- package/dist/index.mjs +1 -1
- package/dist/{internal-BJQ7-qED.d.mts → internal-dmugzBC7.d.mts} +18 -1
- package/dist/{internal-BJQ7-qED.d.ts → internal-dmugzBC7.d.ts} +18 -1
- package/dist/internal.d.mts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +5 -5
- package/dist/internal.mjs +1 -1
- package/package.json +5 -2
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as R,b as T,c as E}from"./chunk-5SICKIGZ.mjs";function v(s,e){let r={...s};for(let t of Object.keys(e)){let n=e[t],o=r[t];n!==null&&typeof n=="object"&&!Array.isArray(n)&&o!==null&&typeof o=="object"&&!Array.isArray(o)?r[t]=v(o,n):r[t]=n}return r}function L(s,e){let r=e.split("."),t=s;for(let n of r){if(t==null||typeof t!="object")return;t=t[n]}return t}var S=class{_state={};_listeners=new Set;_key;_sessionId;_multiplexer;_unsubscribe=null;constructor(e,r,t){this._key=e,this._sessionId=r,this._multiplexer=t,this.subscribe()}setSessionId(e){e!==this._sessionId&&(this._unsubscribe?.(),this._sessionId=e,this.subscribe())}subscribe(){this._unsubscribe=this._multiplexer.on(this._channel,e=>{if(e.type==="doc_update"&&e.patch&&typeof e.patch=="object"){this._state=v(this._state,e.patch);let r={...this._state};for(let t of this._listeners)t(r)}})}get _channel(){return`session:${this._sessionId}:doc:${this._key}`}set(e){this._state=v(this._state,e),this._multiplexer.emit(this._channel,{type:"doc_update",patch:e,snapshot:this._state});let r={...this._state};for(let t of this._listeners)t(r)}get(e,r){return e?L(this._state,e)??r:{...this._state}}on(e,r){return this._listeners.add(r),()=>this._listeners.delete(r)}};var k=class{constructor(e,r){this._name=e;this._transport=r;this._track=this._transport.getTrackByName(this._name)??null,this._unsubscribeTransport=this._transport.on("track",()=>{let t=this._transport.getTrackByName(this._name)??null;t!==this._track&&this._setTrack(t)})}_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,r){let t=this._handlers.get(e);return t||(t=new Set,this._handlers.set(e,t)),t.add(r),()=>{t?.delete(r),t?.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 r=this._handlers.get("track");if(r)for(let t of r)t(e)}},g=class{_sessionId;_multiplexer;_transport;_docs=new Map;_streams=new Map;constructor(e,r,t){this._sessionId=e,this._multiplexer=r,this._transport=t}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 t of this._docs.values())t.setSessionId(e)}stream(e){let r=this._streams.get(e);return r||(r=new k(e,this._transport),this._streams.set(e,r)),r}doc(e){let r=this._docs.get(e);return r||(r=new S(e,this._sessionId,this._multiplexer),this._docs.set(e,r)),r}disconnect(){for(let e of this._streams.values())e.dispose();this._streams.clear(),this._transport.disconnect()}};var j="session-api.prod.cloud.urun.sh",$=["https://session-api.use2.prod.cloud.urun.sh","https://session-api.usw2.prod.cloud.urun.sh"];function N(s){return s.trim().replace(/\/+$/,"")}function B(s,e){let r=[],t=n=>{if(!n)return;let o=N(n);o&&!r.includes(o)&&r.push(o)};t(s);for(let n of e??[])t(n);for(let n of H(s))t(n);return r}var F=120,D=1e3;async function P(s){let e=B(s.baseUrl,s.fallbackUrls),r=null,t=e.length>1;for(let n of e)try{return await G(n,s,{pollAttempts:F,retryAfterLimitMs:t?D:void 0})}catch(o){if(r=o instanceof Error?o:new Error(String(o)),r instanceof w)throw r}throw r??new Error("[urun] session allocation failed")}async function G(s,e,r){let t,n,o,u=new Set;for(let d=0;d<r.pollAttempts;d++){n=o??await x(e),o=void 0;let c=t?await fetch(`${s}/api/session-requests/${encodeURIComponent(t)}`,{headers:A(e,n)}):await fetch(`${s}/api/sessions/create`,{method:"POST",headers:{...A(e,n),"Content-Type":"application/json","Idempotency-Key":e.idempotencyKey},body:JSON.stringify(q(e))}),i=await K(c);if(c.ok&&(i.status==="allocated"||!i.status&&!!i.session_id)&&!!i.session_id){let a=i.session_id;if(!i.ws_url)throw new w(`[urun] gateway allocated session ${a} without ws_url`);if(!W(i.ws_url))throw new w(`[urun] gateway allocated session ${a} with non-absolute ws_url`);return{baseUrl:s,sessionId:a,wsUrl:i.ws_url,response:i}}if(c.status===202||i.status==="pending"||i.status==="queued"||i.queued){if(t=i.request_id||t,!t)throw new Error("[urun] gateway queued session without request_id");await J(z(c,i,r.retryAfterLimitMs));continue}if((c.status===401||c.status===403)&&e.getAccessToken){let a=n??"";u.add(a);let l=await x(e,{forceRefresh:!0,reason:"unauthorized"});if(l&&!u.has(l)){o=l,t=void 0;continue}}let m=i.error_message||i.error_code||`HTTP ${c.status}`;throw new Error(`[urun] session allocation failed at ${s}: ${m}`)}throw new Error(`[urun] timed out waiting for session allocation at ${s}`)}function A(s,e){let r={"X-Tenant-Id":s.orgId,"X-Auth-Provider":s.authProvider||"default","X-User-Id":"anonymous"};return e&&(r.Authorization=`Bearer ${e}`),r}async function x(s,e){return s.getAccessToken?await s.getAccessToken(e)??void 0:s.jwt}function q(s){let e=Number(s.args?.gpus??1),r={app:s.app,function:s.functionName,gpus:Number.isFinite(e)&&e>0?e:1,idempotency_key:s.idempotencyKey},t=s.args?.preferred_zone;return typeof t=="string"&&t&&(r.preferred_zone=t),r}async function K(s){try{let e=await s.json();return e&&typeof e=="object"?e:{}}catch{return{}}}function z(s,e,r){let t=Number(s.headers.get("Retry-After")||""),n=Number(e.retry_after_seconds??t),o=r??1e4;return Number.isFinite(n)&&n>0?Math.min(n*1e3,o):1e3}function J(s){return new Promise(e=>setTimeout(e,s))}var w=class extends Error{};function W(s){try{let e=new URL(s);return e.protocol==="ws:"||e.protocol==="wss:"}catch{return!1}}function H(s){try{return new URL(s).hostname!==j?[]:$}catch{return[]}}var V=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function X(s,e){return new Proxy({},{get(r,t){if(!(V.has(t)||typeof t!="string"))return n=>{let o=n??{},u=()=>`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,d=u(),c=new T,i=new E({url:"",orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:d}),f=new g(d,c,i),m=async a=>{let l=await P({baseUrl:e.baseUrl,fallbackUrls:e.fallbackUrls,app:s,functionName:t,args:o,orgId:e.orgId,jwt:e.jwt,getAccessToken:e.getAccessToken,authProvider:e.authProvider,idempotencyKey:a}),h=e.getAccessToken?await e.getAccessToken()??void 0:e.jwt;return f.setSessionId(l.sessionId),{url:l.wsUrl,sessionId:l.sessionId,jwt:h}};return i.setConnectionResolver(()=>m(u())),i.on("connected",()=>{i.multiplexer&&c.setTarget(i.multiplexer)}),(async()=>{let a=await m(d);i.setAuth({jwt:a.jwt,authProvider:e.authProvider,orgId:e.orgId}),i.setConnection({url:a.url,sessionId:a.sessionId}),await i.connect({app:s,functionName:t,args:o})})().catch(a=>{console.error(`[urun] Failed to connect session for ${t}:`,a)}),f}}})}var M=3e4,O="stream:",b=class{_ws;_multiplexer;_pendingRpc=new Map;_rpcId=0;constructor(e,r){this._ws=e,this._multiplexer=r,this._ws.addEventListener("message",t=>{let n;try{n=JSON.parse(typeof t.data=="string"?t.data:"")}catch{return}if(n.rpcId&&this._pendingRpc.has(n.rpcId)){let o=this._pendingRpc.get(n.rpcId);this._pendingRpc.delete(n.rpcId),clearTimeout(o.timer),n.error?o.reject(new Error(n.error)):o.resolve(n.result)}})}async get(e){return this._sendRpc("store.get",{key:e})}async has(e){return await this._sendRpc("store.has",{key:e})}on(e,r){let t=`${O}${e}`;return this._multiplexer.on(t,n=>{r(n)})}emit(e,r){let t=`${O}${e}`,n=typeof r=="object"&&r!==null&&!Array.isArray(r)?r:{data:r};this._multiplexer.emit(t,n)}_getInternals(){return{multiplexer:this._multiplexer,ws:this._ws}}_sendRpc(e,r){return new Promise((t,n)=>{if(!this._ws||this._ws.readyState!==WebSocket.OPEN){n(new Error("WebSocket not open"));return}let o=String(++this._rpcId),u=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),n(new Error(`RPC request "${e}" timed out after ${M}ms`)))},M);this._pendingRpc.set(o,{resolve:t,reject:n,timer:u}),this._ws.send(JSON.stringify({rpcId:o,type:e,...r}))})}};function Y(s){let t=`${s.baseUrl.replace(/\/$/,"").replace(/^http/,"ws")}/store/${encodeURIComponent(s.orgId)}`,n=new WebSocket(t);(s.jwt||s.apiKey)&&n.addEventListener("open",()=>{n.send(JSON.stringify({type:"auth",orgId:s.orgId,jwt:s.jwt,authProvider:s.authProvider,apiKey:s.apiKey}))});let o=new R(n);return new b(n,o)}var Q='video/mp4; codecs="avc1.42E01E"';function Z(s,e){let r=e?.codec??Q,t=document.createElement("video");t.muted=!0,t.playsInline=!0;let n=new MediaSource,o=URL.createObjectURL(n);t.src=o,n.addEventListener("sourceopen",()=>{let a;try{a=n.addSourceBuffer(r)}catch(p){throw p instanceof DOMException&&p.name==="NotSupportedError"?new Error(`Codec not supported: "${r}". 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).`):p}let l=s.getReader();function h(){return a.updating?new Promise(p=>{a.addEventListener("updateend",()=>p(),{once:!0})}):Promise.resolve()}async function C(){try{for(;;){let{done:p,value:I}=await l.read();if(p){await h(),n.readyState==="open"&&n.endOfStream();return}await h();try{a.appendBuffer(I)}catch(_){if(_ instanceof DOMException&&_.name==="QuotaExceededError"){let y=a.buffered;if(y.length>0){let U=y.start(y.length-1);a.remove(0,U),await h(),a.appendBuffer(I)}else throw _}else throw _}}}catch{if(n.readyState==="open")try{n.endOfStream("decode")}catch{}}}C()}),t.play().catch(()=>{});let u=t,d=u.captureStream??u.mozCaptureStream;if(!d)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let c=d.call(t),i=c.getTracks(),f=0,m=i.length;if(m===0)t.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let a of i)a.addEventListener("ended",()=>{f++,f>=m&&URL.revokeObjectURL(o)});return c}export{X as App,g as Session,Y as createStore,Z as videoStream};
|
|
1
|
+
import{a as A,b as x,c as O}from"./chunk-ELCOC6B4.mjs";import*as g from"yjs";var P=Symbol("urun:doc:remote"),B=Symbol("urun:doc:local");function v(n){return n!==null&&typeof n=="object"&&!Array.isArray(n)}function R(n,e){let t={...n};for(let r of Object.keys(e)){let s=e[r],o=t[r];v(s)&&v(o)?t[r]=R(o,s):t[r]=s}return t}function F(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 U(n){return n===void 0?n:JSON.parse(JSON.stringify(n))}var k=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!==P&&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(U(e)),this.notify();return}this.applyPatch(e)}get(e,t){let r=this.snapshot();return e?F(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,P),!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],a=v(s)&&v(o)?R(o,s):U(s);JSON.stringify(a)!==JSON.stringify(o)&&this._root.set(r,a)}},B)}snapshot(){let e=this._root.toJSON();for(let t of this._pendingPatches)e=R(e,t);return e}notify(){let e=this.snapshot();for(let t of this._listeners)t(e)}};var G=new Set(["control"]),I=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)}},w=class{_sessionId;_multiplexer;_transport;_docs=new Map;_streams=new Map;constructor(e,t,r){this._sessionId=e,this._multiplexer=t,this._transport=r}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 I(e,this._transport),this._streams.set(e,t)),t}doc(e){let t=this._docs.get(e);return t||(t=new k(e,this._sessionId,G.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 J="session-api.prod.cloud.urun.sh",H=["https://session-api.use2.prod.cloud.urun.sh","https://session-api.usw2.prod.cloud.urun.sh"];function L(n){return n.trim().replace(/\/+$/,"")}function K(n,e){let t=[],r=s=>{if(!s)return;let o=L(s);o&&!t.includes(o)&&t.push(o)};r(n);for(let s of e??[])r(s);for(let s of re(n))r(s);return t}var q=120,W=1e3,Y=3;function z(n){if(!n)return null;try{let e=new URL(n),t=e.pathname.replace(/\/api\/sessions\/create\/?$/,"");return L(`${e.origin}${t}`)}catch{return null}}async function D(n){let e=K(n.baseUrl,n.fallbackUrls),t=null,r=e.length>1;for(let s of e)try{return await V(s,n,{pollAttempts:q,retryAfterLimitMs:r?W:void 0})}catch(o){if(t=o instanceof Error?o:new Error(String(o)),t instanceof S)throw t}throw t??new Error("[urun] session allocation failed")}async function V(n,e,t){let r,s,o,a=n,p=0,h=new Set;for(let l=0;l<t.pollAttempts;l++){s=o??await C(e),o=void 0;let d=r?await fetch(`${a}/api/session-requests/${encodeURIComponent(r)}`,{headers:M(e,s)}):await fetch(`${a}/api/sessions/create`,{method:"POST",redirect:"manual",headers:{...M(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=z(d.headers.get("location"));if(u&&u!==a&&p<Y){p+=1,a=u;continue}throw new Error(`[urun] session create redirected from ${a} without a usable target`)}let i=await Q(d);if(d.ok&&(i.status==="allocated"||!i.status&&!!i.session_id)&&!!i.session_id){let u=i.session_id;if(!i.ws_url)throw new S(`[urun] gateway allocated session ${u} without ws_url`);if(!te(i.ws_url))throw new S(`[urun] gateway allocated session ${u} with non-absolute ws_url`);return{baseUrl:a,sessionId:u,wsUrl:i.ws_url,response:i}}if(d.status===202||i.status==="pending"||i.status==="queued"||i.queued){if(r=i.request_id||r,!r)throw new Error("[urun] gateway queued session without request_id");await ee(Z(d,i,t.retryAfterLimitMs));continue}if((d.status===401||d.status===403)&&e.getAccessToken){let u=s??"";h.add(u);let _=await C(e,{forceRefresh:!0,reason:"unauthorized"});if(_&&!h.has(_)){o=_,r=void 0;continue}}let m=i.error_message||i.error_code||`HTTP ${d.status}`;throw new Error(`[urun] session allocation failed at ${a}: ${m}`)}throw new Error(`[urun] timed out waiting for session allocation at ${a}`)}function M(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 C(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 Q(n){try{let e=await n.json();return e&&typeof e=="object"?e:{}}catch{return{}}}function Z(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 ee(n){return new Promise(e=>setTimeout(e,n))}var S=class extends Error{};function te(n){try{let e=new URL(n);return e.protocol==="ws:"||e.protocol==="wss:"}catch{return!1}}function re(n){try{return new URL(n).hostname!==J?[]:H}catch{return[]}}var ne=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function se(n,e){return new Proxy({},{get(t,r){if(!(ne.has(r)||typeof r!="string"))return s=>{let o=s??{},a=()=>`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,p=a(),h=new x,l=new O({url:"",orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:p}),d=new w(p,h,l),i=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 l.setConnectionResolver(()=>i(a())),l.on("connected",()=>{l.multiplexer&&h.setTarget(l.multiplexer)}),(async()=>{let c=await i(p);l.setAuth({jwt:c.jwt,authProvider:e.authProvider,orgId:e.orgId}),l.setConnection({url:c.url,sessionId:c.sessionId}),await l.connect({app:n,functionName:r,args:o})})().catch(c=>{console.error(`[urun] Failed to connect session for ${r}:`,c)}),d}}})}var $=3e4,N="stream:",T=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=`${N}${e}`;return this._multiplexer.on(r,s=>{t(s)})}emit(e,t){let r=`${N}${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),a=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),s(new Error(`RPC request "${e}" timed out after ${$}ms`)))},$);this._pendingRpc.set(o,{resolve:r,reject:s,timer:a}),this._ws.send(JSON.stringify({rpcId:o,type:e,...t}))})}};function oe(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 A(s);return new T(s,o)}var ie='video/mp4; codecs="avc1.42E01E"';function ae(n,e){let t=e?.codec??ie,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:E}=await m.read();if(f){await u(),s.readyState==="open"&&s.endOfStream();return}await u();try{c.appendBuffer(E)}catch(y){if(y instanceof DOMException&&y.name==="QuotaExceededError"){let b=c.buffered;if(b.length>0){let j=b.start(b.length-1);c.remove(0,j),await u(),c.appendBuffer(E)}else throw y}else throw y}}}catch{if(s.readyState==="open")try{s.endOfStream("decode")}catch{}}}_()}),r.play().catch(()=>{});let a=r,p=a.captureStream??a.mozCaptureStream;if(!p)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let h=p.call(r),l=h.getTracks(),d=0,i=l.length;if(i===0)r.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let c of l)c.addEventListener("ended",()=>{d++,d>=i&&URL.revokeObjectURL(o)});return h}export{se as App,w as Session,oe as createStore,ae as videoStream};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
type TransportState = 'connecting' | 'connected' | 'disconnected' | 'failed';
|
|
3
|
+
type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed';
|
|
4
4
|
|
|
5
5
|
interface TransportSessionOptions {
|
|
6
6
|
|
|
@@ -209,6 +209,12 @@ interface TransportEvents {
|
|
|
209
209
|
error: (error: Error) => void;
|
|
210
210
|
started: () => void;
|
|
211
211
|
audioTransportReady: () => void;
|
|
212
|
+
|
|
213
|
+
docSync: (payload: Uint8Array) => void;
|
|
214
|
+
|
|
215
|
+
docSyncReady: () => void;
|
|
216
|
+
|
|
217
|
+
stateChange: (state: TransportState) => void;
|
|
212
218
|
}
|
|
213
219
|
interface SessionStartOptions {
|
|
214
220
|
app?: string;
|
|
@@ -241,6 +247,8 @@ declare class TransportSession {
|
|
|
241
247
|
private _reconnectAttempts;
|
|
242
248
|
private _intentionalDisconnect;
|
|
243
249
|
private _reconnectTimer;
|
|
250
|
+
|
|
251
|
+
private _stableResetTimer;
|
|
244
252
|
private _generation;
|
|
245
253
|
private _lastStartOptions;
|
|
246
254
|
private _resolveConnection;
|
|
@@ -263,6 +271,12 @@ declare class TransportSession {
|
|
|
263
271
|
disconnect(): void;
|
|
264
272
|
on<E extends keyof TransportEvents>(event: E, handler: TransportEvents[E]): () => void;
|
|
265
273
|
sendInput(data: Record<string, unknown>, sequence?: number): void;
|
|
274
|
+
|
|
275
|
+
sendDocSync(payload: Uint8Array): void;
|
|
276
|
+
|
|
277
|
+
onDocSync(handler: (payload: Uint8Array) => void): () => void;
|
|
278
|
+
|
|
279
|
+
onDocSyncReady(handler: () => void): () => void;
|
|
266
280
|
addTrack(track: MediaStreamTrack): Promise<void>;
|
|
267
281
|
removeTrack(): void;
|
|
268
282
|
requestDiagnostics(): void;
|
|
@@ -282,6 +296,9 @@ declare class TransportSession {
|
|
|
282
296
|
private _connectOnce;
|
|
283
297
|
private _startMessage;
|
|
284
298
|
private _cleanupSocket;
|
|
299
|
+
|
|
300
|
+
private _scheduleReconnectBudgetReset;
|
|
301
|
+
private _clearReconnectBudgetReset;
|
|
285
302
|
private _cleanupMedia;
|
|
286
303
|
private _send;
|
|
287
304
|
private _setState;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
type TransportState = 'connecting' | 'connected' | 'disconnected' | 'failed';
|
|
3
|
+
type TransportState = 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed';
|
|
4
4
|
|
|
5
5
|
interface TransportSessionOptions {
|
|
6
6
|
|
|
@@ -209,6 +209,12 @@ interface TransportEvents {
|
|
|
209
209
|
error: (error: Error) => void;
|
|
210
210
|
started: () => void;
|
|
211
211
|
audioTransportReady: () => void;
|
|
212
|
+
|
|
213
|
+
docSync: (payload: Uint8Array) => void;
|
|
214
|
+
|
|
215
|
+
docSyncReady: () => void;
|
|
216
|
+
|
|
217
|
+
stateChange: (state: TransportState) => void;
|
|
212
218
|
}
|
|
213
219
|
interface SessionStartOptions {
|
|
214
220
|
app?: string;
|
|
@@ -241,6 +247,8 @@ declare class TransportSession {
|
|
|
241
247
|
private _reconnectAttempts;
|
|
242
248
|
private _intentionalDisconnect;
|
|
243
249
|
private _reconnectTimer;
|
|
250
|
+
|
|
251
|
+
private _stableResetTimer;
|
|
244
252
|
private _generation;
|
|
245
253
|
private _lastStartOptions;
|
|
246
254
|
private _resolveConnection;
|
|
@@ -263,6 +271,12 @@ declare class TransportSession {
|
|
|
263
271
|
disconnect(): void;
|
|
264
272
|
on<E extends keyof TransportEvents>(event: E, handler: TransportEvents[E]): () => void;
|
|
265
273
|
sendInput(data: Record<string, unknown>, sequence?: number): void;
|
|
274
|
+
|
|
275
|
+
sendDocSync(payload: Uint8Array): void;
|
|
276
|
+
|
|
277
|
+
onDocSync(handler: (payload: Uint8Array) => void): () => void;
|
|
278
|
+
|
|
279
|
+
onDocSyncReady(handler: () => void): () => void;
|
|
266
280
|
addTrack(track: MediaStreamTrack): Promise<void>;
|
|
267
281
|
removeTrack(): void;
|
|
268
282
|
requestDiagnostics(): void;
|
|
@@ -282,6 +296,9 @@ declare class TransportSession {
|
|
|
282
296
|
private _connectOnce;
|
|
283
297
|
private _startMessage;
|
|
284
298
|
private _cleanupSocket;
|
|
299
|
+
|
|
300
|
+
private _scheduleReconnectBudgetReset;
|
|
301
|
+
private _clearReconnectBudgetReset;
|
|
285
302
|
private _cleanupMedia;
|
|
286
303
|
private _send;
|
|
287
304
|
private _setState;
|
package/dist/internal.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { b as ChannelMessage, c as ChannelMultiplexer, d as ChannelName, j as SessionStartOptions, T as TransportSession, n as TransportSessionOptions } from './internal-
|
|
1
|
+
export { b as ChannelMessage, c as ChannelMultiplexer, d as ChannelName, j as SessionStartOptions, T as TransportSession, n as TransportSessionOptions } from './internal-dmugzBC7.mjs';
|
package/dist/internal.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { b as ChannelMessage, c as ChannelMultiplexer, d as ChannelName, j as SessionStartOptions, T as TransportSession, n as TransportSessionOptions } from './internal-
|
|
1
|
+
export { b as ChannelMessage, c as ChannelMultiplexer, d as ChannelName, j as SessionStartOptions, T as TransportSession, n as TransportSessionOptions } from './internal-dmugzBC7.js';
|