@urun-sh/core 0.1.6 → 0.1.7

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 g,b as x}from"./chunk-YOWFDYKX.mjs";function v(s,e){let t={...s};for(let r of Object.keys(e)){let n=e[r],o=t[r];n!==null&&typeof n=="object"&&!Array.isArray(n)&&o!==null&&typeof o=="object"&&!Array.isArray(o)?t[r]=v(o,n):t[r]=n}return t}function C(s,e){let t=e.split("."),r=s;for(let n of t){if(r==null||typeof r!="object")return;r=r[n]}return r}var S=class{_state={};_listeners=new Set;_key;_sessionId;_multiplexer;constructor(e,t,r){this._key=e,this._sessionId=t,this._multiplexer=r,this._multiplexer.on(this._channel,n=>{if(n.type==="doc_update"&&n.patch&&typeof n.patch=="object"){this._state=v(this._state,n.patch);let o={...this._state};for(let i of this._listeners)i(o)}})}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 t={...this._state};for(let r of this._listeners)r(t)}get(e,t){return e?C(this._state,e)??t:{...this._state}}on(e,t){return this._listeners.add(t),()=>this._listeners.delete(t)}};var O="session:",u=class{_sessionId;_multiplexer;_transport;_docs=new Map;constructor(e,t,r){this._sessionId=e,this._multiplexer=t,this._transport=r}stream(e,t){if(arguments.length===1){let r=this._transport.getTrackByName(e);if(!r)throw new Error(`[urun] No track available for stream "${e}"`);return r}else t==null?this._transport.removeTrack():this._transport.addTrack(t).catch(r=>{console.error(`[urun] Failed to add track for stream "${e}":`,r)})}doc(e){let t=this._docs.get(e);return t||(t=new S(e,this._sessionId,this._multiplexer),this._docs.set(e,t)),t}on(e,t){let r=`${O}${this._sessionId}:${e}`;return this._multiplexer.on(r,n=>{t(n)})}};var P=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function T(s){return s.replace(/^http/,"ws").replace(/\/$/,"")+"/ws"}function A(s,e){let t=()=>{s.send(JSON.stringify(e))};if(s.readyState===WebSocket.OPEN){t();return}if(s.readyState===WebSocket.CONNECTING){let r=()=>{s.removeEventListener("open",r),t()};s.addEventListener("open",r)}}function $(s,e){let t=T(e.baseUrl),r=new WebSocket(t),n=new g(r);return new Proxy({},{get(o,i){if(!(P.has(i)||typeof i!="string"))return m=>{let d=m??{},p=`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,l=new x({url:t,orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:p});return l.connect({app:s,functionName:i,args:d}).catch(h=>{console.error(`[urun] Failed to connect session for ${i}:`,h)}),A(r,{type:"app.call",app:s,fn:i,args:d,sessionId:p,orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider}),new u(p,n,l)}}})}var I=3e4,E="stream:",y=class{_ws;_multiplexer;_pendingRpc=new Map;_rpcId=0;constructor(e,t){this._ws=e,this._multiplexer=t,this._ws.addEventListener("message",r=>{let n;try{n=JSON.parse(typeof r.data=="string"?r.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,t){let r=`${E}${e}`;return this._multiplexer.on(r,n=>{t(n)})}emit(e,t){let r=`${E}${e}`,n=typeof t=="object"&&t!==null&&!Array.isArray(t)?t:{data:t};this._multiplexer.emit(r,n)}_getInternals(){return{multiplexer:this._multiplexer,ws:this._ws}}_sendRpc(e,t){return new Promise((r,n)=>{if(!this._ws||this._ws.readyState!==WebSocket.OPEN){n(new Error("WebSocket not open"));return}let o=String(++this._rpcId),i=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),n(new Error(`RPC request "${e}" timed out after ${I}ms`)))},I);this._pendingRpc.set(o,{resolve:r,reject:n,timer:i}),this._ws.send(JSON.stringify({rpcId:o,type:e,...t}))})}};function L(s){let r=`${s.baseUrl.replace(/\/$/,"").replace(/^http/,"ws")}/store/${encodeURIComponent(s.orgId)}`,n=new WebSocket(r);(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 g(n);return new y(n,o)}var j='video/mp4; codecs="avc1.42E01E"';function U(s,e){let t=e?.codec??j,r=document.createElement("video");r.muted=!0,r.playsInline=!0;let n=new MediaSource,o=URL.createObjectURL(n);r.src=o,n.addEventListener("sourceopen",()=>{let a;try{a=n.addSourceBuffer(t)}catch(c){throw c instanceof DOMException&&c.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).`):c}let M=s.getReader();function w(){return a.updating?new Promise(c=>{a.addEventListener("updateend",()=>c(),{once:!0})}):Promise.resolve()}async function R(){try{for(;;){let{done:c,value:k}=await M.read();if(c){await w(),n.readyState==="open"&&n.endOfStream();return}await w();try{a.appendBuffer(k)}catch(f){if(f instanceof DOMException&&f.name==="QuotaExceededError"){let _=a.buffered;if(_.length>0){let b=_.start(_.length-1);a.remove(0,b),await w(),a.appendBuffer(k)}else throw f}else throw f}}}catch{if(n.readyState==="open")try{n.endOfStream("decode")}catch{}}}R()}),r.play().catch(()=>{});let i=r,m=i.captureStream??i.mozCaptureStream;if(!m)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let d=m.call(r),p=d.getTracks(),l=0,h=p.length;if(h===0)r.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let a of p)a.addEventListener("ended",()=>{l++,l>=h&&URL.revokeObjectURL(o)});return d}export{$ as App,u as Session,L as createStore,U as videoStream};
1
+ import{a as _,b as M}from"./chunk-X4M6CFTJ.mjs";function w(s,e){let t={...s};for(let r of Object.keys(e)){let n=e[r],o=t[r];n!==null&&typeof n=="object"&&!Array.isArray(n)&&o!==null&&typeof o=="object"&&!Array.isArray(o)?t[r]=w(o,n):t[r]=n}return t}function C(s,e){let t=e.split("."),r=s;for(let n of t){if(r==null||typeof r!="object")return;r=r[n]}return r}var g=class{_state={};_listeners=new Set;_key;_sessionId;_multiplexer;constructor(e,t,r){this._key=e,this._sessionId=t,this._multiplexer=r,this._multiplexer.on(this._channel,n=>{if(n.type==="doc_update"&&n.patch&&typeof n.patch=="object"){this._state=w(this._state,n.patch);let o={...this._state};for(let i of this._listeners)i(o)}})}get _channel(){return`session:${this._sessionId}:doc:${this._key}`}set(e){this._state=w(this._state,e),this._multiplexer.emit(this._channel,{type:"doc_update",patch:e,snapshot:this._state});let t={...this._state};for(let r of this._listeners)r(t)}get(e,t){return e?C(this._state,e)??t:{...this._state}}on(e,t){return this._listeners.add(t),()=>this._listeners.delete(t)}};var k=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)}},u=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}stream(e){let t=this._streams.get(e);return t||(t=new k(e,this._transport),this._streams.set(e,t)),t}doc(e){let t=this._docs.get(e);return t||(t=new g(e,this._sessionId,this._multiplexer),this._docs.set(e,t)),t}disconnect(){for(let e of this._streams.values())e.dispose();this._streams.clear(),this._transport.disconnect()}};var O=new Set(["then","catch","finally","toJSON",Symbol.toPrimitive]);function P(s){return s.replace(/^http/,"ws").replace(/\/$/,"")+"/ws"}function A(s,e){let t=()=>{s.send(JSON.stringify(e))};if(s.readyState===WebSocket.OPEN){t();return}if(s.readyState===WebSocket.CONNECTING){let r=()=>{s.removeEventListener("open",r),t()};s.addEventListener("open",r)}}function L(s,e){let t=P(e.baseUrl),r=new WebSocket(t),n=new _(r);return new Proxy({},{get(o,i){if(!(O.has(i)||typeof i!="string"))return m=>{let d=m??{},p=`s_${Date.now()}_${Math.random().toString(36).slice(2)}`,l=new M({url:t,orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider,sessionId:p});return l.connect({app:s,functionName:i,args:d}).catch(h=>{console.error(`[urun] Failed to connect session for ${i}:`,h)}),A(r,{type:"app.call",app:s,fn:i,args:d,sessionId:p,orgId:e.orgId,jwt:e.jwt,authProvider:e.authProvider}),new u(p,n,l)}}})}var E=3e4,b="stream:",y=class{_ws;_multiplexer;_pendingRpc=new Map;_rpcId=0;constructor(e,t){this._ws=e,this._multiplexer=t,this._ws.addEventListener("message",r=>{let n;try{n=JSON.parse(typeof r.data=="string"?r.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,t){let r=`${b}${e}`;return this._multiplexer.on(r,n=>{t(n)})}emit(e,t){let r=`${b}${e}`,n=typeof t=="object"&&t!==null&&!Array.isArray(t)?t:{data:t};this._multiplexer.emit(r,n)}_getInternals(){return{multiplexer:this._multiplexer,ws:this._ws}}_sendRpc(e,t){return new Promise((r,n)=>{if(!this._ws||this._ws.readyState!==WebSocket.OPEN){n(new Error("WebSocket not open"));return}let o=String(++this._rpcId),i=setTimeout(()=>{this._pendingRpc.has(o)&&(this._pendingRpc.delete(o),n(new Error(`RPC request "${e}" timed out after ${E}ms`)))},E);this._pendingRpc.set(o,{resolve:r,reject:n,timer:i}),this._ws.send(JSON.stringify({rpcId:o,type:e,...t}))})}};function j(s){let r=`${s.baseUrl.replace(/\/$/,"").replace(/^http/,"ws")}/store/${encodeURIComponent(s.orgId)}`,n=new WebSocket(r);(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 _(n);return new y(n,o)}var U='video/mp4; codecs="avc1.42E01E"';function $(s,e){let t=e?.codec??U,r=document.createElement("video");r.muted=!0,r.playsInline=!0;let n=new MediaSource,o=URL.createObjectURL(n);r.src=o,n.addEventListener("sourceopen",()=>{let a;try{a=n.addSourceBuffer(t)}catch(c){throw c instanceof DOMException&&c.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).`):c}let I=s.getReader();function S(){return a.updating?new Promise(c=>{a.addEventListener("updateend",()=>c(),{once:!0})}):Promise.resolve()}async function R(){try{for(;;){let{done:c,value:x}=await I.read();if(c){await S(),n.readyState==="open"&&n.endOfStream();return}await S();try{a.appendBuffer(x)}catch(f){if(f instanceof DOMException&&f.name==="QuotaExceededError"){let v=a.buffered;if(v.length>0){let T=v.start(v.length-1);a.remove(0,T),await S(),a.appendBuffer(x)}else throw f}else throw f}}}catch{if(n.readyState==="open")try{n.endOfStream("decode")}catch{}}}R()}),r.play().catch(()=>{});let i=r,m=i.captureStream??i.mozCaptureStream;if(!m)throw new Error("captureStream() is not supported in this browser. videoStream() requires HTMLMediaElement.captureStream() support.");let d=m.call(r),p=d.getTracks(),l=0,h=p.length;if(h===0)r.addEventListener("emptied",()=>{URL.revokeObjectURL(o)},{once:!0});else for(let a of p)a.addEventListener("ended",()=>{l++,l>=h&&URL.revokeObjectURL(o)});return d}export{L as App,u as Session,j as createStore,$ as videoStream};
@@ -26,19 +26,6 @@ interface ChannelMessage {
26
26
  [key: string]: unknown;
27
27
  }
28
28
 
29
- interface SessionContext {
30
-
31
- state: TransportState;
32
-
33
- id: string | null;
34
-
35
- emit: (name: string, data?: unknown) => void;
36
-
37
- on: (name: string, handler: (data: unknown) => void) => () => void;
38
-
39
- disconnect: () => void;
40
- }
41
-
42
29
  interface Layer {
43
30
 
44
31
  id: string;
@@ -138,15 +125,28 @@ interface SessionDocument {
138
125
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
139
126
  }
140
127
 
128
+ interface SessionStream {
129
+
130
+ readonly track: MediaStreamTrack | null;
131
+
132
+ attach(track: MediaStreamTrack): Promise<void>;
133
+
134
+ detach(): Promise<void>;
135
+
136
+ seek(target: number | 'live'): Promise<void>;
137
+
138
+ on(event: 'track', handler: (track: MediaStreamTrack | null) => void): () => void;
139
+ }
140
+
141
141
  interface Session {
142
142
 
143
- stream(name: string): MediaStreamTrack;
143
+ readonly id: string;
144
144
 
145
- stream(name: string, track: MediaStreamTrack | null): void;
145
+ stream(name: string): SessionStream;
146
146
 
147
147
  doc(key: string): SessionDocument;
148
148
 
149
- on(name: string, handler: (data: unknown) => void): () => void;
149
+ disconnect(): void;
150
150
  }
151
151
 
152
152
  interface AppOptions {
@@ -233,6 +233,7 @@ declare class TransportSession {
233
233
  addTrack(track: MediaStreamTrack): Promise<void>;
234
234
  removeTrack(): void;
235
235
  requestDiagnostics(): void;
236
+ seekStream(name: string, target: number | 'live'): void;
236
237
  private _handleMessage;
237
238
  private _onRouterCapabilities;
238
239
  private _onCreateRecvTransport;
@@ -244,4 +245,4 @@ declare class TransportSession {
244
245
  private _setState;
245
246
  }
246
247
 
247
- export { type App as A, type ChannelMessage as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, ChannelMultiplexer as b, type ChannelName as c, type LayoutConfig as d, type LayoutContext as e, type SceneGraph as f, type Session as g, type SessionContext as h, type SessionDocument as i, type SessionStartOptions as j, type Store as k, type StoreOptions as l, type TransportSessionOptions as m, type TransportState as n };
248
+ export { type App as A, type ChannelMessage as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, ChannelMultiplexer as b, type ChannelName as c, type LayoutConfig as d, type LayoutContext as e, type SceneGraph as f, type Session as g, type SessionDocument as h, type SessionStartOptions as i, type SessionStream as j, type Store as k, type StoreOptions as l, type TransportSessionOptions as m, type TransportState as n };
@@ -26,19 +26,6 @@ interface ChannelMessage {
26
26
  [key: string]: unknown;
27
27
  }
28
28
 
29
- interface SessionContext {
30
-
31
- state: TransportState;
32
-
33
- id: string | null;
34
-
35
- emit: (name: string, data?: unknown) => void;
36
-
37
- on: (name: string, handler: (data: unknown) => void) => () => void;
38
-
39
- disconnect: () => void;
40
- }
41
-
42
29
  interface Layer {
43
30
 
44
31
  id: string;
@@ -138,15 +125,28 @@ interface SessionDocument {
138
125
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
139
126
  }
140
127
 
128
+ interface SessionStream {
129
+
130
+ readonly track: MediaStreamTrack | null;
131
+
132
+ attach(track: MediaStreamTrack): Promise<void>;
133
+
134
+ detach(): Promise<void>;
135
+
136
+ seek(target: number | 'live'): Promise<void>;
137
+
138
+ on(event: 'track', handler: (track: MediaStreamTrack | null) => void): () => void;
139
+ }
140
+
141
141
  interface Session {
142
142
 
143
- stream(name: string): MediaStreamTrack;
143
+ readonly id: string;
144
144
 
145
- stream(name: string, track: MediaStreamTrack | null): void;
145
+ stream(name: string): SessionStream;
146
146
 
147
147
  doc(key: string): SessionDocument;
148
148
 
149
- on(name: string, handler: (data: unknown) => void): () => void;
149
+ disconnect(): void;
150
150
  }
151
151
 
152
152
  interface AppOptions {
@@ -233,6 +233,7 @@ declare class TransportSession {
233
233
  addTrack(track: MediaStreamTrack): Promise<void>;
234
234
  removeTrack(): void;
235
235
  requestDiagnostics(): void;
236
+ seekStream(name: string, target: number | 'live'): void;
236
237
  private _handleMessage;
237
238
  private _onRouterCapabilities;
238
239
  private _onCreateRecvTransport;
@@ -244,4 +245,4 @@ declare class TransportSession {
244
245
  private _setState;
245
246
  }
246
247
 
247
- export { type App as A, type ChannelMessage as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, ChannelMultiplexer as b, type ChannelName as c, type LayoutConfig as d, type LayoutContext as e, type SceneGraph as f, type Session as g, type SessionContext as h, type SessionDocument as i, type SessionStartOptions as j, type Store as k, type StoreOptions as l, type TransportSessionOptions as m, type TransportState as n };
248
+ export { type App as A, type ChannelMessage as C, type Layer as L, type SceneContext as S, TransportSession as T, type AppOptions as a, ChannelMultiplexer as b, type ChannelName as c, type LayoutConfig as d, type LayoutContext as e, type SceneGraph as f, type Session as g, type SessionDocument as h, type SessionStartOptions as i, type SessionStream as j, type Store as k, type StoreOptions as l, type TransportSessionOptions as m, type TransportState as n };
@@ -1 +1 @@
1
- export { C as ChannelMessage, b as ChannelMultiplexer, c as ChannelName, j as SessionStartOptions, T as TransportSession, m as TransportSessionOptions } from './internal-D8r8sCkX.mjs';
1
+ export { C as ChannelMessage, b as ChannelMultiplexer, c as ChannelName, i as SessionStartOptions, T as TransportSession, m as TransportSessionOptions } from './internal-qHdgijfz.mjs';
@@ -1 +1 @@
1
- export { C as ChannelMessage, b as ChannelMultiplexer, c as ChannelName, j as SessionStartOptions, T as TransportSession, m as TransportSessionOptions } from './internal-D8r8sCkX.js';
1
+ export { C as ChannelMessage, b as ChannelMultiplexer, c as ChannelName, i as SessionStartOptions, T as TransportSession, m as TransportSessionOptions } from './internal-qHdgijfz.js';