@kymoscope/sdk 0.2.0 → 0.3.0

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.
@@ -6,4 +6,4 @@ var e=Symbol(`row`),t=new WeakMap;function n(e){return t.get(e)}function r(e,t){
6
6
  `,t+1))b++;let x=0;for(let t=e.indexOf(`\r`,o);t>=0;t=e.indexOf(`\r`,t+1))e.charCodeAt(t+1)!==10&&x++;b+=x;let C=_.map(()=>new Float64Array(b)),w=_.map(()=>null),T=0,E=(e,t)=>{typeof t==`number`?C[e][T]=t:(C[e][T]=NaN,t!==null&&((w[e]??=[])[T]=t))},D=e=>{let t=d(e,f);if(!(t.length===1&&t[0].trim()===``)){for(let e=0;e<y;e++)E(e,t[e]===void 0?null:c(t[e],g));T++}},O=f.charCodeAt(0),k=O!==9&&e.indexOf(`"`,o)>=0||e.indexOf(``,o)>=0||x>0;for(s=o;s<e.length;){let t=e.indexOf(`
7
7
  `,s);t<0&&(t=e.length);let n=t;n>s&&e.charCodeAt(n-1)===13&&n--;let r=n>s;if(r){let t=e.charCodeAt(n-1);r=t>32&&t<127}if(r&&k)for(let t=s;t<n;t++){let n=e.charCodeAt(t);if(n===13||n===65279||n===34&&O!==9){r=!1;break}}if(r){let t=s;for(let r=0;r<y;r++){if(t>n){E(r,null);continue}let i=ee(e,t,n,O,g);if(i===i){C[r][T]=i,t=S+1;continue}let a=e.indexOf(f,t);(a<0||a>n)&&(a=n),E(r,c(e.slice(t,a),g)),t=a+1}T++}else{let n=e.slice(s,t);for(let e of n.split(/\r\n|\r|\n/)){let t=e.replace(/\uFEFF/g,``).trimEnd();t.length>0&&D(t)}}s=t+1}let ne=C.map(e=>b-T>b/100?e.slice(0,T):e.subarray(0,T));return{meta:a.length>0&&p(a[0],a[1])?a[0]:``,columnNames:m,units:h,table:{keys:_,length:T,values:ne,text:w}}}var x=Array.from({length:23},(e,t)=>10**t),S=0;function ee(e,t,n,r,i){let a=t,o=e.charCodeAt(a),s=o===45;(s||o===43)&&a++;let c=0,l=0,u=0,d=!1,f=!1;for(;a<n;a++)if(o=e.charCodeAt(a),o>=48&&o<=57){if(d=!0,c===0&&o===48){f&&u++;continue}if(++l>15)return NaN;c=c*10+(o-48),f&&u++}else if((o===46||i&&o===44&&r!==44)&&!f)f=!0;else break;if(!d)return NaN;let p=0;if(a<n&&o!==r){if(o!==101&&o!==69)return NaN;a++,o=e.charCodeAt(a);let t=o===45;(t||o===43)&&a++;let i=a;for(;a<n&&(o=e.charCodeAt(a),o!==r);a++){if(o<48||o>57||p>400)return NaN;p=p*10+(o-48)}if(a===i)return NaN;t&&(p=-p)}let m=p-u,h;if(c===0)h=0;else if(m>=0&&m<=22)h=c*x[m];else if(m<0&&m>=-22)h=c/x[-m];else return NaN;return S=a,s?-h:h}function te(e,t){let n=m(e),{headerRow:r,unitsRow:i,dataStartRow:a}=t,o=u(n[r]??``),s=v(n,r),f=[];i!==null&&n[i]!==void 0&&(f=d(n[i],o).map(e=>e.trim())),f.length===0&&(f=s.map(()=>``));let h=n.slice(a),g=l(h.slice(0,10),o),_=s.map((e,t)=>e||`col_${t}`),y=h.length,b=_.map(()=>new Float64Array(y)),x=_.map(()=>null),S=0;for(let e of h){let t=d(e,o);if(!(t.length===1&&t[0].trim()===``)){for(let e=0;e<_.length;e++){let n=t[e]===void 0?null:c(t[e],g);typeof n==`number`?b[e][S]=n:(b[e][S]=NaN,n!==null&&((x[e]??=[])[S]=n))}S++}}return S<y&&(b=b.map(e=>e.slice(0,S))),{meta:n.length>0&&p(n[0],n[1])?n[0]:``,columnNames:s,units:f,table:{keys:_,length:S,values:b,text:x}}}var C=5e3;function w(e,t){let n=T.get(e);n||T.set(e,n=new Map);let r=n.get(t);return r===void 0&&n.set(t,r=E(e,t)),r}var T=new WeakMap;function E(e,t){let r=n(e),i=r?r.keys.lastIndexOf(t):-1;if(r&&i>=0&&!r.text[i]){let e=r.values[i];for(let t=0;t<e.length;t++)if(e[t]===e[t])return!0;return!1}let a=e.length,o=0,s=0,c=Math.max(1,Math.floor(a/C));for(let n=0;n<a;n+=c){let r=e[n][t];r!=null&&(o++,typeof r==`number`&&s++)}if(o===0&&c>1)for(let n=0;n<a&&o<C;n++){let r=e[n][t];r!=null&&(o++,typeof r==`number`&&s++)}return o>0&&s/o>.95}function D(e,t){if(e<=t)return Array.from({length:e},(e,t)=>t);let n=[],r=e/t,i=0;for(let a=0;a<t;a++)n.push(Math.min(e-1,Math.floor(i))),i+=r;return n}var O=[];function k(e){return O.push(e),e}function ne(e){for(let t of O)t.delete(e)}function re(e,t,n,r,i,a){let o=(i-r)/a;if(!(i>r)||a<1)return{x0:r,bucket:0,rows:[],columns:n};let s=Math.floor(a);if(ae(e))return{x0:r,bucket:o,rows:oe(e,t,r,i,o,s),columns:n};let c=new Uint8Array(s),l=t.map(()=>new Float64Array(s).fill(1/0)),u=t.map(()=>new Float64Array(s).fill(-1/0));for(let n=0;n<e.length;n++){let a=e[n];if(!(a>=r&&a<=i))continue;let d=Math.floor((a-r)/o);d>=s&&(d=s-1),c[d]=1;for(let e=0;e<t.length;e++){let r=t[e][n];r===r&&(r<l[e][d]&&(l[e][d]=r),r>u[e][d]&&(u[e][d]=r))}}let d=[];for(let e=0;e<s;e++){if(!c[e])continue;let n={b:e};for(let r=0;r<t.length;r++)l[r][e]!==1/0&&(n[`y${r}lo`]=l[r][e],n[`y${r}hi`]=u[r][e]);d.push(n)}return{x0:r,bucket:o,rows:d,columns:n}}var ie=k(new WeakMap);function ae(e){let t=ie.get(e);if(t===void 0){t=!0;for(let n=0;n<e.length;n++)if(!(e[n]>=(n>0?e[n-1]:-1/0))){t=!1;break}ie.set(e,t)}return t}function oe(e,t,n,r,i,a){let o=t=>{let n=0,r=e.length;for(;n<r;){let i=n+r>>>1;t(e[i])?n=i+1:r=i}return n},s=o(e=>e<n),c=o(e=>e<=r),l=e=>Math.min(a-1,Math.floor((e-n)/i)),u=t.map((e,t)=>`y${t}lo`),d=t.map((e,t)=>`y${t}hi`),f=[],p=s;for(;p<c;){let n=l(e[p]),r=p,i=1;for(;p+i<c&&l(e[p+i])===n;)r=p+i,i*=2;let a=r+1,o=Math.min(p+i,c);for(;a<o;){let t=a+o>>>1;l(e[t])===n?a=t+1:o=t}let s=a,m={b:n};for(let e=0;e<t.length;e++){let n=t[e],r=1/0,i=-1/0;for(let e=p;e<s;e++){let t=n[e];t<r&&(r=t),t>i&&(i=t)}r!==1/0&&(m[u[e]]=r,m[d[e]]=i)}f.push(m),p=s}return f}function se(e,t,n,r,i){let a=(r-n)/i,o=new Map;for(let s=0;s<e.length;s++){let c=e[s],l=t[s];if(c<n||c>r||Number.isNaN(l))continue;let u=Math.floor((c-n)/a);u===i&&(u=i-1);let d=o.get(u);d?(l<d.min&&(d.min=l),l>d.max&&(d.max=l),c<d.firstX&&(d.firstX=c,d.firstY=l),c>d.lastX&&(d.lastX=c,d.lastY=l)):(d={min:l,max:l,firstX:c,firstY:l,lastX:c,lastY:l},o.set(u,d))}let s=[...o.keys()].sort((e,t)=>e-t),c=s.length,l={x:new Float64Array(c),min:new Float64Array(c),max:new Float64Array(c),first:new Float64Array(c),last:new Float64Array(c)};for(let e=0;e<c;e++){let t=s[e],r=o.get(t);l.x[e]=n+(t+.5)*a,l.min[e]=r.min,l.max[e]=r.max,l.first[e]=r.firstY,l.last[e]=r.lastY}return l}function ce(e,t,n,r,i,a){if(!ae(e))return null;let o=t=>{let n=0,r=e.length;for(;n<r;){let i=n+r>>>1;t(e[i])?n=i+1:r=i}return n},s=o(e=>e<r),c=o(e=>e<=i);if(c-s>a)return null;let l=e=>s===0&&c===e.length?e:e.subarray(s,c);return{length:c-s,x:l(e),cols:new Map(n.map((e,n)=>[e,l(t[n])]))}}var le=e=>new DataView(e.buffer,e.byteOffset,e.byteLength);function ue(e,t,n=!0){let r=e.getUint32(n?t:t+4,n);return e.getUint32(n?t+4:t,n)*4294967296+r}function de(e,t,n=!0){let r=e.getUint32(n?t:t+4,n);return e.getInt32(n?t+4:t,n)*4294967296+r}var fe=new TextDecoder(`utf-8`),pe=new TextDecoder(`latin1`);function me(e,t=!1){let n=e.indexOf(0);return(t?pe:fe).decode(n<0?e:e.subarray(0,n))}var he=e=>pe.decode(e),ge=e=>fe.decode(e);function A(e,t,n=0){if(e.length<n+t.length)return!1;for(let r=0;r<t.length;r++)if(e[n+r]!==t.charCodeAt(r))return!1;return!0}var _e={id:`mdf`,label:`ASAM MDF (MF4, MDF 3)`,extensions:[`.mf4`,`.mdf`,`.mf3`,`.dat`],companions:[{ext:`.dbc`,anyName:!0}],sniff:e=>A(e,`MDF `)||A(e,`UnFinMF `)},ve={id:`edf`,label:`EDF / BDF (+)`,extensions:[`.edf`,`.bdf`,`.rec`],sniff:e=>A(e,`0 `)||e[0]===255&&A(e,`BIOSEMI`,1)},ye={id:`xdf`,label:`XDF (Lab Streaming Layer)`,extensions:[`.xdf`],sniff:e=>A(e,`XDF:`)},be={id:`tdms`,label:`NI TDMS`,extensions:[`.tdms`],companions:[{ext:`.tdms_index`}],sniff:e=>A(e,`TDSm`)},xe={id:`mat`,label:`MATLAB MAT (v5, v7.3)`,extensions:[`.mat`],sniff:e=>A(e,`MATLAB 5.0 MAT-file`)||A(e,`MATLAB 7.3 MAT-file`)},Se=`‰HDF\r
8
8
  
9
- `,Ce={id:`hdf5`,label:`HDF5`,extensions:[`.h5`,`.hdf5`,`.hdf`,`.he5`,`.nc`],sniff:e=>[0,512,1024,2048].some(t=>A(e,Se,t))},we={id:`comtrade`,label:`COMTRADE (IEEE C37.111)`,extensions:[`.cfg`,`.cff`],companions:[{ext:`.dat`}],sniff:e=>{let t=he(e.subarray(0,512));if(/^---\s*file type:\s*CFG/i.test(t))return!0;let n=t.split(/\r?\n/);return n.length>2&&/^\s*\d+\s*,\s*\d+A\s*,\s*\d+D\s*$/i.test(n[1])}},Te=`This file couldn't be read (it may be damaged).`,Ee=1032,De=e=>e*Ee+1024,Oe=128e6,j=256,ke=64,M=1e3,N=100,Ae=200,P=(e,t)=>e.length>t?`${e.slice(0,t-1)}…`:e;function je(e){let t=e.notes;for(let n of e.groups){n.name=P(String(n.name),j);for(let e of n.channels)e.name=P(String(e.name),j),e.unit=P(String(e.unit??``),ke);Number.isInteger(n.samples)&&n.samples>=-1||(n.samples=-1),n.rateHz===null||Number.isFinite(n.rateHz)&&n.rateHz>0||(n.rateHz=null),n.time.kind===`rate`&&!(Number.isFinite(n.time.rate)&&n.time.rate>0&&Number.isFinite(n.time.start))&&(t.push(`${n.name}: its sample rate in the file is not usable; samples are numbered instead.`),n.time={kind:`index`},n.rateHz=null)}t.length>N&&t.splice(N,t.length-N,`${t.length-N} more notes.`),e.notes=t.map(e=>P(String(e),M));let n={};for(let[t,r]of Object.entries(e.meta).slice(0,Ae))n[P(t,j)]=P(String(r),M);return e.meta=n,e.events=e.events.filter(e=>Number.isFinite(e.time)).map(e=>({time:e.time,text:P(String(e.text),M)})),e}function Me(e){return e instanceof Error&&(e.constructor===Error||e.name===`KymoscopeError`||e.name===`AbortError`)?e:Error(Te)}function Ne(e,t=[`Time`]){let n=new Set(t.map(e=>e.toLowerCase())),r=new Map;return e.map(e=>{let t=e.trim()||`Channel`,i=t.toLowerCase(),a=t,o=r.get(i)??2;if(n.has(i)){for(;n.has(`${i} (${o})`);)o++;a=`${t} (${o++})`}return r.set(i,o),n.add(a.toLowerCase()),a})}var F=1<<20,Pe=64,Fe=16<<20,Ie=class{size;bytesRead=0;#e;#t=new Map;constructor(e){this.#e=e,this.size=e.size}async read(e,t){let n=Math.min(this.size,e+t);if(n<=e)return new Uint8Array;if(n-e>F)return this.slice(e,n-e);let r=Math.floor(e/F),i=Math.floor((n-1)/F),a=await this.#n(r);if(r===i)return a.subarray(e-r*F,n-r*F);let o=new Uint8Array(n-e),s=a.subarray(e-r*F);return o.set(s),o.set((await this.#n(i)).subarray(0,n-i*F),s.length),o}async slice(e,t){let n=Math.min(this.size,e+t);if(n<=e)return new Uint8Array;let r=new Uint8Array(await this.#e.slice(e,n).arrayBuffer());return this.bytesRead+=r.length,r}async*windows(e,t,n=Fe){let r=Math.min(this.size,e+t);for(let t=e;t<r;t+=n)yield{offset:t,bytes:await this.slice(t,Math.min(n,r-t))}}async#n(e){let t=this.#t.get(e);if(t)return this.#t.delete(e),this.#t.set(e,t),t;let n=await this.slice(e*F,F);return this.#t.size>=Pe&&this.#t.delete(this.#t.keys().next().value),this.#t.set(e,n),n}},Le=`kymoscope-live-key-check-v1`,I=6e5;function L(){let e=globalThis.crypto;if(!e?.subtle)throw Error(`WebCrypto SubtleCrypto is not available`);return e.subtle}function R(e){let t=``;for(let n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}function z(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function B(e){return R(e).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``)}function Re(e){return z(e.replace(/-/g,`+`).replace(/_/g,`/`).padEnd(Math.ceil(e.length/4)*4,`=`))}function ze(e){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return t}function Be(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),R(e)}async function V(e,t){let n=new TextEncoder,r=await L().importKey(`raw`,n.encode(e),`PBKDF2`,!1,[`deriveKey`]);return L().deriveKey({name:`PBKDF2`,salt:z(t),iterations:I,hash:`SHA-256`},r,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}async function Ve(e,t){let n=await L().importKey(`raw`,new TextEncoder().encode(e),`PBKDF2`,!1,[`deriveBits`]),r=await L().deriveBits({name:`PBKDF2`,salt:z(t),iterations:I,hash:`SHA-256`},n,256);return R(new Uint8Array(r))}async function He(e,t){let n=new Uint8Array(12);globalThis.crypto.getRandomValues(n);let r=new TextEncoder().encode(JSON.stringify(t)),i=await L().encrypt({name:`AES-GCM`,iv:n},e,r);return{iv:R(n),ciphertext:R(new Uint8Array(i))}}async function Ue(e,t){let n=z(t.iv),r=z(t.ciphertext),i=await L().decrypt({name:`AES-GCM`,iv:n},e,r);return new Uint8Array(i)}async function We(e,t,n,r){let i=await L().decrypt({name:`AES-GCM`,iv:t,additionalData:r},e,n);return new Uint8Array(i)}async function Ge(e,t){return JSON.parse(new TextDecoder().decode(await Ue(e,t)))}async function Ke(e){return He(e,Le)}async function qe(e,t){try{return await Ge(e,t)===Le}catch{return!1}}async function Je(e){let t=await L().digest(`SHA-256`,new TextEncoder().encode(e));return ze(new Uint8Array(t))}function Ye(){let e=e=>{let t=new Uint8Array(e);return globalThis.crypto.getRandomValues(t),B(t)},t=`kymoscope_${e(24)}.${e(32)}`;return{token:t,prefix:t.slice(0,12)}}var Xe=/^((?:kymoscope|prism)_[A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_-]+))?$/;function Ze(e){let t=Xe.exec(e.trim());return t?{auth:t[1],secret:t[2]??null}:{auth:e,secret:null}}var Qe=new TextEncoder().encode(`kymoscope-device-wrap-v1`);async function $e(){let e=await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),t=new Uint8Array(await L().exportKey(`raw`,e.publicKey));return{privateKey:e.privateKey,publicKey:R(t)}}async function et(e,t){let n=new Uint8Array(e.length+Qe.length);return n.set(e),n.set(Qe,e.length),L().importKey(`raw`,await L().digest(`SHA-256`,n),`AES-GCM`,!1,t)}async function tt(e,t,n){let r=await L().importKey(`raw`,z(t.epk),{name:`ECDH`,namedCurve:`P-256`},!1,[]),i=new Uint8Array(await L().deriveBits({name:`ECDH`,public:r},e,256));return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},await et(i,[`decrypt`]),z(t.ct)))}function nt(e,t,n){return rt(e,t,n)}async function rt(e,t,n,r,i){let a=await L().importKey(`raw`,z(e),{name:`ECDH`,namedCurve:`P-256`},!1,[]),o=r??await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),s=new Uint8Array(await L().deriveBits({name:`ECDH`,public:a},o.privateKey,256)),c=i??globalThis.crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:c,additionalData:new TextEncoder().encode(n)},await et(s,[`encrypt`]),t));return{epk:R(new Uint8Array(await L().exportKey(`raw`,o.publicKey))),iv:R(c),ct:R(l)}}async function it(e,t,n,r){return ut(await tt(e,t,`${n}|${r}`))}var at=new TextEncoder().encode(`kymoscope-sender-bind-v1`),ot=(e,t,n)=>`project:${e}:${t}|${n}`,st=(e,t,n)=>`live_stream:${e}|${t}:${n}`,ct=(e,t,n)=>`bind:${e}|${t}:${n}`;function lt(){return globalThis.crypto.getRandomValues(new Uint8Array(32))}function ut(e){return L().importKey(`raw`,e,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}function dt(e,t,n){return ft(e,t,n)}async function ft(e,t,n,r){let i=r??globalThis.crypto.getRandomValues(new Uint8Array(12)),a=await L().importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`]),o=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},a,t));return{iv:R(i),ct:R(o)}}async function pt(e,t,n){let r=await L().importKey(`raw`,e,`AES-GCM`,!1,[`decrypt`]);return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},r,z(t.ct)))}async function mt(e){let t=new TextEncoder().encode(e),n=new Uint8Array(t.length+at.length);return n.set(t),n.set(at,t.length),new Uint8Array(await L().digest(`SHA-256`,n))}async function ht(e,t,n){let r=await L().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`sign`]);return R(new Uint8Array(await L().sign(`HMAC`,r,new TextEncoder().encode(`sender|${t}|${n}`))))}async function gt(e,t,n,r){let i;try{i=z(r)}catch{return!1}let a=await L().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`verify`]);return L().verify(`HMAC`,a,i,new TextEncoder().encode(`sender|${t}|${n}`))}async function _t(e){let t=ze(new Uint8Array(await L().digest(`SHA-256`,z(e)))).slice(0,16).toUpperCase();return`${t.slice(0,4)}-${t.slice(4,8)}-${t.slice(8,12)}-${t.slice(12,16)}`}var vt=`kymoscope-workbook-export-v2`,yt=(e,t)=>`workbook:${e}:${t}`,bt=(e,t,n)=>`workbook:${e}|${t}:${n}`,xt=(e,t)=>`kymoscope-recovery-key-v1|${e}|${t}`,St=(e,t,n)=>`recovery:${e}|${t}:${n}`;function Ct(e,t,n){return wt(e,t,n)}async function wt(e,t,n,r){let i=r??globalThis.crypto.getRandomValues(new Uint8Array(12)),a=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},e,t));return{iv:R(i),ct:R(a)}}async function Tt(e,t,n){return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},e,z(t.ct)))}function Et(e,t,n){return Dt(e,t,n)}async function Dt(e,t,n,r,i){let a=R(r??globalThis.crypto.getRandomValues(new Uint8Array(16))),o=await wt(await V(e,a),t,n,i);return{kdf:{name:`PBKDF2`,hash:`SHA-256`,iterations:I,salt:a},...o}}async function Ot(e,t,n){let r=t?.kdf;if(r?.name!==`PBKDF2`||r.hash!==`SHA-256`||r.iterations!==I||typeof r.salt!=`string`)throw Error(`unsupported_kdf`);return Tt(await V(e,r.salt),t,n)}var H=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`;function kt(e){let t=0,n=0,r=``;for(let i of e)for(n=(n<<8|i)&1048575,t+=8;t>=5;)r+=H[n>>>t-5&31],t-=5;return t>0&&(r+=H[n<<5-t&31]),r.match(/.{1,4}/g).join(`-`)}function At(e){let t=e.toUpperCase().replace(/[^A-Z2-7]/g,``);if(t.length!==52)return null;let n=[],r=0,i=0;for(let e of t)i=(i<<5|H.indexOf(e))&4095,r+=5,r>=8&&(n.push(i>>>r-8&255),r-=8);return n.length===32?new Uint8Array(n):null}async function jt(){let e=await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!0,[`deriveBits`]),t=await L().exportKey(`jwk`,e.privateKey),n=new Uint8Array(await L().exportKey(`raw`,e.publicKey));return{d:Re(t.d),publicKey:R(n)}}function Mt(e,t){let n=z(t);return L().importKey(`jwk`,{kty:`EC`,crv:`P-256`,ext:!1,x:B(n.subarray(1,33)),y:B(n.subarray(33,65)),d:B(e)},{name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`])}var Nt=k(new WeakSet),Pt={length:0,x:new Float64Array,cols:new Map};function U(e,t,n,r){let i=t.length,a=t.map((e,t)=>`y${t}lo`),o=t.map((e,t)=>`y${t}hi`),s=new Uint8Array(e.length),c=0;for(let t=0;t<e.length;t++){let n=e[t];for(let e=0;e<i;e++){let r=n[o[e]];if(r!=null&&Number(r)!==Number(n[a[e]])){s[t]=1;break}}c+=1+s[t]}let l=new Float64Array(c),u=t.map(()=>new Float64Array(c).fill(NaN)),d,f=0,p=null;for(let t=0;t<e.length;t++){let m=e[t],h=n+(Number(m.b)+.5)*r,g=Number(m.pass??0);p!==null&&g!==p&&((d??=new Uint8Array(c))[f]=1),p=g,l[f]=h;for(let e=0;e<i;e++){let t=m[a[e]];t!=null&&(u[e][f]=Number(t))}if(f++,s[t]){l[f]=h;for(let e=0;e<i;e++){let t=m[o[e]];t!=null&&(u[e][f]=Number(t))}f++}}return{length:c,x:l,brk:d,cols:new Map(t.map((e,t)=>[e,u[t]]))}}function Ft(e,t){if(t>=e.length)return e;let n=new Map;for(let[r,i]of e.cols)n.set(r,i.subarray(0,t));return{length:t,x:e.x.subarray(0,t),brk:e.brk?.subarray(0,t),cols:n}}function It(e,t){if(t===void 0||e.length===0||e.x[e.length-1]<=t)return e;let n=0,r=e.length;for(;n<r;){let i=n+r>>1;e.x[i]<=t?n=i+1:r=i}return Ft(e,n)}function Lt(e,t,n){let r=(t,n)=>{let r=0,i=e.length;for(;r<i;){let a=r+i>>1;(n?e.x[a]<=t:e.x[a]<t)?r=a+1:i=a}return r},i=r(t,!1),a=r(n,!0);if(i===0&&a===e.length)return e;let o=new Map;for(let[t,n]of e.cols)o.set(t,n.subarray(i,a));return{length:a-i,x:e.x.subarray(i,a),brk:e.brk?.subarray(i,a),cols:o}}function Rt(e,t){let n=[];for(let r=0;r<e.length;r++)t(e.x[r])&&n.push(r);if(n.length===e.length)return e;let r=(e,t)=>{for(let r=0;r<n.length;r++)t[r]=e[n[r]];return t},i=new Map;for(let[t,a]of e.cols)i.set(t,r(a,new Float64Array(n.length)));return{length:n.length,x:r(e.x,new Float64Array(n.length)),brk:e.brk&&r(e.brk,new Uint8Array(n.length)),cols:i}}function zt(e,t,i,a,o=``,s=0){let c=n(e),l=D(e.length,a),u=t=>{let n=c&&r(c,t);return n?e=>n[e]:n=>{let r=e[n][t];return typeof r==`number`?r:NaN}},d=u(t),f=[];for(let e of l)Number.isNaN(d(e))||f.push(e);let p=f.length,m=p===e.length&&s===0,h=c&&r(c,t),g=m&&h?h:Float64Array.from(f,e=>d(e)+s),_=new Map;for(let e of i){let t=c&&r(c,e);if(c&&!t)continue;if(m&&t){_.set(e+o,t);continue}let n=u(e),i=Float64Array.from(f,n);!t&&!i.some(e=>!Number.isNaN(e))||_.set(e+o,i)}return{length:p,x:g,cols:_}}function Bt(e,t,n){if(!t&&n===0)return e;let r=e.x;if(n!==0){r=new Float64Array(e.length);for(let t=0;t<e.length;t++)r[t]=e.x[t]+n}let i=new Map;for(let[n,r]of e.cols)i.set(n+t,r);return{length:e.length,x:r,brk:e.brk,cols:i}}function Vt(e){let t=e.filter(e=>e.length>0);if(t.length===0)return Pt;if(t.length===1)return t[0];let n=t.reduce((e,t)=>e+t.length,0),r=new Float64Array(n),i=new Set;for(let e of t)for(let t of e.cols.keys())i.add(t);let a=new Map;for(let e of i)a.set(e,new Float64Array(n).fill(NaN));let o,s=0;for(let e of t){r.set(e.x,s),e.brk&&(o??=new Uint8Array(n)).set(e.brk,s);for(let[t,n]of e.cols)a.get(t).set(n,s);s+=e.length}return{length:n,x:r,brk:o,cols:a}}var W=64,Ht=class{nc;bucket;b;lo;hi;start=0;end=0;rowP;px=new Float64Array;pv=[];pStart=0;pEnd=0;mStart=0;mEnd=0;dirty=1/0;dirtyFront=-1;invalid=!0;constructor(e,t,n=1024){this.nc=e,this.bucket=t,this.b=new Float64Array(n),this.rowP=new Float64Array(n),this.lo=Array.from({length:e},()=>new Float64Array(n)),this.hi=Array.from({length:e},()=>new Float64Array(n))}get count(){return this.end-this.start}lowerBound(e){let t=this.start,n=this.end;for(;t<n;){let r=t+n>>>1;this.b[r]<e?t=r+1:n=r}return t}relocate(e,t){let n=this.count,r=n=>{if(t===n.length&&e<=this.start)return n.copyWithin(e,this.start,this.end),n;let r=new Float64Array(t);return r.set(n.subarray(this.start,this.end),e),r};this.b=r(this.b),this.rowP=r(this.rowP),this.lo=this.lo.map(r),this.hi=this.hi.map(r);let i=e-this.start;this.mStart+=i,this.mEnd+=i,this.dirty!==1/0&&(this.dirty+=i),this.dirtyFront>=0&&(this.dirtyFront+=i),this.start=e,this.end=e+n}touch(e){e===this.start&&e+1<this.mEnd?this.dirtyFront=e:e<this.dirty&&(this.dirty=e)}clearRow(e){for(let t=0;t<this.nc;t++)this.lo[t][e]=NaN,this.hi[t][e]=NaN;this.touch(e)}row(e){let t=this.end;if(t>this.start&&this.b[t-1]===e)return t-1;if(t===this.start||e>this.b[t-1])return t===this.b.length&&(this.start>this.count?this.relocate(0,this.b.length):this.relocate(0,Math.max(1024,this.b.length*2))),this.b[this.end]=e,this.clearRow(this.end),this.end++;let n=this.lowerBound(e);if(this.b[n]===e)return n;if(n!==this.start)throw Error(`TimeBuckets: insert inside the rows`);if(this.start===0){let e=Math.max(1024,this.count);this.relocate(e,2*e+this.count)}return this.start--,this.b[this.start]=e,this.clearRow(this.start),this.invalid=!0,this.start}dropBelow(e){this.start=this.lowerBound(e),this.start===this.end&&(this.start=this.end=0,this.invalid=!0)}reset(e){this.clearRow(e)}fold(e,t,n,r){this.touch(e);for(let i=0;i<t.length;i++){let a=t[i],o=1/0,s=-1/0;for(let e=n;e<r;e++){let t=a[e];t<o&&(o=t),t>s&&(s=t)}if(o===1/0)continue;let c=this.lo[i],l=this.hi[i],u=c[e],d=l[e];c[e]=u<=o?u:o,l[e]=d>=s?d:s}}toRows(e,t,n,r,i){let a=[];for(let o=e;o<t;o++){let e={...n,b:this.b[o],pass:0};for(let t=0;t<this.nc;t++){let n=this.lo[t][o];n===n&&(e[r[t]]=n,e[i[t]]=this.hi[t][o])}a.push(e)}return a}pointsOf(e){for(let t=0;t<this.nc;t++){let n=this.hi[t][e];if(n===n&&n!==this.lo[t][e])return 2}return 1}writeRow(e,t,n){let r=(this.b[e]+.5)*this.bucket;this.px[t]=r;for(let n=0;n<this.nc;n++)this.pv[n][t]=this.lo[n][e];if(n===2){this.px[t+1]=r;for(let n=0;n<this.nc;n++)this.pv[n][t+1]=this.hi[n][e]}}reserve(e){if(this.pEnd+e<=this.px.length)return;let t=this.pEnd-this.pStart,n=W-this.pStart,r=2*(t+e+W)<=this.px.length,i=r?this.px.length:Math.max(4096,2*(t+e+W)),a=e=>{if(r)return e.copyWithin(W,this.pStart,this.pEnd),e;let t=new Float64Array(i);return t.set(e.subarray(this.pStart,this.pEnd),W),t};this.px=a(this.px),this.pv=this.pv.map(a);for(let e=this.mStart;e<this.mEnd;e++)this.rowP[e]+=n;this.pStart=W,this.pEnd=W+t}writeFrom(e){this.mEnd=e;for(let t=e;t<this.end;t++){let e=this.pointsOf(t);this.reserve(e),this.rowP[t]=this.pEnd,this.writeRow(t,this.pEnd,e),this.pEnd+=e,this.mEnd=t+1}}sync(){if(this.pv.length!==this.nc&&(this.pv=Array.from({length:this.nc},()=>new Float64Array)),this.invalid||this.start<this.mStart||this.start>=this.mEnd)this.pStart=this.pEnd=W,this.mStart=this.mEnd=this.start,this.reserve(0),this.writeFrom(this.start);else{this.start>this.mStart&&(this.mStart=this.start,this.pStart=this.rowP[this.start]);let e=Math.max(this.start,Math.min(this.dirty,this.mEnd)),t=this.dirtyFront;if(t===this.start)if(t+1<e){let e=this.pointsOf(t),n=this.rowP[t+1]-e;if(n<0)return this.invalid=!0,this.sync();this.rowP[t]=n,this.pStart=n,this.writeRow(t,n,e)}else e=t;e<this.end&&(e<this.mEnd&&(this.pEnd=this.rowP[e]),this.writeFrom(e))}this.dirty=1/0,this.dirtyFront=-1,this.invalid=!1}toPoints(e,t,n){this.sync();let r=e<t?this.rowP[e]:0,i=e<t?t<this.mEnd?this.rowP[t]:this.pEnd:0;return{length:i-r,x:this.px.subarray(r,i),cols:new Map(n.map((e,t)=>[e,this.pv[t].subarray(r,i)]))}}};function Ut(e){if(!(e>0)||!Number.isFinite(e))return e;let t=10**Math.floor(Math.log10(e)),n=e/t;return(n<1.5?1:n<3.5?2:n<7.5?5:10)*t}function Wt(e,t,n,r,i){if(n instanceof Float64Array||n instanceof Float32Array)e.set(n.subarray(r,r+i),t);else for(let a=0;a<i;a++)e[t+a]=+n[r+a]}function Gt(e,t){let n=e.t.length,r=Object.keys(e.columns).filter(e=>e!==t);for(let t of r)if(e.columns[t].length!==n)throw RangeError(`live column "${t}" has ${e.columns[t].length} values for ${n} times`);return r}var Kt=`ElapsedTime`,qt=[];function Jt(e){let t=qt[e];if(!t){let n=Array.from({length:e},(e,t)=>`y${t}lo`),r=Array.from({length:e},(e,t)=>`y${t}hi`);t={lo:n,hi:r,row:JSON.parse(JSON.stringify(Object.fromEntries([[`b`,0],[`pass`,0],...n.flatMap((e,t)=>[[e,null],[r[t],null]])])))},qt[e]=t}return t}var Yt=4096,Xt=4,Zt=class{time;alloc;maxBytes;cols=new Map;head=0;count=0;full=!1;pushed=0;clears=0;signalOrder=[];unitByName=new Map;capacity;timeColumn;timeUnit;lastBackAbs=-1;get timeMonotonic(){return this.lastBackAbs<=this.pushed-this.size}lastPushT=-1/0;decCache=null;passTrack=null;prevCyclePts=null;curCyclePts=null;trackers=new Map;debugLastScan=0;timeDec=null;mirrors=new Map;historyDec=null;constructor(e,t=Kt,n=`s`,r=1/0){this.capacity=Math.max(2,Math.floor(e)),this.timeColumn=t,this.timeUnit=n,this.maxBytes=r,this.alloc=Math.min(this.capacity,Yt),this.time=new Float64Array(this.alloc)}get maxSamples(){return this.capacity}newColumn(){return new Float64Array(this.alloc).fill(NaN)}grow(){let e=Math.min(this.capacity,this.alloc*2);if(e*8*(1+this.cols.size)>this.maxBytes){this.capacity=this.alloc;return}let t=t=>{let n=new Float64Array(e);return n.set(t),n};this.time=t(this.time);for(let[e,n]of this.cols)this.cols.set(e,t(n));this.alloc=e}get version(){return this.pushed+this.clears}get size(){return this.full?this.capacity:this.count}push(e){for(let[t,n]of this.trackers){let r=t===this.timeColumn?e.t:e.signals.find(e=>e.name===t)?.value;r!==void 0&&Number.isFinite(r)&&this.advanceCycle(t,n,r,this.pushed)}e.t<this.lastPushT&&this.steppedBack(this.pushed),this.lastPushT=e.t,!this.full&&this.count===this.alloc&&(this.grow(),this.count===this.capacity&&(this.full=!0));let t=this.full?this.head:this.count;this.time[t]=e.t;for(let e of this.signalOrder)this.cols.get(e)[t]=NaN;for(let n of e.signals){if(this.computed.has(n.name))continue;let e=this.cols.get(n.name);e?n.unit&&n.unit!==this.unitByName.get(n.name)&&(this.unitByName.get(n.name)&&this.dropColumnSamples(n.name,e),this.unitByName.set(n.name,n.unit)):(e=this.newColumn(),this.cols.set(n.name,e),this.signalOrder.push(n.name),this.unitByName.set(n.name,n.unit??``)),e[t]=n.value}this.pushed++,this.full?this.head=(this.head+1)%this.capacity:(this.count++,this.count===this.capacity&&(this.full=!0))}pushColumns(e){let t=e.t,n=t.length,r=Gt(e,this.timeColumn);if(n===0)return;if(this.trackers.size>0){for(let i=0;i<n;i++)this.push({t:t[i],signals:r.map(t=>({name:t,unit:e.units?.[t],value:e.columns[t][i]}))});return}for(let t of r){if(this.computed.has(t))continue;let n=e.units?.[t],r=this.cols.get(t);r?n&&n!==this.unitByName.get(t)&&(this.unitByName.get(t)&&this.dropColumnSamples(t,r),this.unitByName.set(t,n)):(this.cols.set(t,this.newColumn()),this.signalOrder.push(t),this.unitByName.set(t,n??``))}for(let e=0;e<n;e++){let n=+t[e];n<this.lastPushT&&this.steppedBack(this.pushed+e),this.lastPushT=n}let i=Math.max(0,n-this.capacity);for(this.pushed+=i;i<n;){!this.full&&this.count===this.alloc&&(this.grow(),this.count===this.capacity&&(this.full=!0));let r=this.full?this.head:this.count,a=Math.min(n-i,this.full?this.capacity-this.head:this.alloc-this.count);Wt(this.time,r,t,i,a);for(let t of this.signalOrder){let n=this.cols.get(t),o=this.computed.has(t)?void 0:e.columns[t];o?Wt(n,r,o,i,a):n.fill(NaN,r,r+a)}this.pushed+=a,i+=a,this.full?this.head=(this.head+a)%this.capacity:(this.count+=a,this.count===this.capacity&&(this.full=!0))}}steppedBack(e){this.lastBackAbs=e,this.timeDec=null}dropColumnSamples(e,t){t.fill(NaN),this.decCache=null,this.timeDec=null;for(let t of this.trackers.values())t.previous?.cols.delete(e);this.clears++}clear(){this.clears++,this.head=0,this.count=0,this.full=!1,this.pushed=0,this.cols.clear(),this.computed.clear(),this.signalOrder.length=0,this.unitByName.clear(),this.decCache=null,this.timeDec=null,this.lastBackAbs=-1,this.lastPushT=-1/0,this.trackers.clear()}columnArray(e){return e===this.timeColumn?this.time:this.cols.get(e)}physOf(e){let t=e-(this.pushed-this.size);return this.full?(this.head+t)%this.capacity:t}copyRange(e,t,n,r=[this.timeColumn,...this.signalOrder]){let i=Math.max(t,this.pushed-this.size),a=Math.max(0,n-i),o=this.columnArray(e),s=new Float64Array(a),c=new Map;for(let e of r)this.columnArray(e)&&c.set(e,new Float64Array(a));for(let e=0;e<a;e++){let t=this.physOf(i+e);s[e]=o?o[t]:NaN;for(let[n,r]of c)r[e]=this.columnArray(n)[t]}return{x:s,cols:c}}advanceCycle(e,t,n,r){Number.isFinite(t.lastX)&&n<t.lastX&&t.lastX-n>(t.hi-t.lo)/2?(t.previous=this.copyRange(e,t.start,r),t.start=r,t.lo=t.hi=n):Number.isFinite(t.lastX)?(n<t.lo&&(t.lo=n),n>t.hi&&(t.hi=n)):t.lo=t.hi=n,n<t.allLo&&(t.allLo=n),n>t.allHi&&(t.allHi=n),t.lastX=n}trackCycles(e){if(this.trackers.has(e))return;let t={start:this.pushed-this.size,lo:0,hi:0,lastX:NaN,allLo:1/0,allHi:-1/0,previous:null},n=this.columnArray(e),r=this.pushed-this.size;if(n)for(let i=r;i<this.pushed;i++){let r=n[this.physOf(i)];Number.isFinite(r)&&this.advanceCycle(e,t,r,i)}this.trackers.set(e,t)}cycleSpan(e){let t=this.trackers.get(e);return t&&t.allHi>t.allLo?[t.allLo,t.allHi]:null}decimateCycle(e,t,n,r,i,a){let o=this.trackers.get(e),s=n.filter(e=>this.cols.has(e));if(!o)return{x0:r,bucket:0,rows:[],columns:s};let c=t===`previous`?o.previous:this.copyRange(e,o.start,this.pushed,s);if(!c)return{x0:r,bucket:0,rows:[],columns:s};let l=s.map(e=>c.cols.get(e)??new Float64Array(c.x.length).fill(NaN));return re(c.x,l,s,r,i,a)}decimateCyclePoints(e,t,n,r,i,a){let o=()=>{let o=this.decimateCycle(e,t,n,r,i,a);return U(o.rows,o.columns,o.x0,o.bucket)},s=this.trackers.get(e),c=Math.floor(a),l=this.columnArray(e);if(!s||!l||!(i>r)||c<1)return o();let u=n.filter(e=>this.cols.has(e)),d=u.join(` `);if(t===`previous`){let e=this.prevCyclePts;if(e&&e.snap===s.previous&&e.x0===r&&e.x1===i&&e.mp===a&&e.colsKey===d)return e.pts;let t=o();return this.prevCyclePts={snap:s.previous,x0:r,x1:i,mp:a,colsKey:d,pts:t},t}if(s.start<this.pushed-this.size)return o();let f=this.curCyclePts;if((!f||f.key!==e||f.start!==s.start||f.x0!==r||f.x1!==i||f.mp!==a||f.colsKey!==d||f.clears!==this.clears)&&(f=this.curCyclePts={key:e,start:s.start,x0:r,x1:i,mp:a,colsKey:d,clears:this.clears,done:s.start,pts:null,seen:new Uint8Array(c),lo:u.map(()=>new Float64Array(c).fill(1/0)),hi:u.map(()=>new Float64Array(c).fill(-1/0))}),f.pts&&f.done===this.pushed)return f.pts;let p=(i-r)/a,m=u.map(e=>this.columnArray(e));for(let e=f.done;e<this.pushed;e++){let t=this.physOf(e),n=l[t];if(!(n>=r&&n<=i))continue;let a=Math.floor((n-r)/p);a>=c&&(a=c-1),f.seen[a]=1;for(let e=0;e<m.length;e++){let n=m[e][t];n===n&&(n<f.lo[e][a]&&(f.lo[e][a]=n),n>f.hi[e][a]&&(f.hi[e][a]=n))}}f.done=this.pushed;let h=0,g=new Uint8Array(c);for(let e=0;e<c;e++)if(f.seen[e]){for(let t=0;t<m.length;t++){let n=f.hi[t][e];if(n!==-1/0&&n!==f.lo[t][e]){g[e]=1;break}}h+=1+g[e]}let _=new Float64Array(h),v=u.map(()=>new Float64Array(h)),y=0;for(let e=0;e<c;e++){if(!f.seen[e])continue;let t=r+(e+.5)*p;_[y]=t;for(let t=0;t<m.length;t++){let n=f.lo[t][e];v[t][y]=n===1/0?NaN:n}if(y++,g[e]){_[y]=t;for(let t=0;t<m.length;t++){let n=f.hi[t][e];v[t][y]=n===-1/0?NaN:n}y++}}return f.pts={length:h,x:_,cols:new Map(u.map((e,t)=>[e,v[t]]))},f.pts}computed=new Set;get firstAbs(){return this.pushed-this.size}get endAbs(){return this.pushed}range(e,t,n){let r=Math.max(t,this.firstAbs),i=new Float64Array(Math.max(0,Math.min(n,this.pushed)-r)),a=this.columnArray(e);if(!a)return i.fill(NaN);for(let e=0;e<i.length;e++)i[e]=a[this.physOf(r+e)];return i}mirrorColumn(e){let t=this.columnArray(e);if(!t)return;let n=this.firstAbs,r=this.pushed,i=this.mirrors.get(e);if((!i||i.clears!==this.clears)&&(i={arr:new Float64Array(Math.max(16,r-n)),first:n,end:n,clears:this.clears,view:null},this.mirrors.set(e,i)),i.first===n&&i.end===r&&i.view)return i.view;if(n>i.first){let e=Math.max(0,i.end-n);e>0&&i.arr.copyWithin(0,n-i.first,i.end-i.first),i.first=n,i.end=n+e}if(i.arr.length<r-n){let e=new Float64Array(Math.max(r-n,i.arr.length*2));e.set(i.arr.subarray(0,i.end-i.first)),i.arr=e}for(let e=i.end;e<r;){let a=this.physOf(e),o=Math.min(r-e,(this.full?this.capacity:this.alloc)-a);i.arr.set(t.subarray(a,a+o),e-n),e+=o}return i.end=r,i.view=i.arr.subarray(0,r-n),i.view}writeComputed(e,t,n,r){let i=this.mirrors.get(e);i&&n<i.end&&(i.end=Math.max(i.first,n),i.view=null);let a=this.cols.get(e);(!a||!this.computed.has(e))&&(a||(a=this.newColumn(),this.cols.set(e,a),this.signalOrder.push(e)),this.computed.add(e)),this.unitByName.set(e,t);for(let e=0;e<r.length;e++){let t=n+e;t<this.firstAbs||t>=this.pushed||(a[this.physOf(t)]=r[e]??NaN)}}clearComputed(){for(let e of this.computed){this.cols.delete(e),this.unitByName.delete(e);for(let t of this.trackers.values())t.previous?.cols.delete(e)}this.signalOrder.splice(0,this.signalOrder.length,...this.signalOrder.filter(e=>!this.computed.has(e))),this.computed.clear(),this.decCache=null,this.timeDec=null,this.clears++}get columnNames(){return[this.timeColumn,...this.signalOrder]}unitOf(e){return e===this.timeColumn?this.timeUnit:this.unitByName.get(e)??``}columnValues(e){let t=this.size,n=new Float64Array(t),r=e===this.timeColumn?this.time:this.cols.get(e);if(!r)return n.fill(NaN),n;for(let e=0;e<t;e++)n[e]=r[this.full?(this.head+e)%this.capacity:e];return n}accumulate(e,t,n,r,i){let a=Jt(i.length),o=n[t];if(Number.isNaN(o))return;let s=Math.floor(o/r),c=e.get(s);c||(c={...a.row,b:s},e.set(s,c));for(let e=0;e<i.length;e++){let n=i[e][t];if(Number.isNaN(n))continue;let r=a.lo[e],o=a.hi[e],s=c[r],l=c[o];c[r]=s==null?n:Math.min(s,n),c[o]=l==null?n:Math.max(l,n)}}decimateColumns(e,t,n,r,i){if(e===this.timeColumn&&this.timeMonotonic)return this.decimateTime(t,n,r,i);let a=e===this.timeColumn?this.time:this.cols.get(e),o=a?t.filter(e=>this.cols.has(e)):[],s=(r-n)/i,c=this.size;if(c>1&&e===this.timeColumn&&this.timeMonotonic&&a){let e=a[this.full?this.head:0],t=(a[this.full?(this.head+c-1)%this.capacity:c-1]-e)/(c-1);t>0&&t<s&&(s=.7*Math.max(t,(r-n)/(i*Xt)))}let l=Ut(s);{let t=this.decCache;t&&t.xKey===e&&t.colsKey===o.join(` `)&&t.bucket>0&&s>=t.bucket*.5&&s<=t.bucket*2&&(l=t.bucket)}if(!a||o.length===0||!(r>n)||!(l>0))return{x0:n,bucket:0,rows:[],columns:o};let u=o.map(e=>this.cols.get(e)),d=this.size,f=o.join(`\0`);this.debugLastScan=0;let p=this.decCache,m=p!==null&&e===this.timeColumn&&this.timeMonotonic&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&this.pushed>=p.pushed&&this.pushed-p.pushed<d&&p.map.size<this.capacity*2,h=p!==null&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&p.pushed===this.pushed,g=this.pushed-d,_=e===this.timeColumn&&this.timeMonotonic?g:Math.max(g,this.passStart(e,a)),v=p!==null&&(_>g||!this.full)&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&p.passStart===_&&p.pushed>=_&&p.pushed<=this.pushed,y;if(h&&p)y=p.map;else if(v&&p){y=p.map;for(let e=p.pushed;e<this.pushed;e++)this.debugLastScan++,this.accumulate(y,this.physOf(e),a,l,u);p.pushed=this.pushed}else if(m&&p){y=p.map;let e=this.pushed-p.pushed;for(let t=d-e;t<d;t++){let e=this.full?(this.head+t)%this.capacity:t;this.debugLastScan++,this.accumulate(y,e,a,l,u)}if(this.full&&d>0){let e=a[this.head];if(!Number.isNaN(e)){let t=Math.floor(e/l);for(let e=p.lowB;e<t;e++)y.delete(e);y.delete(t);for(let e=0;e<d;e++){let n=(this.head+e)%this.capacity,r=a[n];if(!Number.isNaN(r)){if(Math.floor(r/l)>t)break;this.debugLastScan++,this.accumulate(y,n,a,l,u)}}p.lowB=t}}p.pushed=this.pushed}else{y=new Map;let t=1/0;for(let e=_;e<this.pushed;e++){let n=this.physOf(e),r=a[n];if(Number.isNaN(r))continue;this.debugLastScan++,this.accumulate(y,n,a,l,u);let i=Math.floor(r/l);i<t&&(t=i)}this.decCache={xKey:e,colsKey:f,cols:o,bucket:l,map:y,pushed:this.pushed,lowB:Number.isFinite(t)?t:0,passStart:_}}let b=Math.floor(n/l),x=Math.floor(r/l),S=[];for(let e of y.values()){let t=e.b;t>=b&&t<=x&&S.push(e)}return S.sort((e,t)=>Number(e.b)-Number(t.b)),{x0:0,bucket:l,rows:S,columns:o}}passStart(e,t){let n=this.pushed-this.size,r=this.passTrack;(!r||r.key!==e||r.clears!==this.clears)&&(r=this.passTrack={key:e,clears:this.clears,done:n,prevX:NaN,cuts:[]});let{x0:i,x1:a}=this.columnBounds(e),o=(a-i)/2;for(let e=Math.max(r.done,n);e<this.pushed;e++){let n=t[this.physOf(e)];Number.isNaN(n)||(n<r.prevX-o&&(r.cuts.push({abs:e,drop:r.prevX-n}),r.cuts.length>64&&r.cuts.shift()),r.prevX=n)}for(r.done=this.pushed;r.cuts.length>0&&r.cuts[0].abs<n;)r.cuts.shift();for(let e=r.cuts.length-1;e>=0;e--)if(r.cuts[e].drop>o)return r.cuts[e].abs;return n}timeIndex(e){let t=0,n=this.size;for(;t<n;){let r=t+n>>>1;this.time[this.full?(this.head+r)%this.capacity:r]<e?t=r+1:n=r}return t}foldBuckets(e,t,n,r,i){this.eachBucketRun(t,n,r,(t,n,r)=>e.fold(e.row(t),i,n,r))}eachBucketRun(e,t,n,r){if(t<=e)return;this.debugLastScan+=t-e;let i=this.time,a=e=>Math.floor(i[e]/n),o=(e,t)=>{let n=e;for(;n<t;){let e=a(n),i=n,o=1;for(;n+o<t&&a(n+o)===e;)i=n+o,o*=2;let s=i+1,c=Math.min(n+o,t);for(;s<c;){let t=s+c>>>1;a(t)===e?s=t+1:c=t}r(e,n,s),n=s}};if(!this.full)return o(e,t);let s=(this.head+e)%this.capacity,c=s+(t-e);if(c<=this.capacity)return o(s,c);o(s,this.capacity),o(0,c-this.capacity)}decimateTime(e,t,n,r){let i=this.timeBuckets(e,t,n,r);if(!i.store)return{x0:t,bucket:0,rows:[],columns:i.cols};let a=Jt(i.cols.length),o=()=>i.store.toRows(i.i0,i.i1,a.row,a.lo,a.hi),s=this.historyDec;return{x0:0,bucket:i.bucket,rows:s?.store===i.store?s.rows??=o():o(),columns:i.cols}}decimatePoints(e,t,n,r,i){if(e===this.timeColumn&&this.timeMonotonic){let e=this.timeBuckets(t,n,r,i);return e.store?e.store.toPoints(e.i0,e.i1,e.cols):{length:0,x:new Float64Array,cols:new Map(e.cols.map(e=>[e,new Float64Array]))}}let a=this.decimateColumns(e,t,n,r,i);return U(a.rows,a.columns,a.x0,a.bucket)}timeBuckets(e,t,n,r){let i=e.filter(e=>this.cols.has(e)),a=this.size,o={cols:i,bucket:0,store:null,i0:0,i1:0};if(i.length===0||!(n>t)||a===0)return o;let s=e=>this.full?(this.head+e)%this.capacity:e,c=this.time[s(0)],l=this.time[s(a-1)],u=(n-t)/r;if(a>1){let e=(l-c)/(a-1);e>0&&e<u&&(u=.7*Math.max(e,(n-t)/(r*Xt)))}let d=Ut(u),f=i.join(` `),p=this.timeDec;if(p&&p.colsKey===f&&u>=p.bucket*.5&&u<=p.bucket*2&&(d=p.bucket),!(d>0))return o;let m=i.map(e=>this.cols.get(e)),h=Math.floor(t/d),g=Math.floor(n/d);this.debugLastScan=0;let _=e=>({cols:i,bucket:d,store:e,i0:e.lowerBound(h),i1:e.lowerBound(g+1)});if(n<l-(n-t)){let e=this.timeIndex(h*d),t=this.timeIndex((g+1)*d),n=this.pushed-a,r=`${f}|${d}|${h}|${g}|${n+e}|${n+t}|${this.clears}`;if(this.historyDec?.key!==r){let n=new Ht(i.length,d);this.foldBuckets(n,e,t,d,m),this.historyDec={key:r,store:n}}return _(this.historyDec.store)}let v=p?this.pushed-p.pushed:0;if(p&&p.colsKey===f&&p.bucket===d&&v>=0&&v<a){let e=p.store;if(h<p.fromB){let t=new Ht(i.length,d);this.foldBuckets(t,this.timeIndex(h*d),this.timeIndex(p.fromB*d),d,m);for(let n=t.end-1;n>=t.start;n--){let r=e.row(t.b[n]);for(let i=0;i<t.nc;i++)e.lo[i][r]=t.lo[i][n],e.hi[i][r]=t.hi[i][n]}p.fromB=h}v>0&&this.foldBuckets(e,a-v,a,d,m);let t=Math.floor(c/d);return this.full&&t>=p.fromB&&(e.dropBelow(t),e.reset(e.row(t)),this.foldBuckets(e,0,this.timeIndex((t+1)*d),d,m),p.fromB=t),h>p.fromB&&(e.dropBelow(h),p.fromB=h),p.pushed=this.pushed,_(e)}let y=new Ht(i.length,d),b=this.timeIndex(h*d);return this.foldBuckets(y,b,a,d,m),this.timeDec={colsKey:f,bucket:d,store:y,pushed:this.pushed,fromB:b===0?Math.floor(c/d):h},_(y)}lastValue(e){let t=e===this.timeColumn?this.time:this.cols.get(e);return!t||this.size===0?NaN:t[this.full?(this.head-1+this.capacity)%this.capacity:this.count-1]}xTravel(e,t){let n=this.columnArray(e),r=this.size;if(!n||r<2)return NaN;let i=e=>this.full?(this.head+e)%this.capacity:e,a=this.time[i(r-1)],o=0,s=NaN,c=a;for(let e=r-1;e>=0;e--){let r=i(e),l=this.time[r];if(l<a-t)break;let u=n[r];Number.isNaN(u)||(s>u&&(o+=s-u),s=u,c=l)}let l=a-c;return l>0&&l<t?o*t/l:o}latestTime(){return this.lastValue(this.timeColumn)}xAtTime(e,t){let n=this.columnArray(e),r=this.size;if(!n||r===0||!this.timeMonotonic)return NaN;let i=e=>this.full?(this.head+e)%this.capacity:e,a=0,o=r-1;if(t<=this.time[i(0)])return n[i(0)];if(t>=this.time[i(o)])return n[i(o)];for(;o-a>1;){let e=a+o>>1;this.time[i(e)]<=t?a=e:o=e}let s=i(a),c=i(o),l=n[s],u=n[c];if(!(u>=l))return l;let d=this.time[s],f=this.time[c];return f>d?l+(u-l)*(t-d)/(f-d):l}boundsCache=new Map;columnBounds(e){let t=e===this.timeColumn?this.time:this.cols.get(e),n=this.size;if(!t||n===0)return{x0:0,x1:0};if(t===this.time&&this.timeMonotonic)return{x0:this.time[this.full?this.head:0],x1:this.time[this.full?(this.head+n-1)%this.capacity:n-1]};let r=this.pushed-n,i=this.boundsCache.get(e);if(i&&i.version===this.version)return i.b;(!i||i.clears!==this.clears||i.loAbs<r||i.hiAbs<r)&&(i={version:-1,clears:this.clears,done:r,lo:1/0,hi:-1/0,loAbs:1/0,hiAbs:1/0,b:{x0:0,x1:0}},this.boundsCache.set(e,i));for(let e=Math.max(i.done,r);e<this.pushed;e++){let n=t[this.physOf(e)];Number.isNaN(n)||(n<=i.lo&&(i.lo=n,i.loAbs=e),n>=i.hi&&(i.hi=n,i.hiAbs=e))}return i.done=this.pushed,i.version=this.version,i.b=Number.isFinite(i.lo)?{x0:i.lo,x1:i.hi}:{x0:0,x1:0},i.b}toDataset(e){let t=[this.timeColumn,...this.signalOrder],n=[this.timeUnit,...this.signalOrder.map(e=>this.unitByName.get(e)??``)],r=this.size,i=e&&e>0&&r>e?Math.ceil(r/e):1,a=e=>{let t=this.full?(this.head+e)%this.capacity:e,n={[this.timeColumn]:this.time[t]};for(let e of this.signalOrder){let r=this.cols.get(e)[t];n[e]=Number.isNaN(r)?null:r}return n},o=this.pushed-r,s=i>1?(i-o%i)%i:0,c=[];for(let e=s;e<r;e+=i)c.push(a(e));return r>0&&(c.length===0||(r-1-s)%i!=0)&&c.push(a(r-1)),{meta:``,columnNames:t,units:n,rows:c}}};new TextEncoder;var Qt=4096;function $t(e,t,n){let r=t,i=r+n,a=[],o=``;for(;r<i;){let t=e[r++];if(!(t&128))a.push(t);else if((t&224)==192){let n=e[r++]&63;a.push((t&31)<<6|n)}else if((t&240)==224){let n=e[r++]&63,i=e[r++]&63;a.push((t&31)<<12|n<<6|i)}else if((t&248)==240){let n=e[r++]&63,i=e[r++]&63,o=e[r++]&63,s=(t&7)<<18|n<<12|i<<6|o;s>65535&&(s-=65536,a.push(s>>>10&1023|55296),s=56320|s&1023),a.push(s)}else a.push(t);a.length>=Qt&&(o+=String.fromCharCode(...a),a.length=0)}return a.length>0&&(o+=String.fromCharCode(...a)),o}var en=new TextDecoder,tn=200;function nn(e,t,n){let r=e.subarray(t,t+n);return en.decode(r)}function rn(e,t,n){return n>tn?nn(e,t,n):$t(e,t,n)}var G=class{type;data;constructor(e,t){this.type=e,this.data=t}},K=class e extends Error{constructor(t){super(t);let n=Object.create(e.prototype);Object.setPrototypeOf(this,n),Object.defineProperty(this,`name`,{configurable:!0,enumerable:!1,value:e.name})}};function an(e,t,n){let r=Math.floor(n/4294967296),i=n;e.setUint32(t,r),e.setUint32(t+4,i)}function on(e,t){let n=e.getInt32(t),r=e.getUint32(t+4);return n*4294967296+r}function sn(e,t){let n=e.getUint32(t),r=e.getUint32(t+4);return n*4294967296+r}var cn=4294967295,ln=17179869183;function un({sec:e,nsec:t}){if(e>=0&&t>=0&&e<=ln)if(t===0&&e<=cn){let t=new Uint8Array(4);return new DataView(t.buffer).setUint32(0,e),t}else{let n=e/4294967296,r=e&4294967295,i=new Uint8Array(8),a=new DataView(i.buffer);return a.setUint32(0,t<<2|n&3),a.setUint32(4,r),i}else{let n=new Uint8Array(12),r=new DataView(n.buffer);return r.setUint32(0,t),an(r,4,e),n}}function dn(e){let t=e.getTime(),n=Math.floor(t/1e3),r=(t-n*1e3)*1e6,i=Math.floor(r/1e9);return{sec:n+i,nsec:r-i*1e9}}function fn(e){return e instanceof Date?un(dn(e)):null}function pn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);switch(e.byteLength){case 4:return{sec:t.getUint32(0),nsec:0};case 8:{let e=t.getUint32(0),n=t.getUint32(4);return{sec:(e&3)*4294967296+n,nsec:e>>>2}}case 12:return{sec:on(t,4),nsec:t.getUint32(0)};default:throw new K(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${e.length}`)}}function mn(e){let t=pn(e);return new Date(t.sec*1e3+t.nsec/1e6)}var hn={type:-1,encode:fn,decode:mn},gn=class e{static defaultCodec=new e;__brand;builtInEncoders=[];builtInDecoders=[];encoders=[];decoders=[];constructor(){this.register(hn)}register({type:e,encode:t,decode:n}){if(e>=0)this.encoders[e]=t,this.decoders[e]=n;else{let r=-1-e;this.builtInEncoders[r]=t,this.builtInDecoders[r]=n}}tryToEncode(e,t){for(let n=0;n<this.builtInEncoders.length;n++){let r=this.builtInEncoders[n];if(r!=null){let i=r(e,t);if(i!=null)return new G(-1-n,i)}}for(let n=0;n<this.encoders.length;n++){let r=this.encoders[n];if(r!=null){let i=r(e,t);if(i!=null)return new G(n,i)}}return e instanceof G?e:null}decode(e,t,n){let r=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return r?r(e,t,n):new G(t,e)}};function _n(e){return e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer}function vn(e){return e instanceof Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):_n(e)?new Uint8Array(e):Uint8Array.from(e)}function yn(e){return`${e<0?`-`:``}0x${Math.abs(e).toString(16).padStart(2,`0`)}`}var bn=16,xn=16,Sn=class{hit=0;miss=0;caches;maxKeyLength;maxLengthPerKey;constructor(e=bn,t=xn){this.maxKeyLength=e,this.maxLengthPerKey=t,this.caches=[];for(let e=0;e<this.maxKeyLength;e++)this.caches.push([])}canBeCached(e){return e>0&&e<=this.maxKeyLength}find(e,t,n){let r=this.caches[n-1];FIND_CHUNK:for(let i of r){let r=i.bytes;for(let i=0;i<n;i++)if(r[i]!==e[t+i])continue FIND_CHUNK;return i.str}return null}store(e,t){let n=this.caches[e.length-1],r={bytes:e,str:t};n.length>=this.maxLengthPerKey?n[Math.random()*n.length|0]=r:n.push(r)}decode(e,t,n){let r=this.find(e,t,n);if(r!=null)return this.hit++,r;this.miss++;let i=$t(e,t,n),a=Uint8Array.prototype.slice.call(e,t,t+n);return this.store(a,i),i}},Cn=`array`,q=`map_key`,wn=`map_value`,Tn=e=>{if(typeof e==`string`||typeof e==`number`)return e;throw new K(`The type of key must be string or number but `+typeof e)},En=class{stack=[];stackHeadPosition=-1;get length(){return this.stackHeadPosition+1}top(){return this.stack[this.stackHeadPosition]}pushArrayState(e){let t=this.getUninitializedStateFromPool();t.type=Cn,t.position=0,t.size=e,t.array=Array(e)}pushMapState(e){let t=this.getUninitializedStateFromPool();t.type=q,t.readCount=0,t.size=e,t.map={}}getUninitializedStateFromPool(){return this.stackHeadPosition++,this.stackHeadPosition===this.stack.length&&this.stack.push({type:void 0,size:0,array:void 0,position:0,readCount:0,map:void 0,key:null}),this.stack[this.stackHeadPosition]}release(e){if(this.stack[this.stackHeadPosition]!==e)throw Error(`Invalid stack state. Released state is not on top of the stack.`);if(e.type===Cn){let t=e;t.size=0,t.array=void 0,t.position=0,t.type=void 0}if(e.type===q||e.type===wn){let t=e;t.size=0,t.map=void 0,t.readCount=0,t.type=void 0}this.stackHeadPosition--}reset(){this.stack.length=0,this.stackHeadPosition=-1}},J=-1,Dn=new DataView(new ArrayBuffer(0)),On=new Uint8Array(Dn.buffer);try{Dn.getInt8(0)}catch(e){if(!(e instanceof RangeError))throw Error(`This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access`)}var kn=RangeError(`Insufficient data`),An=new Sn,jn=class e{extensionCodec;context;useBigInt64;rawStrings;maxStrLength;maxBinLength;maxArrayLength;maxMapLength;maxExtLength;keyDecoder;mapKeyConverter;totalPos=0;pos=0;view=Dn;bytes=On;headByte=J;stack=new En;entered=!1;constructor(e){this.extensionCodec=e?.extensionCodec??gn.defaultCodec,this.context=e?.context,this.useBigInt64=e?.useBigInt64??!1,this.rawStrings=e?.rawStrings??!1,this.maxStrLength=e?.maxStrLength??4294967295,this.maxBinLength=e?.maxBinLength??4294967295,this.maxArrayLength=e?.maxArrayLength??4294967295,this.maxMapLength=e?.maxMapLength??4294967295,this.maxExtLength=e?.maxExtLength??4294967295,this.keyDecoder=e?.keyDecoder===void 0?An:e.keyDecoder,this.mapKeyConverter=e?.mapKeyConverter??Tn}clone(){return new e({extensionCodec:this.extensionCodec,context:this.context,useBigInt64:this.useBigInt64,rawStrings:this.rawStrings,maxStrLength:this.maxStrLength,maxBinLength:this.maxBinLength,maxArrayLength:this.maxArrayLength,maxMapLength:this.maxMapLength,maxExtLength:this.maxExtLength,keyDecoder:this.keyDecoder})}reinitializeState(){this.totalPos=0,this.headByte=J,this.stack.reset()}setBuffer(e){let t=vn(e);this.bytes=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength),this.pos=0}appendBuffer(e){if(this.headByte===J&&!this.hasRemaining(1))this.setBuffer(e);else{let t=this.bytes.subarray(this.pos),n=vn(e),r=new Uint8Array(t.length+n.length);r.set(t),r.set(n,t.length),this.setBuffer(r)}}hasRemaining(e){return this.view.byteLength-this.pos>=e}createExtraByteError(e){let{view:t,pos:n}=this;return RangeError(`Extra ${t.byteLength-n} of ${t.byteLength} byte(s) found at buffer[${e}]`)}decode(e){if(this.entered)return this.clone().decode(e);try{this.entered=!0,this.reinitializeState(),this.setBuffer(e);let t=this.doDecodeSync();if(this.hasRemaining(1))throw this.createExtraByteError(this.pos);return t}finally{this.entered=!1}}*decodeMulti(e){if(this.entered){yield*this.clone().decodeMulti(e);return}try{for(this.entered=!0,this.reinitializeState(),this.setBuffer(e);this.hasRemaining(1);)yield this.doDecodeSync()}finally{this.entered=!1}}async decodeAsync(e){if(this.entered)return this.clone().decodeAsync(e);try{this.entered=!0;let t=!1,n;for await(let r of e){if(t)throw this.entered=!1,this.createExtraByteError(this.totalPos);this.appendBuffer(r);try{n=this.doDecodeSync(),t=!0}catch(e){if(!(e instanceof RangeError))throw e}this.totalPos+=this.pos}if(t){if(this.hasRemaining(1))throw this.createExtraByteError(this.totalPos);return n}let{headByte:r,pos:i,totalPos:a}=this;throw RangeError(`Insufficient data in parsing ${yn(r)} at ${a} (${i} in the current buffer)`)}finally{this.entered=!1}}decodeArrayStream(e){return this.decodeMultiAsync(e,!0)}decodeStream(e){return this.decodeMultiAsync(e,!1)}async*decodeMultiAsync(e,t){if(this.entered){yield*this.clone().decodeMultiAsync(e,t);return}try{this.entered=!0;let n=t,r=-1;for await(let i of e){if(t&&r===0)throw this.createExtraByteError(this.totalPos);this.appendBuffer(i),n&&(r=this.readArraySize(),n=!1,this.complete());try{for(;yield this.doDecodeSync(),--r!==0;);}catch(e){if(!(e instanceof RangeError))throw e}this.totalPos+=this.pos}}finally{this.entered=!1}}doDecodeSync(){DECODE:for(;;){let e=this.readHeadByte(),t;if(e>=224)t=e-256;else if(e<192)if(e<128)t=e;else if(e<144){let n=e-128;if(n!==0){this.pushMapState(n),this.complete();continue DECODE}else t={}}else if(e<160){let n=e-144;if(n!==0){this.pushArrayState(n),this.complete();continue DECODE}else t=[]}else{let n=e-160;t=this.decodeString(n,0)}else if(e===192)t=null;else if(e===194)t=!1;else if(e===195)t=!0;else if(e===202)t=this.readF32();else if(e===203)t=this.readF64();else if(e===204)t=this.readU8();else if(e===205)t=this.readU16();else if(e===206)t=this.readU32();else if(e===207)t=this.useBigInt64?this.readU64AsBigInt():this.readU64();else if(e===208)t=this.readI8();else if(e===209)t=this.readI16();else if(e===210)t=this.readI32();else if(e===211)t=this.useBigInt64?this.readI64AsBigInt():this.readI64();else if(e===217){let e=this.lookU8();t=this.decodeString(e,1)}else if(e===218){let e=this.lookU16();t=this.decodeString(e,2)}else if(e===219){let e=this.lookU32();t=this.decodeString(e,4)}else if(e===220){let e=this.readU16();if(e!==0){this.pushArrayState(e),this.complete();continue DECODE}else t=[]}else if(e===221){let e=this.readU32();if(e!==0){this.pushArrayState(e),this.complete();continue DECODE}else t=[]}else if(e===222){let e=this.readU16();if(e!==0){this.pushMapState(e),this.complete();continue DECODE}else t={}}else if(e===223){let e=this.readU32();if(e!==0){this.pushMapState(e),this.complete();continue DECODE}else t={}}else if(e===196){let e=this.lookU8();t=this.decodeBinary(e,1)}else if(e===197){let e=this.lookU16();t=this.decodeBinary(e,2)}else if(e===198){let e=this.lookU32();t=this.decodeBinary(e,4)}else if(e===212)t=this.decodeExtension(1,0);else if(e===213)t=this.decodeExtension(2,0);else if(e===214)t=this.decodeExtension(4,0);else if(e===215)t=this.decodeExtension(8,0);else if(e===216)t=this.decodeExtension(16,0);else if(e===199){let e=this.lookU8();t=this.decodeExtension(e,1)}else if(e===200){let e=this.lookU16();t=this.decodeExtension(e,2)}else if(e===201){let e=this.lookU32();t=this.decodeExtension(e,4)}else throw new K(`Unrecognized type byte: ${yn(e)}`);this.complete();let n=this.stack;for(;n.length>0;){let e=n.top();if(e.type===Cn)if(e.array[e.position]=t,e.position++,e.position===e.size)t=e.array,n.release(e);else continue DECODE;else if(e.type===q){if(t===`__proto__`)throw new K(`The key __proto__ is not allowed`);e.key=this.mapKeyConverter(t),e.type=wn;continue DECODE}else if(e.map[e.key]=t,e.readCount++,e.readCount===e.size)t=e.map,n.release(e);else{e.key=null,e.type=q;continue DECODE}}return t}}readHeadByte(){return this.headByte===J&&(this.headByte=this.readU8()),this.headByte}complete(){this.headByte=J}readArraySize(){let e=this.readHeadByte();switch(e){case 220:return this.readU16();case 221:return this.readU32();default:if(e<160)return e-144;throw new K(`Unrecognized array type byte: ${yn(e)}`)}}pushMapState(e){if(e>this.maxMapLength)throw new K(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);this.stack.pushMapState(e)}pushArrayState(e){if(e>this.maxArrayLength)throw new K(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);this.stack.pushArrayState(e)}decodeString(e,t){return!this.rawStrings||this.stateIsMapKey()?this.decodeUtf8String(e,t):this.decodeBinary(e,t)}decodeUtf8String(e,t){if(e>this.maxStrLength)throw new K(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);if(this.bytes.byteLength<this.pos+t+e)throw kn;let n=this.pos+t,r;return r=this.stateIsMapKey()&&this.keyDecoder?.canBeCached(e)?this.keyDecoder.decode(this.bytes,n,e):rn(this.bytes,n,e),this.pos+=t+e,r}stateIsMapKey(){return this.stack.length>0?this.stack.top().type===q:!1}decodeBinary(e,t){if(e>this.maxBinLength)throw new K(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);if(!this.hasRemaining(e+t))throw kn;let n=this.pos+t,r=this.bytes.subarray(n,n+e);return this.pos+=t+e,r}decodeExtension(e,t){if(e>this.maxExtLength)throw new K(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);let n=this.view.getInt8(this.pos+t),r=this.decodeBinary(e,t+1);return this.extensionCodec.decode(r,n,this.context)}lookU8(){return this.view.getUint8(this.pos)}lookU16(){return this.view.getUint16(this.pos)}lookU32(){return this.view.getUint32(this.pos)}readU8(){let e=this.view.getUint8(this.pos);return this.pos++,e}readI8(){let e=this.view.getInt8(this.pos);return this.pos++,e}readU16(){let e=this.view.getUint16(this.pos);return this.pos+=2,e}readI16(){let e=this.view.getInt16(this.pos);return this.pos+=2,e}readU32(){let e=this.view.getUint32(this.pos);return this.pos+=4,e}readI32(){let e=this.view.getInt32(this.pos);return this.pos+=4,e}readU64(){let e=sn(this.view,this.pos);return this.pos+=8,e}readI64(){let e=on(this.view,this.pos);return this.pos+=8,e}readU64AsBigInt(){let e=this.view.getBigUint64(this.pos);return this.pos+=8,e}readI64AsBigInt(){let e=this.view.getBigInt64(this.pos);return this.pos+=8,e}readF32(){let e=this.view.getFloat32(this.pos);return this.pos+=4,e}readF64(){let e=this.view.getFloat64(this.pos);return this.pos+=8,e}};function Mn(e,t){return new jn(t).decode(e)}var Y=12,Nn=16;function Pn(e){let t=new Uint8Array(e),n=t.length>0&&t[0]&128?2:1;return t.length<n+Y+Nn?null:{kind:t[0]&-129,keyId:n===2?t[1]:null,iv:t.subarray(n,n+Y),ciphertext:t.subarray(n+Y),aad:t.subarray(0,n)}}async function Fn(e,t){let n=Pn(t);if(!n)return null;let r=typeof e==`function`?await e(n.keyId):e;if(!r)return null;let i=await We(r,n.iv,n.ciphertext,n.aad);return{kind:n.kind,plaintext:i}}function In(e,t,n,r){return Ln(e,t,n,r)}async function Ln(e,t,n,r,i){let a=r===void 0?new Uint8Array([t]):new Uint8Array([t|128,r&255]),o=i??globalThis.crypto.getRandomValues(new Uint8Array(Y)),s=new Uint8Array(await globalThis.crypto.subtle.encrypt({name:`AES-GCM`,iv:o,additionalData:a},e,n)),c=new Uint8Array(a.length+Y+s.length);return c.set(a),c.set(o,a.length),c.set(s,a.length+Y),c.buffer}var Rn=1,X=e=>e+7&-8;function zn(e){let t=e=>RangeError(`columns message: ${e}`);e.byteOffset%8!=0&&(e=e.slice());let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.byteLength<8)throw t(`short`);if(n.getUint8(0)!==Rn)throw t(`version ${n.getUint8(0)}`);let r=n.getUint16(2,!0),i=n.getUint32(4,!0),a=new TextDecoder,o=[],s=8;for(let i=0;i<r;i++){if(s+3>e.byteLength)throw t(`short channel table`);let r=n.getUint8(s),i=n.getUint8(s+1),c=n.getUint8(s+2);if(s+=3,r!==4&&r!==8)throw t(`value size ${r}`);if(s+i+c>e.byteLength)throw t(`short channel table`);o.push({name:a.decode(e.subarray(s,s+i)),unit:a.decode(e.subarray(s+i,s+i+c)),size:r}),s+=i+c}if(s=X(s),s+X(i*8)+o.reduce((e,t)=>e+X(i*t.size),0)>e.byteLength)throw t(`short data`);let c=e.byteOffset,l=new Float64Array(e.buffer,c+s,i);s+=X(i*8);let u={},d={};for(let t of o)u[t.name]=t.size===4?new Float32Array(e.buffer,c+s,i):new Float64Array(e.buffer,c+s,i),t.unit&&(d[t.name]=t.unit),s+=X(i*t.size);return{t:l,columns:u,units:d}}var Bn=2,Vn=1,Hn=2,Z=20,Un=64*1024*1024,Q=(e,t)=>RangeError(`${e}: ${t}`);function Wn(e){if(e.byteLength<8)throw Q(`block schema`,`short`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(t.getUint8(0)!==Bn)throw Q(`block schema`,`version ${t.getUint8(0)}`);let n=t.getUint16(2,!0),r=t.getUint32(4,!0),i=new TextDecoder(`utf-8`,{fatal:!0}),a=[],o=8;try{for(let r=0;r<n;r++){if(o+3>e.byteLength)throw Q(`block schema`,`short channel table`);let n=t.getUint8(o),r=t.getUint8(o+1),s=t.getUint8(o+2);if(o+=3,n!==4&&n!==8)throw Q(`block schema`,`value size ${n}`);if(o+r+s>e.byteLength)throw Q(`block schema`,`short channel table`);a.push({name:i.decode(e.subarray(o,o+r)),unit:i.decode(e.subarray(o+r,o+r+s)),size:n}),o+=r+s}}catch(e){throw e instanceof RangeError?e:Q(`block schema`,`invalid text`)}return{id:r,channels:a}}async function Gn(e,t){let n=new Blob([e]).stream().pipeThrough(new DecompressionStream(`deflate`)).getReader(),r=new Uint8Array(t),i=0;try{for(;;){let{done:e,value:a}=await n.read();if(e)break;if(i+a.length>t)throw Q(`block`,`body larger than its header says`);r.set(a,i),i+=a.length}}catch(e){throw await n.cancel().catch(()=>{}),e instanceof RangeError?e:Q(`block`,`body is not valid deflate`)}if(i!==t)throw Q(`block`,`body size mismatch`);return r}async function Kn(e,t){if(e.byteLength<Z)throw Q(`block`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==Bn)throw Q(`block`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint16(2,!0),a=n.getUint32(4,!0),o=t.get(n.getUint32(8,!0));if(!o)return null;if(o.channels.length!==i)throw Q(`block`,`channel count differs from its schema`);let s=n.getFloat64(12,!0),c=Math.max(0,a-1),l=4*c+o.channels.reduce((e,t)=>e+t.size*a,0);if(l>Un)throw Q(`block`,`too large`);let u;if(r&Vn)u=await Gn(e.subarray(Z),l);else if(u=e.subarray(Z),u.byteLength<l)throw Q(`block`,`body size mismatch`);let d=new DataView(u.buffer,u.byteOffset,u.byteLength),f=new Float64Array(a),p=0;a&&(f[0]=s);for(let e=1;e<a;e++)p+=d.getInt32(4*(e-1),!0),f[e]=s+p/1e6;let m=4*c,h={},g={};for(let e of o.channels){let t=new Uint8Array(a*e.size);for(let n=0;n<e.size;n++)for(let r=0;r<a;r++)t[r*e.size+n]=u[m+n*a+r];h[e.name]=e.size===4?new Float32Array(t.buffer):new Float64Array(t.buffer),e.unit&&(g[e.name]=e.unit),m+=a*e.size}return{block:{t:f,columns:h,units:g},historical:(r&Hn)!==0}}var qn=(e,t,n)=>`recording_key:${e}|${t}:${n}`,Jn=(e,t,n)=>n===e.stream_epoch?st(e.stream_id,t,n):qn(e.id,t,n),$=12,Yn=16;function Xn(e){let t=new Map;return async n=>{if(n.length===0)return null;let r=n[0]&128?2:1;if(n.length<r+$+Yn)return null;let i=r===2?n[1]:null,a=t.get(i),o=[...a?[a]:[],...e.filter(e=>e.keyId===i&&e.key!==a).map(e=>e.key)];for(let e of o)try{let a=await We(e,n.subarray(r,r+$),n.subarray(r+$),n.subarray(0,r));return t.set(i,e),{kind:n[0]&-129,plaintext:a}}catch{}return null}}var Zn=56,Qn=[75,82,67,49];function $n(e){let t=Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`}function er(e){if(e.length<56||Qn.some((t,n)=>e[n]!==t))throw RangeError(`not a KRC1 chunk`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint16(4,!0);if(n!==1)throw RangeError(`KRC version ${n}`);let r=t.getUint16(6,!0);if(r<56)throw RangeError(`KRC header too short`);return{recordingId:$n(e.subarray(8,24)),seq:t.getUint32(24,!0),count:t.getUint32(28,!0),bytes:t.getUint32(32,!0),headerBytes:r,t0:t.getFloat64(40,!0),t1:t.getFloat64(48,!0)}}function tr(e){let t=er(e),n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Math.min(e.length,t.bytes),i=[],a=t.headerBytes;for(;i.length<t.count&&a+12<=r;){let t=n.getUint32(a+8,!0);if(a+12+t>r)break;i.push({at:n.getFloat64(a,!0),bytes:e.subarray(a+12,a+12+t)}),a+=12+t}return{header:t,messages:i,truncated:i.length<t.count}}export{st as $,Ye as A,re as At,At as B,h as Bt,Bt as C,me as Ct,V as D,ge as Dt,ct as E,ue as Et,ut as F,l as Ft,xt as G,n as Gt,lt as H,y as Ht,Ke as I,u as It,Ct as J,Mt as K,a as Kt,pt as L,m as Lt,jt as M,k as Mt,Be as N,ne as Nt,Ve as O,le as Ot,Je as P,w as Pt,Ze as Q,Tt as R,_ as Rt,U as S,he as St,vt as T,A as Tt,St as U,D as Ut,ot as V,b as Vt,kt as W,r as Wt,mt as X,Et as Y,ht as Z,Lt as _,xe as _t,qn as a,bt as at,Rt as b,ve as bt,Wn as c,Fe as ct,In as d,Oe as dt,tt as et,Mn as f,je as ft,Pt as g,Ce as gt,Gt as h,we as ht,Xn as i,yt as it,$e as j,ce as jt,_t as k,se as kt,zn as l,Ne as lt,Zt as m,De as mt,tr as n,qe as nt,Jn as o,nt as ot,Kt as p,Me as pt,dt as q,er as r,gt as rt,Kn as s,Ie as st,Zn as t,it as tt,Fn as u,Te as ut,It as v,be as vt,zt as w,de as wt,Nt as x,_e as xt,Vt as y,ye as yt,Ot as z,v as zt};
9
+ `,Ce={id:`hdf5`,label:`HDF5`,extensions:[`.h5`,`.hdf5`,`.hdf`,`.he5`,`.nc`],sniff:e=>[0,512,1024,2048].some(t=>A(e,Se,t))},we={id:`comtrade`,label:`COMTRADE (IEEE C37.111)`,extensions:[`.cfg`,`.cff`],companions:[{ext:`.dat`}],sniff:e=>{let t=he(e.subarray(0,512));if(/^---\s*file type:\s*CFG/i.test(t))return!0;let n=t.split(/\r?\n/);return n.length>2&&/^\s*\d+\s*,\s*\d+A\s*,\s*\d+D\s*$/i.test(n[1])}},Te=`This file couldn't be read (it may be damaged).`,Ee=1032,De=e=>e*Ee+1024,Oe=128e6,j=256,ke=64,M=1e3,N=100,Ae=200,P=(e,t)=>e.length>t?`${e.slice(0,t-1)}…`:e;function je(e){let t=e.notes;for(let n of e.groups){n.name=P(String(n.name),j);for(let e of n.channels)e.name=P(String(e.name),j),e.unit=P(String(e.unit??``),ke);Number.isInteger(n.samples)&&n.samples>=-1||(n.samples=-1),n.rateHz===null||Number.isFinite(n.rateHz)&&n.rateHz>0||(n.rateHz=null),n.time.kind===`rate`&&!(Number.isFinite(n.time.rate)&&n.time.rate>0&&Number.isFinite(n.time.start))&&(t.push(`${n.name}: its sample rate in the file is not usable; samples are numbered instead.`),n.time={kind:`index`},n.rateHz=null)}t.length>N&&t.splice(N,t.length-N,`${t.length-N} more notes.`),e.notes=t.map(e=>P(String(e),M));let n={};for(let[t,r]of Object.entries(e.meta).slice(0,Ae))n[P(t,j)]=P(String(r),M);return e.meta=n,e.events=e.events.filter(e=>Number.isFinite(e.time)).map(e=>({time:e.time,text:P(String(e.text),M)})),e}function Me(e){return e instanceof Error&&(e.constructor===Error||e.name===`KymoscopeError`||e.name===`AbortError`)?e:Error(Te)}function Ne(e,t=[`Time`]){let n=new Set(t.map(e=>e.toLowerCase())),r=new Map;return e.map(e=>{let t=e.trim()||`Channel`,i=t.toLowerCase(),a=t,o=r.get(i)??2;if(n.has(i)){for(;n.has(`${i} (${o})`);)o++;a=`${t} (${o++})`}return r.set(i,o),n.add(a.toLowerCase()),a})}var F=1<<20,Pe=64,Fe=16<<20,Ie=class{size;bytesRead=0;#e;#t=new Map;constructor(e){this.#e=e,this.size=e.size}async read(e,t){let n=Math.min(this.size,e+t);if(n<=e)return new Uint8Array;if(n-e>F)return this.slice(e,n-e);let r=Math.floor(e/F),i=Math.floor((n-1)/F),a=await this.#n(r);if(r===i)return a.subarray(e-r*F,n-r*F);let o=new Uint8Array(n-e),s=a.subarray(e-r*F);return o.set(s),o.set((await this.#n(i)).subarray(0,n-i*F),s.length),o}async slice(e,t){let n=Math.min(this.size,e+t);if(n<=e)return new Uint8Array;let r=new Uint8Array(await this.#e.slice(e,n).arrayBuffer());return this.bytesRead+=r.length,r}async*windows(e,t,n=Fe){let r=Math.min(this.size,e+t);for(let t=e;t<r;t+=n)yield{offset:t,bytes:await this.slice(t,Math.min(n,r-t))}}async#n(e){let t=this.#t.get(e);if(t)return this.#t.delete(e),this.#t.set(e,t),t;let n=await this.slice(e*F,F);return this.#t.size>=Pe&&this.#t.delete(this.#t.keys().next().value),this.#t.set(e,n),n}},Le=`kymoscope-live-key-check-v1`,I=6e5;function L(){let e=globalThis.crypto;if(!e?.subtle)throw Error(`WebCrypto SubtleCrypto is not available`);return e.subtle}function R(e){let t=``;for(let n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}function z(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function B(e){return R(e).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``)}function Re(e){return z(e.replace(/-/g,`+`).replace(/_/g,`/`).padEnd(Math.ceil(e.length/4)*4,`=`))}function ze(e){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return t}function Be(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),R(e)}async function V(e,t){let n=new TextEncoder,r=await L().importKey(`raw`,n.encode(e),`PBKDF2`,!1,[`deriveKey`]);return L().deriveKey({name:`PBKDF2`,salt:z(t),iterations:I,hash:`SHA-256`},r,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}async function Ve(e,t){let n=await L().importKey(`raw`,new TextEncoder().encode(e),`PBKDF2`,!1,[`deriveBits`]),r=await L().deriveBits({name:`PBKDF2`,salt:z(t),iterations:I,hash:`SHA-256`},n,256);return R(new Uint8Array(r))}async function He(e,t){let n=new Uint8Array(12);globalThis.crypto.getRandomValues(n);let r=new TextEncoder().encode(JSON.stringify(t)),i=await L().encrypt({name:`AES-GCM`,iv:n},e,r);return{iv:R(n),ciphertext:R(new Uint8Array(i))}}async function Ue(e,t){let n=z(t.iv),r=z(t.ciphertext),i=await L().decrypt({name:`AES-GCM`,iv:n},e,r);return new Uint8Array(i)}async function We(e,t,n,r){let i=await L().decrypt({name:`AES-GCM`,iv:t,additionalData:r},e,n);return new Uint8Array(i)}async function Ge(e,t){return JSON.parse(new TextDecoder().decode(await Ue(e,t)))}async function Ke(e){return He(e,Le)}async function qe(e,t){try{return await Ge(e,t)===Le}catch{return!1}}async function Je(e){let t=await L().digest(`SHA-256`,new TextEncoder().encode(e));return ze(new Uint8Array(t))}function Ye(){let e=e=>{let t=new Uint8Array(e);return globalThis.crypto.getRandomValues(t),B(t)},t=`kymoscope_${e(24)}.${e(32)}`;return{token:t,prefix:t.slice(0,12)}}var Xe=/^((?:kymoscope|prism)_[A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_-]+))?$/;function Ze(e){let t=Xe.exec(e.trim());return t?{auth:t[1],secret:t[2]??null}:{auth:e,secret:null}}var Qe=new TextEncoder().encode(`kymoscope-device-wrap-v1`);async function $e(){let e=await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),t=new Uint8Array(await L().exportKey(`raw`,e.publicKey));return{privateKey:e.privateKey,publicKey:R(t)}}async function et(e,t){let n=new Uint8Array(e.length+Qe.length);return n.set(e),n.set(Qe,e.length),L().importKey(`raw`,await L().digest(`SHA-256`,n),`AES-GCM`,!1,t)}async function tt(e,t,n){let r=await L().importKey(`raw`,z(t.epk),{name:`ECDH`,namedCurve:`P-256`},!1,[]),i=new Uint8Array(await L().deriveBits({name:`ECDH`,public:r},e,256));return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},await et(i,[`decrypt`]),z(t.ct)))}function nt(e,t,n){return rt(e,t,n)}async function rt(e,t,n,r,i){let a=await L().importKey(`raw`,z(e),{name:`ECDH`,namedCurve:`P-256`},!1,[]),o=r??await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),s=new Uint8Array(await L().deriveBits({name:`ECDH`,public:a},o.privateKey,256)),c=i??globalThis.crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:c,additionalData:new TextEncoder().encode(n)},await et(s,[`encrypt`]),t));return{epk:R(new Uint8Array(await L().exportKey(`raw`,o.publicKey))),iv:R(c),ct:R(l)}}async function it(e,t,n,r){return ut(await tt(e,t,`${n}|${r}`))}var at=new TextEncoder().encode(`kymoscope-sender-bind-v1`),ot=(e,t,n)=>`project:${e}:${t}|${n}`,st=(e,t,n)=>`live_stream:${e}|${t}:${n}`,ct=(e,t,n)=>`bind:${e}|${t}:${n}`;function lt(){return globalThis.crypto.getRandomValues(new Uint8Array(32))}function ut(e){return L().importKey(`raw`,e,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}function dt(e,t,n){return ft(e,t,n)}async function ft(e,t,n,r){let i=r??globalThis.crypto.getRandomValues(new Uint8Array(12)),a=await L().importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`]),o=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},a,t));return{iv:R(i),ct:R(o)}}async function pt(e,t,n){let r=await L().importKey(`raw`,e,`AES-GCM`,!1,[`decrypt`]);return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},r,z(t.ct)))}async function mt(e){let t=new TextEncoder().encode(e),n=new Uint8Array(t.length+at.length);return n.set(t),n.set(at,t.length),new Uint8Array(await L().digest(`SHA-256`,n))}async function ht(e,t,n){let r=await L().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`sign`]);return R(new Uint8Array(await L().sign(`HMAC`,r,new TextEncoder().encode(`sender|${t}|${n}`))))}async function gt(e,t,n,r){let i;try{i=z(r)}catch{return!1}let a=await L().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`verify`]);return L().verify(`HMAC`,a,i,new TextEncoder().encode(`sender|${t}|${n}`))}async function _t(e){let t=ze(new Uint8Array(await L().digest(`SHA-256`,z(e)))).slice(0,16).toUpperCase();return`${t.slice(0,4)}-${t.slice(4,8)}-${t.slice(8,12)}-${t.slice(12,16)}`}var vt=`kymoscope-workbook-export-v2`,yt=(e,t)=>`workbook:${e}:${t}`,bt=(e,t,n)=>`workbook:${e}|${t}:${n}`,xt=(e,t)=>`kymoscope-recovery-key-v1|${e}|${t}`,St=(e,t,n)=>`recovery:${e}|${t}:${n}`;function Ct(e,t,n){return wt(e,t,n)}async function wt(e,t,n,r){let i=r??globalThis.crypto.getRandomValues(new Uint8Array(12)),a=new Uint8Array(await L().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},e,t));return{iv:R(i),ct:R(a)}}async function Tt(e,t,n){return new Uint8Array(await L().decrypt({name:`AES-GCM`,iv:z(t.iv),additionalData:new TextEncoder().encode(n)},e,z(t.ct)))}function Et(e,t,n){return Dt(e,t,n)}async function Dt(e,t,n,r,i){let a=R(r??globalThis.crypto.getRandomValues(new Uint8Array(16))),o=await wt(await V(e,a),t,n,i);return{kdf:{name:`PBKDF2`,hash:`SHA-256`,iterations:I,salt:a},...o}}async function Ot(e,t,n){let r=t?.kdf;if(r?.name!==`PBKDF2`||r.hash!==`SHA-256`||r.iterations!==I||typeof r.salt!=`string`)throw Error(`unsupported_kdf`);return Tt(await V(e,r.salt),t,n)}var H=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`;function kt(e){let t=0,n=0,r=``;for(let i of e)for(n=(n<<8|i)&1048575,t+=8;t>=5;)r+=H[n>>>t-5&31],t-=5;return t>0&&(r+=H[n<<5-t&31]),r.match(/.{1,4}/g).join(`-`)}function At(e){let t=e.toUpperCase().replace(/[^A-Z2-7]/g,``);if(t.length!==52)return null;let n=[],r=0,i=0;for(let e of t)i=(i<<5|H.indexOf(e))&4095,r+=5,r>=8&&(n.push(i>>>r-8&255),r-=8);return n.length===32?new Uint8Array(n):null}async function jt(){let e=await L().generateKey({name:`ECDH`,namedCurve:`P-256`},!0,[`deriveBits`]),t=await L().exportKey(`jwk`,e.privateKey),n=new Uint8Array(await L().exportKey(`raw`,e.publicKey));return{d:Re(t.d),publicKey:R(n)}}function Mt(e,t){let n=z(t);return L().importKey(`jwk`,{kty:`EC`,crv:`P-256`,ext:!1,x:B(n.subarray(1,33)),y:B(n.subarray(33,65)),d:B(e)},{name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`])}var Nt=k(new WeakSet),Pt={length:0,x:new Float64Array,cols:new Map};function U(e,t,n,r){let i=t.length,a=t.map((e,t)=>`y${t}lo`),o=t.map((e,t)=>`y${t}hi`),s=new Uint8Array(e.length),c=0;for(let t=0;t<e.length;t++){let n=e[t];for(let e=0;e<i;e++){let r=n[o[e]];if(r!=null&&Number(r)!==Number(n[a[e]])){s[t]=1;break}}c+=1+s[t]}let l=new Float64Array(c),u=t.map(()=>new Float64Array(c).fill(NaN)),d,f=0,p=null;for(let t=0;t<e.length;t++){let m=e[t],h=n+(Number(m.b)+.5)*r,g=Number(m.pass??0);p!==null&&g!==p&&((d??=new Uint8Array(c))[f]=1),p=g,l[f]=h;for(let e=0;e<i;e++){let t=m[a[e]];t!=null&&(u[e][f]=Number(t))}if(f++,s[t]){l[f]=h;for(let e=0;e<i;e++){let t=m[o[e]];t!=null&&(u[e][f]=Number(t))}f++}}return{length:c,x:l,brk:d,cols:new Map(t.map((e,t)=>[e,u[t]]))}}function Ft(e,t){if(t>=e.length)return e;let n=new Map;for(let[r,i]of e.cols)n.set(r,i.subarray(0,t));return{length:t,x:e.x.subarray(0,t),brk:e.brk?.subarray(0,t),cols:n}}function It(e,t){if(t===void 0||e.length===0||e.x[e.length-1]<=t)return e;let n=0,r=e.length;for(;n<r;){let i=n+r>>1;e.x[i]<=t?n=i+1:r=i}return Ft(e,n)}function Lt(e,t,n){let r=(t,n)=>{let r=0,i=e.length;for(;r<i;){let a=r+i>>1;(n?e.x[a]<=t:e.x[a]<t)?r=a+1:i=a}return r},i=r(t,!1),a=r(n,!0);if(i===0&&a===e.length)return e;let o=new Map;for(let[t,n]of e.cols)o.set(t,n.subarray(i,a));return{length:a-i,x:e.x.subarray(i,a),brk:e.brk?.subarray(i,a),cols:o}}function Rt(e,t){let n=[];for(let r=0;r<e.length;r++)t(e.x[r])&&n.push(r);if(n.length===e.length)return e;let r=(e,t)=>{for(let r=0;r<n.length;r++)t[r]=e[n[r]];return t},i=new Map;for(let[t,a]of e.cols)i.set(t,r(a,new Float64Array(n.length)));return{length:n.length,x:r(e.x,new Float64Array(n.length)),brk:e.brk&&r(e.brk,new Uint8Array(n.length)),cols:i}}function zt(e,t,i,a,o=``,s=0){let c=n(e),l=D(e.length,a),u=t=>{let n=c&&r(c,t);return n?e=>n[e]:n=>{let r=e[n][t];return typeof r==`number`?r:NaN}},d=u(t),f=[];for(let e of l)Number.isNaN(d(e))||f.push(e);let p=f.length,m=p===e.length&&s===0,h=c&&r(c,t),g=m&&h?h:Float64Array.from(f,e=>d(e)+s),_=new Map;for(let e of i){let t=c&&r(c,e);if(c&&!t)continue;if(m&&t){_.set(e+o,t);continue}let n=u(e),i=Float64Array.from(f,n);!t&&!i.some(e=>!Number.isNaN(e))||_.set(e+o,i)}return{length:p,x:g,cols:_}}function Bt(e,t,n){if(!t&&n===0)return e;let r=e.x;if(n!==0){r=new Float64Array(e.length);for(let t=0;t<e.length;t++)r[t]=e.x[t]+n}let i=new Map;for(let[n,r]of e.cols)i.set(n+t,r);return{length:e.length,x:r,brk:e.brk,cols:i}}function Vt(e){let t=e.filter(e=>e.length>0);if(t.length===0)return Pt;if(t.length===1)return t[0];let n=t.reduce((e,t)=>e+t.length,0),r=new Float64Array(n),i=new Set;for(let e of t)for(let t of e.cols.keys())i.add(t);let a=new Map;for(let e of i)a.set(e,new Float64Array(n).fill(NaN));let o,s=0;for(let e of t){r.set(e.x,s),e.brk&&(o??=new Uint8Array(n)).set(e.brk,s);for(let[t,n]of e.cols)a.get(t).set(n,s);s+=e.length}return{length:n,x:r,brk:o,cols:a}}var W=64,Ht=class{nc;bucket;b;lo;hi;start=0;end=0;rowP;px=new Float64Array;pv=[];pStart=0;pEnd=0;mStart=0;mEnd=0;dirty=1/0;dirtyFront=-1;invalid=!0;constructor(e,t,n=1024){this.nc=e,this.bucket=t,this.b=new Float64Array(n),this.rowP=new Float64Array(n),this.lo=Array.from({length:e},()=>new Float64Array(n)),this.hi=Array.from({length:e},()=>new Float64Array(n))}get count(){return this.end-this.start}lowerBound(e){let t=this.start,n=this.end;for(;t<n;){let r=t+n>>>1;this.b[r]<e?t=r+1:n=r}return t}relocate(e,t){let n=this.count,r=n=>{if(t===n.length&&e<=this.start)return n.copyWithin(e,this.start,this.end),n;let r=new Float64Array(t);return r.set(n.subarray(this.start,this.end),e),r};this.b=r(this.b),this.rowP=r(this.rowP),this.lo=this.lo.map(r),this.hi=this.hi.map(r);let i=e-this.start;this.mStart+=i,this.mEnd+=i,this.dirty!==1/0&&(this.dirty+=i),this.dirtyFront>=0&&(this.dirtyFront+=i),this.start=e,this.end=e+n}touch(e){e===this.start&&e+1<this.mEnd?this.dirtyFront=e:e<this.dirty&&(this.dirty=e)}clearRow(e){for(let t=0;t<this.nc;t++)this.lo[t][e]=NaN,this.hi[t][e]=NaN;this.touch(e)}row(e){let t=this.end;if(t>this.start&&this.b[t-1]===e)return t-1;if(t===this.start||e>this.b[t-1])return t===this.b.length&&(this.start>this.count?this.relocate(0,this.b.length):this.relocate(0,Math.max(1024,this.b.length*2))),this.b[this.end]=e,this.clearRow(this.end),this.end++;let n=this.lowerBound(e);if(this.b[n]===e)return n;if(n!==this.start)throw Error(`TimeBuckets: insert inside the rows`);if(this.start===0){let e=Math.max(1024,this.count);this.relocate(e,2*e+this.count)}return this.start--,this.b[this.start]=e,this.clearRow(this.start),this.invalid=!0,this.start}dropBelow(e){this.start=this.lowerBound(e),this.start===this.end&&(this.start=this.end=0,this.invalid=!0)}reset(e){this.clearRow(e)}fold(e,t,n,r){this.touch(e);for(let i=0;i<t.length;i++){let a=t[i],o=1/0,s=-1/0;for(let e=n;e<r;e++){let t=a[e];t<o&&(o=t),t>s&&(s=t)}if(o===1/0)continue;let c=this.lo[i],l=this.hi[i],u=c[e],d=l[e];c[e]=u<=o?u:o,l[e]=d>=s?d:s}}toRows(e,t,n,r,i){let a=[];for(let o=e;o<t;o++){let e={...n,b:this.b[o],pass:0};for(let t=0;t<this.nc;t++){let n=this.lo[t][o];n===n&&(e[r[t]]=n,e[i[t]]=this.hi[t][o])}a.push(e)}return a}pointsOf(e){for(let t=0;t<this.nc;t++){let n=this.hi[t][e];if(n===n&&n!==this.lo[t][e])return 2}return 1}writeRow(e,t,n){let r=(this.b[e]+.5)*this.bucket;this.px[t]=r;for(let n=0;n<this.nc;n++)this.pv[n][t]=this.lo[n][e];if(n===2){this.px[t+1]=r;for(let n=0;n<this.nc;n++)this.pv[n][t+1]=this.hi[n][e]}}reserve(e){if(this.pEnd+e<=this.px.length)return;let t=this.pEnd-this.pStart,n=W-this.pStart,r=2*(t+e+W)<=this.px.length,i=r?this.px.length:Math.max(4096,2*(t+e+W)),a=e=>{if(r)return e.copyWithin(W,this.pStart,this.pEnd),e;let t=new Float64Array(i);return t.set(e.subarray(this.pStart,this.pEnd),W),t};this.px=a(this.px),this.pv=this.pv.map(a);for(let e=this.mStart;e<this.mEnd;e++)this.rowP[e]+=n;this.pStart=W,this.pEnd=W+t}writeFrom(e){this.mEnd=e;for(let t=e;t<this.end;t++){let e=this.pointsOf(t);this.reserve(e),this.rowP[t]=this.pEnd,this.writeRow(t,this.pEnd,e),this.pEnd+=e,this.mEnd=t+1}}sync(){if(this.pv.length!==this.nc&&(this.pv=Array.from({length:this.nc},()=>new Float64Array)),this.invalid||this.start<this.mStart||this.start>=this.mEnd)this.pStart=this.pEnd=W,this.mStart=this.mEnd=this.start,this.reserve(0),this.writeFrom(this.start);else{this.start>this.mStart&&(this.mStart=this.start,this.pStart=this.rowP[this.start]);let e=Math.max(this.start,Math.min(this.dirty,this.mEnd)),t=this.dirtyFront;if(t===this.start)if(t+1<e){let e=this.pointsOf(t),n=this.rowP[t+1]-e;if(n<0)return this.invalid=!0,this.sync();this.rowP[t]=n,this.pStart=n,this.writeRow(t,n,e)}else e=t;e<this.end&&(e<this.mEnd&&(this.pEnd=this.rowP[e]),this.writeFrom(e))}this.dirty=1/0,this.dirtyFront=-1,this.invalid=!1}toPoints(e,t,n){this.sync();let r=e<t?this.rowP[e]:0,i=e<t?t<this.mEnd?this.rowP[t]:this.pEnd:0;return{length:i-r,x:this.px.subarray(r,i),cols:new Map(n.map((e,t)=>[e,this.pv[t].subarray(r,i)]))}}};function Ut(e){if(!(e>0)||!Number.isFinite(e))return e;let t=10**Math.floor(Math.log10(e)),n=e/t;return(n<1.5?1:n<3.5?2:n<7.5?5:10)*t}function Wt(e,t,n,r,i){if(n instanceof Float64Array||n instanceof Float32Array)e.set(n.subarray(r,r+i),t);else for(let a=0;a<i;a++)e[t+a]=+n[r+a]}function Gt(e,t){let n=e.t.length,r=Object.keys(e.columns).filter(e=>e!==t);for(let t of r)if(e.columns[t].length!==n)throw RangeError(`live column "${t}" has ${e.columns[t].length} values for ${n} times`);return r}var Kt=`ElapsedTime`,qt=[];function Jt(e){let t=qt[e];if(!t){let n=Array.from({length:e},(e,t)=>`y${t}lo`),r=Array.from({length:e},(e,t)=>`y${t}hi`);t={lo:n,hi:r,row:JSON.parse(JSON.stringify(Object.fromEntries([[`b`,0],[`pass`,0],...n.flatMap((e,t)=>[[e,null],[r[t],null]])])))},qt[e]=t}return t}var Yt=4096,Xt=4,Zt=class{time;alloc;maxBytes;cols=new Map;head=0;count=0;full=!1;pushed=0;clears=0;signalOrder=[];unitByName=new Map;capacity;timeColumn;timeUnit;lastBackAbs=-1;get timeMonotonic(){return this.lastBackAbs<=this.pushed-this.size}lastPushT=-1/0;decCache=null;passTrack=null;prevCyclePts=null;curCyclePts=null;trackers=new Map;debugLastScan=0;timeDec=null;mirrors=new Map;historyDec=null;constructor(e,t=Kt,n=`s`,r=1/0){this.capacity=Math.max(2,Math.floor(e)),this.timeColumn=t,this.timeUnit=n,this.maxBytes=r,this.alloc=Math.min(this.capacity,Yt),this.time=new Float64Array(this.alloc)}get maxSamples(){return this.capacity}newColumn(){return new Float64Array(this.alloc).fill(NaN)}grow(){let e=Math.min(this.capacity,this.alloc*2);if(e*8*(1+this.cols.size)>this.maxBytes){this.capacity=this.alloc;return}let t=t=>{let n=new Float64Array(e);return n.set(t),n};this.time=t(this.time);for(let[e,n]of this.cols)this.cols.set(e,t(n));this.alloc=e}get version(){return this.pushed+this.clears}get size(){return this.full?this.capacity:this.count}push(e){for(let[t,n]of this.trackers){let r=t===this.timeColumn?e.t:e.signals.find(e=>e.name===t)?.value;r!==void 0&&Number.isFinite(r)&&this.advanceCycle(t,n,r,this.pushed)}e.t<this.lastPushT&&this.steppedBack(this.pushed),this.lastPushT=e.t,!this.full&&this.count===this.alloc&&(this.grow(),this.count===this.capacity&&(this.full=!0));let t=this.full?this.head:this.count;this.time[t]=e.t;for(let e of this.signalOrder)this.cols.get(e)[t]=NaN;for(let n of e.signals){if(this.computed.has(n.name))continue;let e=this.cols.get(n.name);e?n.unit&&n.unit!==this.unitByName.get(n.name)&&(this.unitByName.get(n.name)&&this.dropColumnSamples(n.name,e),this.unitByName.set(n.name,n.unit)):(e=this.newColumn(),this.cols.set(n.name,e),this.signalOrder.push(n.name),this.unitByName.set(n.name,n.unit??``)),e[t]=n.value}this.pushed++,this.full?this.head=(this.head+1)%this.capacity:(this.count++,this.count===this.capacity&&(this.full=!0))}pushColumns(e){let t=e.t,n=t.length,r=Gt(e,this.timeColumn);if(n===0)return;if(this.trackers.size>0){for(let i=0;i<n;i++)this.push({t:t[i],signals:r.map(t=>({name:t,unit:e.units?.[t],value:e.columns[t][i]}))});return}for(let t of r){if(this.computed.has(t))continue;let n=e.units?.[t],r=this.cols.get(t);r?n&&n!==this.unitByName.get(t)&&(this.unitByName.get(t)&&this.dropColumnSamples(t,r),this.unitByName.set(t,n)):(this.cols.set(t,this.newColumn()),this.signalOrder.push(t),this.unitByName.set(t,n??``))}for(let e=0;e<n;e++){let n=+t[e];n<this.lastPushT&&this.steppedBack(this.pushed+e),this.lastPushT=n}let i=Math.max(0,n-this.capacity);for(this.pushed+=i;i<n;){!this.full&&this.count===this.alloc&&(this.grow(),this.count===this.capacity&&(this.full=!0));let r=this.full?this.head:this.count,a=Math.min(n-i,this.full?this.capacity-this.head:this.alloc-this.count);Wt(this.time,r,t,i,a);for(let t of this.signalOrder){let n=this.cols.get(t),o=this.computed.has(t)?void 0:e.columns[t];o?Wt(n,r,o,i,a):n.fill(NaN,r,r+a)}this.pushed+=a,i+=a,this.full?this.head=(this.head+a)%this.capacity:(this.count+=a,this.count===this.capacity&&(this.full=!0))}}steppedBack(e){this.lastBackAbs=e,this.timeDec=null}dropColumnSamples(e,t){t.fill(NaN),this.decCache=null,this.timeDec=null;for(let t of this.trackers.values())t.previous?.cols.delete(e);this.clears++}clear(){this.clears++,this.head=0,this.count=0,this.full=!1,this.pushed=0,this.cols.clear(),this.computed.clear(),this.signalOrder.length=0,this.unitByName.clear(),this.decCache=null,this.timeDec=null,this.lastBackAbs=-1,this.lastPushT=-1/0,this.trackers.clear()}columnArray(e){return e===this.timeColumn?this.time:this.cols.get(e)}physOf(e){let t=e-(this.pushed-this.size);return this.full?(this.head+t)%this.capacity:t}copyRange(e,t,n,r=[this.timeColumn,...this.signalOrder]){let i=Math.max(t,this.pushed-this.size),a=Math.max(0,n-i),o=this.columnArray(e),s=new Float64Array(a),c=new Map;for(let e of r)this.columnArray(e)&&c.set(e,new Float64Array(a));for(let e=0;e<a;e++){let t=this.physOf(i+e);s[e]=o?o[t]:NaN;for(let[n,r]of c)r[e]=this.columnArray(n)[t]}return{x:s,cols:c}}advanceCycle(e,t,n,r){Number.isFinite(t.lastX)&&n<t.lastX&&t.lastX-n>(t.hi-t.lo)/2?(t.previous=this.copyRange(e,t.start,r),t.start=r,t.lo=t.hi=n):Number.isFinite(t.lastX)?(n<t.lo&&(t.lo=n),n>t.hi&&(t.hi=n)):t.lo=t.hi=n,n<t.allLo&&(t.allLo=n),n>t.allHi&&(t.allHi=n),t.lastX=n}trackCycles(e){if(this.trackers.has(e))return;let t={start:this.pushed-this.size,lo:0,hi:0,lastX:NaN,allLo:1/0,allHi:-1/0,previous:null},n=this.columnArray(e),r=this.pushed-this.size;if(n)for(let i=r;i<this.pushed;i++){let r=n[this.physOf(i)];Number.isFinite(r)&&this.advanceCycle(e,t,r,i)}this.trackers.set(e,t)}cycleSpan(e){let t=this.trackers.get(e);return t&&t.allHi>t.allLo?[t.allLo,t.allHi]:null}decimateCycle(e,t,n,r,i,a){let o=this.trackers.get(e),s=n.filter(e=>this.cols.has(e));if(!o)return{x0:r,bucket:0,rows:[],columns:s};let c=t===`previous`?o.previous:this.copyRange(e,o.start,this.pushed,s);if(!c)return{x0:r,bucket:0,rows:[],columns:s};let l=s.map(e=>c.cols.get(e)??new Float64Array(c.x.length).fill(NaN));return re(c.x,l,s,r,i,a)}decimateCyclePoints(e,t,n,r,i,a){let o=()=>{let o=this.decimateCycle(e,t,n,r,i,a);return U(o.rows,o.columns,o.x0,o.bucket)},s=this.trackers.get(e),c=Math.floor(a),l=this.columnArray(e);if(!s||!l||!(i>r)||c<1)return o();let u=n.filter(e=>this.cols.has(e)),d=u.join(` `);if(t===`previous`){let e=this.prevCyclePts;if(e&&e.snap===s.previous&&e.x0===r&&e.x1===i&&e.mp===a&&e.colsKey===d)return e.pts;let t=o();return this.prevCyclePts={snap:s.previous,x0:r,x1:i,mp:a,colsKey:d,pts:t},t}if(s.start<this.pushed-this.size)return o();let f=this.curCyclePts;if((!f||f.key!==e||f.start!==s.start||f.x0!==r||f.x1!==i||f.mp!==a||f.colsKey!==d||f.clears!==this.clears)&&(f=this.curCyclePts={key:e,start:s.start,x0:r,x1:i,mp:a,colsKey:d,clears:this.clears,done:s.start,pts:null,seen:new Uint8Array(c),lo:u.map(()=>new Float64Array(c).fill(1/0)),hi:u.map(()=>new Float64Array(c).fill(-1/0))}),f.pts&&f.done===this.pushed)return f.pts;let p=(i-r)/a,m=u.map(e=>this.columnArray(e));for(let e=f.done;e<this.pushed;e++){let t=this.physOf(e),n=l[t];if(!(n>=r&&n<=i))continue;let a=Math.floor((n-r)/p);a>=c&&(a=c-1),f.seen[a]=1;for(let e=0;e<m.length;e++){let n=m[e][t];n===n&&(n<f.lo[e][a]&&(f.lo[e][a]=n),n>f.hi[e][a]&&(f.hi[e][a]=n))}}f.done=this.pushed;let h=0,g=new Uint8Array(c);for(let e=0;e<c;e++)if(f.seen[e]){for(let t=0;t<m.length;t++){let n=f.hi[t][e];if(n!==-1/0&&n!==f.lo[t][e]){g[e]=1;break}}h+=1+g[e]}let _=new Float64Array(h),v=u.map(()=>new Float64Array(h)),y=0;for(let e=0;e<c;e++){if(!f.seen[e])continue;let t=r+(e+.5)*p;_[y]=t;for(let t=0;t<m.length;t++){let n=f.lo[t][e];v[t][y]=n===1/0?NaN:n}if(y++,g[e]){_[y]=t;for(let t=0;t<m.length;t++){let n=f.hi[t][e];v[t][y]=n===-1/0?NaN:n}y++}}return f.pts={length:h,x:_,cols:new Map(u.map((e,t)=>[e,v[t]]))},f.pts}computed=new Set;get firstAbs(){return this.pushed-this.size}get endAbs(){return this.pushed}range(e,t,n){let r=Math.max(t,this.firstAbs),i=new Float64Array(Math.max(0,Math.min(n,this.pushed)-r)),a=this.columnArray(e);if(!a)return i.fill(NaN);for(let e=0;e<i.length;e++)i[e]=a[this.physOf(r+e)];return i}mirrorColumn(e){let t=this.columnArray(e);if(!t)return;let n=this.firstAbs,r=this.pushed,i=this.mirrors.get(e);if((!i||i.clears!==this.clears)&&(i={arr:new Float64Array(Math.max(16,r-n)),first:n,end:n,clears:this.clears,view:null},this.mirrors.set(e,i)),i.first===n&&i.end===r&&i.view)return i.view;if(n>i.first){let e=Math.max(0,i.end-n);e>0&&i.arr.copyWithin(0,n-i.first,i.end-i.first),i.first=n,i.end=n+e}if(i.arr.length<r-n){let e=new Float64Array(Math.max(r-n,i.arr.length*2));e.set(i.arr.subarray(0,i.end-i.first)),i.arr=e}for(let e=i.end;e<r;){let a=this.physOf(e),o=Math.min(r-e,(this.full?this.capacity:this.alloc)-a);i.arr.set(t.subarray(a,a+o),e-n),e+=o}return i.end=r,i.view=i.arr.subarray(0,r-n),i.view}writeComputed(e,t,n,r){let i=this.mirrors.get(e);i&&n<i.end&&(i.end=Math.max(i.first,n),i.view=null);let a=this.cols.get(e);(!a||!this.computed.has(e))&&(a||(a=this.newColumn(),this.cols.set(e,a),this.signalOrder.push(e)),this.computed.add(e)),this.unitByName.set(e,t);for(let e=0;e<r.length;e++){let t=n+e;t<this.firstAbs||t>=this.pushed||(a[this.physOf(t)]=r[e]??NaN)}}clearComputed(){for(let e of this.computed){this.cols.delete(e),this.unitByName.delete(e);for(let t of this.trackers.values())t.previous?.cols.delete(e)}this.signalOrder.splice(0,this.signalOrder.length,...this.signalOrder.filter(e=>!this.computed.has(e))),this.computed.clear(),this.decCache=null,this.timeDec=null,this.clears++}get columnNames(){return[this.timeColumn,...this.signalOrder]}unitOf(e){return e===this.timeColumn?this.timeUnit:this.unitByName.get(e)??``}columnValues(e){let t=this.size,n=new Float64Array(t),r=e===this.timeColumn?this.time:this.cols.get(e);if(!r)return n.fill(NaN),n;for(let e=0;e<t;e++)n[e]=r[this.full?(this.head+e)%this.capacity:e];return n}accumulate(e,t,n,r,i){let a=Jt(i.length),o=n[t];if(Number.isNaN(o))return;let s=Math.floor(o/r),c=e.get(s);c||(c={...a.row,b:s},e.set(s,c));for(let e=0;e<i.length;e++){let n=i[e][t];if(Number.isNaN(n))continue;let r=a.lo[e],o=a.hi[e],s=c[r],l=c[o];c[r]=s==null?n:Math.min(s,n),c[o]=l==null?n:Math.max(l,n)}}decimateColumns(e,t,n,r,i){if(e===this.timeColumn&&this.timeMonotonic)return this.decimateTime(t,n,r,i);let a=e===this.timeColumn?this.time:this.cols.get(e),o=a?t.filter(e=>this.cols.has(e)):[],s=(r-n)/i,c=this.size;if(c>1&&e===this.timeColumn&&this.timeMonotonic&&a){let e=a[this.full?this.head:0],t=(a[this.full?(this.head+c-1)%this.capacity:c-1]-e)/(c-1);t>0&&t<s&&(s=.7*Math.max(t,(r-n)/(i*Xt)))}let l=Ut(s);{let t=this.decCache;t&&t.xKey===e&&t.colsKey===o.join(` `)&&t.bucket>0&&s>=t.bucket*.5&&s<=t.bucket*2&&(l=t.bucket)}if(!a||o.length===0||!(r>n)||!(l>0))return{x0:n,bucket:0,rows:[],columns:o};let u=o.map(e=>this.cols.get(e)),d=this.size,f=o.join(`\0`);this.debugLastScan=0;let p=this.decCache,m=p!==null&&e===this.timeColumn&&this.timeMonotonic&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&this.pushed>=p.pushed&&this.pushed-p.pushed<d&&p.map.size<this.capacity*2,h=p!==null&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&p.pushed===this.pushed,g=this.pushed-d,_=e===this.timeColumn&&this.timeMonotonic?g:Math.max(g,this.passStart(e,a)),v=p!==null&&(_>g||!this.full)&&p.xKey===e&&p.colsKey===f&&p.bucket===l&&p.passStart===_&&p.pushed>=_&&p.pushed<=this.pushed,y;if(h&&p)y=p.map;else if(v&&p){y=p.map;for(let e=p.pushed;e<this.pushed;e++)this.debugLastScan++,this.accumulate(y,this.physOf(e),a,l,u);p.pushed=this.pushed}else if(m&&p){y=p.map;let e=this.pushed-p.pushed;for(let t=d-e;t<d;t++){let e=this.full?(this.head+t)%this.capacity:t;this.debugLastScan++,this.accumulate(y,e,a,l,u)}if(this.full&&d>0){let e=a[this.head];if(!Number.isNaN(e)){let t=Math.floor(e/l);for(let e=p.lowB;e<t;e++)y.delete(e);y.delete(t);for(let e=0;e<d;e++){let n=(this.head+e)%this.capacity,r=a[n];if(!Number.isNaN(r)){if(Math.floor(r/l)>t)break;this.debugLastScan++,this.accumulate(y,n,a,l,u)}}p.lowB=t}}p.pushed=this.pushed}else{y=new Map;let t=1/0;for(let e=_;e<this.pushed;e++){let n=this.physOf(e),r=a[n];if(Number.isNaN(r))continue;this.debugLastScan++,this.accumulate(y,n,a,l,u);let i=Math.floor(r/l);i<t&&(t=i)}this.decCache={xKey:e,colsKey:f,cols:o,bucket:l,map:y,pushed:this.pushed,lowB:Number.isFinite(t)?t:0,passStart:_}}let b=Math.floor(n/l),x=Math.floor(r/l),S=[];for(let e of y.values()){let t=e.b;t>=b&&t<=x&&S.push(e)}return S.sort((e,t)=>Number(e.b)-Number(t.b)),{x0:0,bucket:l,rows:S,columns:o}}passStart(e,t){let n=this.pushed-this.size,r=this.passTrack;(!r||r.key!==e||r.clears!==this.clears)&&(r=this.passTrack={key:e,clears:this.clears,done:n,prevX:NaN,cuts:[]});let{x0:i,x1:a}=this.columnBounds(e),o=(a-i)/2;for(let e=Math.max(r.done,n);e<this.pushed;e++){let n=t[this.physOf(e)];Number.isNaN(n)||(n<r.prevX-o&&(r.cuts.push({abs:e,drop:r.prevX-n}),r.cuts.length>64&&r.cuts.shift()),r.prevX=n)}for(r.done=this.pushed;r.cuts.length>0&&r.cuts[0].abs<n;)r.cuts.shift();for(let e=r.cuts.length-1;e>=0;e--)if(r.cuts[e].drop>o)return r.cuts[e].abs;return n}timeIndex(e){let t=0,n=this.size;for(;t<n;){let r=t+n>>>1;this.time[this.full?(this.head+r)%this.capacity:r]<e?t=r+1:n=r}return t}foldBuckets(e,t,n,r,i){this.eachBucketRun(t,n,r,(t,n,r)=>e.fold(e.row(t),i,n,r))}eachBucketRun(e,t,n,r){if(t<=e)return;this.debugLastScan+=t-e;let i=this.time,a=e=>Math.floor(i[e]/n),o=(e,t)=>{let n=e;for(;n<t;){let e=a(n),i=n,o=1;for(;n+o<t&&a(n+o)===e;)i=n+o,o*=2;let s=i+1,c=Math.min(n+o,t);for(;s<c;){let t=s+c>>>1;a(t)===e?s=t+1:c=t}r(e,n,s),n=s}};if(!this.full)return o(e,t);let s=(this.head+e)%this.capacity,c=s+(t-e);if(c<=this.capacity)return o(s,c);o(s,this.capacity),o(0,c-this.capacity)}decimateTime(e,t,n,r){let i=this.timeBuckets(e,t,n,r);if(!i.store)return{x0:t,bucket:0,rows:[],columns:i.cols};let a=Jt(i.cols.length),o=()=>i.store.toRows(i.i0,i.i1,a.row,a.lo,a.hi),s=this.historyDec;return{x0:0,bucket:i.bucket,rows:s?.store===i.store?s.rows??=o():o(),columns:i.cols}}decimatePoints(e,t,n,r,i){if(e===this.timeColumn&&this.timeMonotonic){let e=this.timeBuckets(t,n,r,i);return e.store?e.store.toPoints(e.i0,e.i1,e.cols):{length:0,x:new Float64Array,cols:new Map(e.cols.map(e=>[e,new Float64Array]))}}let a=this.decimateColumns(e,t,n,r,i);return U(a.rows,a.columns,a.x0,a.bucket)}timeBuckets(e,t,n,r){let i=e.filter(e=>this.cols.has(e)),a=this.size,o={cols:i,bucket:0,store:null,i0:0,i1:0};if(i.length===0||!(n>t)||a===0)return o;let s=e=>this.full?(this.head+e)%this.capacity:e,c=this.time[s(0)],l=this.time[s(a-1)],u=(n-t)/r;if(a>1){let e=(l-c)/(a-1);e>0&&e<u&&(u=.7*Math.max(e,(n-t)/(r*Xt)))}let d=Ut(u),f=i.join(` `),p=this.timeDec;if(p&&p.colsKey===f&&u>=p.bucket*.5&&u<=p.bucket*2&&(d=p.bucket),!(d>0))return o;let m=i.map(e=>this.cols.get(e)),h=Math.floor(t/d),g=Math.floor(n/d);this.debugLastScan=0;let _=e=>({cols:i,bucket:d,store:e,i0:e.lowerBound(h),i1:e.lowerBound(g+1)});if(n<l-(n-t)){let e=this.timeIndex(h*d),t=this.timeIndex((g+1)*d),n=this.pushed-a,r=`${f}|${d}|${h}|${g}|${n+e}|${n+t}|${this.clears}`;if(this.historyDec?.key!==r){let n=new Ht(i.length,d);this.foldBuckets(n,e,t,d,m),this.historyDec={key:r,store:n}}return _(this.historyDec.store)}let v=p?this.pushed-p.pushed:0;if(p&&p.colsKey===f&&p.bucket===d&&v>=0&&v<a){let e=p.store;if(h<p.fromB){let t=new Ht(i.length,d);this.foldBuckets(t,this.timeIndex(h*d),this.timeIndex(p.fromB*d),d,m);for(let n=t.end-1;n>=t.start;n--){let r=e.row(t.b[n]);for(let i=0;i<t.nc;i++)e.lo[i][r]=t.lo[i][n],e.hi[i][r]=t.hi[i][n]}p.fromB=h}v>0&&this.foldBuckets(e,a-v,a,d,m);let t=Math.floor(c/d);return this.full&&t>=p.fromB&&(e.dropBelow(t),e.reset(e.row(t)),this.foldBuckets(e,0,this.timeIndex((t+1)*d),d,m),p.fromB=t),h>p.fromB&&(e.dropBelow(h),p.fromB=h),p.pushed=this.pushed,_(e)}let y=new Ht(i.length,d),b=this.timeIndex(h*d);return this.foldBuckets(y,b,a,d,m),this.timeDec={colsKey:f,bucket:d,store:y,pushed:this.pushed,fromB:b===0?Math.floor(c/d):h},_(y)}lastValue(e){let t=e===this.timeColumn?this.time:this.cols.get(e);return!t||this.size===0?NaN:t[this.full?(this.head-1+this.capacity)%this.capacity:this.count-1]}xTravel(e,t){let n=this.columnArray(e),r=this.size;if(!n||r<2)return NaN;let i=e=>this.full?(this.head+e)%this.capacity:e,a=this.time[i(r-1)],o=0,s=NaN,c=a;for(let e=r-1;e>=0;e--){let r=i(e),l=this.time[r];if(l<a-t)break;let u=n[r];Number.isNaN(u)||(s>u&&(o+=s-u),s=u,c=l)}let l=a-c;return l>0&&l<t?o*t/l:o}latestTime(){return this.lastValue(this.timeColumn)}xAtTime(e,t,n=!1){let r=this.columnArray(e),i=this.size;if(!r||i===0||!this.timeMonotonic)return NaN;let a=e=>this.full?(this.head+e)%this.capacity:e,o=0,s=i-1;if(t<=this.time[a(0)])return r[a(0)];if(t>=this.time[a(s)])return r[a(s)];for(;s-o>1;){let e=o+s>>1;this.time[a(e)]<=t?o=e:s=e}let c=a(o),l=a(s),u=r[c],d=r[l];if(n||!(d>=u))return u;let f=this.time[c],p=this.time[l];return p>f?u+(d-u)*(t-f)/(p-f):u}boundsCache=new Map;columnBounds(e){let t=e===this.timeColumn?this.time:this.cols.get(e),n=this.size;if(!t||n===0)return{x0:0,x1:0};if(t===this.time&&this.timeMonotonic)return{x0:this.time[this.full?this.head:0],x1:this.time[this.full?(this.head+n-1)%this.capacity:n-1]};let r=this.pushed-n,i=this.boundsCache.get(e);if(i&&i.version===this.version)return i.b;(!i||i.clears!==this.clears||i.loAbs<r||i.hiAbs<r)&&(i={version:-1,clears:this.clears,done:r,lo:1/0,hi:-1/0,loAbs:1/0,hiAbs:1/0,b:{x0:0,x1:0}},this.boundsCache.set(e,i));for(let e=Math.max(i.done,r);e<this.pushed;e++){let n=t[this.physOf(e)];Number.isNaN(n)||(n<=i.lo&&(i.lo=n,i.loAbs=e),n>=i.hi&&(i.hi=n,i.hiAbs=e))}return i.done=this.pushed,i.version=this.version,i.b=Number.isFinite(i.lo)?{x0:i.lo,x1:i.hi}:{x0:0,x1:0},i.b}toDataset(e){let t=[this.timeColumn,...this.signalOrder],n=[this.timeUnit,...this.signalOrder.map(e=>this.unitByName.get(e)??``)],r=this.size,i=e&&e>0&&r>e?Math.ceil(r/e):1,a=e=>{let t=this.full?(this.head+e)%this.capacity:e,n={[this.timeColumn]:this.time[t]};for(let e of this.signalOrder){let r=this.cols.get(e)[t];n[e]=Number.isNaN(r)?null:r}return n},o=this.pushed-r,s=i>1?(i-o%i)%i:0,c=[];for(let e=s;e<r;e+=i)c.push(a(e));return r>0&&(c.length===0||(r-1-s)%i!=0)&&c.push(a(r-1)),{meta:``,columnNames:t,units:n,rows:c}}};new TextEncoder;var Qt=4096;function $t(e,t,n){let r=t,i=r+n,a=[],o=``;for(;r<i;){let t=e[r++];if(!(t&128))a.push(t);else if((t&224)==192){let n=e[r++]&63;a.push((t&31)<<6|n)}else if((t&240)==224){let n=e[r++]&63,i=e[r++]&63;a.push((t&31)<<12|n<<6|i)}else if((t&248)==240){let n=e[r++]&63,i=e[r++]&63,o=e[r++]&63,s=(t&7)<<18|n<<12|i<<6|o;s>65535&&(s-=65536,a.push(s>>>10&1023|55296),s=56320|s&1023),a.push(s)}else a.push(t);a.length>=Qt&&(o+=String.fromCharCode(...a),a.length=0)}return a.length>0&&(o+=String.fromCharCode(...a)),o}var en=new TextDecoder,tn=200;function nn(e,t,n){let r=e.subarray(t,t+n);return en.decode(r)}function rn(e,t,n){return n>tn?nn(e,t,n):$t(e,t,n)}var G=class{type;data;constructor(e,t){this.type=e,this.data=t}},K=class e extends Error{constructor(t){super(t);let n=Object.create(e.prototype);Object.setPrototypeOf(this,n),Object.defineProperty(this,`name`,{configurable:!0,enumerable:!1,value:e.name})}};function an(e,t,n){let r=Math.floor(n/4294967296),i=n;e.setUint32(t,r),e.setUint32(t+4,i)}function on(e,t){let n=e.getInt32(t),r=e.getUint32(t+4);return n*4294967296+r}function sn(e,t){let n=e.getUint32(t),r=e.getUint32(t+4);return n*4294967296+r}var cn=4294967295,ln=17179869183;function un({sec:e,nsec:t}){if(e>=0&&t>=0&&e<=ln)if(t===0&&e<=cn){let t=new Uint8Array(4);return new DataView(t.buffer).setUint32(0,e),t}else{let n=e/4294967296,r=e&4294967295,i=new Uint8Array(8),a=new DataView(i.buffer);return a.setUint32(0,t<<2|n&3),a.setUint32(4,r),i}else{let n=new Uint8Array(12),r=new DataView(n.buffer);return r.setUint32(0,t),an(r,4,e),n}}function dn(e){let t=e.getTime(),n=Math.floor(t/1e3),r=(t-n*1e3)*1e6,i=Math.floor(r/1e9);return{sec:n+i,nsec:r-i*1e9}}function fn(e){return e instanceof Date?un(dn(e)):null}function pn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);switch(e.byteLength){case 4:return{sec:t.getUint32(0),nsec:0};case 8:{let e=t.getUint32(0),n=t.getUint32(4);return{sec:(e&3)*4294967296+n,nsec:e>>>2}}case 12:return{sec:on(t,4),nsec:t.getUint32(0)};default:throw new K(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${e.length}`)}}function mn(e){let t=pn(e);return new Date(t.sec*1e3+t.nsec/1e6)}var hn={type:-1,encode:fn,decode:mn},gn=class e{static defaultCodec=new e;__brand;builtInEncoders=[];builtInDecoders=[];encoders=[];decoders=[];constructor(){this.register(hn)}register({type:e,encode:t,decode:n}){if(e>=0)this.encoders[e]=t,this.decoders[e]=n;else{let r=-1-e;this.builtInEncoders[r]=t,this.builtInDecoders[r]=n}}tryToEncode(e,t){for(let n=0;n<this.builtInEncoders.length;n++){let r=this.builtInEncoders[n];if(r!=null){let i=r(e,t);if(i!=null)return new G(-1-n,i)}}for(let n=0;n<this.encoders.length;n++){let r=this.encoders[n];if(r!=null){let i=r(e,t);if(i!=null)return new G(n,i)}}return e instanceof G?e:null}decode(e,t,n){let r=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return r?r(e,t,n):new G(t,e)}};function _n(e){return e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer}function vn(e){return e instanceof Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):_n(e)?new Uint8Array(e):Uint8Array.from(e)}function yn(e){return`${e<0?`-`:``}0x${Math.abs(e).toString(16).padStart(2,`0`)}`}var bn=16,xn=16,Sn=class{hit=0;miss=0;caches;maxKeyLength;maxLengthPerKey;constructor(e=bn,t=xn){this.maxKeyLength=e,this.maxLengthPerKey=t,this.caches=[];for(let e=0;e<this.maxKeyLength;e++)this.caches.push([])}canBeCached(e){return e>0&&e<=this.maxKeyLength}find(e,t,n){let r=this.caches[n-1];FIND_CHUNK:for(let i of r){let r=i.bytes;for(let i=0;i<n;i++)if(r[i]!==e[t+i])continue FIND_CHUNK;return i.str}return null}store(e,t){let n=this.caches[e.length-1],r={bytes:e,str:t};n.length>=this.maxLengthPerKey?n[Math.random()*n.length|0]=r:n.push(r)}decode(e,t,n){let r=this.find(e,t,n);if(r!=null)return this.hit++,r;this.miss++;let i=$t(e,t,n),a=Uint8Array.prototype.slice.call(e,t,t+n);return this.store(a,i),i}},Cn=`array`,q=`map_key`,wn=`map_value`,Tn=e=>{if(typeof e==`string`||typeof e==`number`)return e;throw new K(`The type of key must be string or number but `+typeof e)},En=class{stack=[];stackHeadPosition=-1;get length(){return this.stackHeadPosition+1}top(){return this.stack[this.stackHeadPosition]}pushArrayState(e){let t=this.getUninitializedStateFromPool();t.type=Cn,t.position=0,t.size=e,t.array=Array(e)}pushMapState(e){let t=this.getUninitializedStateFromPool();t.type=q,t.readCount=0,t.size=e,t.map={}}getUninitializedStateFromPool(){return this.stackHeadPosition++,this.stackHeadPosition===this.stack.length&&this.stack.push({type:void 0,size:0,array:void 0,position:0,readCount:0,map:void 0,key:null}),this.stack[this.stackHeadPosition]}release(e){if(this.stack[this.stackHeadPosition]!==e)throw Error(`Invalid stack state. Released state is not on top of the stack.`);if(e.type===Cn){let t=e;t.size=0,t.array=void 0,t.position=0,t.type=void 0}if(e.type===q||e.type===wn){let t=e;t.size=0,t.map=void 0,t.readCount=0,t.type=void 0}this.stackHeadPosition--}reset(){this.stack.length=0,this.stackHeadPosition=-1}},J=-1,Dn=new DataView(new ArrayBuffer(0)),On=new Uint8Array(Dn.buffer);try{Dn.getInt8(0)}catch(e){if(!(e instanceof RangeError))throw Error(`This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access`)}var kn=RangeError(`Insufficient data`),An=new Sn,jn=class e{extensionCodec;context;useBigInt64;rawStrings;maxStrLength;maxBinLength;maxArrayLength;maxMapLength;maxExtLength;keyDecoder;mapKeyConverter;totalPos=0;pos=0;view=Dn;bytes=On;headByte=J;stack=new En;entered=!1;constructor(e){this.extensionCodec=e?.extensionCodec??gn.defaultCodec,this.context=e?.context,this.useBigInt64=e?.useBigInt64??!1,this.rawStrings=e?.rawStrings??!1,this.maxStrLength=e?.maxStrLength??4294967295,this.maxBinLength=e?.maxBinLength??4294967295,this.maxArrayLength=e?.maxArrayLength??4294967295,this.maxMapLength=e?.maxMapLength??4294967295,this.maxExtLength=e?.maxExtLength??4294967295,this.keyDecoder=e?.keyDecoder===void 0?An:e.keyDecoder,this.mapKeyConverter=e?.mapKeyConverter??Tn}clone(){return new e({extensionCodec:this.extensionCodec,context:this.context,useBigInt64:this.useBigInt64,rawStrings:this.rawStrings,maxStrLength:this.maxStrLength,maxBinLength:this.maxBinLength,maxArrayLength:this.maxArrayLength,maxMapLength:this.maxMapLength,maxExtLength:this.maxExtLength,keyDecoder:this.keyDecoder})}reinitializeState(){this.totalPos=0,this.headByte=J,this.stack.reset()}setBuffer(e){let t=vn(e);this.bytes=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength),this.pos=0}appendBuffer(e){if(this.headByte===J&&!this.hasRemaining(1))this.setBuffer(e);else{let t=this.bytes.subarray(this.pos),n=vn(e),r=new Uint8Array(t.length+n.length);r.set(t),r.set(n,t.length),this.setBuffer(r)}}hasRemaining(e){return this.view.byteLength-this.pos>=e}createExtraByteError(e){let{view:t,pos:n}=this;return RangeError(`Extra ${t.byteLength-n} of ${t.byteLength} byte(s) found at buffer[${e}]`)}decode(e){if(this.entered)return this.clone().decode(e);try{this.entered=!0,this.reinitializeState(),this.setBuffer(e);let t=this.doDecodeSync();if(this.hasRemaining(1))throw this.createExtraByteError(this.pos);return t}finally{this.entered=!1}}*decodeMulti(e){if(this.entered){yield*this.clone().decodeMulti(e);return}try{for(this.entered=!0,this.reinitializeState(),this.setBuffer(e);this.hasRemaining(1);)yield this.doDecodeSync()}finally{this.entered=!1}}async decodeAsync(e){if(this.entered)return this.clone().decodeAsync(e);try{this.entered=!0;let t=!1,n;for await(let r of e){if(t)throw this.entered=!1,this.createExtraByteError(this.totalPos);this.appendBuffer(r);try{n=this.doDecodeSync(),t=!0}catch(e){if(!(e instanceof RangeError))throw e}this.totalPos+=this.pos}if(t){if(this.hasRemaining(1))throw this.createExtraByteError(this.totalPos);return n}let{headByte:r,pos:i,totalPos:a}=this;throw RangeError(`Insufficient data in parsing ${yn(r)} at ${a} (${i} in the current buffer)`)}finally{this.entered=!1}}decodeArrayStream(e){return this.decodeMultiAsync(e,!0)}decodeStream(e){return this.decodeMultiAsync(e,!1)}async*decodeMultiAsync(e,t){if(this.entered){yield*this.clone().decodeMultiAsync(e,t);return}try{this.entered=!0;let n=t,r=-1;for await(let i of e){if(t&&r===0)throw this.createExtraByteError(this.totalPos);this.appendBuffer(i),n&&(r=this.readArraySize(),n=!1,this.complete());try{for(;yield this.doDecodeSync(),--r!==0;);}catch(e){if(!(e instanceof RangeError))throw e}this.totalPos+=this.pos}}finally{this.entered=!1}}doDecodeSync(){DECODE:for(;;){let e=this.readHeadByte(),t;if(e>=224)t=e-256;else if(e<192)if(e<128)t=e;else if(e<144){let n=e-128;if(n!==0){this.pushMapState(n),this.complete();continue DECODE}else t={}}else if(e<160){let n=e-144;if(n!==0){this.pushArrayState(n),this.complete();continue DECODE}else t=[]}else{let n=e-160;t=this.decodeString(n,0)}else if(e===192)t=null;else if(e===194)t=!1;else if(e===195)t=!0;else if(e===202)t=this.readF32();else if(e===203)t=this.readF64();else if(e===204)t=this.readU8();else if(e===205)t=this.readU16();else if(e===206)t=this.readU32();else if(e===207)t=this.useBigInt64?this.readU64AsBigInt():this.readU64();else if(e===208)t=this.readI8();else if(e===209)t=this.readI16();else if(e===210)t=this.readI32();else if(e===211)t=this.useBigInt64?this.readI64AsBigInt():this.readI64();else if(e===217){let e=this.lookU8();t=this.decodeString(e,1)}else if(e===218){let e=this.lookU16();t=this.decodeString(e,2)}else if(e===219){let e=this.lookU32();t=this.decodeString(e,4)}else if(e===220){let e=this.readU16();if(e!==0){this.pushArrayState(e),this.complete();continue DECODE}else t=[]}else if(e===221){let e=this.readU32();if(e!==0){this.pushArrayState(e),this.complete();continue DECODE}else t=[]}else if(e===222){let e=this.readU16();if(e!==0){this.pushMapState(e),this.complete();continue DECODE}else t={}}else if(e===223){let e=this.readU32();if(e!==0){this.pushMapState(e),this.complete();continue DECODE}else t={}}else if(e===196){let e=this.lookU8();t=this.decodeBinary(e,1)}else if(e===197){let e=this.lookU16();t=this.decodeBinary(e,2)}else if(e===198){let e=this.lookU32();t=this.decodeBinary(e,4)}else if(e===212)t=this.decodeExtension(1,0);else if(e===213)t=this.decodeExtension(2,0);else if(e===214)t=this.decodeExtension(4,0);else if(e===215)t=this.decodeExtension(8,0);else if(e===216)t=this.decodeExtension(16,0);else if(e===199){let e=this.lookU8();t=this.decodeExtension(e,1)}else if(e===200){let e=this.lookU16();t=this.decodeExtension(e,2)}else if(e===201){let e=this.lookU32();t=this.decodeExtension(e,4)}else throw new K(`Unrecognized type byte: ${yn(e)}`);this.complete();let n=this.stack;for(;n.length>0;){let e=n.top();if(e.type===Cn)if(e.array[e.position]=t,e.position++,e.position===e.size)t=e.array,n.release(e);else continue DECODE;else if(e.type===q){if(t===`__proto__`)throw new K(`The key __proto__ is not allowed`);e.key=this.mapKeyConverter(t),e.type=wn;continue DECODE}else if(e.map[e.key]=t,e.readCount++,e.readCount===e.size)t=e.map,n.release(e);else{e.key=null,e.type=q;continue DECODE}}return t}}readHeadByte(){return this.headByte===J&&(this.headByte=this.readU8()),this.headByte}complete(){this.headByte=J}readArraySize(){let e=this.readHeadByte();switch(e){case 220:return this.readU16();case 221:return this.readU32();default:if(e<160)return e-144;throw new K(`Unrecognized array type byte: ${yn(e)}`)}}pushMapState(e){if(e>this.maxMapLength)throw new K(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);this.stack.pushMapState(e)}pushArrayState(e){if(e>this.maxArrayLength)throw new K(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);this.stack.pushArrayState(e)}decodeString(e,t){return!this.rawStrings||this.stateIsMapKey()?this.decodeUtf8String(e,t):this.decodeBinary(e,t)}decodeUtf8String(e,t){if(e>this.maxStrLength)throw new K(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);if(this.bytes.byteLength<this.pos+t+e)throw kn;let n=this.pos+t,r;return r=this.stateIsMapKey()&&this.keyDecoder?.canBeCached(e)?this.keyDecoder.decode(this.bytes,n,e):rn(this.bytes,n,e),this.pos+=t+e,r}stateIsMapKey(){return this.stack.length>0?this.stack.top().type===q:!1}decodeBinary(e,t){if(e>this.maxBinLength)throw new K(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);if(!this.hasRemaining(e+t))throw kn;let n=this.pos+t,r=this.bytes.subarray(n,n+e);return this.pos+=t+e,r}decodeExtension(e,t){if(e>this.maxExtLength)throw new K(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);let n=this.view.getInt8(this.pos+t),r=this.decodeBinary(e,t+1);return this.extensionCodec.decode(r,n,this.context)}lookU8(){return this.view.getUint8(this.pos)}lookU16(){return this.view.getUint16(this.pos)}lookU32(){return this.view.getUint32(this.pos)}readU8(){let e=this.view.getUint8(this.pos);return this.pos++,e}readI8(){let e=this.view.getInt8(this.pos);return this.pos++,e}readU16(){let e=this.view.getUint16(this.pos);return this.pos+=2,e}readI16(){let e=this.view.getInt16(this.pos);return this.pos+=2,e}readU32(){let e=this.view.getUint32(this.pos);return this.pos+=4,e}readI32(){let e=this.view.getInt32(this.pos);return this.pos+=4,e}readU64(){let e=sn(this.view,this.pos);return this.pos+=8,e}readI64(){let e=on(this.view,this.pos);return this.pos+=8,e}readU64AsBigInt(){let e=this.view.getBigUint64(this.pos);return this.pos+=8,e}readI64AsBigInt(){let e=this.view.getBigInt64(this.pos);return this.pos+=8,e}readF32(){let e=this.view.getFloat32(this.pos);return this.pos+=4,e}readF64(){let e=this.view.getFloat64(this.pos);return this.pos+=8,e}};function Mn(e,t){return new jn(t).decode(e)}var Y=12,Nn=16;function Pn(e){let t=new Uint8Array(e),n=t.length>0&&t[0]&128?2:1;return t.length<n+Y+Nn?null:{kind:t[0]&-129,keyId:n===2?t[1]:null,iv:t.subarray(n,n+Y),ciphertext:t.subarray(n+Y),aad:t.subarray(0,n)}}async function Fn(e,t){let n=Pn(t);if(!n)return null;let r=typeof e==`function`?await e(n.keyId):e;if(!r)return null;let i=await We(r,n.iv,n.ciphertext,n.aad);return{kind:n.kind,plaintext:i}}function In(e,t,n,r){return Ln(e,t,n,r)}async function Ln(e,t,n,r,i){let a=r===void 0?new Uint8Array([t]):new Uint8Array([t|128,r&255]),o=i??globalThis.crypto.getRandomValues(new Uint8Array(Y)),s=new Uint8Array(await globalThis.crypto.subtle.encrypt({name:`AES-GCM`,iv:o,additionalData:a},e,n)),c=new Uint8Array(a.length+Y+s.length);return c.set(a),c.set(o,a.length),c.set(s,a.length+Y),c.buffer}var Rn=1,X=e=>e+7&-8;function zn(e){let t=e=>RangeError(`columns message: ${e}`);e.byteOffset%8!=0&&(e=e.slice());let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.byteLength<8)throw t(`short`);if(n.getUint8(0)!==Rn)throw t(`version ${n.getUint8(0)}`);let r=n.getUint16(2,!0),i=n.getUint32(4,!0),a=new TextDecoder,o=[],s=8;for(let i=0;i<r;i++){if(s+3>e.byteLength)throw t(`short channel table`);let r=n.getUint8(s),i=n.getUint8(s+1),c=n.getUint8(s+2);if(s+=3,r!==4&&r!==8)throw t(`value size ${r}`);if(s+i+c>e.byteLength)throw t(`short channel table`);o.push({name:a.decode(e.subarray(s,s+i)),unit:a.decode(e.subarray(s+i,s+i+c)),size:r}),s+=i+c}if(s=X(s),s+X(i*8)+o.reduce((e,t)=>e+X(i*t.size),0)>e.byteLength)throw t(`short data`);let c=e.byteOffset,l=new Float64Array(e.buffer,c+s,i);s+=X(i*8);let u={},d={};for(let t of o)u[t.name]=t.size===4?new Float32Array(e.buffer,c+s,i):new Float64Array(e.buffer,c+s,i),t.unit&&(d[t.name]=t.unit),s+=X(i*t.size);return{t:l,columns:u,units:d}}var Bn=2,Vn=1,Hn=2,Z=20,Un=64*1024*1024,Q=(e,t)=>RangeError(`${e}: ${t}`);function Wn(e){if(e.byteLength<8)throw Q(`block schema`,`short`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(t.getUint8(0)!==Bn)throw Q(`block schema`,`version ${t.getUint8(0)}`);let n=t.getUint16(2,!0),r=t.getUint32(4,!0),i=new TextDecoder(`utf-8`,{fatal:!0}),a=[],o=8;try{for(let r=0;r<n;r++){if(o+3>e.byteLength)throw Q(`block schema`,`short channel table`);let n=t.getUint8(o),r=t.getUint8(o+1),s=t.getUint8(o+2);if(o+=3,n!==4&&n!==8)throw Q(`block schema`,`value size ${n}`);if(o+r+s>e.byteLength)throw Q(`block schema`,`short channel table`);a.push({name:i.decode(e.subarray(o,o+r)),unit:i.decode(e.subarray(o+r,o+r+s)),size:n}),o+=r+s}}catch(e){throw e instanceof RangeError?e:Q(`block schema`,`invalid text`)}return{id:r,channels:a}}async function Gn(e,t){let n=new Blob([e]).stream().pipeThrough(new DecompressionStream(`deflate`)).getReader(),r=new Uint8Array(t),i=0;try{for(;;){let{done:e,value:a}=await n.read();if(e)break;if(i+a.length>t)throw Q(`block`,`body larger than its header says`);r.set(a,i),i+=a.length}}catch(e){throw await n.cancel().catch(()=>{}),e instanceof RangeError?e:Q(`block`,`body is not valid deflate`)}if(i!==t)throw Q(`block`,`body size mismatch`);return r}async function Kn(e,t){if(e.byteLength<Z)throw Q(`block`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==Bn)throw Q(`block`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint16(2,!0),a=n.getUint32(4,!0),o=t.get(n.getUint32(8,!0));if(!o)return null;if(o.channels.length!==i)throw Q(`block`,`channel count differs from its schema`);let s=n.getFloat64(12,!0),c=Math.max(0,a-1),l=4*c+o.channels.reduce((e,t)=>e+t.size*a,0);if(l>Un)throw Q(`block`,`too large`);let u;if(r&Vn)u=await Gn(e.subarray(Z),l);else if(u=e.subarray(Z),u.byteLength<l)throw Q(`block`,`body size mismatch`);let d=new DataView(u.buffer,u.byteOffset,u.byteLength),f=new Float64Array(a),p=0;a&&(f[0]=s);for(let e=1;e<a;e++)p+=d.getInt32(4*(e-1),!0),f[e]=s+p/1e6;let m=4*c,h={},g={};for(let e of o.channels){let t=new Uint8Array(a*e.size);for(let n=0;n<e.size;n++)for(let r=0;r<a;r++)t[r*e.size+n]=u[m+n*a+r];h[e.name]=e.size===4?new Float32Array(t.buffer):new Float64Array(t.buffer),e.unit&&(g[e.name]=e.unit),m+=a*e.size}return{block:{t:f,columns:h,units:g},historical:(r&Hn)!==0}}var qn=(e,t,n)=>`recording_key:${e}|${t}:${n}`,Jn=(e,t,n)=>n===e.stream_epoch?st(e.stream_id,t,n):qn(e.id,t,n),$=12,Yn=16;function Xn(e){let t=new Map;return async n=>{if(n.length===0)return null;let r=n[0]&128?2:1;if(n.length<r+$+Yn)return null;let i=r===2?n[1]:null,a=t.get(i),o=[...a?[a]:[],...e.filter(e=>e.keyId===i&&e.key!==a).map(e=>e.key)];for(let e of o)try{let a=await We(e,n.subarray(r,r+$),n.subarray(r+$),n.subarray(0,r));return t.set(i,e),{kind:n[0]&-129,plaintext:a}}catch{}return null}}var Zn=56,Qn=[75,82,67,49];function $n(e){let t=Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`}function er(e){if(e.length<56||Qn.some((t,n)=>e[n]!==t))throw RangeError(`not a KRC1 chunk`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint16(4,!0);if(n!==1)throw RangeError(`KRC version ${n}`);let r=t.getUint16(6,!0);if(r<56)throw RangeError(`KRC header too short`);return{recordingId:$n(e.subarray(8,24)),seq:t.getUint32(24,!0),count:t.getUint32(28,!0),bytes:t.getUint32(32,!0),headerBytes:r,t0:t.getFloat64(40,!0),t1:t.getFloat64(48,!0)}}function tr(e){let t=er(e),n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Math.min(e.length,t.bytes),i=[],a=t.headerBytes;for(;i.length<t.count&&a+12<=r;){let t=n.getUint32(a+8,!0);if(a+12+t>r)break;i.push({at:n.getFloat64(a,!0),bytes:e.subarray(a+12,a+12+t)}),a+=12+t}return{header:t,messages:i,truncated:i.length<t.count}}export{st as $,Ye as A,re as At,At as B,h as Bt,Bt as C,me as Ct,V as D,ge as Dt,ct as E,ue as Et,ut as F,l as Ft,xt as G,n as Gt,lt as H,y as Ht,Ke as I,u as It,Ct as J,Mt as K,a as Kt,pt as L,m as Lt,jt as M,k as Mt,Be as N,ne as Nt,Ve as O,le as Ot,Je as P,w as Pt,Ze as Q,Tt as R,_ as Rt,U as S,he as St,vt as T,A as Tt,St as U,D as Ut,ot as V,b as Vt,kt as W,r as Wt,mt as X,Et as Y,ht as Z,Lt as _,xe as _t,qn as a,bt as at,Rt as b,ve as bt,Wn as c,Fe as ct,In as d,Oe as dt,tt as et,Mn as f,je as ft,Pt as g,Ce as gt,Gt as h,we as ht,Xn as i,yt as it,$e as j,ce as jt,_t as k,se as kt,zn as l,Ne as lt,Zt as m,De as mt,tr as n,qe as nt,Jn as o,nt as ot,Kt as p,Me as pt,dt as q,er as r,gt as rt,Kn as s,Ie as st,Zn as t,it as tt,Fn as u,Te as ut,It as v,be as vt,zt as w,de as wt,Nt as x,_e as xt,Vt as y,ye as yt,Ot as z,v as zt};