@multitapio/multitap 0.0.11 → 0.0.12

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 +1 @@
1
- {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../../src/codegen/rust.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAKpB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AA67C/D;;;GAGG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,QAAQ,EAAG,MAAM,CAAU;IACpC,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,eAAe,CAAS;gBAEpB,IAAI,GAAE,gBAAqB;IAKvC,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA+ClD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA2BlD,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,MAAM;CAe7E"}
1
+ {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../../src/codegen/rust.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAKpB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AA66C/D;;;GAGG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,QAAQ,EAAG,MAAM,CAAU;IACpC,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,eAAe,CAAS;gBAEpB,IAAI,GAAE,gBAAqB;IAKvC,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA+ClD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA2BlD,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,MAAM;CAe7E"}
@@ -6019,7 +6019,7 @@ function createStoreApi(schemaDef) {
6019
6019
  }
6020
6020
 
6021
6021
  // src/rollback.worker.ts
6022
- var workerCode = 'var Or=Object.defineProperty;var Fr=(t,e,n)=>e in t?Or(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var y=(t,e,n)=>Fr(t,typeof e!="symbol"?e+"":e,n);var Ht=Symbol("Comlink.proxy"),Nr=Symbol("Comlink.endpoint"),Mr=Symbol("Comlink.releaseProxy"),ft=Symbol("Comlink.finalizer"),ve=Symbol("Comlink.thrown"),Dt=t=>typeof t=="object"&&t!==null||typeof t=="function",Rr={canHandle:t=>Dt(t)&&t[Ht],serialize(t){let{port1:e,port2:n}=new MessageChannel;return Be(t,e),[n,[n]]},deserialize(t){return t.start(),dt(t)}},Hr={canHandle:t=>Dt(t)&&ve in t,serialize({value:t}){let e;return t instanceof Error?e={isError:!0,value:{message:t.message,name:t.name,stack:t.stack}}:e={isError:!1,value:t},[e,[]]},deserialize(t){throw t.isError?Object.assign(new Error(t.value.message),t.value):t.value}},Lt=new Map([["proxy",Rr],["throw",Hr]]);function Dr(t,e){for(let n of t)if(e===n||n==="*"||n instanceof RegExp&&n.test(e))return!0;return!1}function Be(t,e=globalThis,n=["*"]){e.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Dr(n,i.origin)){console.warn(`Invalid origin \'${i.origin}\' for comlink proxy`);return}let{id:o,type:s,path:a}=Object.assign({path:[]},i.data),c=(i.data.argumentList||[]).map(j),u;try{let f=a.slice(0,-1).reduce((g,S)=>g[S],t),d=a.reduce((g,S)=>g[S],t);switch(s){case"GET":u=d;break;case"SET":f[a.slice(-1)[0]]=j(i.data.value),u=!0;break;case"APPLY":u=d.apply(f,c);break;case"CONSTRUCT":{let g=new d(...c);u=qr(g)}break;case"ENDPOINT":{let{port1:g,port2:S}=new MessageChannel;Be(t,S),u=Zr(g,[g])}break;case"RELEASE":u=void 0;break;default:return}}catch(f){u={value:f,[ve]:0}}Promise.resolve(u).catch(f=>({value:f,[ve]:0})).then(f=>{let[d,g]=Ie(f);e.postMessage(Object.assign(Object.assign({},d),{id:o}),g),s==="RELEASE"&&(e.removeEventListener("message",r),zt(e),ft in t&&typeof t[ft]=="function"&&t[ft]())}).catch(f=>{let[d,g]=Ie({value:new TypeError("Unserializable return value"),[ve]:0});e.postMessage(Object.assign(Object.assign({},d),{id:o}),g)})}),e.start&&e.start()}function Lr(t){return t.constructor.name==="MessagePort"}function zt(t){Lr(t)&&t.close()}function dt(t,e){let n=new Map;return t.addEventListener("message",function(i){let{data:o}=i;if(!o||!o.id)return;let s=n.get(o.id);if(s)try{s(o)}finally{n.delete(o.id)}}),lt(t,n,[],e)}function Ae(t){if(t)throw new Error("Proxy has been released and is not useable")}function Vt(t){return re(t,new Map,{type:"RELEASE"}).then(()=>{zt(t)})}var Ue=new WeakMap,Ce="FinalizationRegistry"in globalThis&&new FinalizationRegistry(t=>{let e=(Ue.get(t)||0)-1;Ue.set(t,e),e===0&&Vt(t)});function zr(t,e){let n=(Ue.get(e)||0)+1;Ue.set(e,n),Ce&&Ce.register(t,e,t)}function Vr(t){Ce&&Ce.unregister(t)}function lt(t,e,n=[],r=function(){}){let i=!1,o=new Proxy(r,{get(s,a){if(Ae(i),a===Mr)return()=>{Vr(o),Vt(t),e.clear(),i=!0};if(a==="then"){if(n.length===0)return{then:()=>o};let c=re(t,e,{type:"GET",path:n.map(u=>u.toString())}).then(j);return c.then.bind(c)}return lt(t,e,[...n,a])},set(s,a,c){Ae(i);let[u,f]=Ie(c);return re(t,e,{type:"SET",path:[...n,a].map(d=>d.toString()),value:u},f).then(j)},apply(s,a,c){Ae(i);let u=n[n.length-1];if(u===Nr)return re(t,e,{type:"ENDPOINT"}).then(j);if(u==="bind")return lt(t,e,n.slice(0,-1));let[f,d]=Rt(c);return re(t,e,{type:"APPLY",path:n.map(g=>g.toString()),argumentList:f},d).then(j)},construct(s,a){Ae(i);let[c,u]=Rt(a);return re(t,e,{type:"CONSTRUCT",path:n.map(f=>f.toString()),argumentList:c},u).then(j)}});return zr(o,t),o}function Gr(t){return Array.prototype.concat.apply([],t)}function Rt(t){let e=t.map(Ie);return[e.map(n=>n[0]),Gr(e.map(n=>n[1]))]}var Gt=new WeakMap;function Zr(t,e){return Gt.set(t,e),t}function qr(t){return Object.assign(t,{[Ht]:!0})}function Ie(t){for(let[e,n]of Lt)if(n.canHandle(t)){let[r,i]=n.serialize(t);return[{type:"HANDLER",name:e,value:r},i]}return[{type:"RAW",value:t},Gt.get(t)||[]]}function j(t){switch(t.type){case"HANDLER":return Lt.get(t.name).deserialize(t.value);case"RAW":return t.value}}function re(t,e,n,r){return new Promise(i=>{let o=Kr();e.set(o,i),t.start&&t.start(),t.postMessage(Object.assign({id:o},n),r)})}function Kr(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var Wr={p:0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,n:0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,h:1n,a:0n,b:7n,Gx:0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,Gy:0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n},{p:J,n:yt,Gx:Yr,Gy:jr,b:Xt}=Wr,ie=32,pt=64,U=(t="")=>{throw new Error(t)},Jt=t=>typeof t=="bigint",Qt=t=>typeof t=="string",Xr=t=>t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array",me=(t,e)=>!Xr(t)||typeof e=="number"&&e>0&&t.length!==e?U("Uint8Array expected"):t,Ne=t=>new Uint8Array(t),Jr=t=>Uint8Array.from(t),en=(t,e)=>t.toString(16).padStart(e,"0"),gt=t=>Array.from(me(t)).map(e=>en(e,2)).join(""),z={_0:48,_9:57,A:65,F:70,a:97,f:102},Zt=t=>{if(t>=z._0&&t<=z._9)return t-z._0;if(t>=z.A&&t<=z.F)return t-(z.A-10);if(t>=z.a&&t<=z.f)return t-(z.a-10)},bt=t=>{let e="hex invalid";if(!Qt(t))return U(e);let n=t.length,r=n/2;if(n%2)return U(e);let i=Ne(r);for(let o=0,s=0;o<r;o++,s+=2){let a=Zt(t.charCodeAt(s)),c=Zt(t.charCodeAt(s+1));if(a===void 0||c===void 0)return U(e);i[o]=a*16+c}return i},xt=(t,e)=>me(Qt(t)?bt(t):Jr(me(t)),e),tn=()=>globalThis?.crypto,Qr=()=>tn()?.subtle??U("crypto.subtle must be defined"),_e=(...t)=>{let e=Ne(t.reduce((r,i)=>r+me(i).length,0)),n=0;return t.forEach(r=>{e.set(r,n),n+=r.length}),e},ei=(t=ie)=>tn().getRandomValues(Ne(t)),$e=BigInt,he=(t,e,n,r="bad number: out of range")=>Jt(t)&&e<=t&&t<n?t:U(r),h=(t,e=J)=>{let n=t%e;return n>=0n?n:e+n};var nn=(t,e)=>{(t===0n||e<=0n)&&U("no inverse n="+t+" mod="+e);let n=h(t,e),r=e,i=0n,o=1n,s=1n,a=0n;for(;n!==0n;){let c=r/n,u=r%n,f=i-s*c,d=o-a*c;r=n,n=u,i=s,o=a,s=f,a=d}return r===1n?h(i,e):U("no inverse")};var qt=t=>t instanceof Q?t:U("Point expected"),rn=t=>h(h(t*t)*t+Xt),Kt=t=>he(t,0n,J),Pe=t=>he(t,1n,J),ti=t=>he(t,1n,yt),mt=t=>(t&1n)===0n,on=t=>Uint8Array.of(t),ni=t=>on(mt(t)?2:3),ri=t=>{let e=rn(Pe(t)),n=1n;for(let r=e,i=(J+1n)/4n;i>0n;i>>=1n)i&1n&&(n=n*r%J),r=r*r%J;return h(n*n)===e?n:U("sqrt invalid")},R=class R{constructor(e,n,r){y(this,"px");y(this,"py");y(this,"pz");this.px=Kt(e),this.py=Pe(n),this.pz=Kt(r),Object.freeze(this)}static fromBytes(e){me(e);let n,r=e[0],i=e.subarray(1),o=Wt(i,0,ie),s=e.length;if(s===ie+1&&[2,3].includes(r)){let a=ri(o),c=mt(a);mt($e(r))!==c&&(a=h(-a)),n=new R(o,a,1n)}return s===pt+1&&r===4&&(n=new R(o,Wt(i,ie,pt),1n)),n?n.assertValidity():U("bad point: not on curve")}equals(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=qt(e),c=h(n*a),u=h(o*i),f=h(r*a),d=h(s*i);return c===u&&f===d}is0(){return this.equals(X)}negate(){return new R(this.px,h(-this.py),this.pz)}double(){return this.add(this)}add(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=qt(e),c=0n,u=Xt,f=0n,d=0n,g=0n,S=h(u*3n),k=h(n*o),A=h(r*s),O=h(i*a),ne=h(n+r),v=h(o+s);ne=h(ne*v),v=h(k+A),ne=h(ne-v),v=h(n+i);let M=h(o+a);return v=h(v*M),M=h(k+O),v=h(v-M),M=h(r+i),f=h(s+a),M=h(M*f),f=h(A+O),M=h(M-f),g=h(c*v),f=h(S*O),g=h(f+g),f=h(A-g),g=h(A+g),d=h(f*g),A=h(k+k),A=h(A+k),O=h(c*O),v=h(S*v),A=h(A+O),O=h(k-O),O=h(c*O),v=h(v+O),k=h(A*v),d=h(d+k),k=h(M*v),f=h(ne*f),f=h(f-k),k=h(ne*A),g=h(M*g),g=h(g+k),new R(f,d,g)}multiply(e,n=!0){if(!n&&e===0n)return X;if(ti(e),e===1n)return this;if(this.equals(oe))return fi(e).p;let r=X,i=oe;for(let o=this;e>0n;o=o.double(),e>>=1n)e&1n?r=r.add(o):n&&(i=i.add(o));return r}toAffine(){let{px:e,py:n,pz:r}=this;if(this.equals(X))return{x:0n,y:0n};if(r===1n)return{x:e,y:n};let i=nn(r,J);return h(r*i)!==1n&&U("inverse invalid"),{x:h(e*i),y:h(n*i)}}assertValidity(){let{x:e,y:n}=this.toAffine();return Pe(e),Pe(n),h(n*n)===rn(e)?this:U("bad point: not on curve")}toBytes(e=!0){let{x:n,y:r}=this.assertValidity().toAffine(),i=Oe(n);return e?_e(ni(r),i):_e(on(4),i,Oe(r))}static fromAffine(e){let{x:n,y:r}=e;return n===0n&&r===0n?X:new R(n,r,1n)}toHex(e){return gt(this.toBytes(e))}static fromPrivateKey(e){return oe.multiply(oi(e))}static fromHex(e){return R.fromBytes(xt(e))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}toRawBytes(e){return this.toBytes(e)}};y(R,"BASE"),y(R,"ZERO");var Q=R,oe=new Q(Yr,jr,1n),X=new Q(0n,1n,0n);Q.BASE=oe;Q.ZERO=X;var Me=t=>$e("0x"+(gt(t)||"0")),Wt=(t,e,n)=>Me(t.subarray(e,n)),ii=2n**256n,Oe=t=>bt(en(he(t,0n,ii),pt)),oi=t=>{let e=Jt(t)?t:Me(xt(t,ie));return he(e,1n,yt,"private key invalid 3")};var si=t=>{t=xt(t),(t.length<ie+8||t.length>1024)&&U("expected 40-1024b");let e=h(Me(t),yt-1n);return Oe(e+1n)};var ai="SHA-256",wt={hexToBytes:bt,bytesToHex:gt,concatBytes:_e,bytesToNumberBE:Me,numberToBytesBE:Oe,mod:h,invert:nn,hmacSha256Async:async(t,...e)=>{let n=Qr(),r="HMAC",i=await n.importKey("raw",t,{name:r,hash:{name:ai}},!1,["sign"]);return Ne(await n.sign(r,i,_e(...e)))},hmacSha256Sync:void 0,hashToPrivateKey:si,randomBytes:ei};var Fe=8,ci=256,sn=Math.ceil(ci/Fe)+1,ht=2**(Fe-1),ui=()=>{let t=[],e=oe,n=e;for(let r=0;r<sn;r++){n=e,t.push(n);for(let i=1;i<ht;i++)n=n.add(e),t.push(n);e=n.double()}return t},Yt,jt=(t,e)=>{let n=e.negate();return t?n:e},fi=t=>{let e=Yt||(Yt=ui()),n=X,r=oe,i=2**Fe,o=i,s=$e(i-1),a=$e(Fe);for(let c=0;c<sn;c++){let u=Number(t&s);t>>=a,u>ht&&(u-=o,t+=1n);let f=c*ht,d=f,g=f+Math.abs(u)-1,S=c%2!==0,k=u<0;u===0?r=r.add(jt(S,e[d])):n=n.add(jt(k,e[g]))}return{p:n,f:r}};function di(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function an(t){if(!Number.isSafeInteger(t)||t<0)throw new Error("positive integer expected, got "+t)}function ae(t,...e){if(!di(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}function cn(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash should be wrapped by utils.createHasher");an(t.outputLen),an(t.blockLen)}function ce(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function un(t,e){ae(t);let n=e.outputLen;if(t.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function ee(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Re(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function F(t,e){return t<<32-e|t>>>e}function pi(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}function ye(t){return typeof t=="string"&&(t=pi(t)),ae(t),t}var se=class{};function fn(t){let e=r=>t().update(ye(r)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function mi(t,e,n,r){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,n,r);let i=BigInt(32),o=BigInt(4294967295),s=Number(n>>i&o),a=Number(n&o),c=r?4:0,u=r?0:4;t.setUint32(e+c,s,r),t.setUint32(e+u,a,r)}function ln(t,e,n){return t&e^~t&n}function dn(t,e,n){return t&e^t&n^e&n}var He=class extends se{constructor(e,n,r,i){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=i,this.buffer=new Uint8Array(e),this.view=Re(this.buffer)}update(e){ce(this),e=ye(e),ae(e);let{view:n,buffer:r,blockLen:i}=this,o=e.length;for(let s=0;s<o;){let a=Math.min(i-this.pos,o-s);if(a===i){let c=Re(e);for(;i<=o-s;s+=i)this.process(c,s);continue}r.set(e.subarray(s,s+a),this.pos),this.pos+=a,s+=a,this.pos===i&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){ce(this),un(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:i,isLE:o}=this,{pos:s}=this;n[s++]=128,ee(this.buffer.subarray(s)),this.padOffset>i-s&&(this.process(r,0),s=0);for(let d=s;d<i;d++)n[d]=0;mi(r,i-8,BigInt(this.length*8),o),this.process(r,0);let a=Re(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let u=c/4,f=this.get();if(u>f.length)throw new Error("_sha2: outputLen bigger than state");for(let d=0;d<u;d++)a.setUint32(4*d,f[d],o)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:i,finished:o,destroyed:s,pos:a}=this;return e.destroyed=s,e.finished=o,e.length=i,e.pos=a,i%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},V=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var hi=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Z=new Uint32Array(64),De=class extends He{constructor(e=32){super(64,e,8,!1),this.A=V[0]|0,this.B=V[1]|0,this.C=V[2]|0,this.D=V[3]|0,this.E=V[4]|0,this.F=V[5]|0,this.G=V[6]|0,this.H=V[7]|0}get(){let{A:e,B:n,C:r,D:i,E:o,F:s,G:a,H:c}=this;return[e,n,r,i,o,s,a,c]}set(e,n,r,i,o,s,a,c){this.A=e|0,this.B=n|0,this.C=r|0,this.D=i|0,this.E=o|0,this.F=s|0,this.G=a|0,this.H=c|0}process(e,n){for(let d=0;d<16;d++,n+=4)Z[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let g=Z[d-15],S=Z[d-2],k=F(g,7)^F(g,18)^g>>>3,A=F(S,17)^F(S,19)^S>>>10;Z[d]=A+Z[d-7]+k+Z[d-16]|0}let{A:r,B:i,C:o,D:s,E:a,F:c,G:u,H:f}=this;for(let d=0;d<64;d++){let g=F(a,6)^F(a,11)^F(a,25),S=f+g+ln(a,c,u)+hi[d]+Z[d]|0,A=(F(r,2)^F(r,13)^F(r,22))+dn(r,i,o)|0;f=u,u=c,c=a,a=s+S|0,s=o,o=i,i=r,r=S+A|0}r=r+this.A|0,i=i+this.B|0,o=o+this.C|0,s=s+this.D|0,a=a+this.E|0,c=c+this.F|0,u=u+this.G|0,f=f+this.H|0,this.set(r,i,o,s,a,c,u,f)}roundClean(){ee(Z)}destroy(){this.set(0,0,0,0,0,0,0,0),ee(this.buffer)}};var ge=fn(()=>new De);var St=ge;var Le=class extends se{constructor(e,n){super(),this.finished=!1,this.destroyed=!1,cn(e);let r=ye(n);if(this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let i=this.blockLen,o=new Uint8Array(i);o.set(r.length>i?e.create().update(r).digest():r);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),ee(o)}update(e){return ce(this),this.iHash.update(e),this}digestInto(e){ce(this),ae(e,this.outputLen),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:i,destroyed:o,blockLen:s,outputLen:a}=this;return e=e,e.finished=i,e.destroyed=o,e.blockLen=s,e.outputLen=a,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Et=(t,e,n)=>new Le(t,e).update(n).digest();Et.create=(t,e)=>new Le(t,e);var yi=["string","number","bigint","symbol"],gi=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function pn(t){if(t===null)return"null";if(t===void 0)return"undefined";if(t===!0||t===!1)return"boolean";let e=typeof t;if(yi.includes(e))return e;if(e==="function")return"Function";if(Array.isArray(t))return"Array";if(bi(t))return"Buffer";let n=xi(t);return n||"Object"}function bi(t){return t&&t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer.call(null,t)}function xi(t){let e=Object.prototype.toString.call(t).slice(8,-1);if(gi.includes(e))return e}var l=class{constructor(e,n,r){this.major=e,this.majorEncoded=e<<5,this.name=n,this.terminal=r}toString(){return`Type[${this.major}].${this.name}`}compare(e){return this.major<e.major?-1:this.major>e.major?1:0}};l.uint=new l(0,"uint",!0);l.negint=new l(1,"negint",!0);l.bytes=new l(2,"bytes",!0);l.string=new l(3,"string",!0);l.array=new l(4,"array",!1);l.map=new l(5,"map",!1);l.tag=new l(6,"tag",!1);l.float=new l(7,"float",!0);l.false=new l(7,"false",!0);l.true=new l(7,"true",!0);l.null=new l(7,"null",!0);l.undefined=new l(7,"undefined",!0);l.break=new l(7,"break",!0);var m=class{constructor(e,n,r){this.type=e,this.value=n,this.encodedLength=r,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var ue=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",wi=new TextDecoder,Si=new TextEncoder;function ze(t){return ue&&globalThis.Buffer.isBuffer(t)}function kt(t){return t instanceof Uint8Array?ze(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):t:Uint8Array.from(t)}var gn=ue?(t,e,n)=>n-e>64?globalThis.Buffer.from(t.subarray(e,n)).toString("utf8"):hn(t,e,n):(t,e,n)=>n-e>64?wi.decode(t.subarray(e,n)):hn(t,e,n),bn=ue?t=>t.length>64?globalThis.Buffer.from(t):mn(t):t=>t.length>64?Si.encode(t):mn(t),H=t=>Uint8Array.from(t),fe=ue?(t,e,n)=>ze(t)?new Uint8Array(t.subarray(e,n)):t.slice(e,n):(t,e,n)=>t.slice(e,n),xn=ue?(t,e)=>(t=t.map(n=>n instanceof Uint8Array?n:globalThis.Buffer.from(n)),kt(globalThis.Buffer.concat(t,e))):(t,e)=>{let n=new Uint8Array(e),r=0;for(let i of t)r+i.length>n.length&&(i=i.subarray(0,n.length-r)),n.set(i,r),r+=i.length;return n},wn=ue?t=>globalThis.Buffer.allocUnsafe(t):t=>new Uint8Array(t);function Ve(t,e){if(ze(t)&&ze(e))return t.compare(e);for(let n=0;n<t.length;n++)if(t[n]!==e[n])return t[n]<e[n]?-1:1;return 0}function mn(t){let e=[],n=0;for(let r=0;r<t.length;r++){let i=t.charCodeAt(r);i<128?e[n++]=i:i<2048?(e[n++]=i>>6|192,e[n++]=i&63|128):(i&64512)===55296&&r+1<t.length&&(t.charCodeAt(r+1)&64512)===56320?(i=65536+((i&1023)<<10)+(t.charCodeAt(++r)&1023),e[n++]=i>>18|240,e[n++]=i>>12&63|128,e[n++]=i>>6&63|128,e[n++]=i&63|128):(e[n++]=i>>12|224,e[n++]=i>>6&63|128,e[n++]=i&63|128)}return e}function hn(t,e,n){let r=[];for(;e<n;){let i=t[e],o=null,s=i>239?4:i>223?3:i>191?2:1;if(e+s<=n){let a,c,u,f;switch(s){case 1:i<128&&(o=i);break;case 2:a=t[e+1],(a&192)===128&&(f=(i&31)<<6|a&63,f>127&&(o=f));break;case 3:a=t[e+1],c=t[e+2],(a&192)===128&&(c&192)===128&&(f=(i&15)<<12|(a&63)<<6|c&63,f>2047&&(f<55296||f>57343)&&(o=f));break;case 4:a=t[e+1],c=t[e+2],u=t[e+3],(a&192)===128&&(c&192)===128&&(u&192)===128&&(f=(i&15)<<18|(a&63)<<12|(c&63)<<6|u&63,f>65535&&f<1114112&&(o=f))}}o===null?(o=65533,s=1):o>65535&&(o-=65536,r.push(o>>>10&1023|55296),o=56320|o&1023),r.push(o),e+=s}return Ei(r)}var yn=4096;function Ei(t){let e=t.length;if(e<=yn)return String.fromCharCode.apply(String,t);let n="",r=0;for(;r<e;)n+=String.fromCharCode.apply(String,t.slice(r,r+=yn));return n}var ki=256,be=class{constructor(e=ki){this.chunkSize=e,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(e){let n=this.chunks[this.chunks.length-1];if(this.cursor+e.length<=this.maxCursor+1){let i=n.length-(this.maxCursor-this.cursor)-1;n.set(e,i)}else{if(n){let i=n.length-(this.maxCursor-this.cursor)-1;i<n.length&&(this.chunks[this.chunks.length-1]=n.subarray(0,i),this.maxCursor=this.cursor-1)}e.length<64&&e.length<this.chunkSize?(n=wn(this.chunkSize),this.chunks.push(n),this.maxCursor+=n.length,this._initReuseChunk===null&&(this._initReuseChunk=n),n.set(e,0)):(this.chunks.push(e),this.maxCursor+=e.length)}this.cursor+=e.length}toBytes(e=!1){let n;if(this.chunks.length===1){let r=this.chunks[0];e&&this.cursor>r.length/2?(n=this.cursor===r.length?r:r.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):n=fe(r,0,this.cursor)}else n=xn(this.chunks,this.cursor);return e&&this.reset(),n}};var b="CBOR decode error:",Tt="CBOR encode error:",xe=[];xe[23]=1;xe[24]=2;xe[25]=3;xe[26]=5;xe[27]=9;function G(t,e,n){if(t.length-e<n)throw new Error(`${b} not enough data for type`)}var E=[24,256,65536,4294967296,BigInt("18446744073709551616")];function C(t,e,n){G(t,e,1);let r=t[e];if(n.strict===!0&&r<E[0])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function I(t,e,n){G(t,e,2);let r=t[e]<<8|t[e+1];if(n.strict===!0&&r<E[1])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function B(t,e,n){G(t,e,4);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3];if(n.strict===!0&&r<E[2])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function P(t,e,n){G(t,e,8);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3],i=t[e+4]*16777216+(t[e+5]<<16)+(t[e+6]<<8)+t[e+7],o=(BigInt(r)<<BigInt(32))+BigInt(i);if(n.strict===!0&&o<E[3])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);if(o<=Number.MAX_SAFE_INTEGER)return Number(o);if(n.allowBigInt===!0)return o;throw new Error(`${b} integers outside of the safe integer range are not supported`)}function Sn(t,e,n,r){return new m(l.uint,C(t,e+1,r),2)}function En(t,e,n,r){return new m(l.uint,I(t,e+1,r),3)}function kn(t,e,n,r){return new m(l.uint,B(t,e+1,r),5)}function Tn(t,e,n,r){return new m(l.uint,P(t,e+1,r),9)}function _(t,e){return T(t,0,e.value)}function T(t,e,n){if(n<E[0]){let r=Number(n);t.push([e|r])}else if(n<E[1]){let r=Number(n);t.push([e|24,r])}else if(n<E[2]){let r=Number(n);t.push([e|25,r>>>8,r&255])}else if(n<E[3]){let r=Number(n);t.push([e|26,r>>>24&255,r>>>16&255,r>>>8&255,r&255])}else{let r=BigInt(n);if(r<E[4]){let i=[e|27,0,0,0,0,0,0,0],o=Number(r&BigInt(4294967295)),s=Number(r>>BigInt(32)&BigInt(4294967295));i[8]=o&255,o=o>>8,i[7]=o&255,o=o>>8,i[6]=o&255,o=o>>8,i[5]=o&255,i[4]=s&255,s=s>>8,i[3]=s&255,s=s>>8,i[2]=s&255,s=s>>8,i[1]=s&255,t.push(i)}else throw new Error(`${b} encountered BigInt larger than allowable range`)}}_.encodedSize=function(e){return T.encodedSize(e.value)};T.encodedSize=function(e){return e<E[0]?1:e<E[1]?2:e<E[2]?3:e<E[3]?5:9};_.compareTokens=function(e,n){return e.value<n.value?-1:e.value>n.value?1:0};function An(t,e,n,r){return new m(l.negint,-1-C(t,e+1,r),2)}function vn(t,e,n,r){return new m(l.negint,-1-I(t,e+1,r),3)}function Un(t,e,n,r){return new m(l.negint,-1-B(t,e+1,r),5)}var At=BigInt(-1),Cn=BigInt(1);function In(t,e,n,r){let i=P(t,e+1,r);if(typeof i!="bigint"){let o=-1-i;if(o>=Number.MIN_SAFE_INTEGER)return new m(l.negint,o,9)}if(r.allowBigInt!==!0)throw new Error(`${b} integers outside of the safe integer range are not supported`);return new m(l.negint,At-BigInt(i),9)}function Ge(t,e){let n=e.value,r=typeof n=="bigint"?n*At-Cn:n*-1-1;T(t,e.type.majorEncoded,r)}Ge.encodedSize=function(e){let n=e.value,r=typeof n=="bigint"?n*At-Cn:n*-1-1;return r<E[0]?1:r<E[1]?2:r<E[2]?3:r<E[3]?5:9};Ge.compareTokens=function(e,n){return e.value<n.value?1:e.value>n.value?-1:0};function we(t,e,n,r){G(t,e,n+r);let i=fe(t,e+n,e+n+r);return new m(l.bytes,i,n+r)}function Bn(t,e,n,r){return we(t,e,1,n)}function Pn(t,e,n,r){return we(t,e,2,C(t,e+1,r))}function _n(t,e,n,r){return we(t,e,3,I(t,e+1,r))}function $n(t,e,n,r){return we(t,e,5,B(t,e+1,r))}function On(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer bytes lengths not supported`);return we(t,e,9,i)}function Ze(t){return t.encodedBytes===void 0&&(t.encodedBytes=t.type===l.string?bn(t.value):t.value),t.encodedBytes}function le(t,e){let n=Ze(e);T(t,e.type.majorEncoded,n.length),t.push(n)}le.encodedSize=function(e){let n=Ze(e);return T.encodedSize(n.length)+n.length};le.compareTokens=function(e,n){return Ai(Ze(e),Ze(n))};function Ai(t,e){return t.length<e.length?-1:t.length>e.length?1:Ve(t,e)}function Se(t,e,n,r,i){let o=n+r;G(t,e,o);let s=new m(l.string,gn(t,e+n,e+o),o);return i.retainStringBytes===!0&&(s.byteValue=fe(t,e+n,e+o)),s}function Fn(t,e,n,r){return Se(t,e,1,n,r)}function Nn(t,e,n,r){return Se(t,e,2,C(t,e+1,r),r)}function Mn(t,e,n,r){return Se(t,e,3,I(t,e+1,r),r)}function Rn(t,e,n,r){return Se(t,e,5,B(t,e+1,r),r)}function Hn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer string lengths not supported`);return Se(t,e,9,i,r)}var Dn=le;function de(t,e,n,r){return new m(l.array,r,n)}function Ln(t,e,n,r){return de(t,e,1,n)}function zn(t,e,n,r){return de(t,e,2,C(t,e+1,r))}function Vn(t,e,n,r){return de(t,e,3,I(t,e+1,r))}function Gn(t,e,n,r){return de(t,e,5,B(t,e+1,r))}function Zn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer array lengths not supported`);return de(t,e,9,i)}function qn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return de(t,e,1,1/0)}function qe(t,e){T(t,l.array.majorEncoded,e.value)}qe.compareTokens=_.compareTokens;qe.encodedSize=function(e){return T.encodedSize(e.value)};function pe(t,e,n,r){return new m(l.map,r,n)}function Kn(t,e,n,r){return pe(t,e,1,n)}function Wn(t,e,n,r){return pe(t,e,2,C(t,e+1,r))}function Yn(t,e,n,r){return pe(t,e,3,I(t,e+1,r))}function jn(t,e,n,r){return pe(t,e,5,B(t,e+1,r))}function Xn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer map lengths not supported`);return pe(t,e,9,i)}function Jn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return pe(t,e,1,1/0)}function Ke(t,e){T(t,l.map.majorEncoded,e.value)}Ke.compareTokens=_.compareTokens;Ke.encodedSize=function(e){return T.encodedSize(e.value)};function Qn(t,e,n,r){return new m(l.tag,n,1)}function er(t,e,n,r){return new m(l.tag,C(t,e+1,r),2)}function tr(t,e,n,r){return new m(l.tag,I(t,e+1,r),3)}function nr(t,e,n,r){return new m(l.tag,B(t,e+1,r),5)}function rr(t,e,n,r){return new m(l.tag,P(t,e+1,r),9)}function We(t,e){T(t,l.tag.majorEncoded,e.value)}We.compareTokens=_.compareTokens;We.encodedSize=function(e){return T.encodedSize(e.value)};var Pi=20,_i=21,$i=22,Oi=23;function ir(t,e,n,r){if(r.allowUndefined===!1)throw new Error(`${b} undefined values are not supported`);return r.coerceUndefinedToNull===!0?new m(l.null,null,1):new m(l.undefined,void 0,1)}function or(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return new m(l.break,void 0,1)}function vt(t,e,n){if(n){if(n.allowNaN===!1&&Number.isNaN(t))throw new Error(`${b} NaN values are not supported`);if(n.allowInfinity===!1&&(t===1/0||t===-1/0))throw new Error(`${b} Infinity values are not supported`)}return new m(l.float,t,e)}function sr(t,e,n,r){return vt(Ut(t,e+1),3,r)}function ar(t,e,n,r){return vt(Ct(t,e+1),5,r)}function cr(t,e,n,r){return vt(dr(t,e+1),9,r)}function Ye(t,e,n){let r=e.value;if(r===!1)t.push([l.float.majorEncoded|Pi]);else if(r===!0)t.push([l.float.majorEncoded|_i]);else if(r===null)t.push([l.float.majorEncoded|$i]);else if(r===void 0)t.push([l.float.majorEncoded|Oi]);else{let i,o=!1;(!n||n.float64!==!0)&&(fr(r),i=Ut(N,1),r===i||Number.isNaN(r)?(N[0]=249,t.push(N.slice(0,3)),o=!0):(lr(r),i=Ct(N,1),r===i&&(N[0]=250,t.push(N.slice(0,5)),o=!0))),o||(Fi(r),i=dr(N,1),N[0]=251,t.push(N.slice(0,9)))}}Ye.encodedSize=function(e,n){let r=e.value;if(r===!1||r===!0||r===null||r===void 0)return 1;if(!n||n.float64!==!0){fr(r);let i=Ut(N,1);if(r===i||Number.isNaN(r))return 3;if(lr(r),i=Ct(N,1),r===i)return 5}return 9};var ur=new ArrayBuffer(9),$=new DataView(ur,1),N=new Uint8Array(ur,0);function fr(t){if(t===1/0)$.setUint16(0,31744,!1);else if(t===-1/0)$.setUint16(0,64512,!1);else if(Number.isNaN(t))$.setUint16(0,32256,!1);else{$.setFloat32(0,t);let e=$.getUint32(0),n=(e&2139095040)>>23,r=e&8388607;if(n===255)$.setUint16(0,31744,!1);else if(n===0)$.setUint16(0,(t&2147483648)>>16|r>>13,!1);else{let i=n-127;i<-24?$.setUint16(0,0):i<-14?$.setUint16(0,(e&2147483648)>>16|1<<24+i,!1):$.setUint16(0,(e&2147483648)>>16|i+15<<10|r>>13,!1)}}}function Ut(t,e){if(t.length-e<2)throw new Error(`${b} not enough data for float16`);let n=(t[e]<<8)+t[e+1];if(n===31744)return 1/0;if(n===64512)return-1/0;if(n===32256)return NaN;let r=n>>10&31,i=n&1023,o;return r===0?o=i*2**-24:r!==31?o=(i+1024)*2**(r-25):o=i===0?1/0:NaN,n&32768?-o:o}function lr(t){$.setFloat32(0,t,!1)}function Ct(t,e){if(t.length-e<4)throw new Error(`${b} not enough data for float32`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,4).getFloat32(0,!1)}function Fi(t){$.setFloat64(0,t,!1)}function dr(t,e){if(t.length-e<8)throw new Error(`${b} not enough data for float64`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,8).getFloat64(0,!1)}Ye.compareTokens=_.compareTokens;function x(t,e,n){throw new Error(`${b} encountered invalid minor (${n}) for major ${t[e]>>>5}`)}function je(t){return()=>{throw new Error(`${b} ${t}`)}}var p=[];for(let t=0;t<=23;t++)p[t]=x;p[24]=Sn;p[25]=En;p[26]=kn;p[27]=Tn;p[28]=x;p[29]=x;p[30]=x;p[31]=x;for(let t=32;t<=55;t++)p[t]=x;p[56]=An;p[57]=vn;p[58]=Un;p[59]=In;p[60]=x;p[61]=x;p[62]=x;p[63]=x;for(let t=64;t<=87;t++)p[t]=Bn;p[88]=Pn;p[89]=_n;p[90]=$n;p[91]=On;p[92]=x;p[93]=x;p[94]=x;p[95]=je("indefinite length bytes/strings are not supported");for(let t=96;t<=119;t++)p[t]=Fn;p[120]=Nn;p[121]=Mn;p[122]=Rn;p[123]=Hn;p[124]=x;p[125]=x;p[126]=x;p[127]=je("indefinite length bytes/strings are not supported");for(let t=128;t<=151;t++)p[t]=Ln;p[152]=zn;p[153]=Vn;p[154]=Gn;p[155]=Zn;p[156]=x;p[157]=x;p[158]=x;p[159]=qn;for(let t=160;t<=183;t++)p[t]=Kn;p[184]=Wn;p[185]=Yn;p[186]=jn;p[187]=Xn;p[188]=x;p[189]=x;p[190]=x;p[191]=Jn;for(let t=192;t<=215;t++)p[t]=Qn;p[216]=er;p[217]=tr;p[218]=nr;p[219]=rr;p[220]=x;p[221]=x;p[222]=x;p[223]=x;for(let t=224;t<=243;t++)p[t]=je("simple values are not supported");p[244]=x;p[245]=x;p[246]=x;p[247]=ir;p[248]=je("simple values are not supported");p[249]=sr;p[250]=ar;p[251]=cr;p[252]=x;p[253]=x;p[254]=x;p[255]=or;var D=[];for(let t=0;t<24;t++)D[t]=new m(l.uint,t,1);for(let t=-1;t>=-24;t--)D[31-t]=new m(l.negint,t,1);D[64]=new m(l.bytes,new Uint8Array(0),1);D[96]=new m(l.string,"",1);D[128]=new m(l.array,0,1);D[160]=new m(l.map,0,1);D[244]=new m(l.false,!1,1);D[245]=new m(l.true,!0,1);D[246]=new m(l.null,null,1);function pr(t){switch(t.type){case l.false:return H([244]);case l.true:return H([245]);case l.null:return H([246]);case l.bytes:return t.value.length?void 0:H([64]);case l.string:return t.value===""?H([96]):void 0;case l.array:return t.value===0?H([128]):void 0;case l.map:return t.value===0?H([160]):void 0;case l.uint:return t.value<24?H([Number(t.value)]):void 0;case l.negint:if(t.value>=-24)return H([31-Number(t.value)])}}var hr=Object.freeze({float64:!0,mapSorter:Di,quickEncodeToken:pr});function Mi(){let t=[];return t[l.uint.major]=_,t[l.negint.major]=Ge,t[l.bytes.major]=le,t[l.string.major]=Dn,t[l.array.major]=qe,t[l.map.major]=Ke,t[l.tag.major]=We,t[l.float.major]=Ye,t}var Ri=Mi(),It=new be,Je=class t{constructor(e,n){this.obj=e,this.parent=n}includes(e){let n=this;do if(n.obj===e)return!0;while(n=n.parent);return!1}static createCheck(e,n){if(e&&e.includes(n))throw new Error(`${Tt} object contains circular references`);return new t(n,e)}},q={null:new m(l.null,null),undefined:new m(l.undefined,void 0),true:new m(l.true,!0),false:new m(l.false,!1),emptyArray:new m(l.array,0),emptyMap:new m(l.map,0)},K={number(t,e,n,r){return!Number.isInteger(t)||!Number.isSafeInteger(t)?new m(l.float,t):t>=0?new m(l.uint,t):new m(l.negint,t)},bigint(t,e,n,r){return t>=BigInt(0)?new m(l.uint,t):new m(l.negint,t)},Uint8Array(t,e,n,r){return new m(l.bytes,t)},string(t,e,n,r){return new m(l.string,t)},boolean(t,e,n,r){return t?q.true:q.false},null(t,e,n,r){return q.null},undefined(t,e,n,r){return q.undefined},ArrayBuffer(t,e,n,r){return new m(l.bytes,new Uint8Array(t))},DataView(t,e,n,r){return new m(l.bytes,new Uint8Array(t.buffer,t.byteOffset,t.byteLength))},Array(t,e,n,r){if(!t.length)return n.addBreakTokens===!0?[q.emptyArray,new m(l.break)]:q.emptyArray;r=Je.createCheck(r,t);let i=[],o=0;for(let s of t)i[o++]=Xe(s,n,r);return n.addBreakTokens?[new m(l.array,t.length),i,new m(l.break)]:[new m(l.array,t.length),i]},Object(t,e,n,r){let i=e!=="Object",o=i?t.keys():Object.keys(t),s=i?t.size:o.length;if(!s)return n.addBreakTokens===!0?[q.emptyMap,new m(l.break)]:q.emptyMap;r=Je.createCheck(r,t);let a=[],c=0;for(let u of o)a[c++]=[Xe(u,n,r),Xe(i?t.get(u):t[u],n,r)];return Hi(a,n),n.addBreakTokens?[new m(l.map,s),a,new m(l.break)]:[new m(l.map,s),a]}};K.Map=K.Object;K.Buffer=K.Uint8Array;for(let t of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))K[`${t}Array`]=K.DataView;function Xe(t,e={},n){let r=pn(t),i=e&&e.typeEncoders&&e.typeEncoders[r]||K[r];if(typeof i=="function"){let s=i(t,r,e,n);if(s!=null)return s}let o=K[r];if(!o)throw new Error(`${Tt} unsupported type: ${r}`);return o(t,r,e,n)}function Hi(t,e){e.mapSorter&&t.sort(e.mapSorter)}function Di(t,e){if(t[0]instanceof m&&e[0]instanceof m){let n=t[0],r=e[0];return n._keyBytes||(n._keyBytes=mr(n.value)),r._keyBytes||(r._keyBytes=mr(r.value)),Ve(n._keyBytes,r._keyBytes)}throw new Error("rfc8949MapSorter: complex key types are not supported yet")}function mr(t){return Li(t,Ri,hr)}function yr(t,e,n,r){if(Array.isArray(e))for(let i of e)yr(t,i,n,r);else n[e.type.major](t,e,r)}function Li(t,e,n){let r=Xe(t,n);if(!Array.isArray(r)&&n.quickEncodeToken){let i=n.quickEncodeToken(r);if(i)return i;let o=e[r.type.major];if(o.encodedSize){let s=o.encodedSize(r,n),a=new be(s);if(o(a,r,n),a.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${r} was wrong`);return kt(a.chunks[0])}}return It.reset(),yr(It,r,e,n),It.toBytes(!0)}wt.hmacSha256Sync=(t,...e)=>Et(St,t,wt.concatBytes(...e));var Gi=32;function Qe(t){if(t.length===0)return new Uint8Array(Gi);let e=t.length,n=e*4,r=t.reduce((c,u)=>c+u.length,0),i=4+n+r,o=new Uint8Array(i),s=new DataView(o.buffer,o.byteOffset,o.byteLength);s.setUint32(0,e,!0);let a=4;for(let c of t)s.setUint32(a,c.length,!0),a+=4;for(let c of t)o.set(c,a),a+=c.length;return St(o)}var Bt={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,entityRef:4,f32:4,vec2:8,vec3:12,quat:16,enum:1},Ee={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,f32:4,flags8:1,flags16:2,flags32:4,vec2:8,vec3:12,quat:16};var gr=new Set(["bool","uint8","int8","uint16","int16","uint32","int32","f32"]);function $t(t){let e=t.match(/^(\\w+)\\[(\\d+)\\]$/);if(!e||!e[1]||!e[2])return null;let n=e[1],r=parseInt(e[2],10);if(!gr.has(n))throw new Error(`Invalid array element type \'${n}\': arrays only support primitive scalar types (bool, uint8, int8, uint16, int16, uint32, int32, f32)`);return{elementType:n,length:r}}function br(t){let e=$t(t);if(e){let r=Ee[e.elementType];if(r===void 0)throw new Error(`Unknown element type: ${e.elementType}`);return r*e.length}let n=Ee[t];if(n===void 0)throw new Error(`Unknown type: ${t}`);return n}function Zi(t){let e=[],n=[],r=new Map,i=new Map,o=0;for(let s of t.controls){let a=br(s.type),c=$t(s.type),u={name:s.name,type:s.type,size:a,offset:0,options:s.options};c&&(u.arrayLength=c.length,u.arrayElementType=c.elementType);let f={name:s.name,index:o++,field:u,hint:s.hint,retain:s.retain==="always"?"always":"tick"};e.push(f),r.set(s.name,f)}for(let s of t.commands){let a=[],c=0;for(let f of s.args){let d=br(f.type),g=$t(f.type),S={name:f.name,type:f.type,size:d,offset:c};g&&(S.arrayLength=g.length,S.arrayElementType=g.elementType),a.push(S),c+=d}let u={name:s.name,index:o++,args:a,totalSize:c};n.push(u),i.set(s.name,u)}return{controls:e,commands:n,controlByName:r,commandByName:i}}function Pt(t,e,n,r){switch(n){case"bool":t.setUint8(e,r?1:0);break;case"uint8":case"flags8":t.setUint8(e,r);break;case"int8":t.setInt8(e,r);break;case"uint16":case"flags16":t.setUint16(e,r,!0);break;case"int16":t.setInt16(e,r,!0);break;case"uint32":case"flags32":t.setUint32(e,r,!0);break;case"int32":t.setInt32(e,r,!0);break;case"f32":t.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function _t(t,e,n){switch(n){case"bool":return t.getUint8(e)!==0;case"uint8":case"flags8":return t.getUint8(e);case"int8":return t.getInt8(e);case"uint16":case"flags16":return t.getUint16(e,!0);case"int16":return t.getInt16(e,!0);case"uint32":case"flags32":return t.getUint32(e,!0);case"int32":return t.getInt32(e,!0);case"f32":return t.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}var et=class t{constructor(e){y(this,"schema");y(this,"fields",new Map);y(this,"commandList",[]);this.schema=e}setControl(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);let i=new Uint8Array(r.field.size),o=new DataView(i.buffer);this.encodeValue(o,0,r.field,n),this.fields.set(r.index,i)}setFlags(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(!r.field.options)throw new Error(`Control ${e} is not a flags type`);let i=0;for(let a=0;a<r.field.options.length;a++){let c=r.field.options[a];n[c]&&(i|=1<<a)}let o=new Uint8Array(r.field.size),s=new DataView(o.buffer);Pt(s,0,r.field.type,i),this.fields.set(r.index,o)}setVec2(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let i=new Uint8Array(8),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),this.fields.set(r.index,i)}setVec3(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let i=new Uint8Array(12),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),this.fields.set(r.index,i)}setQuat(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let i=new Uint8Array(16),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),o.setFloat32(12,n[3],!0),this.fields.set(r.index,i)}getControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);let r=this.fields.get(n.index);if(!r)return this.getDefaultValue(n.field);let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return this.decodeValue(i,0,n.field)}getFlags(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(!n.field.options)throw new Error(`Control ${e} is not a flags type`);let r=this.fields.get(n.index),i=0;if(r){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);i=_t(s,0,n.field.type)}let o={};for(let s=0;s<n.field.options.length;s++)o[n.field.options[s]]=(i&1<<s)!==0;return o}getVec2(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let r=this.fields.get(n.index);if(!r)return[0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0)]}getVec3(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let r=this.fields.get(n.index);if(!r)return[0,0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0)]}getQuat(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let r=this.fields.get(n.index);if(!r)return[0,0,0,1];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0),i.getFloat32(12,!0)]}hasControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);return this.fields.has(n.index)}addCommand(e,n){let r=this.schema.commandByName.get(e);if(!r)throw new Error(`Unknown command: ${e}`);let i=new Uint8Array(r.totalSize),o=new DataView(i.buffer);for(let s of r.args){let a=n[s.name];if(a===void 0)throw new Error(`Missing required argument: ${s.name}`);this.encodeValue(o,s.offset,s,a)}this.commandList.push({index:r.index,data:i})}getCommands(){let e=[];for(let{index:n,data:r}of this.commandList){let i=this.schema.commands.find(a=>a.index===n);if(!i)continue;let o=new DataView(r.buffer,r.byteOffset,r.byteLength),s={name:i.name};for(let a of i.args)s[a.name]=this.decodeValue(o,a.offset,a);e.push(s)}return e}encode(){let e=[];for(let[o,s]of this.fields){let a=new Uint8Array(2+s.length);a[0]=o,a[1]=s.length,a.set(s,2),e.push(a)}for(let o of this.commandList){let s=new Uint8Array(2+o.data.length);s[0]=o.index,s[1]=o.data.length,s.set(o.data,2),e.push(s)}let n=e.reduce((o,s)=>o+s.length,0),r=new Uint8Array(n),i=0;for(let o of e)r.set(o,i),i+=o.length;return r}static decode(e,n){let r=new t(e),i=0;for(;i<n.length;){if(i+2>n.length)throw new Error("Truncated TLV field header");let o=n[i],s=n[i+1];if(i+2+s>n.length)throw new Error(`Truncated TLV field data at index ${o}`);let a=n.subarray(i+2,i+2+s);e.controls.find(u=>u.index===o)?r.fields.set(o,new Uint8Array(a)):e.commands.find(f=>f.index===o)&&r.commandList.push({index:o,data:new Uint8Array(a)}),i+=2+s}return r}clear(){this.fields.clear(),this.commandList.length=0}encodeValue(e,n,r,i){if(r.type==="vec2"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0)}else if(r.type==="vec3"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0)}else if(r.type==="quat"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0),e.setFloat32(n+12,o[3],!0)}else if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let o=i,s=Ee[r.arrayElementType];for(let a=0;a<Math.min(o.length,r.arrayLength);a++)Pt(e,n+a*s,r.arrayElementType,o[a])}else Pt(e,n,r.type,i)}decodeValue(e,n,r){if(r.type==="vec2")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0)];if(r.type==="vec3")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)];if(r.type==="quat")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0),e.getFloat32(n+12,!0)];if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let i=[],o=Ee[r.arrayElementType];for(let s=0;s<r.arrayLength;s++)i.push(_t(e,n+s*o,r.arrayElementType));return i}else return _t(e,n,r.type)}getDefaultValue(e){return e.type==="vec2"?[0,0]:e.type==="vec3"?[0,0,0]:e.type==="quat"?[0,0,0,1]:e.arrayLength!==void 0?new Array(e.arrayLength).fill(0):e.type==="bool"?!1:0}};function xr(t){let e=Zi(t);return{create(){return new et(e)},decode(n){return et.decode(e,n)},get schema(){return e}}}function wr(t,e){return((e&65535)<<16|t&65535)>>>0}function ke(t){return t&65535}function ji(t){return t>>>16&65535}function W(t){return t.subarray(8,40)}function Ot(t,e){if(e.length!==32)throw new Error(`stateId must be ${32} bytes, got ${e.length}`);t.set(e,8)}function Sr(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint32(40,e,!0)}function rt(t,e,n){let r=new Uint8Array(68);return r.set(t,0),new DataView(r.buffer).setUint32(32,e,!0),r.set(n,36),ge(r)}function it(t,e,n){return rt(t,e,Qe(n))}function Ft(t){return ge(t)}function Er(t,e,n){let r=new ArrayBuffer(t.totalSize),i=new Uint8Array(r),o=new DataView(r);if(o.setUint32(0,1297367376,!0),o.setUint16(4,0,!0),i[6]=n??0,i[7]=0,e){let a=Ft(e);i.set(a,8)}let s=t.freeStackOffset;o.setUint16(s,t.maxEntities,!0);for(let a=0;a<t.maxEntities;a++){let c=t.freeStackOffset+2+a*2;o.setUint16(c,a,!0)}return i}function kr(t,e){return t.entityTableOffset+e*t.entityRecordSize}function Nt(t,e,n){let r=kr(t,n);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(r,!0)}function Xi(t){return new DataView(t.buffer,t.byteOffset,t.byteLength).getUint16(4,!0)}function Ji(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint16(4,e,!0)}function Qi(t,e){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t.freeStackOffset,!0)}function eo(t,e,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t.freeStackOffset,n,!0)}function to(t,e){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Qi(t,e);if(r===0)throw new Error(`No free entity slots available (max: ${t.maxEntities})`);let i=t.freeStackOffset+2+(r-1)*2,o=n.getUint16(i,!0);return eo(t,e,r-1),o}function Tr(t,e){let n=to(t,e),r=Nt(t,e,n);return Ji(e,Xi(e)+1),wr(n,r)}function ot(t,e,n){if(n===4294967295)return!1;let r=ke(n);if(r>=t.maxEntities)return!1;let i=ji(n);return Nt(t,e,r)===i}function Mt(t,e,n){if(n<0)throw new Error("Singleton components do not have component bitmask entries");let r=kr(t,e)+2,i=Math.floor(n/8),o=n%8;return{byteOffset:r+i,bitIndex:o}}function Ar(t,e,n,r){if(!ot(t,e,n))return!1;let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and is always present`);let o=ke(n),{byteOffset:s,bitIndex:a}=Mt(t,o,i.index);return(e[s]&1<<a)!==0}function vr(t,e,n,r){if(!ot(t,e,n)){let c=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${c} is not alive`)}let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and cannot be added to entities`);let o=ke(n),{byteOffset:s,bitIndex:a}=Mt(t,o,i.index);e[s]=(e[s]??0)|1<<a}function Ur(t,e,n,r){let i=ke(e);return n.storageOffset+i*n.size+r.offset}function no(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":return(t[e]??0)!==0;case"uint8":return t[e]??0;case"int8":return r.getInt8(e);case"uint16":return r.getUint16(e,!0);case"int16":return r.getInt16(e,!0);case"uint32":return r.getUint32(e,!0);case"int32":return r.getInt32(e,!0);case"entityRef":return r.getUint32(e,!0);case"f32":return r.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}function Cr(t,e,n,r){let i=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":t[e]=r?1:0;break;case"uint8":t[e]=r&255;break;case"int8":i.setInt8(e,r);break;case"uint16":i.setUint16(e,r,!0);break;case"int16":i.setInt16(e,r,!0);break;case"uint32":i.setUint32(e,r,!0);break;case"int32":i.setInt32(e,r,!0);break;case"entityRef":i.setUint32(e,r,!0);break;case"f32":i.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function ro(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0)}function io(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0)}function oo(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0),r.setFloat32(e+12,n[3],!0)}function Ir(t,e,n,r,i,o){if(!ot(t,e,n))return;let s=t.componentByName.get(r);if(!s)throw new Error(`Unknown component: \'${r}\'`);if(s.isSingleton)throw new Error(`Component \'${r}\' is a singleton; use singleton accessors instead`);if(!nt(t,e,n,s))return;let a=s.fields.find(f=>f.name===i);if(!a)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let c=Ur(t,n,s,a),u=a.type;if(a.arrayLength!==void 0&&a.arrayElementType!==void 0){if(o===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(o<0||o>=a.arrayLength)throw new Error(`Array index ${o} out of bounds for ${r}.${i} (length: ${a.arrayLength})`);let f=Bt[a.arrayElementType];if(f===void 0)throw new Error(`Unknown array element type: ${a.arrayElementType}`);c+=o*f,u=a.arrayElementType}return no(e,c,u)}function w(t,e,n,r,i,o,s){if(!ot(t,e,n)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} is not alive`)}let a=t.componentByName.get(r);if(!a)throw new Error(`Unknown component: \'${r}\'`);if(!nt(t,e,n,a)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} does not have component \'${r}\'`)}let c=a.fields.find(d=>d.name===i);if(!c)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let u=Ur(t,n,a,c),f=c.type;if(c.arrayLength!==void 0&&c.arrayElementType!==void 0){if(s===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(s<0||s>=c.arrayLength)throw new Error(`Array index ${s} out of bounds for ${r}.${i} (length: ${c.arrayLength})`);let d=Bt[c.arrayElementType];if(d===void 0)throw new Error(`Unknown array element type: ${c.arrayElementType}`);u+=s*d,f=c.arrayElementType}Cr(e,u,f,o)}function nt(t,e,n,r){if(r.isSingleton)throw new Error(`Component \'${r.name}\' is a singleton and cannot be queried per entity`);let i=ke(n),{byteOffset:o,bitIndex:s}=Mt(t,i,r.index);return(e[o]&1<<s)!==0}function Br(t,e,n,r){if(n.length===0)throw new Error("Query must include at least one component");let i=[];for(let a of n){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);i.push(c)}let o=[];if(r)for(let a of r){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);o.push(c)}function*s(){for(let a=0;a<t.maxEntities;a++){let c=Nt(t,e,a),u=wr(a,c),f=!0;for(let d of i)if(!nt(t,e,u,d)){f=!1;break}if(f){for(let d of o)if(nt(t,e,u,d)){f=!1;break}f&&(yield u)}}}return s()}function Pr(t,e){if(!t.events||!t.eventByName)throw new Error("Schema has no events");let n=t.eventByName.get(e);if(!n)throw new Error(`Unknown event: \'${e}\'`);return n}function _r(t,e,n){let r=Pr(t,n);new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(r.storageOffset,0,!0)}function $r(t,e,n,r){let i=Pr(t,n),o=new DataView(e.buffer,e.byteOffset,e.byteLength),s=o.getUint16(i.storageOffset,!0);if(s>=i.maxEvents)return!1;let a=i.storageOffset+2+s*i.recordSize;for(let c of i.fields){let u=r[c.name];if(u===void 0)throw new Error(`Missing required field \'${c.name}\' for event \'${n}\'`);let f=a+c.offset;c.type==="vec2"?ro(e,f,u):c.type==="vec3"?io(e,f,u):c.type==="quat"?oo(e,f,u):Cr(e,f,c.type,u)}return o.setUint16(i.storageOffset,s+1,!0),!0}function st(t){if(t.length!==32)throw new Error(`stateId must be ${32} bytes, got ${t.length}`);return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}var at=class{constructor(e={}){y(this,"cache");y(this,"maxSize");this.cache=new Map,this.maxSize=e.maxSize??100}store(e){let n=W(e),r=st(n);if(this.cache.size>=this.maxSize&&!this.cache.has(r)){let i=this.cache.keys().next().value;i!==void 0&&this.cache.delete(i)}this.cache.set(r,e.slice())}getByStateId(e){let n=st(e),r=this.cache.get(n);return r?r.slice():void 0}getCached(e,n,r){let i=W(e),o=rt(i,n,r);return this.getByStateId(o)}has(e){let n=st(e);return this.cache.has(n)}hasCached(e,n,r){let i=W(e),o=rt(i,n,r);return this.has(o)}delete(e){let n=st(e);return this.cache.delete(n)}clear(){this.cache.clear()}get size(){return this.cache.size}keys(){return this.cache.keys()}};var Te=65536,ct=class{constructor(e){y(this,"memory",null);y(this,"plugins",[]);y(this,"options");y(this,"stateSize",null);y(this,"statePtr",null);y(this,"heapPos",0);y(this,"arenaResetMark",0);y(this,"inputCodec",null);y(this,"playerEntities",new Map);y(this,"hostAlloc",(e,n)=>{let r=this.memory;if(!r)throw new Error("Memory not initialized for host_alloc");let i=this.heapPos+n-1&~(n-1),o=i+e,s=r.buffer.byteLength;if(o>s){let c=Math.ceil((o-s)/Te);if(this.options.debug){let u=(s+c*Te)/1048576;console.warn(`[mt] WASM memory grew to ${u.toFixed(1)}MB`)}r.grow(c)}return new Uint8Array(r.buffer).fill(0,i,o),this.heapPos=o,i});this.options=e}markArenaReset(){this.arenaResetMark=this.heapPos}resetArena(){this.heapPos=this.arenaResetMark}async init(){if(this.stateSize=this.options.stateSchema.totalSize,!this.stateSize)throw new Error("State schema total size is required");this.options.inputSchema&&(this.inputCodec=xr(this.options.inputSchema));let e=4*1024*1024,n=this.options.plugins??(this.options.moduleBytes?[{name:"main",wasmBytes:this.options.moduleBytes,reservedBytes:e}]:[]);for(let c of n)if(typeof c.reservedBytes!="number"||c.reservedBytes<=0)throw new Error(`Plugin "${c.name}" requires reservedBytes > 0`);let r=n.reduce((c,u)=>{let f=Math.ceil(u.reservedBytes/Te)*Te;return c+f},0),i=1024*1024*5,o=this.stateSize+i+r,s=Math.ceil(o/Te);this.memory=new WebAssembly.Memory({initial:s}),this.heapPos=r;let a={env:{memory:this.memory,host_alloc:this.hostAlloc,abort:()=>{throw new Error("WASM abort called")}}};for(let c of n){let u=c.wasmBytes.slice().buffer,f=await WebAssembly.compile(u),d=await WebAssembly.instantiate(f,a);if(typeof d.exports.apply!="function")throw new Error(`Plugin "${c.name}" missing required apply() export`);this.plugins.push({name:c.name,instance:d,exports:d.exports})}return this.statePtr=this.hostAlloc(this.stateSize,8),this.markArenaReset(),this}findPlayerEntity(e,n){let r=this.options.stateSchema,i=this.playerEntities.get(n);if(i!==void 0){if(Ar(r,e,i,"Player"))return i;this.playerEntities.delete(n)}for(let o of Br(r,e,["Player"]))if(Ir(r,e,o,"Player","index")===n)return this.playerEntities.set(n,o),o}spawnPlayerEntity(e,n){let r=this.options.stateSchema,i=Tr(r,e);return vr(r,e,i,"Player"),w(r,e,i,"Player","index",n),this.playerEntities.set(n,i),i}findOrSpawnPlayerEntity(e,n){let r=this.findPlayerEntity(e,n);return r!==void 0?r:this.spawnPlayerEntity(e,n)}resetPlayerControls(e,n){let r=this.options.stateSchema;if(this.inputCodec)for(let i of this.inputCodec.schema.controls){if(i.retain==="always")continue;let o=i.field.type,s=i.name;if(o==="vec2")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0);else if(o==="vec3")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0);else if(o==="quat")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0),w(r,e,n,"Player",`${s}_w`,1);else if(o.startsWith("flags"))w(r,e,n,"Player",s,0);else if(o.includes("[")){let a=o.match(/\\[(\\d+)\\]/);if(a?.[1]){let c=parseInt(a[1],10);for(let u=0;u<c;u++)w(r,e,n,"Player",s,0,u)}}else w(r,e,n,"Player",s,o==="bool"?!1:0)}}writeControlsToPlayer(e,n,r){let i=this.options.stateSchema,o=this.options.inputSchema;if(o)for(let s of o.controls){if(!r.hasControl(s.name))continue;let a=s.type,c=s.name;if(a==="vec2"){let[u,f]=r.getVec2(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f)}else if(a==="vec3"){let[u,f,d]=r.getVec3(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d)}else if(a==="quat"){let[u,f,d,g]=r.getQuat(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d),w(i,e,n,"Player",`${c}_w`,g)}else if(a.startsWith("flags")){let u=r.getControl(c);w(i,e,n,"Player",c,u)}else if(a.includes("[")){let u=r.getControl(c);for(let f=0;f<u.length;f++)w(i,e,n,"Player",c,u[f],f)}else{let u=r.getControl(c);w(i,e,n,"Player",c,u)}}}pushCommands(e,n,r){let i=this.options.stateSchema,o=r.getCommands();for(let s of o){let a=s.name.charAt(0).toUpperCase()+s.name.slice(1)+"Command",c={player_index:n};for(let[u,f]of Object.entries(s))u!=="name"&&(Array.isArray(f)?f.length===2?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1]):f.length===3?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2]):f.length===4&&(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2],c[`${u}_w`]=f[3]):c[u]=f);$r(i,e,a,c)}}clearCommandEvents(e){let n=this.options.stateSchema,r=this.options.inputSchema;if(!(!r||!n.events))for(let i of r.commands){let o=i.name.charAt(0).toUpperCase()+i.name.slice(1)+"Command";n.eventByName?.has(o)&&_r(n,e,o)}}decodePayloadsToState(e,n){if(this.inputCodec)for(let r=0;r<n.length;r++){let i=n[r];if(i===void 0)continue;let o=this.findOrSpawnPlayerEntity(e,r);if(this.resetPlayerControls(e,o),i.length===0)continue;let s=this.inputCodec.decode(i);this.writeControlsToPlayer(e,o,s),this.pushCommands(e,r,s)}}transition(e,n){let r=this.stateSize;if(r===null||e.length!==r)throw new Error(`State size mismatch: expected ${this.stateSize}, got ${e.length}`);if(this.plugins.length===0||n.length===0)return e.slice();let i=this.memory;if(!i)throw new Error("WASM memory not initialized");let o=this.statePtr;if(o===null||o<0)throw new Error("State pointer not initialized");let s=new Uint8Array(i.buffer);s.set(e,o);let a=this.hostAlloc(32,8),c=W(e);for(let u of n){let f=it(c,u.tick,u.payloads);s=new Uint8Array(i.buffer),s.set(f,a);let d=new Uint8Array(i.buffer,o,r);Sr(d,u.tick),this.clearCommandEvents(d),this.decodePayloadsToState(d,u.payloads);let g=u.sync?1:0;for(let S of this.plugins)S.exports.apply(o,a,g),s=new Uint8Array(i.buffer);c=f}return s.slice(o,o+r)}close(){}getStateSize(){let e=this.stateSize;if(e===null||e<0)throw new Error("State size not initialized");return e}};var Y=class{constructor(e=1e3,n=1024){y(this,"buffer");y(this,"windowMs");y(this,"head",0);y(this,"tail",0);y(this,"size",0);this.windowMs=e,this.buffer=new Float64Array(n)}inc(){let e=performance.now();this.buffer[this.head]=e,this.head=(this.head+1)%this.buffer.length,this.size<this.buffer.length?this.size++:this.tail=(this.tail+1)%this.buffer.length}count(e=performance.now()){let n=e-this.windowMs;for(;this.size>0&&!(this.buffer[this.tail]>=n);)this.tail=(this.tail+1)%this.buffer.length,this.size--;return this.size}rate(e=performance.now()){return this.count(e)*(1e3/this.windowMs)}};function so(t){return("moduleBytes"in t||"plugins"in t)&&"stateSchema"in t}function ao(t,e){for(let n=t.length-1;n>=0;n--)if(e(t[n]))return n;return-1}var ut=class{constructor(e){y(this,"debug");y(this,"stateSchema");y(this,"inputs",null);y(this,"cache");y(this,"executor",null);y(this,"genesisStateId");y(this,"options");y(this,"ticking",!1);y(this,"tickGraceMs");y(this,"tickLag");y(this,"maxBatchSize");y(this,"currentState");y(this,"currentTick");y(this,"currentNodeId");y(this,"prevOnStateUpdateState",null);y(this,"onStateUpdate");y(this,"syncCheckpoint");y(this,"stateHistory",[]);y(this,"maxHistory",64);y(this,"stats");y(this,"loop",()=>{this._loop().catch(co)});y(this,"_loop",async()=>{if(this.ticking)try{let e=await this.tick();if(this.onStateUpdate&&this.currentState!==this.prevOnStateUpdateState&&(this.onStateUpdate(this.currentState),this.prevOnStateUpdateState=this.currentState,this.stats.updates.inc()),e.rolledBack&&this.stats.rollbacks.inc(),e.ticksComputed>0)for(let n=0;n<e.ticksComputed;n++)this.stats.executions.inc()}catch(e){console.error("Error in tick loop:",e)}finally{setTimeout(this.loop,this.tickGraceMs)}});e.log&&(this.inputs=e.log),this.debug=e.debug??!1,this.options=e,this.stateSchema=e.stateSchema,this.cache=e.cache??new at,this.genesisStateId=Ft(e.genesisHash),this.tickGraceMs=e.tickGraceMs??10,this.tickLag=e.tickLag??1,this.maxBatchSize=e.maxBatchSize??200,this.stats={rollbacks:new Y,executions:new Y,updates:new Y,cacheHits:new Y,cacheMisses:new Y},this.currentState=Er(this.stateSchema,e.genesisHash,e.tickRate),this.currentNodeId=null,this.syncCheckpoint=null,this.currentTick=null,this.cache.store(this.currentState)}setLog(e){this.inputs=dt(e)}setOnStateUpdate(e){e===null&&(this.prevOnStateUpdateState=null),this.onStateUpdate=e}async init(){if(!this.inputs)throw new Error("Rollback.init() called before log was configured. Call setLog() first or pass log in options.");let e=this.options.executor;if(so(e)){let r=new ct(e);await r.init(),this.executor=r}else this.executor=e;if((await this.tick(0)).ticksComputed===0||this.currentTick===null)throw new Error("Failed to record genesis snapshot");return this.options.disableTicking||(this.ticking=!0,this.loop()),this.getState()}async tick(e){if(!this.inputs)throw new Error("Rollback.tick() called before log was configured. Call setLog() and init() first.");let n=await this.inputs.getTicksAfter(this.currentNodeId,{limit:this.maxBatchSize,lag:this.tickLag});if(e&&(n=n.filter(a=>a.tick<=e)),n.length===0)return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:!1,ticksComputed:0};let r=!1,i=null;this.currentTick!==null&&n[0].tick<this.currentTick&&(r=!0,i=this.currentTick,this.rollbackTo(n[0]));let o=n.filter(a=>this.currentTick===null||a.tick>this.currentTick);if(o.length===0)return this.currentNodeId=n[n.length-1].id,{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId,rolledBack:r,ticksComputed:0};let s=this.processTicks(o,r,i);return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:r,ticksComputed:s}}async getStats(){return{rollbacks:this.stats.rollbacks.rate(),executions:this.stats.executions.rate(),updates:this.stats.updates.rate(),cacheHits:this.stats.cacheHits.rate(),cacheMisses:this.stats.cacheMisses.rate()}}processTicks(e,n=!1,r=null){if(!this.executor)throw new Error("Executor not initialized");let i=0,o=0;for(let u=0;u<e.length;u++){let f=e[u],d=Qe(f.payloads),g=this.cache.getCached(this.currentState,f.tick,d);if(g)this.currentState=g,this.currentTick=f.tick,this.currentNodeId=f.id,o=u+1,f.sync&&this.updateSyncCheckpoint(f);else break}if(n&&r!==null&&o>0)for(let u=0;u<o;u++)e[u].tick<=r&&this.stats.cacheHits.inc();let s=e.slice(o);if(s.length===0)return 0;let a=ao(s,u=>u.sync);if(a>=0){let u=s.slice(0,a+1),f=this.executor.transition(this.currentState,u),d=this.computeBatchStateId(this.currentState,u);Ot(f,d);let g=u[u.length-1];this.currentState=f,this.currentTick=g.tick,this.currentNodeId=g.id,i+=u.length,this.cache.store(f),this.recordStateSnapshot(this.currentTick,this.currentNodeId,d),this.updateSyncCheckpoint(g)}let c=a>=0?s.slice(a+1):s;if(c.length>0){let u=this.executor.transition(this.currentState,c),f=this.computeBatchStateId(this.currentState,c);Ot(u,f);let d=c[c.length-1];this.currentState=u,this.currentTick=d.tick,this.currentNodeId=d.id,i+=c.length,this.cache.store(u),this.recordStateSnapshot(this.currentTick,this.currentNodeId,f)}if(n&&r!==null)for(let u of s)u.tick<=r&&this.stats.cacheMisses.inc();return i}computeBatchStateId(e,n){let r=W(e);for(let i of n)r=it(r,i.tick,i.payloads);return r}getState(){return this.currentState.slice()}getTick(){return this.currentTick}reset(){this.currentTick=0,this.currentNodeId=null,this.syncCheckpoint=null}close(){this.ticking=!1,this.setOnStateUpdate(null),this.executor&&(this.executor.close(),this.executor=null)}rollbackTo(e){if(this.syncCheckpoint&&this.syncCheckpoint.tick>=e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}if(this.syncCheckpoint&&this.syncCheckpoint.tick<e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}let n=e.tick-1,r=this.findSnapshotAtOrBefore(n);if(r){let o=this.cache.getByStateId(r.stateId);if(o){this.currentState=o,this.currentTick=r.tick,this.currentNodeId=r.nodeId;return}}let i=this.cache.getByStateId(this.genesisStateId);if(i){this.currentState=i,this.currentTick=0,this.currentNodeId=null;return}}updateSyncCheckpoint(e){this.syncCheckpoint={state:this.currentState.slice(),tick:e.tick,nodeId:e.id}}recordStateSnapshot(e,n,r){this.stateHistory.push({tick:e,nodeId:n,stateId:r.slice()}),this.stateHistory.length>this.maxHistory&&this.stateHistory.shift()}findSnapshotAtOrBefore(e){for(let n=this.stateHistory.length-1;n>=0;n--){let r=this.stateHistory[n];if(r.tick<=e)return r}return null}};function co(t){console.warn("rollback unexpected:",t)}Be(ut);var Ga=null;globalThis.onerror=t=>(console.error("\\u{1F534} FATAL ROLLBACK WORKER ERROR (Uncaught Exception):",t),!0);export{Ga as default};\n/*! Bundled license information:\n\ncomlink/dist/esm/comlink.mjs:\n (**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n *)\n\n@noble/secp256k1/index.js:\n (*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)\n\n@noble/hashes/esm/utils.js:\n (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)\n*/\n';
6022
+ var workerCode = 'var Or=Object.defineProperty;var Fr=(t,e,n)=>e in t?Or(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var y=(t,e,n)=>Fr(t,typeof e!="symbol"?e+"":e,n);var Rt=Symbol("Comlink.proxy"),Nr=Symbol("Comlink.endpoint"),Mr=Symbol("Comlink.releaseProxy"),ut=Symbol("Comlink.finalizer"),ve=Symbol("Comlink.thrown"),Ht=t=>typeof t=="object"&&t!==null||typeof t=="function",Rr={canHandle:t=>Ht(t)&&t[Rt],serialize(t){let{port1:e,port2:n}=new MessageChannel;return Be(t,e),[n,[n]]},deserialize(t){return t.start(),lt(t)}},Hr={canHandle:t=>Ht(t)&&ve in t,serialize({value:t}){let e;return t instanceof Error?e={isError:!0,value:{message:t.message,name:t.name,stack:t.stack}}:e={isError:!1,value:t},[e,[]]},deserialize(t){throw t.isError?Object.assign(new Error(t.value.message),t.value):t.value}},Dt=new Map([["proxy",Rr],["throw",Hr]]);function Dr(t,e){for(let n of t)if(e===n||n==="*"||n instanceof RegExp&&n.test(e))return!0;return!1}function Be(t,e=globalThis,n=["*"]){e.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Dr(n,i.origin)){console.warn(`Invalid origin \'${i.origin}\' for comlink proxy`);return}let{id:o,type:s,path:a}=Object.assign({path:[]},i.data),c=(i.data.argumentList||[]).map(Y),u;try{let f=a.slice(0,-1).reduce((g,S)=>g[S],t),d=a.reduce((g,S)=>g[S],t);switch(s){case"GET":u=d;break;case"SET":f[a.slice(-1)[0]]=Y(i.data.value),u=!0;break;case"APPLY":u=d.apply(f,c);break;case"CONSTRUCT":{let g=new d(...c);u=qr(g)}break;case"ENDPOINT":{let{port1:g,port2:S}=new MessageChannel;Be(t,S),u=Zr(g,[g])}break;case"RELEASE":u=void 0;break;default:return}}catch(f){u={value:f,[ve]:0}}Promise.resolve(u).catch(f=>({value:f,[ve]:0})).then(f=>{let[d,g]=Ie(f);e.postMessage(Object.assign(Object.assign({},d),{id:o}),g),s==="RELEASE"&&(e.removeEventListener("message",r),Lt(e),ut in t&&typeof t[ut]=="function"&&t[ut]())}).catch(f=>{let[d,g]=Ie({value:new TypeError("Unserializable return value"),[ve]:0});e.postMessage(Object.assign(Object.assign({},d),{id:o}),g)})}),e.start&&e.start()}function Lr(t){return t.constructor.name==="MessagePort"}function Lt(t){Lr(t)&&t.close()}function lt(t,e){let n=new Map;return t.addEventListener("message",function(i){let{data:o}=i;if(!o||!o.id)return;let s=n.get(o.id);if(s)try{s(o)}finally{n.delete(o.id)}}),ft(t,n,[],e)}function Ae(t){if(t)throw new Error("Proxy has been released and is not useable")}function zt(t){return ne(t,new Map,{type:"RELEASE"}).then(()=>{Lt(t)})}var Ue=new WeakMap,Ce="FinalizationRegistry"in globalThis&&new FinalizationRegistry(t=>{let e=(Ue.get(t)||0)-1;Ue.set(t,e),e===0&&zt(t)});function zr(t,e){let n=(Ue.get(e)||0)+1;Ue.set(e,n),Ce&&Ce.register(t,e,t)}function Vr(t){Ce&&Ce.unregister(t)}function ft(t,e,n=[],r=function(){}){let i=!1,o=new Proxy(r,{get(s,a){if(Ae(i),a===Mr)return()=>{Vr(o),zt(t),e.clear(),i=!0};if(a==="then"){if(n.length===0)return{then:()=>o};let c=ne(t,e,{type:"GET",path:n.map(u=>u.toString())}).then(Y);return c.then.bind(c)}return ft(t,e,[...n,a])},set(s,a,c){Ae(i);let[u,f]=Ie(c);return ne(t,e,{type:"SET",path:[...n,a].map(d=>d.toString()),value:u},f).then(Y)},apply(s,a,c){Ae(i);let u=n[n.length-1];if(u===Nr)return ne(t,e,{type:"ENDPOINT"}).then(Y);if(u==="bind")return ft(t,e,n.slice(0,-1));let[f,d]=Mt(c);return ne(t,e,{type:"APPLY",path:n.map(g=>g.toString()),argumentList:f},d).then(Y)},construct(s,a){Ae(i);let[c,u]=Mt(a);return ne(t,e,{type:"CONSTRUCT",path:n.map(f=>f.toString()),argumentList:c},u).then(Y)}});return zr(o,t),o}function Gr(t){return Array.prototype.concat.apply([],t)}function Mt(t){let e=t.map(Ie);return[e.map(n=>n[0]),Gr(e.map(n=>n[1]))]}var Vt=new WeakMap;function Zr(t,e){return Vt.set(t,e),t}function qr(t){return Object.assign(t,{[Rt]:!0})}function Ie(t){for(let[e,n]of Dt)if(n.canHandle(t)){let[r,i]=n.serialize(t);return[{type:"HANDLER",name:e,value:r},i]}return[{type:"RAW",value:t},Vt.get(t)||[]]}function Y(t){switch(t.type){case"HANDLER":return Dt.get(t.name).deserialize(t.value);case"RAW":return t.value}}function ne(t,e,n,r){return new Promise(i=>{let o=Kr();e.set(o,i),t.start&&t.start(),t.postMessage(Object.assign({id:o},n),r)})}function Kr(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var Wr={p:0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,n:0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,h:1n,a:0n,b:7n,Gx:0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,Gy:0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n},{p:X,n:ht,Gx:Yr,Gy:jr,b:jt}=Wr,re=32,dt=64,U=(t="")=>{throw new Error(t)},Xt=t=>typeof t=="bigint",Jt=t=>typeof t=="string",Xr=t=>t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array",me=(t,e)=>!Xr(t)||typeof e=="number"&&e>0&&t.length!==e?U("Uint8Array expected"):t,Ne=t=>new Uint8Array(t),Jr=t=>Uint8Array.from(t),Qt=(t,e)=>t.toString(16).padStart(e,"0"),yt=t=>Array.from(me(t)).map(e=>Qt(e,2)).join(""),z={_0:48,_9:57,A:65,F:70,a:97,f:102},Gt=t=>{if(t>=z._0&&t<=z._9)return t-z._0;if(t>=z.A&&t<=z.F)return t-(z.A-10);if(t>=z.a&&t<=z.f)return t-(z.a-10)},gt=t=>{let e="hex invalid";if(!Jt(t))return U(e);let n=t.length,r=n/2;if(n%2)return U(e);let i=Ne(r);for(let o=0,s=0;o<r;o++,s+=2){let a=Gt(t.charCodeAt(s)),c=Gt(t.charCodeAt(s+1));if(a===void 0||c===void 0)return U(e);i[o]=a*16+c}return i},bt=(t,e)=>me(Jt(t)?gt(t):Jr(me(t)),e),en=()=>globalThis?.crypto,Qr=()=>en()?.subtle??U("crypto.subtle must be defined"),_e=(...t)=>{let e=Ne(t.reduce((r,i)=>r+me(i).length,0)),n=0;return t.forEach(r=>{e.set(r,n),n+=r.length}),e},ei=(t=re)=>en().getRandomValues(Ne(t)),$e=BigInt,he=(t,e,n,r="bad number: out of range")=>Xt(t)&&e<=t&&t<n?t:U(r),h=(t,e=X)=>{let n=t%e;return n>=0n?n:e+n};var tn=(t,e)=>{(t===0n||e<=0n)&&U("no inverse n="+t+" mod="+e);let n=h(t,e),r=e,i=0n,o=1n,s=1n,a=0n;for(;n!==0n;){let c=r/n,u=r%n,f=i-s*c,d=o-a*c;r=n,n=u,i=s,o=a,s=f,a=d}return r===1n?h(i,e):U("no inverse")};var Zt=t=>t instanceof J?t:U("Point expected"),nn=t=>h(h(t*t)*t+jt),qt=t=>he(t,0n,X),Pe=t=>he(t,1n,X),ti=t=>he(t,1n,ht),pt=t=>(t&1n)===0n,rn=t=>Uint8Array.of(t),ni=t=>rn(pt(t)?2:3),ri=t=>{let e=nn(Pe(t)),n=1n;for(let r=e,i=(X+1n)/4n;i>0n;i>>=1n)i&1n&&(n=n*r%X),r=r*r%X;return h(n*n)===e?n:U("sqrt invalid")},R=class R{constructor(e,n,r){y(this,"px");y(this,"py");y(this,"pz");this.px=qt(e),this.py=Pe(n),this.pz=qt(r),Object.freeze(this)}static fromBytes(e){me(e);let n,r=e[0],i=e.subarray(1),o=Kt(i,0,re),s=e.length;if(s===re+1&&[2,3].includes(r)){let a=ri(o),c=pt(a);pt($e(r))!==c&&(a=h(-a)),n=new R(o,a,1n)}return s===dt+1&&r===4&&(n=new R(o,Kt(i,re,dt),1n)),n?n.assertValidity():U("bad point: not on curve")}equals(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=Zt(e),c=h(n*a),u=h(o*i),f=h(r*a),d=h(s*i);return c===u&&f===d}is0(){return this.equals(j)}negate(){return new R(this.px,h(-this.py),this.pz)}double(){return this.add(this)}add(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=Zt(e),c=0n,u=jt,f=0n,d=0n,g=0n,S=h(u*3n),k=h(n*o),A=h(r*s),O=h(i*a),te=h(n+r),v=h(o+s);te=h(te*v),v=h(k+A),te=h(te-v),v=h(n+i);let M=h(o+a);return v=h(v*M),M=h(k+O),v=h(v-M),M=h(r+i),f=h(s+a),M=h(M*f),f=h(A+O),M=h(M-f),g=h(c*v),f=h(S*O),g=h(f+g),f=h(A-g),g=h(A+g),d=h(f*g),A=h(k+k),A=h(A+k),O=h(c*O),v=h(S*v),A=h(A+O),O=h(k-O),O=h(c*O),v=h(v+O),k=h(A*v),d=h(d+k),k=h(M*v),f=h(te*f),f=h(f-k),k=h(te*A),g=h(M*g),g=h(g+k),new R(f,d,g)}multiply(e,n=!0){if(!n&&e===0n)return j;if(ti(e),e===1n)return this;if(this.equals(ie))return fi(e).p;let r=j,i=ie;for(let o=this;e>0n;o=o.double(),e>>=1n)e&1n?r=r.add(o):n&&(i=i.add(o));return r}toAffine(){let{px:e,py:n,pz:r}=this;if(this.equals(j))return{x:0n,y:0n};if(r===1n)return{x:e,y:n};let i=tn(r,X);return h(r*i)!==1n&&U("inverse invalid"),{x:h(e*i),y:h(n*i)}}assertValidity(){let{x:e,y:n}=this.toAffine();return Pe(e),Pe(n),h(n*n)===nn(e)?this:U("bad point: not on curve")}toBytes(e=!0){let{x:n,y:r}=this.assertValidity().toAffine(),i=Oe(n);return e?_e(ni(r),i):_e(rn(4),i,Oe(r))}static fromAffine(e){let{x:n,y:r}=e;return n===0n&&r===0n?j:new R(n,r,1n)}toHex(e){return yt(this.toBytes(e))}static fromPrivateKey(e){return ie.multiply(oi(e))}static fromHex(e){return R.fromBytes(bt(e))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}toRawBytes(e){return this.toBytes(e)}};y(R,"BASE"),y(R,"ZERO");var J=R,ie=new J(Yr,jr,1n),j=new J(0n,1n,0n);J.BASE=ie;J.ZERO=j;var Me=t=>$e("0x"+(yt(t)||"0")),Kt=(t,e,n)=>Me(t.subarray(e,n)),ii=2n**256n,Oe=t=>gt(Qt(he(t,0n,ii),dt)),oi=t=>{let e=Xt(t)?t:Me(bt(t,re));return he(e,1n,ht,"private key invalid 3")};var si=t=>{t=bt(t),(t.length<re+8||t.length>1024)&&U("expected 40-1024b");let e=h(Me(t),ht-1n);return Oe(e+1n)};var ai="SHA-256",xt={hexToBytes:gt,bytesToHex:yt,concatBytes:_e,bytesToNumberBE:Me,numberToBytesBE:Oe,mod:h,invert:tn,hmacSha256Async:async(t,...e)=>{let n=Qr(),r="HMAC",i=await n.importKey("raw",t,{name:r,hash:{name:ai}},!1,["sign"]);return Ne(await n.sign(r,i,_e(...e)))},hmacSha256Sync:void 0,hashToPrivateKey:si,randomBytes:ei};var Fe=8,ci=256,on=Math.ceil(ci/Fe)+1,mt=2**(Fe-1),ui=()=>{let t=[],e=ie,n=e;for(let r=0;r<on;r++){n=e,t.push(n);for(let i=1;i<mt;i++)n=n.add(e),t.push(n);e=n.double()}return t},Wt,Yt=(t,e)=>{let n=e.negate();return t?n:e},fi=t=>{let e=Wt||(Wt=ui()),n=j,r=ie,i=2**Fe,o=i,s=$e(i-1),a=$e(Fe);for(let c=0;c<on;c++){let u=Number(t&s);t>>=a,u>mt&&(u-=o,t+=1n);let f=c*mt,d=f,g=f+Math.abs(u)-1,S=c%2!==0,k=u<0;u===0?r=r.add(Yt(S,e[d])):n=n.add(Yt(k,e[g]))}return{p:n,f:r}};function di(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function sn(t){if(!Number.isSafeInteger(t)||t<0)throw new Error("positive integer expected, got "+t)}function se(t,...e){if(!di(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}function an(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash should be wrapped by utils.createHasher");sn(t.outputLen),sn(t.blockLen)}function ae(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function cn(t,e){se(t);let n=e.outputLen;if(t.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function Q(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Re(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function F(t,e){return t<<32-e|t>>>e}function pi(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}function ye(t){return typeof t=="string"&&(t=pi(t)),se(t),t}var oe=class{};function un(t){let e=r=>t().update(ye(r)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function mi(t,e,n,r){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,n,r);let i=BigInt(32),o=BigInt(4294967295),s=Number(n>>i&o),a=Number(n&o),c=r?4:0,u=r?0:4;t.setUint32(e+c,s,r),t.setUint32(e+u,a,r)}function fn(t,e,n){return t&e^~t&n}function ln(t,e,n){return t&e^t&n^e&n}var He=class extends oe{constructor(e,n,r,i){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=i,this.buffer=new Uint8Array(e),this.view=Re(this.buffer)}update(e){ae(this),e=ye(e),se(e);let{view:n,buffer:r,blockLen:i}=this,o=e.length;for(let s=0;s<o;){let a=Math.min(i-this.pos,o-s);if(a===i){let c=Re(e);for(;i<=o-s;s+=i)this.process(c,s);continue}r.set(e.subarray(s,s+a),this.pos),this.pos+=a,s+=a,this.pos===i&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){ae(this),cn(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:i,isLE:o}=this,{pos:s}=this;n[s++]=128,Q(this.buffer.subarray(s)),this.padOffset>i-s&&(this.process(r,0),s=0);for(let d=s;d<i;d++)n[d]=0;mi(r,i-8,BigInt(this.length*8),o),this.process(r,0);let a=Re(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let u=c/4,f=this.get();if(u>f.length)throw new Error("_sha2: outputLen bigger than state");for(let d=0;d<u;d++)a.setUint32(4*d,f[d],o)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:i,finished:o,destroyed:s,pos:a}=this;return e.destroyed=s,e.finished=o,e.length=i,e.pos=a,i%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},V=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var hi=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Z=new Uint32Array(64),De=class extends He{constructor(e=32){super(64,e,8,!1),this.A=V[0]|0,this.B=V[1]|0,this.C=V[2]|0,this.D=V[3]|0,this.E=V[4]|0,this.F=V[5]|0,this.G=V[6]|0,this.H=V[7]|0}get(){let{A:e,B:n,C:r,D:i,E:o,F:s,G:a,H:c}=this;return[e,n,r,i,o,s,a,c]}set(e,n,r,i,o,s,a,c){this.A=e|0,this.B=n|0,this.C=r|0,this.D=i|0,this.E=o|0,this.F=s|0,this.G=a|0,this.H=c|0}process(e,n){for(let d=0;d<16;d++,n+=4)Z[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let g=Z[d-15],S=Z[d-2],k=F(g,7)^F(g,18)^g>>>3,A=F(S,17)^F(S,19)^S>>>10;Z[d]=A+Z[d-7]+k+Z[d-16]|0}let{A:r,B:i,C:o,D:s,E:a,F:c,G:u,H:f}=this;for(let d=0;d<64;d++){let g=F(a,6)^F(a,11)^F(a,25),S=f+g+fn(a,c,u)+hi[d]+Z[d]|0,A=(F(r,2)^F(r,13)^F(r,22))+ln(r,i,o)|0;f=u,u=c,c=a,a=s+S|0,s=o,o=i,i=r,r=S+A|0}r=r+this.A|0,i=i+this.B|0,o=o+this.C|0,s=s+this.D|0,a=a+this.E|0,c=c+this.F|0,u=u+this.G|0,f=f+this.H|0,this.set(r,i,o,s,a,c,u,f)}roundClean(){Q(Z)}destroy(){this.set(0,0,0,0,0,0,0,0),Q(this.buffer)}};var ge=un(()=>new De);var wt=ge;var Le=class extends oe{constructor(e,n){super(),this.finished=!1,this.destroyed=!1,an(e);let r=ye(n);if(this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let i=this.blockLen,o=new Uint8Array(i);o.set(r.length>i?e.create().update(r).digest():r);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),Q(o)}update(e){return ae(this),this.iHash.update(e),this}digestInto(e){ae(this),se(e,this.outputLen),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:i,destroyed:o,blockLen:s,outputLen:a}=this;return e=e,e.finished=i,e.destroyed=o,e.blockLen=s,e.outputLen=a,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},St=(t,e,n)=>new Le(t,e).update(n).digest();St.create=(t,e)=>new Le(t,e);var yi=["string","number","bigint","symbol"],gi=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function dn(t){if(t===null)return"null";if(t===void 0)return"undefined";if(t===!0||t===!1)return"boolean";let e=typeof t;if(yi.includes(e))return e;if(e==="function")return"Function";if(Array.isArray(t))return"Array";if(bi(t))return"Buffer";let n=xi(t);return n||"Object"}function bi(t){return t&&t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer.call(null,t)}function xi(t){let e=Object.prototype.toString.call(t).slice(8,-1);if(gi.includes(e))return e}var l=class{constructor(e,n,r){this.major=e,this.majorEncoded=e<<5,this.name=n,this.terminal=r}toString(){return`Type[${this.major}].${this.name}`}compare(e){return this.major<e.major?-1:this.major>e.major?1:0}};l.uint=new l(0,"uint",!0);l.negint=new l(1,"negint",!0);l.bytes=new l(2,"bytes",!0);l.string=new l(3,"string",!0);l.array=new l(4,"array",!1);l.map=new l(5,"map",!1);l.tag=new l(6,"tag",!1);l.float=new l(7,"float",!0);l.false=new l(7,"false",!0);l.true=new l(7,"true",!0);l.null=new l(7,"null",!0);l.undefined=new l(7,"undefined",!0);l.break=new l(7,"break",!0);var m=class{constructor(e,n,r){this.type=e,this.value=n,this.encodedLength=r,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var ce=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",wi=new TextDecoder,Si=new TextEncoder;function ze(t){return ce&&globalThis.Buffer.isBuffer(t)}function Et(t){return t instanceof Uint8Array?ze(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):t:Uint8Array.from(t)}var yn=ce?(t,e,n)=>n-e>64?globalThis.Buffer.from(t.subarray(e,n)).toString("utf8"):mn(t,e,n):(t,e,n)=>n-e>64?wi.decode(t.subarray(e,n)):mn(t,e,n),gn=ce?t=>t.length>64?globalThis.Buffer.from(t):pn(t):t=>t.length>64?Si.encode(t):pn(t),H=t=>Uint8Array.from(t),ue=ce?(t,e,n)=>ze(t)?new Uint8Array(t.subarray(e,n)):t.slice(e,n):(t,e,n)=>t.slice(e,n),bn=ce?(t,e)=>(t=t.map(n=>n instanceof Uint8Array?n:globalThis.Buffer.from(n)),Et(globalThis.Buffer.concat(t,e))):(t,e)=>{let n=new Uint8Array(e),r=0;for(let i of t)r+i.length>n.length&&(i=i.subarray(0,n.length-r)),n.set(i,r),r+=i.length;return n},xn=ce?t=>globalThis.Buffer.allocUnsafe(t):t=>new Uint8Array(t);function Ve(t,e){if(ze(t)&&ze(e))return t.compare(e);for(let n=0;n<t.length;n++)if(t[n]!==e[n])return t[n]<e[n]?-1:1;return 0}function pn(t){let e=[],n=0;for(let r=0;r<t.length;r++){let i=t.charCodeAt(r);i<128?e[n++]=i:i<2048?(e[n++]=i>>6|192,e[n++]=i&63|128):(i&64512)===55296&&r+1<t.length&&(t.charCodeAt(r+1)&64512)===56320?(i=65536+((i&1023)<<10)+(t.charCodeAt(++r)&1023),e[n++]=i>>18|240,e[n++]=i>>12&63|128,e[n++]=i>>6&63|128,e[n++]=i&63|128):(e[n++]=i>>12|224,e[n++]=i>>6&63|128,e[n++]=i&63|128)}return e}function mn(t,e,n){let r=[];for(;e<n;){let i=t[e],o=null,s=i>239?4:i>223?3:i>191?2:1;if(e+s<=n){let a,c,u,f;switch(s){case 1:i<128&&(o=i);break;case 2:a=t[e+1],(a&192)===128&&(f=(i&31)<<6|a&63,f>127&&(o=f));break;case 3:a=t[e+1],c=t[e+2],(a&192)===128&&(c&192)===128&&(f=(i&15)<<12|(a&63)<<6|c&63,f>2047&&(f<55296||f>57343)&&(o=f));break;case 4:a=t[e+1],c=t[e+2],u=t[e+3],(a&192)===128&&(c&192)===128&&(u&192)===128&&(f=(i&15)<<18|(a&63)<<12|(c&63)<<6|u&63,f>65535&&f<1114112&&(o=f))}}o===null?(o=65533,s=1):o>65535&&(o-=65536,r.push(o>>>10&1023|55296),o=56320|o&1023),r.push(o),e+=s}return Ei(r)}var hn=4096;function Ei(t){let e=t.length;if(e<=hn)return String.fromCharCode.apply(String,t);let n="",r=0;for(;r<e;)n+=String.fromCharCode.apply(String,t.slice(r,r+=hn));return n}var ki=256,be=class{constructor(e=ki){this.chunkSize=e,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(e){let n=this.chunks[this.chunks.length-1];if(this.cursor+e.length<=this.maxCursor+1){let i=n.length-(this.maxCursor-this.cursor)-1;n.set(e,i)}else{if(n){let i=n.length-(this.maxCursor-this.cursor)-1;i<n.length&&(this.chunks[this.chunks.length-1]=n.subarray(0,i),this.maxCursor=this.cursor-1)}e.length<64&&e.length<this.chunkSize?(n=xn(this.chunkSize),this.chunks.push(n),this.maxCursor+=n.length,this._initReuseChunk===null&&(this._initReuseChunk=n),n.set(e,0)):(this.chunks.push(e),this.maxCursor+=e.length)}this.cursor+=e.length}toBytes(e=!1){let n;if(this.chunks.length===1){let r=this.chunks[0];e&&this.cursor>r.length/2?(n=this.cursor===r.length?r:r.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):n=ue(r,0,this.cursor)}else n=bn(this.chunks,this.cursor);return e&&this.reset(),n}};var b="CBOR decode error:",kt="CBOR encode error:",xe=[];xe[23]=1;xe[24]=2;xe[25]=3;xe[26]=5;xe[27]=9;function G(t,e,n){if(t.length-e<n)throw new Error(`${b} not enough data for type`)}var E=[24,256,65536,4294967296,BigInt("18446744073709551616")];function C(t,e,n){G(t,e,1);let r=t[e];if(n.strict===!0&&r<E[0])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function I(t,e,n){G(t,e,2);let r=t[e]<<8|t[e+1];if(n.strict===!0&&r<E[1])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function B(t,e,n){G(t,e,4);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3];if(n.strict===!0&&r<E[2])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function P(t,e,n){G(t,e,8);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3],i=t[e+4]*16777216+(t[e+5]<<16)+(t[e+6]<<8)+t[e+7],o=(BigInt(r)<<BigInt(32))+BigInt(i);if(n.strict===!0&&o<E[3])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);if(o<=Number.MAX_SAFE_INTEGER)return Number(o);if(n.allowBigInt===!0)return o;throw new Error(`${b} integers outside of the safe integer range are not supported`)}function wn(t,e,n,r){return new m(l.uint,C(t,e+1,r),2)}function Sn(t,e,n,r){return new m(l.uint,I(t,e+1,r),3)}function En(t,e,n,r){return new m(l.uint,B(t,e+1,r),5)}function kn(t,e,n,r){return new m(l.uint,P(t,e+1,r),9)}function _(t,e){return T(t,0,e.value)}function T(t,e,n){if(n<E[0]){let r=Number(n);t.push([e|r])}else if(n<E[1]){let r=Number(n);t.push([e|24,r])}else if(n<E[2]){let r=Number(n);t.push([e|25,r>>>8,r&255])}else if(n<E[3]){let r=Number(n);t.push([e|26,r>>>24&255,r>>>16&255,r>>>8&255,r&255])}else{let r=BigInt(n);if(r<E[4]){let i=[e|27,0,0,0,0,0,0,0],o=Number(r&BigInt(4294967295)),s=Number(r>>BigInt(32)&BigInt(4294967295));i[8]=o&255,o=o>>8,i[7]=o&255,o=o>>8,i[6]=o&255,o=o>>8,i[5]=o&255,i[4]=s&255,s=s>>8,i[3]=s&255,s=s>>8,i[2]=s&255,s=s>>8,i[1]=s&255,t.push(i)}else throw new Error(`${b} encountered BigInt larger than allowable range`)}}_.encodedSize=function(e){return T.encodedSize(e.value)};T.encodedSize=function(e){return e<E[0]?1:e<E[1]?2:e<E[2]?3:e<E[3]?5:9};_.compareTokens=function(e,n){return e.value<n.value?-1:e.value>n.value?1:0};function Tn(t,e,n,r){return new m(l.negint,-1-C(t,e+1,r),2)}function An(t,e,n,r){return new m(l.negint,-1-I(t,e+1,r),3)}function vn(t,e,n,r){return new m(l.negint,-1-B(t,e+1,r),5)}var Tt=BigInt(-1),Un=BigInt(1);function Cn(t,e,n,r){let i=P(t,e+1,r);if(typeof i!="bigint"){let o=-1-i;if(o>=Number.MIN_SAFE_INTEGER)return new m(l.negint,o,9)}if(r.allowBigInt!==!0)throw new Error(`${b} integers outside of the safe integer range are not supported`);return new m(l.negint,Tt-BigInt(i),9)}function Ge(t,e){let n=e.value,r=typeof n=="bigint"?n*Tt-Un:n*-1-1;T(t,e.type.majorEncoded,r)}Ge.encodedSize=function(e){let n=e.value,r=typeof n=="bigint"?n*Tt-Un:n*-1-1;return r<E[0]?1:r<E[1]?2:r<E[2]?3:r<E[3]?5:9};Ge.compareTokens=function(e,n){return e.value<n.value?1:e.value>n.value?-1:0};function we(t,e,n,r){G(t,e,n+r);let i=ue(t,e+n,e+n+r);return new m(l.bytes,i,n+r)}function In(t,e,n,r){return we(t,e,1,n)}function Bn(t,e,n,r){return we(t,e,2,C(t,e+1,r))}function Pn(t,e,n,r){return we(t,e,3,I(t,e+1,r))}function _n(t,e,n,r){return we(t,e,5,B(t,e+1,r))}function $n(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer bytes lengths not supported`);return we(t,e,9,i)}function Ze(t){return t.encodedBytes===void 0&&(t.encodedBytes=t.type===l.string?gn(t.value):t.value),t.encodedBytes}function fe(t,e){let n=Ze(e);T(t,e.type.majorEncoded,n.length),t.push(n)}fe.encodedSize=function(e){let n=Ze(e);return T.encodedSize(n.length)+n.length};fe.compareTokens=function(e,n){return Ai(Ze(e),Ze(n))};function Ai(t,e){return t.length<e.length?-1:t.length>e.length?1:Ve(t,e)}function Se(t,e,n,r,i){let o=n+r;G(t,e,o);let s=new m(l.string,yn(t,e+n,e+o),o);return i.retainStringBytes===!0&&(s.byteValue=ue(t,e+n,e+o)),s}function On(t,e,n,r){return Se(t,e,1,n,r)}function Fn(t,e,n,r){return Se(t,e,2,C(t,e+1,r),r)}function Nn(t,e,n,r){return Se(t,e,3,I(t,e+1,r),r)}function Mn(t,e,n,r){return Se(t,e,5,B(t,e+1,r),r)}function Rn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer string lengths not supported`);return Se(t,e,9,i,r)}var Hn=fe;function le(t,e,n,r){return new m(l.array,r,n)}function Dn(t,e,n,r){return le(t,e,1,n)}function Ln(t,e,n,r){return le(t,e,2,C(t,e+1,r))}function zn(t,e,n,r){return le(t,e,3,I(t,e+1,r))}function Vn(t,e,n,r){return le(t,e,5,B(t,e+1,r))}function Gn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer array lengths not supported`);return le(t,e,9,i)}function Zn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return le(t,e,1,1/0)}function qe(t,e){T(t,l.array.majorEncoded,e.value)}qe.compareTokens=_.compareTokens;qe.encodedSize=function(e){return T.encodedSize(e.value)};function de(t,e,n,r){return new m(l.map,r,n)}function qn(t,e,n,r){return de(t,e,1,n)}function Kn(t,e,n,r){return de(t,e,2,C(t,e+1,r))}function Wn(t,e,n,r){return de(t,e,3,I(t,e+1,r))}function Yn(t,e,n,r){return de(t,e,5,B(t,e+1,r))}function jn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer map lengths not supported`);return de(t,e,9,i)}function Xn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return de(t,e,1,1/0)}function Ke(t,e){T(t,l.map.majorEncoded,e.value)}Ke.compareTokens=_.compareTokens;Ke.encodedSize=function(e){return T.encodedSize(e.value)};function Jn(t,e,n,r){return new m(l.tag,n,1)}function Qn(t,e,n,r){return new m(l.tag,C(t,e+1,r),2)}function er(t,e,n,r){return new m(l.tag,I(t,e+1,r),3)}function tr(t,e,n,r){return new m(l.tag,B(t,e+1,r),5)}function nr(t,e,n,r){return new m(l.tag,P(t,e+1,r),9)}function We(t,e){T(t,l.tag.majorEncoded,e.value)}We.compareTokens=_.compareTokens;We.encodedSize=function(e){return T.encodedSize(e.value)};var Pi=20,_i=21,$i=22,Oi=23;function rr(t,e,n,r){if(r.allowUndefined===!1)throw new Error(`${b} undefined values are not supported`);return r.coerceUndefinedToNull===!0?new m(l.null,null,1):new m(l.undefined,void 0,1)}function ir(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return new m(l.break,void 0,1)}function At(t,e,n){if(n){if(n.allowNaN===!1&&Number.isNaN(t))throw new Error(`${b} NaN values are not supported`);if(n.allowInfinity===!1&&(t===1/0||t===-1/0))throw new Error(`${b} Infinity values are not supported`)}return new m(l.float,t,e)}function or(t,e,n,r){return At(vt(t,e+1),3,r)}function sr(t,e,n,r){return At(Ut(t,e+1),5,r)}function ar(t,e,n,r){return At(lr(t,e+1),9,r)}function Ye(t,e,n){let r=e.value;if(r===!1)t.push([l.float.majorEncoded|Pi]);else if(r===!0)t.push([l.float.majorEncoded|_i]);else if(r===null)t.push([l.float.majorEncoded|$i]);else if(r===void 0)t.push([l.float.majorEncoded|Oi]);else{let i,o=!1;(!n||n.float64!==!0)&&(ur(r),i=vt(N,1),r===i||Number.isNaN(r)?(N[0]=249,t.push(N.slice(0,3)),o=!0):(fr(r),i=Ut(N,1),r===i&&(N[0]=250,t.push(N.slice(0,5)),o=!0))),o||(Fi(r),i=lr(N,1),N[0]=251,t.push(N.slice(0,9)))}}Ye.encodedSize=function(e,n){let r=e.value;if(r===!1||r===!0||r===null||r===void 0)return 1;if(!n||n.float64!==!0){ur(r);let i=vt(N,1);if(r===i||Number.isNaN(r))return 3;if(fr(r),i=Ut(N,1),r===i)return 5}return 9};var cr=new ArrayBuffer(9),$=new DataView(cr,1),N=new Uint8Array(cr,0);function ur(t){if(t===1/0)$.setUint16(0,31744,!1);else if(t===-1/0)$.setUint16(0,64512,!1);else if(Number.isNaN(t))$.setUint16(0,32256,!1);else{$.setFloat32(0,t);let e=$.getUint32(0),n=(e&2139095040)>>23,r=e&8388607;if(n===255)$.setUint16(0,31744,!1);else if(n===0)$.setUint16(0,(t&2147483648)>>16|r>>13,!1);else{let i=n-127;i<-24?$.setUint16(0,0):i<-14?$.setUint16(0,(e&2147483648)>>16|1<<24+i,!1):$.setUint16(0,(e&2147483648)>>16|i+15<<10|r>>13,!1)}}}function vt(t,e){if(t.length-e<2)throw new Error(`${b} not enough data for float16`);let n=(t[e]<<8)+t[e+1];if(n===31744)return 1/0;if(n===64512)return-1/0;if(n===32256)return NaN;let r=n>>10&31,i=n&1023,o;return r===0?o=i*2**-24:r!==31?o=(i+1024)*2**(r-25):o=i===0?1/0:NaN,n&32768?-o:o}function fr(t){$.setFloat32(0,t,!1)}function Ut(t,e){if(t.length-e<4)throw new Error(`${b} not enough data for float32`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,4).getFloat32(0,!1)}function Fi(t){$.setFloat64(0,t,!1)}function lr(t,e){if(t.length-e<8)throw new Error(`${b} not enough data for float64`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,8).getFloat64(0,!1)}Ye.compareTokens=_.compareTokens;function x(t,e,n){throw new Error(`${b} encountered invalid minor (${n}) for major ${t[e]>>>5}`)}function je(t){return()=>{throw new Error(`${b} ${t}`)}}var p=[];for(let t=0;t<=23;t++)p[t]=x;p[24]=wn;p[25]=Sn;p[26]=En;p[27]=kn;p[28]=x;p[29]=x;p[30]=x;p[31]=x;for(let t=32;t<=55;t++)p[t]=x;p[56]=Tn;p[57]=An;p[58]=vn;p[59]=Cn;p[60]=x;p[61]=x;p[62]=x;p[63]=x;for(let t=64;t<=87;t++)p[t]=In;p[88]=Bn;p[89]=Pn;p[90]=_n;p[91]=$n;p[92]=x;p[93]=x;p[94]=x;p[95]=je("indefinite length bytes/strings are not supported");for(let t=96;t<=119;t++)p[t]=On;p[120]=Fn;p[121]=Nn;p[122]=Mn;p[123]=Rn;p[124]=x;p[125]=x;p[126]=x;p[127]=je("indefinite length bytes/strings are not supported");for(let t=128;t<=151;t++)p[t]=Dn;p[152]=Ln;p[153]=zn;p[154]=Vn;p[155]=Gn;p[156]=x;p[157]=x;p[158]=x;p[159]=Zn;for(let t=160;t<=183;t++)p[t]=qn;p[184]=Kn;p[185]=Wn;p[186]=Yn;p[187]=jn;p[188]=x;p[189]=x;p[190]=x;p[191]=Xn;for(let t=192;t<=215;t++)p[t]=Jn;p[216]=Qn;p[217]=er;p[218]=tr;p[219]=nr;p[220]=x;p[221]=x;p[222]=x;p[223]=x;for(let t=224;t<=243;t++)p[t]=je("simple values are not supported");p[244]=x;p[245]=x;p[246]=x;p[247]=rr;p[248]=je("simple values are not supported");p[249]=or;p[250]=sr;p[251]=ar;p[252]=x;p[253]=x;p[254]=x;p[255]=ir;var D=[];for(let t=0;t<24;t++)D[t]=new m(l.uint,t,1);for(let t=-1;t>=-24;t--)D[31-t]=new m(l.negint,t,1);D[64]=new m(l.bytes,new Uint8Array(0),1);D[96]=new m(l.string,"",1);D[128]=new m(l.array,0,1);D[160]=new m(l.map,0,1);D[244]=new m(l.false,!1,1);D[245]=new m(l.true,!0,1);D[246]=new m(l.null,null,1);function dr(t){switch(t.type){case l.false:return H([244]);case l.true:return H([245]);case l.null:return H([246]);case l.bytes:return t.value.length?void 0:H([64]);case l.string:return t.value===""?H([96]):void 0;case l.array:return t.value===0?H([128]):void 0;case l.map:return t.value===0?H([160]):void 0;case l.uint:return t.value<24?H([Number(t.value)]):void 0;case l.negint:if(t.value>=-24)return H([31-Number(t.value)])}}var mr=Object.freeze({float64:!0,mapSorter:Di,quickEncodeToken:dr});function Mi(){let t=[];return t[l.uint.major]=_,t[l.negint.major]=Ge,t[l.bytes.major]=fe,t[l.string.major]=Hn,t[l.array.major]=qe,t[l.map.major]=Ke,t[l.tag.major]=We,t[l.float.major]=Ye,t}var Ri=Mi(),Ct=new be,Je=class t{constructor(e,n){this.obj=e,this.parent=n}includes(e){let n=this;do if(n.obj===e)return!0;while(n=n.parent);return!1}static createCheck(e,n){if(e&&e.includes(n))throw new Error(`${kt} object contains circular references`);return new t(n,e)}},q={null:new m(l.null,null),undefined:new m(l.undefined,void 0),true:new m(l.true,!0),false:new m(l.false,!1),emptyArray:new m(l.array,0),emptyMap:new m(l.map,0)},K={number(t,e,n,r){return!Number.isInteger(t)||!Number.isSafeInteger(t)?new m(l.float,t):t>=0?new m(l.uint,t):new m(l.negint,t)},bigint(t,e,n,r){return t>=BigInt(0)?new m(l.uint,t):new m(l.negint,t)},Uint8Array(t,e,n,r){return new m(l.bytes,t)},string(t,e,n,r){return new m(l.string,t)},boolean(t,e,n,r){return t?q.true:q.false},null(t,e,n,r){return q.null},undefined(t,e,n,r){return q.undefined},ArrayBuffer(t,e,n,r){return new m(l.bytes,new Uint8Array(t))},DataView(t,e,n,r){return new m(l.bytes,new Uint8Array(t.buffer,t.byteOffset,t.byteLength))},Array(t,e,n,r){if(!t.length)return n.addBreakTokens===!0?[q.emptyArray,new m(l.break)]:q.emptyArray;r=Je.createCheck(r,t);let i=[],o=0;for(let s of t)i[o++]=Xe(s,n,r);return n.addBreakTokens?[new m(l.array,t.length),i,new m(l.break)]:[new m(l.array,t.length),i]},Object(t,e,n,r){let i=e!=="Object",o=i?t.keys():Object.keys(t),s=i?t.size:o.length;if(!s)return n.addBreakTokens===!0?[q.emptyMap,new m(l.break)]:q.emptyMap;r=Je.createCheck(r,t);let a=[],c=0;for(let u of o)a[c++]=[Xe(u,n,r),Xe(i?t.get(u):t[u],n,r)];return Hi(a,n),n.addBreakTokens?[new m(l.map,s),a,new m(l.break)]:[new m(l.map,s),a]}};K.Map=K.Object;K.Buffer=K.Uint8Array;for(let t of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))K[`${t}Array`]=K.DataView;function Xe(t,e={},n){let r=dn(t),i=e&&e.typeEncoders&&e.typeEncoders[r]||K[r];if(typeof i=="function"){let s=i(t,r,e,n);if(s!=null)return s}let o=K[r];if(!o)throw new Error(`${kt} unsupported type: ${r}`);return o(t,r,e,n)}function Hi(t,e){e.mapSorter&&t.sort(e.mapSorter)}function Di(t,e){if(t[0]instanceof m&&e[0]instanceof m){let n=t[0],r=e[0];return n._keyBytes||(n._keyBytes=pr(n.value)),r._keyBytes||(r._keyBytes=pr(r.value)),Ve(n._keyBytes,r._keyBytes)}throw new Error("rfc8949MapSorter: complex key types are not supported yet")}function pr(t){return Li(t,Ri,mr)}function hr(t,e,n,r){if(Array.isArray(e))for(let i of e)hr(t,i,n,r);else n[e.type.major](t,e,r)}function Li(t,e,n){let r=Xe(t,n);if(!Array.isArray(r)&&n.quickEncodeToken){let i=n.quickEncodeToken(r);if(i)return i;let o=e[r.type.major];if(o.encodedSize){let s=o.encodedSize(r,n),a=new be(s);if(o(a,r,n),a.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${r} was wrong`);return Et(a.chunks[0])}}return Ct.reset(),hr(Ct,r,e,n),Ct.toBytes(!0)}xt.hmacSha256Sync=(t,...e)=>St(wt,t,xt.concatBytes(...e));var Gi=32;function Qe(t){if(t.length===0)return new Uint8Array(Gi);let e=t.length,n=e*4,r=t.reduce((c,u)=>c+u.length,0),i=4+n+r,o=new Uint8Array(i),s=new DataView(o.buffer,o.byteOffset,o.byteLength);s.setUint32(0,e,!0);let a=4;for(let c of t)s.setUint32(a,c.length,!0),a+=4;for(let c of t)o.set(c,a),a+=c.length;return wt(o)}var It={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,entityRef:4,f32:4,vec2:8,vec3:12,quat:16,enum:1},Ee={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,f32:4,flags8:1,flags16:2,flags32:4,vec2:8,vec3:12,quat:16};var yr=new Set(["bool","uint8","int8","uint16","int16","uint32","int32","f32"]);function _t(t){let e=t.match(/^(\\w+)\\[(\\d+)\\]$/);if(!e||!e[1]||!e[2])return null;let n=e[1],r=parseInt(e[2],10);if(!yr.has(n))throw new Error(`Invalid array element type \'${n}\': arrays only support primitive scalar types (bool, uint8, int8, uint16, int16, uint32, int32, f32)`);return{elementType:n,length:r}}function gr(t){let e=_t(t);if(e){let r=Ee[e.elementType];if(r===void 0)throw new Error(`Unknown element type: ${e.elementType}`);return r*e.length}let n=Ee[t];if(n===void 0)throw new Error(`Unknown type: ${t}`);return n}function Zi(t){let e=[],n=[],r=new Map,i=new Map,o=0;for(let s of t.controls){let a=gr(s.type),c=_t(s.type),u={name:s.name,type:s.type,size:a,offset:0,options:s.options};c&&(u.arrayLength=c.length,u.arrayElementType=c.elementType);let f={name:s.name,index:o++,field:u,hint:s.hint,retain:s.retain==="always"?"always":"tick"};e.push(f),r.set(s.name,f)}for(let s of t.commands){let a=[],c=0;for(let f of s.args){let d=gr(f.type),g=_t(f.type),S={name:f.name,type:f.type,size:d,offset:c};g&&(S.arrayLength=g.length,S.arrayElementType=g.elementType),a.push(S),c+=d}let u={name:s.name,index:o++,args:a,totalSize:c};n.push(u),i.set(s.name,u)}return{controls:e,commands:n,controlByName:r,commandByName:i}}function Bt(t,e,n,r){switch(n){case"bool":t.setUint8(e,r?1:0);break;case"uint8":case"flags8":t.setUint8(e,r);break;case"int8":t.setInt8(e,r);break;case"uint16":case"flags16":t.setUint16(e,r,!0);break;case"int16":t.setInt16(e,r,!0);break;case"uint32":case"flags32":t.setUint32(e,r,!0);break;case"int32":t.setInt32(e,r,!0);break;case"f32":t.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function Pt(t,e,n){switch(n){case"bool":return t.getUint8(e)!==0;case"uint8":case"flags8":return t.getUint8(e);case"int8":return t.getInt8(e);case"uint16":case"flags16":return t.getUint16(e,!0);case"int16":return t.getInt16(e,!0);case"uint32":case"flags32":return t.getUint32(e,!0);case"int32":return t.getInt32(e,!0);case"f32":return t.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}var et=class t{constructor(e){y(this,"schema");y(this,"fields",new Map);y(this,"commandList",[]);this.schema=e}setControl(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);let i=new Uint8Array(r.field.size),o=new DataView(i.buffer);this.encodeValue(o,0,r.field,n),this.fields.set(r.index,i)}setFlags(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(!r.field.options)throw new Error(`Control ${e} is not a flags type`);let i=0;for(let a=0;a<r.field.options.length;a++){let c=r.field.options[a];n[c]&&(i|=1<<a)}let o=new Uint8Array(r.field.size),s=new DataView(o.buffer);Bt(s,0,r.field.type,i),this.fields.set(r.index,o)}setVec2(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let i=new Uint8Array(8),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),this.fields.set(r.index,i)}setVec3(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let i=new Uint8Array(12),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),this.fields.set(r.index,i)}setQuat(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let i=new Uint8Array(16),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),o.setFloat32(12,n[3],!0),this.fields.set(r.index,i)}getControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);let r=this.fields.get(n.index);if(!r)return this.getDefaultValue(n.field);let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return this.decodeValue(i,0,n.field)}getFlags(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(!n.field.options)throw new Error(`Control ${e} is not a flags type`);let r=this.fields.get(n.index),i=0;if(r){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);i=Pt(s,0,n.field.type)}let o={};for(let s=0;s<n.field.options.length;s++)o[n.field.options[s]]=(i&1<<s)!==0;return o}getVec2(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let r=this.fields.get(n.index);if(!r)return[0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0)]}getVec3(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let r=this.fields.get(n.index);if(!r)return[0,0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0)]}getQuat(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let r=this.fields.get(n.index);if(!r)return[0,0,0,1];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0),i.getFloat32(12,!0)]}hasControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);return this.fields.has(n.index)}addCommand(e,n){let r=this.schema.commandByName.get(e);if(!r)throw new Error(`Unknown command: ${e}`);let i=new Uint8Array(r.totalSize),o=new DataView(i.buffer);for(let s of r.args){let a=n[s.name];if(a===void 0)throw new Error(`Missing required argument: ${s.name}`);this.encodeValue(o,s.offset,s,a)}this.commandList.push({index:r.index,data:i})}getCommands(){let e=[];for(let{index:n,data:r}of this.commandList){let i=this.schema.commands.find(a=>a.index===n);if(!i)continue;let o=new DataView(r.buffer,r.byteOffset,r.byteLength),s={name:i.name};for(let a of i.args)s[a.name]=this.decodeValue(o,a.offset,a);e.push(s)}return e}encode(){let e=[];for(let[o,s]of this.fields){let a=new Uint8Array(2+s.length);a[0]=o,a[1]=s.length,a.set(s,2),e.push(a)}for(let o of this.commandList){let s=new Uint8Array(2+o.data.length);s[0]=o.index,s[1]=o.data.length,s.set(o.data,2),e.push(s)}let n=e.reduce((o,s)=>o+s.length,0),r=new Uint8Array(n),i=0;for(let o of e)r.set(o,i),i+=o.length;return r}static decode(e,n){let r=new t(e),i=0;for(;i<n.length;){if(i+2>n.length)throw new Error("Truncated TLV field header");let o=n[i],s=n[i+1];if(i+2+s>n.length)throw new Error(`Truncated TLV field data at index ${o}`);let a=n.subarray(i+2,i+2+s);e.controls.find(u=>u.index===o)?r.fields.set(o,new Uint8Array(a)):e.commands.find(f=>f.index===o)&&r.commandList.push({index:o,data:new Uint8Array(a)}),i+=2+s}return r}clear(){this.fields.clear(),this.commandList.length=0}encodeValue(e,n,r,i){if(r.type==="vec2"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0)}else if(r.type==="vec3"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0)}else if(r.type==="quat"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0),e.setFloat32(n+12,o[3],!0)}else if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let o=i,s=Ee[r.arrayElementType];for(let a=0;a<Math.min(o.length,r.arrayLength);a++)Bt(e,n+a*s,r.arrayElementType,o[a])}else Bt(e,n,r.type,i)}decodeValue(e,n,r){if(r.type==="vec2")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0)];if(r.type==="vec3")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)];if(r.type==="quat")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0),e.getFloat32(n+12,!0)];if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let i=[],o=Ee[r.arrayElementType];for(let s=0;s<r.arrayLength;s++)i.push(Pt(e,n+s*o,r.arrayElementType));return i}else return Pt(e,n,r.type)}getDefaultValue(e){return e.type==="vec2"?[0,0]:e.type==="vec3"?[0,0,0]:e.type==="quat"?[0,0,0,1]:e.arrayLength!==void 0?new Array(e.arrayLength).fill(0):e.type==="bool"?!1:0}};function br(t){let e=Zi(t);return{create(){return new et(e)},decode(n){return et.decode(e,n)},get schema(){return e}}}function xr(t,e){return((e&65535)<<16|t&65535)>>>0}function ke(t){return t&65535}function ji(t){return t>>>16&65535}function pe(t){return t.subarray(8,40)}function $t(t,e){if(e.length!==32)throw new Error(`stateId must be ${32} bytes, got ${e.length}`);t.set(e,8)}function wr(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint32(40,e,!0)}function rt(t,e,n){let r=new Uint8Array(68);return r.set(t,0),new DataView(r.buffer).setUint32(32,e,!0),r.set(n,36),ge(r)}function Sr(t,e,n){return rt(t,e,Qe(n))}function Ot(t){return ge(t)}function Er(t,e,n){let r=new ArrayBuffer(t.totalSize),i=new Uint8Array(r),o=new DataView(r);if(o.setUint32(0,1297367376,!0),o.setUint16(4,0,!0),i[6]=n??0,i[7]=0,e){let a=Ot(e);i.set(a,8)}let s=t.freeStackOffset;o.setUint16(s,t.maxEntities,!0);for(let a=0;a<t.maxEntities;a++){let c=t.freeStackOffset+2+a*2;o.setUint16(c,a,!0)}return i}function kr(t,e){return t.entityTableOffset+e*t.entityRecordSize}function Ft(t,e,n){let r=kr(t,n);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(r,!0)}function Xi(t){return new DataView(t.buffer,t.byteOffset,t.byteLength).getUint16(4,!0)}function Ji(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint16(4,e,!0)}function Qi(t,e){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t.freeStackOffset,!0)}function eo(t,e,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t.freeStackOffset,n,!0)}function to(t,e){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Qi(t,e);if(r===0)throw new Error(`No free entity slots available (max: ${t.maxEntities})`);let i=t.freeStackOffset+2+(r-1)*2,o=n.getUint16(i,!0);return eo(t,e,r-1),o}function Tr(t,e){let n=to(t,e),r=Ft(t,e,n);return Ji(e,Xi(e)+1),xr(n,r)}function it(t,e,n){if(n===4294967295)return!1;let r=ke(n);if(r>=t.maxEntities)return!1;let i=ji(n);return Ft(t,e,r)===i}function Nt(t,e,n){if(n<0)throw new Error("Singleton components do not have component bitmask entries");let r=kr(t,e)+2,i=Math.floor(n/8),o=n%8;return{byteOffset:r+i,bitIndex:o}}function Ar(t,e,n,r){if(!it(t,e,n))return!1;let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and is always present`);let o=ke(n),{byteOffset:s,bitIndex:a}=Nt(t,o,i.index);return(e[s]&1<<a)!==0}function vr(t,e,n,r){if(!it(t,e,n)){let c=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${c} is not alive`)}let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and cannot be added to entities`);let o=ke(n),{byteOffset:s,bitIndex:a}=Nt(t,o,i.index);e[s]=(e[s]??0)|1<<a}function Ur(t,e,n,r){let i=ke(e);return n.storageOffset+i*n.size+r.offset}function no(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":return(t[e]??0)!==0;case"uint8":return t[e]??0;case"int8":return r.getInt8(e);case"uint16":return r.getUint16(e,!0);case"int16":return r.getInt16(e,!0);case"uint32":return r.getUint32(e,!0);case"int32":return r.getInt32(e,!0);case"entityRef":return r.getUint32(e,!0);case"f32":return r.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}function Cr(t,e,n,r){let i=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":t[e]=r?1:0;break;case"uint8":t[e]=r&255;break;case"int8":i.setInt8(e,r);break;case"uint16":i.setUint16(e,r,!0);break;case"int16":i.setInt16(e,r,!0);break;case"uint32":i.setUint32(e,r,!0);break;case"int32":i.setInt32(e,r,!0);break;case"entityRef":i.setUint32(e,r,!0);break;case"f32":i.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function ro(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0)}function io(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0)}function oo(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0),r.setFloat32(e+12,n[3],!0)}function Ir(t,e,n,r,i,o){if(!it(t,e,n))return;let s=t.componentByName.get(r);if(!s)throw new Error(`Unknown component: \'${r}\'`);if(s.isSingleton)throw new Error(`Component \'${r}\' is a singleton; use singleton accessors instead`);if(!nt(t,e,n,s))return;let a=s.fields.find(f=>f.name===i);if(!a)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let c=Ur(t,n,s,a),u=a.type;if(a.arrayLength!==void 0&&a.arrayElementType!==void 0){if(o===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(o<0||o>=a.arrayLength)throw new Error(`Array index ${o} out of bounds for ${r}.${i} (length: ${a.arrayLength})`);let f=It[a.arrayElementType];if(f===void 0)throw new Error(`Unknown array element type: ${a.arrayElementType}`);c+=o*f,u=a.arrayElementType}return no(e,c,u)}function w(t,e,n,r,i,o,s){if(!it(t,e,n)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} is not alive`)}let a=t.componentByName.get(r);if(!a)throw new Error(`Unknown component: \'${r}\'`);if(!nt(t,e,n,a)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} does not have component \'${r}\'`)}let c=a.fields.find(d=>d.name===i);if(!c)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let u=Ur(t,n,a,c),f=c.type;if(c.arrayLength!==void 0&&c.arrayElementType!==void 0){if(s===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(s<0||s>=c.arrayLength)throw new Error(`Array index ${s} out of bounds for ${r}.${i} (length: ${c.arrayLength})`);let d=It[c.arrayElementType];if(d===void 0)throw new Error(`Unknown array element type: ${c.arrayElementType}`);u+=s*d,f=c.arrayElementType}Cr(e,u,f,o)}function nt(t,e,n,r){if(r.isSingleton)throw new Error(`Component \'${r.name}\' is a singleton and cannot be queried per entity`);let i=ke(n),{byteOffset:o,bitIndex:s}=Nt(t,i,r.index);return(e[o]&1<<s)!==0}function Br(t,e,n,r){if(n.length===0)throw new Error("Query must include at least one component");let i=[];for(let a of n){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);i.push(c)}let o=[];if(r)for(let a of r){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);o.push(c)}function*s(){for(let a=0;a<t.maxEntities;a++){let c=Ft(t,e,a),u=xr(a,c),f=!0;for(let d of i)if(!nt(t,e,u,d)){f=!1;break}if(f){for(let d of o)if(nt(t,e,u,d)){f=!1;break}f&&(yield u)}}}return s()}function Pr(t,e){if(!t.events||!t.eventByName)throw new Error("Schema has no events");let n=t.eventByName.get(e);if(!n)throw new Error(`Unknown event: \'${e}\'`);return n}function _r(t,e,n){let r=Pr(t,n);new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(r.storageOffset,0,!0)}function $r(t,e,n,r){let i=Pr(t,n),o=new DataView(e.buffer,e.byteOffset,e.byteLength),s=o.getUint16(i.storageOffset,!0);if(s>=i.maxEvents)return!1;let a=i.storageOffset+2+s*i.recordSize;for(let c of i.fields){let u=r[c.name];if(u===void 0)throw new Error(`Missing required field \'${c.name}\' for event \'${n}\'`);let f=a+c.offset;c.type==="vec2"?ro(e,f,u):c.type==="vec3"?io(e,f,u):c.type==="quat"?oo(e,f,u):Cr(e,f,c.type,u)}return o.setUint16(i.storageOffset,s+1,!0),!0}function ot(t){if(t.length!==32)throw new Error(`stateId must be ${32} bytes, got ${t.length}`);return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}var st=class{constructor(e={}){y(this,"cache");y(this,"maxSize");this.cache=new Map,this.maxSize=e.maxSize??100}store(e){let n=pe(e),r=ot(n);if(this.cache.size>=this.maxSize&&!this.cache.has(r)){let i=this.cache.keys().next().value;i!==void 0&&this.cache.delete(i)}this.cache.set(r,e.slice())}getByStateId(e){let n=ot(e),r=this.cache.get(n);return r?r.slice():void 0}getCached(e,n,r){let i=pe(e),o=rt(i,n,r);return this.getByStateId(o)}has(e){let n=ot(e);return this.cache.has(n)}hasCached(e,n,r){let i=pe(e),o=rt(i,n,r);return this.has(o)}delete(e){let n=ot(e);return this.cache.delete(n)}clear(){this.cache.clear()}get size(){return this.cache.size}keys(){return this.cache.keys()}};var Te=65536,at=class{constructor(e){y(this,"memory",null);y(this,"plugins",[]);y(this,"options");y(this,"stateSize",null);y(this,"statePtr",null);y(this,"heapPos",0);y(this,"arenaResetMark",0);y(this,"inputCodec",null);y(this,"playerEntities",new Map);y(this,"hostAlloc",(e,n)=>{let r=this.memory;if(!r)throw new Error("Memory not initialized for host_alloc");let i=this.heapPos+n-1&~(n-1),o=i+e,s=r.buffer.byteLength;if(o>s){let c=Math.ceil((o-s)/Te);if(this.options.debug){let u=(s+c*Te)/1048576;console.warn(`[mt] WASM memory grew to ${u.toFixed(1)}MB`)}r.grow(c)}return new Uint8Array(r.buffer).fill(0,i,o),this.heapPos=o,i});this.options=e}markArenaReset(){this.arenaResetMark=this.heapPos}resetArena(){this.heapPos=this.arenaResetMark}async init(){if(this.stateSize=this.options.stateSchema.totalSize,!this.stateSize)throw new Error("State schema total size is required");this.options.inputSchema&&(this.inputCodec=br(this.options.inputSchema));let e=4*1024*1024,n=this.options.plugins??(this.options.moduleBytes?[{name:"main",wasmBytes:this.options.moduleBytes,reservedBytes:e}]:[]);for(let c of n)if(typeof c.reservedBytes!="number"||c.reservedBytes<=0)throw new Error(`Plugin "${c.name}" requires reservedBytes > 0`);let r=n.reduce((c,u)=>{let f=Math.ceil(u.reservedBytes/Te)*Te;return c+f},0),i=1024*1024*5,o=this.stateSize+i+r,s=Math.ceil(o/Te);this.memory=new WebAssembly.Memory({initial:s}),this.heapPos=r;let a={env:{memory:this.memory,host_alloc:this.hostAlloc,abort:()=>{throw new Error("WASM abort called")}}};for(let c of n){let u=c.wasmBytes.slice().buffer,f=await WebAssembly.compile(u),d=await WebAssembly.instantiate(f,a);if(typeof d.exports.apply!="function")throw new Error(`Plugin "${c.name}" missing required apply() export`);this.plugins.push({name:c.name,instance:d,exports:d.exports})}return this.statePtr=this.hostAlloc(this.stateSize,8),this.markArenaReset(),this}findPlayerEntity(e,n){let r=this.options.stateSchema,i=this.playerEntities.get(n);if(i!==void 0){if(Ar(r,e,i,"Player"))return i;this.playerEntities.delete(n)}for(let o of Br(r,e,["Player"]))if(Ir(r,e,o,"Player","index")===n)return this.playerEntities.set(n,o),o}spawnPlayerEntity(e,n){let r=this.options.stateSchema,i=Tr(r,e);return vr(r,e,i,"Player"),w(r,e,i,"Player","index",n),this.playerEntities.set(n,i),i}findOrSpawnPlayerEntity(e,n){let r=this.findPlayerEntity(e,n);return r!==void 0?r:this.spawnPlayerEntity(e,n)}resetPlayerControls(e,n){let r=this.options.stateSchema;if(this.inputCodec)for(let i of this.inputCodec.schema.controls){if(i.retain==="always")continue;let o=i.field.type,s=i.name;if(o==="vec2")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0);else if(o==="vec3")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0);else if(o==="quat")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0),w(r,e,n,"Player",`${s}_w`,1);else if(o.startsWith("flags"))w(r,e,n,"Player",s,0);else if(o.includes("[")){let a=o.match(/\\[(\\d+)\\]/);if(a?.[1]){let c=parseInt(a[1],10);for(let u=0;u<c;u++)w(r,e,n,"Player",s,0,u)}}else w(r,e,n,"Player",s,o==="bool"?!1:0)}}writeControlsToPlayer(e,n,r){let i=this.options.stateSchema,o=this.options.inputSchema;if(o)for(let s of o.controls){if(!r.hasControl(s.name))continue;let a=s.type,c=s.name;if(a==="vec2"){let[u,f]=r.getVec2(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f)}else if(a==="vec3"){let[u,f,d]=r.getVec3(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d)}else if(a==="quat"){let[u,f,d,g]=r.getQuat(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d),w(i,e,n,"Player",`${c}_w`,g)}else if(a.startsWith("flags")){let u=r.getControl(c);w(i,e,n,"Player",c,u)}else if(a.includes("[")){let u=r.getControl(c);for(let f=0;f<u.length;f++)w(i,e,n,"Player",c,u[f],f)}else{let u=r.getControl(c);w(i,e,n,"Player",c,u)}}}pushCommands(e,n,r){let i=this.options.stateSchema,o=r.getCommands();for(let s of o){let a=s.name.charAt(0).toUpperCase()+s.name.slice(1)+"Command",c={player_index:n};for(let[u,f]of Object.entries(s))u!=="name"&&(Array.isArray(f)?f.length===2?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1]):f.length===3?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2]):f.length===4&&(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2],c[`${u}_w`]=f[3]):c[u]=f);$r(i,e,a,c)}}clearCommandEvents(e){let n=this.options.stateSchema,r=this.options.inputSchema;if(!(!r||!n.events))for(let i of r.commands){let o=i.name.charAt(0).toUpperCase()+i.name.slice(1)+"Command";n.eventByName?.has(o)&&_r(n,e,o)}}decodePayloadsToState(e,n){if(this.inputCodec)for(let r=0;r<n.length;r++){let i=n[r];if(i===void 0)continue;let o=this.findOrSpawnPlayerEntity(e,r);if(this.resetPlayerControls(e,o),i.length===0)continue;let s=this.inputCodec.decode(i);this.writeControlsToPlayer(e,o,s),this.pushCommands(e,r,s)}}transition(e,n){let r=this.stateSize;if(r===null||e.length!==r)throw new Error(`State size mismatch: expected ${this.stateSize}, got ${e.length}`);if(this.plugins.length===0||n.length===0)return e.slice();let i=this.memory;if(!i)throw new Error("WASM memory not initialized");let o=this.statePtr;if(o===null||o<0)throw new Error("State pointer not initialized");let s=new Uint8Array(i.buffer);s.set(e,o);for(let a of n){let c=new Uint8Array(i.buffer,o,r);wr(c,a.tick),this.clearCommandEvents(c),this.decodePayloadsToState(c,a.payloads);for(let u of this.plugins)u.exports.apply(o),s=new Uint8Array(i.buffer)}return s.slice(o,o+r)}close(){}getStateSize(){let e=this.stateSize;if(e===null||e<0)throw new Error("State size not initialized");return e}};var W=class{constructor(e=1e3,n=1024){y(this,"buffer");y(this,"windowMs");y(this,"head",0);y(this,"tail",0);y(this,"size",0);this.windowMs=e,this.buffer=new Float64Array(n)}inc(){let e=performance.now();this.buffer[this.head]=e,this.head=(this.head+1)%this.buffer.length,this.size<this.buffer.length?this.size++:this.tail=(this.tail+1)%this.buffer.length}count(e=performance.now()){let n=e-this.windowMs;for(;this.size>0&&!(this.buffer[this.tail]>=n);)this.tail=(this.tail+1)%this.buffer.length,this.size--;return this.size}rate(e=performance.now()){return this.count(e)*(1e3/this.windowMs)}};function so(t){return("moduleBytes"in t||"plugins"in t)&&"stateSchema"in t}function ao(t,e){for(let n=t.length-1;n>=0;n--)if(e(t[n]))return n;return-1}var ct=class{constructor(e){y(this,"debug");y(this,"stateSchema");y(this,"inputs",null);y(this,"cache");y(this,"executor",null);y(this,"genesisStateId");y(this,"options");y(this,"ticking",!1);y(this,"tickGraceMs");y(this,"tickLag");y(this,"maxBatchSize");y(this,"currentState");y(this,"currentTick");y(this,"currentNodeId");y(this,"prevOnStateUpdateState",null);y(this,"onStateUpdate");y(this,"syncCheckpoint");y(this,"stateHistory",[]);y(this,"maxHistory",64);y(this,"stats");y(this,"loop",()=>{this._loop().catch(co)});y(this,"_loop",async()=>{if(this.ticking)try{let e=await this.tick();if(this.onStateUpdate&&this.currentState!==this.prevOnStateUpdateState&&(this.onStateUpdate(this.currentState),this.prevOnStateUpdateState=this.currentState,this.stats.updates.inc()),e.rolledBack&&this.stats.rollbacks.inc(),e.ticksComputed>0)for(let n=0;n<e.ticksComputed;n++)this.stats.executions.inc()}catch(e){console.error("Error in tick loop:",e)}finally{setTimeout(this.loop,this.tickGraceMs)}});e.log&&(this.inputs=e.log),this.debug=e.debug??!1,this.options=e,this.stateSchema=e.stateSchema,this.cache=e.cache??new st,this.genesisStateId=Ot(e.genesisHash),this.tickGraceMs=e.tickGraceMs??10,this.tickLag=e.tickLag??1,this.maxBatchSize=e.maxBatchSize??200,this.stats={rollbacks:new W,executions:new W,updates:new W,cacheHits:new W,cacheMisses:new W},this.currentState=Er(this.stateSchema,e.genesisHash,e.tickRate),this.currentNodeId=null,this.syncCheckpoint=null,this.currentTick=null,this.cache.store(this.currentState)}setLog(e){this.inputs=lt(e)}setOnStateUpdate(e){e===null&&(this.prevOnStateUpdateState=null),this.onStateUpdate=e}async init(){if(!this.inputs)throw new Error("Rollback.init() called before log was configured. Call setLog() first or pass log in options.");let e=this.options.executor;if(so(e)){let r=new at(e);await r.init(),this.executor=r}else this.executor=e;if((await this.tick(0)).ticksComputed===0||this.currentTick===null)throw new Error("Failed to record genesis snapshot");return this.options.disableTicking||(this.ticking=!0,this.loop()),this.getState()}async tick(e){if(!this.inputs)throw new Error("Rollback.tick() called before log was configured. Call setLog() and init() first.");let n=await this.inputs.getTicksAfter(this.currentNodeId,{limit:this.maxBatchSize,lag:this.tickLag});if(e&&(n=n.filter(a=>a.tick<=e)),n.length===0)return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:!1,ticksComputed:0};let r=!1,i=null;this.currentTick!==null&&n[0].tick<this.currentTick&&(r=!0,i=this.currentTick,this.rollbackTo(n[0]));let o=n.filter(a=>this.currentTick===null||a.tick>this.currentTick);if(o.length===0)return this.currentNodeId=n[n.length-1].id,{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId,rolledBack:r,ticksComputed:0};let s=this.processTicks(o,r,i);return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:r,ticksComputed:s}}async getStats(){return{rollbacks:this.stats.rollbacks.rate(),executions:this.stats.executions.rate(),updates:this.stats.updates.rate(),cacheHits:this.stats.cacheHits.rate(),cacheMisses:this.stats.cacheMisses.rate()}}processTicks(e,n=!1,r=null){if(!this.executor)throw new Error("Executor not initialized");let i=0,o=0;for(let u=0;u<e.length;u++){let f=e[u],d=Qe(f.payloads),g=this.cache.getCached(this.currentState,f.tick,d);if(g)this.currentState=g,this.currentTick=f.tick,this.currentNodeId=f.id,o=u+1,f.sync&&this.updateSyncCheckpoint(f);else break}if(n&&r!==null&&o>0)for(let u=0;u<o;u++)e[u].tick<=r&&this.stats.cacheHits.inc();let s=e.slice(o);if(s.length===0)return 0;let a=ao(s,u=>u.sync);if(a>=0){let u=s.slice(0,a+1),f=this.executor.transition(this.currentState,u),d=this.computeBatchStateId(this.currentState,u);$t(f,d);let g=u[u.length-1];this.currentState=f,this.currentTick=g.tick,this.currentNodeId=g.id,i+=u.length,this.cache.store(f),this.recordStateSnapshot(this.currentTick,this.currentNodeId,d),this.updateSyncCheckpoint(g)}let c=a>=0?s.slice(a+1):s;if(c.length>0){let u=this.executor.transition(this.currentState,c),f=this.computeBatchStateId(this.currentState,c);$t(u,f);let d=c[c.length-1];this.currentState=u,this.currentTick=d.tick,this.currentNodeId=d.id,i+=c.length,this.cache.store(u),this.recordStateSnapshot(this.currentTick,this.currentNodeId,f)}if(n&&r!==null)for(let u of s)u.tick<=r&&this.stats.cacheMisses.inc();return i}computeBatchStateId(e,n){let r=pe(e);for(let i of n)r=Sr(r,i.tick,i.payloads);return r}getState(){return this.currentState.slice()}getTick(){return this.currentTick}reset(){this.currentTick=0,this.currentNodeId=null,this.syncCheckpoint=null}close(){this.ticking=!1,this.setOnStateUpdate(null),this.executor&&(this.executor.close(),this.executor=null)}rollbackTo(e){if(this.syncCheckpoint&&this.syncCheckpoint.tick>=e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}if(this.syncCheckpoint&&this.syncCheckpoint.tick<e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}let n=e.tick-1,r=this.findSnapshotAtOrBefore(n);if(r){let o=this.cache.getByStateId(r.stateId);if(o){this.currentState=o,this.currentTick=r.tick,this.currentNodeId=r.nodeId;return}}let i=this.cache.getByStateId(this.genesisStateId);if(i){this.currentState=i,this.currentTick=0,this.currentNodeId=null;return}}updateSyncCheckpoint(e){this.syncCheckpoint={state:this.currentState.slice(),tick:e.tick,nodeId:e.id}}recordStateSnapshot(e,n,r){this.stateHistory.push({tick:e,nodeId:n,stateId:r.slice()}),this.stateHistory.length>this.maxHistory&&this.stateHistory.shift()}findSnapshotAtOrBefore(e){for(let n=this.stateHistory.length-1;n>=0;n--){let r=this.stateHistory[n];if(r.tick<=e)return r}return null}};function co(t){console.warn("rollback unexpected:",t)}Be(ct);var Ga=null;globalThis.onerror=t=>(console.error("\\u{1F534} FATAL ROLLBACK WORKER ERROR (Uncaught Exception):",t),!0);export{Ga as default};\n/*! Bundled license information:\n\ncomlink/dist/esm/comlink.mjs:\n (**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n *)\n\n@noble/secp256k1/index.js:\n (*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)\n\n@noble/hashes/esm/utils.js:\n (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)\n*/\n';
6023
6023
  function Worker() {
6024
6024
  const blob = new Blob([workerCode], { type: "text/javascript" });
6025
6025
  const url = URL.createObjectURL(blob);
@@ -19,12 +19,8 @@ export interface BatchTransitionExecutor {
19
19
  close: () => void;
20
20
  }
21
21
  export interface TransitionExports {
22
- /** Per-tick transition function - mutates state in-place
23
- * @param statePtr - Pointer to state buffer in WASM memory
24
- * @param outputStateIdPtr - Pointer to 32-byte output stateId (for physics caching)
25
- * @param isSync - 1 if this is a sync tick, 0 otherwise (for cache checkpoint)
26
- */
27
- apply: (statePtr: number, outputStateIdPtr: number, isSync: number) => void;
22
+ /** Per-tick transition function - mutates state in-place */
23
+ apply: (statePtr: number) => void;
28
24
  /** WASM memory */
29
25
  memory: WebAssembly.Memory;
30
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsC,KAAK,WAAW,EAAsB,MAAM,eAAe,CAAC;AACzG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAajB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,WAAW,CAAC;IACxE,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5E,kBAAkB;IAClB,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;CAC5B;AAED,kDAAkD;AAClD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,UAAU,CAAC;IACtB,oFAAoF;IACpF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC5B,sEAAsE;IACtE,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,SAAS,EAAE,CAAC;IACtB,uBAAuB;IACvB,WAAW,EAAE,cAAc,CAAC;IAC5B,uBAAuB;IACvB,WAAW,EAAE,WAAW,CAAC;IACzB,mFAAmF;IACnF,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAYD,qBAAa,QAAS,YAAW,uBAAuB;IACtD,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,OAAO,CAAkB;IAGjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,QAAQ,CAAuB;IAGvC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,cAAc,CAAa;IAGnC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,cAAc,CAAqC;gBAE/C,OAAO,EAAE,eAAe;IAIpC;;;;OAIG;IACH,OAAO,CAAC,SAAS,CA2Bf;IAEF;;;OAGG;IACH,cAAc,IAAI,IAAI;IAItB;;;OAGG;IACH,UAAU,IAAI,IAAI;IAIZ,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IAyF/B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IASzB;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAQ/B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IA6C3B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IA+C7B;;;OAGG;IACH,OAAO,CAAC,YAAY;IAuCpB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAe1B;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IA+B7B;;;;;;OAMG;IACH,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,UAAU;IA2EjE,KAAK,IAAI,IAAI;IAKb,YAAY,IAAI,MAAM;CAOvB"}
1
+ {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsC,KAAK,WAAW,EAAsB,MAAM,eAAe,CAAC;AACzG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAWjB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,WAAW,CAAC;IACxE,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,4DAA4D;IAC5D,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,kBAAkB;IAClB,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;CAC5B;AAED,kDAAkD;AAClD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,UAAU,CAAC;IACtB,oFAAoF;IACpF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC5B,sEAAsE;IACtE,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,SAAS,EAAE,CAAC;IACtB,uBAAuB;IACvB,WAAW,EAAE,cAAc,CAAC;IAC5B,uBAAuB;IACvB,WAAW,EAAE,WAAW,CAAC;IACzB,mFAAmF;IACnF,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAYD,qBAAa,QAAS,YAAW,uBAAuB;IACtD,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,OAAO,CAAkB;IAGjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,QAAQ,CAAuB;IAGvC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,cAAc,CAAa;IAGnC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,cAAc,CAAqC;gBAE/C,OAAO,EAAE,eAAe;IAIpC;;;;OAIG;IACH,OAAO,CAAC,SAAS,CA2Bf;IAEF;;;OAGG;IACH,cAAc,IAAI,IAAI;IAItB;;;OAGG;IACH,UAAU,IAAI,IAAI;IAIZ,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IAyF/B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IASzB;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAQ/B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IA6C3B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IA+C7B;;;OAGG;IACH,OAAO,CAAC,YAAY;IAuCpB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAe1B;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IA+B7B;;;;;;OAMG;IACH,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,UAAU;IAuDjE,KAAK,IAAI,IAAI;IAKb,YAAY,IAAI,MAAM;CAOvB"}
package/dist/lib.js CHANGED
@@ -6792,22 +6792,15 @@ var Executor = class {
6792
6792
  }
6793
6793
  let view = new Uint8Array(memory.buffer);
6794
6794
  view.set(state, statePtr);
6795
- const outputStateIdPtr = this.hostAlloc(32, 8);
6796
- let currentStateId = getStateId(state);
6797
6795
  for (const tick of ticks) {
6798
- const outputStateId = computeNextStateIdFromPayloads(currentStateId, tick.tick, tick.payloads);
6799
- view = new Uint8Array(memory.buffer);
6800
- view.set(outputStateId, outputStateIdPtr);
6801
6796
  let stateView2 = new Uint8Array(memory.buffer, statePtr, stateSize);
6802
6797
  setTick(stateView2, tick.tick);
6803
6798
  this.clearCommandEvents(stateView2);
6804
6799
  this.decodePayloadsToState(stateView2, tick.payloads);
6805
- const isSync = tick.sync ? 1 : 0;
6806
6800
  for (const plugin of this.plugins) {
6807
- plugin.exports.apply(statePtr, outputStateIdPtr, isSync);
6801
+ plugin.exports.apply(statePtr);
6808
6802
  view = new Uint8Array(memory.buffer);
6809
6803
  }
6810
- currentStateId = outputStateId;
6811
6804
  }
6812
6805
  return view.slice(statePtr, statePtr + stateSize);
6813
6806
  }
@@ -7224,7 +7217,7 @@ function warnUnexpected2(err2) {
7224
7217
  }
7225
7218
 
7226
7219
  // src/rollback.worker.ts
7227
- var workerCode = 'var Or=Object.defineProperty;var Fr=(t,e,n)=>e in t?Or(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var y=(t,e,n)=>Fr(t,typeof e!="symbol"?e+"":e,n);var Ht=Symbol("Comlink.proxy"),Nr=Symbol("Comlink.endpoint"),Mr=Symbol("Comlink.releaseProxy"),ft=Symbol("Comlink.finalizer"),ve=Symbol("Comlink.thrown"),Dt=t=>typeof t=="object"&&t!==null||typeof t=="function",Rr={canHandle:t=>Dt(t)&&t[Ht],serialize(t){let{port1:e,port2:n}=new MessageChannel;return Be(t,e),[n,[n]]},deserialize(t){return t.start(),dt(t)}},Hr={canHandle:t=>Dt(t)&&ve in t,serialize({value:t}){let e;return t instanceof Error?e={isError:!0,value:{message:t.message,name:t.name,stack:t.stack}}:e={isError:!1,value:t},[e,[]]},deserialize(t){throw t.isError?Object.assign(new Error(t.value.message),t.value):t.value}},Lt=new Map([["proxy",Rr],["throw",Hr]]);function Dr(t,e){for(let n of t)if(e===n||n==="*"||n instanceof RegExp&&n.test(e))return!0;return!1}function Be(t,e=globalThis,n=["*"]){e.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Dr(n,i.origin)){console.warn(`Invalid origin \'${i.origin}\' for comlink proxy`);return}let{id:o,type:s,path:a}=Object.assign({path:[]},i.data),c=(i.data.argumentList||[]).map(j),u;try{let f=a.slice(0,-1).reduce((g,S)=>g[S],t),d=a.reduce((g,S)=>g[S],t);switch(s){case"GET":u=d;break;case"SET":f[a.slice(-1)[0]]=j(i.data.value),u=!0;break;case"APPLY":u=d.apply(f,c);break;case"CONSTRUCT":{let g=new d(...c);u=qr(g)}break;case"ENDPOINT":{let{port1:g,port2:S}=new MessageChannel;Be(t,S),u=Zr(g,[g])}break;case"RELEASE":u=void 0;break;default:return}}catch(f){u={value:f,[ve]:0}}Promise.resolve(u).catch(f=>({value:f,[ve]:0})).then(f=>{let[d,g]=Ie(f);e.postMessage(Object.assign(Object.assign({},d),{id:o}),g),s==="RELEASE"&&(e.removeEventListener("message",r),zt(e),ft in t&&typeof t[ft]=="function"&&t[ft]())}).catch(f=>{let[d,g]=Ie({value:new TypeError("Unserializable return value"),[ve]:0});e.postMessage(Object.assign(Object.assign({},d),{id:o}),g)})}),e.start&&e.start()}function Lr(t){return t.constructor.name==="MessagePort"}function zt(t){Lr(t)&&t.close()}function dt(t,e){let n=new Map;return t.addEventListener("message",function(i){let{data:o}=i;if(!o||!o.id)return;let s=n.get(o.id);if(s)try{s(o)}finally{n.delete(o.id)}}),lt(t,n,[],e)}function Ae(t){if(t)throw new Error("Proxy has been released and is not useable")}function Vt(t){return re(t,new Map,{type:"RELEASE"}).then(()=>{zt(t)})}var Ue=new WeakMap,Ce="FinalizationRegistry"in globalThis&&new FinalizationRegistry(t=>{let e=(Ue.get(t)||0)-1;Ue.set(t,e),e===0&&Vt(t)});function zr(t,e){let n=(Ue.get(e)||0)+1;Ue.set(e,n),Ce&&Ce.register(t,e,t)}function Vr(t){Ce&&Ce.unregister(t)}function lt(t,e,n=[],r=function(){}){let i=!1,o=new Proxy(r,{get(s,a){if(Ae(i),a===Mr)return()=>{Vr(o),Vt(t),e.clear(),i=!0};if(a==="then"){if(n.length===0)return{then:()=>o};let c=re(t,e,{type:"GET",path:n.map(u=>u.toString())}).then(j);return c.then.bind(c)}return lt(t,e,[...n,a])},set(s,a,c){Ae(i);let[u,f]=Ie(c);return re(t,e,{type:"SET",path:[...n,a].map(d=>d.toString()),value:u},f).then(j)},apply(s,a,c){Ae(i);let u=n[n.length-1];if(u===Nr)return re(t,e,{type:"ENDPOINT"}).then(j);if(u==="bind")return lt(t,e,n.slice(0,-1));let[f,d]=Rt(c);return re(t,e,{type:"APPLY",path:n.map(g=>g.toString()),argumentList:f},d).then(j)},construct(s,a){Ae(i);let[c,u]=Rt(a);return re(t,e,{type:"CONSTRUCT",path:n.map(f=>f.toString()),argumentList:c},u).then(j)}});return zr(o,t),o}function Gr(t){return Array.prototype.concat.apply([],t)}function Rt(t){let e=t.map(Ie);return[e.map(n=>n[0]),Gr(e.map(n=>n[1]))]}var Gt=new WeakMap;function Zr(t,e){return Gt.set(t,e),t}function qr(t){return Object.assign(t,{[Ht]:!0})}function Ie(t){for(let[e,n]of Lt)if(n.canHandle(t)){let[r,i]=n.serialize(t);return[{type:"HANDLER",name:e,value:r},i]}return[{type:"RAW",value:t},Gt.get(t)||[]]}function j(t){switch(t.type){case"HANDLER":return Lt.get(t.name).deserialize(t.value);case"RAW":return t.value}}function re(t,e,n,r){return new Promise(i=>{let o=Kr();e.set(o,i),t.start&&t.start(),t.postMessage(Object.assign({id:o},n),r)})}function Kr(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var Wr={p:0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,n:0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,h:1n,a:0n,b:7n,Gx:0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,Gy:0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n},{p:J,n:yt,Gx:Yr,Gy:jr,b:Xt}=Wr,ie=32,pt=64,U=(t="")=>{throw new Error(t)},Jt=t=>typeof t=="bigint",Qt=t=>typeof t=="string",Xr=t=>t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array",me=(t,e)=>!Xr(t)||typeof e=="number"&&e>0&&t.length!==e?U("Uint8Array expected"):t,Ne=t=>new Uint8Array(t),Jr=t=>Uint8Array.from(t),en=(t,e)=>t.toString(16).padStart(e,"0"),gt=t=>Array.from(me(t)).map(e=>en(e,2)).join(""),z={_0:48,_9:57,A:65,F:70,a:97,f:102},Zt=t=>{if(t>=z._0&&t<=z._9)return t-z._0;if(t>=z.A&&t<=z.F)return t-(z.A-10);if(t>=z.a&&t<=z.f)return t-(z.a-10)},bt=t=>{let e="hex invalid";if(!Qt(t))return U(e);let n=t.length,r=n/2;if(n%2)return U(e);let i=Ne(r);for(let o=0,s=0;o<r;o++,s+=2){let a=Zt(t.charCodeAt(s)),c=Zt(t.charCodeAt(s+1));if(a===void 0||c===void 0)return U(e);i[o]=a*16+c}return i},xt=(t,e)=>me(Qt(t)?bt(t):Jr(me(t)),e),tn=()=>globalThis?.crypto,Qr=()=>tn()?.subtle??U("crypto.subtle must be defined"),_e=(...t)=>{let e=Ne(t.reduce((r,i)=>r+me(i).length,0)),n=0;return t.forEach(r=>{e.set(r,n),n+=r.length}),e},ei=(t=ie)=>tn().getRandomValues(Ne(t)),$e=BigInt,he=(t,e,n,r="bad number: out of range")=>Jt(t)&&e<=t&&t<n?t:U(r),h=(t,e=J)=>{let n=t%e;return n>=0n?n:e+n};var nn=(t,e)=>{(t===0n||e<=0n)&&U("no inverse n="+t+" mod="+e);let n=h(t,e),r=e,i=0n,o=1n,s=1n,a=0n;for(;n!==0n;){let c=r/n,u=r%n,f=i-s*c,d=o-a*c;r=n,n=u,i=s,o=a,s=f,a=d}return r===1n?h(i,e):U("no inverse")};var qt=t=>t instanceof Q?t:U("Point expected"),rn=t=>h(h(t*t)*t+Xt),Kt=t=>he(t,0n,J),Pe=t=>he(t,1n,J),ti=t=>he(t,1n,yt),mt=t=>(t&1n)===0n,on=t=>Uint8Array.of(t),ni=t=>on(mt(t)?2:3),ri=t=>{let e=rn(Pe(t)),n=1n;for(let r=e,i=(J+1n)/4n;i>0n;i>>=1n)i&1n&&(n=n*r%J),r=r*r%J;return h(n*n)===e?n:U("sqrt invalid")},R=class R{constructor(e,n,r){y(this,"px");y(this,"py");y(this,"pz");this.px=Kt(e),this.py=Pe(n),this.pz=Kt(r),Object.freeze(this)}static fromBytes(e){me(e);let n,r=e[0],i=e.subarray(1),o=Wt(i,0,ie),s=e.length;if(s===ie+1&&[2,3].includes(r)){let a=ri(o),c=mt(a);mt($e(r))!==c&&(a=h(-a)),n=new R(o,a,1n)}return s===pt+1&&r===4&&(n=new R(o,Wt(i,ie,pt),1n)),n?n.assertValidity():U("bad point: not on curve")}equals(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=qt(e),c=h(n*a),u=h(o*i),f=h(r*a),d=h(s*i);return c===u&&f===d}is0(){return this.equals(X)}negate(){return new R(this.px,h(-this.py),this.pz)}double(){return this.add(this)}add(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=qt(e),c=0n,u=Xt,f=0n,d=0n,g=0n,S=h(u*3n),k=h(n*o),A=h(r*s),O=h(i*a),ne=h(n+r),v=h(o+s);ne=h(ne*v),v=h(k+A),ne=h(ne-v),v=h(n+i);let M=h(o+a);return v=h(v*M),M=h(k+O),v=h(v-M),M=h(r+i),f=h(s+a),M=h(M*f),f=h(A+O),M=h(M-f),g=h(c*v),f=h(S*O),g=h(f+g),f=h(A-g),g=h(A+g),d=h(f*g),A=h(k+k),A=h(A+k),O=h(c*O),v=h(S*v),A=h(A+O),O=h(k-O),O=h(c*O),v=h(v+O),k=h(A*v),d=h(d+k),k=h(M*v),f=h(ne*f),f=h(f-k),k=h(ne*A),g=h(M*g),g=h(g+k),new R(f,d,g)}multiply(e,n=!0){if(!n&&e===0n)return X;if(ti(e),e===1n)return this;if(this.equals(oe))return fi(e).p;let r=X,i=oe;for(let o=this;e>0n;o=o.double(),e>>=1n)e&1n?r=r.add(o):n&&(i=i.add(o));return r}toAffine(){let{px:e,py:n,pz:r}=this;if(this.equals(X))return{x:0n,y:0n};if(r===1n)return{x:e,y:n};let i=nn(r,J);return h(r*i)!==1n&&U("inverse invalid"),{x:h(e*i),y:h(n*i)}}assertValidity(){let{x:e,y:n}=this.toAffine();return Pe(e),Pe(n),h(n*n)===rn(e)?this:U("bad point: not on curve")}toBytes(e=!0){let{x:n,y:r}=this.assertValidity().toAffine(),i=Oe(n);return e?_e(ni(r),i):_e(on(4),i,Oe(r))}static fromAffine(e){let{x:n,y:r}=e;return n===0n&&r===0n?X:new R(n,r,1n)}toHex(e){return gt(this.toBytes(e))}static fromPrivateKey(e){return oe.multiply(oi(e))}static fromHex(e){return R.fromBytes(xt(e))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}toRawBytes(e){return this.toBytes(e)}};y(R,"BASE"),y(R,"ZERO");var Q=R,oe=new Q(Yr,jr,1n),X=new Q(0n,1n,0n);Q.BASE=oe;Q.ZERO=X;var Me=t=>$e("0x"+(gt(t)||"0")),Wt=(t,e,n)=>Me(t.subarray(e,n)),ii=2n**256n,Oe=t=>bt(en(he(t,0n,ii),pt)),oi=t=>{let e=Jt(t)?t:Me(xt(t,ie));return he(e,1n,yt,"private key invalid 3")};var si=t=>{t=xt(t),(t.length<ie+8||t.length>1024)&&U("expected 40-1024b");let e=h(Me(t),yt-1n);return Oe(e+1n)};var ai="SHA-256",wt={hexToBytes:bt,bytesToHex:gt,concatBytes:_e,bytesToNumberBE:Me,numberToBytesBE:Oe,mod:h,invert:nn,hmacSha256Async:async(t,...e)=>{let n=Qr(),r="HMAC",i=await n.importKey("raw",t,{name:r,hash:{name:ai}},!1,["sign"]);return Ne(await n.sign(r,i,_e(...e)))},hmacSha256Sync:void 0,hashToPrivateKey:si,randomBytes:ei};var Fe=8,ci=256,sn=Math.ceil(ci/Fe)+1,ht=2**(Fe-1),ui=()=>{let t=[],e=oe,n=e;for(let r=0;r<sn;r++){n=e,t.push(n);for(let i=1;i<ht;i++)n=n.add(e),t.push(n);e=n.double()}return t},Yt,jt=(t,e)=>{let n=e.negate();return t?n:e},fi=t=>{let e=Yt||(Yt=ui()),n=X,r=oe,i=2**Fe,o=i,s=$e(i-1),a=$e(Fe);for(let c=0;c<sn;c++){let u=Number(t&s);t>>=a,u>ht&&(u-=o,t+=1n);let f=c*ht,d=f,g=f+Math.abs(u)-1,S=c%2!==0,k=u<0;u===0?r=r.add(jt(S,e[d])):n=n.add(jt(k,e[g]))}return{p:n,f:r}};function di(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function an(t){if(!Number.isSafeInteger(t)||t<0)throw new Error("positive integer expected, got "+t)}function ae(t,...e){if(!di(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}function cn(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash should be wrapped by utils.createHasher");an(t.outputLen),an(t.blockLen)}function ce(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function un(t,e){ae(t);let n=e.outputLen;if(t.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function ee(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Re(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function F(t,e){return t<<32-e|t>>>e}function pi(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}function ye(t){return typeof t=="string"&&(t=pi(t)),ae(t),t}var se=class{};function fn(t){let e=r=>t().update(ye(r)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function mi(t,e,n,r){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,n,r);let i=BigInt(32),o=BigInt(4294967295),s=Number(n>>i&o),a=Number(n&o),c=r?4:0,u=r?0:4;t.setUint32(e+c,s,r),t.setUint32(e+u,a,r)}function ln(t,e,n){return t&e^~t&n}function dn(t,e,n){return t&e^t&n^e&n}var He=class extends se{constructor(e,n,r,i){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=i,this.buffer=new Uint8Array(e),this.view=Re(this.buffer)}update(e){ce(this),e=ye(e),ae(e);let{view:n,buffer:r,blockLen:i}=this,o=e.length;for(let s=0;s<o;){let a=Math.min(i-this.pos,o-s);if(a===i){let c=Re(e);for(;i<=o-s;s+=i)this.process(c,s);continue}r.set(e.subarray(s,s+a),this.pos),this.pos+=a,s+=a,this.pos===i&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){ce(this),un(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:i,isLE:o}=this,{pos:s}=this;n[s++]=128,ee(this.buffer.subarray(s)),this.padOffset>i-s&&(this.process(r,0),s=0);for(let d=s;d<i;d++)n[d]=0;mi(r,i-8,BigInt(this.length*8),o),this.process(r,0);let a=Re(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let u=c/4,f=this.get();if(u>f.length)throw new Error("_sha2: outputLen bigger than state");for(let d=0;d<u;d++)a.setUint32(4*d,f[d],o)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:i,finished:o,destroyed:s,pos:a}=this;return e.destroyed=s,e.finished=o,e.length=i,e.pos=a,i%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},V=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var hi=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Z=new Uint32Array(64),De=class extends He{constructor(e=32){super(64,e,8,!1),this.A=V[0]|0,this.B=V[1]|0,this.C=V[2]|0,this.D=V[3]|0,this.E=V[4]|0,this.F=V[5]|0,this.G=V[6]|0,this.H=V[7]|0}get(){let{A:e,B:n,C:r,D:i,E:o,F:s,G:a,H:c}=this;return[e,n,r,i,o,s,a,c]}set(e,n,r,i,o,s,a,c){this.A=e|0,this.B=n|0,this.C=r|0,this.D=i|0,this.E=o|0,this.F=s|0,this.G=a|0,this.H=c|0}process(e,n){for(let d=0;d<16;d++,n+=4)Z[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let g=Z[d-15],S=Z[d-2],k=F(g,7)^F(g,18)^g>>>3,A=F(S,17)^F(S,19)^S>>>10;Z[d]=A+Z[d-7]+k+Z[d-16]|0}let{A:r,B:i,C:o,D:s,E:a,F:c,G:u,H:f}=this;for(let d=0;d<64;d++){let g=F(a,6)^F(a,11)^F(a,25),S=f+g+ln(a,c,u)+hi[d]+Z[d]|0,A=(F(r,2)^F(r,13)^F(r,22))+dn(r,i,o)|0;f=u,u=c,c=a,a=s+S|0,s=o,o=i,i=r,r=S+A|0}r=r+this.A|0,i=i+this.B|0,o=o+this.C|0,s=s+this.D|0,a=a+this.E|0,c=c+this.F|0,u=u+this.G|0,f=f+this.H|0,this.set(r,i,o,s,a,c,u,f)}roundClean(){ee(Z)}destroy(){this.set(0,0,0,0,0,0,0,0),ee(this.buffer)}};var ge=fn(()=>new De);var St=ge;var Le=class extends se{constructor(e,n){super(),this.finished=!1,this.destroyed=!1,cn(e);let r=ye(n);if(this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let i=this.blockLen,o=new Uint8Array(i);o.set(r.length>i?e.create().update(r).digest():r);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),ee(o)}update(e){return ce(this),this.iHash.update(e),this}digestInto(e){ce(this),ae(e,this.outputLen),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:i,destroyed:o,blockLen:s,outputLen:a}=this;return e=e,e.finished=i,e.destroyed=o,e.blockLen=s,e.outputLen=a,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Et=(t,e,n)=>new Le(t,e).update(n).digest();Et.create=(t,e)=>new Le(t,e);var yi=["string","number","bigint","symbol"],gi=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function pn(t){if(t===null)return"null";if(t===void 0)return"undefined";if(t===!0||t===!1)return"boolean";let e=typeof t;if(yi.includes(e))return e;if(e==="function")return"Function";if(Array.isArray(t))return"Array";if(bi(t))return"Buffer";let n=xi(t);return n||"Object"}function bi(t){return t&&t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer.call(null,t)}function xi(t){let e=Object.prototype.toString.call(t).slice(8,-1);if(gi.includes(e))return e}var l=class{constructor(e,n,r){this.major=e,this.majorEncoded=e<<5,this.name=n,this.terminal=r}toString(){return`Type[${this.major}].${this.name}`}compare(e){return this.major<e.major?-1:this.major>e.major?1:0}};l.uint=new l(0,"uint",!0);l.negint=new l(1,"negint",!0);l.bytes=new l(2,"bytes",!0);l.string=new l(3,"string",!0);l.array=new l(4,"array",!1);l.map=new l(5,"map",!1);l.tag=new l(6,"tag",!1);l.float=new l(7,"float",!0);l.false=new l(7,"false",!0);l.true=new l(7,"true",!0);l.null=new l(7,"null",!0);l.undefined=new l(7,"undefined",!0);l.break=new l(7,"break",!0);var m=class{constructor(e,n,r){this.type=e,this.value=n,this.encodedLength=r,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var ue=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",wi=new TextDecoder,Si=new TextEncoder;function ze(t){return ue&&globalThis.Buffer.isBuffer(t)}function kt(t){return t instanceof Uint8Array?ze(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):t:Uint8Array.from(t)}var gn=ue?(t,e,n)=>n-e>64?globalThis.Buffer.from(t.subarray(e,n)).toString("utf8"):hn(t,e,n):(t,e,n)=>n-e>64?wi.decode(t.subarray(e,n)):hn(t,e,n),bn=ue?t=>t.length>64?globalThis.Buffer.from(t):mn(t):t=>t.length>64?Si.encode(t):mn(t),H=t=>Uint8Array.from(t),fe=ue?(t,e,n)=>ze(t)?new Uint8Array(t.subarray(e,n)):t.slice(e,n):(t,e,n)=>t.slice(e,n),xn=ue?(t,e)=>(t=t.map(n=>n instanceof Uint8Array?n:globalThis.Buffer.from(n)),kt(globalThis.Buffer.concat(t,e))):(t,e)=>{let n=new Uint8Array(e),r=0;for(let i of t)r+i.length>n.length&&(i=i.subarray(0,n.length-r)),n.set(i,r),r+=i.length;return n},wn=ue?t=>globalThis.Buffer.allocUnsafe(t):t=>new Uint8Array(t);function Ve(t,e){if(ze(t)&&ze(e))return t.compare(e);for(let n=0;n<t.length;n++)if(t[n]!==e[n])return t[n]<e[n]?-1:1;return 0}function mn(t){let e=[],n=0;for(let r=0;r<t.length;r++){let i=t.charCodeAt(r);i<128?e[n++]=i:i<2048?(e[n++]=i>>6|192,e[n++]=i&63|128):(i&64512)===55296&&r+1<t.length&&(t.charCodeAt(r+1)&64512)===56320?(i=65536+((i&1023)<<10)+(t.charCodeAt(++r)&1023),e[n++]=i>>18|240,e[n++]=i>>12&63|128,e[n++]=i>>6&63|128,e[n++]=i&63|128):(e[n++]=i>>12|224,e[n++]=i>>6&63|128,e[n++]=i&63|128)}return e}function hn(t,e,n){let r=[];for(;e<n;){let i=t[e],o=null,s=i>239?4:i>223?3:i>191?2:1;if(e+s<=n){let a,c,u,f;switch(s){case 1:i<128&&(o=i);break;case 2:a=t[e+1],(a&192)===128&&(f=(i&31)<<6|a&63,f>127&&(o=f));break;case 3:a=t[e+1],c=t[e+2],(a&192)===128&&(c&192)===128&&(f=(i&15)<<12|(a&63)<<6|c&63,f>2047&&(f<55296||f>57343)&&(o=f));break;case 4:a=t[e+1],c=t[e+2],u=t[e+3],(a&192)===128&&(c&192)===128&&(u&192)===128&&(f=(i&15)<<18|(a&63)<<12|(c&63)<<6|u&63,f>65535&&f<1114112&&(o=f))}}o===null?(o=65533,s=1):o>65535&&(o-=65536,r.push(o>>>10&1023|55296),o=56320|o&1023),r.push(o),e+=s}return Ei(r)}var yn=4096;function Ei(t){let e=t.length;if(e<=yn)return String.fromCharCode.apply(String,t);let n="",r=0;for(;r<e;)n+=String.fromCharCode.apply(String,t.slice(r,r+=yn));return n}var ki=256,be=class{constructor(e=ki){this.chunkSize=e,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(e){let n=this.chunks[this.chunks.length-1];if(this.cursor+e.length<=this.maxCursor+1){let i=n.length-(this.maxCursor-this.cursor)-1;n.set(e,i)}else{if(n){let i=n.length-(this.maxCursor-this.cursor)-1;i<n.length&&(this.chunks[this.chunks.length-1]=n.subarray(0,i),this.maxCursor=this.cursor-1)}e.length<64&&e.length<this.chunkSize?(n=wn(this.chunkSize),this.chunks.push(n),this.maxCursor+=n.length,this._initReuseChunk===null&&(this._initReuseChunk=n),n.set(e,0)):(this.chunks.push(e),this.maxCursor+=e.length)}this.cursor+=e.length}toBytes(e=!1){let n;if(this.chunks.length===1){let r=this.chunks[0];e&&this.cursor>r.length/2?(n=this.cursor===r.length?r:r.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):n=fe(r,0,this.cursor)}else n=xn(this.chunks,this.cursor);return e&&this.reset(),n}};var b="CBOR decode error:",Tt="CBOR encode error:",xe=[];xe[23]=1;xe[24]=2;xe[25]=3;xe[26]=5;xe[27]=9;function G(t,e,n){if(t.length-e<n)throw new Error(`${b} not enough data for type`)}var E=[24,256,65536,4294967296,BigInt("18446744073709551616")];function C(t,e,n){G(t,e,1);let r=t[e];if(n.strict===!0&&r<E[0])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function I(t,e,n){G(t,e,2);let r=t[e]<<8|t[e+1];if(n.strict===!0&&r<E[1])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function B(t,e,n){G(t,e,4);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3];if(n.strict===!0&&r<E[2])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function P(t,e,n){G(t,e,8);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3],i=t[e+4]*16777216+(t[e+5]<<16)+(t[e+6]<<8)+t[e+7],o=(BigInt(r)<<BigInt(32))+BigInt(i);if(n.strict===!0&&o<E[3])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);if(o<=Number.MAX_SAFE_INTEGER)return Number(o);if(n.allowBigInt===!0)return o;throw new Error(`${b} integers outside of the safe integer range are not supported`)}function Sn(t,e,n,r){return new m(l.uint,C(t,e+1,r),2)}function En(t,e,n,r){return new m(l.uint,I(t,e+1,r),3)}function kn(t,e,n,r){return new m(l.uint,B(t,e+1,r),5)}function Tn(t,e,n,r){return new m(l.uint,P(t,e+1,r),9)}function _(t,e){return T(t,0,e.value)}function T(t,e,n){if(n<E[0]){let r=Number(n);t.push([e|r])}else if(n<E[1]){let r=Number(n);t.push([e|24,r])}else if(n<E[2]){let r=Number(n);t.push([e|25,r>>>8,r&255])}else if(n<E[3]){let r=Number(n);t.push([e|26,r>>>24&255,r>>>16&255,r>>>8&255,r&255])}else{let r=BigInt(n);if(r<E[4]){let i=[e|27,0,0,0,0,0,0,0],o=Number(r&BigInt(4294967295)),s=Number(r>>BigInt(32)&BigInt(4294967295));i[8]=o&255,o=o>>8,i[7]=o&255,o=o>>8,i[6]=o&255,o=o>>8,i[5]=o&255,i[4]=s&255,s=s>>8,i[3]=s&255,s=s>>8,i[2]=s&255,s=s>>8,i[1]=s&255,t.push(i)}else throw new Error(`${b} encountered BigInt larger than allowable range`)}}_.encodedSize=function(e){return T.encodedSize(e.value)};T.encodedSize=function(e){return e<E[0]?1:e<E[1]?2:e<E[2]?3:e<E[3]?5:9};_.compareTokens=function(e,n){return e.value<n.value?-1:e.value>n.value?1:0};function An(t,e,n,r){return new m(l.negint,-1-C(t,e+1,r),2)}function vn(t,e,n,r){return new m(l.negint,-1-I(t,e+1,r),3)}function Un(t,e,n,r){return new m(l.negint,-1-B(t,e+1,r),5)}var At=BigInt(-1),Cn=BigInt(1);function In(t,e,n,r){let i=P(t,e+1,r);if(typeof i!="bigint"){let o=-1-i;if(o>=Number.MIN_SAFE_INTEGER)return new m(l.negint,o,9)}if(r.allowBigInt!==!0)throw new Error(`${b} integers outside of the safe integer range are not supported`);return new m(l.negint,At-BigInt(i),9)}function Ge(t,e){let n=e.value,r=typeof n=="bigint"?n*At-Cn:n*-1-1;T(t,e.type.majorEncoded,r)}Ge.encodedSize=function(e){let n=e.value,r=typeof n=="bigint"?n*At-Cn:n*-1-1;return r<E[0]?1:r<E[1]?2:r<E[2]?3:r<E[3]?5:9};Ge.compareTokens=function(e,n){return e.value<n.value?1:e.value>n.value?-1:0};function we(t,e,n,r){G(t,e,n+r);let i=fe(t,e+n,e+n+r);return new m(l.bytes,i,n+r)}function Bn(t,e,n,r){return we(t,e,1,n)}function Pn(t,e,n,r){return we(t,e,2,C(t,e+1,r))}function _n(t,e,n,r){return we(t,e,3,I(t,e+1,r))}function $n(t,e,n,r){return we(t,e,5,B(t,e+1,r))}function On(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer bytes lengths not supported`);return we(t,e,9,i)}function Ze(t){return t.encodedBytes===void 0&&(t.encodedBytes=t.type===l.string?bn(t.value):t.value),t.encodedBytes}function le(t,e){let n=Ze(e);T(t,e.type.majorEncoded,n.length),t.push(n)}le.encodedSize=function(e){let n=Ze(e);return T.encodedSize(n.length)+n.length};le.compareTokens=function(e,n){return Ai(Ze(e),Ze(n))};function Ai(t,e){return t.length<e.length?-1:t.length>e.length?1:Ve(t,e)}function Se(t,e,n,r,i){let o=n+r;G(t,e,o);let s=new m(l.string,gn(t,e+n,e+o),o);return i.retainStringBytes===!0&&(s.byteValue=fe(t,e+n,e+o)),s}function Fn(t,e,n,r){return Se(t,e,1,n,r)}function Nn(t,e,n,r){return Se(t,e,2,C(t,e+1,r),r)}function Mn(t,e,n,r){return Se(t,e,3,I(t,e+1,r),r)}function Rn(t,e,n,r){return Se(t,e,5,B(t,e+1,r),r)}function Hn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer string lengths not supported`);return Se(t,e,9,i,r)}var Dn=le;function de(t,e,n,r){return new m(l.array,r,n)}function Ln(t,e,n,r){return de(t,e,1,n)}function zn(t,e,n,r){return de(t,e,2,C(t,e+1,r))}function Vn(t,e,n,r){return de(t,e,3,I(t,e+1,r))}function Gn(t,e,n,r){return de(t,e,5,B(t,e+1,r))}function Zn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer array lengths not supported`);return de(t,e,9,i)}function qn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return de(t,e,1,1/0)}function qe(t,e){T(t,l.array.majorEncoded,e.value)}qe.compareTokens=_.compareTokens;qe.encodedSize=function(e){return T.encodedSize(e.value)};function pe(t,e,n,r){return new m(l.map,r,n)}function Kn(t,e,n,r){return pe(t,e,1,n)}function Wn(t,e,n,r){return pe(t,e,2,C(t,e+1,r))}function Yn(t,e,n,r){return pe(t,e,3,I(t,e+1,r))}function jn(t,e,n,r){return pe(t,e,5,B(t,e+1,r))}function Xn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer map lengths not supported`);return pe(t,e,9,i)}function Jn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return pe(t,e,1,1/0)}function Ke(t,e){T(t,l.map.majorEncoded,e.value)}Ke.compareTokens=_.compareTokens;Ke.encodedSize=function(e){return T.encodedSize(e.value)};function Qn(t,e,n,r){return new m(l.tag,n,1)}function er(t,e,n,r){return new m(l.tag,C(t,e+1,r),2)}function tr(t,e,n,r){return new m(l.tag,I(t,e+1,r),3)}function nr(t,e,n,r){return new m(l.tag,B(t,e+1,r),5)}function rr(t,e,n,r){return new m(l.tag,P(t,e+1,r),9)}function We(t,e){T(t,l.tag.majorEncoded,e.value)}We.compareTokens=_.compareTokens;We.encodedSize=function(e){return T.encodedSize(e.value)};var Pi=20,_i=21,$i=22,Oi=23;function ir(t,e,n,r){if(r.allowUndefined===!1)throw new Error(`${b} undefined values are not supported`);return r.coerceUndefinedToNull===!0?new m(l.null,null,1):new m(l.undefined,void 0,1)}function or(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return new m(l.break,void 0,1)}function vt(t,e,n){if(n){if(n.allowNaN===!1&&Number.isNaN(t))throw new Error(`${b} NaN values are not supported`);if(n.allowInfinity===!1&&(t===1/0||t===-1/0))throw new Error(`${b} Infinity values are not supported`)}return new m(l.float,t,e)}function sr(t,e,n,r){return vt(Ut(t,e+1),3,r)}function ar(t,e,n,r){return vt(Ct(t,e+1),5,r)}function cr(t,e,n,r){return vt(dr(t,e+1),9,r)}function Ye(t,e,n){let r=e.value;if(r===!1)t.push([l.float.majorEncoded|Pi]);else if(r===!0)t.push([l.float.majorEncoded|_i]);else if(r===null)t.push([l.float.majorEncoded|$i]);else if(r===void 0)t.push([l.float.majorEncoded|Oi]);else{let i,o=!1;(!n||n.float64!==!0)&&(fr(r),i=Ut(N,1),r===i||Number.isNaN(r)?(N[0]=249,t.push(N.slice(0,3)),o=!0):(lr(r),i=Ct(N,1),r===i&&(N[0]=250,t.push(N.slice(0,5)),o=!0))),o||(Fi(r),i=dr(N,1),N[0]=251,t.push(N.slice(0,9)))}}Ye.encodedSize=function(e,n){let r=e.value;if(r===!1||r===!0||r===null||r===void 0)return 1;if(!n||n.float64!==!0){fr(r);let i=Ut(N,1);if(r===i||Number.isNaN(r))return 3;if(lr(r),i=Ct(N,1),r===i)return 5}return 9};var ur=new ArrayBuffer(9),$=new DataView(ur,1),N=new Uint8Array(ur,0);function fr(t){if(t===1/0)$.setUint16(0,31744,!1);else if(t===-1/0)$.setUint16(0,64512,!1);else if(Number.isNaN(t))$.setUint16(0,32256,!1);else{$.setFloat32(0,t);let e=$.getUint32(0),n=(e&2139095040)>>23,r=e&8388607;if(n===255)$.setUint16(0,31744,!1);else if(n===0)$.setUint16(0,(t&2147483648)>>16|r>>13,!1);else{let i=n-127;i<-24?$.setUint16(0,0):i<-14?$.setUint16(0,(e&2147483648)>>16|1<<24+i,!1):$.setUint16(0,(e&2147483648)>>16|i+15<<10|r>>13,!1)}}}function Ut(t,e){if(t.length-e<2)throw new Error(`${b} not enough data for float16`);let n=(t[e]<<8)+t[e+1];if(n===31744)return 1/0;if(n===64512)return-1/0;if(n===32256)return NaN;let r=n>>10&31,i=n&1023,o;return r===0?o=i*2**-24:r!==31?o=(i+1024)*2**(r-25):o=i===0?1/0:NaN,n&32768?-o:o}function lr(t){$.setFloat32(0,t,!1)}function Ct(t,e){if(t.length-e<4)throw new Error(`${b} not enough data for float32`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,4).getFloat32(0,!1)}function Fi(t){$.setFloat64(0,t,!1)}function dr(t,e){if(t.length-e<8)throw new Error(`${b} not enough data for float64`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,8).getFloat64(0,!1)}Ye.compareTokens=_.compareTokens;function x(t,e,n){throw new Error(`${b} encountered invalid minor (${n}) for major ${t[e]>>>5}`)}function je(t){return()=>{throw new Error(`${b} ${t}`)}}var p=[];for(let t=0;t<=23;t++)p[t]=x;p[24]=Sn;p[25]=En;p[26]=kn;p[27]=Tn;p[28]=x;p[29]=x;p[30]=x;p[31]=x;for(let t=32;t<=55;t++)p[t]=x;p[56]=An;p[57]=vn;p[58]=Un;p[59]=In;p[60]=x;p[61]=x;p[62]=x;p[63]=x;for(let t=64;t<=87;t++)p[t]=Bn;p[88]=Pn;p[89]=_n;p[90]=$n;p[91]=On;p[92]=x;p[93]=x;p[94]=x;p[95]=je("indefinite length bytes/strings are not supported");for(let t=96;t<=119;t++)p[t]=Fn;p[120]=Nn;p[121]=Mn;p[122]=Rn;p[123]=Hn;p[124]=x;p[125]=x;p[126]=x;p[127]=je("indefinite length bytes/strings are not supported");for(let t=128;t<=151;t++)p[t]=Ln;p[152]=zn;p[153]=Vn;p[154]=Gn;p[155]=Zn;p[156]=x;p[157]=x;p[158]=x;p[159]=qn;for(let t=160;t<=183;t++)p[t]=Kn;p[184]=Wn;p[185]=Yn;p[186]=jn;p[187]=Xn;p[188]=x;p[189]=x;p[190]=x;p[191]=Jn;for(let t=192;t<=215;t++)p[t]=Qn;p[216]=er;p[217]=tr;p[218]=nr;p[219]=rr;p[220]=x;p[221]=x;p[222]=x;p[223]=x;for(let t=224;t<=243;t++)p[t]=je("simple values are not supported");p[244]=x;p[245]=x;p[246]=x;p[247]=ir;p[248]=je("simple values are not supported");p[249]=sr;p[250]=ar;p[251]=cr;p[252]=x;p[253]=x;p[254]=x;p[255]=or;var D=[];for(let t=0;t<24;t++)D[t]=new m(l.uint,t,1);for(let t=-1;t>=-24;t--)D[31-t]=new m(l.negint,t,1);D[64]=new m(l.bytes,new Uint8Array(0),1);D[96]=new m(l.string,"",1);D[128]=new m(l.array,0,1);D[160]=new m(l.map,0,1);D[244]=new m(l.false,!1,1);D[245]=new m(l.true,!0,1);D[246]=new m(l.null,null,1);function pr(t){switch(t.type){case l.false:return H([244]);case l.true:return H([245]);case l.null:return H([246]);case l.bytes:return t.value.length?void 0:H([64]);case l.string:return t.value===""?H([96]):void 0;case l.array:return t.value===0?H([128]):void 0;case l.map:return t.value===0?H([160]):void 0;case l.uint:return t.value<24?H([Number(t.value)]):void 0;case l.negint:if(t.value>=-24)return H([31-Number(t.value)])}}var hr=Object.freeze({float64:!0,mapSorter:Di,quickEncodeToken:pr});function Mi(){let t=[];return t[l.uint.major]=_,t[l.negint.major]=Ge,t[l.bytes.major]=le,t[l.string.major]=Dn,t[l.array.major]=qe,t[l.map.major]=Ke,t[l.tag.major]=We,t[l.float.major]=Ye,t}var Ri=Mi(),It=new be,Je=class t{constructor(e,n){this.obj=e,this.parent=n}includes(e){let n=this;do if(n.obj===e)return!0;while(n=n.parent);return!1}static createCheck(e,n){if(e&&e.includes(n))throw new Error(`${Tt} object contains circular references`);return new t(n,e)}},q={null:new m(l.null,null),undefined:new m(l.undefined,void 0),true:new m(l.true,!0),false:new m(l.false,!1),emptyArray:new m(l.array,0),emptyMap:new m(l.map,0)},K={number(t,e,n,r){return!Number.isInteger(t)||!Number.isSafeInteger(t)?new m(l.float,t):t>=0?new m(l.uint,t):new m(l.negint,t)},bigint(t,e,n,r){return t>=BigInt(0)?new m(l.uint,t):new m(l.negint,t)},Uint8Array(t,e,n,r){return new m(l.bytes,t)},string(t,e,n,r){return new m(l.string,t)},boolean(t,e,n,r){return t?q.true:q.false},null(t,e,n,r){return q.null},undefined(t,e,n,r){return q.undefined},ArrayBuffer(t,e,n,r){return new m(l.bytes,new Uint8Array(t))},DataView(t,e,n,r){return new m(l.bytes,new Uint8Array(t.buffer,t.byteOffset,t.byteLength))},Array(t,e,n,r){if(!t.length)return n.addBreakTokens===!0?[q.emptyArray,new m(l.break)]:q.emptyArray;r=Je.createCheck(r,t);let i=[],o=0;for(let s of t)i[o++]=Xe(s,n,r);return n.addBreakTokens?[new m(l.array,t.length),i,new m(l.break)]:[new m(l.array,t.length),i]},Object(t,e,n,r){let i=e!=="Object",o=i?t.keys():Object.keys(t),s=i?t.size:o.length;if(!s)return n.addBreakTokens===!0?[q.emptyMap,new m(l.break)]:q.emptyMap;r=Je.createCheck(r,t);let a=[],c=0;for(let u of o)a[c++]=[Xe(u,n,r),Xe(i?t.get(u):t[u],n,r)];return Hi(a,n),n.addBreakTokens?[new m(l.map,s),a,new m(l.break)]:[new m(l.map,s),a]}};K.Map=K.Object;K.Buffer=K.Uint8Array;for(let t of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))K[`${t}Array`]=K.DataView;function Xe(t,e={},n){let r=pn(t),i=e&&e.typeEncoders&&e.typeEncoders[r]||K[r];if(typeof i=="function"){let s=i(t,r,e,n);if(s!=null)return s}let o=K[r];if(!o)throw new Error(`${Tt} unsupported type: ${r}`);return o(t,r,e,n)}function Hi(t,e){e.mapSorter&&t.sort(e.mapSorter)}function Di(t,e){if(t[0]instanceof m&&e[0]instanceof m){let n=t[0],r=e[0];return n._keyBytes||(n._keyBytes=mr(n.value)),r._keyBytes||(r._keyBytes=mr(r.value)),Ve(n._keyBytes,r._keyBytes)}throw new Error("rfc8949MapSorter: complex key types are not supported yet")}function mr(t){return Li(t,Ri,hr)}function yr(t,e,n,r){if(Array.isArray(e))for(let i of e)yr(t,i,n,r);else n[e.type.major](t,e,r)}function Li(t,e,n){let r=Xe(t,n);if(!Array.isArray(r)&&n.quickEncodeToken){let i=n.quickEncodeToken(r);if(i)return i;let o=e[r.type.major];if(o.encodedSize){let s=o.encodedSize(r,n),a=new be(s);if(o(a,r,n),a.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${r} was wrong`);return kt(a.chunks[0])}}return It.reset(),yr(It,r,e,n),It.toBytes(!0)}wt.hmacSha256Sync=(t,...e)=>Et(St,t,wt.concatBytes(...e));var Gi=32;function Qe(t){if(t.length===0)return new Uint8Array(Gi);let e=t.length,n=e*4,r=t.reduce((c,u)=>c+u.length,0),i=4+n+r,o=new Uint8Array(i),s=new DataView(o.buffer,o.byteOffset,o.byteLength);s.setUint32(0,e,!0);let a=4;for(let c of t)s.setUint32(a,c.length,!0),a+=4;for(let c of t)o.set(c,a),a+=c.length;return St(o)}var Bt={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,entityRef:4,f32:4,vec2:8,vec3:12,quat:16,enum:1},Ee={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,f32:4,flags8:1,flags16:2,flags32:4,vec2:8,vec3:12,quat:16};var gr=new Set(["bool","uint8","int8","uint16","int16","uint32","int32","f32"]);function $t(t){let e=t.match(/^(\\w+)\\[(\\d+)\\]$/);if(!e||!e[1]||!e[2])return null;let n=e[1],r=parseInt(e[2],10);if(!gr.has(n))throw new Error(`Invalid array element type \'${n}\': arrays only support primitive scalar types (bool, uint8, int8, uint16, int16, uint32, int32, f32)`);return{elementType:n,length:r}}function br(t){let e=$t(t);if(e){let r=Ee[e.elementType];if(r===void 0)throw new Error(`Unknown element type: ${e.elementType}`);return r*e.length}let n=Ee[t];if(n===void 0)throw new Error(`Unknown type: ${t}`);return n}function Zi(t){let e=[],n=[],r=new Map,i=new Map,o=0;for(let s of t.controls){let a=br(s.type),c=$t(s.type),u={name:s.name,type:s.type,size:a,offset:0,options:s.options};c&&(u.arrayLength=c.length,u.arrayElementType=c.elementType);let f={name:s.name,index:o++,field:u,hint:s.hint,retain:s.retain==="always"?"always":"tick"};e.push(f),r.set(s.name,f)}for(let s of t.commands){let a=[],c=0;for(let f of s.args){let d=br(f.type),g=$t(f.type),S={name:f.name,type:f.type,size:d,offset:c};g&&(S.arrayLength=g.length,S.arrayElementType=g.elementType),a.push(S),c+=d}let u={name:s.name,index:o++,args:a,totalSize:c};n.push(u),i.set(s.name,u)}return{controls:e,commands:n,controlByName:r,commandByName:i}}function Pt(t,e,n,r){switch(n){case"bool":t.setUint8(e,r?1:0);break;case"uint8":case"flags8":t.setUint8(e,r);break;case"int8":t.setInt8(e,r);break;case"uint16":case"flags16":t.setUint16(e,r,!0);break;case"int16":t.setInt16(e,r,!0);break;case"uint32":case"flags32":t.setUint32(e,r,!0);break;case"int32":t.setInt32(e,r,!0);break;case"f32":t.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function _t(t,e,n){switch(n){case"bool":return t.getUint8(e)!==0;case"uint8":case"flags8":return t.getUint8(e);case"int8":return t.getInt8(e);case"uint16":case"flags16":return t.getUint16(e,!0);case"int16":return t.getInt16(e,!0);case"uint32":case"flags32":return t.getUint32(e,!0);case"int32":return t.getInt32(e,!0);case"f32":return t.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}var et=class t{constructor(e){y(this,"schema");y(this,"fields",new Map);y(this,"commandList",[]);this.schema=e}setControl(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);let i=new Uint8Array(r.field.size),o=new DataView(i.buffer);this.encodeValue(o,0,r.field,n),this.fields.set(r.index,i)}setFlags(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(!r.field.options)throw new Error(`Control ${e} is not a flags type`);let i=0;for(let a=0;a<r.field.options.length;a++){let c=r.field.options[a];n[c]&&(i|=1<<a)}let o=new Uint8Array(r.field.size),s=new DataView(o.buffer);Pt(s,0,r.field.type,i),this.fields.set(r.index,o)}setVec2(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let i=new Uint8Array(8),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),this.fields.set(r.index,i)}setVec3(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let i=new Uint8Array(12),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),this.fields.set(r.index,i)}setQuat(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let i=new Uint8Array(16),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),o.setFloat32(12,n[3],!0),this.fields.set(r.index,i)}getControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);let r=this.fields.get(n.index);if(!r)return this.getDefaultValue(n.field);let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return this.decodeValue(i,0,n.field)}getFlags(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(!n.field.options)throw new Error(`Control ${e} is not a flags type`);let r=this.fields.get(n.index),i=0;if(r){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);i=_t(s,0,n.field.type)}let o={};for(let s=0;s<n.field.options.length;s++)o[n.field.options[s]]=(i&1<<s)!==0;return o}getVec2(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let r=this.fields.get(n.index);if(!r)return[0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0)]}getVec3(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let r=this.fields.get(n.index);if(!r)return[0,0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0)]}getQuat(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let r=this.fields.get(n.index);if(!r)return[0,0,0,1];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0),i.getFloat32(12,!0)]}hasControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);return this.fields.has(n.index)}addCommand(e,n){let r=this.schema.commandByName.get(e);if(!r)throw new Error(`Unknown command: ${e}`);let i=new Uint8Array(r.totalSize),o=new DataView(i.buffer);for(let s of r.args){let a=n[s.name];if(a===void 0)throw new Error(`Missing required argument: ${s.name}`);this.encodeValue(o,s.offset,s,a)}this.commandList.push({index:r.index,data:i})}getCommands(){let e=[];for(let{index:n,data:r}of this.commandList){let i=this.schema.commands.find(a=>a.index===n);if(!i)continue;let o=new DataView(r.buffer,r.byteOffset,r.byteLength),s={name:i.name};for(let a of i.args)s[a.name]=this.decodeValue(o,a.offset,a);e.push(s)}return e}encode(){let e=[];for(let[o,s]of this.fields){let a=new Uint8Array(2+s.length);a[0]=o,a[1]=s.length,a.set(s,2),e.push(a)}for(let o of this.commandList){let s=new Uint8Array(2+o.data.length);s[0]=o.index,s[1]=o.data.length,s.set(o.data,2),e.push(s)}let n=e.reduce((o,s)=>o+s.length,0),r=new Uint8Array(n),i=0;for(let o of e)r.set(o,i),i+=o.length;return r}static decode(e,n){let r=new t(e),i=0;for(;i<n.length;){if(i+2>n.length)throw new Error("Truncated TLV field header");let o=n[i],s=n[i+1];if(i+2+s>n.length)throw new Error(`Truncated TLV field data at index ${o}`);let a=n.subarray(i+2,i+2+s);e.controls.find(u=>u.index===o)?r.fields.set(o,new Uint8Array(a)):e.commands.find(f=>f.index===o)&&r.commandList.push({index:o,data:new Uint8Array(a)}),i+=2+s}return r}clear(){this.fields.clear(),this.commandList.length=0}encodeValue(e,n,r,i){if(r.type==="vec2"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0)}else if(r.type==="vec3"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0)}else if(r.type==="quat"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0),e.setFloat32(n+12,o[3],!0)}else if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let o=i,s=Ee[r.arrayElementType];for(let a=0;a<Math.min(o.length,r.arrayLength);a++)Pt(e,n+a*s,r.arrayElementType,o[a])}else Pt(e,n,r.type,i)}decodeValue(e,n,r){if(r.type==="vec2")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0)];if(r.type==="vec3")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)];if(r.type==="quat")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0),e.getFloat32(n+12,!0)];if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let i=[],o=Ee[r.arrayElementType];for(let s=0;s<r.arrayLength;s++)i.push(_t(e,n+s*o,r.arrayElementType));return i}else return _t(e,n,r.type)}getDefaultValue(e){return e.type==="vec2"?[0,0]:e.type==="vec3"?[0,0,0]:e.type==="quat"?[0,0,0,1]:e.arrayLength!==void 0?new Array(e.arrayLength).fill(0):e.type==="bool"?!1:0}};function xr(t){let e=Zi(t);return{create(){return new et(e)},decode(n){return et.decode(e,n)},get schema(){return e}}}function wr(t,e){return((e&65535)<<16|t&65535)>>>0}function ke(t){return t&65535}function ji(t){return t>>>16&65535}function W(t){return t.subarray(8,40)}function Ot(t,e){if(e.length!==32)throw new Error(`stateId must be ${32} bytes, got ${e.length}`);t.set(e,8)}function Sr(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint32(40,e,!0)}function rt(t,e,n){let r=new Uint8Array(68);return r.set(t,0),new DataView(r.buffer).setUint32(32,e,!0),r.set(n,36),ge(r)}function it(t,e,n){return rt(t,e,Qe(n))}function Ft(t){return ge(t)}function Er(t,e,n){let r=new ArrayBuffer(t.totalSize),i=new Uint8Array(r),o=new DataView(r);if(o.setUint32(0,1297367376,!0),o.setUint16(4,0,!0),i[6]=n??0,i[7]=0,e){let a=Ft(e);i.set(a,8)}let s=t.freeStackOffset;o.setUint16(s,t.maxEntities,!0);for(let a=0;a<t.maxEntities;a++){let c=t.freeStackOffset+2+a*2;o.setUint16(c,a,!0)}return i}function kr(t,e){return t.entityTableOffset+e*t.entityRecordSize}function Nt(t,e,n){let r=kr(t,n);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(r,!0)}function Xi(t){return new DataView(t.buffer,t.byteOffset,t.byteLength).getUint16(4,!0)}function Ji(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint16(4,e,!0)}function Qi(t,e){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t.freeStackOffset,!0)}function eo(t,e,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t.freeStackOffset,n,!0)}function to(t,e){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Qi(t,e);if(r===0)throw new Error(`No free entity slots available (max: ${t.maxEntities})`);let i=t.freeStackOffset+2+(r-1)*2,o=n.getUint16(i,!0);return eo(t,e,r-1),o}function Tr(t,e){let n=to(t,e),r=Nt(t,e,n);return Ji(e,Xi(e)+1),wr(n,r)}function ot(t,e,n){if(n===4294967295)return!1;let r=ke(n);if(r>=t.maxEntities)return!1;let i=ji(n);return Nt(t,e,r)===i}function Mt(t,e,n){if(n<0)throw new Error("Singleton components do not have component bitmask entries");let r=kr(t,e)+2,i=Math.floor(n/8),o=n%8;return{byteOffset:r+i,bitIndex:o}}function Ar(t,e,n,r){if(!ot(t,e,n))return!1;let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and is always present`);let o=ke(n),{byteOffset:s,bitIndex:a}=Mt(t,o,i.index);return(e[s]&1<<a)!==0}function vr(t,e,n,r){if(!ot(t,e,n)){let c=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${c} is not alive`)}let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and cannot be added to entities`);let o=ke(n),{byteOffset:s,bitIndex:a}=Mt(t,o,i.index);e[s]=(e[s]??0)|1<<a}function Ur(t,e,n,r){let i=ke(e);return n.storageOffset+i*n.size+r.offset}function no(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":return(t[e]??0)!==0;case"uint8":return t[e]??0;case"int8":return r.getInt8(e);case"uint16":return r.getUint16(e,!0);case"int16":return r.getInt16(e,!0);case"uint32":return r.getUint32(e,!0);case"int32":return r.getInt32(e,!0);case"entityRef":return r.getUint32(e,!0);case"f32":return r.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}function Cr(t,e,n,r){let i=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":t[e]=r?1:0;break;case"uint8":t[e]=r&255;break;case"int8":i.setInt8(e,r);break;case"uint16":i.setUint16(e,r,!0);break;case"int16":i.setInt16(e,r,!0);break;case"uint32":i.setUint32(e,r,!0);break;case"int32":i.setInt32(e,r,!0);break;case"entityRef":i.setUint32(e,r,!0);break;case"f32":i.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function ro(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0)}function io(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0)}function oo(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0),r.setFloat32(e+12,n[3],!0)}function Ir(t,e,n,r,i,o){if(!ot(t,e,n))return;let s=t.componentByName.get(r);if(!s)throw new Error(`Unknown component: \'${r}\'`);if(s.isSingleton)throw new Error(`Component \'${r}\' is a singleton; use singleton accessors instead`);if(!nt(t,e,n,s))return;let a=s.fields.find(f=>f.name===i);if(!a)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let c=Ur(t,n,s,a),u=a.type;if(a.arrayLength!==void 0&&a.arrayElementType!==void 0){if(o===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(o<0||o>=a.arrayLength)throw new Error(`Array index ${o} out of bounds for ${r}.${i} (length: ${a.arrayLength})`);let f=Bt[a.arrayElementType];if(f===void 0)throw new Error(`Unknown array element type: ${a.arrayElementType}`);c+=o*f,u=a.arrayElementType}return no(e,c,u)}function w(t,e,n,r,i,o,s){if(!ot(t,e,n)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} is not alive`)}let a=t.componentByName.get(r);if(!a)throw new Error(`Unknown component: \'${r}\'`);if(!nt(t,e,n,a)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} does not have component \'${r}\'`)}let c=a.fields.find(d=>d.name===i);if(!c)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let u=Ur(t,n,a,c),f=c.type;if(c.arrayLength!==void 0&&c.arrayElementType!==void 0){if(s===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(s<0||s>=c.arrayLength)throw new Error(`Array index ${s} out of bounds for ${r}.${i} (length: ${c.arrayLength})`);let d=Bt[c.arrayElementType];if(d===void 0)throw new Error(`Unknown array element type: ${c.arrayElementType}`);u+=s*d,f=c.arrayElementType}Cr(e,u,f,o)}function nt(t,e,n,r){if(r.isSingleton)throw new Error(`Component \'${r.name}\' is a singleton and cannot be queried per entity`);let i=ke(n),{byteOffset:o,bitIndex:s}=Mt(t,i,r.index);return(e[o]&1<<s)!==0}function Br(t,e,n,r){if(n.length===0)throw new Error("Query must include at least one component");let i=[];for(let a of n){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);i.push(c)}let o=[];if(r)for(let a of r){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);o.push(c)}function*s(){for(let a=0;a<t.maxEntities;a++){let c=Nt(t,e,a),u=wr(a,c),f=!0;for(let d of i)if(!nt(t,e,u,d)){f=!1;break}if(f){for(let d of o)if(nt(t,e,u,d)){f=!1;break}f&&(yield u)}}}return s()}function Pr(t,e){if(!t.events||!t.eventByName)throw new Error("Schema has no events");let n=t.eventByName.get(e);if(!n)throw new Error(`Unknown event: \'${e}\'`);return n}function _r(t,e,n){let r=Pr(t,n);new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(r.storageOffset,0,!0)}function $r(t,e,n,r){let i=Pr(t,n),o=new DataView(e.buffer,e.byteOffset,e.byteLength),s=o.getUint16(i.storageOffset,!0);if(s>=i.maxEvents)return!1;let a=i.storageOffset+2+s*i.recordSize;for(let c of i.fields){let u=r[c.name];if(u===void 0)throw new Error(`Missing required field \'${c.name}\' for event \'${n}\'`);let f=a+c.offset;c.type==="vec2"?ro(e,f,u):c.type==="vec3"?io(e,f,u):c.type==="quat"?oo(e,f,u):Cr(e,f,c.type,u)}return o.setUint16(i.storageOffset,s+1,!0),!0}function st(t){if(t.length!==32)throw new Error(`stateId must be ${32} bytes, got ${t.length}`);return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}var at=class{constructor(e={}){y(this,"cache");y(this,"maxSize");this.cache=new Map,this.maxSize=e.maxSize??100}store(e){let n=W(e),r=st(n);if(this.cache.size>=this.maxSize&&!this.cache.has(r)){let i=this.cache.keys().next().value;i!==void 0&&this.cache.delete(i)}this.cache.set(r,e.slice())}getByStateId(e){let n=st(e),r=this.cache.get(n);return r?r.slice():void 0}getCached(e,n,r){let i=W(e),o=rt(i,n,r);return this.getByStateId(o)}has(e){let n=st(e);return this.cache.has(n)}hasCached(e,n,r){let i=W(e),o=rt(i,n,r);return this.has(o)}delete(e){let n=st(e);return this.cache.delete(n)}clear(){this.cache.clear()}get size(){return this.cache.size}keys(){return this.cache.keys()}};var Te=65536,ct=class{constructor(e){y(this,"memory",null);y(this,"plugins",[]);y(this,"options");y(this,"stateSize",null);y(this,"statePtr",null);y(this,"heapPos",0);y(this,"arenaResetMark",0);y(this,"inputCodec",null);y(this,"playerEntities",new Map);y(this,"hostAlloc",(e,n)=>{let r=this.memory;if(!r)throw new Error("Memory not initialized for host_alloc");let i=this.heapPos+n-1&~(n-1),o=i+e,s=r.buffer.byteLength;if(o>s){let c=Math.ceil((o-s)/Te);if(this.options.debug){let u=(s+c*Te)/1048576;console.warn(`[mt] WASM memory grew to ${u.toFixed(1)}MB`)}r.grow(c)}return new Uint8Array(r.buffer).fill(0,i,o),this.heapPos=o,i});this.options=e}markArenaReset(){this.arenaResetMark=this.heapPos}resetArena(){this.heapPos=this.arenaResetMark}async init(){if(this.stateSize=this.options.stateSchema.totalSize,!this.stateSize)throw new Error("State schema total size is required");this.options.inputSchema&&(this.inputCodec=xr(this.options.inputSchema));let e=4*1024*1024,n=this.options.plugins??(this.options.moduleBytes?[{name:"main",wasmBytes:this.options.moduleBytes,reservedBytes:e}]:[]);for(let c of n)if(typeof c.reservedBytes!="number"||c.reservedBytes<=0)throw new Error(`Plugin "${c.name}" requires reservedBytes > 0`);let r=n.reduce((c,u)=>{let f=Math.ceil(u.reservedBytes/Te)*Te;return c+f},0),i=1024*1024*5,o=this.stateSize+i+r,s=Math.ceil(o/Te);this.memory=new WebAssembly.Memory({initial:s}),this.heapPos=r;let a={env:{memory:this.memory,host_alloc:this.hostAlloc,abort:()=>{throw new Error("WASM abort called")}}};for(let c of n){let u=c.wasmBytes.slice().buffer,f=await WebAssembly.compile(u),d=await WebAssembly.instantiate(f,a);if(typeof d.exports.apply!="function")throw new Error(`Plugin "${c.name}" missing required apply() export`);this.plugins.push({name:c.name,instance:d,exports:d.exports})}return this.statePtr=this.hostAlloc(this.stateSize,8),this.markArenaReset(),this}findPlayerEntity(e,n){let r=this.options.stateSchema,i=this.playerEntities.get(n);if(i!==void 0){if(Ar(r,e,i,"Player"))return i;this.playerEntities.delete(n)}for(let o of Br(r,e,["Player"]))if(Ir(r,e,o,"Player","index")===n)return this.playerEntities.set(n,o),o}spawnPlayerEntity(e,n){let r=this.options.stateSchema,i=Tr(r,e);return vr(r,e,i,"Player"),w(r,e,i,"Player","index",n),this.playerEntities.set(n,i),i}findOrSpawnPlayerEntity(e,n){let r=this.findPlayerEntity(e,n);return r!==void 0?r:this.spawnPlayerEntity(e,n)}resetPlayerControls(e,n){let r=this.options.stateSchema;if(this.inputCodec)for(let i of this.inputCodec.schema.controls){if(i.retain==="always")continue;let o=i.field.type,s=i.name;if(o==="vec2")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0);else if(o==="vec3")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0);else if(o==="quat")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0),w(r,e,n,"Player",`${s}_w`,1);else if(o.startsWith("flags"))w(r,e,n,"Player",s,0);else if(o.includes("[")){let a=o.match(/\\[(\\d+)\\]/);if(a?.[1]){let c=parseInt(a[1],10);for(let u=0;u<c;u++)w(r,e,n,"Player",s,0,u)}}else w(r,e,n,"Player",s,o==="bool"?!1:0)}}writeControlsToPlayer(e,n,r){let i=this.options.stateSchema,o=this.options.inputSchema;if(o)for(let s of o.controls){if(!r.hasControl(s.name))continue;let a=s.type,c=s.name;if(a==="vec2"){let[u,f]=r.getVec2(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f)}else if(a==="vec3"){let[u,f,d]=r.getVec3(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d)}else if(a==="quat"){let[u,f,d,g]=r.getQuat(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d),w(i,e,n,"Player",`${c}_w`,g)}else if(a.startsWith("flags")){let u=r.getControl(c);w(i,e,n,"Player",c,u)}else if(a.includes("[")){let u=r.getControl(c);for(let f=0;f<u.length;f++)w(i,e,n,"Player",c,u[f],f)}else{let u=r.getControl(c);w(i,e,n,"Player",c,u)}}}pushCommands(e,n,r){let i=this.options.stateSchema,o=r.getCommands();for(let s of o){let a=s.name.charAt(0).toUpperCase()+s.name.slice(1)+"Command",c={player_index:n};for(let[u,f]of Object.entries(s))u!=="name"&&(Array.isArray(f)?f.length===2?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1]):f.length===3?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2]):f.length===4&&(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2],c[`${u}_w`]=f[3]):c[u]=f);$r(i,e,a,c)}}clearCommandEvents(e){let n=this.options.stateSchema,r=this.options.inputSchema;if(!(!r||!n.events))for(let i of r.commands){let o=i.name.charAt(0).toUpperCase()+i.name.slice(1)+"Command";n.eventByName?.has(o)&&_r(n,e,o)}}decodePayloadsToState(e,n){if(this.inputCodec)for(let r=0;r<n.length;r++){let i=n[r];if(i===void 0)continue;let o=this.findOrSpawnPlayerEntity(e,r);if(this.resetPlayerControls(e,o),i.length===0)continue;let s=this.inputCodec.decode(i);this.writeControlsToPlayer(e,o,s),this.pushCommands(e,r,s)}}transition(e,n){let r=this.stateSize;if(r===null||e.length!==r)throw new Error(`State size mismatch: expected ${this.stateSize}, got ${e.length}`);if(this.plugins.length===0||n.length===0)return e.slice();let i=this.memory;if(!i)throw new Error("WASM memory not initialized");let o=this.statePtr;if(o===null||o<0)throw new Error("State pointer not initialized");let s=new Uint8Array(i.buffer);s.set(e,o);let a=this.hostAlloc(32,8),c=W(e);for(let u of n){let f=it(c,u.tick,u.payloads);s=new Uint8Array(i.buffer),s.set(f,a);let d=new Uint8Array(i.buffer,o,r);Sr(d,u.tick),this.clearCommandEvents(d),this.decodePayloadsToState(d,u.payloads);let g=u.sync?1:0;for(let S of this.plugins)S.exports.apply(o,a,g),s=new Uint8Array(i.buffer);c=f}return s.slice(o,o+r)}close(){}getStateSize(){let e=this.stateSize;if(e===null||e<0)throw new Error("State size not initialized");return e}};var Y=class{constructor(e=1e3,n=1024){y(this,"buffer");y(this,"windowMs");y(this,"head",0);y(this,"tail",0);y(this,"size",0);this.windowMs=e,this.buffer=new Float64Array(n)}inc(){let e=performance.now();this.buffer[this.head]=e,this.head=(this.head+1)%this.buffer.length,this.size<this.buffer.length?this.size++:this.tail=(this.tail+1)%this.buffer.length}count(e=performance.now()){let n=e-this.windowMs;for(;this.size>0&&!(this.buffer[this.tail]>=n);)this.tail=(this.tail+1)%this.buffer.length,this.size--;return this.size}rate(e=performance.now()){return this.count(e)*(1e3/this.windowMs)}};function so(t){return("moduleBytes"in t||"plugins"in t)&&"stateSchema"in t}function ao(t,e){for(let n=t.length-1;n>=0;n--)if(e(t[n]))return n;return-1}var ut=class{constructor(e){y(this,"debug");y(this,"stateSchema");y(this,"inputs",null);y(this,"cache");y(this,"executor",null);y(this,"genesisStateId");y(this,"options");y(this,"ticking",!1);y(this,"tickGraceMs");y(this,"tickLag");y(this,"maxBatchSize");y(this,"currentState");y(this,"currentTick");y(this,"currentNodeId");y(this,"prevOnStateUpdateState",null);y(this,"onStateUpdate");y(this,"syncCheckpoint");y(this,"stateHistory",[]);y(this,"maxHistory",64);y(this,"stats");y(this,"loop",()=>{this._loop().catch(co)});y(this,"_loop",async()=>{if(this.ticking)try{let e=await this.tick();if(this.onStateUpdate&&this.currentState!==this.prevOnStateUpdateState&&(this.onStateUpdate(this.currentState),this.prevOnStateUpdateState=this.currentState,this.stats.updates.inc()),e.rolledBack&&this.stats.rollbacks.inc(),e.ticksComputed>0)for(let n=0;n<e.ticksComputed;n++)this.stats.executions.inc()}catch(e){console.error("Error in tick loop:",e)}finally{setTimeout(this.loop,this.tickGraceMs)}});e.log&&(this.inputs=e.log),this.debug=e.debug??!1,this.options=e,this.stateSchema=e.stateSchema,this.cache=e.cache??new at,this.genesisStateId=Ft(e.genesisHash),this.tickGraceMs=e.tickGraceMs??10,this.tickLag=e.tickLag??1,this.maxBatchSize=e.maxBatchSize??200,this.stats={rollbacks:new Y,executions:new Y,updates:new Y,cacheHits:new Y,cacheMisses:new Y},this.currentState=Er(this.stateSchema,e.genesisHash,e.tickRate),this.currentNodeId=null,this.syncCheckpoint=null,this.currentTick=null,this.cache.store(this.currentState)}setLog(e){this.inputs=dt(e)}setOnStateUpdate(e){e===null&&(this.prevOnStateUpdateState=null),this.onStateUpdate=e}async init(){if(!this.inputs)throw new Error("Rollback.init() called before log was configured. Call setLog() first or pass log in options.");let e=this.options.executor;if(so(e)){let r=new ct(e);await r.init(),this.executor=r}else this.executor=e;if((await this.tick(0)).ticksComputed===0||this.currentTick===null)throw new Error("Failed to record genesis snapshot");return this.options.disableTicking||(this.ticking=!0,this.loop()),this.getState()}async tick(e){if(!this.inputs)throw new Error("Rollback.tick() called before log was configured. Call setLog() and init() first.");let n=await this.inputs.getTicksAfter(this.currentNodeId,{limit:this.maxBatchSize,lag:this.tickLag});if(e&&(n=n.filter(a=>a.tick<=e)),n.length===0)return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:!1,ticksComputed:0};let r=!1,i=null;this.currentTick!==null&&n[0].tick<this.currentTick&&(r=!0,i=this.currentTick,this.rollbackTo(n[0]));let o=n.filter(a=>this.currentTick===null||a.tick>this.currentTick);if(o.length===0)return this.currentNodeId=n[n.length-1].id,{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId,rolledBack:r,ticksComputed:0};let s=this.processTicks(o,r,i);return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:r,ticksComputed:s}}async getStats(){return{rollbacks:this.stats.rollbacks.rate(),executions:this.stats.executions.rate(),updates:this.stats.updates.rate(),cacheHits:this.stats.cacheHits.rate(),cacheMisses:this.stats.cacheMisses.rate()}}processTicks(e,n=!1,r=null){if(!this.executor)throw new Error("Executor not initialized");let i=0,o=0;for(let u=0;u<e.length;u++){let f=e[u],d=Qe(f.payloads),g=this.cache.getCached(this.currentState,f.tick,d);if(g)this.currentState=g,this.currentTick=f.tick,this.currentNodeId=f.id,o=u+1,f.sync&&this.updateSyncCheckpoint(f);else break}if(n&&r!==null&&o>0)for(let u=0;u<o;u++)e[u].tick<=r&&this.stats.cacheHits.inc();let s=e.slice(o);if(s.length===0)return 0;let a=ao(s,u=>u.sync);if(a>=0){let u=s.slice(0,a+1),f=this.executor.transition(this.currentState,u),d=this.computeBatchStateId(this.currentState,u);Ot(f,d);let g=u[u.length-1];this.currentState=f,this.currentTick=g.tick,this.currentNodeId=g.id,i+=u.length,this.cache.store(f),this.recordStateSnapshot(this.currentTick,this.currentNodeId,d),this.updateSyncCheckpoint(g)}let c=a>=0?s.slice(a+1):s;if(c.length>0){let u=this.executor.transition(this.currentState,c),f=this.computeBatchStateId(this.currentState,c);Ot(u,f);let d=c[c.length-1];this.currentState=u,this.currentTick=d.tick,this.currentNodeId=d.id,i+=c.length,this.cache.store(u),this.recordStateSnapshot(this.currentTick,this.currentNodeId,f)}if(n&&r!==null)for(let u of s)u.tick<=r&&this.stats.cacheMisses.inc();return i}computeBatchStateId(e,n){let r=W(e);for(let i of n)r=it(r,i.tick,i.payloads);return r}getState(){return this.currentState.slice()}getTick(){return this.currentTick}reset(){this.currentTick=0,this.currentNodeId=null,this.syncCheckpoint=null}close(){this.ticking=!1,this.setOnStateUpdate(null),this.executor&&(this.executor.close(),this.executor=null)}rollbackTo(e){if(this.syncCheckpoint&&this.syncCheckpoint.tick>=e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}if(this.syncCheckpoint&&this.syncCheckpoint.tick<e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}let n=e.tick-1,r=this.findSnapshotAtOrBefore(n);if(r){let o=this.cache.getByStateId(r.stateId);if(o){this.currentState=o,this.currentTick=r.tick,this.currentNodeId=r.nodeId;return}}let i=this.cache.getByStateId(this.genesisStateId);if(i){this.currentState=i,this.currentTick=0,this.currentNodeId=null;return}}updateSyncCheckpoint(e){this.syncCheckpoint={state:this.currentState.slice(),tick:e.tick,nodeId:e.id}}recordStateSnapshot(e,n,r){this.stateHistory.push({tick:e,nodeId:n,stateId:r.slice()}),this.stateHistory.length>this.maxHistory&&this.stateHistory.shift()}findSnapshotAtOrBefore(e){for(let n=this.stateHistory.length-1;n>=0;n--){let r=this.stateHistory[n];if(r.tick<=e)return r}return null}};function co(t){console.warn("rollback unexpected:",t)}Be(ut);var Ga=null;globalThis.onerror=t=>(console.error("\\u{1F534} FATAL ROLLBACK WORKER ERROR (Uncaught Exception):",t),!0);export{Ga as default};\n/*! Bundled license information:\n\ncomlink/dist/esm/comlink.mjs:\n (**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n *)\n\n@noble/secp256k1/index.js:\n (*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)\n\n@noble/hashes/esm/utils.js:\n (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)\n*/\n';
7220
+ var workerCode = 'var Or=Object.defineProperty;var Fr=(t,e,n)=>e in t?Or(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var y=(t,e,n)=>Fr(t,typeof e!="symbol"?e+"":e,n);var Rt=Symbol("Comlink.proxy"),Nr=Symbol("Comlink.endpoint"),Mr=Symbol("Comlink.releaseProxy"),ut=Symbol("Comlink.finalizer"),ve=Symbol("Comlink.thrown"),Ht=t=>typeof t=="object"&&t!==null||typeof t=="function",Rr={canHandle:t=>Ht(t)&&t[Rt],serialize(t){let{port1:e,port2:n}=new MessageChannel;return Be(t,e),[n,[n]]},deserialize(t){return t.start(),lt(t)}},Hr={canHandle:t=>Ht(t)&&ve in t,serialize({value:t}){let e;return t instanceof Error?e={isError:!0,value:{message:t.message,name:t.name,stack:t.stack}}:e={isError:!1,value:t},[e,[]]},deserialize(t){throw t.isError?Object.assign(new Error(t.value.message),t.value):t.value}},Dt=new Map([["proxy",Rr],["throw",Hr]]);function Dr(t,e){for(let n of t)if(e===n||n==="*"||n instanceof RegExp&&n.test(e))return!0;return!1}function Be(t,e=globalThis,n=["*"]){e.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Dr(n,i.origin)){console.warn(`Invalid origin \'${i.origin}\' for comlink proxy`);return}let{id:o,type:s,path:a}=Object.assign({path:[]},i.data),c=(i.data.argumentList||[]).map(Y),u;try{let f=a.slice(0,-1).reduce((g,S)=>g[S],t),d=a.reduce((g,S)=>g[S],t);switch(s){case"GET":u=d;break;case"SET":f[a.slice(-1)[0]]=Y(i.data.value),u=!0;break;case"APPLY":u=d.apply(f,c);break;case"CONSTRUCT":{let g=new d(...c);u=qr(g)}break;case"ENDPOINT":{let{port1:g,port2:S}=new MessageChannel;Be(t,S),u=Zr(g,[g])}break;case"RELEASE":u=void 0;break;default:return}}catch(f){u={value:f,[ve]:0}}Promise.resolve(u).catch(f=>({value:f,[ve]:0})).then(f=>{let[d,g]=Ie(f);e.postMessage(Object.assign(Object.assign({},d),{id:o}),g),s==="RELEASE"&&(e.removeEventListener("message",r),Lt(e),ut in t&&typeof t[ut]=="function"&&t[ut]())}).catch(f=>{let[d,g]=Ie({value:new TypeError("Unserializable return value"),[ve]:0});e.postMessage(Object.assign(Object.assign({},d),{id:o}),g)})}),e.start&&e.start()}function Lr(t){return t.constructor.name==="MessagePort"}function Lt(t){Lr(t)&&t.close()}function lt(t,e){let n=new Map;return t.addEventListener("message",function(i){let{data:o}=i;if(!o||!o.id)return;let s=n.get(o.id);if(s)try{s(o)}finally{n.delete(o.id)}}),ft(t,n,[],e)}function Ae(t){if(t)throw new Error("Proxy has been released and is not useable")}function zt(t){return ne(t,new Map,{type:"RELEASE"}).then(()=>{Lt(t)})}var Ue=new WeakMap,Ce="FinalizationRegistry"in globalThis&&new FinalizationRegistry(t=>{let e=(Ue.get(t)||0)-1;Ue.set(t,e),e===0&&zt(t)});function zr(t,e){let n=(Ue.get(e)||0)+1;Ue.set(e,n),Ce&&Ce.register(t,e,t)}function Vr(t){Ce&&Ce.unregister(t)}function ft(t,e,n=[],r=function(){}){let i=!1,o=new Proxy(r,{get(s,a){if(Ae(i),a===Mr)return()=>{Vr(o),zt(t),e.clear(),i=!0};if(a==="then"){if(n.length===0)return{then:()=>o};let c=ne(t,e,{type:"GET",path:n.map(u=>u.toString())}).then(Y);return c.then.bind(c)}return ft(t,e,[...n,a])},set(s,a,c){Ae(i);let[u,f]=Ie(c);return ne(t,e,{type:"SET",path:[...n,a].map(d=>d.toString()),value:u},f).then(Y)},apply(s,a,c){Ae(i);let u=n[n.length-1];if(u===Nr)return ne(t,e,{type:"ENDPOINT"}).then(Y);if(u==="bind")return ft(t,e,n.slice(0,-1));let[f,d]=Mt(c);return ne(t,e,{type:"APPLY",path:n.map(g=>g.toString()),argumentList:f},d).then(Y)},construct(s,a){Ae(i);let[c,u]=Mt(a);return ne(t,e,{type:"CONSTRUCT",path:n.map(f=>f.toString()),argumentList:c},u).then(Y)}});return zr(o,t),o}function Gr(t){return Array.prototype.concat.apply([],t)}function Mt(t){let e=t.map(Ie);return[e.map(n=>n[0]),Gr(e.map(n=>n[1]))]}var Vt=new WeakMap;function Zr(t,e){return Vt.set(t,e),t}function qr(t){return Object.assign(t,{[Rt]:!0})}function Ie(t){for(let[e,n]of Dt)if(n.canHandle(t)){let[r,i]=n.serialize(t);return[{type:"HANDLER",name:e,value:r},i]}return[{type:"RAW",value:t},Vt.get(t)||[]]}function Y(t){switch(t.type){case"HANDLER":return Dt.get(t.name).deserialize(t.value);case"RAW":return t.value}}function ne(t,e,n,r){return new Promise(i=>{let o=Kr();e.set(o,i),t.start&&t.start(),t.postMessage(Object.assign({id:o},n),r)})}function Kr(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var Wr={p:0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,n:0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,h:1n,a:0n,b:7n,Gx:0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,Gy:0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n},{p:X,n:ht,Gx:Yr,Gy:jr,b:jt}=Wr,re=32,dt=64,U=(t="")=>{throw new Error(t)},Xt=t=>typeof t=="bigint",Jt=t=>typeof t=="string",Xr=t=>t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array",me=(t,e)=>!Xr(t)||typeof e=="number"&&e>0&&t.length!==e?U("Uint8Array expected"):t,Ne=t=>new Uint8Array(t),Jr=t=>Uint8Array.from(t),Qt=(t,e)=>t.toString(16).padStart(e,"0"),yt=t=>Array.from(me(t)).map(e=>Qt(e,2)).join(""),z={_0:48,_9:57,A:65,F:70,a:97,f:102},Gt=t=>{if(t>=z._0&&t<=z._9)return t-z._0;if(t>=z.A&&t<=z.F)return t-(z.A-10);if(t>=z.a&&t<=z.f)return t-(z.a-10)},gt=t=>{let e="hex invalid";if(!Jt(t))return U(e);let n=t.length,r=n/2;if(n%2)return U(e);let i=Ne(r);for(let o=0,s=0;o<r;o++,s+=2){let a=Gt(t.charCodeAt(s)),c=Gt(t.charCodeAt(s+1));if(a===void 0||c===void 0)return U(e);i[o]=a*16+c}return i},bt=(t,e)=>me(Jt(t)?gt(t):Jr(me(t)),e),en=()=>globalThis?.crypto,Qr=()=>en()?.subtle??U("crypto.subtle must be defined"),_e=(...t)=>{let e=Ne(t.reduce((r,i)=>r+me(i).length,0)),n=0;return t.forEach(r=>{e.set(r,n),n+=r.length}),e},ei=(t=re)=>en().getRandomValues(Ne(t)),$e=BigInt,he=(t,e,n,r="bad number: out of range")=>Xt(t)&&e<=t&&t<n?t:U(r),h=(t,e=X)=>{let n=t%e;return n>=0n?n:e+n};var tn=(t,e)=>{(t===0n||e<=0n)&&U("no inverse n="+t+" mod="+e);let n=h(t,e),r=e,i=0n,o=1n,s=1n,a=0n;for(;n!==0n;){let c=r/n,u=r%n,f=i-s*c,d=o-a*c;r=n,n=u,i=s,o=a,s=f,a=d}return r===1n?h(i,e):U("no inverse")};var Zt=t=>t instanceof J?t:U("Point expected"),nn=t=>h(h(t*t)*t+jt),qt=t=>he(t,0n,X),Pe=t=>he(t,1n,X),ti=t=>he(t,1n,ht),pt=t=>(t&1n)===0n,rn=t=>Uint8Array.of(t),ni=t=>rn(pt(t)?2:3),ri=t=>{let e=nn(Pe(t)),n=1n;for(let r=e,i=(X+1n)/4n;i>0n;i>>=1n)i&1n&&(n=n*r%X),r=r*r%X;return h(n*n)===e?n:U("sqrt invalid")},R=class R{constructor(e,n,r){y(this,"px");y(this,"py");y(this,"pz");this.px=qt(e),this.py=Pe(n),this.pz=qt(r),Object.freeze(this)}static fromBytes(e){me(e);let n,r=e[0],i=e.subarray(1),o=Kt(i,0,re),s=e.length;if(s===re+1&&[2,3].includes(r)){let a=ri(o),c=pt(a);pt($e(r))!==c&&(a=h(-a)),n=new R(o,a,1n)}return s===dt+1&&r===4&&(n=new R(o,Kt(i,re,dt),1n)),n?n.assertValidity():U("bad point: not on curve")}equals(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=Zt(e),c=h(n*a),u=h(o*i),f=h(r*a),d=h(s*i);return c===u&&f===d}is0(){return this.equals(j)}negate(){return new R(this.px,h(-this.py),this.pz)}double(){return this.add(this)}add(e){let{px:n,py:r,pz:i}=this,{px:o,py:s,pz:a}=Zt(e),c=0n,u=jt,f=0n,d=0n,g=0n,S=h(u*3n),k=h(n*o),A=h(r*s),O=h(i*a),te=h(n+r),v=h(o+s);te=h(te*v),v=h(k+A),te=h(te-v),v=h(n+i);let M=h(o+a);return v=h(v*M),M=h(k+O),v=h(v-M),M=h(r+i),f=h(s+a),M=h(M*f),f=h(A+O),M=h(M-f),g=h(c*v),f=h(S*O),g=h(f+g),f=h(A-g),g=h(A+g),d=h(f*g),A=h(k+k),A=h(A+k),O=h(c*O),v=h(S*v),A=h(A+O),O=h(k-O),O=h(c*O),v=h(v+O),k=h(A*v),d=h(d+k),k=h(M*v),f=h(te*f),f=h(f-k),k=h(te*A),g=h(M*g),g=h(g+k),new R(f,d,g)}multiply(e,n=!0){if(!n&&e===0n)return j;if(ti(e),e===1n)return this;if(this.equals(ie))return fi(e).p;let r=j,i=ie;for(let o=this;e>0n;o=o.double(),e>>=1n)e&1n?r=r.add(o):n&&(i=i.add(o));return r}toAffine(){let{px:e,py:n,pz:r}=this;if(this.equals(j))return{x:0n,y:0n};if(r===1n)return{x:e,y:n};let i=tn(r,X);return h(r*i)!==1n&&U("inverse invalid"),{x:h(e*i),y:h(n*i)}}assertValidity(){let{x:e,y:n}=this.toAffine();return Pe(e),Pe(n),h(n*n)===nn(e)?this:U("bad point: not on curve")}toBytes(e=!0){let{x:n,y:r}=this.assertValidity().toAffine(),i=Oe(n);return e?_e(ni(r),i):_e(rn(4),i,Oe(r))}static fromAffine(e){let{x:n,y:r}=e;return n===0n&&r===0n?j:new R(n,r,1n)}toHex(e){return yt(this.toBytes(e))}static fromPrivateKey(e){return ie.multiply(oi(e))}static fromHex(e){return R.fromBytes(bt(e))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}toRawBytes(e){return this.toBytes(e)}};y(R,"BASE"),y(R,"ZERO");var J=R,ie=new J(Yr,jr,1n),j=new J(0n,1n,0n);J.BASE=ie;J.ZERO=j;var Me=t=>$e("0x"+(yt(t)||"0")),Kt=(t,e,n)=>Me(t.subarray(e,n)),ii=2n**256n,Oe=t=>gt(Qt(he(t,0n,ii),dt)),oi=t=>{let e=Xt(t)?t:Me(bt(t,re));return he(e,1n,ht,"private key invalid 3")};var si=t=>{t=bt(t),(t.length<re+8||t.length>1024)&&U("expected 40-1024b");let e=h(Me(t),ht-1n);return Oe(e+1n)};var ai="SHA-256",xt={hexToBytes:gt,bytesToHex:yt,concatBytes:_e,bytesToNumberBE:Me,numberToBytesBE:Oe,mod:h,invert:tn,hmacSha256Async:async(t,...e)=>{let n=Qr(),r="HMAC",i=await n.importKey("raw",t,{name:r,hash:{name:ai}},!1,["sign"]);return Ne(await n.sign(r,i,_e(...e)))},hmacSha256Sync:void 0,hashToPrivateKey:si,randomBytes:ei};var Fe=8,ci=256,on=Math.ceil(ci/Fe)+1,mt=2**(Fe-1),ui=()=>{let t=[],e=ie,n=e;for(let r=0;r<on;r++){n=e,t.push(n);for(let i=1;i<mt;i++)n=n.add(e),t.push(n);e=n.double()}return t},Wt,Yt=(t,e)=>{let n=e.negate();return t?n:e},fi=t=>{let e=Wt||(Wt=ui()),n=j,r=ie,i=2**Fe,o=i,s=$e(i-1),a=$e(Fe);for(let c=0;c<on;c++){let u=Number(t&s);t>>=a,u>mt&&(u-=o,t+=1n);let f=c*mt,d=f,g=f+Math.abs(u)-1,S=c%2!==0,k=u<0;u===0?r=r.add(Yt(S,e[d])):n=n.add(Yt(k,e[g]))}return{p:n,f:r}};function di(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function sn(t){if(!Number.isSafeInteger(t)||t<0)throw new Error("positive integer expected, got "+t)}function se(t,...e){if(!di(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}function an(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash should be wrapped by utils.createHasher");sn(t.outputLen),sn(t.blockLen)}function ae(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function cn(t,e){se(t);let n=e.outputLen;if(t.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function Q(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Re(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function F(t,e){return t<<32-e|t>>>e}function pi(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}function ye(t){return typeof t=="string"&&(t=pi(t)),se(t),t}var oe=class{};function un(t){let e=r=>t().update(ye(r)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function mi(t,e,n,r){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,n,r);let i=BigInt(32),o=BigInt(4294967295),s=Number(n>>i&o),a=Number(n&o),c=r?4:0,u=r?0:4;t.setUint32(e+c,s,r),t.setUint32(e+u,a,r)}function fn(t,e,n){return t&e^~t&n}function ln(t,e,n){return t&e^t&n^e&n}var He=class extends oe{constructor(e,n,r,i){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=i,this.buffer=new Uint8Array(e),this.view=Re(this.buffer)}update(e){ae(this),e=ye(e),se(e);let{view:n,buffer:r,blockLen:i}=this,o=e.length;for(let s=0;s<o;){let a=Math.min(i-this.pos,o-s);if(a===i){let c=Re(e);for(;i<=o-s;s+=i)this.process(c,s);continue}r.set(e.subarray(s,s+a),this.pos),this.pos+=a,s+=a,this.pos===i&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){ae(this),cn(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:i,isLE:o}=this,{pos:s}=this;n[s++]=128,Q(this.buffer.subarray(s)),this.padOffset>i-s&&(this.process(r,0),s=0);for(let d=s;d<i;d++)n[d]=0;mi(r,i-8,BigInt(this.length*8),o),this.process(r,0);let a=Re(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let u=c/4,f=this.get();if(u>f.length)throw new Error("_sha2: outputLen bigger than state");for(let d=0;d<u;d++)a.setUint32(4*d,f[d],o)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:i,finished:o,destroyed:s,pos:a}=this;return e.destroyed=s,e.finished=o,e.length=i,e.pos=a,i%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},V=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var hi=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Z=new Uint32Array(64),De=class extends He{constructor(e=32){super(64,e,8,!1),this.A=V[0]|0,this.B=V[1]|0,this.C=V[2]|0,this.D=V[3]|0,this.E=V[4]|0,this.F=V[5]|0,this.G=V[6]|0,this.H=V[7]|0}get(){let{A:e,B:n,C:r,D:i,E:o,F:s,G:a,H:c}=this;return[e,n,r,i,o,s,a,c]}set(e,n,r,i,o,s,a,c){this.A=e|0,this.B=n|0,this.C=r|0,this.D=i|0,this.E=o|0,this.F=s|0,this.G=a|0,this.H=c|0}process(e,n){for(let d=0;d<16;d++,n+=4)Z[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let g=Z[d-15],S=Z[d-2],k=F(g,7)^F(g,18)^g>>>3,A=F(S,17)^F(S,19)^S>>>10;Z[d]=A+Z[d-7]+k+Z[d-16]|0}let{A:r,B:i,C:o,D:s,E:a,F:c,G:u,H:f}=this;for(let d=0;d<64;d++){let g=F(a,6)^F(a,11)^F(a,25),S=f+g+fn(a,c,u)+hi[d]+Z[d]|0,A=(F(r,2)^F(r,13)^F(r,22))+ln(r,i,o)|0;f=u,u=c,c=a,a=s+S|0,s=o,o=i,i=r,r=S+A|0}r=r+this.A|0,i=i+this.B|0,o=o+this.C|0,s=s+this.D|0,a=a+this.E|0,c=c+this.F|0,u=u+this.G|0,f=f+this.H|0,this.set(r,i,o,s,a,c,u,f)}roundClean(){Q(Z)}destroy(){this.set(0,0,0,0,0,0,0,0),Q(this.buffer)}};var ge=un(()=>new De);var wt=ge;var Le=class extends oe{constructor(e,n){super(),this.finished=!1,this.destroyed=!1,an(e);let r=ye(n);if(this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let i=this.blockLen,o=new Uint8Array(i);o.set(r.length>i?e.create().update(r).digest():r);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),Q(o)}update(e){return ae(this),this.iHash.update(e),this}digestInto(e){ae(this),se(e,this.outputLen),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:i,destroyed:o,blockLen:s,outputLen:a}=this;return e=e,e.finished=i,e.destroyed=o,e.blockLen=s,e.outputLen=a,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},St=(t,e,n)=>new Le(t,e).update(n).digest();St.create=(t,e)=>new Le(t,e);var yi=["string","number","bigint","symbol"],gi=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function dn(t){if(t===null)return"null";if(t===void 0)return"undefined";if(t===!0||t===!1)return"boolean";let e=typeof t;if(yi.includes(e))return e;if(e==="function")return"Function";if(Array.isArray(t))return"Array";if(bi(t))return"Buffer";let n=xi(t);return n||"Object"}function bi(t){return t&&t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer.call(null,t)}function xi(t){let e=Object.prototype.toString.call(t).slice(8,-1);if(gi.includes(e))return e}var l=class{constructor(e,n,r){this.major=e,this.majorEncoded=e<<5,this.name=n,this.terminal=r}toString(){return`Type[${this.major}].${this.name}`}compare(e){return this.major<e.major?-1:this.major>e.major?1:0}};l.uint=new l(0,"uint",!0);l.negint=new l(1,"negint",!0);l.bytes=new l(2,"bytes",!0);l.string=new l(3,"string",!0);l.array=new l(4,"array",!1);l.map=new l(5,"map",!1);l.tag=new l(6,"tag",!1);l.float=new l(7,"float",!0);l.false=new l(7,"false",!0);l.true=new l(7,"true",!0);l.null=new l(7,"null",!0);l.undefined=new l(7,"undefined",!0);l.break=new l(7,"break",!0);var m=class{constructor(e,n,r){this.type=e,this.value=n,this.encodedLength=r,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var ce=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",wi=new TextDecoder,Si=new TextEncoder;function ze(t){return ce&&globalThis.Buffer.isBuffer(t)}function Et(t){return t instanceof Uint8Array?ze(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):t:Uint8Array.from(t)}var yn=ce?(t,e,n)=>n-e>64?globalThis.Buffer.from(t.subarray(e,n)).toString("utf8"):mn(t,e,n):(t,e,n)=>n-e>64?wi.decode(t.subarray(e,n)):mn(t,e,n),gn=ce?t=>t.length>64?globalThis.Buffer.from(t):pn(t):t=>t.length>64?Si.encode(t):pn(t),H=t=>Uint8Array.from(t),ue=ce?(t,e,n)=>ze(t)?new Uint8Array(t.subarray(e,n)):t.slice(e,n):(t,e,n)=>t.slice(e,n),bn=ce?(t,e)=>(t=t.map(n=>n instanceof Uint8Array?n:globalThis.Buffer.from(n)),Et(globalThis.Buffer.concat(t,e))):(t,e)=>{let n=new Uint8Array(e),r=0;for(let i of t)r+i.length>n.length&&(i=i.subarray(0,n.length-r)),n.set(i,r),r+=i.length;return n},xn=ce?t=>globalThis.Buffer.allocUnsafe(t):t=>new Uint8Array(t);function Ve(t,e){if(ze(t)&&ze(e))return t.compare(e);for(let n=0;n<t.length;n++)if(t[n]!==e[n])return t[n]<e[n]?-1:1;return 0}function pn(t){let e=[],n=0;for(let r=0;r<t.length;r++){let i=t.charCodeAt(r);i<128?e[n++]=i:i<2048?(e[n++]=i>>6|192,e[n++]=i&63|128):(i&64512)===55296&&r+1<t.length&&(t.charCodeAt(r+1)&64512)===56320?(i=65536+((i&1023)<<10)+(t.charCodeAt(++r)&1023),e[n++]=i>>18|240,e[n++]=i>>12&63|128,e[n++]=i>>6&63|128,e[n++]=i&63|128):(e[n++]=i>>12|224,e[n++]=i>>6&63|128,e[n++]=i&63|128)}return e}function mn(t,e,n){let r=[];for(;e<n;){let i=t[e],o=null,s=i>239?4:i>223?3:i>191?2:1;if(e+s<=n){let a,c,u,f;switch(s){case 1:i<128&&(o=i);break;case 2:a=t[e+1],(a&192)===128&&(f=(i&31)<<6|a&63,f>127&&(o=f));break;case 3:a=t[e+1],c=t[e+2],(a&192)===128&&(c&192)===128&&(f=(i&15)<<12|(a&63)<<6|c&63,f>2047&&(f<55296||f>57343)&&(o=f));break;case 4:a=t[e+1],c=t[e+2],u=t[e+3],(a&192)===128&&(c&192)===128&&(u&192)===128&&(f=(i&15)<<18|(a&63)<<12|(c&63)<<6|u&63,f>65535&&f<1114112&&(o=f))}}o===null?(o=65533,s=1):o>65535&&(o-=65536,r.push(o>>>10&1023|55296),o=56320|o&1023),r.push(o),e+=s}return Ei(r)}var hn=4096;function Ei(t){let e=t.length;if(e<=hn)return String.fromCharCode.apply(String,t);let n="",r=0;for(;r<e;)n+=String.fromCharCode.apply(String,t.slice(r,r+=hn));return n}var ki=256,be=class{constructor(e=ki){this.chunkSize=e,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(e){let n=this.chunks[this.chunks.length-1];if(this.cursor+e.length<=this.maxCursor+1){let i=n.length-(this.maxCursor-this.cursor)-1;n.set(e,i)}else{if(n){let i=n.length-(this.maxCursor-this.cursor)-1;i<n.length&&(this.chunks[this.chunks.length-1]=n.subarray(0,i),this.maxCursor=this.cursor-1)}e.length<64&&e.length<this.chunkSize?(n=xn(this.chunkSize),this.chunks.push(n),this.maxCursor+=n.length,this._initReuseChunk===null&&(this._initReuseChunk=n),n.set(e,0)):(this.chunks.push(e),this.maxCursor+=e.length)}this.cursor+=e.length}toBytes(e=!1){let n;if(this.chunks.length===1){let r=this.chunks[0];e&&this.cursor>r.length/2?(n=this.cursor===r.length?r:r.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):n=ue(r,0,this.cursor)}else n=bn(this.chunks,this.cursor);return e&&this.reset(),n}};var b="CBOR decode error:",kt="CBOR encode error:",xe=[];xe[23]=1;xe[24]=2;xe[25]=3;xe[26]=5;xe[27]=9;function G(t,e,n){if(t.length-e<n)throw new Error(`${b} not enough data for type`)}var E=[24,256,65536,4294967296,BigInt("18446744073709551616")];function C(t,e,n){G(t,e,1);let r=t[e];if(n.strict===!0&&r<E[0])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function I(t,e,n){G(t,e,2);let r=t[e]<<8|t[e+1];if(n.strict===!0&&r<E[1])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function B(t,e,n){G(t,e,4);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3];if(n.strict===!0&&r<E[2])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);return r}function P(t,e,n){G(t,e,8);let r=t[e]*16777216+(t[e+1]<<16)+(t[e+2]<<8)+t[e+3],i=t[e+4]*16777216+(t[e+5]<<16)+(t[e+6]<<8)+t[e+7],o=(BigInt(r)<<BigInt(32))+BigInt(i);if(n.strict===!0&&o<E[3])throw new Error(`${b} integer encoded in more bytes than necessary (strict decode)`);if(o<=Number.MAX_SAFE_INTEGER)return Number(o);if(n.allowBigInt===!0)return o;throw new Error(`${b} integers outside of the safe integer range are not supported`)}function wn(t,e,n,r){return new m(l.uint,C(t,e+1,r),2)}function Sn(t,e,n,r){return new m(l.uint,I(t,e+1,r),3)}function En(t,e,n,r){return new m(l.uint,B(t,e+1,r),5)}function kn(t,e,n,r){return new m(l.uint,P(t,e+1,r),9)}function _(t,e){return T(t,0,e.value)}function T(t,e,n){if(n<E[0]){let r=Number(n);t.push([e|r])}else if(n<E[1]){let r=Number(n);t.push([e|24,r])}else if(n<E[2]){let r=Number(n);t.push([e|25,r>>>8,r&255])}else if(n<E[3]){let r=Number(n);t.push([e|26,r>>>24&255,r>>>16&255,r>>>8&255,r&255])}else{let r=BigInt(n);if(r<E[4]){let i=[e|27,0,0,0,0,0,0,0],o=Number(r&BigInt(4294967295)),s=Number(r>>BigInt(32)&BigInt(4294967295));i[8]=o&255,o=o>>8,i[7]=o&255,o=o>>8,i[6]=o&255,o=o>>8,i[5]=o&255,i[4]=s&255,s=s>>8,i[3]=s&255,s=s>>8,i[2]=s&255,s=s>>8,i[1]=s&255,t.push(i)}else throw new Error(`${b} encountered BigInt larger than allowable range`)}}_.encodedSize=function(e){return T.encodedSize(e.value)};T.encodedSize=function(e){return e<E[0]?1:e<E[1]?2:e<E[2]?3:e<E[3]?5:9};_.compareTokens=function(e,n){return e.value<n.value?-1:e.value>n.value?1:0};function Tn(t,e,n,r){return new m(l.negint,-1-C(t,e+1,r),2)}function An(t,e,n,r){return new m(l.negint,-1-I(t,e+1,r),3)}function vn(t,e,n,r){return new m(l.negint,-1-B(t,e+1,r),5)}var Tt=BigInt(-1),Un=BigInt(1);function Cn(t,e,n,r){let i=P(t,e+1,r);if(typeof i!="bigint"){let o=-1-i;if(o>=Number.MIN_SAFE_INTEGER)return new m(l.negint,o,9)}if(r.allowBigInt!==!0)throw new Error(`${b} integers outside of the safe integer range are not supported`);return new m(l.negint,Tt-BigInt(i),9)}function Ge(t,e){let n=e.value,r=typeof n=="bigint"?n*Tt-Un:n*-1-1;T(t,e.type.majorEncoded,r)}Ge.encodedSize=function(e){let n=e.value,r=typeof n=="bigint"?n*Tt-Un:n*-1-1;return r<E[0]?1:r<E[1]?2:r<E[2]?3:r<E[3]?5:9};Ge.compareTokens=function(e,n){return e.value<n.value?1:e.value>n.value?-1:0};function we(t,e,n,r){G(t,e,n+r);let i=ue(t,e+n,e+n+r);return new m(l.bytes,i,n+r)}function In(t,e,n,r){return we(t,e,1,n)}function Bn(t,e,n,r){return we(t,e,2,C(t,e+1,r))}function Pn(t,e,n,r){return we(t,e,3,I(t,e+1,r))}function _n(t,e,n,r){return we(t,e,5,B(t,e+1,r))}function $n(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer bytes lengths not supported`);return we(t,e,9,i)}function Ze(t){return t.encodedBytes===void 0&&(t.encodedBytes=t.type===l.string?gn(t.value):t.value),t.encodedBytes}function fe(t,e){let n=Ze(e);T(t,e.type.majorEncoded,n.length),t.push(n)}fe.encodedSize=function(e){let n=Ze(e);return T.encodedSize(n.length)+n.length};fe.compareTokens=function(e,n){return Ai(Ze(e),Ze(n))};function Ai(t,e){return t.length<e.length?-1:t.length>e.length?1:Ve(t,e)}function Se(t,e,n,r,i){let o=n+r;G(t,e,o);let s=new m(l.string,yn(t,e+n,e+o),o);return i.retainStringBytes===!0&&(s.byteValue=ue(t,e+n,e+o)),s}function On(t,e,n,r){return Se(t,e,1,n,r)}function Fn(t,e,n,r){return Se(t,e,2,C(t,e+1,r),r)}function Nn(t,e,n,r){return Se(t,e,3,I(t,e+1,r),r)}function Mn(t,e,n,r){return Se(t,e,5,B(t,e+1,r),r)}function Rn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer string lengths not supported`);return Se(t,e,9,i,r)}var Hn=fe;function le(t,e,n,r){return new m(l.array,r,n)}function Dn(t,e,n,r){return le(t,e,1,n)}function Ln(t,e,n,r){return le(t,e,2,C(t,e+1,r))}function zn(t,e,n,r){return le(t,e,3,I(t,e+1,r))}function Vn(t,e,n,r){return le(t,e,5,B(t,e+1,r))}function Gn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer array lengths not supported`);return le(t,e,9,i)}function Zn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return le(t,e,1,1/0)}function qe(t,e){T(t,l.array.majorEncoded,e.value)}qe.compareTokens=_.compareTokens;qe.encodedSize=function(e){return T.encodedSize(e.value)};function de(t,e,n,r){return new m(l.map,r,n)}function qn(t,e,n,r){return de(t,e,1,n)}function Kn(t,e,n,r){return de(t,e,2,C(t,e+1,r))}function Wn(t,e,n,r){return de(t,e,3,I(t,e+1,r))}function Yn(t,e,n,r){return de(t,e,5,B(t,e+1,r))}function jn(t,e,n,r){let i=P(t,e+1,r);if(typeof i=="bigint")throw new Error(`${b} 64-bit integer map lengths not supported`);return de(t,e,9,i)}function Xn(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return de(t,e,1,1/0)}function Ke(t,e){T(t,l.map.majorEncoded,e.value)}Ke.compareTokens=_.compareTokens;Ke.encodedSize=function(e){return T.encodedSize(e.value)};function Jn(t,e,n,r){return new m(l.tag,n,1)}function Qn(t,e,n,r){return new m(l.tag,C(t,e+1,r),2)}function er(t,e,n,r){return new m(l.tag,I(t,e+1,r),3)}function tr(t,e,n,r){return new m(l.tag,B(t,e+1,r),5)}function nr(t,e,n,r){return new m(l.tag,P(t,e+1,r),9)}function We(t,e){T(t,l.tag.majorEncoded,e.value)}We.compareTokens=_.compareTokens;We.encodedSize=function(e){return T.encodedSize(e.value)};var Pi=20,_i=21,$i=22,Oi=23;function rr(t,e,n,r){if(r.allowUndefined===!1)throw new Error(`${b} undefined values are not supported`);return r.coerceUndefinedToNull===!0?new m(l.null,null,1):new m(l.undefined,void 0,1)}function ir(t,e,n,r){if(r.allowIndefinite===!1)throw new Error(`${b} indefinite length items not allowed`);return new m(l.break,void 0,1)}function At(t,e,n){if(n){if(n.allowNaN===!1&&Number.isNaN(t))throw new Error(`${b} NaN values are not supported`);if(n.allowInfinity===!1&&(t===1/0||t===-1/0))throw new Error(`${b} Infinity values are not supported`)}return new m(l.float,t,e)}function or(t,e,n,r){return At(vt(t,e+1),3,r)}function sr(t,e,n,r){return At(Ut(t,e+1),5,r)}function ar(t,e,n,r){return At(lr(t,e+1),9,r)}function Ye(t,e,n){let r=e.value;if(r===!1)t.push([l.float.majorEncoded|Pi]);else if(r===!0)t.push([l.float.majorEncoded|_i]);else if(r===null)t.push([l.float.majorEncoded|$i]);else if(r===void 0)t.push([l.float.majorEncoded|Oi]);else{let i,o=!1;(!n||n.float64!==!0)&&(ur(r),i=vt(N,1),r===i||Number.isNaN(r)?(N[0]=249,t.push(N.slice(0,3)),o=!0):(fr(r),i=Ut(N,1),r===i&&(N[0]=250,t.push(N.slice(0,5)),o=!0))),o||(Fi(r),i=lr(N,1),N[0]=251,t.push(N.slice(0,9)))}}Ye.encodedSize=function(e,n){let r=e.value;if(r===!1||r===!0||r===null||r===void 0)return 1;if(!n||n.float64!==!0){ur(r);let i=vt(N,1);if(r===i||Number.isNaN(r))return 3;if(fr(r),i=Ut(N,1),r===i)return 5}return 9};var cr=new ArrayBuffer(9),$=new DataView(cr,1),N=new Uint8Array(cr,0);function ur(t){if(t===1/0)$.setUint16(0,31744,!1);else if(t===-1/0)$.setUint16(0,64512,!1);else if(Number.isNaN(t))$.setUint16(0,32256,!1);else{$.setFloat32(0,t);let e=$.getUint32(0),n=(e&2139095040)>>23,r=e&8388607;if(n===255)$.setUint16(0,31744,!1);else if(n===0)$.setUint16(0,(t&2147483648)>>16|r>>13,!1);else{let i=n-127;i<-24?$.setUint16(0,0):i<-14?$.setUint16(0,(e&2147483648)>>16|1<<24+i,!1):$.setUint16(0,(e&2147483648)>>16|i+15<<10|r>>13,!1)}}}function vt(t,e){if(t.length-e<2)throw new Error(`${b} not enough data for float16`);let n=(t[e]<<8)+t[e+1];if(n===31744)return 1/0;if(n===64512)return-1/0;if(n===32256)return NaN;let r=n>>10&31,i=n&1023,o;return r===0?o=i*2**-24:r!==31?o=(i+1024)*2**(r-25):o=i===0?1/0:NaN,n&32768?-o:o}function fr(t){$.setFloat32(0,t,!1)}function Ut(t,e){if(t.length-e<4)throw new Error(`${b} not enough data for float32`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,4).getFloat32(0,!1)}function Fi(t){$.setFloat64(0,t,!1)}function lr(t,e){if(t.length-e<8)throw new Error(`${b} not enough data for float64`);let n=(t.byteOffset||0)+e;return new DataView(t.buffer,n,8).getFloat64(0,!1)}Ye.compareTokens=_.compareTokens;function x(t,e,n){throw new Error(`${b} encountered invalid minor (${n}) for major ${t[e]>>>5}`)}function je(t){return()=>{throw new Error(`${b} ${t}`)}}var p=[];for(let t=0;t<=23;t++)p[t]=x;p[24]=wn;p[25]=Sn;p[26]=En;p[27]=kn;p[28]=x;p[29]=x;p[30]=x;p[31]=x;for(let t=32;t<=55;t++)p[t]=x;p[56]=Tn;p[57]=An;p[58]=vn;p[59]=Cn;p[60]=x;p[61]=x;p[62]=x;p[63]=x;for(let t=64;t<=87;t++)p[t]=In;p[88]=Bn;p[89]=Pn;p[90]=_n;p[91]=$n;p[92]=x;p[93]=x;p[94]=x;p[95]=je("indefinite length bytes/strings are not supported");for(let t=96;t<=119;t++)p[t]=On;p[120]=Fn;p[121]=Nn;p[122]=Mn;p[123]=Rn;p[124]=x;p[125]=x;p[126]=x;p[127]=je("indefinite length bytes/strings are not supported");for(let t=128;t<=151;t++)p[t]=Dn;p[152]=Ln;p[153]=zn;p[154]=Vn;p[155]=Gn;p[156]=x;p[157]=x;p[158]=x;p[159]=Zn;for(let t=160;t<=183;t++)p[t]=qn;p[184]=Kn;p[185]=Wn;p[186]=Yn;p[187]=jn;p[188]=x;p[189]=x;p[190]=x;p[191]=Xn;for(let t=192;t<=215;t++)p[t]=Jn;p[216]=Qn;p[217]=er;p[218]=tr;p[219]=nr;p[220]=x;p[221]=x;p[222]=x;p[223]=x;for(let t=224;t<=243;t++)p[t]=je("simple values are not supported");p[244]=x;p[245]=x;p[246]=x;p[247]=rr;p[248]=je("simple values are not supported");p[249]=or;p[250]=sr;p[251]=ar;p[252]=x;p[253]=x;p[254]=x;p[255]=ir;var D=[];for(let t=0;t<24;t++)D[t]=new m(l.uint,t,1);for(let t=-1;t>=-24;t--)D[31-t]=new m(l.negint,t,1);D[64]=new m(l.bytes,new Uint8Array(0),1);D[96]=new m(l.string,"",1);D[128]=new m(l.array,0,1);D[160]=new m(l.map,0,1);D[244]=new m(l.false,!1,1);D[245]=new m(l.true,!0,1);D[246]=new m(l.null,null,1);function dr(t){switch(t.type){case l.false:return H([244]);case l.true:return H([245]);case l.null:return H([246]);case l.bytes:return t.value.length?void 0:H([64]);case l.string:return t.value===""?H([96]):void 0;case l.array:return t.value===0?H([128]):void 0;case l.map:return t.value===0?H([160]):void 0;case l.uint:return t.value<24?H([Number(t.value)]):void 0;case l.negint:if(t.value>=-24)return H([31-Number(t.value)])}}var mr=Object.freeze({float64:!0,mapSorter:Di,quickEncodeToken:dr});function Mi(){let t=[];return t[l.uint.major]=_,t[l.negint.major]=Ge,t[l.bytes.major]=fe,t[l.string.major]=Hn,t[l.array.major]=qe,t[l.map.major]=Ke,t[l.tag.major]=We,t[l.float.major]=Ye,t}var Ri=Mi(),Ct=new be,Je=class t{constructor(e,n){this.obj=e,this.parent=n}includes(e){let n=this;do if(n.obj===e)return!0;while(n=n.parent);return!1}static createCheck(e,n){if(e&&e.includes(n))throw new Error(`${kt} object contains circular references`);return new t(n,e)}},q={null:new m(l.null,null),undefined:new m(l.undefined,void 0),true:new m(l.true,!0),false:new m(l.false,!1),emptyArray:new m(l.array,0),emptyMap:new m(l.map,0)},K={number(t,e,n,r){return!Number.isInteger(t)||!Number.isSafeInteger(t)?new m(l.float,t):t>=0?new m(l.uint,t):new m(l.negint,t)},bigint(t,e,n,r){return t>=BigInt(0)?new m(l.uint,t):new m(l.negint,t)},Uint8Array(t,e,n,r){return new m(l.bytes,t)},string(t,e,n,r){return new m(l.string,t)},boolean(t,e,n,r){return t?q.true:q.false},null(t,e,n,r){return q.null},undefined(t,e,n,r){return q.undefined},ArrayBuffer(t,e,n,r){return new m(l.bytes,new Uint8Array(t))},DataView(t,e,n,r){return new m(l.bytes,new Uint8Array(t.buffer,t.byteOffset,t.byteLength))},Array(t,e,n,r){if(!t.length)return n.addBreakTokens===!0?[q.emptyArray,new m(l.break)]:q.emptyArray;r=Je.createCheck(r,t);let i=[],o=0;for(let s of t)i[o++]=Xe(s,n,r);return n.addBreakTokens?[new m(l.array,t.length),i,new m(l.break)]:[new m(l.array,t.length),i]},Object(t,e,n,r){let i=e!=="Object",o=i?t.keys():Object.keys(t),s=i?t.size:o.length;if(!s)return n.addBreakTokens===!0?[q.emptyMap,new m(l.break)]:q.emptyMap;r=Je.createCheck(r,t);let a=[],c=0;for(let u of o)a[c++]=[Xe(u,n,r),Xe(i?t.get(u):t[u],n,r)];return Hi(a,n),n.addBreakTokens?[new m(l.map,s),a,new m(l.break)]:[new m(l.map,s),a]}};K.Map=K.Object;K.Buffer=K.Uint8Array;for(let t of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))K[`${t}Array`]=K.DataView;function Xe(t,e={},n){let r=dn(t),i=e&&e.typeEncoders&&e.typeEncoders[r]||K[r];if(typeof i=="function"){let s=i(t,r,e,n);if(s!=null)return s}let o=K[r];if(!o)throw new Error(`${kt} unsupported type: ${r}`);return o(t,r,e,n)}function Hi(t,e){e.mapSorter&&t.sort(e.mapSorter)}function Di(t,e){if(t[0]instanceof m&&e[0]instanceof m){let n=t[0],r=e[0];return n._keyBytes||(n._keyBytes=pr(n.value)),r._keyBytes||(r._keyBytes=pr(r.value)),Ve(n._keyBytes,r._keyBytes)}throw new Error("rfc8949MapSorter: complex key types are not supported yet")}function pr(t){return Li(t,Ri,mr)}function hr(t,e,n,r){if(Array.isArray(e))for(let i of e)hr(t,i,n,r);else n[e.type.major](t,e,r)}function Li(t,e,n){let r=Xe(t,n);if(!Array.isArray(r)&&n.quickEncodeToken){let i=n.quickEncodeToken(r);if(i)return i;let o=e[r.type.major];if(o.encodedSize){let s=o.encodedSize(r,n),a=new be(s);if(o(a,r,n),a.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${r} was wrong`);return Et(a.chunks[0])}}return Ct.reset(),hr(Ct,r,e,n),Ct.toBytes(!0)}xt.hmacSha256Sync=(t,...e)=>St(wt,t,xt.concatBytes(...e));var Gi=32;function Qe(t){if(t.length===0)return new Uint8Array(Gi);let e=t.length,n=e*4,r=t.reduce((c,u)=>c+u.length,0),i=4+n+r,o=new Uint8Array(i),s=new DataView(o.buffer,o.byteOffset,o.byteLength);s.setUint32(0,e,!0);let a=4;for(let c of t)s.setUint32(a,c.length,!0),a+=4;for(let c of t)o.set(c,a),a+=c.length;return wt(o)}var It={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,entityRef:4,f32:4,vec2:8,vec3:12,quat:16,enum:1},Ee={bool:1,uint8:1,int8:1,uint16:2,int16:2,uint32:4,int32:4,f32:4,flags8:1,flags16:2,flags32:4,vec2:8,vec3:12,quat:16};var yr=new Set(["bool","uint8","int8","uint16","int16","uint32","int32","f32"]);function _t(t){let e=t.match(/^(\\w+)\\[(\\d+)\\]$/);if(!e||!e[1]||!e[2])return null;let n=e[1],r=parseInt(e[2],10);if(!yr.has(n))throw new Error(`Invalid array element type \'${n}\': arrays only support primitive scalar types (bool, uint8, int8, uint16, int16, uint32, int32, f32)`);return{elementType:n,length:r}}function gr(t){let e=_t(t);if(e){let r=Ee[e.elementType];if(r===void 0)throw new Error(`Unknown element type: ${e.elementType}`);return r*e.length}let n=Ee[t];if(n===void 0)throw new Error(`Unknown type: ${t}`);return n}function Zi(t){let e=[],n=[],r=new Map,i=new Map,o=0;for(let s of t.controls){let a=gr(s.type),c=_t(s.type),u={name:s.name,type:s.type,size:a,offset:0,options:s.options};c&&(u.arrayLength=c.length,u.arrayElementType=c.elementType);let f={name:s.name,index:o++,field:u,hint:s.hint,retain:s.retain==="always"?"always":"tick"};e.push(f),r.set(s.name,f)}for(let s of t.commands){let a=[],c=0;for(let f of s.args){let d=gr(f.type),g=_t(f.type),S={name:f.name,type:f.type,size:d,offset:c};g&&(S.arrayLength=g.length,S.arrayElementType=g.elementType),a.push(S),c+=d}let u={name:s.name,index:o++,args:a,totalSize:c};n.push(u),i.set(s.name,u)}return{controls:e,commands:n,controlByName:r,commandByName:i}}function Bt(t,e,n,r){switch(n){case"bool":t.setUint8(e,r?1:0);break;case"uint8":case"flags8":t.setUint8(e,r);break;case"int8":t.setInt8(e,r);break;case"uint16":case"flags16":t.setUint16(e,r,!0);break;case"int16":t.setInt16(e,r,!0);break;case"uint32":case"flags32":t.setUint32(e,r,!0);break;case"int32":t.setInt32(e,r,!0);break;case"f32":t.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function Pt(t,e,n){switch(n){case"bool":return t.getUint8(e)!==0;case"uint8":case"flags8":return t.getUint8(e);case"int8":return t.getInt8(e);case"uint16":case"flags16":return t.getUint16(e,!0);case"int16":return t.getInt16(e,!0);case"uint32":case"flags32":return t.getUint32(e,!0);case"int32":return t.getInt32(e,!0);case"f32":return t.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}var et=class t{constructor(e){y(this,"schema");y(this,"fields",new Map);y(this,"commandList",[]);this.schema=e}setControl(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);let i=new Uint8Array(r.field.size),o=new DataView(i.buffer);this.encodeValue(o,0,r.field,n),this.fields.set(r.index,i)}setFlags(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(!r.field.options)throw new Error(`Control ${e} is not a flags type`);let i=0;for(let a=0;a<r.field.options.length;a++){let c=r.field.options[a];n[c]&&(i|=1<<a)}let o=new Uint8Array(r.field.size),s=new DataView(o.buffer);Bt(s,0,r.field.type,i),this.fields.set(r.index,o)}setVec2(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let i=new Uint8Array(8),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),this.fields.set(r.index,i)}setVec3(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let i=new Uint8Array(12),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),this.fields.set(r.index,i)}setQuat(e,n){let r=this.schema.controlByName.get(e);if(!r)throw new Error(`Unknown control: ${e}`);if(r.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let i=new Uint8Array(16),o=new DataView(i.buffer);o.setFloat32(0,n[0],!0),o.setFloat32(4,n[1],!0),o.setFloat32(8,n[2],!0),o.setFloat32(12,n[3],!0),this.fields.set(r.index,i)}getControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);let r=this.fields.get(n.index);if(!r)return this.getDefaultValue(n.field);let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return this.decodeValue(i,0,n.field)}getFlags(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(!n.field.options)throw new Error(`Control ${e} is not a flags type`);let r=this.fields.get(n.index),i=0;if(r){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);i=Pt(s,0,n.field.type)}let o={};for(let s=0;s<n.field.options.length;s++)o[n.field.options[s]]=(i&1<<s)!==0;return o}getVec2(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec2")throw new Error(`Control ${e} is not a vec2`);let r=this.fields.get(n.index);if(!r)return[0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0)]}getVec3(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="vec3")throw new Error(`Control ${e} is not a vec3`);let r=this.fields.get(n.index);if(!r)return[0,0,0];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0)]}getQuat(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);if(n.field.type!=="quat")throw new Error(`Control ${e} is not a quat`);let r=this.fields.get(n.index);if(!r)return[0,0,0,1];let i=new DataView(r.buffer,r.byteOffset,r.byteLength);return[i.getFloat32(0,!0),i.getFloat32(4,!0),i.getFloat32(8,!0),i.getFloat32(12,!0)]}hasControl(e){let n=this.schema.controlByName.get(e);if(!n)throw new Error(`Unknown control: ${e}`);return this.fields.has(n.index)}addCommand(e,n){let r=this.schema.commandByName.get(e);if(!r)throw new Error(`Unknown command: ${e}`);let i=new Uint8Array(r.totalSize),o=new DataView(i.buffer);for(let s of r.args){let a=n[s.name];if(a===void 0)throw new Error(`Missing required argument: ${s.name}`);this.encodeValue(o,s.offset,s,a)}this.commandList.push({index:r.index,data:i})}getCommands(){let e=[];for(let{index:n,data:r}of this.commandList){let i=this.schema.commands.find(a=>a.index===n);if(!i)continue;let o=new DataView(r.buffer,r.byteOffset,r.byteLength),s={name:i.name};for(let a of i.args)s[a.name]=this.decodeValue(o,a.offset,a);e.push(s)}return e}encode(){let e=[];for(let[o,s]of this.fields){let a=new Uint8Array(2+s.length);a[0]=o,a[1]=s.length,a.set(s,2),e.push(a)}for(let o of this.commandList){let s=new Uint8Array(2+o.data.length);s[0]=o.index,s[1]=o.data.length,s.set(o.data,2),e.push(s)}let n=e.reduce((o,s)=>o+s.length,0),r=new Uint8Array(n),i=0;for(let o of e)r.set(o,i),i+=o.length;return r}static decode(e,n){let r=new t(e),i=0;for(;i<n.length;){if(i+2>n.length)throw new Error("Truncated TLV field header");let o=n[i],s=n[i+1];if(i+2+s>n.length)throw new Error(`Truncated TLV field data at index ${o}`);let a=n.subarray(i+2,i+2+s);e.controls.find(u=>u.index===o)?r.fields.set(o,new Uint8Array(a)):e.commands.find(f=>f.index===o)&&r.commandList.push({index:o,data:new Uint8Array(a)}),i+=2+s}return r}clear(){this.fields.clear(),this.commandList.length=0}encodeValue(e,n,r,i){if(r.type==="vec2"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0)}else if(r.type==="vec3"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0)}else if(r.type==="quat"){let o=i;e.setFloat32(n,o[0],!0),e.setFloat32(n+4,o[1],!0),e.setFloat32(n+8,o[2],!0),e.setFloat32(n+12,o[3],!0)}else if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let o=i,s=Ee[r.arrayElementType];for(let a=0;a<Math.min(o.length,r.arrayLength);a++)Bt(e,n+a*s,r.arrayElementType,o[a])}else Bt(e,n,r.type,i)}decodeValue(e,n,r){if(r.type==="vec2")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0)];if(r.type==="vec3")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)];if(r.type==="quat")return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0),e.getFloat32(n+12,!0)];if(r.arrayLength!==void 0&&r.arrayElementType!==void 0){let i=[],o=Ee[r.arrayElementType];for(let s=0;s<r.arrayLength;s++)i.push(Pt(e,n+s*o,r.arrayElementType));return i}else return Pt(e,n,r.type)}getDefaultValue(e){return e.type==="vec2"?[0,0]:e.type==="vec3"?[0,0,0]:e.type==="quat"?[0,0,0,1]:e.arrayLength!==void 0?new Array(e.arrayLength).fill(0):e.type==="bool"?!1:0}};function br(t){let e=Zi(t);return{create(){return new et(e)},decode(n){return et.decode(e,n)},get schema(){return e}}}function xr(t,e){return((e&65535)<<16|t&65535)>>>0}function ke(t){return t&65535}function ji(t){return t>>>16&65535}function pe(t){return t.subarray(8,40)}function $t(t,e){if(e.length!==32)throw new Error(`stateId must be ${32} bytes, got ${e.length}`);t.set(e,8)}function wr(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint32(40,e,!0)}function rt(t,e,n){let r=new Uint8Array(68);return r.set(t,0),new DataView(r.buffer).setUint32(32,e,!0),r.set(n,36),ge(r)}function Sr(t,e,n){return rt(t,e,Qe(n))}function Ot(t){return ge(t)}function Er(t,e,n){let r=new ArrayBuffer(t.totalSize),i=new Uint8Array(r),o=new DataView(r);if(o.setUint32(0,1297367376,!0),o.setUint16(4,0,!0),i[6]=n??0,i[7]=0,e){let a=Ot(e);i.set(a,8)}let s=t.freeStackOffset;o.setUint16(s,t.maxEntities,!0);for(let a=0;a<t.maxEntities;a++){let c=t.freeStackOffset+2+a*2;o.setUint16(c,a,!0)}return i}function kr(t,e){return t.entityTableOffset+e*t.entityRecordSize}function Ft(t,e,n){let r=kr(t,n);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(r,!0)}function Xi(t){return new DataView(t.buffer,t.byteOffset,t.byteLength).getUint16(4,!0)}function Ji(t,e){new DataView(t.buffer,t.byteOffset,t.byteLength).setUint16(4,e,!0)}function Qi(t,e){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t.freeStackOffset,!0)}function eo(t,e,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t.freeStackOffset,n,!0)}function to(t,e){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Qi(t,e);if(r===0)throw new Error(`No free entity slots available (max: ${t.maxEntities})`);let i=t.freeStackOffset+2+(r-1)*2,o=n.getUint16(i,!0);return eo(t,e,r-1),o}function Tr(t,e){let n=to(t,e),r=Ft(t,e,n);return Ji(e,Xi(e)+1),xr(n,r)}function it(t,e,n){if(n===4294967295)return!1;let r=ke(n);if(r>=t.maxEntities)return!1;let i=ji(n);return Ft(t,e,r)===i}function Nt(t,e,n){if(n<0)throw new Error("Singleton components do not have component bitmask entries");let r=kr(t,e)+2,i=Math.floor(n/8),o=n%8;return{byteOffset:r+i,bitIndex:o}}function Ar(t,e,n,r){if(!it(t,e,n))return!1;let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and is always present`);let o=ke(n),{byteOffset:s,bitIndex:a}=Nt(t,o,i.index);return(e[s]&1<<a)!==0}function vr(t,e,n,r){if(!it(t,e,n)){let c=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${c} is not alive`)}let i=t.componentByName.get(r);if(!i)throw new Error(`Unknown component: \'${r}\'`);if(i.isSingleton)throw new Error(`Component \'${r}\' is a singleton and cannot be added to entities`);let o=ke(n),{byteOffset:s,bitIndex:a}=Nt(t,o,i.index);e[s]=(e[s]??0)|1<<a}function Ur(t,e,n,r){let i=ke(e);return n.storageOffset+i*n.size+r.offset}function no(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":return(t[e]??0)!==0;case"uint8":return t[e]??0;case"int8":return r.getInt8(e);case"uint16":return r.getUint16(e,!0);case"int16":return r.getInt16(e,!0);case"uint32":return r.getUint32(e,!0);case"int32":return r.getInt32(e,!0);case"entityRef":return r.getUint32(e,!0);case"f32":return r.getFloat32(e,!0);default:throw new Error(`Cannot read primitive type: ${n}`)}}function Cr(t,e,n,r){let i=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(n){case"bool":t[e]=r?1:0;break;case"uint8":t[e]=r&255;break;case"int8":i.setInt8(e,r);break;case"uint16":i.setUint16(e,r,!0);break;case"int16":i.setInt16(e,r,!0);break;case"uint32":i.setUint32(e,r,!0);break;case"int32":i.setInt32(e,r,!0);break;case"entityRef":i.setUint32(e,r,!0);break;case"f32":i.setFloat32(e,r,!0);break;default:throw new Error(`Cannot write primitive type: ${n}`)}}function ro(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0)}function io(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0)}function oo(t,e,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength);r.setFloat32(e,n[0],!0),r.setFloat32(e+4,n[1],!0),r.setFloat32(e+8,n[2],!0),r.setFloat32(e+12,n[3],!0)}function Ir(t,e,n,r,i,o){if(!it(t,e,n))return;let s=t.componentByName.get(r);if(!s)throw new Error(`Unknown component: \'${r}\'`);if(s.isSingleton)throw new Error(`Component \'${r}\' is a singleton; use singleton accessors instead`);if(!nt(t,e,n,s))return;let a=s.fields.find(f=>f.name===i);if(!a)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let c=Ur(t,n,s,a),u=a.type;if(a.arrayLength!==void 0&&a.arrayElementType!==void 0){if(o===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(o<0||o>=a.arrayLength)throw new Error(`Array index ${o} out of bounds for ${r}.${i} (length: ${a.arrayLength})`);let f=It[a.arrayElementType];if(f===void 0)throw new Error(`Unknown array element type: ${a.arrayElementType}`);c+=o*f,u=a.arrayElementType}return no(e,c,u)}function w(t,e,n,r,i,o,s){if(!it(t,e,n)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} is not alive`)}let a=t.componentByName.get(r);if(!a)throw new Error(`Unknown component: \'${r}\'`);if(!nt(t,e,n,a)){let d=n.toString(16).padStart(8,"0").toUpperCase();throw new Error(`Entity 0x${d} does not have component \'${r}\'`)}let c=a.fields.find(d=>d.name===i);if(!c)throw new Error(`Unknown field \'${i}\' in component \'${r}\'`);let u=Ur(t,n,a,c),f=c.type;if(c.arrayLength!==void 0&&c.arrayElementType!==void 0){if(s===void 0)throw new Error(`Field \'${r}.${i}\' is an array, index required`);if(s<0||s>=c.arrayLength)throw new Error(`Array index ${s} out of bounds for ${r}.${i} (length: ${c.arrayLength})`);let d=It[c.arrayElementType];if(d===void 0)throw new Error(`Unknown array element type: ${c.arrayElementType}`);u+=s*d,f=c.arrayElementType}Cr(e,u,f,o)}function nt(t,e,n,r){if(r.isSingleton)throw new Error(`Component \'${r.name}\' is a singleton and cannot be queried per entity`);let i=ke(n),{byteOffset:o,bitIndex:s}=Nt(t,i,r.index);return(e[o]&1<<s)!==0}function Br(t,e,n,r){if(n.length===0)throw new Error("Query must include at least one component");let i=[];for(let a of n){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);i.push(c)}let o=[];if(r)for(let a of r){let c=t.componentByName.get(a);if(!c)throw new Error(`Unknown component: \'${a}\'`);if(c.isSingleton)throw new Error(`Singleton component \'${a}\' cannot be used in queries`);o.push(c)}function*s(){for(let a=0;a<t.maxEntities;a++){let c=Ft(t,e,a),u=xr(a,c),f=!0;for(let d of i)if(!nt(t,e,u,d)){f=!1;break}if(f){for(let d of o)if(nt(t,e,u,d)){f=!1;break}f&&(yield u)}}}return s()}function Pr(t,e){if(!t.events||!t.eventByName)throw new Error("Schema has no events");let n=t.eventByName.get(e);if(!n)throw new Error(`Unknown event: \'${e}\'`);return n}function _r(t,e,n){let r=Pr(t,n);new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(r.storageOffset,0,!0)}function $r(t,e,n,r){let i=Pr(t,n),o=new DataView(e.buffer,e.byteOffset,e.byteLength),s=o.getUint16(i.storageOffset,!0);if(s>=i.maxEvents)return!1;let a=i.storageOffset+2+s*i.recordSize;for(let c of i.fields){let u=r[c.name];if(u===void 0)throw new Error(`Missing required field \'${c.name}\' for event \'${n}\'`);let f=a+c.offset;c.type==="vec2"?ro(e,f,u):c.type==="vec3"?io(e,f,u):c.type==="quat"?oo(e,f,u):Cr(e,f,c.type,u)}return o.setUint16(i.storageOffset,s+1,!0),!0}function ot(t){if(t.length!==32)throw new Error(`stateId must be ${32} bytes, got ${t.length}`);return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}var st=class{constructor(e={}){y(this,"cache");y(this,"maxSize");this.cache=new Map,this.maxSize=e.maxSize??100}store(e){let n=pe(e),r=ot(n);if(this.cache.size>=this.maxSize&&!this.cache.has(r)){let i=this.cache.keys().next().value;i!==void 0&&this.cache.delete(i)}this.cache.set(r,e.slice())}getByStateId(e){let n=ot(e),r=this.cache.get(n);return r?r.slice():void 0}getCached(e,n,r){let i=pe(e),o=rt(i,n,r);return this.getByStateId(o)}has(e){let n=ot(e);return this.cache.has(n)}hasCached(e,n,r){let i=pe(e),o=rt(i,n,r);return this.has(o)}delete(e){let n=ot(e);return this.cache.delete(n)}clear(){this.cache.clear()}get size(){return this.cache.size}keys(){return this.cache.keys()}};var Te=65536,at=class{constructor(e){y(this,"memory",null);y(this,"plugins",[]);y(this,"options");y(this,"stateSize",null);y(this,"statePtr",null);y(this,"heapPos",0);y(this,"arenaResetMark",0);y(this,"inputCodec",null);y(this,"playerEntities",new Map);y(this,"hostAlloc",(e,n)=>{let r=this.memory;if(!r)throw new Error("Memory not initialized for host_alloc");let i=this.heapPos+n-1&~(n-1),o=i+e,s=r.buffer.byteLength;if(o>s){let c=Math.ceil((o-s)/Te);if(this.options.debug){let u=(s+c*Te)/1048576;console.warn(`[mt] WASM memory grew to ${u.toFixed(1)}MB`)}r.grow(c)}return new Uint8Array(r.buffer).fill(0,i,o),this.heapPos=o,i});this.options=e}markArenaReset(){this.arenaResetMark=this.heapPos}resetArena(){this.heapPos=this.arenaResetMark}async init(){if(this.stateSize=this.options.stateSchema.totalSize,!this.stateSize)throw new Error("State schema total size is required");this.options.inputSchema&&(this.inputCodec=br(this.options.inputSchema));let e=4*1024*1024,n=this.options.plugins??(this.options.moduleBytes?[{name:"main",wasmBytes:this.options.moduleBytes,reservedBytes:e}]:[]);for(let c of n)if(typeof c.reservedBytes!="number"||c.reservedBytes<=0)throw new Error(`Plugin "${c.name}" requires reservedBytes > 0`);let r=n.reduce((c,u)=>{let f=Math.ceil(u.reservedBytes/Te)*Te;return c+f},0),i=1024*1024*5,o=this.stateSize+i+r,s=Math.ceil(o/Te);this.memory=new WebAssembly.Memory({initial:s}),this.heapPos=r;let a={env:{memory:this.memory,host_alloc:this.hostAlloc,abort:()=>{throw new Error("WASM abort called")}}};for(let c of n){let u=c.wasmBytes.slice().buffer,f=await WebAssembly.compile(u),d=await WebAssembly.instantiate(f,a);if(typeof d.exports.apply!="function")throw new Error(`Plugin "${c.name}" missing required apply() export`);this.plugins.push({name:c.name,instance:d,exports:d.exports})}return this.statePtr=this.hostAlloc(this.stateSize,8),this.markArenaReset(),this}findPlayerEntity(e,n){let r=this.options.stateSchema,i=this.playerEntities.get(n);if(i!==void 0){if(Ar(r,e,i,"Player"))return i;this.playerEntities.delete(n)}for(let o of Br(r,e,["Player"]))if(Ir(r,e,o,"Player","index")===n)return this.playerEntities.set(n,o),o}spawnPlayerEntity(e,n){let r=this.options.stateSchema,i=Tr(r,e);return vr(r,e,i,"Player"),w(r,e,i,"Player","index",n),this.playerEntities.set(n,i),i}findOrSpawnPlayerEntity(e,n){let r=this.findPlayerEntity(e,n);return r!==void 0?r:this.spawnPlayerEntity(e,n)}resetPlayerControls(e,n){let r=this.options.stateSchema;if(this.inputCodec)for(let i of this.inputCodec.schema.controls){if(i.retain==="always")continue;let o=i.field.type,s=i.name;if(o==="vec2")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0);else if(o==="vec3")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0);else if(o==="quat")w(r,e,n,"Player",`${s}_x`,0),w(r,e,n,"Player",`${s}_y`,0),w(r,e,n,"Player",`${s}_z`,0),w(r,e,n,"Player",`${s}_w`,1);else if(o.startsWith("flags"))w(r,e,n,"Player",s,0);else if(o.includes("[")){let a=o.match(/\\[(\\d+)\\]/);if(a?.[1]){let c=parseInt(a[1],10);for(let u=0;u<c;u++)w(r,e,n,"Player",s,0,u)}}else w(r,e,n,"Player",s,o==="bool"?!1:0)}}writeControlsToPlayer(e,n,r){let i=this.options.stateSchema,o=this.options.inputSchema;if(o)for(let s of o.controls){if(!r.hasControl(s.name))continue;let a=s.type,c=s.name;if(a==="vec2"){let[u,f]=r.getVec2(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f)}else if(a==="vec3"){let[u,f,d]=r.getVec3(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d)}else if(a==="quat"){let[u,f,d,g]=r.getQuat(c);w(i,e,n,"Player",`${c}_x`,u),w(i,e,n,"Player",`${c}_y`,f),w(i,e,n,"Player",`${c}_z`,d),w(i,e,n,"Player",`${c}_w`,g)}else if(a.startsWith("flags")){let u=r.getControl(c);w(i,e,n,"Player",c,u)}else if(a.includes("[")){let u=r.getControl(c);for(let f=0;f<u.length;f++)w(i,e,n,"Player",c,u[f],f)}else{let u=r.getControl(c);w(i,e,n,"Player",c,u)}}}pushCommands(e,n,r){let i=this.options.stateSchema,o=r.getCommands();for(let s of o){let a=s.name.charAt(0).toUpperCase()+s.name.slice(1)+"Command",c={player_index:n};for(let[u,f]of Object.entries(s))u!=="name"&&(Array.isArray(f)?f.length===2?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1]):f.length===3?(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2]):f.length===4&&(c[`${u}_x`]=f[0],c[`${u}_y`]=f[1],c[`${u}_z`]=f[2],c[`${u}_w`]=f[3]):c[u]=f);$r(i,e,a,c)}}clearCommandEvents(e){let n=this.options.stateSchema,r=this.options.inputSchema;if(!(!r||!n.events))for(let i of r.commands){let o=i.name.charAt(0).toUpperCase()+i.name.slice(1)+"Command";n.eventByName?.has(o)&&_r(n,e,o)}}decodePayloadsToState(e,n){if(this.inputCodec)for(let r=0;r<n.length;r++){let i=n[r];if(i===void 0)continue;let o=this.findOrSpawnPlayerEntity(e,r);if(this.resetPlayerControls(e,o),i.length===0)continue;let s=this.inputCodec.decode(i);this.writeControlsToPlayer(e,o,s),this.pushCommands(e,r,s)}}transition(e,n){let r=this.stateSize;if(r===null||e.length!==r)throw new Error(`State size mismatch: expected ${this.stateSize}, got ${e.length}`);if(this.plugins.length===0||n.length===0)return e.slice();let i=this.memory;if(!i)throw new Error("WASM memory not initialized");let o=this.statePtr;if(o===null||o<0)throw new Error("State pointer not initialized");let s=new Uint8Array(i.buffer);s.set(e,o);for(let a of n){let c=new Uint8Array(i.buffer,o,r);wr(c,a.tick),this.clearCommandEvents(c),this.decodePayloadsToState(c,a.payloads);for(let u of this.plugins)u.exports.apply(o),s=new Uint8Array(i.buffer)}return s.slice(o,o+r)}close(){}getStateSize(){let e=this.stateSize;if(e===null||e<0)throw new Error("State size not initialized");return e}};var W=class{constructor(e=1e3,n=1024){y(this,"buffer");y(this,"windowMs");y(this,"head",0);y(this,"tail",0);y(this,"size",0);this.windowMs=e,this.buffer=new Float64Array(n)}inc(){let e=performance.now();this.buffer[this.head]=e,this.head=(this.head+1)%this.buffer.length,this.size<this.buffer.length?this.size++:this.tail=(this.tail+1)%this.buffer.length}count(e=performance.now()){let n=e-this.windowMs;for(;this.size>0&&!(this.buffer[this.tail]>=n);)this.tail=(this.tail+1)%this.buffer.length,this.size--;return this.size}rate(e=performance.now()){return this.count(e)*(1e3/this.windowMs)}};function so(t){return("moduleBytes"in t||"plugins"in t)&&"stateSchema"in t}function ao(t,e){for(let n=t.length-1;n>=0;n--)if(e(t[n]))return n;return-1}var ct=class{constructor(e){y(this,"debug");y(this,"stateSchema");y(this,"inputs",null);y(this,"cache");y(this,"executor",null);y(this,"genesisStateId");y(this,"options");y(this,"ticking",!1);y(this,"tickGraceMs");y(this,"tickLag");y(this,"maxBatchSize");y(this,"currentState");y(this,"currentTick");y(this,"currentNodeId");y(this,"prevOnStateUpdateState",null);y(this,"onStateUpdate");y(this,"syncCheckpoint");y(this,"stateHistory",[]);y(this,"maxHistory",64);y(this,"stats");y(this,"loop",()=>{this._loop().catch(co)});y(this,"_loop",async()=>{if(this.ticking)try{let e=await this.tick();if(this.onStateUpdate&&this.currentState!==this.prevOnStateUpdateState&&(this.onStateUpdate(this.currentState),this.prevOnStateUpdateState=this.currentState,this.stats.updates.inc()),e.rolledBack&&this.stats.rollbacks.inc(),e.ticksComputed>0)for(let n=0;n<e.ticksComputed;n++)this.stats.executions.inc()}catch(e){console.error("Error in tick loop:",e)}finally{setTimeout(this.loop,this.tickGraceMs)}});e.log&&(this.inputs=e.log),this.debug=e.debug??!1,this.options=e,this.stateSchema=e.stateSchema,this.cache=e.cache??new st,this.genesisStateId=Ot(e.genesisHash),this.tickGraceMs=e.tickGraceMs??10,this.tickLag=e.tickLag??1,this.maxBatchSize=e.maxBatchSize??200,this.stats={rollbacks:new W,executions:new W,updates:new W,cacheHits:new W,cacheMisses:new W},this.currentState=Er(this.stateSchema,e.genesisHash,e.tickRate),this.currentNodeId=null,this.syncCheckpoint=null,this.currentTick=null,this.cache.store(this.currentState)}setLog(e){this.inputs=lt(e)}setOnStateUpdate(e){e===null&&(this.prevOnStateUpdateState=null),this.onStateUpdate=e}async init(){if(!this.inputs)throw new Error("Rollback.init() called before log was configured. Call setLog() first or pass log in options.");let e=this.options.executor;if(so(e)){let r=new at(e);await r.init(),this.executor=r}else this.executor=e;if((await this.tick(0)).ticksComputed===0||this.currentTick===null)throw new Error("Failed to record genesis snapshot");return this.options.disableTicking||(this.ticking=!0,this.loop()),this.getState()}async tick(e){if(!this.inputs)throw new Error("Rollback.tick() called before log was configured. Call setLog() and init() first.");let n=await this.inputs.getTicksAfter(this.currentNodeId,{limit:this.maxBatchSize,lag:this.tickLag});if(e&&(n=n.filter(a=>a.tick<=e)),n.length===0)return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:!1,ticksComputed:0};let r=!1,i=null;this.currentTick!==null&&n[0].tick<this.currentTick&&(r=!0,i=this.currentTick,this.rollbackTo(n[0]));let o=n.filter(a=>this.currentTick===null||a.tick>this.currentTick);if(o.length===0)return this.currentNodeId=n[n.length-1].id,{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId,rolledBack:r,ticksComputed:0};let s=this.processTicks(o,r,i);return{state:this.currentState,tick:this.currentTick,nodeId:this.currentNodeId??"",rolledBack:r,ticksComputed:s}}async getStats(){return{rollbacks:this.stats.rollbacks.rate(),executions:this.stats.executions.rate(),updates:this.stats.updates.rate(),cacheHits:this.stats.cacheHits.rate(),cacheMisses:this.stats.cacheMisses.rate()}}processTicks(e,n=!1,r=null){if(!this.executor)throw new Error("Executor not initialized");let i=0,o=0;for(let u=0;u<e.length;u++){let f=e[u],d=Qe(f.payloads),g=this.cache.getCached(this.currentState,f.tick,d);if(g)this.currentState=g,this.currentTick=f.tick,this.currentNodeId=f.id,o=u+1,f.sync&&this.updateSyncCheckpoint(f);else break}if(n&&r!==null&&o>0)for(let u=0;u<o;u++)e[u].tick<=r&&this.stats.cacheHits.inc();let s=e.slice(o);if(s.length===0)return 0;let a=ao(s,u=>u.sync);if(a>=0){let u=s.slice(0,a+1),f=this.executor.transition(this.currentState,u),d=this.computeBatchStateId(this.currentState,u);$t(f,d);let g=u[u.length-1];this.currentState=f,this.currentTick=g.tick,this.currentNodeId=g.id,i+=u.length,this.cache.store(f),this.recordStateSnapshot(this.currentTick,this.currentNodeId,d),this.updateSyncCheckpoint(g)}let c=a>=0?s.slice(a+1):s;if(c.length>0){let u=this.executor.transition(this.currentState,c),f=this.computeBatchStateId(this.currentState,c);$t(u,f);let d=c[c.length-1];this.currentState=u,this.currentTick=d.tick,this.currentNodeId=d.id,i+=c.length,this.cache.store(u),this.recordStateSnapshot(this.currentTick,this.currentNodeId,f)}if(n&&r!==null)for(let u of s)u.tick<=r&&this.stats.cacheMisses.inc();return i}computeBatchStateId(e,n){let r=pe(e);for(let i of n)r=Sr(r,i.tick,i.payloads);return r}getState(){return this.currentState.slice()}getTick(){return this.currentTick}reset(){this.currentTick=0,this.currentNodeId=null,this.syncCheckpoint=null}close(){this.ticking=!1,this.setOnStateUpdate(null),this.executor&&(this.executor.close(),this.executor=null)}rollbackTo(e){if(this.syncCheckpoint&&this.syncCheckpoint.tick>=e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}if(this.syncCheckpoint&&this.syncCheckpoint.tick<e.tick){this.currentState=this.syncCheckpoint.state.slice(),this.currentTick=this.syncCheckpoint.tick,this.currentNodeId=this.syncCheckpoint.nodeId;return}let n=e.tick-1,r=this.findSnapshotAtOrBefore(n);if(r){let o=this.cache.getByStateId(r.stateId);if(o){this.currentState=o,this.currentTick=r.tick,this.currentNodeId=r.nodeId;return}}let i=this.cache.getByStateId(this.genesisStateId);if(i){this.currentState=i,this.currentTick=0,this.currentNodeId=null;return}}updateSyncCheckpoint(e){this.syncCheckpoint={state:this.currentState.slice(),tick:e.tick,nodeId:e.id}}recordStateSnapshot(e,n,r){this.stateHistory.push({tick:e,nodeId:n,stateId:r.slice()}),this.stateHistory.length>this.maxHistory&&this.stateHistory.shift()}findSnapshotAtOrBefore(e){for(let n=this.stateHistory.length-1;n>=0;n--){let r=this.stateHistory[n];if(r.tick<=e)return r}return null}};function co(t){console.warn("rollback unexpected:",t)}Be(ct);var Ga=null;globalThis.onerror=t=>(console.error("\\u{1F534} FATAL ROLLBACK WORKER ERROR (Uncaught Exception):",t),!0);export{Ga as default};\n/*! Bundled license information:\n\ncomlink/dist/esm/comlink.mjs:\n (**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n *)\n\n@noble/secp256k1/index.js:\n (*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)\n\n@noble/hashes/esm/utils.js:\n (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)\n*/\n';
7228
7221
  function Worker() {
7229
7222
  const blob = new Blob([workerCode], { type: "text/javascript" });
7230
7223
  const url = URL.createObjectURL(blob);
@@ -1 +1 @@
1
- {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../../src/codegen/rust.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAKpB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AA67C/D;;;GAGG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,QAAQ,EAAG,MAAM,CAAU;IACpC,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,eAAe,CAAS;gBAEpB,IAAI,GAAE,gBAAqB;IAKvC,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA+ClD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA2BlD,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,MAAM;CAe7E"}
1
+ {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../../src/codegen/rust.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EAKpB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AA66C/D;;;GAGG;AACH,qBAAa,aAAc,YAAW,SAAS;IAC7C,QAAQ,CAAC,QAAQ,EAAG,MAAM,CAAU;IACpC,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,eAAe,CAAS;gBAEpB,IAAI,GAAE,gBAAqB;IAKvC,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA+ClD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM;IA2BlD,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,MAAM;CAe7E"}
@@ -19,12 +19,8 @@ export interface BatchTransitionExecutor {
19
19
  close: () => void;
20
20
  }
21
21
  export interface TransitionExports {
22
- /** Per-tick transition function - mutates state in-place
23
- * @param statePtr - Pointer to state buffer in WASM memory
24
- * @param outputStateIdPtr - Pointer to 32-byte output stateId (for physics caching)
25
- * @param isSync - 1 if this is a sync tick, 0 otherwise (for cache checkpoint)
26
- */
27
- apply: (statePtr: number, outputStateIdPtr: number, isSync: number) => void;
22
+ /** Per-tick transition function - mutates state in-place */
23
+ apply: (statePtr: number) => void;
28
24
  /** WASM memory */
29
25
  memory: WebAssembly.Memory;
30
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsC,KAAK,WAAW,EAAsB,MAAM,eAAe,CAAC;AACzG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAajB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,WAAW,CAAC;IACxE,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5E,kBAAkB;IAClB,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;CAC5B;AAED,kDAAkD;AAClD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,UAAU,CAAC;IACtB,oFAAoF;IACpF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC5B,sEAAsE;IACtE,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,SAAS,EAAE,CAAC;IACtB,uBAAuB;IACvB,WAAW,EAAE,cAAc,CAAC;IAC5B,uBAAuB;IACvB,WAAW,EAAE,WAAW,CAAC;IACzB,mFAAmF;IACnF,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAYD,qBAAa,QAAS,YAAW,uBAAuB;IACtD,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,OAAO,CAAkB;IAGjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,QAAQ,CAAuB;IAGvC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,cAAc,CAAa;IAGnC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,cAAc,CAAqC;gBAE/C,OAAO,EAAE,eAAe;IAIpC;;;;OAIG;IACH,OAAO,CAAC,SAAS,CA2Bf;IAEF;;;OAGG;IACH,cAAc,IAAI,IAAI;IAItB;;;OAGG;IACH,UAAU,IAAI,IAAI;IAIZ,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IAyF/B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IASzB;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAQ/B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IA6C3B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IA+C7B;;;OAGG;IACH,OAAO,CAAC,YAAY;IAuCpB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAe1B;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IA+B7B;;;;;;OAMG;IACH,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,UAAU;IA2EjE,KAAK,IAAI,IAAI;IAKb,YAAY,IAAI,MAAM;CAOvB"}
1
+ {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsC,KAAK,WAAW,EAAsB,MAAM,eAAe,CAAC;AACzG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAWjB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,WAAW,CAAC;IACxE,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,4DAA4D;IAC5D,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,kBAAkB;IAClB,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;CAC5B;AAED,kDAAkD;AAClD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,UAAU,CAAC;IACtB,oFAAoF;IACpF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC5B,sEAAsE;IACtE,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,SAAS,EAAE,CAAC;IACtB,uBAAuB;IACvB,WAAW,EAAE,cAAc,CAAC;IAC5B,uBAAuB;IACvB,WAAW,EAAE,WAAW,CAAC;IACzB,mFAAmF;IACnF,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAYD,qBAAa,QAAS,YAAW,uBAAuB;IACtD,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,OAAO,CAAkB;IAGjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,QAAQ,CAAuB;IAGvC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,cAAc,CAAa;IAGnC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,cAAc,CAAqC;gBAE/C,OAAO,EAAE,eAAe;IAIpC;;;;OAIG;IACH,OAAO,CAAC,SAAS,CA2Bf;IAEF;;;OAGG;IACH,cAAc,IAAI,IAAI;IAItB;;;OAGG;IACH,UAAU,IAAI,IAAI;IAIZ,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IAyF/B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IASzB;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAQ/B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IA6C3B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IA+C7B;;;OAGG;IACH,OAAO,CAAC,YAAY;IAuCpB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAe1B;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IA+B7B;;;;;;OAMG;IACH,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,UAAU;IAuDjE,KAAK,IAAI,IAAI;IAKb,YAAY,IAAI,MAAM;CAOvB"}
@@ -2237,22 +2237,6 @@ pub const TICK_OFFSET: u32 = 40;
2237
2237
 
2238
2238
  // Tick rate location in header (uses one of the reserved bytes)
2239
2239
  pub const TICK_RATE_OFFSET: u32 = 6;
2240
-
2241
- /// Read the 32-byte state ID from the state header (input state's stateId)
2242
- #[inline]
2243
- pub unsafe fn get_state_id(state: *const u8) -> [u8; 32] {
2244
- let mut id = [0u8; 32];
2245
- core::ptr::copy_nonoverlapping(state.add(STATE_ID_OFFSET as usize), id.as_mut_ptr(), 32);
2246
- id
2247
- }
2248
-
2249
- /// Read output state ID from a pointer (passed by host for cache keying)
2250
- #[inline]
2251
- pub unsafe fn read_output_state_id(ptr: *const u8) -> [u8; 32] {
2252
- let mut id = [0u8; 32];
2253
- core::ptr::copy_nonoverlapping(ptr, id.as_mut_ptr(), 32);
2254
- id
2255
- }
2256
2240
  `;
2257
2241
  }
2258
2242
  function generateComponentOffsets2(schema) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@multitapio/multitap",
3
3
  "type": "module",
4
- "version": "0.0.11",
4
+ "version": "0.0.12",
5
5
  "description": "Rollback networking SDK for fast-paced multiplayer games",
6
6
  "main": "./dist/lib.js",
7
7
  "types": "./dist/lib.d.ts",