@synnaxlabs/client 0.17.4 → 0.17.5

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @synnaxlabs/client@0.17.4 build /home/runner/work/synnax/synnax/client/ts
2
+ > @synnaxlabs/client@0.17.5 build /home/runner/work/synnax/synnax/client/ts
3
3
  > tsc --noEmit && vite build
4
4
 
5
5
  vite v5.1.2 building for production...
@@ -9,8 +9,8 @@ rendering chunks...
9
9
  
10
10
  [vite:dts] Start generate declaration files...
11
11
  computing gzip size...
12
- dist/client.js 536.81 kB │ gzip: 118.15 kB │ map: 1,205.72 kB
13
- [vite:dts] Declaration files built in 3718ms.
12
+ dist/client.js 536.96 kB │ gzip: 118.20 kB │ map: 1,206.15 kB
13
+ [vite:dts] Declaration files built in 3761ms.
14
14
  
15
- dist/client.cjs 354.56 kB │ gzip: 93.44 kB │ map: 1,150.60 kB
16
- ✓ built in 5.24s
15
+ dist/client.cjs 354.67 kB │ gzip: 93.49 kB │ map: 1,151.00 kB
16
+ ✓ built in 5.36s
package/dist/client.cjs CHANGED
@@ -18,7 +18,7 @@
18
18
  `)}}class qt extends Hr{}class Sw extends qt{}class Nw extends qt{}class ph extends Hr{constructor(t,n){super(t);h(this,"path");this.path=n}}class Ew extends Hr{}const Aw=r=>{if(!r.type.startsWith(kr))return null;switch(r.type){case $r.General:return new dh(r.data);case $r.Parse:return new hh(r.data);case $r.Auth:return new Ec(r.data);case $r.Unexpected:return new wn(r.data);case $r.Validation:return new gt(r.data);case $r.Query:return new qt(r.data);case $r.Route:return new ph(r.data,r.data);default:return new wn(r.data)}},Iw=()=>{throw new Error("Not implemented")};ih({encode:Iw,decode:Aw});const Zw=async(r,e)=>{const[t,n]=await e(r);return n==null?[t,n]:n instanceof Ws?[t,new Ws({message:`Cannot reach cluster at ${n.url.host}:${n.url.port}`,url:n.url})]:[t,n]},$w=u.z.object({leaseholder:u.z.number().optional(),keys:u.z.number().array().optional(),names:u.z.string().array().optional(),search:u.z.string().optional(),rangeKey:u.z.string().optional(),limit:u.z.number().optional(),offset:u.z.number().optional()}),Cw=u.z.object({channels:Qs.array()}),_i=class _i{constructor(e){h(this,"client");this.client=e}async search(e,t){return await this.execute({search:e,rangeKey:t})}async retrieve(e,t){const{variant:n,normalized:s}=Xr(e);return await this.execute({[n]:s,rangeKey:t})}async page(e,t,n){return await this.execute({offset:e,limit:t,rangeKey:n})}async execute(e){const[t,n]=await this.client.send(_i.ENDPOINT,e,$w,Cw);if(n!=null)throw n;return t.channels}};h(_i,"ENDPOINT","/channel/retrieve");let Ys=_i;class Ac{constructor(e){h(this,"cache");h(this,"namesToKeys");h(this,"wrapped");this.cache=new Map,this.namesToKeys=new Map,this.wrapped=e}async search(e,t){return await this.wrapped.search(e,t)}async page(e,t,n){return await this.wrapped.page(e,t,n)}async retrieve(e){const{normalized:t}=Xr(e),n=[],s=[];if(t.forEach(i=>{const o=this.getFromCache(i);o!=null?n.push(o):s.push(i)}),s.length===0)return n;const a=await this.wrapped.retrieve(s);return this.updateCache(a),n.concat(a)}updateCache(e){e.forEach(t=>{this.cache.set(t.key,t),this.namesToKeys.set(t.name,t.key)})}getFromCache(e){const t=typeof e=="number"?e:this.namesToKeys.get(e);if(t!=null)return this.cache.get(t)}}const Xr=r=>{let e=Ne(r).filter(n=>n!==0),t="keys";return typeof e[0]=="string"&&(isNaN(parseInt(e[0]))?t="names":e=e.map(n=>parseInt(n))),{single:!Array.isArray(r),variant:t,normalized:e,actual:r}};class fh{constructor(e,t){h(this,"mu",new Um);h(this,"requests",new Map);h(this,"wrapped");h(this,"debouncedRun");this.wrapped=e,this.debouncedRun=om(()=>{this.run()},t)}async search(e,t){return await this.wrapped.search(e,t)}async page(e,t,n){return await this.wrapped.page(e,t,n)}async retrieve(e){const{normalized:t,variant:n}=Xr(e);return n==="names"?await this.wrapped.retrieve(t):await new Promise((a,i)=>{this.mu.runExclusive(()=>{this.requests.set(t,{resolve:a,reject:i}),this.debouncedRun()})})}async run(){await this.mu.runExclusive(async()=>{const e=new Set;this.requests.forEach((t,n)=>n.forEach(s=>e.add(s)));try{const t=await this.wrapped.retrieve(Array.from(e));this.requests.forEach((n,s)=>n.resolve(t.filter(a=>s.includes(a.key))))}catch(t){this.requests.forEach(n=>n.reject(t))}finally{this.requests.clear()}})}}const mh=async(r,e)=>{const{normalized:t}=Xr(e),n=await r.retrieve(t),s=[];if(t.forEach(a=>{n.find(i=>i.name===a||i.key===a)==null&&s.push(a)}),s.length>0)throw new qt(`Could not find channels: ${JSON.stringify(s)}`);return n};class Ic{constructor({dataType:e,rate:t,name:n,leaseholder:s=0,key:a=0,isIndex:i=!1,index:o=0,frameClient:c,alias:l}){h(this,"_frameClient");h(this,"key");h(this,"name");h(this,"rate");h(this,"dataType");h(this,"leaseholder");h(this,"index");h(this,"isIndex");h(this,"alias");this.key=a,this.name=n,this.rate=new Ni(t??0),this.dataType=new ze(e),this.leaseholder=s,this.index=o,this.isIndex=i,this.alias=l,this._frameClient=c??null}get framer(){if(this._frameClient==null)throw new gt("cannot read from a channel that has not been created");return this._frameClient}get payload(){return Qs.parse({key:this.key,name:this.name,rate:this.rate.valueOf(),dataType:this.dataType.valueOf(),leaseholder:this.leaseholder,index:this.index,isIndex:this.isIndex})}async read(e){return await this.framer.read(e,this.key)}async write(e,t){return await this.framer.write(this.key,e,t)}}let yh=class{constructor(e,t,n,s){h(this,"frameClient");h(this,"retriever");h(this,"creator");h(this,"client");this.frameClient=e,this.retriever=t,this.client=n,this.creator=s}async create(e,t={}){const{retrieveIfNameExists:n=!1}=t,s=!Array.isArray(e);let a=Ne(e),i=[];if(n){const o=await this.retriever.retrieve(a.map(l=>l.name)),c=new Set(o.map(l=>l.name));a=a.filter(l=>!c.has(l.name)),i=this.sugar(o)}return i=i.concat(this.sugar(await this.creator.create(a))),s?i[0]:i}async retrieve(e,t){const{single:n,actual:s,normalized:a}=Xr(e);if(a.length===0)return[];const i=this.sugar(await this.retriever.retrieve(e,t));if(!n)return i;if(i.length===0)throw new Sw(`channel matching ${s} not found`);if(i.length>1)throw new Nw(`multiple channels matching ${s} found`);return i[0]}async search(e,t){return this.sugar(await this.retriever.search(e,t))}newSearcherUnderRange(e){return new jw(this,e)}async page(e,t,n){return this.sugar(await this.retriever.page(e,t,n))}createDebouncedBatchRetriever(e=10){return new Ac(new fh(new Ys(this.client),e))}sugar(e){const{frameClient:t}=this;return e.map(n=>new Ic({...n,frameClient:t}))}};class jw{constructor(e,t){h(this,"client");h(this,"rangeKey");this.client=e,this.rangeKey=t}async search(e){return await this.client.search(e,this.rangeKey)}async page(e,t){return await this.client.page(e,t,this.rangeKey)}async retrieve(e){return await this.client.retrieve(e,this.rangeKey)}}const Rw=Object.freeze(Object.defineProperty({__proto__:null,CacheRetriever:Ac,Channel:Ic,Client:yh,ClusterRetriever:Ys,Creator:Xa,DebouncedBatchRetriever:fh,analyzeParams:Xr,keyZ:Xs,newPayload:Ql,parseChannels:ed,payload:Qs,retrieveRequired:mh},Symbol.toStringTag,{value:"Module"})),zw=u.z.object({key:u.z.string(),username:u.z.string()}),Pw=r=>async(e,t)=>{try{const n=await r();e.params.Authorization=`Bearer ${n}`}catch(n){return[e,n]}return await t(e)},Mw=u.z.object({username:u.z.string(),password:u.z.string()}),Uw=u.z.object({token:u.z.string(),user:zw}),Dw="/auth/login";let Lw=class{constructor(e,t){h(this,"token");h(this,"client");h(this,"authenticating");h(this,"authenticated");h(this,"user");this.client=e,this.authenticated=!1,this.authenticate(t)}authenticate(e){this.authenticating=new Promise((t,n)=>{this.client.send(Dw,e,Mw,Uw).then(([s,a])=>{if(a!=null)return n(a);this.token=s==null?void 0:s.token,this.user=s==null?void 0:s.user,this.authenticated=!0,t()}).catch(n)})}middleware(){return Pw(async()=>{if(this.authenticated||await this.authenticating,this.token==null)throw new Ec("[auth] - attempting to authenticate without a token");return this.token})}};const Bw=["disconnected","connecting","connected","failed"],gh=u.z.enum(Bw),vh=u.z.object({status:gh,error:u.z.instanceof(Error).optional(),message:u.z.string().optional(),clusterKey:u.z.string()}),Vw=u.z.object({clusterKey:u.z.string()}),Uu={clusterKey:"",status:"disconnected",error:void 0,message:"Disconnected"},An=class An{constructor(e,t=Re.seconds(30),n){h(this,"_state");h(this,"pollFrequency",Re.seconds(30));h(this,"client");h(this,"name");h(this,"interval");h(this,"onChangeHandlers",[]);this._state={...Uu},this.client=e,this.pollFrequency=t,this.name=n,this.check(),this.startChecking()}stopChecking(){this.interval!=null&&clearInterval(this.interval)}async check(){var t;const e=this._state.status;try{const[n,s]=await this.client.send(An.ENDPOINT,{},u.z.object({}),Vw);if(s!=null)throw s;this._state.status="connected",this._state.message=`Connected to ${this.name??"cluster"}`,this._state.clusterKey=n.clusterKey}catch(n){this._state.status="failed",this._state.error=n,this._state.message=(t=this.state.error)==null?void 0:t.message}return this.onChangeHandlers.length>0&&e!==this._state.status&&this.onChangeHandlers.forEach(n=>n(this.state)),this.state}get state(){return{...this._state}}onChange(e){this.onChangeHandlers.push(e)}startChecking(){this.interval=setInterval(()=>{this.check()},this.pollFrequency.milliseconds)}};h(An,"ENDPOINT","/connectivity/check"),h(An,"DEFAULT",Uu),h(An,"connectionStateZ",vh);let Js=An;const qw=Object.freeze(Object.defineProperty({__proto__:null,Checker:Js,state:vh,status:gh},Symbol.toStringTag,{value:"Module"})),Cr=class Cr extends Number{};h(Cr,"Absolute",255),h(Cr,"Default",1),h(Cr,"z",u.z.union([u.z.instanceof(Cr),u.z.number().int().min(0).max(255).transform(e=>new Cr(e)),u.z.instanceof(Number).transform(e=>new Cr(e))]));let Gr=Cr;const Du=r=>{const e=Ne(r);return e.length===0?null:typeof e[0]=="number"?"key":"name"},Lu=(r,e)=>{const t=Ne(r);if(t.length===e.length)return;const n=Du(r);throw Du===null?new gt("[Frame] - channel keys or names must be provided when constructing a frame."):new gt(`[Frame] - ${n}s and arrays must be the same length.
19
19
  Got ${t.length} ${n}s and ${e.length} arrays.`)};class Je{constructor(e=[],t=[]){h(this,"columns",[]);h(this,"series",[]);if(e instanceof Je){this.columns=e.columns,this.series=e.series;return}if(e instanceof Map){e.forEach((s,a)=>this.push(a,...Ne(s)));return}if(typeof e=="object"&&!Array.isArray(e)){if("keys"in e&&"series"in e){const s=e,a=s.series.map(i=>bh(i));Lu(s.keys,a),s.keys.forEach((i,o)=>this.push(i,a[o]))}else Object.entries(e).forEach(([s,a])=>{const i=parseInt(s);if(isNaN(i))this.push(s,...Ne(a));else return this.push(i,...Ne(a))});return}if(Array.isArray(e)||["string","number"].includes(typeof e)){const s=Ne(t),a=Ne(e);Lu(a,s),s.forEach((i,o)=>this.push(a[o],i));return}throw new gt(`[Frame] - invalid frame construction parameters. data parameter ust be a frame
20
20
  payload, a list of lazy arrays, a lazy array, a map, or a record keyed by channel
21
- name. keys parameter must be a set of channel keys or channel names.`)}get colType(){return this.columns.length===0?null:typeof this.columns[0]=="string"?"name":"key"}get keys(){if(this.colType==="name")throw new wn("colType is not key");return this.columns??[]}get uniqueKeys(){return eu(this.keys)}get names(){if(this.colType==="key")throw new wn("colType is not name");return this.columns??[]}get uniqueNames(){return eu(this.names)}get uniqueColumns(){return this.colType==="key"?this.uniqueKeys:this.uniqueNames}toPayload(){return{series:this.series.map(e=>_h(e)),keys:this.keys}}get isVertical(){return this.uniqueColumns.length===this.columns.length}get isHorizontal(){return this.uniqueColumns.length===1}get isSquare(){return this.isHorizontal&&this.isVertical}get isWeaklyAligned(){if(this.columns.length<=1)return!0;const e=this.timeRanges;return e.every(t=>t.equals(e[0]))}timeRange(e){if(e==null){if(this.columns.length===0)return $t.ZERO;const n=Se.min(...this.series.map(a=>a.timeRange.start)),s=Se.max(...this.series.map(a=>a.timeRange.end));return new $t(n,s)}const t=this.get(e);return t==null?$t.ZERO:new $t(t[0].timeRange.start,t[t.length-1].timeRange.end)}get timeRanges(){return this.uniqueColumns.map(e=>this.timeRange(e))}get(e){return Array.isArray(e)?this.filter(t=>e.includes(t)):this.series.filter((t,n)=>this.columns[n]===e)}push(e,...t){if(e instanceof Je){if(this.colType!==null&&e.colType!==this.colType)throw new gt("keyVariant must match");this.series.push(...e.series),this.columns.push(...e.columns)}else{if(this.series.push(...t),typeof e=="string"&&this.colType==="key")throw new gt("keyVariant must match");if(typeof e!="string"&&this.colType==="name")throw new gt("keyVariant must match");this.columns.push(...Array.from({length:t.length},()=>e))}}concat(e){return new Je([...this.columns,...e.columns],[...this.series,...e.series])}has(e){return typeof e=="string"&&this.colType==="key"||typeof e=="number"&&this.colType==="name"?!1:this.columns.includes(e)}map(e){const t=new Je;return this.forEach((n,s,a)=>t.push(...e(n,s,a))),t}forEach(e){this.columns.forEach((t,n)=>{const s=this.series[n];e(t,s,n)})}filter(e){const t=new Je;return this.columns.forEach((n,s)=>{const a=this.series[s];e(n,a,s)&&t.push(n,a)}),t}get byteLength(){return new Ga(this.series.reduce((e,t)=>e.add(t.byteLength),Ga.ZERO))}get length(){return this.series.reduce((e,t)=>e+t.length,0)}}const wh=u.z.object({timeRange:$t.z.optional(),alignment:u.z.number().optional(),dataType:ze.z,data:u.z.string().transform(r=>new Uint8Array(atob(r).split("").map(e=>e.charCodeAt(0))).buffer)}),Ui=u.z.object({keys:u.z.union([u.z.null().transform(()=>[]),u.z.number().array().optional().default([])]),series:u.z.union([u.z.null().transform(()=>[]),wh.array().optional().default([])])}),bh=r=>{const{dataType:e,data:t,timeRange:n,alignment:s}=r;return new Zn({data:t,dataType:e,timeRange:n,glBufferUsage:"static",alignment:s})},_h=r=>({timeRange:r._timeRange,dataType:r.dataType,data:new Uint8Array(r.data.buffer),alignment:r.alignment});class Di{constructor(e){h(this,"adapter");h(this,"retriever");h(this,"keys");this.retriever=e,this.adapter=null,this.keys=[]}static async open(e,t){const n=new Di(e);return await n.update(t),n}async update(e){const{variant:t,normalized:n}=Xr(e);if(t==="keys"){this.adapter=null,this.keys=n;return}const s=await this.retriever.retrieve(n),a=new Map;this.adapter=a,n.forEach(i=>{const o=s.find(c=>c.name===i);if(o==null)throw new Error(`Channel ${i} not found`);a.set(o.key,o.name)}),this.keys=Array.from(this.adapter.keys())}adapt(e){if(this.adapter==null)return e;const t=this.adapter;return e.map((n,s)=>{if(typeof n=="number"){const a=t.get(n);if(a==null)throw new Error(`Channel ${n} not found`);return[a,s]}return[n,s]})}}class Zc{constructor(e){h(this,"adapter");h(this,"retriever");h(this,"keys");this.retriever=e,this.adapter=null,this.keys=[]}static async open(e,t){const n=new Zc(e);return await n.update(t),n}async update(e){const t=await mh(this.retriever,e);this.adapter=new Map(t.map(n=>[n.name,n.key])),this.keys=t.map(n=>n.key)}async fetchChannel(e){const t=await this.retriever.retrieve(e);if(t.length===0)throw new Error(`Channel ${e} not found`);return t[0]}async adapt(e,t){if(typeof e=="string"||typeof e=="number"){if(t==null)throw new gt(`
21
+ name. keys parameter must be a set of channel keys or channel names.`)}get colType(){return this.columns.length===0?null:typeof this.columns[0]=="string"?"name":"key"}get keys(){if(this.colType==="name")throw new wn("colType is not key");return this.columns??[]}get uniqueKeys(){return eu(this.keys)}get names(){if(this.colType==="key")throw new wn("colType is not name");return this.columns??[]}get uniqueNames(){return eu(this.names)}get uniqueColumns(){return this.colType==="key"?this.uniqueKeys:this.uniqueNames}toPayload(){return{series:this.series.map(e=>_h(e)),keys:this.keys}}get isVertical(){return this.uniqueColumns.length===this.columns.length}get isHorizontal(){return this.uniqueColumns.length===1}get isSquare(){return this.isHorizontal&&this.isVertical}get isWeaklyAligned(){if(this.columns.length<=1)return!0;const e=this.timeRanges;return e.every(t=>t.equals(e[0]))}timeRange(e){if(e==null){if(this.columns.length===0)return $t.ZERO;const n=Se.min(...this.series.map(a=>a.timeRange.start)),s=Se.max(...this.series.map(a=>a.timeRange.end));return new $t(n,s)}const t=this.get(e);return t==null?$t.ZERO:new $t(t[0].timeRange.start,t[t.length-1].timeRange.end)}latest(){return Object.fromEntries(this.columns.map((e,t)=>[e,this.series[t].at(-1)]).filter(([e,t])=>t!=null))}get timeRanges(){return this.uniqueColumns.map(e=>this.timeRange(e))}get(e){return Array.isArray(e)?this.filter(t=>e.includes(t)):this.series.filter((t,n)=>this.columns[n]===e)}push(e,...t){if(e instanceof Je){if(this.colType!==null&&e.colType!==this.colType)throw new gt("keyVariant must match");this.series.push(...e.series),this.columns.push(...e.columns)}else{if(this.series.push(...t),typeof e=="string"&&this.colType==="key")throw new gt("keyVariant must match");if(typeof e!="string"&&this.colType==="name")throw new gt("keyVariant must match");this.columns.push(...Array.from({length:t.length},()=>e))}}concat(e){return new Je([...this.columns,...e.columns],[...this.series,...e.series])}has(e){return typeof e=="string"&&this.colType==="key"||typeof e=="number"&&this.colType==="name"?!1:this.columns.includes(e)}map(e){const t=new Je;return this.forEach((n,s,a)=>t.push(...e(n,s,a))),t}forEach(e){this.columns.forEach((t,n)=>{const s=this.series[n];e(t,s,n)})}filter(e){const t=new Je;return this.columns.forEach((n,s)=>{const a=this.series[s];e(n,a,s)&&t.push(n,a)}),t}get byteLength(){return new Ga(this.series.reduce((e,t)=>e.add(t.byteLength),Ga.ZERO))}get length(){return this.series.reduce((e,t)=>e+t.length,0)}}const wh=u.z.object({timeRange:$t.z.optional(),alignment:u.z.number().optional(),dataType:ze.z,data:u.z.string().transform(r=>new Uint8Array(atob(r).split("").map(e=>e.charCodeAt(0))).buffer)}),Ui=u.z.object({keys:u.z.union([u.z.null().transform(()=>[]),u.z.number().array().optional().default([])]),series:u.z.union([u.z.null().transform(()=>[]),wh.array().optional().default([])])}),bh=r=>{const{dataType:e,data:t,timeRange:n,alignment:s}=r;return new Zn({data:t,dataType:e,timeRange:n,glBufferUsage:"static",alignment:s})},_h=r=>({timeRange:r._timeRange,dataType:r.dataType,data:new Uint8Array(r.data.buffer),alignment:r.alignment});class Di{constructor(e){h(this,"adapter");h(this,"retriever");h(this,"keys");this.retriever=e,this.adapter=null,this.keys=[]}static async open(e,t){const n=new Di(e);return await n.update(t),n}async update(e){const{variant:t,normalized:n}=Xr(e);if(t==="keys"){this.adapter=null,this.keys=n;return}const s=await this.retriever.retrieve(n),a=new Map;this.adapter=a,n.forEach(i=>{const o=s.find(c=>c.name===i);if(o==null)throw new Error(`Channel ${i} not found`);a.set(o.key,o.name)}),this.keys=Array.from(this.adapter.keys())}adapt(e){if(this.adapter==null)return e;const t=this.adapter;return e.map((n,s)=>{if(typeof n=="number"){const a=t.get(n);if(a==null)throw new Error(`Channel ${n} not found`);return[a,s]}return[n,s]})}}class Zc{constructor(e){h(this,"adapter");h(this,"retriever");h(this,"keys");this.retriever=e,this.adapter=null,this.keys=[]}static async open(e,t){const n=new Zc(e);return await n.update(t),n}async update(e){const t=await mh(this.retriever,e);this.adapter=new Map(t.map(n=>[n.name,n.key])),this.keys=t.map(n=>n.key)}async fetchChannel(e){const t=await this.retriever.retrieve(e);if(t.length===0)throw new Error(`Channel ${e} not found`);return t[0]}async adapt(e,t){if(typeof e=="string"||typeof e=="number"){if(t==null)throw new gt(`
22
22
  Received a single channel name or key but no series.
23
23
  `);if(Array.isArray(t)){if(t.length>1)throw new gt(`
24
24
  Received a single channel name or key but multiple series.