@kymoscope/sdk 0.17.0 → 0.18.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.
- package/dist/assets/{importer.worker-BblHvxro.js → importer.worker-BEjFe0TV.js} +4 -4
- package/dist/assets/liveIngestWorker-MA_RO8uZ.js +1 -0
- package/dist/importers-4ETLHzYe.js +5 -0
- package/dist/keys-CbPoU_jx.js +11 -0
- package/dist/kymoscope-embed.js +61 -61
- package/dist/kymoscope-sdk.js +84 -84
- package/dist/types/lib/data/liveDataSource.d.ts +5 -0
- package/dist/types/lib/importers/types.d.ts +2 -0
- package/dist/types/lib/live/blockWire.d.ts +17 -7
- package/dist/types/lib/live/buffer.d.ts +14 -1
- package/dist/types/lib/live/liveIngest.d.ts +16 -0
- package/dist/types/lib/live/sparseFeed.d.ts +39 -0
- package/dist/types/lib/live/sparseStore.d.ts +18 -0
- package/dist/types/lib/live/wire.d.ts +2 -0
- package/package.json +1 -1
- package/dist/assets/liveIngestWorker-UV5D515-.js +0 -1
- package/dist/importers-CLmQyVrb.js +0 -5
- package/dist/keys-B-nvFeb4.js +0 -11
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e.startsWith(`eyJ`);function t(e){try{let t=e.split(`.`)[1]??``,n=atob(t.replace(/-/g,`+`).replace(/_/g,`/`)+`===`.slice((t.length+3)%4)),r=JSON.parse(n).exp;return typeof r==`number`?r:null}catch{return null}}function n(e,t){return`${e.replace(/\/+$/,``)}/v1/streams/${t}/watch`}function r(r,i,a,o){let s=null,c=null,l=null,u=!1,d=0,f=()=>{l&&=(clearTimeout(l),null)},p=(n,a)=>{f();let o=t(a);if(o===null)return;let c=e(a),d=c?Math.min(o,Date.now()/1e3+300):o,m=Math.min(Math.max((d-(c?90:30))*1e3-Date.now(),1e4),2**31-1);l=setTimeout(async()=>{l=null;let e=null;try{e=await r.getToken(i)}catch{e=null}if(!(u||s!==n||!e)){try{n.send(JSON.stringify({type:`auth`,token:e}))}catch{return}p(n,e)}},m)},m=(e=!1)=>{if(u||c)return;let t=e?0:Math.min(3e4,2e3*2**d++)*(.75+Math.random()/2);c=setTimeout(()=>{c=null,h()},t)},h=async()=>{if(u)return;let e=null;try{e=await r.getToken(i)}catch{e=null}if(u)return;if(!e){o?.(`error`),m();return}let t=new WebSocket(n(r.url,i),[`kymoscope-live.v1`,`kymoscope-live.ack1`,`kymoscope-live.ack2`,`bearer.${e}`]);t.binaryType=`arraybuffer`,s=t;let c=e,l=Date.now(),h=0,g=setInterval(()=>{if(!(h===0||t.readyState!==1)){try{t.send(JSON.stringify({type:`ack`,b:h}))}catch{return}h=0}},1e3);t.onopen=()=>{o?.(`subscribed`),p(t,c)},t.onmessage=e=>{if(d=0,typeof e.data!=`string`){h+=e.data.byteLength,a(e.data);return}if(e.data.startsWith(`{"type":"probe"`))try{let n=JSON.parse(e.data).id;typeof n==`string`&&n.length<=64&&t.send(JSON.stringify({type:`ack`,id:n}))}catch{}},t.onclose=()=>{clearInterval(g),s===t&&(s=null,f(),!u&&(o?.(`error`),m(Date.now()-l>=1e4)))}};return h(),()=>{if(u=!0,c&&=(clearTimeout(c),null),f(),s){let e=s;s=null,e.close(1e3)}}}new TextEncoder;function i(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>=4096&&(o+=String.fromCharCode(...a),a.length=0)}return a.length>0&&(o+=String.fromCharCode(...a)),o}const a=new TextDecoder;function o(e,t,n){let r=e.subarray(t,t+n);return a.decode(r)}function s(e,t,n){return n>200?o(e,t,n):i(e,t,n)}var c=class{type;data;constructor(e,t){this.type=e,this.data=t}},l=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 u(e,t,n){let r=Math.floor(n/4294967296),i=n;e.setUint32(t,r),e.setUint32(t+4,i)}function d(e,t){let n=e.getInt32(t),r=e.getUint32(t+4);return n*4294967296+r}function f(e,t){let n=e.getUint32(t),r=e.getUint32(t+4);return n*4294967296+r}function p({sec:e,nsec:t}){if(e>=0&&t>=0&&e<=17179869183)if(t===0&&e<=4294967295){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),u(r,4,e),n}}function m(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 h(e){return e instanceof Date?p(m(e)):null}function g(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:d(t,4),nsec:t.getUint32(0)};default:throw new l(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${e.length}`)}}function _(e){let t=g(e);return new Date(t.sec*1e3+t.nsec/1e6)}const v={type:-1,encode:h,decode:_};var y=class e{static defaultCodec=new e;__brand;builtInEncoders=[];builtInDecoders=[];encoders=[];decoders=[];constructor(){this.register(v)}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 c(-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 c(n,i)}}return e instanceof c?e:null}decode(e,t,n){let r=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return r?r(e,t,n):new c(t,e)}};function b(e){return e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer}function x(e){return e instanceof Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):b(e)?new Uint8Array(e):Uint8Array.from(e)}function S(e){return`${e<0?`-`:``}0x${Math.abs(e).toString(16).padStart(2,`0`)}`}var C=class{hit=0;miss=0;caches;maxKeyLength;maxLengthPerKey;constructor(e=16,t=16){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 a=i(e,t,n),o=Uint8Array.prototype.slice.call(e,t,t+n);return this.store(o,a),a}};const w=`array`,T=`map_key`,E=`map_value`,D=e=>{if(typeof e==`string`||typeof e==`number`)return e;throw new l(`The type of key must be string or number but `+typeof e)};var ee=class{stack=[];stackHeadPosition=-1;get length(){return this.stackHeadPosition+1}top(){return this.stack[this.stackHeadPosition]}pushArrayState(e){let t=this.getUninitializedStateFromPool();t.type=w,t.position=0,t.size=e,t.array=Array(e)}pushMapState(e){let t=this.getUninitializedStateFromPool();t.type=T,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===w){let t=e;t.size=0,t.array=void 0,t.position=0,t.type=void 0}if(e.type===T||e.type===E){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}};const O=new DataView(new ArrayBuffer(0)),k=new Uint8Array(O.buffer);try{O.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`)}const A=RangeError(`Insufficient data`),j=new C;var M=class e{extensionCodec;context;useBigInt64;rawStrings;maxStrLength;maxBinLength;maxArrayLength;maxMapLength;maxExtLength;keyDecoder;mapKeyConverter;totalPos=0;pos=0;view=O;bytes=k;headByte=-1;stack=new ee;entered=!1;constructor(e){this.extensionCodec=e?.extensionCodec??y.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?j:e.keyDecoder,this.mapKeyConverter=e?.mapKeyConverter??D}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=-1,this.stack.reset()}setBuffer(e){let t=x(e);this.bytes=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength),this.pos=0}appendBuffer(e){if(this.headByte===-1&&!this.hasRemaining(1))this.setBuffer(e);else{let t=this.bytes.subarray(this.pos),n=x(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 ${S(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 l(`Unrecognized type byte: ${S(e)}`);this.complete();let n=this.stack;for(;n.length>0;){let e=n.top();if(e.type===w)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===T){if(t===`__proto__`)throw new l(`The key __proto__ is not allowed`);e.key=this.mapKeyConverter(t),e.type=E;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=T;continue DECODE}}return t}}readHeadByte(){return this.headByte===-1&&(this.headByte=this.readU8()),this.headByte}complete(){this.headByte=-1}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 l(`Unrecognized array type byte: ${S(e)}`)}}pushMapState(e){if(e>this.maxMapLength)throw new l(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);this.stack.pushMapState(e)}pushArrayState(e){if(e>this.maxArrayLength)throw new l(`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 l(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);if(this.bytes.byteLength<this.pos+t+e)throw A;let n=this.pos+t,r;return r=this.stateIsMapKey()&&this.keyDecoder?.canBeCached(e)?this.keyDecoder.decode(this.bytes,n,e):s(this.bytes,n,e),this.pos+=t+e,r}stateIsMapKey(){return this.stack.length>0?this.stack.top().type===T:!1}decodeBinary(e,t){if(e>this.maxBinLength)throw new l(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);if(!this.hasRemaining(e+t))throw A;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 l(`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=f(this.view,this.pos);return this.pos+=8,e}readI64(){let e=d(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 N(e,t){return new M(t).decode(e)}function te(){let e=globalThis.crypto;if(!e?.subtle)throw Error(`WebCrypto SubtleCrypto is not available`);return e.subtle}async function ne(e,t,n,r){let i=await te().decrypt({name:`AES-GCM`,iv:t,additionalData:r},e,n);return new Uint8Array(i)}new TextEncoder().encode(`kymoscope-device-wrap-v1`),new TextEncoder().encode(`kymoscope-sender-bind-v1`);function re(e){let t=new Uint8Array(e),n=t.length>0&&t[0]&128?2:1;return t.length<n+12+16?null:{kind:t[0]&-129,keyId:n===2?t[1]:null,iv:t.subarray(n,n+12),ciphertext:t.subarray(n+12),aad:t.subarray(0,n)}}async function ie(e,t){let n=re(t);if(!n)return null;let r=typeof e==`function`?await e(n.keyId):e;if(!r)return null;let i=await ne(r,n.iv,n.ciphertext,n.aad);return{kind:n.kind,plaintext:i}}const P=e=>e+7&-8;function ae(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)!==1)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=P(s),s+P(i*8)+o.reduce((e,t)=>e+P(i*t.size),0)>e.byteLength)throw t(`short data`);let c=e.byteOffset,l=new Float64Array(e.buffer,c+s,i);s+=P(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+=P(i*t.size);return{t:l,columns:u,units:d}}function oe(e){if(e.length<24||e[0]!==1||e[2]!==1)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=(e[1]&1)!=0,r=t.getUint16(16,!0),i=t.getUint16(18,!0);if(i===0||r>=i)return null;let a=24,o=``;if(n&&r===0){if(e.length<a+1||e.length<a+1+e[a])return null;o=new TextDecoder().decode(e.subarray(a+1,a+1+e[a])),a+=1+e[a]}return{key:n,last:(e[1]&2)!=0,t:t.getFloat64(4,!0),seq:t.getUint32(12,!0),fragIndex:r,fragCount:i,width:t.getUint16(20,!0),height:t.getUint16(22,!0),codec:o,payload:e.subarray(a)}}const F=64*1024*1024,I=(e,t)=>RangeError(`${e}: ${t}`);function L(e,t){let n=new TextDecoder(`utf-8`,{fatal:!0}),r=[],i=0;try{for(let a=0;a<t;a++){if(i+3>e.byteLength)throw I(`block schema`,`short channel table`);let t=e[i],a=e[i+1],o=e[i+2];if(i+=3,t!==4&&t!==8)throw I(`block schema`,`value size ${t}`);if(i+a+o>e.byteLength)throw I(`block schema`,`short channel table`);r.push({name:n.decode(e.subarray(i,i+a)),unit:n.decode(e.subarray(i+a,i+a+o)),size:t}),i+=a+o}}catch(e){throw e instanceof RangeError?e:I(`block schema`,`invalid text`)}return r}async function R(e){if(e.byteLength<8)throw I(`block schema`,`short`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint8(0);if(n===3){if(e.byteLength<12)throw I(`block schema`,`short`);let n=t.getUint32(4,!0);if(n>1e5)throw I(`block schema`,`too many channels`);let r=t.getUint8(1)&1?await H(e.subarray(12),n*513,!1):e.subarray(12);return{id:t.getUint32(8,!0),channels:L(r,n)}}if(n!==2)throw I(`block schema`,`version ${n}`);return{id:t.getUint32(4,!0),channels:L(e.subarray(8),t.getUint16(2,!0))}}function z(e,t,n){let r=n.get(t.getUint32(4,!0));if(!r)return null;let i=t.getUint32(8,!0);if(i!==r.channels.length)throw I(`last values`,`channel count differs from its schema`);if(e.byteLength<12+16*i)throw I(`last values`,`short`);let a=[];for(let e=0;e<i;e++){let n=t.getFloat64(12+8*e,!0),o=t.getFloat64(12+8*(i+e),!0);Number.isFinite(n)&&Number.isFinite(o)&&a.push({name:r.channels[e].name,unit:r.channels[e].unit,t:n,v:o})}return a}async function B(e,t){if(e.byteLength<12)throw I(`last values`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=n.getUint8(0);if(r===1)return z(e,n,t);if(r!==2)throw I(`last values`,`version ${r}`);if(e.byteLength<20)throw I(`last values`,`short`);let i=n.getUint8(1),a=t.get(n.getUint32(4,!0));if(!a)return null;let o=n.getUint32(8,!0),s=n.getFloat64(12,!0),c=a.channels.length,l=(i&2)!=0;if(o>c||l&&o!==c)throw I(`last values`,`entry count differs from its schema`);let u=(l?0:4*o)+4*o,d=i&1?await H(e.subarray(20),u+8*o,!1):e.subarray(20);if(d.byteLength<u)throw I(`last values`,`short`);let f=new DataView(d.buffer,d.byteOffset,d.byteLength),p=[];for(let e=0;e<o;e++){let t=l?e:f.getUint32(4*e,!0);if(t>=c)throw I(`last values`,`channel outside its schema`);p.push(t)}let m=l?0:4*o,h=p.filter(e=>a.channels[e].size===4),g=p.filter(e=>a.channels[e].size===8);if(d.byteLength!==u+4*h.length+8*g.length)throw I(`last values`,`body size mismatch`);let _=V(d,u,h.length,4),v=V(d,u+4*h.length,g.length,8),y=new Map;h.forEach((e,t)=>y.set(e,_[t])),g.forEach((e,t)=>y.set(e,v[t]));let b=[];return p.forEach((e,t)=>{let n=s-f.getUint32(m+4*t,!0)/1e3,r=y.get(e);Number.isFinite(n)&&Number.isFinite(r)&&b.push({name:a.channels[e].name,unit:a.channels[e].unit,t:n,v:r})}),b}function V(e,t,n,r){let i=new Uint8Array(n*r);for(let a=0;a<r;a++)for(let o=0;o<n;o++)i[o*r+a]=e[t+a*n+o];return r===4?new Float32Array(i.buffer):new Float64Array(i.buffer)}async function H(e,t,n=!0){let r=new Blob([e]).stream().pipeThrough(new DecompressionStream(`deflate`)).getReader(),i=new Uint8Array(Math.min(t,e.byteLength*4+1024)),a=0;try{for(;;){let{done:e,value:n}=await r.read();if(e)break;if(a+n.length>t)throw I(`block`,`body larger than its header says`);if(a+n.length>i.length){let e=new Uint8Array(Math.min(t,Math.max(2*i.length,a+n.length)));e.set(i.subarray(0,a)),i=e}i.set(n,a),a+=n.length}}catch(e){throw await r.cancel().catch(()=>{}),e instanceof RangeError?e:I(`block`,`body is not valid deflate`)}if(n&&a!==t)throw I(`block`,`body size mismatch`);return i.length===a?i:i.subarray(0,a)}async function U(e,t){if(e.byteLength<20)throw I(`block`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==2)throw I(`block`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint16(2,!0),a=n.getUint32(4,!0),o=n.getUint32(8,!0),s=t.get(o);if(!s)return null;if(s.channels.length!==i)throw I(`block`,`channel count differs from its schema`);let c=n.getFloat64(12,!0),l=Math.max(0,a-1),u=4*l+s.channels.reduce((e,t)=>e+t.size*a,0);if(u>F)throw I(`block`,`too large`);let d;if(r&1)d=await H(e.subarray(20),u);else if(d=e.subarray(20),d.byteLength<u)throw I(`block`,`body size mismatch`);let f=new DataView(d.buffer,d.byteOffset,d.byteLength),p=new Float64Array(a),m=0;a&&(p[0]=c);for(let e=1;e<a;e++)m+=f.getInt32(4*(e-1),!0),p[e]=c+m/1e6;let h=4*l,g={},_={};for(let e of s.channels)g[e.name]=V(d,h,a,e.size),e.unit&&(_[e.name]=e.unit),h+=a*e.size;return{block:{t:p,columns:g,units:_},historical:(r&2)!=0,schemaId:o}}async function W(e,t){if(e.byteLength<20)throw I(`sparse`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==1)throw I(`sparse`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint32(4,!0),a=t.get(i);if(!a)return null;let o=n.getUint32(8,!0),s=n.getFloat64(12,!0),c=4*o+4*Math.max(0,o-1);if(c+8*o>F)throw I(`sparse`,`too large`);let l=r&1?await H(e.subarray(20),c+8*o,!1):e.subarray(20);if(l.byteLength<c)throw I(`sparse`,`body size mismatch`);let u=new Uint32Array(o);for(let e=0;e<o;e++)u[e]=(l[e]|l[o+e]<<8|l[2*o+e]<<16|l[3*o+e]<<24)>>>0;let d=a.channels.length,f=0;for(let e=0;e<o;e++){if(u[e]>=d)throw I(`sparse`,`event outside its schema`);a.channels[u[e]].size===4&&f++}if(l.byteLength!==c+4*f+8*(o-f))throw I(`sparse`,`body size mismatch`);let p=new DataView(l.buffer,l.byteOffset,l.byteLength),m=new Float64Array(o),h=0;o&&(m[0]=s);for(let e=1;e<o;e++)h+=p.getInt32(4*o+4*(e-1),!0),m[e]=s+h/1e6;let g=V(l,c,f,4),_=V(l,c+4*f,o-f,8),v=new Float64Array(o);for(let e=0,t=0,n=0;e<o;e++)v[e]=a.channels[u[e]].size===4?g[t++]:_[n++];return{schemaId:i,index:u,t:m,v,historical:(r&2)!=0}}const G=e=>Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``),K=e=>{let t=G(e);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`};function se(e){if(e.length!==70||e[0]!==1)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return{streamId:K(e.subarray(1,17)),run:G(e.subarray(17,25)),lane:e[25],seq:t.getBigUint64(26,!0),count:t.getUint32(34,!0),hash:e.slice(38,70)}}const ce=()=>globalThis.crypto.subtle,q=e=>ce().digest(`SHA-256`,e);async function le(e){let t=await Promise.all(e),n=new Uint8Array(t.length*32);return t.forEach((e,t)=>n.set(new Uint8Array(e),t*32)),new Uint8Array(await q(n))}function ue(e,t,n=()=>Date.now()){let r={verified:0,broken:0},i=null,a=0n,o=null,s=0,c=0,l=0,u=Promise.resolve(),d=e=>{e?r.verified++:(r.broken++,r.lastBrokenAt=n()),t({...r})};return{integrity:r,reset(){i=null,o=null,c=s,l++},arrive(t,n,r){if(n!==8){o&&(o.push(q(t)),s++);return}let f=s;o||(o=[],c=s);let p=l;u=u.then(async()=>{let t=await r.catch(()=>null);if(p!==l)return;let n=t?se(t.plaintext):null;if(!n||n.lane!==0||n.streamId!==e.toLowerCase()){i!==null&&d(!1);return}let s=f-c;if(i===null||n.run!==i){i=n.run,a=n.seq,o=o.slice(Math.max(0,s)),c=Math.max(c,f);return}let u=n.seq===a+1n&&n.count<=s&&de(await le(o.slice(0,n.count)),n.hash);if(p!==l)return;a=n.seq;let m=u?n.count:s;o=o.slice(m),c+=m,d(u)})}}}const de=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]);function fe(e,t,n){let r=new Map,i=new Set,a=null,o=e=>{i.add(e.id),a=e.id,t({type:`schema`,id:e.id,names:e.channels.map(e=>e.name),units:e.channels.map(e=>e.unit)})},s=Promise.resolve(),c=n?ue(n,e=>t({type:`integrity`,integrity:e})):null;return Object.assign(n=>{let l=ie(e,n).catch(()=>null);c?.arrive(n,new Uint8Array(n,0,1)[0]&127,l),s=s.then(async()=>{try{let e=await l;if(!e)return;if(e.kind===6){let n=oe(e.plaintext);n&&t({type:`video`,m:n})}else if(e.kind===5){let t=await R(e.plaintext);if(r.delete(t.id),r.set(t.id,t),r.size>8){let e=r.keys().next().value;r.delete(e),i.delete(e)}(t.channels.length>1e3||a!==null)&&t.id!==a&&o(t)}else if(e.kind===7){let n=await B(e.plaintext,r);n&&t({type:`last`,values:n})}else if(e.kind===4){let n=await U(e.plaintext,r);n&&t(n.historical?{type:`columns`,block:n.block,historical:!0}:{type:`columns`,block:n.block})}else if(e.kind===9){let n=await W(e.plaintext,r);if(!n||!n.index.length)return;i.has(n.schemaId)||o(r.get(n.schemaId)),t({type:`sparse`,...n})}else if(e.kind===3)t({type:`columns`,block:ae(e.plaintext)});else if(e.kind===2){let n=N(e.plaintext);t({type:`frames`,frames:Array.isArray(n?.frames)?n.frames:[]})}}catch{}})},{reset:()=>c?.reset()})}function J(e){if(e.type!==`columns`&&e.type!==`sparse`)return[];let t=new Set,n=e=>{ArrayBuffer.isView(e)&&Object.prototype.toString.call(e.buffer)===`[object ArrayBuffer]`&&t.add(e.buffer)};if(e.type===`sparse`)return n(e.index),n(e.t),n(e.v),[...t];n(e.block.t);for(let t of Object.values(e.block.columns))n(t);return[...t]}function pe(e,t=67108864){let n=0,r=0,i=0;return{emit(a){let o=J(a).reduce((e,t)=>e+t.byteLength,0),s=a.type===`columns`?a.block.t.length:a.type===`sparse`?a.t.length:-1;if(s>=0&&n>0&&n+o>t){r+=s,i+=s;return}s>=0&&i>0&&(e({type:`gap`,samples:i},0),i=0),n+=o,e(a,o)},ack(e){n=Math.max(0,n-e)},get dropped(){return r}}}let Y=1;const X=new Map,Z=(e,t)=>new Promise(n=>{let r=Y++;X.set(r,n),self.postMessage({type:`ask`,id:r,what:e,arg:t})});let Q=null;const $=pe((e,t)=>self.postMessage({type:`decoded`,d:e,bytes:t},J(e)));self.onmessage=e=>{let t=e.data;if(t.type===`reply`){X.get(t.id)?.(t.value),X.delete(t.id);return}if(t.type===`ack`){$.ack(t.bytes);return}if(t.type===`stop`){Q?.(),Q=null,self.close();return}let n=new Map,i=fe(t.key??(async e=>{let t=n.get(e);if(t)return t;let r=await Z(`key`,e);return r&&n.set(e,r),r}),$.emit,t.streamId);Q=r({url:t.url,getToken:()=>Z(`token`)},t.streamId,i,e=>{e===`subscribed`&&i.reset(),self.postMessage({type:`status`,status:e})})};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/*! @kymoscope/sdk (c) 2026 Kymoscope. Proprietary software: use requires a license. See LICENSE.md. */
|
|
2
|
+
import{$t as e,Bt as t,C as n,D as r,E as i,Gt as a,Ht as o,Jt as s,Kt as c,M as l,Mt as u,N as d,Nt as f,Pt as p,Qt as m,T as h,Ut as g,Vt as _,Wt as v,Xt as y,Yt as b,Zt as x,a as S,c as C,i as ee,l as te,qt as w,t as ne,u as re,w as ie,x as ae,zt as oe}from"./keys-CbPoU_jx.js";function T(e,t){return e.selection.find(e=>e.group===t)?.channels??[]}var se=4096,ce={sin:Math.sin,cos:Math.cos,tan:Math.tan,asin:Math.asin,acos:Math.acos,atan:Math.atan,sinh:Math.sinh,cosh:Math.cosh,tanh:Math.tanh,exp:Math.exp,ln:Math.log,log:Math.log,log10:Math.log10,sqrt:Math.sqrt,abs:Math.abs,pow:Math.pow,min:Math.min,max:Math.max};function le(e){if(e.length>se)return null;let t=e.match(/\d+\.?\d*(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?|[A-Za-z_]\w*|\*\*|[-+*/^(),]|\S/g)??[],n=0,r=()=>t[n],i=()=>{throw Error(`formula`)},a=e=>{t[n++]!==e&&i()},o=()=>{let e=s();for(;r()===`+`||r()===`-`;){let r=t[n++],i=e,a=s();e=r===`+`?e=>i(e)+a(e):e=>i(e)-a(e)}return e},s=()=>{let e=c();for(;r()===`*`||r()===`/`;){let r=t[n++],i=e,a=c();e=r===`*`?e=>i(e)*a(e):e=>i(e)/a(e)}return e},c=()=>{if(r()===`-`){n++;let e=c();return t=>-e(t)}return r()===`+`?(n++,c()):l()},l=()=>{let e=u();if(r()===`^`||r()===`**`){n++;let t=c();return n=>e(n)**t(n)}return e},u=()=>{let e=t[n++];if(e===void 0&&i(),e===`(`){let e=o();return a(`)`),e}if(/^[\d.]/.test(e)){let t=Number(e);return Number.isFinite(t)||i(),()=>t}if(e===`X`||e===`x`||e===`X1`)return e=>e;if(Object.hasOwn(ce,e)){let t=ce[e];a(`(`);let i=[o()];for(;r()===`,`;)n++,i.push(o());return a(`)`),i.length===1?e=>t(i[0](e)):e=>t(...i.map(t=>t(e)))}return i()};try{let e=o();return n===t.length?e:null}catch{return null}}var E={k:`id`};function ue(e,t){let{keys:n,vals:r}=e,i=n.length;if(i===0)return t;if(t<=n[0])return r[0];if(t>=n[i-1])return r[i-1];let a=0,o=i-1;for(;o-a>1;){let e=a+o>>1;n[e]<=t?a=e:o=e}return e.interp?r[a]+(r[o]-r[a])*(t-n[a])/(n[o]-n[a]):t-n[a]<=n[o]-t?r[a]:r[o]}function D(e,t){let n=t.length;switch(e.k){case`lin`:{let{a:r,b:i}=e;if(r===1&&i===0)return;for(let e=0;e<n;e++)t[e]=r*t[e]+i;return}case`rat`:{let[r,i,a,o,s,c]=e.p;for(let e=0;e<n;e++){let n=t[e];t[e]=(r*n*n+i*n+a)/(o*n*n+s*n+c)}return}case`poly`:{let[r,i,a,o,s,c]=e.p;for(let e=0;e<n;e++){let n=t[e]-s-c;t[e]=(i-o*n)/(a*n-r)}return}case`alg`:{let r=e.f;for(let e=0;e<n;e++)t[e]=r(t[e]);return}case`tab`:for(let r=0;r<n;r++)t[r]=ue(e,t[r]);return;case`rtv`:for(let r=0;r<n;r++){let n=t[r],i=e.def;for(let t=0;t<e.lo.length;t++)if(n>=e.lo[t]&&n<=e.hi[t]){i=e.vals[t];break}t[r]=i}return;default:return}}function de(e,t){let n=Float64Array.of(t);return D(e,n),n[0]}var fe=(e,t)=>e===null||typeof e==`string`?e:String(de(e,t));function pe(e,t){let n=Array(t.length),r=e.ranges.every(e=>e.lo===e.hi)?new Map(e.ranges.map(e=>[e.lo,e.to])):null;for(let i=0;i<t.length;i++){let a=t[i];if(a!==a){n[i]=null;continue}let o=e.def;if(r){let e=r.get(a);e!==void 0&&(o=e)}else for(let t of e.ranges)if(a>=t.lo&&(e.int||t.lo===t.hi?a<=t.hi:a<t.hi)){o=t.to;break}n[i]=fe(o,a)}return n}function me(e,t){let n=(e,t)=>({type:e,bigEndian:t,encoding:`latin1`});switch(e){case 0:return n(`uint`,!t);case 1:return n(`int`,!t);case 2:case 3:return n(`float`,!t);case 7:return n(`string`,!1);case 8:return n(`bytes`,!1);case 9:return n(`uint`,!0);case 10:return n(`int`,!0);case 11:case 12:return n(`float`,!0);case 13:return n(`uint`,!1);case 14:return n(`int`,!1);case 15:case 16:return n(`float`,!1);default:return null}}async function he(t,n,r){let i=e(n).getUint16(24,!0)===0,a=async n=>{let r=e(await t.read(n,4)).getUint16(2,i),a=await t.read(n,r);return{id:w(a.subarray(0,2)),v:e(a),b:a,size:r}},o=(e,t)=>e.getUint32(t,i),c=(e,t)=>e.getUint16(t,i),l=(e,t)=>e.getFloat64(t,i),u=async e=>e?s((await a(e)).b.subarray(4),!0):``,d=[],f=new Set,p=e=>{if(f.has(e))throw Error(`This MDF file is damaged (its blocks link in a loop).`);f.add(e)},m=async(e,t)=>{if(!e)return{conv:E,unit:``};let{v:n,b:r}=await a(e),i=s(r.subarray(22,42),!0),d=c(n,42),f=c(n,44),p=e=>l(n,46+8*e);switch(d){case 0:return{conv:{k:`lin`,a:p(1),b:p(0)},unit:i};case 1:case 2:{let e=new Float64Array(f),t=new Float64Array(f);for(let n=0;n<f;n++)e[n]=p(2*n),t[n]=p(2*n+1);return{conv:{k:`tab`,keys:e,vals:t,interp:d===1},unit:i}}case 6:return{conv:{k:`poly`,p:[0,1,2,3,4,5].map(p)},unit:i};case 9:return{conv:{k:`rat`,p:[0,1,2,3,4,5].map(p)},unit:i};case 10:{let e=s(r.subarray(46,302),!0),t=le(e);return{conv:t?{k:`alg`,f:t}:{k:`unsupported`,what:`the formula "${e}"`},unit:i}}case 11:return{conv:{k:`text`,ranges:Array.from({length:f},(e,t)=>{let i=46+40*t,a=l(n,i);return{lo:a,hi:a,to:s(r.subarray(i+8,i+40),!0)}}),def:null,int:t},unit:i};case 12:{let e=await Promise.all(Array.from({length:f},async(e,t)=>{let r=46+20*t;return{lo:l(n,r),hi:l(n,r+8),to:await u(o(n,r+16))}}));return{conv:{k:`text`,ranges:e.slice(1),def:e[0]?.to??null,int:t},unit:i}}case 65535:return{conv:E,unit:i};default:return{conv:{k:`unsupported`,what:`MDF3 conversion type ${d}`},unit:i}}},h=await a(64),g=null;if(h.size>=172&&(o(h.v,164)||o(h.v,168))){let e=o(h.v,168)*4294967296+o(h.v,164);g=new Date(e/1e6).toISOString()}else{let e=w(h.b.subarray(18,28)),t=w(h.b.subarray(28,36));/^\d\d:\d\d:\d{4}$/.test(e)&&(g=`${e.slice(6)}-${e.slice(3,5)}-${e.slice(0,2)}T${t}`)}let _=[],v=0;for(let e=o(h.v,4),t=0;e;t++){p(e);let n=await a(e),r=c(n.v,22),l=[];for(let e=o(n.v,8);e;){p(e);let n=await a(e),f=[],h=null;for(let e=o(n.v,8);e;){p(e);let t=await a(e),n=s(t.b.subarray(26,58),!0),r=(t.size>=222?await u(o(t.v,218)):``)||n,l=me(c(t.v,190),i),g=c(t.v,186)+(t.size>=228?8*c(t.v,226):0);if(!l)d.push(`${r}: MDF3 data type ${c(t.v,190)} is not supported; skipped.`);else{let{conv:e,unit:n}=await m(o(t.v,8),l.type!==`float`),i={name:r,unit:n,kind:c(t.v,24)===1?`master`:`value`,...l,conv:e,sd:null,sdRecordId:null,byteOffset:g>>3,bitOffset:g&7,bitCount:c(t.v,188),invalBit:null,allInvalid:!1};i.kind===`master`&&!h?h=i:i.kind===`value`&&f.push(i)}e=o(t.v,4)}v++,l.push({name:await u(o(n.v,12))||`Group ${v}`,dg:t,recordId:c(n.v,16),recordIdSize:+(r>0),idTrailer:+(r===2),dataBytes:c(n.v,20),invalBytes:0,cycles:o(n.v,22),vlsdCg:!1,sorted:!0,master:h,channels:f,stream:[],busType:0,busEvent:!1}),e=o(n.v,4)}let f=l.reduce((e,t)=>e+t.cycles*(t.recordIdSize+t.dataBytes+t.idTrailer),0),h=[{offset:o(n.v,16),length:f,zip:null}];for(let e of l)e.stream=h,e.sorted=l.length===1,_.push(e);e=o(n.v,4)}return{version:`MDF ${r}`,startTime:g,groups:_,notes:d,meta:{}}}var ge=`This MF4 is damaged (its blocks link in a loop).`;function O(e,t){if(e.seen.has(t))throw Error(ge);e.seen.add(t)}async function k(t,n){let r=await t.read(n,24);if(r.length<24)throw Error(`This MF4 is damaged (bad block at byte ${n}).`);let i=e(r),a=w(r.subarray(0,4)),o=x(i,8),s=x(i,16);if(!a.startsWith(`##`)||o<24+8*s||o>1<<26)throw Error(`This MF4 is damaged (bad block at byte ${n}).`);let c=await t.read(n,o);if(c.length<o)throw Error(`This MF4 is damaged (the block at byte ${n} runs past the end of the file).`);let l=e(c),u=[];for(let e=0;e<s;e++)u.push(x(l,24+8*e));let d=c.subarray(24+8*s);return{id:a,links:u,data:e(d),bytes:d}}var _e={lt:`<`,gt:`>`,amp:`&`,quot:`"`,apos:`'`};function ve(e){let t=e.indexOf(`<TX>`),n=t<0?-1:e.indexOf(`</TX>`,t+4);return(n>=0?e.slice(t+4,n):e.replace(/<[^<>]*>/g,``)).replace(/&(lt|gt|amp|quot|apos);/g,(e,t)=>_e[t]).trim()}function A(e,t){if(!t)return Promise.resolve(``);let n=e.texts.get(t);return n||(n=k(e.io,t).then(e=>{let t=s(e.bytes);return e.id===`##MD`?ve(t):t}),e.texts.set(t,n)),n}async function j(t,n,r=0){if(!n)return[];let i=t.io,a=await i.read(n,24),o=w(a.subarray(0,4)),s=a.length<24?0:x(e(a),8);switch(o){case`##DT`:case`##SD`:case`##RD`:return[{offset:n+24,length:s-24,zip:null}];case`##DZ`:{let t=e(await i.read(n+24,24));if(t.byteLength<24)throw Error(`This MF4 is damaged (bad block at byte ${n}).`);let r=t.getUint8(2);if(r===2||r===3)throw Error(`This MF4 uses zstd compression, which isn't supported yet.`);if(r===4||r===5)throw Error(`This MF4 uses LZ4 compression, which isn't supported yet.`);if(r>5)throw Error(`This MF4 is damaged (unknown compression type ${r}).`);let a=t.getUint32(4,!0),o=x(t,8),s=x(t,16);if(o>oe(s)||r===1&&a===0)throw Error(`This MF4 is damaged (bad compressed block at byte ${n}).`);if(r===1&&o>268435456)throw Error(`This MF4 has a compressed block over 256 MB, which Kymoscope cannot read.`);return[{offset:n+48,length:s,zip:{type:r,param:a,orgLength:o}}]}case`##DL`:{if(r>1)throw Error(`This MF4 is damaged (unexpected ${o} block in its data).`);let e=[];for(let r=n;r;){O(t,r);let n=await k(i,r);for(let r of n.links.slice(1))e.push(...await j(t,r,2));r=n.links[0]}return e}case`##HL`:if(r>0)throw Error(`This MF4 is damaged (unexpected ${o} block in its data).`);return O(t,n),j(t,(await k(i,n)).links[0],1);case`##DV`:case`##DI`:throw Error(`This MF4 uses column-oriented storage (MDF 4.2 DV/DI blocks), which Kymoscope cannot read yet.`);default:throw Error(`This MF4 is damaged (unexpected ${o} block in its data).`)}}async function ye(e,t,n){return t?w(await e.io.read(t,4))===`##CC`?be(e,t,n+1):A(e,t):null}async function be(e,t,n=0){if(!t||n>4||e.busy.has(t))return E;let r=e.convs.get(t);if(r)return r;e.busy.add(t);try{let r=await xe(e,t,n);return e.convs.set(t,r),r}finally{e.busy.delete(t)}}async function xe(e,t,n){let r=await k(e.io,t),i=r.data,a=i.getUint8(0),o=i.getUint16(4,!0),s=i.getUint16(6,!0),c=new Float64Array(s);for(let e=0;e<s;e++)c[e]=i.getFloat64(24+8*e,!0);let l=r.links.slice(4,4+o),u=()=>Promise.all(l.map(t=>ye(e,t,n)));switch(a){case 0:return E;case 1:return{k:`lin`,b:c[0],a:c[1]};case 2:return{k:`rat`,p:[...c]};case 3:{let t=await A(e,l[0]),n=le(t);return n?{k:`alg`,f:n}:{k:`unsupported`,what:`the formula "${t}"`}}case 4:case 5:{let e=s>>1,t=new Float64Array(e),n=new Float64Array(e);for(let r=0;r<e;r++)t[r]=c[2*r],n[r]=c[2*r+1];return{k:`tab`,keys:t,vals:n,interp:a===4}}case 6:{let e=Math.max(0,Math.floor((s-1)/3)),t=new Float64Array(e),n=new Float64Array(e),r=new Float64Array(e);for(let i=0;i<e;i++)t[i]=c[3*i],n[i]=c[3*i+1],r[i]=c[3*i+2];return{k:`rtv`,lo:t,hi:n,vals:r,def:c[s-1]}}case 7:{let e=await u();return{k:`text`,ranges:[...c].map((t,n)=>({lo:t,hi:t,to:e[n]??null})),def:e[s]??null,int:!0}}case 8:{let e=await u(),t=s>>1;return{k:`text`,ranges:Array.from({length:t},(t,n)=>({lo:c[2*n],hi:c[2*n+1],to:e[n]??null})),def:e[t]??null,int:!0}}default:return{k:`unsupported`,what:`conversion type ${a}`}}}var Se={0:{type:`uint`,bigEndian:!1,encoding:`latin1`},1:{type:`uint`,bigEndian:!0,encoding:`latin1`},2:{type:`int`,bigEndian:!1,encoding:`latin1`},3:{type:`int`,bigEndian:!0,encoding:`latin1`},4:{type:`float`,bigEndian:!1,encoding:`latin1`},5:{type:`float`,bigEndian:!0,encoding:`latin1`},6:{type:`string`,bigEndian:!1,encoding:`latin1`},7:{type:`string`,bigEndian:!1,encoding:`utf-8`},8:{type:`string`,bigEndian:!1,encoding:`utf-16le`},9:{type:`string`,bigEndian:!0,encoding:`utf-16be`},10:{type:`bytes`,bigEndian:!1,encoding:`latin1`}};async function Ce(e,t){let{io:n,notes:r}=e,i=t.data,a=i.getUint8(0),o=i.getUint8(1),s=i.getUint8(2),c=await A(e,t.links[2]),l=Se[s];if(!l)return r.push(`${c}: data type ${s} is not supported; the channel was skipped.`),null;let u;if(a===2||a===3)if(o===1)u=a===2?`master`:`vmaster`;else{if(r.push(`The master channel ${c} is not time (sync type ${o}): samples are numbered instead.`),a===3)return null;u=`value`}else if(a===1)u=`vlsd`;else if(a===0||a===4)u=`value`;else return r.push(`${c}: channel type ${a} is not supported; skipped.`),null;let d=await be(e,t.links[4]),f=d.k===`text`?{...d,int:l.type!==`float`}:d,p=await A(e,t.links[6]);!p&&t.links[4]&&(p=await A(e,(await k(n,t.links[4])).links[1]));let m=null,h=null;u===`vlsd`&&t.links[5]&&(w(await n.read(t.links[5],4))===`##CG`?h=x((await k(n,t.links[5])).data,0):m=await j(e,t.links[5]));let g=i.getUint32(12,!0);return{name:c,unit:p,kind:u,...l,conv:f,sd:m,sdRecordId:h,byteOffset:i.getUint32(4,!0),bitOffset:i.getUint8(3),bitCount:i.getUint32(8,!0),invalBit:g&2?i.getUint32(16,!0):null,allInvalid:(g&1)!=0}}async function we(e,t,n,r=0){let{io:i,notes:a}=e;for(let o=t;o;){O(e,o);let t=await k(i,o),s=t.links[1];s&&r<4&&(w(await i.read(s,4))===`##CN`?await we(e,s,n,r+1):a.push(`${await A(e,t.links[2])}: array channels (CA) are not supported yet; skipped.`));let c=await Ce(e,t);c&&n.push(c),o=t.links[0]}}async function Te(e,t,n,r){let i=t.data,a=i.getUint16(16,!0),o=0;t.links[3]&&(o=(await k(e.io,t.links[3])).data.getUint8(1));let s={name:await A(e,t.links[2]),dg:n,recordId:x(i,0),recordIdSize:r,idTrailer:0,dataBytes:i.getUint32(24,!0),invalBytes:i.getUint32(28,!0),cycles:x(i,8),vlsdCg:(a&1)!=0,sorted:!0,master:null,channels:[],stream:[],busType:o,busEvent:(a&2)!=0};if(s.vlsdCg)return s;let c=[];await we(e,t.links[1],c);for(let e of c)e.kind===`master`||e.kind===`vmaster`?s.master||=e:s.channels.push(e);return s}async function Ee(e,t){let n=await k(e,64);if(n.id!==`##HD`)throw Error(`This MF4 is damaged (no header block).`);let r=x(n.data,0),i=(n.data.getUint8(12)&1)!=0,a=new Date(r/1e6).toISOString(),o=r>0?i?a.slice(0,-1):a:null,s={io:e,notes:[],seen:new Set,texts:new Map,convs:new Map,busy:new Set},c=[],l=0;for(let t=n.links[0],r=0;t;r++){O(s,t);let n=await k(e,t),i=n.data.getUint8(0),a=[];for(let t=n.links[1];t;){O(s,t);let n=await k(e,t),o=await Te(s,n,r,i);o.vlsdCg||(l++,o.name||=`Group ${l}`),a.push(o),t=n.links[0]}let o=await j(s,n.links[2]),u=a.length===1;for(let e of a)e.stream=o,e.sorted=u,c.push(e);t=n.links[0]}return{version:`MF4 ${t}`,startTime:o,groups:c,notes:s.notes,meta:{}}}function De(e){let t=e&32768?-1:1,n=e>>10&31,r=e&1023;return n===0?t*2**-14*(r/1024):n===31?r?NaN:t*(1/0):t*2**(n-15)*(1+r/1024)}function M(t,n,r,i,a,o,s=0){let c=e(n),l=!t.bigEndian,u=a+t.byteOffset,d=t.bitCount;if(t.bitOffset===0)if(t.type===`float`){if(d===64){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getFloat64(u,l);return}if(d===32){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getFloat32(u,l);return}if(d===16){for(let e=0;e<r;e++,u+=i)o[s+e]=De(c.getUint16(u,l));return}}else if(t.type===`int`){if(d===8){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getInt8(u);return}if(d===16){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getInt16(u,l);return}if(d===32){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getInt32(u,l);return}if(d===64){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getInt32(u+(l?4:0),l)*4294967296+c.getUint32(u+(l?0:4),l);return}}else{if(d===8){for(let e=0;e<r;e++,u+=i)o[s+e]=n[u];return}if(d===16){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getUint16(u,l);return}if(d===32){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getUint32(u,l);return}if(d===64){for(let e=0;e<r;e++,u+=i)o[s+e]=c.getUint32(u+(l?4:0),l)*4294967296+c.getUint32(u+(l?0:4),l);return}}let f=t.bitOffset+d+7>>3,p=2**t.bitOffset,m=2**d,h=m/2,g=t.type===`int`;for(let e=0;e<r;e++,u+=i){let t=0;if(l)for(let e=f-1;e>=0;e--)t=t*256+n[u+e];else for(let e=0;e<f;e++)t=t*256+n[u+e];t=Math.floor(t/p)%m,o[s+e]=g&&t>=h?t-m:t}}var Oe=new Map,ke=e=>{let t=Oe.get(e);return t||Oe.set(e,t=new TextDecoder(e)),t};function Ae(e){let t=e.indexOf(`\0`);return t<0?e:e.slice(0,t)}function je(e,t,n,r,i){let a=ke(e.encoding),o=e.bitCount>>3,s=Array(n);for(let c=0,l=i+e.byteOffset;c<n;c++,l+=r)s[c]=Ae(a.decode(t.subarray(l,l+o)));return s}function Me(e,t,n,r,i,a){let o=a instanceof Float64Array?NaN:null;if(e.allInvalid){for(let e=0;e<n;e++)a[e]=o;return}if(e.invalBit===null)return;let s=1<<(e.invalBit&7);for(let c=0,l=i+(e.invalBit>>3);c<n;c++,l+=r)t[l]&s&&(a[c]=o)}var N=e=>e.recordIdSize+e.dataBytes+e.invalBytes+e.idTrailer,Ne=64<<20,Pe=(e,t)=>e.reduce((e,n)=>e+(n.zip?n.zip.orgLength:Math.max(0,Math.min(n.length,t-n.offset))),0),Fe=e=>e.byteOffset+Math.ceil((e.bitOffset+e.bitCount)/8);function Ie(e,t){return e.kind===`vmaster`?!0:(e.kind===`vlsd`||e.type===`uint`||e.type===`int`||e.type===`float`?!(e.bitCount>=1&&e.bitCount<=64):!(e.bitCount>=8))?!1:Fe(e)<=t.dataBytes}function Le(e,t){let n=e.notes;for(let r of e.groups){if(![0,1,2,4,8].includes(r.recordIdSize))throw Error(`This MDF file is damaged (bad record id size).`);if(r.vlsdCg)continue;let e=N(r);if(e>Ne||e===0){r.cycles>0&&n.push(`${r.name||`A group`}: its records are ${e===0?`empty`:`too large`} (damaged); skipped.`),r.cycles=0;continue}r.cycles=Math.min(r.cycles,Math.floor(Pe(r.stream,t)/e));let i=r.channels.filter(e=>!Ie(e,r));for(let e of i)n.push(`${e.name}: its bits lie outside its record (damaged); skipped.`);i.length&&(r.channels=r.channels.filter(e=>!i.includes(e))),r.master&&!Ie(r.master,r)&&(n.push(`${r.master.name}: the time channel lies outside its record (damaged): samples are numbered instead.`),r.master=null);for(let e of r.master?[...r.channels,r.master]:r.channels)e.invalBit!==null&&e.invalBit>=8*r.invalBytes&&(e.invalBit=null)}}function Re(e,t){let n=0;for(let r of e.master?[...t,e.master]:t){if(r.invalBit!==null)return 1/0;r.kind!==`vmaster`&&(n=Math.max(n,r.byteOffset+(r.bitOffset+r.bitCount+7>>3)))}return e.recordIdSize+n}var ze=65536,Be=`This file's compressed data is damaged.`;async function*Ve(e){for(let t=0;t<e.length;t+=ze)yield e.subarray(t,t+ze)}async function*He(e){if(typeof DecompressionStream!=`function`){let t=[],n=new re(e=>{t.push(e)});for await(let r of e)for(n.push(r,!1);t.length;)yield t.shift();for(n.push(new Uint8Array,!0);t.length;)yield t.shift();return}let t=new DecompressionStream(`deflate`),n=t.writable.getWriter();(async()=>{for await(let t of e)await n.write(t);await n.close()})().catch(e=>n.abort(e).catch(()=>{}));let r=t.readable.getReader();try{for(;;){let{done:e,value:t}=await r.read().catch(e=>{throw e instanceof TypeError?Error(Be):e});if(e)return;yield t}}finally{r.cancel().catch(()=>{})}}async function P(e,t=-1,n=1/0){let r=[],i=Math.min(t,n),a=t>=0?new Uint8Array(i):null,o=0;for await(let s of He(Ve(e))){if(a){if(o+s.length>t&&n>t)throw Error(`Compressed data is longer than its header says.`);a.set(o+s.length>i?s.subarray(0,i-o):s,o)}else r.push(s);if(o+=s.length,o>=n)break}if(a)return o>=i?a:a.subarray(0,o);let s=r.length===1?r[0]:new Uint8Array(o);return r.length>1&&r.reduce((e,t)=>(s.set(t,e),e+t.length),0),o>n?s.subarray(0,n):s}var Ue=new Uint8Array(Uint32Array.of(1).buffer)[0]===1;function We(e,t,n,r=t){let i=Math.floor(n/t),a=new Uint8Array(r<t?i*r:n),o=r%4==0&&Ue?new Uint32Array(a.buffer,0,i*r>>2):null,s=r>>2;for(let t=0;t<i;t+=512){let n=Math.min(i,t+512);if(o)for(let a=0;a<r;a+=4){let r=a*i,c=r+i,l=c+i,u=l+i;for(let i=t,d=t*s+(a>>2);i<n;i++,d+=s)o[d]=e[r+i]|e[c+i]<<8|e[l+i]<<16|e[u+i]<<24}else for(let o=0;o<r;o++){let s=o*i;for(let i=t,c=t*r+o;i<n;i++,c+=r)a[c]=e[s+i]}}let c=i*t;return r===t&&a.set(e.subarray(c,n),c),a}function Ge(e,t,n){if(!(n<t)||e.length===0)return t;let r=Math.min(t,n+3&-4);return r===t?t:e.every(e=>e.zip?.type===1&&e.zip.param===t&&e.zip.orgLength%t===0)?r:t}async function Ke(e,t,n=1/0,r=1/0){let i=await e.slice(t.offset,t.length),{type:a,param:o,orgLength:s}=t.zip;if(a===0)return P(i,s,r);let c=Math.min(n,o);return We(await P(i,s,c<o?s/o*c:1/0),o,s,c)}async function*F(e,t,n,r=1/0){let i=e=>!!e?.zip&&!(e.zip.type===0&&(e.length>16777216||e.zip.orgLength>268435456)),a=null;for(let o=0;o<t.length;o++){let s=t[o];if(n?.throwIfAborted(),!s.zip)for await(let t of e.windows(s.offset,s.length))yield t.bytes;else if(i(s)){let n=a??Ke(e,s,r);a=i(t[o+1])?Ke(e,t[o+1],r):null,a?.catch(()=>{}),yield await n}else{let t=s.zip.orgLength;for await(let n of He((async function*(){for await(let t of e.windows(s.offset,s.length))yield t.bytes})()))if(yield n.length>t?n.subarray(0,t):n,(t-=n.length)<=0)break}}}async function*qe(e,t,n,r){if(r<=0||t<=0)return;let i=Math.min(n,r),a=null,o=0,s=0;for await(let n of e){let e=0;for(;e<n.length&&s<r;){if(o===0){let a=Math.min(i,r-s,Math.floor((n.length-e)/t));if(a>0){yield{buf:n.subarray(e,e+a*t),n:a},s+=a,e+=a*t;continue}}a??=new Uint8Array(t*i);let c=Math.min(a.length,(r-s)*t),l=Math.min(n.length-e,c-o);if(a.set(n.subarray(e,e+l),o),o+=l,e+=l,o===c){let e=o/t;yield{buf:a,n:e},s+=e,o=0}}if(s>=r)return}a&&o>=t&&(yield{buf:a,n:Math.floor(o/t)})}async function Je(e,t,n,r){let i=n*r,a=0;for(let n of t){let t=n.zip?n.zip.orgLength:n.length;if(i<a)return null;if(i+r<=a+t)return n.zip?null:e.read(n.offset+i-a,r);a+=t}return null}var Ye=(e,t)=>(e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0,Xe=class{#e;#t;#n;#r;#i=new Uint8Array;#a=0;#o;#s=0;constructor(e,t,n){this.#e=e,this.#t=t,this.#n=n,this.#r=F(e,t,n),this.#o=t.reduce((e,t)=>e+(t.zip?t.zip.orgLength:t.length),0)}peek(e){let t=e-this.#a;if(t<0||t+4>this.#i.length)return null;let n=Ye(this.#i,t);return t+4+n>this.#i.length?null:this.#i.subarray(t+4,t+4+n)}async get(e){for(;;){let t=this.peek(e);if(t)return t;let n=e-this.#a;if(e+4>this.#o||n>=0&&n+4<=this.#i.length&&e+4+Ye(this.#i,n)>this.#o)throw Error(`This MF4 is damaged (string data ends early).`);e<this.#a&&(this.#r=F(this.#e,this.#t,this.#n),this.#i=new Uint8Array,this.#a=0);let r=await this.#r.next();if(r.done)throw Error(`This MF4 is damaged (string data ends early).`);if((this.#s+=r.value.length)>4*this.#o+16777216)throw Error(`This MF4 is damaged (its string offsets jump back and forth).`);let i=Math.max(0,Math.min(e-this.#a,this.#i.length)),a=this.#i.subarray(i),o=new Uint8Array(a.length+r.value.length);o.set(a),o.set(r.value,a.length),this.#a+=i,this.#i=o}}},Ze=e=>Math.max(1,Math.min(65536,Math.floor((8<<20)/Math.max(1,e)))),Qe=64<<20,I=class{#e;#t;#n=new Map;#r=new Map;constructor(e,t){this.#e=e,this.#t=t}want(e){this.#r.has(e)||this.#r.set(e,new Map)}wanted(e){return this.#r.has(e)}put(e,t,n){this.#r.get(e).set(t,n)}async bytes(e,t){let n=Array(t.length);if(e.sdRecordId!==null){let r=this.#r.get(e.sdRecordId);for(let e=0;e<t.length;e++)n[e]=r?.get(t[e])??null,r?.delete(t[e]);return n}if(!e.sd)return n.fill(null);let r=this.#n.get(e);r||this.#n.set(e,r=new Xe(this.#e,e.sd,this.#t));for(let e=0;e<t.length;e++)n[e]=r.peek(t[e])??await r.get(t[e]);return n}async strings(e,t){let n=ke(e.encoding);return(await this.bytes(e,t)).map(e=>e?Ae(n.decode(e)):null)}};function $e(e,t,n,r,i=N(e),a=new Float64Array(n)){let o=e.master;if(!o)return null;if(o.kind===`vmaster`)for(let e=0;e<n;e++)a[e]=r+e;else M(o,t,n,i,e.recordIdSize,a);return D(o.conv,a),a}function et(e,t,n){return n===1?e[t]:n===2?e[t]|e[t+1]<<8:n===4?(e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0:((e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0)+4294967296*((e[t+4]|e[t+5]<<8|e[t+6]<<16|e[t+7]<<24)>>>0)}function tt(e,t){let n=new Uint8Array(e.length+t.length);return n.set(e),n.set(t,e.length),n}async function*nt(e,t,n,r,i,a=1/0){let o=t.groups[n[0]];if(o.sorted){let t=Ge(o.stream,N(o),a),r=0;for await(let{buf:a,n:s}of qe(F(e,o.stream,i,t),t,Ze(t),o.cycles))yield{g:n[0],buf:a,n:s,start:r,size:t},r+=s;return}let s=t.groups.map((e,t)=>({g:e,k:t})).filter(({g:e})=>e.dg===o.dg),c=o.recordIdSize,l=new Map;for(let e of n){let n=t.groups[e],r=N(n);l.set(n.recordId,{k:e,size:r,buf:new Uint8Array(r*Math.min(Ze(r),Math.max(1,n.cycles))),fill:0,start:0})}let u=new Map(s.map(({g:e})=>[e.recordId,e.vlsdCg?-1:N(e)])),d=new Map,f=new Uint8Array;for await(let t of F(e,o.stream,i)){let e=f.length?tt(f,t):t,n=0;for(;!(n+c>e.length);){let t=et(e,n,c),i=u.get(t);if(i===void 0)throw Error(`This MF4 is damaged (unknown record id ${t}).`);if(i<0){if(n+c+4>e.length)break;let i=(e[n+c]|e[n+c+1]<<8|e[n+c+2]<<16|e[n+c+3]<<24)>>>0;if(i>Qe)throw Error(`This MF4 is damaged (a string record of ${i} bytes).`);let a=n+c+4+i;if(a>e.length)break;let o=d.get(t)??0;r.wanted(t)&&r.put(t,o,e.slice(n+c+4,a)),d.set(t,o+4+i),n=a;continue}if(n+i>e.length)break;let a=l.get(t);if(a&&(a.buf.set(e.subarray(n,n+i),a.fill),a.fill+=i,a.fill===a.buf.length)){let e=a.fill/a.size;yield{g:a.k,buf:a.buf,n:e,start:a.start,size:a.size},a.start+=e,a.fill=0}n+=i}f=e.slice(n)}for(let e of l.values())e.fill&&(yield{g:e.k,buf:e.buf,n:e.fill/e.size,start:e.start,size:e.size})}var L=(e,t)=>t?e+2147483648:e,rt=/^BO_\s+(\d+)\s+(\w+)\s*:\s*(\d+)/,it=/^SG_\s+(\w+)\s*(?:(M|m\d+M?)\s*)?:\s*(\d+)\|(\d+)@([01])([+-])\s*\(([^,)]*),([^)]*)\)\s*\[[^\]]*\]\s*"([^"]*)"/,at=/^VAL_\s+(\d+)\s+(\w+)\s+(.*);/,ot=/^SIG_VALTYPE_\s+(\d+)\s+(\w+)\s*:\s*([12])\s*;/,st=/^-?\d+(?:\.\d+)?$/;function ct(e){let t=e.split(`"`),n=new Map;for(let e=0;e+1<t.length;e+=2){let r=t[e].trim();st.test(r)&&n.set(Number(r),t[e+1])}return n}var lt=(e,t)=>e<=511&&t>=1&&t<=64;function ut(e){let t=new Map,n=new Map,r=new Map,i=(e,t)=>{let i=n.get(Number(e));return i?r.get(i)?.get(t):void 0},a=null;for(let o of e.split(/\r?\n/)){let e=o.trim(),s;if(s=rt.exec(e)){let e=Number(s[1]),r=e>=2147483648,i=r?e-2147483648:e;a={id:i,extended:r,name:s[2],size:Number(s[3]),signals:[]},t.set(L(i,r),a),n.set(e,a)}else if((s=it.exec(e))&&a){let e=s[2],t=Number(s[3]),n=Number(s[4]);if(!lt(t,n)||!s[7].trim()||!s[8].trim())continue;let i={name:s[1],start:t,length:n,littleEndian:s[5]===`1`,signed:s[6]===`-`,factor:Number(s[7]),offset:Number(s[8]),unit:s[9],mux:e?e===`M`?`switch`:parseInt(e.slice(1),10):null,float:0,values:null};a.signals.push(i);let o=r.get(a);o||r.set(a,o=new Map),o.has(i.name)||o.set(i.name,i)}else if(s=at.exec(e)){let e=i(s[1],s[2]);e&&(e.values=ct(s[3]))}else if(s=ot.exec(e)){let e=i(s[1],s[2]);e&&(e.float=s[3]===`1`?32:64)}else (e.startsWith(`BO_`)||e===``)&&e===``&&(a=null)}return t}function dt(e){if(e.float){let t=e.float/8,n=e.start>>3,r=e.littleEndian,i=new DataView(new ArrayBuffer(8));return(e,a)=>{for(let r=0;r<t;r++)i.setUint8(r,e[a+n+r]??0);return t===4?i.getFloat32(0,r):i.getFloat64(0,r)}}let t=e.length,n=2**t,r=n/2,i=e.signed;if(e.littleEndian){let a=e.start>>3,o=e.start&7;if(o+t<=32){let e=t===32?-1:(1<<t)-1;return(t,s)=>{let c=s+a,l=((t[c]|t[c+1]<<8|t[c+2]<<16|t[c+3]<<24)>>>o&e)>>>0;return i&&l>=r?l-n:l}}let s=o+t+7>>3,c=2**(t-(32-o)),l=2**(32-o);return(e,t)=>{let u=t+a,d=(e[u]|e[u+1]<<8|e[u+2]<<16|e[u+3]<<24)>>>o,f=0;for(let t=s-1;t>=4;t--)f=f*256+(e[u+t]??0);let p=d+f%c*l;return i&&p>=r?p-n:p}}let a=e.start>>3,o=e.start&7;if(7-o+t<=32){let e=32-(7-o)-t,s=t===32?-1:(1<<t)-1;return(t,o)=>{let c=o+a,l=((t[c]<<24|t[c+1]<<16|t[c+2]<<8|t[c+3])>>>e&s)>>>0;return i&&l>=r?l-n:l}}let s=(1<<o+1)-1,c=t-(o+1)>>3,l=t-(o+1)&7,u=2**l;return(e,t)=>{let o=(e[t+a]??0)&s,d=t+a+1;for(let t=0;t<c;t++)o=o*256+(e[d++]??0);return l>0&&(o=o*u+((e[d]??0)>>8-l)),i&&o>=r?o-n:o}}function ft(e){let t=e.length,n=2**t,r=n/2,i=e.signed,a=t===32?-1:(1<<t)-1;if(!e.float&&e.littleEndian&&(e.start&7)+t<=32){let t=e.start>>3,o=e.start&7;return(e,s,c,l,u,d,f)=>{for(let p=0;p<l;p++){let l=s[c[p]]+t,m=((e[l]|e[l+1]<<8|e[l+2]<<16|e[l+3]<<24)>>>o&a)>>>0;u[p]=(i&&m>=r?m-n:m)*d+f}}}if(!e.float&&!e.littleEndian&&7-(e.start&7)+t<=32){let o=e.start>>3,s=32-(7-(e.start&7))-t;return(e,t,c,l,u,d,f)=>{for(let p=0;p<l;p++){let l=t[c[p]]+o,m=((e[l]<<24|e[l+1]<<16|e[l+2]<<8|e[l+3])>>>s&a)>>>0;u[p]=(i&&m>=r?m-n:m)*d+f}}}let o=dt(e);return(e,t,n,r,i,a,s)=>{for(let c=0;c<r;c++)i[c]=o(e,t[n[c]])*a+s}}function pt(e){if(!e.busEvent||e.busType!==2)return null;let t=t=>e.channels.find(e=>e.name.endsWith(t))??null,n=t(`DataFrame.ID`),r=t(`DataFrame.DataBytes`);return!n||!r?null:{id:n,ide:t(`DataFrame.IDE`),data:r}}var mt=()=>({n:0,time:new Float64Array,key:new Float64Array,id:new Float64Array,ide:new Float64Array,bytes:null,buf:new Uint8Array,dataAt:new Int32Array});async function ht(e,t,n,r,i,a,o,s){s.key.length<r&&Object.assign(s,{time:new Float64Array(r),key:new Float64Array(r),id:new Float64Array(r),ide:new Float64Array(r),dataAt:new Int32Array(r)});let c=e.recordIdSize,{id:l,ide:u,key:d,dataAt:f}=s;M(t.id,n,r,a,c,l),t.ide&&M(t.ide,n,r,a,c,u);for(let e=0;e<r;e++){let n=l[e],r=n&536870911;d[e]=L(r,t.ide?u[e]!==0:n>=2147483648||r>2047)}if(s.bytes=null,t.data.kind===`vlsd`){let e=new Float64Array(r);M({...t.data,type:`uint`},n,r,a,c,e),s.bytes=await o.bytes(t.data,e)}else for(let e=0,n=c+t.data.byteOffset;e<r;e++,n+=a)f[e]=n;if(!$e(e,n,r,i,a,s.time.subarray(0,r)))for(let e=0;e<r;e++)s.time[e]=i+e;return s.n=r,s.buf=n,s}var gt=new Uint8Array;function _t(e){return e.float?(e.start>>3)+e.float/8:e.littleEndian?(e.start>>3)+((e.start&7)+e.length+7>>3):(e.start>>3)+1+(Math.max(0,e.length-((e.start&7)+1))+7>>3)}function vt(e,t,n){let r=0;for(let e of n.values())for(let t of e.signals)r=Math.max(r,_t(t));let i=t.data.kind===`vlsd`?t.data:{...t.data,bitCount:Math.min(t.data.bitCount,r*8)};return Re(e,t.ide?[t.id,t.ide,i]:[t.id,i])}function yt(e,t){let n=t.map(t=>e.signals[e.channels[t].s]);return{route:e,col:n.map(ft),read:n.map(dt),factor:n.map(e=>e.factor),offset:n.map(e=>e.offset),table:t.map((t,r)=>e.channels[t].text?n[r].values:null),idx:new Int32Array,cnt:0,n:0,t0:NaN,t1:NaN}}function R(e,t,n,r,i,a=1,o=0){let s=new Float64Array(i);if(n.bytes)for(let e=0;e<i;e++)s[e]=t(n.bytes[r[e]]??gt,0)*a+o;else e(n.buf,n.dataAt,r,i,s,a,o);return s}function bt(e,t){let{idx:n,cnt:r}=e,i=new Float64Array(r);for(let e=0;e<r;e++)i[e]=t.time[n[e]];return e.n===0&&(e.t0=i[0]),e.t1=i[r-1],e.n+=r,e.cnt=0,{time:i,cols:e.col.map((i,a)=>{let o=e.table[a];return o?Array.from(R(i,e.read[a],t,n,r),e=>o.get(e)??null):R(i,e.read[a],t,n,r,e.factor[a],e.offset[a])})}}var xt=e=>({key:e,skip:!0,frames:0,all:null,sw:null,byMux:new Map,idx:new Int32Array,cnt:0});function St(e,t){let n=e.signals.find(e=>e.mux===`switch`);return{key:L(e.id,e.extended),skip:!1,frames:0,all:t,sw:n?{col:ft(n),read:dt(n)}:null,byMux:new Map,idx:new Int32Array,cnt:0}}var Ct=class{#e=Array(2048);#t=new Map;#n;constructor(e){this.#n=e}get(e){let t=e<2048?this.#e[e]:this.#t.get(e);return t||(t=this.#n(e),e<2048?this.#e[e]=t:this.#t.set(e,t)),t}};function wt(e,t,n){let r=[];for(let n=0;n<e.n;n++){let i=t.get(e.key[n]);if(i.skip){i.frames++;continue}i.cnt===0&&(r.push(i),i.idx.length<e.n&&(i.idx=new Int32Array(e.n))),i.idx[i.cnt++]=n}let i=[];for(let t of r){if(t.all&&(t.all.idx=t.idx,t.all.cnt=t.cnt,i.push(t.all)),t.sw){let r=R(t.sw.col,t.sw.read,e,t.idx,t.cnt);for(let e=0;e<t.cnt;e++){let a=t.byMux.get(r[e]);a===void 0&&((a=n(t,r[e]))||t.byMux.size<4096)&&t.byMux.set(r[e],a),a&&(a.cnt===0&&(i.push(a),a.idx.length<t.cnt&&(a.idx=new Int32Array(t.cnt))),a.idx[a.cnt++]=t.idx[e])}}t.cnt=0}return i}var Tt=48e6;async function Et(e,t,n,r){let i=new Map;if(t.groups.forEach((e,t)=>{let n=pt(e);n&&e.cycles>0&&i.set(t,n)}),i.size===0)return null;if(n.length===0)return{dbc:new Map,layouts:i,routes:[],notes:[],cache:new Map};let a=ut(m(new Uint8Array(await n[0].arrayBuffer()))),o=[],s=new Map,c=new Map,l=0,u=new I(e,r);for(let[n,d]of i){let i=t.groups[n],f=(e,t)=>{let n=e.signals.filter(e=>t===null?typeof e.mux!=`number`:e.mux===t),r=[];return n.forEach((e,t)=>{r.push({s:t,text:!1}),e.values&&r.push({s:t,text:!0})}),yt({key:L(e.id,e.extended),mux:t,signals:n,channels:r},r.map((e,t)=>t))},p=new Ct(e=>{let t=a.get(e);if(!t){let t=xt(e);return s.set(e,t),t}let r=St(t,f(t,null));return o.push({model:n,m:r}),r});d.data.sdRecordId!==null&&u.want(d.data.sdRecordId);let m=mt();for await(let o of nt(e,t,[n],u,r,vt(i,d,a))){let e=await ht(i,d,o.buf,o.n,o.start,o.size,u,m),t=(e,t)=>{let n=a.get(e.key);return n.signals.some(e=>e.mux===t)?f(n,t):null};for(let n of wt(e,p,t)){let t=bt(n,e);if(l<0)continue;if((l+=t.time.length*(1+t.cols.length))>Tt){l=-1,c.clear();continue}let r=c.get(n);r||c.set(n,r=[]),r.push(t)}}}let d=[],f=new Map;for(let{model:e,m:t}of o)for(let n of[t.all,...t.byMux.values()]){if(!n)continue;let{route:t,n:r,t0:i,t1:o}=n;if(t.signals.length===0)continue;let s=a.get(t.key);l>=0&&f.set(t,c.get(n)??[]),d.push({model:e,route:t,group:{name:t.mux===null?s.name:`${s.name} [m=${t.mux}]`,channels:t.channels.map(e=>({name:e.text?`${t.signals[e.s].name} (text)`:t.signals[e.s].name,unit:e.text?``:t.signals[e.s].unit,text:e.text})),samples:r,time:{kind:`timestamps`},rateHz:r>1&&o>i?Math.round((r-1)/(o-i)*1e6)/1e6:null}})}let p=[];if(s.size){let e=[...s.values()].reduce((e,t)=>e+t.frames,0),t=[...s.keys()].map(e=>`0x${(e%2147483648).toString(16).toUpperCase()}`).join(`, `);p.push(`${e} frame${e===1?``:`s`} with ${s.size} id${s.size===1?``:`s`} not in the DBC ${e===1?`was`:`were`} skipped (${t}).`)}return{dbc:a,layouts:i,routes:d,notes:p,cache:f}}async function*Dt(e,t,n,r,i,a){let o=[];for(let e of i){let t=r.cache.get(e.route);if(!t){o.push(e);continue}r.cache.delete(e.route);let n=0;for(let r of t)yield{group:e.group,start:n,length:r.time.length,time:r.time,columns:e.sel.map(e=>r.cols[e])},n+=r.time.length}if(!o.length)return;let s=r.layouts.get(n),c=t.groups[n],l=new Map,u=new Map;for(let e of o){let t=yt(e.route,e.sel);l.set(t,e.group);let n=u.get(e.route.key);n||u.set(e.route.key,n={all:null,byMux:new Map}),e.route.mux===null?n.all=t:n.byMux.set(e.route.mux,t)}let d=new Ct(e=>{let t=u.get(e);if(!t)return xt(e);let n=St(r.dbc.get(e),t.all);return t.byMux.size||(n.sw=null),n}),f=new I(e,a);s.data.sdRecordId!==null&&f.want(s.data.sdRecordId);let p=mt();for await(let i of nt(e,t,[n],f,a,vt(c,s,r.dbc))){let e=await ht(c,s,i.buf,i.n,i.start,i.size,f,p);for(let t of wt(e,d,(e,t)=>u.get(e.key).byMux.get(t)??null)){let n=t.n,r=bt(t,e);yield{group:l.get(t),start:n,length:r.time.length,time:r.time,columns:r.cols}}}}var Ot=e=>Math.round(e*1e6)/1e6,kt=e=>e.kind===`value`?e.type!==`bytes`:e.kind===`vlsd`&&e.type===`string`,At=e=>({name:e.name,unit:e.unit,text:e.type===`string`||e.kind===`vlsd`||e.conv.k===`text`});async function jt(e,t){let n=t.master;if(!n||t.cycles<2)return null;if(n.kind===`vmaster`)return n.conv.k===`lin`&&n.conv.a>0?Ot(1/n.conv.a):null;if(!t.sorted)return null;let r=N(t),i=t.cycles,a,o;if(t.stream[0]?.zip){let n=Ge(t.stream.slice(0,1),r,Re(t,[])),s=await Ke(e,t.stream[0],n,4096*r);if(i=Math.min(t.cycles,Math.floor(s.length/n)),i<2)return null;a=s.subarray(0,n),o=s.subarray((i-1)*n,i*n)}else a=await Je(e,t.stream,0,r),o=await Je(e,t.stream,t.cycles-1,r);if(!a||!o)return null;let s=new Float64Array(2);return M(n,a,1,r,t.recordIdSize,s,0),M(n,o,1,r,t.recordIdSize,s,1),D(n.conv,s),s[1]>s[0]?Ot((i-1)/(s[1]-s[0])):null}async function Mt(e,t,n,r,i,a,o){let s;if(e.kind===`vlsd`){let a=new Float64Array(n);M({...e,type:`uint`},t,n,r,i,a),s=await o.strings(e,a)}else if(e.type===`string`)s=je(e,t,n,r,i);else{let a=new Float64Array(n);M(e,t,n,r,i,a),e.conv.k===`text`?s=pe(e.conv,a):(D(e.conv,a),s=a)}return Me(e,t,n,r,a,s),s}async function Nt(e,t,n,r,i,a,o,s){let c=e.recordIdSize,l=[];for(let i of t)l.push(await Mt(e.channels[i],n,r,a,c,c+e.dataBytes,s));return{group:o,time:$e(e,n,r,i,a),start:i,length:r,columns:l}}async function*Pt(e,t){let n=e.plan.state,r=t.filter(e=>n.groups[e].route),i=t.filter(e=>!n.groups[e].route);if(r.length){let t=r.map(t=>({group:t,route:n.groups[t].route,sel:T(e,t)}));yield*Dt(e.io,n.model,n.groups[r[0]].model,n.bus,t,e.signal)}if(!i.length)return;let a=new I(e.io,e.signal),o=new Map;for(let t of i){let r=n.groups[t],i=T(e,t).map(e=>r.channels[e]);for(let e of i){let t=n.model.groups[r.model].channels[e].sdRecordId;t!==null&&a.want(t)}o.set(r.model,{pg:t,cols:i})}let[s,{cols:c}]=o.entries().next().value,l=n.model.groups[s],u=o.size===1?Re(l,c.map(e=>l.channels[e])):1/0;for await(let t of nt(e.io,n.model,[...o.keys()],a,e.signal,u)){let{pg:e,cols:r}=o.get(t.g);yield await Nt(n.model.groups[t.g],r,t.buf,t.n,t.start,t.size,e,a)}}function Ft(e){let t=e.plan.state,n=new Map;for(let r of e.selection){let e=t.model.groups[t.groups[r.group].model].dg;n.set(e,[...n.get(e)??[],r.group])}return[...n].sort((e,t)=>e[0]-t[0]).map(([,e])=>e)}var It={...c,async plan(e,t){let n=await t.read(0,64);if(y(n,`UnFinMF`))throw Error(`This MF4 was not finalized by its logger (it starts with UnFinMF). Finalize it with the logger software or asammdf, then open it again.`);let r=w(n.subarray(8,16)).replace(/\0/g,``).trim(),i=Number(r.replace(`.`,``).slice(0,3))>=400?await Ee(t,r):await he(t,n,r);Le(i,t.size);let a=await Et(t,i,e.companions.filter(e=>e.name.toLowerCase().endsWith(`.dbc`))),o=new Set(a?.layouts.keys()??[]),s=[],c=[],l=[...i.notes],u=0;for(let e=0;e<i.groups.length;e++){let n=i.groups[e];if(n.vlsdCg||n.cycles===0||o.has(e))continue;let r=[];if(n.channels.forEach((e,t)=>{kt(e)?r.push(t):u++}),r.length!==0){for(let e of r){let t=n.channels[e].conv;t.k===`unsupported`&&l.push(`${n.channels[e].name}: ${t.what} is not supported; raw values are shown.`)}c.push({model:e,channels:r}),s.push({name:n.name,channels:r.map(e=>At(n.channels[e])),samples:n.cycles,time:n.master?{kind:`timestamps`}:{kind:`index`},rateHz:await jt(t,n)})}}if(u&&l.push(`${u} byte-array channel${u===1?` was`:`s were`} skipped.`),a){if(a.routes.length===0&&a.dbc.size===0){if(s.length===0)throw Error(`This MF4 has raw CAN frames. Drop its .dbc file together with it to decode signals.`);l.push(`This MF4 also has raw CAN frames. Drop its .dbc file together with it to decode signals.`)}for(let e of a.routes)c.push({model:e.model,route:e.route}),s.push(e.group);l.push(...a.notes)}if(s.length===0)throw Error(`This MDF file has no channels with samples.`);let d={model:i,groups:c,bus:a};return{format:i.version,startTime:i.startTime,groups:s,meta:i.meta,events:[],notes:l,interleaved:!!a?.routes.length||i.groups.some(e=>!e.sorted&&!e.vlsdCg),state:d}},async*stream(e){for(let t of Ft(e))yield*Pt(e,t)},streamGroup(e,t){return Pt(e,[t])}};function Lt(e){let t=[];for(let n of m(e).split(`\0`)){if(!n)continue;let e=n.split(``),r=parseFloat(e[0].split(``)[0]);Number.isFinite(r)&&t.push({onset:r,texts:e.slice(1).filter(e=>e.length>0)})}return t}function Rt(e,t){let n=/^(\d\d)\.(\d\d)\.(\d\d)$/.exec(e),r=/^(\d\d)\.(\d\d)\.(\d\d)$/.exec(t);if(!n||!r)return null;let i=Number(n[3]);return`${i>=85?1900+i:2e3+i}-${n[2]}-${n[1]}T${r[1]}:${r[2]}:${r[3]}`}var zt=e=>`${+e.toFixed(6)} Hz`;function Bt(e,t,n,r,i,a,o,s){let{bit:c,off:l}=e;if(a===2)for(let e=0;e<i;e++)o[s+e]=c*(l+t.getInt16(r+2*e,!0));else for(let e=0,t=r;e<i;e++,t+=3)o[s+e]=c*(l+((n[t]|n[t+1]<<8|n[t+2]<<16)<<8>>8))}var Vt={...a,async plan(e,t){let n=await t.read(0,256),r=(e,t)=>w(n.subarray(e,e+t)).trim(),i=n[0]===255,a=parseInt(r(252,4),10),o=parseInt(r(184,8),10),s=r(192,44),c=/^(EDF|BDF)\+/.test(s),l=/^(EDF|BDF)\+D/.test(s),u=parseFloat(r(244,8));if(!(a>0)||o!==256*(a+1)||!(u>0))throw Error(`This EDF/BDF header is damaged.`);let d=await t.read(256,a*256),f=(e,t,n)=>w(d.subarray(e*a+t*n,e*a+t*(n+1))).trim(),p=i?3:2,m=[],h=0;for(let e=0;e<a;e++){let t=parseFloat(f(104,8,e)),n=parseFloat(f(112,8,e)),r=parseFloat(f(120,8,e)),i=parseFloat(f(128,8,e)),a=Number(f(216,8,e));if(!(Number.isInteger(a)&&a>=0))throw Error(`This EDF/BDF header is damaged (samples per record).`);let o=(n-t)/(i-r);m.push({label:f(0,16,e),unit:f(96,8,e),spr:a,offset:h,bit:o,off:n/o-i}),h+=a*p}let g=h;if(!(g>0&&o+g<=t.size))throw Error(`This EDF/BDF file is damaged (its data records do not fit in it).`);let _=Math.floor((t.size-o)/g),v=parseInt(r(236,8),10),y=[];v>=0?v>_&&(y.push(`The file is shorter than its header says: ${_} of ${v} data records were read.`),v=_):v=_;let b=c?m.flatMap((e,t)=>/^(EDF|BDF) Annotations$/.test(e.label)?[t]:[]):[],x=new Map;m.forEach((e,t)=>{!b.includes(t)&&e.spr>0&&x.set(e.spr,[...x.get(e.spr)??[],t])});let S=[...x.values()],C=S.map(e=>{let t=m[e[0]].spr/u;return{name:zt(t),channels:e.map(e=>({name:m[e].label,unit:m[e].unit})),samples:v*m[e[0]].spr,rateHz:t,time:l?{kind:`timestamps`}:{kind:`rate`,rate:t,start:0}}}),ee={headerBytes:o,records:v,duration:u,recordBytes:g,bps:p,signals:m,groups:S,annotations:b,discontinuous:l};return{format:`${i?`BDF`:`EDF`}${c?l?`+D`:`+`:``}`,startTime:Rt(r(168,8),r(176,8)),groups:C,meta:{patient:r(8,80),recording:r(88,80)},events:[],notes:l?[`Discontinuous recording: each gap shows as a break in the traces.`,...y]:y,state:ee}},async*stream(t){let n=t.plan.state,r=Math.max(1,Math.floor(f/n.recordBytes)),i=t.selection.map(e=>({g:e.group,chans:e.channels.map(t=>n.signals[n.groups[e.group][t]]),spr:n.signals[n.groups[e.group][0]].spr})),a=i.map(()=>0),o=0,s=NaN;for await(let c of t.io.windows(n.headerBytes,n.records*n.recordBytes,r*n.recordBytes)){t.signal?.throwIfAborted();let r=Math.floor(c.bytes.length/n.recordBytes);if(r===0)break;let l=e(c.bytes),u=new Float64Array(r),d=[];for(let e=0;e<r;e++)u[e]=(o+e)*n.duration,n.annotations.forEach((t,r)=>{let i=n.signals[t],a=e*n.recordBytes+i.offset,o=Lt(c.bytes.subarray(a,a+i.spr*n.bps));r===0&&o.length&&(u[e]=o[0].onset);for(let e of o)for(let t of e.texts)d.push({time:e.onset,text:t})});let f=new Uint8Array(r),p=0;if(n.discontinuous)for(let e=0;e<r;e++){let t=e?u[e-1]+n.duration:s;t===t&&u[e]>t+1e-9&&(f[e]=1,p++)}for(let e=0;e<i.length;e++){let t=i[e],o=t.spr/n.duration,m=r*t.spr+p,h=n.discontinuous?new Float64Array(m):null,g=t.chans.map(()=>new Float64Array(m)),_=0;for(let e=0;e<r;e++){if(f[e]){h[_]=e?u[e-1]+n.duration:s;for(let e of g)e[_]=NaN;_++}if(h)for(let n=0;n<t.spr;n++)h[_+n]=u[e]+n/o;let r=e*n.recordBytes;t.chans.forEach((e,i)=>Bt(e,l,c.bytes,r+e.offset,t.spr,n.bps,g[i],_)),_+=t.spr}let v={group:t.g,time:h,start:a[e],length:m,columns:g};e===0&&d.length&&(v.events=d),a[e]+=m,yield v}s=u[r-1]+n.duration,o+=r}}},z={float32:4,double64:8,int8:1,int16:2,int32:4,int64:8};function B(e,t,n=0){let r=e.indexOf(`<${t}>`,n);if(r<0)return``;let i=e.indexOf(`</${t}>`,r);return i<0?``:e.slice(r+t.length+2,i).trim()}function Ht(e,t){let n=[];for(let r=e.indexOf(`<${t}>`);r>=0;){let i=e.indexOf(`</${t}>`,r);if(i<0)break;n.push(e.slice(r,i+t.length+3)),r=e.indexOf(`<${t}>`,i)}return n}var Ut=1e5,Wt=e=>e.fmt===`string`||z[e.fmt]!==void 0;function V(e,t,n){let r=e[n];if((r===1||r===4||r===8)&&n+1+r<=e.length)return r===1?[e[n+1],2]:r===4?[t.getUint32(n+1,!0),5]:[x(t,n+1),9];throw Error(`This XDF file is damaged (bad length field).`)}function Gt(e,t,n,r){if(n>=e.length)return-1;if(e[n++]===8&&(n+=8),r.fmt!==`string`)return n+z[r.fmt]*r.nch<=e.length?n+z[r.fmt]*r.nch:-1;for(let i=0;i<r.nch;i++){if(n>=e.length)return-1;let[r,i]=V(e,t,n);n+=i+r}return n<=e.length?n:-1}async function*Kt(t,n){let r=new Uint8Array;for await(let i of t.windows(4,t.size-4)){n?.throwIfAborted();let a=i.bytes,o=i.offset-r.length;if(r.length){let e=new Uint8Array(r.length+a.length);e.set(r),e.set(a,r.length),a=e}let s=e(a),c=0;for(;!(c+1>a.length);){let e=a[c];if(c+1+e>a.length)break;let[n,r]=V(a,s,c),i=c+r+n;if(o+i>t.size)return;if(i>a.length)break;yield{tag:a[c+r]|a[c+r+1]<<8,body:a.subarray(c+r+2,i)},c=i}r=a.slice(c)}}function qt(e,t,n){let r=t-e.i0,i=n-e.t0;e.n++,e.si+=r,e.st+=i,e.sii+=r*r,e.sit+=r*i}function Jt(e){let t=e.n*e.sii-e.si*e.si;e.b=t>0?(e.n*e.sit-e.si*e.st)/t:0,e.a=(e.st-e.b*e.si)/Math.max(1,e.n)}function Yt(e){if(e.length===0)return[0,0];if(e.length===1)return[e[0][1],0];let t={i0:e[0][0],t0:0,n:0,si:0,st:0,sii:0,sit:0,a:0,b:0};for(let[n,r]of e)qt(t,n,r);return Jt(t),[t.a-t.b*t.i0,t.b]}function Xt(e,t,n){let r=t.t0+t.a+t.b*(n-t.i0);return r+e.c0+e.c1*r}var Zt={...v,async plan(t,n){let r=new Map,i=``;for await(let t of Kt(n)){let n=e(t.body);if(t.tag===1)i=m(t.body);else if(t.tag===2){let e=n.getUint32(0,!0),i=m(t.body.subarray(4)),a=parseInt(B(i,`channel_count`),10)||1;if(!(a>=1&&a<=Ut))throw Error(`This XDF file is damaged (a stream with ${a} channels).`);let o=Ht(i,`channel`);r.set(e,{id:e,name:B(i,`name`)||`Stream ${e}`,nch:a,srate:parseFloat(B(i,`nominal_srate`))||0,fmt:B(i,`channel_format`)||`float32`,labels:Array.from({length:a},(e,t)=>B(o[t]??``,`label`)||`Ch${t+1}`),units:Array.from({length:a},(e,t)=>B(o[t]??``,`unit`)),count:0,segs:[],clock:[],c0:0,c1:0,lastTs:NaN})}else if(t.tag===3){let e=r.get(n.getUint32(0,!0));if(!e||!Wt(e))continue;let[i,a]=V(t.body,n,4),o=4+a,s=z[e.fmt],c=e.srate>0?Math.max(1,500/e.srate):1/0;for(let r=0;r<i&&!(Gt(t.body,n,o,e)<0);r++){let r;if(t.body[o++]===8?(r=n.getFloat64(o,!0),o+=8):r=e.lastTs+(e.srate>0?1/e.srate:0),e.fmt===`string`)for(let r=0;r<e.nch;r++){let[e,r]=V(t.body,n,o);o+=r+e}else o+=s*e.nch;if(e.srate>0){let t=e.segs[e.segs.length-1];(!t||r-e.lastTs>c)&&e.segs.push(t={i0:e.count,t0:r,n:0,si:0,st:0,sii:0,sit:0,a:0,b:0}),qt(t,e.count,r)}else e.count===0&&e.segs.push({i0:0,t0:r,n:1,si:0,st:0,sii:0,sit:0,a:0,b:0});e.lastTs=r,e.count++}}else if(t.tag===4){let e=r.get(n.getUint32(0,!0));e&&e.clock.push([n.getFloat64(4,!0),n.getFloat64(12,!0)])}}let a=[],o=[...r.values()].sort((e,t)=>e.id-t.id).filter(e=>(Wt(e)||a.push(`${e.name}: channel format ${e.fmt} is not supported; skipped.`),Wt(e))),s=1/0;for(let e of o){[e.c0,e.c1]=Yt(e.clock);for(let t of e.segs)Jt(t);if(e.count===0)continue;let t=e.srate>0?Xt(e,e.segs[0],0):e.segs[0].t0+e.c0+e.c1*e.segs[0].t0;s=Math.min(s,t)}let c=p(o.map(e=>e.name),[]),l=o.map((e,t)=>({name:c[t],channels:e.labels.map((t,n)=>({name:t,unit:e.units[n],...e.fmt===`string`?{text:!0,sparse:!0}:{}})),samples:e.count,time:{kind:`timestamps`},rateHz:e.srate>0?e.srate:null,irregular:e.srate===0})),u=B(i,`version`);return{format:`XDF ${u||`1.0`}`,startTime:null,groups:l,meta:{header:u},events:[],notes:a,state:{streams:o,origin:Number.isFinite(s)?s:0}}},async*stream(t){let n=t.plan.state,r=8192,i=new Map,a=e=>e.sel.map(()=>e.s.fmt===`string`?Array(r):new Float64Array(r));for(let e of t.selection){let o=n.streams[e.group],s={g:e.group,s:o,sel:T(t,e.group),time:new Float64Array(r),cols:[],n:0,start:0,seg:0,i:0,raw:NaN};s.cols=a(s),i.set(o.id,s)}let o=e=>{let t={group:e.g,time:e.time.slice(0,e.n),start:e.start,length:e.n,columns:e.cols.map(t=>t.slice(0,e.n))};return e.start+=e.n,e.n=0,e.time=new Float64Array(r),e.cols=a(e),t};for await(let a of Kt(t.io,t.signal)){if(a.tag!==3)continue;let t=e(a.body),s=i.get(t.getUint32(0,!0));if(!s)continue;let c=s.s,[l,u]=V(a.body,t,4),d=4+u,f=z[c.fmt],p=[];for(let e=0;e<l&&!(Gt(a.body,t,d,c)<0);e++){let e;a.body[d++]===8?(e=t.getFloat64(d,!0),d+=8):e=s.raw+(c.srate>0?1/c.srate:0),s.raw=e;let i;if(c.srate>0){for(;s.seg+1<c.segs.length&&c.segs[s.seg+1].i0<=s.i;)s.seg++;i=Xt(c,c.segs[s.seg],s.i)}else i=e+c.c0+c.c1*e;if(s.time[s.n]=i-n.origin,c.fmt===`string`){let e=[];for(let n=0;n<c.nch;n++){let[n,r]=V(a.body,t,d);e.push(m(a.body.subarray(d+r,d+r+n))),d+=r+n}s.sel.forEach((t,n)=>{s.cols[n][s.n]=e[t]}),p.push({time:i-n.origin,text:e.join(` `)})}else s.sel.forEach((e,n)=>{let r=d+e*f,i=s.cols[n];switch(c.fmt){case`float32`:i[s.n]=t.getFloat32(r,!0);break;case`double64`:i[s.n]=t.getFloat64(r,!0);break;case`int8`:i[s.n]=t.getInt8(r);break;case`int16`:i[s.n]=t.getInt16(r,!0);break;case`int32`:i[s.n]=t.getInt32(r,!0);break;default:i[s.n]=t.getInt32(r+4,!0)*4294967296+t.getUint32(r,!0)}}),d+=f*c.nch;s.i++,++s.n===r&&(yield o(s))}p.length&&(yield{group:s.g,time:new Float64Array,start:s.start,length:0,columns:s.sel.map(()=>new Float64Array),events:p})}for(let e of i.values())e.n&&(yield o(e))}},Qt=2,$t=8,en=4,tn=32,nn=64,rn=4713,an=4714,H=32,U=68,W={1:1,2:2,3:4,4:8,5:1,6:2,7:4,8:8,9:4,10:8,25:4,26:8,33:1,68:16},on={0:5,1:1,2:6,3:2,4:7,5:3,6:8,7:4,8:9,9:10,4294967295:U},sn=65536,cn=65536,ln=(e,t,n)=>n?b(e,t+8,n)+x(e,t,n)/2**64:b(e,t,n)+x(e,t+8,n)/2**64;function un(e,t,n,r,i){switch(r){case H:{let r=e.getUint32(n,i);return[m(t.subarray(n+4,n+4+r)),4+r]}case 1:return[e.getInt8(n),1];case 2:return[e.getInt16(n,i),2];case 3:return[e.getInt32(n,i),4];case 4:return[b(e,n,i),8];case 5:return[e.getUint8(n),1];case 6:return[e.getUint16(n,i),2];case 7:return[e.getUint32(n,i),4];case 8:return[x(e,n,i),8];case 9:case 25:return[e.getFloat32(n,i),4];case 10:case 26:return[e.getFloat64(n,i),8];case 33:return[e.getUint8(n)!==0,1];case U:return[ln(e,n,i),16];default:throw Error(`This TDMS file has a property of type ${r}, which Kymoscope cannot read.`)}}function dn(e){let t=/^\/'((?:[^']|'')*)'(?:\/'((?:[^']|'')*)')?$/.exec(e);return t?[t[1].replace(/''/g,`'`),(t[2]??``).replace(/''/g,`'`)]:null}var fn=4294967295;function pn(e){if(e.has(`NI_Number_Of_Scales`))return Number(e.get(`NI_Number_Of_Scales`));let t=0;for(let n of e.keys()){let e=/^NI_Scale\[(\d+)\]_Scale_Type$/.exec(n);e&&(t=Math.max(t,Number(e[1])+1))}return t}function mn(e,t,n){let r=e.find(e=>pn(e)>0);if(!r||r.get(`NI_Scaling_Status`)===`scaled`)return null;let i=(e,a)=>{if(e===fn||e<0||a>8)return e=>e;let o=r.get(`NI_Scale[${e}]_Scale_Type`);if(o===void 0)return e=>e;if(o===`Linear`){let t=Number(r.get(`NI_Scale[${e}]_Linear_Slope`)),n=Number(r.get(`NI_Scale[${e}]_Linear_Y_Intercept`)??0),o=i(Number(r.get(`NI_Scale[${e}]_Linear_Input_Source`)??fn),a+1);return o&&(e=>t*o(e)+n)}if(o===`Polynomial`){let o=Number(r.get(`NI_Scale[${e}]_Polynomial_Coefficients_Size`)??4);if(!(Number.isInteger(o)&&o>=0&&o<=64))return t.push(`${n}: an NI polynomial scale with ${o} coefficients is not supported; raw values are shown.`),null;let s=Array.from({length:o},(t,n)=>Number(r.get(`NI_Scale[${e}]_Polynomial_Coefficients[${n}]`))),c=i(Number(r.get(`NI_Scale[${e}]_Polynomial_Input_Source`)??fn),a+1);return c&&(e=>{let t=c(e),n=0;for(let e=o-1;e>=0;e--)n=n*t+s[e];return n})}return t.push(`${n}: NI scale type ${String(o)} is not supported; raw values are shown.`),null};return i(pn(r)-1,0)}var hn={1:Int8Array,2:Int16Array,3:Int32Array,5:Uint8Array,6:Uint16Array,7:Uint32Array,9:Float32Array,10:Float64Array,25:Float32Array,26:Float64Array,33:Uint8Array};function gn(e,t,n,r,i,a,o,s){let c=hn[e];if(c&&a&&r===W[e]&&(t.byteOffset+n)%r===0){o.set(new c(t.buffer,t.byteOffset+n,i),s);return}switch(e){case 1:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getInt8(n);return;case 2:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getInt16(n,a);return;case 3:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getInt32(n,a);return;case 4:for(let e=0;e<i;e++,n+=r)o[s+e]=b(t,n,a);return;case 5:case 33:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getUint8(n);return;case 6:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getUint16(n,a);return;case 7:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getUint32(n,a);return;case 8:for(let e=0;e<i;e++,n+=r)o[s+e]=x(t,n,a);return;case 9:case 25:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getFloat32(n,a);return;case 10:case 26:for(let e=0;e<i;e++,n+=r)o[s+e]=t.getFloat64(n,a);return;case U:for(let e=0;e<i;e++,n+=r)o[s+e]=ln(t,n,a);return;default:o.fill(NaN,s,s+i)}}async function _n(t,n,r){let i=new Map,a=[],o=[],s=new Map,c=new Map,l=n??t,u=0,d=0;for(;u+28<=t.size&&d+28<=l.size;){let f=n?d:u,p=await l.read(f,28),h=e(p);if(!(p[0]===84&&p[1]===68&&p[2]===83&&(p[3]===109||p[3]===104)))throw Error(`This TDMS file is damaged (no segment lead-in where one should be).`);let g=h.getUint32(4,!0),_=!(g&nn),v=x(h,12,_),y=x(h,20,_),b=v>=2**53?t.size:Math.min(t.size,u+28+v);if(y>b-u-28)throw Error(`This TDMS file is damaged (segment metadata longer than its segment).`);if(g&Qt){let t=await l.read(f+28,y),n=e(t);g&en&&(o=[],s=new Map);let a=0,u=n.getUint32(a,_);a+=4;for(let e=0;e<u;e++){let e=n.getUint32(a,_),l=m(t.subarray(a+4,a+4+e));a+=4+e;let u=dn(l),d=i.get(l);d||i.set(l,d={path:l,group:u?.[0]??``,name:u?.[1]??``,type:0,props:new Map,pieces:[],total:0});let f=n.getUint32(a,_);a+=4;let p=null;if(f===4294967295)p=null;else if(f===0)p=c.get(d)??null;else if(f===rn||f===an){let e=x(n,a+8,_),t=n.getUint32(a+16,_),i=f===rn?20:17,o=a+20,s=on[n.getUint32(o,_)]??0,c=n.getUint32(o+8,_);a=o+t*i;let l=n.getUint32(a,_);a+=4;let u=0;for(let e=0;e<l;e++)u+=n.getUint32(a+4*e,_);a+=4*l,f===an?r.push(`${d.name}: DAQmx digital line data is not supported; skipped.`):(t>1&&r.push(`${d.name}: only the first of its ${t} DAQmx scalers is read.`),p={type:s,count:e,bytes:0,daqmx:{byteOffset:c,stride:u}})}else{let e=n.getUint32(a,_),t=x(n,a+8,_),r=e===H?x(n,a+16,_):t*(W[e]??0);a+=f-4,p={type:e,count:t,bytes:r,daqmx:null}}p&&(d.type=p.type,c.set(d,p));let h=n.getUint32(a,_);a+=4;for(let e=0;e<h;e++){let e=n.getUint32(a,_),r=m(t.subarray(a+4,a+4+e));a+=4+e;let i=n.getUint32(a,_);a+=4;let[o,s]=un(n,t,a,i,_);d.props.set(r,o),a+=s}let g=s.get(d);g===void 0?s.set(d,o.push({obj:d,idx:p})-1):o[g].idx=p}}if(g&$t){let e=u+28+y,t=o.filter(e=>e.idx&&e.idx.count>0),n=t.filter(e=>e.idx.daqmx),r=(g&tn)!==0,i=0,s=0;n.length?(s=n[0].idx.daqmx.stride,i=n[0].idx.count*s):r?(s=t.reduce((e,t)=>e+(W[t.idx.type]??0),0),i=(t[0]?.idx.count??0)*s):i=t.reduce((e,t)=>e+t.idx.bytes,0);let c=i>0?Math.floor((b-e)/i):0,l=a.push({rawStart:e,chunkSize:i,chunks:c,le:_})-1,d=0;for(let e of t){let t=e.idx,n=W[t.type]??0,a=t.daqmx?{seg:l,count:t.count,offset:t.daqmx.byteOffset,bytes:0,stride:s,type:t.type}:{seg:l,count:t.count,offset:d,bytes:t.bytes,stride:r?s:n,type:t.type};t.daqmx||(d+=r?n:t.bytes),a.type===H?a.count=Math.min(a.count,Math.floor(a.bytes/4)):n>0&&(a.count=Math.min(a.count,a.stride>0?Math.max(0,Math.floor((i-a.offset-n)/a.stride)+1):0)),e.obj.pieces.push(a),c&&(e.obj.total+=a.count*c)}}if(u=b,d+=28+y,b>=t.size)break}return{objs:[...i.values()],segments:a}}var vn={...g,async plan(t,n){let r=t.companions.find(e=>e.name.toLowerCase().endsWith(`.tdms_index`)),i=[],{objs:a,segments:o}=await _n(n,r?new u(r):null,i),s=a.find(e=>e.path===`/`)?.props,c=a.filter(e=>e.name&&e.total>0&&(e.type===H||W[e.type])),l=new Set(c);for(let e of a)e.name&&e.total>0&&!l.has(e)&&i.push(`${e.name}: TDMS data type ${e.type} is not supported; skipped.`);let d=c.map(e=>e.props.get(`wf_start_time`)).filter(e=>typeof e==`number`),f=d.reduce((e,t)=>Math.min(e,t),d.length?1/0:0),p=new Map;for(let e of c){let t=`${e.total}|${e.props.get(`wf_increment`)??``}`,n=p.get(e.group)??new Map,r=n.get(t);r?r.push(e):n.set(t,[e]),p.set(e.group,n)}let m=[],h=[];for(let[e,t]of p)for(let n of t.values()){let r=n[0].props.get(`wf_increment`),i=typeof r==`number`&&r>0?1/r:null,a=i?null:n.find(e=>e.type===U)??n.find(e=>e.type!==H&&/^(time|timestamp|t)$/i.test(e.name))??null,o=n.filter(e=>e!==a),s=n[0].props.get(`wf_start_time`),c=i?Number(n[0].props.get(`wf_start_offset`)??0)+(typeof s==`number`?s-f:0):0;m.push({name:t.size===1?e:`${e} (${i?+i.toFixed(6):n[0].total} ${i?`Hz`:`samples`})`,channels:o.map(e=>({name:e.name,unit:String(e.props.get(`unit_string`)??``),text:e.type===H})),samples:n[0].total,time:i?{kind:`rate`,rate:i,start:c}:a?{kind:`timestamps`}:{kind:`index`},rateHz:i}),h.push({channels:o,time:a,origin:0})}if(m.length===0)throw Error(`This TDMS file has no channels with data.`);let g=[];for(let t of h){if(!t.time)continue;let r=t.time.pieces[0],i=o[r.seg],a=await n.read(i.rawStart+r.offset,W[r.type]??8),s=new Float64Array(1);gn(r.type,e(a),0,0,1,i.le,s,0),g.push(s[0])}let _=g.reduce((e,t)=>Math.min(e,t),g.length?1/0:0);for(let e of h)e.origin=_;let v=new Date(Date.UTC(1904,0,1)+f*1e3),y=f&&Number.isFinite(v.getTime())?v.toISOString():null,b={objs:a,segments:o,groups:h},x={};for(let[e,t]of s??[])typeof t==`string`&&(x[e]=t);return{format:`TDMS`,startTime:y,groups:m,meta:x,events:[],notes:i,state:b}},async*stream(e){for(let t of e.selection)yield*bn(e,t.group)},streamGroup:(e,t)=>bn(e,t)};function yn(e,t,n,r,i,a){let o=0;for(;o<i&&r.pi<t.pieces.length;){let s=t.pieces[r.pi],c=e.segments[s.seg];if(r.chunk>=c.chunks){r.pi++,r.chunk=0,r.row=0;continue}let l=Math.min(i-o,s.count-r.row),u=c.rawStart+r.chunk*c.chunkSize+s.offset;if(s.type===H)a.push({ch:n,at:u,end:u+s.bytes,rows:l,first:r.row,out:o,piece:s,le:c.le,range:0});else{let e=u+r.row*s.stride;a.push({ch:n,at:e,end:e+(l-1)*s.stride+W[s.type],rows:l,first:r.row,out:o,piece:s,le:c.le,range:0})}o+=l,r.row+=l,r.row>=s.count&&(r.chunk++,r.row=0)}}async function*bn(t,n){let r=t.plan.state,i=r.groups[n],a=T(t,n).map(e=>i.channels[e]),o=i.time?[i.time,...a]:a;if(!o.length)return;let s=new Map(r.objs.map(e=>[e.path,e.props])),c=e=>s.get(e)??new Map,l=c(`/`),u=o.map(e=>mn([e.props,c(`/'${e.group.replace(/'/g,`''`)}'`),l],[],e.name)),d=o.map(()=>({pi:0,chunk:0,row:0})),f=o.reduce((e,t)=>Math.min(e,t.total),1/0);for(let a=0;a<f;){t.signal?.throwIfAborted();let s=Math.min(sn,f-a),c=[];o.forEach((e,t)=>yn(r,e,t,d[t],s,c));let l=c.slice().sort((e,t)=>e.at-t.at),p=[];for(let e of l){let t=p[p.length-1];t&&e.at<=t.end+cn&&Math.max(t.end,e.end)-t.at<=16777216?t.end=Math.max(t.end,e.end):p.push({at:e.at,end:e.end}),e.range=p.length-1}let h=[];for(let e of p)h.push(await t.io.slice(e.at,e.end-e.at));let g=h.map(e),_=o.map(e=>e.type===H?Array(s).fill(null):new Float64Array(s).fill(NaN));for(let e of c){let t=e.at-p[e.range].at,n=e.piece;if(n.type===H){let r=h[e.range],i=g[e.range],a=t+4*n.count,o=_[e.ch];for(let n=0;n<e.rows;n++){let s=e.first+n,c=s===0?0:i.getUint32(t+4*(s-1),e.le),l=i.getUint32(t+4*s,e.le);o[e.out+n]=m(r.subarray(a+c,a+l))}}else gn(n.type,g[e.range],t,n.stride,e.rows,e.le,_[e.ch],e.out)}for(let e=0;e<o.length;e++){let t=u[e],n=_[e];if(t&&n instanceof Float64Array)for(let e=0;e<s;e++)n[e]=t(n[e])}let v=null;if(i.time){v=_.shift();for(let e=0;e<s;e++)v[e]-=i.origin}yield{group:n,time:v,start:a,length:s,columns:_},a+=s}}var xn=e=>!!e&&typeof e.keys==`function`,Sn=1e5;function Cn(){let e=new Set,t=0,n=!1;return{enter(t){let r=null;try{r=t.create_reference?Array.prototype.join.call(t.create_reference().ref_data,`,`):null}catch{r=null}return r===null?!0:e.has(r)?(n=!0,!1):(e.add(r),!0)},count:()=>++t<=Sn,note:()=>t>1e5?`This file has more than ${Sn.toLocaleString(`en-US`)} objects (or links that repeat its groups); only the first were read.`:n?`Some groups of this file link back to a group already read; each was read once.`:null}}function wn(e,t,n,r){return t.reduce((e,t)=>e*t,1)*Math.max(1,e.metadata.size||1)>(r?n*1032+1024:n)}var Tn=e=>!!e&&typeof e.slice==`function`,En=null,G=null,Dn=new WeakMap;function On(){!En||G||(G=En(),G.catch(()=>{G=null}))}function kn(e){if(!En)return Promise.reject(Error(`HDF5 and MAT 7.3 files are read in the import worker, and this environment has no workers.`));On();let t=Dn.get(e);return t||(t=G.then(t=>t(e)).then(e=>({root:e.root,close:()=>{}})),Dn.set(e,t),t.catch(()=>Dn.delete(e))),t}var An=new WeakMap;function K(e,t,n){let r=An.get(e);r||An.set(e,r=new Map);let i=r.get(t);return i||r.set(t,i=n??e.get(t)),i}function q(e){return e instanceof Float64Array?e:e instanceof BigInt64Array||e instanceof BigUint64Array?Float64Array.from(e,e=>Number(e)):ArrayBuffer.isView(e)?new Float64Array(e):Float64Array.from(e)}var jn={s:1,sec:1,second:1,seconds:1,ms:.001,msec:.001,us:1e-6,µs:1e-6,ns:1e-9};function J(e,...t){for(let n of t){let t=e[n]?.value;if(typeof t==`string`)return t;if(Array.isArray(t)&&typeof t[0]==`string`)return t[0]}return``}function Mn(e,...t){for(let n of t){let t=e[n]?.value,r=typeof t==`number`?t:ArrayBuffer.isView(t)?Number(t[0]):NaN;if(Number.isFinite(r))return r}return null}var Nn=0,Pn=1,Fn=3,In=6,Ln=new Set([1,2,3,4,5,6]),Rn=/^(time|t|timestamps?|.*[_ ]time|time[_ ].*)$/i,zn=[`sampling_rate`,`sample_rate`,`samplerate`,`sampleRate`,`fs`,`Fs`,`rate`];function Bn(e,t,n,r,i,a,o=0){i.set(n,t.attrs);for(let s of t.keys()){if(!a.count())return;let c=n===`/`?`/${s}`:`${n}/${s}`,l=t.get(s);if(xn(l))o<16&&a.enter(l)&&Bn(e,l,c,r,i,a,o+1);else if(Tn(l)){let t=K(e,c,l);r.push({path:c,parent:n,name:s,ds:t,attrs:t.attrs})}}}var Y=e=>e===`/`?`root`:e.slice(1),Vn=e=>Math.max(4096,Math.min(1<<20,Math.floor((8<<20)/(8*Math.max(1,e)))));async function Hn(e,t){let n=K(e,t.path);if(t.col===null)return q(n.slice([[0,1]]))[0]*t.factor;let r=n.slice([[0,1]]);return Number(r[0][t.col])*t.factor}var Un={..._,async plan(e){let t=await kn(e.main);try{let n=[],r=new Map,i=Cn();i.enter(t.root),Bn(t.root,t.root,`/`,n,r,i);let a=[],o=i.note();o&&a.push(o);let s=new Map,c=[],l=0;for(let t of n){let n=t.ds.metadata.shape??[],r=t.ds.metadata.type,i=t.ds.metadata.chunks?t.ds.filters??[]:[],o=i.find(e=>!Ln.has(e.id)),u=!o;if(u&&i.length>0&&n.length>0&&n.every(e=>e>0))try{t.ds.slice(n.map(()=>[0,1]))}catch{u=!1}if(!u){a.push(`${t.path}: compressed with a filter Kymoscope cannot read${o?` (${o.name||o.id})`:``}; skipped.`);continue}if(wn(t.ds,n,e.main.size,i.length>0)){a.push(`${t.path}: larger than the file can hold (never written, or damaged); skipped.`);continue}if(n.length===1&&n[0]>1&&(r===Nn||r===Pn||r===Fn)){let e=`${t.parent}|${n[0]}`,r=s.get(e)??{parent:t.parent,n:n[0],leaves:[]};r.leaves.push(t),s.set(e,r)}else if(n.length===2&&n[0]>1&&n[1]<=4096&&(r===Nn||r===Pn)){let e=Mn(t.attrs,...zn);c.push({name:Y(t.path),kind:`matrix`,rows:n[0],rate:e,time:null,origin:0,cols:Array.from({length:n[1]},(e,n)=>({path:t.path,col:n,text:!1,factor:1}))})}else if(n.length===1&&r===In){let e=(t.ds.metadata.compound_type?.members??[]).flatMap((e,n)=>e.type===Nn||e.type===Pn?[{path:t.path,col:n,text:!1,factor:1,name:e.name}]:[]),r=e.find(e=>Rn.test(e.name));c.push({name:Y(t.path),kind:`compound`,rows:n[0],rate:null,origin:0,time:r??null,cols:e.filter(e=>e!==r)})}else l++}for(let e of s.values()){let t=[...s.values()].filter(t=>t.parent===e.parent).length>1,n=e.leaves.find(e=>J(e.attrs,`CLASS`)===`DIMENSION_SCALE`)??e.leaves.find(e=>Rn.test(e.name)&&e.ds.metadata.type!==Fn),i=n?jn[J(n.attrs,`units`,`unit`,`Units`)]??1:1,a=n?null:Mn(r.get(e.parent)??{},...zn);c.push({name:t?`${Y(e.parent)} (${e.n} rows)`:Y(e.parent),kind:`vectors`,rows:e.n,rate:a,origin:0,time:n?{path:n.path,col:null,text:!1,factor:i}:null,cols:e.leaves.filter(e=>e!==n).map(e=>({path:e.path,col:null,text:e.ds.metadata.type===Fn,factor:1}))})}l&&a.push(`${l} dataset${l===1?``:`s`} with other shapes or types (images, references, 3-D) skipped.`),c.sort((e,t)=>e.name.localeCompare(t.name));let u=c.map((e,t)=>e.time||e.rate?t:-1).filter(e=>e>=0),d=1/0;for(let e of c)e.time&&(d=Math.min(d,await Hn(t.root,e.time)));for(let e of c)e.origin=Number.isFinite(d)?d:0;let f=new Map(n.map(e=>[e.path,e]));for(let e of c)e.chunk=[...e.cols,...e.time?[e.time]:[]].reduce((e,t)=>Math.max(e,f.get(t.path).ds.metadata.chunks?.[0]??0),0);let p=e=>J(f.get(e.path).attrs,`units`,`unit`,`Units`,`UNITS`),m=(e,t)=>e.kind===`matrix`?`${e.name.split(`/`).pop()}[${t.col+1}]`:e.kind===`compound`?f.get(t.path).ds.metadata.compound_type.members[t.col].name:t.path.split(`/`).pop(),h=c.map(e=>({name:e.name,channels:e.cols.map(t=>({name:m(e,t),unit:e.kind===`vectors`?p(t):``,text:t.text})),samples:e.rows,time:e.time?{kind:`timestamps`}:e.rate?{kind:`rate`,rate:e.rate,start:0}:{kind:`index`},rateHz:e.rate}));if(h.length===0)throw Error(`This HDF5 file has no datasets Kymoscope can plot.`);let g=h.length>20||h.length>1&&u.length<h.length,_=g?u.length?u:[c.reduce((e,t,n)=>t.rows*t.cols.length>c[e].rows*c[e].cols.length?n:e,0)]:void 0;return g&&a.push(`Only the groups with a time base are read unless you choose others in the channel picker.`),{format:`HDF5`,startTime:null,groups:h,meta:{},events:[],notes:a,pick:g,defaultGroups:_,state:{groups:c}}}finally{t.close()}},async*stream(e){for(let t of e.selection)yield*Wn(e,t.group)},streamGroup:(e,t)=>Wn(e,t)};async function*Wn(e,t){let n=e.plan.state.groups[t],r=T(e,t).map(e=>n.cols[e]),i=await kn(e.files.main);try{let a=n.chunk??0,o=a>0?Math.max(1,Math.min(Math.floor((1<<20)/a),Math.ceil(Vn(r.length+1)/a)))*a:Vn(r.length+1);for(let a=0;a<n.rows;a+=o){e.signal?.throwIfAborted();let s=Math.min(n.rows,a+o),c=s-a,l=null,u=e=>{let t=K(i.root,e.path);if(n.kind===`vectors`){let n=t.slice([[a,s]]);if(e.text)return Array.from(n,e=>e==null?null:String(e));let r=q(n);if(e.factor!==1)for(let t=0;t<c;t++)r[t]*=e.factor;return r}let r=new Float64Array(c);if(n.kind===`matrix`){let n=(t.shape??[0,1])[1],i=l??=t.slice([[a,s],[0,n]]);for(let t=0;t<c;t++)r[t]=Number(i[t*n+e.col])}else{let n=l??=t.slice([[a,s]]);for(let t=0;t<c;t++)r[t]=Number(n[t][e.col])*e.factor}return r},d=null;if(n.time){d=u(n.time);for(let e=0;e<c;e++)d[e]-=n.origin}yield{group:t,time:d,start:a,length:c,columns:r.map(u)}}}finally{i.close()}}var Gn=/^(time|t|tout|timestamps?)$/i,Kn=/^(fs|sample_?rate|sampling_?rate|rate|samplerate)$/i,qn=e=>e.slice(e.lastIndexOf(`.`)+1),Jn=e=>e.includes(`.`)?e.slice(0,e.lastIndexOf(`.`)):``;function Yn(e,t){let n=/^(.*)\.values(\[\d+\])?$/.exec(e.name),r=n&&(t.get(`${n[1]}.label`)||t.get(`${n[1]}.blockName`));return r?{...e,name:`${r}${n[2]??``}`}:e}function Xn(e,t,n){let r=new Map;for(let t of e){if(!(t.length>1))continue;let e=r.get(t.length);e?e.push(t):r.set(t.length,[t])}let i=[...n].filter(([e])=>Kn.test(qn(e))),a=i.length===1?i[0][1]:null,o=[];for(let[e,n]of r){let r=n.find(e=>Gn.test(qn(e.name))||/(_time|\.Time)$/.test(e.name))??null;o.push({name:r?Jn(r.name)||r.name:`${e} samples`,channels:n.filter(e=>e!==r).map(e=>Yn(e,t)),time:r,rate:r?null:a,origin:0})}return o}function Zn(e){return e.map(e=>({name:e.name,channels:e.channels.map(e=>({name:e.name,unit:``})),samples:e.channels[0]?.length??e.time?.length??0,time:e.time?{kind:`timestamps`}:e.rate?{kind:`rate`,rate:e.rate,start:0}:{kind:`index`},rateHz:e.rate}))}async function Qn(e){let t=1/0;for(let n of e)n.time&&(t=Math.min(t,(await n.time.read(0,1))[0]));for(let n of e)n.origin=Number.isFinite(t)?t:0}var $n=class{#e;size;constructor(e){this.#e=e,this.size=e.size}read(e,t){return t>=65536?this.#e.slice(e,t):this.#e.read(e,t)}release(){}},er=class{#e;#t;#n;#r=null;keep=!0;size;constructor(e,t,n,r){this.#e=e,this.#t=t,this.#n=n,this.#r=r,this.size=r.length}async read(e,t){return this.#r??=await P(await this.#e.slice(this.#t,this.#n),this.size,this.size),this.#r.subarray(e,e+t)}release(){this.keep||(this.#r=null)}},X={1:1,2:1,3:2,4:2,5:4,6:4,7:4,9:8,12:8,13:8};function tr(t,n,r,i,a=1){let o=e(n),s=i.length,c=X[t]*a;if(t===9&&a===1&&r){i.set(n.byteOffset%8==0?new Float64Array(n.buffer,n.byteOffset,s):new Float64Array(n.slice(0,s*8).buffer));return}switch(t){case 1:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getInt8(t);return;case 2:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getUint8(t);return;case 3:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getInt16(t,r);return;case 4:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getUint16(t,r);return;case 5:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getInt32(t,r);return;case 6:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getUint32(t,r);return;case 7:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getFloat32(t,r);return;case 9:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getFloat64(t,r);return;case 12:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getInt32(t+(r?4:0),r)*4294967296+o.getUint32(t+(r?0:4),r);return;case 13:for(let e=0,t=0;e<s;e++,t+=c)i[e]=o.getUint32(t+(r?4:0),r)*4294967296+o.getUint32(t+(r?0:4),r);return;default:i.fill(NaN)}}async function Z(t,n,r){let i=e(await t.read(n,8)),a=i.getUint32(0,r);if(a>>>16)return{type:a&65535,bytes:a>>>16,data:n+4,next:n+8};let o=i.getUint32(4,r);return{type:a,bytes:o,data:n+8,next:a===15?n+8+o:n+8+o+(8-o%8)%8}}function nr(e,t,n,r,i){let[a,o]=t,s=X[r.type];if(!s)return[];let c=(e,t)=>async(a,o)=>{let c=new Float64Array(o-a);return t===1?tr(r.type,await n.read(r.data+(e+a)*s,(o-a)*s),i,c):tr(r.type,await n.read(r.data+(e+a*t)*s,((o-a-1)*t+1)*s),i,c,t),c};return a===1||o===1?[{name:e,length:a*o,read:c(0,1)}]:a>=o?Array.from({length:o},(t,n)=>({name:`${e}[${n+1}]`,length:a,read:c(n*a,1)})):Array.from({length:a},(t,n)=>({name:`${e}[${n+1}]`,length:o,read:c(n,a)}))}async function rr(t,n,r,i,a,o,c){let l=n,u=await Z(t,l,r),d=e(await t.read(u.data,8)).getUint32(0,r),f=d&255;l=u.next;let p=await Z(t,l,r);if(p.bytes>128){o.notes.push(`A MAT variable with more than 32 dimensions was skipped (damaged).`);return}let h=e(await t.read(p.data,p.bytes)),g=Array.from({length:p.bytes/4},(e,t)=>h.getInt32(4*t,r));l=p.next;let _=await Z(t,l,r),v=w(await t.read(_.data,_.bytes));l=_.next;let y=a??v,b=i?`${i}.${y}`:y,x=g.reduce((e,t)=>e*t,1);if(f>=6&&f<=15){if(x===0)return;let e=await Z(t,l,r);if(d&2048&&o.notes.push(`${b}: the imaginary part was skipped.`),g.length>2){o.notes.push(`${b}: arrays with more than two dimensions are skipped.`);return}if(!X[e.type])return;if(!(x>0)||x*X[e.type]>e.bytes||e.data+e.bytes>t.size){o.notes.push(`${b}: its data is shorter than its dimensions (damaged); skipped.`);return}if(x===1){let n=new Float64Array(1);tr(e.type,await t.read(e.data,X[e.type]??8),r,n),o.scalars.set(b,n[0]);return}o.channels.push(...nr(b,g,t,e,r));return}if(f===4){let e=await Z(t,l,r);if(e.bytes>ir)return;let n=await t.read(e.data,e.bytes),i=e.type===3||e.type===4||e.type===17?new TextDecoder(r?`utf-16le`:`utf-16be`).decode(n.subarray(0,n.length&-2)):m(n);g[0]<=1&&o.strings.set(b,i);return}if(f===2){let n=await Z(t,l,r),i=e(await t.read(n.data,4)).getInt32(0,r);l=n.next;let a=await Z(t,l,r);if(!(i>0))return;let u=await t.read(a.data,a.bytes),d=Array.from({length:Math.floor(u.length/i)},(e,t)=>s(u.subarray(t*i,(t+1)*i)));if(l=a.next,d.length===0||x*d.length*8>t.size-l){x>0&&d.length>0&&o.notes.push(`${b}: its fields are longer than the file (damaged); skipped.`);return}for(let e=0;e<x;e++){let n=x>1?`${b}(${e+1})`:b;for(let e of d){let i=await Z(t,l,r);i.type===14&&i.bytes>0&&c<16&&await rr(t,i.data,r,n,e,o,c+1),l=i.next}}return}f===1?o.notes.push(`${b}: cell arrays are skipped.`):f===5?o.notes.push(`${b}: sparse arrays are skipped.`):o.notes.includes(ar)||o.notes.push(ar)}var ir=1<<17,ar=`MATLAB objects (timeseries, SimulationOutput) were skipped: save them as 'Structure with time' or as structs.`,or=512<<20;async function sr(t){let n=w((await t.read(0,128)).subarray(126,128))===`IM`,r={channels:[],strings:new Map,scalars:new Map,notes:[],sources:[]},i=new $n(t),a=0;for(let o=128;o+8<=t.size;){let s=await Z(i,o,n);if(s.type===15){if(s.bytes>1<<30)throw Error(`A compressed variable in this MAT file is over 1 GB; save it with -v7.3 to open it here.`);let i=await t.slice(s.data,s.bytes),o=await P(i,-1,8),c=o.length<8?0:8+e(o).getUint32(4,n);if(c>oe(s.bytes))throw Error(`A compressed variable in this MAT file is damaged.`);if(c>1<<30)throw Error(`A compressed variable in this MAT file is over 1 GB; save it with -v7.3 to open it here.`);let l=await P(i,c,c),u=new er(t,s.data,s.bytes,l);r.sources.push(u),a+=l.length;let d=await Z(u,0,n);if(d.type===14&&await rr(u,d.data,n,``,null,r,0),a>or)for(let e of r.sources)e.keep=!1,e.release()}else s.type===14&&await rr(i,s.data,n,``,null,r,0);o=s.next}return r}var cr=65536,lr=new Set([`double`,`single`,`int8`,`uint8`,`int16`,`uint16`,`int32`,`uint32`,`int64`,`uint64`,`logical`]);function ur(e,t){let n={channels:[],strings:new Map,scalars:new Map,notes:[]},r=Cn();r.enter(e);let i=t=>K(e,t),a=(e,t,n)=>{let r=null;return(a,o)=>((r?.s!==a||r.e!==o)&&(r={s:a,e:o,v:q(t?i(e).slice([[0,n],[a,o]]):i(e).slice([[a,o],[0,n]]))}),r.v)},o=(e,s,c,l)=>{for(let u of e.keys()){if(!r.count())return;if(u.startsWith(`#`))continue;let d=e.get(u);if(!d)continue;let f=`${s}/${u}`,p=c?`${c}.${u}`:u,m=d.attrs,h=J(m,`MATLAB_class`);if(m.MATLAB_empty)continue;if(xn(d)){h===`struct`&&l<16&&r.enter(d)&&o(d,f,p,l+1);continue}if(!Tn(d))continue;let g=d.shape??[];if(h===`char`){if(g.reduce((e,t)=>e*t,1)>cr)continue;let e=q(d.slice(g.map(e=>[0,e])));n.strings.set(p,new TextDecoder(`utf-16le`).decode(Uint16Array.from(e)))}else if(lr.has(h)&&g.length===2&&wn(d,g,t,!!d.metadata.chunks))n.notes.push(`${p}: larger than the file can hold (never written, or damaged); skipped.`);else if(lr.has(h)&&g.length===2){let[e,t]=g;if(e*t===1){n.scalars.set(p,q(i(f).slice([[0,1],[0,1]]))[0]);continue}if(e===1||t===1){let r=e*t;n.channels.push({name:p,length:r,read:async(t,n)=>q(e===1?i(f).slice([[0,1],[t,n]]):i(f).slice([[t,n],[0,1]]))})}else if(t>=e){let r=a(f,!0,e);for(let i=0;i<e;i++)n.channels.push({name:`${p}[${i+1}]`,length:t,read:async(e,t)=>r(e,t).slice(i*(t-e),(i+1)*(t-e))})}else{let r=a(f,!1,t);for(let i=0;i<t;i++)n.channels.push({name:`${p}[${i+1}]`,length:e,read:async(e,n)=>{let a=r(e,n),o=new Float64Array(n-e);for(let e=0;e<o.length;e++)o[e]=a[e*t+i];return o}})}}else h===`cell`?n.notes.push(`${p}: cell arrays are skipped.`):h&&n.notes.push(`${p}: MATLAB class ${h} is skipped.`)}};o(e,``,``,0);let s=r.note();return s&&n.notes.push(s),n}var dr=e=>Math.max(4096,Math.min(1<<18,Math.floor((16<<20)/(8*Math.max(1,e))))),fr={...o,async plan(e,t){let n=y(await t.read(0,20),`MATLAB 7.3`),r,i=[];if(n)r=ur((await kn(e.main)).root,e.main.size);else{let e=await sr(t);i=e.sources,r=e}let a=Xn(r.channels,r.strings,r.scalars).filter(e=>e.channels.length>0);await Qn(a);let o=Zn(a);if(o.length===0)throw Error(`This MAT file has no numeric vectors or matrices to plot.`);let s=o.flatMap((e,t)=>e.time.kind===`index`?[]:[t]),c=o.length>1&&s.length<o.length,l=Object.fromEntries(r.strings);for(let[e,t]of r.scalars)l[e]=String(t);return{format:n?`MAT 7.3`:`MAT 5`,startTime:null,groups:o,meta:l,events:[],notes:r.notes,pick:c,defaultGroups:c&&s.length?s:void 0,state:{groups:a,sources:i}}},async*stream(e){for(let t of e.selection)yield*pr(e,t.group)},streamGroup:(e,t)=>pr(e,t)};async function*pr(e,t){let n=e.plan.state,r=n.groups[t],i=T(e,t).map(e=>r.channels[e]),a=r.channels[0].length,o=dr(i.length+1);try{for(let n=0;n<a;n+=o){e.signal?.throwIfAborted();let s=Math.min(a,n+o),c=null;if(r.time){c=await r.time.read(n,s);for(let e=0;e<c.length;e++)c[e]-=r.origin}let l=[];for(let e of i)l.push(await e.read(n,s));yield{group:t,time:c,start:n,length:s-n,columns:l}}}finally{for(let e of n.sources)e.release()}}function mr(e,t){let n=/^\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*,\s*(\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))?/.exec(e??``);if(!n)return[null,0,!1];let[r,i]=t===1991?[n[2],n[1]]:[n[1],n[2]],a=n[3].length===2?2e3+Number(n[3]):Number(n[3]),o=Number(`${n[6]}.${n[7]??`0`}`);return[`${a}-${i.padStart(2,`0`)}-${r.padStart(2,`0`)}T${n[4].padStart(2,`0`)}:${n[5]}:${o.toFixed(3).padStart(6,`0`)}`,Date.UTC(a,Number(i)-1,Number(r),Number(n[4]),Number(n[5]))/1e3+o,(n[7]??``).length>6]}function hr(e){let t=e.split(/\r?\n/).map(e=>e.trim()),[n,,r]=t[0].split(`,`),i=Number(r)||1991,a=/(\d+)A\s*,\s*(\d+)D/i.exec(t[1]??``);if(!a)throw Error(`This COMTRADE .cfg is damaged (second line).`);let o=Number(a[1]),s=Number(a[2]);if(o+s>t.length)throw Error(`This COMTRADE .cfg is damaged (more channels than lines).`);let c=2,l=[];for(let e=0;e<o;e++,c++){let e=(t[c]??``).split(`,`);l.push({name:(e[1]??``).trim(),unit:(e[4]??``).trim(),a:Number(e[5]),b:Number(e[6]??0)||0})}let u=[];for(let e=0;e<s;e++,c++)u.push(((t[c]??``).split(`,`)[1]??``).trim());c++;let d=Number(t[c++]);if(!(Number.isInteger(d)&&d>=0&&d<=t.length-c))throw Error(`This COMTRADE .cfg is damaged (sample rates).`);let f=[];for(let e=0;e<Math.max(1,d);e++,c++){let[e,n]=(t[c]??``).split(`,`).map(Number);f.push([d===0?0:e,n])}let[p,m,h]=mr(t[c++],i),[g,_,v]=mr(t[c++],i),y=(t[c++]||`ASCII`).toUpperCase(),b=Number(t[c++])||1;return{station:n.trim(),rev:i,analog:l,digital:u,rates:f,start:p,trigger:g,startSec:m,triggerSec:_,ft:y,timemult:b,timebase:h||v?1e-9:1e-6}}var gr={BINARY:2,BINARY32:4,FLOAT32:4},_r=16<<20;async function vr(e){let t=w(await e.read(0,Math.min(e.size,1<<20))),n=t.search(/---\s*file type:\s*CFG\s*---\r?\n/i),r=/---\s*file type:\s*DAT\s+(ASCII|BINARY32|BINARY|FLOAT32)(?:\s*:\s*(\d+))?\s*---\r?\n/i.exec(t);if(n<0||!r)throw Error(`This COMTRADE .cff is damaged (no CFG or DAT section in its first megabyte).`);let i=t.indexOf(`
|
|
3
|
+
`,n)+1,a=t.slice(i).search(/---\s*file type:\s*(INF|HDR|DAT)/i),o=t.slice(i,a<0?void 0:i+a),s=r.index+r[0].length;return{cfg:o,offset:s,length:r[2]?Number(r[2]):e.size-s}}function yr(e,t,n,r){let i=0,a=0,o=t,s=0;for(let[t,c]of e){for(;o<c&&s<n;)r[s++]=a+(o-i)/t,o++;if(a+=(c-i)/t,i=c,s>=n)return}let c=e[e.length-1];for(;s<n;s++,o++)r[s]=a+(o-i)/c[0]}var br=e=>e.companions.find(e=>e.name.toLowerCase().endsWith(`.dat`)),xr={...t,async plan(e,t){let n=e.main.name.toLowerCase().endsWith(`.cff`),r,i;if(n){let e=await vr(t);r=e.cfg,i={data:`self`,offset:e.offset,length:e.length}}else{let n=br(e);if(!n)throw Error(`This COMTRADE .cfg needs its .dat file: drop both together.`);if(t.size>_r)throw Error(`This .cfg is too large to be a COMTRADE configuration file.`);r=w(await t.read(0,t.size)),i={data:`companion`,offset:0,length:n.size}}let a=hr(r);if(a.ft!==`ASCII`&&!gr[a.ft])throw Error(`This COMTRADE file has data file type ${a.ft}, which Kymoscope cannot read.`);let o=Math.ceil(a.digital.length/16),s=a.ft===`ASCII`?0:8+a.analog.length*gr[a.ft]+2*o,c=s?Math.floor(i.length/s):i.length,l=Math.min(a.rates[a.rates.length-1][1]||(s?c:-1),c),u=a.rates.length===1&&a.rates[0][0]>0?a.rates[0][0]:null,d=[`Analog values are shown as recorded (primary or secondary, per the CFG).`];return{format:`COMTRADE ${a.rev} (${a.ft})`,startTime:a.start,meta:{station:a.station,trigger:a.trigger??``},groups:[{name:a.station||`COMTRADE`,channels:[...a.analog.map(e=>({name:e.name,unit:e.unit})),...a.digital.map(e=>({name:e,unit:``}))],samples:l,time:{kind:`timestamps`},rateHz:u}],events:a.start&&a.trigger?[{time:Math.round((a.triggerSec-a.startSec)*1e6)/1e6,text:`Trigger`}]:[],notes:d,state:{...i,cfg:a,samples:l,recordSize:s}}},stream:e=>Sr(e)};async function*Sr(t){let n=t.plan.state,{cfg:r}=n,i=n.data===`self`?t.io:new u(br(t.files)),a=T(t,0),o=r.analog.length,s=r.rates[0][0]>0,c=r.timemult*r.timebase,l=65536,d=0,p=(e,t,n)=>{let i=new Float64Array(e);if(s)yr(r.rates,d,e,i);else for(let n=0;n<e;n++)i[n]=t[n]*c;let a={group:0,time:i,start:d,length:e,columns:n};return d+=e,a};if(r.ft===`ASCII`){let e=r.rev>=1999?`99999`:``,s=a.map(e=>e<o?r.analog[e]:null),c=new Float64Array(l),u=a.map(()=>new Float64Array(l)),d=0,f=(t,n,r)=>{let i=t.slice(n,r).split(`,`);if(!(i.length<2)){c[d]=Number(i[1]);for(let t=0;t<a.length;t++){let n=(i[2+a[t]]??``).trim(),r=s[t];u[t][d]=n===``||r&&n===e?NaN:r?r.a*Number(n)+r.b:Number(n)}d++}},m=``,h=new TextDecoder(`latin1`);for await(let e of i.windows(n.offset,n.length)){t.signal?.throwIfAborted();let n=m+h.decode(e.bytes),r=0;for(let e=n.indexOf(`
|
|
4
|
+
`,r);e>=0;e=n.indexOf(`
|
|
5
|
+
`,r))f(n,r,e),r=e+1,d===l&&(yield p(d,c,u),c=new Float64Array(l),u=a.map(()=>new Float64Array(l)),d=0);m=n.slice(r)}m.trim()&&f(m,0,m.length),d&&(yield p(d,c.slice(0,d),u.map(e=>e.slice(0,d))));return}let m=n.recordSize,h=gr[r.ft],g=Math.max(1,Math.floor(Math.min(f,l*m)/m)),_=n.samples;for await(let c of i.windows(n.offset,Math.min(n.length,n.samples*m),g*m)){t.signal?.throwIfAborted();let n=Math.min(_,Math.floor(c.bytes.length/m));if(n<=0)break;let i=e(c.bytes),l=new Float64Array(n);if(!s)for(let e=0;e<n;e++)l[e]=i.getUint32(e*m+4,!0);yield p(n,l,a.map(e=>{let t=new Float64Array(n);if(e<o){let{a,b:o}=r.analog[e],s=8+e*h;if(r.ft===`BINARY`)for(let e=0,r=s;e<n;e++,r+=m){let n=i.getInt16(r,!0);t[e]=n===-32768?NaN:a*n+o}else if(r.ft===`BINARY32`)for(let e=0,r=s;e<n;e++,r+=m){let n=i.getInt32(r,!0);t[e]=n===-2147483648?NaN:a*n+o}else for(let e=0,r=s;e<n;e++,r+=m)t[e]=a*i.getFloat32(r,!0)+o}else{let r=e-o,a=8+o*h+2*(r>>4),s=1<<(r&15);for(let e=0,r=a;e<n;e++,r+=m)t[e]=i.getUint16(r,!0)&s?1:0}return t})),_-=n}}var Q=8192;async function*Cr(e){let t=0;for(;t+56<=e.size;){let n=te(await e.read(t,56));if(yield await e.slice(t,n.bytes),n.bytes<=56)return;t+=n.bytes}}function wr(e){let t=e.filter(e=>e&&typeof e.t==`number`&&Array.isArray(e.signals)),n={},r={};return t.forEach((e,i)=>{for(let a of e.signals){if(!a||typeof a.name!=`string`)continue;let e=n[a.name]??=new Float64Array(t.length).fill(NaN);e[i]=typeof a.value==`number`?a.value:NaN,a.unit&&!r[a.name]&&(r[a.name]=a.unit)}}),{t:t.map(e=>e.t),columns:n,units:r}}var Tr=4096;function Er(e){let t=2166136261^e.length;for(let n=0,r=Math.min(e.length,64);n<r;n++)t=Math.imul(t^e[n],16777619);return t>>>0}var Dr=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]),Or=class{known=new Map;real=new Set;pending=new Set;held=new Set;note(e){for(let t of e){let e=this.known.get(t.name);(!e||t.t>=e.t)&&this.known.set(t.name,{t:t.t,v:t.v,unit:t.unit}),!this.real.has(t.name)&&!this.held.has(t.name)&&this.pending.add(t.name)}}sampled(e){if(this.pending.size+this.held.size===0)return;let t=e=>{(this.pending.delete(e)||this.held.delete(e))&&this.real.add(e)};for(let[n,r]of Object.entries(e.columns))if(!(!this.pending.has(n)&&!this.held.has(n))){for(let e=0;e<r.length;e++)if(Number.isFinite(r[e])){t(n);break}}if(e.events&&e.schema){let{index:n,v:r}=e.events;for(let i=0;i<r.length;i++)Number.isFinite(r[i])&&t(e.schema.channels[n[i]].name)}}start(e){if(this.pending.size===0||e.t.length===0)return e;let t={...e.columns},n={...e.units};for(let e of this.pending){let r=this.known.get(e);t[e]=Float64Array.of(r.v),r.unit&&!n[e]&&(n[e]=r.unit),this.held.add(e)}let r=[...this.pending];return this.pending.clear(),{...e,columns:t,units:n,more:r}}end(e,t){if(e===null||this.held.size===0)return null;let n={},r={};for(let e of this.held){let t=this.known.get(e);n[e]=Float64Array.of(t.v),t.unit&&(r[e]=t.unit)}return{at:t,t:[e],columns:n,units:r}}};async function*$(e,t,a,o,s=!1){let c=ne(e.keys??[]),u=new Map,d=new Map,f=o?null:new Or,p=null,m=0,g=e=>f?(f.sampled(e),e.t.length&&(p=+e.t[e.t.length-1],m=e.at),f.start(e)):e,_={results:[]};for await(let e of Cr(t)){let{header:t,messages:p}=C(e);if(a.t0??=t.t0,s){let e=t.recordingId;_.results.push({rid:e,check:await S(t,p,c,_.rid===e?(_.seq??-1)+1:0)}),_.rid=e,_.seq=t.seq,a.chain=ee(_.results)}for(let e of p){let t=Er(e.bytes),s=d.get(t);if(s&&Dr(s,e.bytes))continue;d.set(t,e.bytes),d.size>Tr&&d.delete(d.keys().next().value);let p=await c(e.bytes);if(!p){a.lost++;continue}try{if(p.kind===5){let e=await ie(p.plaintext);u.set(e.id,e);continue}if(p.kind===4||p.kind===9){let t;if(p.kind===4){let r=await n(p.plaintext,u);if(!r){a.lost++;continue}t={at:e.at,t:r.block.t,columns:r.block.columns,units:r.block.units??{},historical:r.historical,schema:u.get(r.schemaId)}}else{let n=await i(p.plaintext,u);if(!n){a.lost++;continue}t={at:e.at,t:n.t,columns:{},units:{},historical:n.historical,events:{index:n.index,v:n.v},schema:u.get(n.schemaId)}}t.historical&&f?.sampled(t),(o===void 0||o===!!t.historical)&&(yield t.historical?t:g(t));continue}if(o)continue;if(p.kind===7){let e=await h(p.plaintext,u);e&&f?.note(e)}else if(p.kind===3){let t=r(p.plaintext);yield g({at:e.at,t:t.t,columns:t.columns,units:t.units??{}})}else if(p.kind===2){let t=l(p.plaintext);yield g({at:e.at,...wr(Array.isArray(t?.frames)?t.frames:[])})}}catch{a.lost++}}}let v=f?.end(p,m);v&&(yield v)}function kr(e,t,n){let r=e===null?t:t===null?e:Math.min(e,t);return r===null?{}:r>1e9?{wallOrigin:String(r),wallQuality:`exact`}:Number.isFinite(n)?{wallOrigin:String(r+n),wallQuality:`approx`}:{}}var Ar={mdf:It,edf:Vt,xdf:Zt,tdms:vn,hdf5:Un,mat:fr,comtrade:xr,krc:{id:`krc`,label:`Kymoscope recording`,extensions:[],sniff:()=>!1,async plan(e,t){let n=[d],r=new Map([[d,`s`]]),i={lost:0,t0:null},a=0,o=!1,s=null,c=null,l=0,u=-1/0,f=0,p=1/0,m=(e,t=``)=>{e!==`ElapsedTime`&&(r.has(e)?!r.get(e)&&t&&r.set(e,t):(n.push(e),r.set(e,t)))},h=new Set;for await(let n of $(e,t,i,void 0,!0)){a+=n.t.length,n.historical&&(o=!0);let e=n.t.length;if(e&&n.historical?c??=+n.t[0]:e&&(s??=+n.t[0],+n.t[0]+l<u&&(l=u+Math.max(0,(n.at-f)/1e3)-+n.t[0]),f=n.at,u=+n.t[e-1]+l,p=Math.min(p,n.at/1e3-u)),n.schema){if(!h.has(n.schema.id)){h.add(n.schema.id);for(let e of n.schema.channels)m(e.name,e.unit)}for(let e of n.more??[])m(e,n.units[e])}else for(let e of Object.keys(n.columns))m(e,n.units[e])}let g=n.map(e=>({name:e,unit:r.get(e)??``})),_=i.lost?[`${i.lost} ${i.lost===1?`message`:`messages`} couldn't be opened with the keys this browser has.`]:[],v=i.chain?.broken??0,y=n.length-1,b=y>1e3?[{group:0,channels:n.slice(0,ae+1).map((e,t)=>t)}]:void 0;return b&&_.push(`This recording has ${y.toLocaleString(`en-US`)} channels; the first ${ae.toLocaleString(`en-US`)} are loaded.`),v&&_.push(`Some of this recording was missing, out of order or changed where it is stored (${v} of ${v+(i.chain?.verified??0)} checked ${v+(i.chain?.verified??0)===1?`chunk`:`chunks`}).`),{format:`Kymoscope recording`,startTime:i.t0===null?null:new Date(i.t0).toISOString(),groups:[{name:`Recording`,channels:g,samples:a,time:{kind:`timestamps`},rateHz:null}],meta:{...kr(s,c,p),...i.chain&&(i.chain.verified||i.chain.broken)?{chainVerified:String(i.chain.verified),chainBroken:String(i.chain.broken),chainUnchecked:String(i.chain.unchecked)}:{}},events:[],notes:_,defaultSelection:b,state:{names:n,backlog:o}}},async*stream(e){let{names:t,backlog:n=!1}=e.plan.state,r=e.selection.find(e=>e.group===0)?.channels??[],i=new Map(r.map((e,n)=>[t[e],n])),a=i.get(d),o=()=>r.map(()=>new Float64Array(Q).fill(NaN)),s=new Float64Array(Q),c=o(),l=0,u=0,f=0,p=-1/0,m=0,h=null,g=()=>{let e={group:0,time:s.slice(0,l),start:u,length:l,columns:c.map(e=>e.slice(0,l))};return u+=l,l=0,s=new Float64Array(Q),c=o(),e},_=new WeakMap,v=e=>{let t=_.get(e);return t||_.set(e,t=Int32Array.from(e.channels,e=>e.name===`ElapsedTime`?-2:i.get(e.name)??-1)),t};function*y(e,t,n,r){let o=Object.entries(e.columns).flatMap(([e,t])=>{let n=e===`ElapsedTime`?void 0:i.get(e);return n===void 0?[]:[[n,+t[0]]]});for(let i=0;i<e.t.length;i++){let u=r[t[i]];if(u===-1&&!(i===0&&o.length))continue;let d=+e.t[i];if(s[l]=d+f-h,a!==void 0&&(c[a][l]=d),u>=0&&(c[u][l]=n[i]),i===0)for(let[e,t]of o)c[e][l]=t;++l===Q&&(yield g())}}function*b(e){let t=e.t.length;if(t&&(h??=+e.t[0]+f),e.events&&e.schema)return yield*y(e,e.events.index,e.events.v,v(e.schema));for(let n=0;n<t;){let r=Math.min(t-n,Q-l);for(let t=0;t<r;t++)s[l+t]=+e.t[n+t]+f-h;if(a!==void 0)for(let t=0;t<r;t++)c[a][l+t]=+e.t[n+t];for(let[t,a]of Object.entries(e.columns)){let e=t===`ElapsedTime`?void 0:i.get(t);if(e===void 0)continue;let o=c[e];if(a instanceof Float64Array||a instanceof Float32Array)o.set(a.subarray(n,n+r),l);else for(let e=0;e<r;e++)o[l+e]=+a[n+e]}l+=r,n+=r,l===Q&&(yield g())}}let x=n?$(e.files,e.io,{lost:0,t0:null},!0):null,S=async()=>{for(let e=await x?.next();e&&!e.done;e=await x.next())if(e.value.t.length)return e.value;return null},C=await S();for await(let t of $(e.files,e.io,{lost:0,t0:null},n?!1:void 0)){if(e.signal?.aborted)return;let n=t.t.length;if(n!==0){for(+t.t[0]+f<p&&(f=p+Math.max(0,(t.at-m)/1e3)-+t.t[0]),m=t.at;C&&+C.t[0]<+t.t[0];C=await S())yield*b(C);yield*b(t),p=+t.t[n-1]+f}}for(;C&&!e.signal?.aborted;C=await S())yield*b(C);l>0&&(yield g())}}};function jr(e){let t=Ar[e];if(!t)throw Error(`No importer for format ${e}.`);return t}export{jr as importerFor};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*! @kymoscope/sdk (c) 2026 Kymoscope. Proprietary software: use requires a license. See LICENSE.md. */
|
|
2
|
+
var e=Symbol(`row`),t=new WeakMap;function n(e){return t.get(e)}function r(e,t){let n=e.keys.lastIndexOf(t);return n<0?void 0:e.values[n]}var i=new WeakMap;function a(n,r){let a=n.keys.join(`
|
|
3
|
+
`),o=r&&i.get(r);if(o&&o.keys===a&&r.length===n.length){o.t=n;let e=new Proxy(o.base,{});return i.set(e,o),t.set(e,n),t.set(r,n),e}let s=Array(n.length),c={t:n,keys:a,base:s},l={};n.keys.forEach((t,n)=>{Object.defineProperty(l,t,{enumerable:!0,configurable:!0,get(){let t=this[e],{values:r,text:i}=c.t,a=r[n][t],o=i[n];return a===a?a:o?o[t]??null:null},set(e){Object.defineProperty(this,t,{value:e,writable:!0,enumerable:!0,configurable:!0})}})});for(let t=0;t<n.length;t++){let n=Object.create(l);n[e]=t,s[t]=n}return i.set(s,c),t.set(s,n),s}function o(e){let t=Array(e.length);for(let n=0;n<e.length;n++){let r={};for(let t=0;t<e.keys.length;t++){let i=e.values[t][n];r[e.keys[t]]=i===i?i:e.text[t]?.[n]??null}t[n]=r}return t}function s(e,t){let n=Number(t?e.replace(`,`,`.`):e);return Number.isFinite(n)?n:null}function c(e,t){let n=e.trim();if(n===``)return null;let r=s(n,t);return r===null?n:r}function l(e,t){if(t===`,`)return!1;let n=0,r=0;for(let i of e)for(let e of d(i,t)){let t=e.trim();/^-?\d+,\d+$/.test(t)?n++:/^-?\d+\.\d+$/.test(t)&&r++}return n>r}function u(e){let t=(e.match(/\t/g)??[]).length,n=(e.match(/;/g)??[]).length,r=0,i=!1;for(let t=0;t<e.length;t++){let n=e[t];n===`"`?i=!i:!i&&n===`,`&&r++}return t>0&&t>=r&&t>=n?` `:n>r?`;`:r>0?`,`:` `}function d(e,t){if(t===` `)return e.split(` `);let n=[],r=``,i=!1;for(let a=0;a<e.length;a++){let o=e[a];i?o===`"`?e[a+1]===`"`?(r+=`"`,a++):i=!1:r+=o:o===`"`?i=!0:o===t?(n.push(r),r=``):r+=o}return n.push(r),n}function f(e,t){let n=0,r=0;for(let i of e){let e=i.trim();e!==``&&(n++,s(e,t)!==null&&r++)}return n>0&&r/n>=.5}function p(e,t,n,r){let i=d(e[t],n);if(f(i,r))return!1;let a=e.slice(t+1,t+11).map(e=>d(e,n));return a.length===0||i.every(e=>e.trim()===``)?!0:i.some((e,t)=>{let n=e.trim();return n!==``&&s(n,r)===null&&f(a.map(e=>e[t]??``),r)})}function m(e,t){if(t===void 0)return!1;let n=e=>/[\t,;]/.test(e);return!n(e)&&n(t)}function h(e){let t=[];for(let n of e.split(/\r\n|\r|\n/)){let e=n.replace(/\uFEFF/g,``).trimEnd();e.length>0&&t.push(e)}return t}function g(e,t){return e.headerRow===t.headerRow&&e.unitsRow===t.unitsRow&&e.dataStartRow===t.dataStartRow}function _(e){let t=[` `,`;`,`,`],n={delim:` `,cols:1,agree:0};for(let r of t){let t=new Map;for(let n of e){let e=d(n,r).length;e>1&&t.set(e,(t.get(e)??0)+1)}for(let[e,i]of t)(i>n.agree||i===n.agree&&e>n.cols)&&(n={delim:r,cols:e,agree:i})}return{delim:n.delim,cols:n.cols}}function v(e){let t=h(e);if(t.length===0)return{headerRow:0,unitsRow:null,dataStartRow:1};let n=t.slice(0,10),{delim:r,cols:i}=_(n),a=e=>d(e,r).length===i,o=e=>d(e,r).every(e=>e.trim()!==``),s=n.findIndex(e=>a(e)&&o(e));s<0&&(s=n.findIndex(a)),(s<0||i<=1)&&(s=0);let c=null,u=s+1;if(t.length>s+1){let e=l(t.slice(s+1,s+11),r);p(t,s+1,r,e)&&(c=s+1,u=s+2)}return{headerRow:s,unitsRow:c,dataStartRow:u}}function y(e,t){let n=e[t]??``,r=d(n,u(n)).map(e=>e.trim());if(r.length===0||r.every(e=>e===``))throw Error(`No column names were found.`);return r}function b(e,t){let{table:n,...r}=x(e,t);return{...r,rows:o(n)}}function x(e,t){let{headerRow:n,unitsRow:r,dataStartRow:i}=t,a=[],o=-1,s=0;for(;s<=e.length&&a.length<i+10;){a.length===i&&o<0&&(o=s);let n=e.indexOf(`
|
|
4
|
+
`,s);n<0&&(n=e.length);let r=e.slice(s,n);if(/\r(?!$)/.test(r))return T(e,t);let c=r.replace(/\uFEFF/g,``).trimEnd();c.length>0&&a.push(c),s=n+1}if(a.length===i&&o<0&&(o=s),o<0)return T(e,t);let f=u(a[n]??``),p=y(a,n),h=[];r!==null&&a[r]!==void 0&&(h=d(a[r],f).map(e=>e.trim())),h.length===0&&(h=p.map(()=>``));let g=l(a.slice(i,i+10),f),_=p.map((e,t)=>e||`col_${t}`),v=_.length,b=1;for(let t=e.indexOf(`
|
|
5
|
+
`,o);t>=0;t=e.indexOf(`
|
|
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 S=_.map(()=>new Float64Array(b)),E=_.map(()=>null),D=0,O=(e,t)=>{typeof t==`number`?S[e][D]=t:(S[e][D]=NaN,t!==null&&((E[e]??=[])[D]=t))},ee=e=>{let t=d(e,f);if(!(t.length===1&&t[0].trim()===``)){for(let e=0;e<v;e++)O(e,t[e]===void 0?null:c(t[e],g));D++}},k=f.charCodeAt(0),A=k!==9&&e.indexOf(`"`,o)>=0||e.indexOf(``,o)>=0||x>0;for(s=o;s<e.length;){let t=e.indexOf(`
|
|
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&&A)for(let t=s;t<n;t++){let n=e.charCodeAt(t);if(n===13||n===65279||n===34&&k!==9){r=!1;break}}if(r){let t=s;for(let r=0;r<v;r++){if(t>n){O(r,null);continue}let i=w(e,t,n,k,g);if(i===i){S[r][D]=i,t=C+1;continue}let a=e.indexOf(f,t);(a<0||a>n)&&(a=n),O(r,c(e.slice(t,a),g)),t=a+1}D++}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&&ee(t)}}s=t+1}let j=S.map(e=>b-D>b/100?e.slice(0,D):e.subarray(0,D));return{meta:a.length>0&&m(a[0],a[1])?a[0]:``,columnNames:p,units:h,table:{keys:_,length:D,values:j,text:E}}}var S=Array.from({length:23},(e,t)=>10**t),C=0;function w(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*S[m];else if(m<0&&m>=-22)h=c/S[-m];else return NaN;return C=a,s?-h:h}function T(e,t){let n=h(e),{headerRow:r,unitsRow:i,dataStartRow:a}=t,o=u(n[r]??``),s=y(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 p=n.slice(a),g=l(p.slice(0,10),o),_=s.map((e,t)=>e||`col_${t}`),v=p.length,b=_.map(()=>new Float64Array(v)),x=_.map(()=>null),S=0;for(let e of p){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<v&&(b=b.map(e=>e.slice(0,S))),{meta:n.length>0&&m(n[0],n[1])?n[0]:``,columnNames:s,units:f,table:{keys:_,length:S,values:b,text:x}}}var E=5e3;function D(e,t){let n=O.get(e);n||O.set(e,n=new Map);let r=n.get(t);return r===void 0&&n.set(t,r=ee(e,t)),r}var O=new WeakMap;function ee(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/E));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<E;n++){let r=e[n][t];r!=null&&(o++,typeof r==`number`&&s++)}return o>0&&s/o>.95}function k(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 A=[];function j(e){return A.push(e),e}function M(e){for(let t of A)t.delete(e)}function N(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(te(e))return{x0:r,bucket:o,rows:F(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 P=j(new WeakMap);function te(e){let t=P.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}P.set(e,t)}return t}function F(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 ne(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 I(e,t,n,r,i,a){if(!te(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 L=e=>new DataView(e.buffer,e.byteOffset,e.byteLength);function R(e,t,n=!0){let r=e.getUint32(n?t:t+4,n);return e.getUint32(n?t+4:t,n)*4294967296+r}function z(e,t,n=!0){let r=e.getUint32(n?t:t+4,n);return e.getInt32(n?t+4:t,n)*4294967296+r}var re=new TextDecoder(`utf-8`),ie=new TextDecoder(`latin1`);function ae(e,t=!1){let n=e.indexOf(0);return(t?ie:re).decode(n<0?e:e.subarray(0,n))}var oe=e=>ie.decode(e),se=e=>re.decode(e);function B(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 ce={id:`mdf`,label:`ASAM MDF (MF4, MDF 3)`,extensions:[`.mf4`,`.mdf`,`.mf3`,`.dat`],companions:[{ext:`.dbc`,anyName:!0}],sniff:e=>B(e,`MDF `)||B(e,`UnFinMF `)},le={id:`edf`,label:`EDF / BDF (+)`,extensions:[`.edf`,`.bdf`,`.rec`],sniff:e=>B(e,`0 `)||e[0]===255&&B(e,`BIOSEMI`,1)},ue={id:`xdf`,label:`XDF (Lab Streaming Layer)`,extensions:[`.xdf`],sniff:e=>B(e,`XDF:`)},de={id:`tdms`,label:`NI TDMS`,extensions:[`.tdms`],companions:[{ext:`.tdms_index`}],sniff:e=>B(e,`TDSm`)},fe={id:`mat`,label:`MATLAB MAT (v5, v7.3)`,extensions:[`.mat`],sniff:e=>B(e,`MATLAB 5.0 MAT-file`)||B(e,`MATLAB 7.3 MAT-file`)},pe=`HDF\r
|
|
8
|
+
|
|
9
|
+
`,me={id:`hdf5`,label:`HDF5`,extensions:[`.h5`,`.hdf5`,`.hdf`,`.he5`,`.nc`],sniff:e=>[0,512,1024,2048].some(t=>B(e,pe,t))},he={id:`comtrade`,label:`COMTRADE (IEEE C37.111)`,extensions:[`.cfg`,`.cff`],companions:[{ext:`.dat`}],sniff:e=>{let t=oe(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])}},ge=`This file couldn't be read (it may be damaged).`,_e=1032,ve=e=>e*_e+1024,ye=128e6,be=256,xe=64,Se=1e3,Ce=100,we=200,Te=(e,t)=>e.length>t?`${e.slice(0,t-1)}…`:e;function Ee(e){let t=e.notes;for(let n of e.groups){n.name=Te(String(n.name),be);for(let e of n.channels)e.name=Te(String(e.name),be),e.unit=Te(String(e.unit??``),xe);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>Ce&&t.splice(Ce,t.length-Ce,`${t.length-Ce} more notes.`),e.notes=t.map(e=>Te(String(e),Se));let n={};for(let[t,r]of Object.entries(e.meta).slice(0,we))n[Te(t,be)]=Te(String(r),Se);return e.meta=n,e.events=e.events.filter(e=>Number.isFinite(e.time)).map(e=>({time:e.time,text:Te(String(e.text),Se)})),e}function De(e){return e instanceof Error&&(e.constructor===Error||e.name===`KymoscopeError`||e.name===`AbortError`)?e:Error(ge)}function Oe(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 V=1<<20,ke=64,Ae=16<<20,je=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>V)return this.slice(e,n-e);let r=Math.floor(e/V),i=Math.floor((n-1)/V),a=await this.#n(r);if(r===i)return a.subarray(e-r*V,n-r*V);let o=new Uint8Array(n-e),s=a.subarray(e-r*V);return o.set(s),o.set((await this.#n(i)).subarray(0,n-i*V),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=Ae){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*V,V);return this.#t.size>=ke&&this.#t.delete(this.#t.keys().next().value),this.#t.set(e,n),n}},Me=`kymoscope-live-key-check-v1`,Ne=6e5,Pe=`kymoscope-open-stream-v1`;function H(){let e=globalThis.crypto;if(!e?.subtle)throw Error(`WebCrypto SubtleCrypto is not available`);return e.subtle}function U(e){let t=``;for(let n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}function W(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 Fe(e){return U(e).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``)}function Ie(e){return W(e.replace(/-/g,`+`).replace(/_/g,`/`).padEnd(Math.ceil(e.length/4)*4,`=`))}function Le(e){let t=``;for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,`0`);return t}function Re(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),U(e)}async function ze(e,t){let n=new TextEncoder,r=await H().importKey(`raw`,n.encode(e),`PBKDF2`,!1,[`deriveKey`]);return H().deriveKey({name:`PBKDF2`,salt:W(t),iterations:Ne,hash:`SHA-256`},r,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}async function Be(e,t){let n=await H().importKey(`raw`,new TextEncoder().encode(e),`PBKDF2`,!1,[`deriveBits`]),r=await H().deriveBits({name:`PBKDF2`,salt:W(t),iterations:Ne,hash:`SHA-256`},n,256);return U(new Uint8Array(r))}var Ve=null,He=0;function Ue(){(!Ve||He>4294967295)&&(Ve=globalThis.crypto.getRandomValues(new Uint8Array(8)),He=0);let e=new Uint8Array(12);return e.set(Ve),new DataView(e.buffer).setUint32(8,He++),e}async function We(e,t){let n=Ue(),r=new TextEncoder().encode(JSON.stringify(t)),i=await H().encrypt({name:`AES-GCM`,iv:n},e,r);return{iv:U(n),ciphertext:U(new Uint8Array(i))}}async function Ge(e,t){let n=W(t.iv),r=W(t.ciphertext),i=await H().decrypt({name:`AES-GCM`,iv:n},e,r);return new Uint8Array(i)}async function Ke(e,t,n,r){let i=await H().decrypt({name:`AES-GCM`,iv:t,additionalData:r},e,n);return new Uint8Array(i)}async function qe(e,t){return JSON.parse(new TextDecoder().decode(await Ge(e,t)))}async function Je(e){return We(e,Me)}async function Ye(e,t){try{return await qe(e,t)===Me}catch{return!1}}async function Xe(e){let t=await H().digest(`SHA-256`,new TextEncoder().encode(e));return Le(new Uint8Array(t))}function Ze(){let e=e=>{let t=new Uint8Array(e);return globalThis.crypto.getRandomValues(t),Fe(t)},t=`kymoscope_${e(24)}.${e(32)}`;return{token:t,prefix:t.slice(0,12)}}var Qe=/^((?:kymoscope|prism)_[A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_-]+))?$/;function $e(e){let t=Qe.exec(e.trim());return t?{auth:t[1],secret:t[2]??null}:{auth:e,secret:null}}var et=new TextEncoder().encode(`kymoscope-device-wrap-v1`);async function tt(){let e=await H().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),t=new Uint8Array(await H().exportKey(`raw`,e.publicKey));return{privateKey:e.privateKey,publicKey:U(t)}}async function nt(e,t){let n=new Uint8Array(e.length+et.length);return n.set(e),n.set(et,e.length),H().importKey(`raw`,await H().digest(`SHA-256`,n),`AES-GCM`,!1,t)}async function rt(e,t,n){let r=await H().importKey(`raw`,W(t.epk),{name:`ECDH`,namedCurve:`P-256`},!1,[]),i=new Uint8Array(await H().deriveBits({name:`ECDH`,public:r},e,256));return new Uint8Array(await H().decrypt({name:`AES-GCM`,iv:W(t.iv),additionalData:new TextEncoder().encode(n)},await nt(i,[`decrypt`]),W(t.ct)))}function it(e,t,n){return at(e,t,n)}async function at(e,t,n,r,i){let a=await H().importKey(`raw`,W(e),{name:`ECDH`,namedCurve:`P-256`},!1,[]),o=r??await H().generateKey({name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`]),s=new Uint8Array(await H().deriveBits({name:`ECDH`,public:a},o.privateKey,256)),c=i??Ue(),l=new Uint8Array(await H().encrypt({name:`AES-GCM`,iv:c,additionalData:new TextEncoder().encode(n)},await nt(s,[`encrypt`]),t));return{epk:U(new Uint8Array(await H().exportKey(`raw`,o.publicKey))),iv:U(c),ct:U(l)}}async function ot(e,t,n,r){return ft(await rt(e,t,`${n}|${r}`))}var st=new TextEncoder().encode(`kymoscope-sender-bind-v1`),ct=(e,t,n)=>`project:${e}:${t}|${n}`,lt=(e,t,n)=>`live_stream:${e}|${t}:${n}`,ut=(e,t,n)=>`bind:${e}|${t}:${n}`;function dt(){return globalThis.crypto.getRandomValues(new Uint8Array(32))}function ft(e){return H().importKey(`raw`,e,{name:`AES-GCM`,length:256},!1,[`encrypt`,`decrypt`])}function pt(e,t,n){return mt(e,t,n)}async function mt(e,t,n,r){let i=r??Ue(),a=await H().importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`]),o=new Uint8Array(await H().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},a,t));return{iv:U(i),ct:U(o)}}async function ht(e,t,n){let r=await H().importKey(`raw`,e,`AES-GCM`,!1,[`decrypt`]);return new Uint8Array(await H().decrypt({name:`AES-GCM`,iv:W(t.iv),additionalData:new TextEncoder().encode(n)},r,W(t.ct)))}async function gt(e){let t=new TextEncoder().encode(e),n=new Uint8Array(t.length+st.length);return n.set(t),n.set(st,t.length),new Uint8Array(await H().digest(`SHA-256`,n))}async function _t(e,t,n){let r=await H().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`sign`]);return U(new Uint8Array(await H().sign(`HMAC`,r,new TextEncoder().encode(`sender|${t}|${n}`))))}async function vt(e,t,n,r){let i;try{i=W(r)}catch{return!1}let a=await H().importKey(`raw`,e,{name:`HMAC`,hash:`SHA-256`},!1,[`verify`]);return H().verify(`HMAC`,a,i,new TextEncoder().encode(`sender|${t}|${n}`))}async function yt(e){let t=Le(new Uint8Array(await H().digest(`SHA-256`,W(e)))).slice(0,16).toUpperCase();return`${t.slice(0,4)}-${t.slice(4,8)}-${t.slice(8,12)}-${t.slice(12,16)}`}var bt=`kymoscope-workbook-export-v2`,xt=(e,t)=>`workbook:${e}:${t}`,St=(e,t,n)=>`workbook:${e}|${t}:${n}`,Ct=(e,t)=>`project_config:${e}|${e}:${t}`,wt=(e,t,n)=>`alert_rule:${e}:${t}|${n}`,Tt=(e,t,n)=>`alert_event:${e}:${t}|${n}`,Et=(e,t)=>`kymoscope-recovery-key-v1|${e}|${t}`,Dt=(e,t,n)=>`recovery:${e}|${t}:${n}`;function Ot(e,t,n){return kt(e,t,n)}async function kt(e,t,n,r){let i=r??Ue(),a=new Uint8Array(await H().encrypt({name:`AES-GCM`,iv:i,additionalData:new TextEncoder().encode(n)},e,t));return{iv:U(i),ct:U(a)}}async function At(e,t,n){return new Uint8Array(await H().decrypt({name:`AES-GCM`,iv:W(t.iv),additionalData:new TextEncoder().encode(n)},e,W(t.ct)))}function jt(e,t,n){return Mt(e,t,n)}async function Mt(e,t,n,r,i){let a=U(r??globalThis.crypto.getRandomValues(new Uint8Array(16))),o=await kt(await ze(e,a),t,n,i);return{kdf:{name:`PBKDF2`,hash:`SHA-256`,iterations:Ne,salt:a},...o}}async function Nt(e,t,n){let r=t?.kdf;if(r?.name!==`PBKDF2`||r.hash!==`SHA-256`||r.iterations!==Ne||typeof r.salt!=`string`)throw Error(`unsupported_kdf`);return At(await ze(e,r.salt),t,n)}var Pt=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`;function Ft(e){let t=0,n=0,r=``;for(let i of e)for(n=(n<<8|i)&1048575,t+=8;t>=5;)r+=Pt[n>>>t-5&31],t-=5;return t>0&&(r+=Pt[n<<5-t&31]),r.match(/.{1,4}/g).join(`-`)}function It(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|Pt.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 Lt(){let e=await H().generateKey({name:`ECDH`,namedCurve:`P-256`},!0,[`deriveBits`]),t=await H().exportKey(`jwk`,e.privateKey),n=new Uint8Array(await H().exportKey(`raw`,e.publicKey));return{d:Ie(t.d),publicKey:U(n)}}function Rt(e,t){let n=W(t);return H().importKey(`jwk`,{kty:`EC`,crv:`P-256`,ext:!1,x:Fe(n.subarray(1,33)),y:Fe(n.subarray(33,65)),d:Fe(e)},{name:`ECDH`,namedCurve:`P-256`},!1,[`deriveBits`])}var zt=j(new WeakSet),Bt={length:0,x:new Float64Array,cols:new Map};function Vt(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 Ht(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 Ut(e,t,n,r,i){let a=[];for(let o of new Set(e)){let e=t(o);if(!e||!(e.t<r))continue;let s=Math.max(n,e.t);s<i&&a.push({c:o,a:s,v:e.v})}if(a.length===0)return null;a.sort((e,t)=>e.a-t.a);let o=a.length+1,s=new Float64Array(o),c=new Map;return a.forEach((e,t)=>{let n=new Float64Array(o).fill(NaN);n[t]=n[o-1]=e.v,c.set(e.c,n),s[t]=e.a}),s[o-1]=i,{length:o,x:s,cols:c,hold:!0}}function Wt(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 Ht(e,n)}function Gt(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 Kt(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 qt(e,t,i,a,o=``,s=0){let c=n(e),l=k(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 Jt(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 Yt(e){let t=e.filter(e=>e.length>0);if(t.length===0)return Bt;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 G=64,Xt=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){this.end===this.b.length&&(this.start>this.count?this.relocate(0,this.b.length):this.relocate(0,Math.max(1024,this.b.length*2)),n=this.lowerBound(e));let t=e=>e.copyWithin(n+1,n,this.end);return t(this.b),t(this.rowP),this.lo.forEach(t),this.hi.forEach(t),this.end++,n<this.mEnd&&this.mEnd++,this.dirty!==1/0&&this.dirty>=n&&this.dirty++,this.b[n]=e,this.clearRow(n),n}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}coarsen(e){let t=new Int32Array(this.end-this.start),n=this.start-1;for(let r=this.start;r<this.end;r++){let i=Math.floor(this.b[r]/e);(n<this.start||this.b[n]!==i)&&(this.b[++n]=i),t[r-this.start]=n}for(let e=0;e<this.nc;e++){let n=this.lo[e],r=this.hi[e],i=-1;for(let e=this.start;e<this.end;e++){let a=t[e-this.start],o=n[e],s=r[e];if(a!==i){n[a]=o,r[a]=s,i=a;continue}o===o&&(n[a]<=o||(n[a]=o),r[a]>=s||(r[a]=s))}}this.end=n+1,this.bucket*=e,this.dirty=1/0,this.dirtyFront=-1,this.invalid=!0}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=G-this.pStart,r=2*(t+e+G)<=this.px.length,i=r?this.px.length:Math.max(4*this.b.length+G,2*(t+e+G)),a=e=>{if(r)return e.copyWithin(G,this.pStart,this.pEnd),e;let t=new Float64Array(i);return t.set(e.subarray(this.pStart,this.pEnd),G),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=G,this.pEnd=G+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}}writeAll(){let e=this.start,t=this.count,n=new Uint8Array(t);for(let r=0;r<this.nc;r++){let i=this.lo[r],a=this.hi[r];for(let r=0;r<t;r++){let t=a[e+r];t===t&&t!==i[e+r]&&(n[r]=1)}}let r=0;for(let e=0;e<t;e++)r+=1+n[e];this.pStart=this.pEnd=G,this.mStart=this.mEnd=e,this.reserve(r);let i=G;for(let r=0;r<t;r++){let t=(this.b[e+r]+.5)*this.bucket;this.rowP[e+r]=i,this.px[i]=t,n[r]&&(this.px[i+1]=t),i+=1+n[r]}for(let r=0;r<this.nc;r++){let i=this.lo[r],a=this.hi[r],o=this.pv[r];for(let r=0;r<t;r++){let t=this.rowP[e+r];o[t]=i[e+r],n[r]&&(o[t+1]=a[e+r])}}this.pEnd=i,this.mEnd=this.end}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.writeAll();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 Zt(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}var Qt=4e6,$t=60,en=32,tn=4;function nn(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 rn(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 an=`ElapsedTime`,on=[];function sn(e){let t=on[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]])])))},on[e]=t}return t}var cn=4096,ln=4,un=class{time;alloc;maxBytes;cols=new Map;head=0;count=0;full=!1;pushed=0;clears=0;pushedBefore=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;debugGrowCopied=0;timeDecs=new Map;mirrors=new Map;historyDec=null;snapDec=null;latest=new Map;listed=new Map;constructor(e,t=an,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,cn),this.time=new Float64Array(this.alloc)}get maxSamples(){return this.capacity}newColumn(){if(this.growing)try{return new Float64Array(this.growing.next).fill(NaN)}catch(e){this.refused(e)}return new Float64Array(this.alloc).fill(NaN)}growing=null;stepGrow(e){if(this.full)return;if(!this.growing){if(this.count<this.alloc*.75||this.alloc>=this.capacity)return;let e=Math.min(this.capacity,this.alloc*2);if(e*8*(1+this.cols.size)>this.maxBytes)return;this.growing={next:e,todo:[null,...this.cols.keys()],owed:0}}let t=this.growing;t.owed+=t.todo.length*e/Math.max(1,this.alloc-this.count),this.moveColumns(Math.floor(t.owed))}moveColumns(e){let t=this.growing;for(;e>0&&t.todo.length>0;e--,t.owed--){let e=t.todo.pop(),n=e===null?this.time:this.cols.get(e);if(!n||n.length>=t.next)continue;let r;try{r=new Float64Array(t.next)}catch(e){this.refused(e);return}r.set(n),this.debugGrowCopied+=n.length,e===null?this.time=r:this.cols.set(e,r)}t.todo.length===0&&(this.alloc=t.next,this.growing=null)}growNow(){this.growing?this.moveColumns(1/0):this.grow()}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),this.debugGrowCopied+=t.length,n};try{this.time=t(this.time);for(let[e,n]of this.cols)this.cols.set(e,t(n))}catch(e){this.refused(e);return}this.alloc=e}refused(e){if(!(e instanceof RangeError))throw e;this.capacity=this.alloc,this.growing=null}get version(){return this.pushedBefore+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)}let t=this.lastPushT;this.lastPushT=Math.max(t,e.t),!this.full&&this.count===this.alloc&&(this.growNow(),this.count===this.capacity&&(this.full=!0));let n=this.full?this.head:this.count;this.time[n]=e.t;for(let e of this.signalOrder)this.cols.get(e)[n]=NaN;for(let t of e.signals){if(this.computed.has(t.name))continue;let r=this.cols.get(t.name);r?t.unit&&t.unit!==this.unitByName.get(t.name)&&(this.unitByName.get(t.name)&&this.dropColumnSamples(t.name,r),this.unitByName.set(t.name,t.unit)):(r=this.newColumn(),this.cols.set(t.name,r),this.signalOrder.push(t.name),this.listed.delete(t.name),this.unitByName.set(t.name,t.unit??``)),r[n]=t.value,Number.isFinite(t.value)&&this.noteValue(t.name,e.t,t.value)}this.pushed++,this.full?this.head=(this.head+1)%this.capacity:(this.count++,this.count===this.capacity&&(this.full=!0)),this.stepGrow(1),e.t<t&&this.placeLate(t,e.t,1,!0,this.pushed-1)}pushColumns(e){let t=e.t,n=t.length,r=rn(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.listed.delete(t),this.unitByName.set(t,n??``))}for(let i of r){if(this.computed.has(i))continue;let r=e.columns[i];for(let e=n-1;e>=0;e--)if(Number.isFinite(r[e])){this.noteValue(i,+t[e],r[e]);break}}let i=this.lastPushT,a=1/0,o=-1/0,s=!0,c=-1;for(let e=0;e<n;e++){let n=+t[e];n<(e>0?+t[e-1]:i)&&(c=e,e>0&&(s=!1)),n<a&&(a=n),n>o&&(o=n)}let l=this.pushed+c;this.lastPushT=Math.max(i,o);let u=Math.max(0,n-this.capacity);for(this.pushed+=u;u<n;){!this.full&&this.count===this.alloc&&(this.growNow(),this.count===this.capacity&&(this.full=!0));let r=this.full?this.head:this.count,i=Math.min(n-u,this.full?this.capacity-this.head:this.alloc-this.count);nn(this.time,r,t,u,i);for(let t of this.signalOrder){let n=this.cols.get(t),a=this.computed.has(t)?void 0:e.columns[t];a?nn(n,r,a,u,i):n.fill(NaN,r,r+i)}this.pushed+=i,u+=i,this.full?this.head=(this.head+i)%this.capacity:(this.count+=i,this.count===this.capacity&&(this.full=!0))}this.stepGrow(Math.min(n,this.capacity)),c>=0&&this.placeLate(i,a,Math.min(n,this.capacity),s,l)}placeLate(e,t,n,r,i){if(e-t>$t){this.steppedBack(i);return}let a=this.size,o=a-n,s=e=>this.full?(this.head+e)%this.capacity:e,c=0,l=o;for(;c<l;){let e=c+l>>>1;this.time[s(e)]<=t?c=e+1:l=e}r||(c=Math.min(c,o));let u=a-c;if(u<2)return;if(u*(this.cols.size+1)>Qt||!this.timeMonotonic){this.steppedBack(i);return}let d=new Float64Array(u);for(let e=0;e<u;e++)d[e]=this.time[s(c+e)];let f=new Uint32Array(u);for(let e=0;e<u;e++)f[e]=e;f.sort((e,t)=>d[e]-d[t]||e-t);let p=new Set;for(let t=u-n;t<u;t++)if(!(t<0||d[t]>=e))for(let[e,n]of this.cols)Number.isFinite(n[s(c+t)])&&p.add(e);let m=[],h=this.snapDec;h&&h.key===`${this.signalOrder.join(`
|
|
10
|
+
`)}|${this.clears}`?m.push({k:h,names:this.signalOrder,ycols:this.signalOrder.map(e=>this.cols.get(e)),drop:()=>{this.snapDec===h&&(this.snapDec=null)}}):this.snapDec=null;for(let[e,t]of this.timeDecs)for(let n of t)m.push({k:n,names:n.cols,ycols:n.cols.map(e=>this.cols.get(e)),drop:()=>{let t=(this.timeDecs.get(e)??[]).filter(e=>e!==n);t.length>0?this.timeDecs.set(e,t):this.timeDecs.delete(e)}});let g=m.filter(e=>{let t=this.pushed-n-e.k.pushed;try{if(t<0||t>o||t*e.ycols.length>Qt)throw Error(`behind`);return t>0&&(this.foldBuckets(e.k.store,o-t,o,e.k.bucket,e.ycols),e.k.pushed+=t),!0}catch{return e.drop(),!1}}),_=new Float64Array(u),v=e=>{for(let t=0;t<u;t++)_[t]=e[s(c+f[t])];for(let t=0;t<u;t++)e[s(c+t)]=_[t]};v(this.time);for(let e of this.cols.values())v(e);let y=this.pushed-a+c;this.decCache&&this.decCache.pushed>y&&(this.decCache.pushed=y);let b=this.passTrack;if(b&&b.done>y){b.cuts=b.cuts.filter(e=>e.abs<y),b.done=y,b.prevX=NaN;let e=this.cols.get(b.key);for(let t=y-1;e&&t>=this.pushed-a;t--){let n=e[this.physOf(t)];if(!Number.isNaN(n)){b.prevX=n;break}}}for(let t of g)if(t.names.some(e=>p.has(e)))try{for(let r=0;r<u;r++){if(f[r]<u-n||d[f[r]]>=e)continue;let i=s(c+r),a=Math.floor(this.time[i]/t.k.bucket);a>=t.k.fromB&&t.k.store.fold(t.k.store.row(a),t.ycols,i,i+1)}}catch{t.drop()}this.historyDec=null;for(let e of this.mirrors.values())e.end=Math.max(e.first,Math.min(e.end,y)),e.view=null;for(let[e,t]of this.boundsCache)t.loAbs>=y||t.hiAbs>=y?this.boundsCache.delete(e):(t.done=Math.min(t.done,y),t.version=-1)}steppedBack(e){this.lastBackAbs=e,this.timeDecs.clear(),this.snapDec=null}dropColumnSamples(e,t){t.fill(NaN),this.decCache=null,this.timeDecs.clear(),this.snapDec=null;for(let t of this.trackers.values())t.previous?.cols.delete(e);this.clears++}clear(){this.clears++,this.pushedBefore+=this.pushed,this.head=0,this.count=0,this.full=!1,this.pushed=0,this.growing=null,this.cols.clear(),this.computed.clear(),this.signalOrder.length=0,this.unitByName.clear(),this.decCache=null,this.timeDecs.clear(),this.snapDec=null,this.latest.clear(),this.listed.clear(),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,next:this.pushed},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 N(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 Vt(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.listed.delete(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)}let o=this.trackers.get(e);if(o){for(let t=0;t<r.length;t++){let i=n+t,a=r[t];i<o.next||i<this.firstAbs||i>=this.pushed||a===null||!Number.isFinite(a)||this.advanceCycle(e,o,a,i)}o.next=Math.max(o.next,Math.min(this.pushed,n+r.length))}}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.timeDecs.clear(),this.clears++}get columnNames(){return[this.timeColumn,...this.signalOrder,...this.listed.keys()]}get columnCount(){return 1+this.signalOrder.length+this.listed.size}has(e){return e===this.timeColumn||this.cols.has(e)||this.listed.has(e)}unitOf(e){return e===this.timeColumn?this.timeUnit:this.unitByName.get(e)??this.listed.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=sn(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*ln)))}let l=Zt(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=sn(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 Vt(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*ln)))}let d=Zt(u),f=i.join(` `),p=this.timeDecs.get(f)??[],m=p.find(e=>u>=e.bucket*.5&&u<=e.bucket*2)??null;if(m&&(d=m.bucket),!(d>0))return o;let h=i.map(e=>this.cols.get(e)),g=Math.floor(t/d),_=Math.floor(n/d);this.debugLastScan=0;let v=e=>({cols:i,bucket:d,store:e,i0:e.lowerBound(g),i1:e.lowerBound(_+1)});if(n<l-(n-t)){let e=this.timeIndex(g*d),t=this.timeIndex((_+1)*d),n=this.pushed-a,r=`${f}|${d}|${g}|${_}|${n+e}|${n+t}|${this.clears}`;if(this.historyDec?.key!==r){let n=new Xt(i.length,d);this.foldBuckets(n,e,t,d,h),this.historyDec={key:r,store:n}}return v(this.historyDec.store)}let y=m?this.pushed-m.pushed:0;if(m&&m.colsKey===f&&m.bucket===d&&y>=0&&y<a){let e=m.store;if(g<m.fromB){let t=new Xt(i.length,d);this.foldBuckets(t,this.timeIndex(g*d),this.timeIndex(m.fromB*d),d,h);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]}m.fromB=g}y>0&&this.foldBuckets(e,a-y,a,d,h);let t=Math.floor(c/d);return this.full&&t>=m.fromB&&(e.dropBelow(t),e.reset(e.row(t)),this.foldBuckets(e,0,this.timeIndex((t+1)*d),d,h),m.fromB=t),g>m.fromB&&(e.dropBelow(g),m.fromB=g),m.pushed=this.pushed,v(e)}let b=new Xt(i.length,d),x=this.timeIndex(g*d);this.foldBuckets(b,x,a,d,h);let S={colsKey:f,cols:i,bucket:d,store:b,pushed:this.pushed,fromB:x===0?Math.floor(c/d):g};return this.timeDecs.delete(f),this.timeDecs.set(f,[S,...p.filter(e=>e!==m&&e.bucket!==d)].slice(0,tn)),this.timeDecs.size>en&&this.timeDecs.delete(this.timeDecs.keys().next().value),v(b)}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}timeAfter(e){if(!this.timeMonotonic)return NaN;let t=e=>this.full?(this.head+e)%this.capacity:e,n=0,r=this.size;for(;n<r;){let i=n+r>>>1;this.time[t(i)]<=e?n=i+1:r=i}return n<this.size?this.time[t(n)]:NaN}latestTime(){return this.size===0?NaN:this.columnBounds(this.timeColumn).x1}xAtTime(e,t,n=!1){let r=this.columnArray(e),i=this.size;if(!r||i===0)return NaN;if(e===this.timeColumn&&!n){let{x0:n,x1:r}=this.columnBounds(e);return Math.min(r,Math.max(n,t))}if(!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.listed.keys()],n=[this.timeColumn,...this.signalOrder,...t],r=[this.timeUnit,...this.signalOrder.map(e=>this.unitByName.get(e)??``),...t.map(e=>this.listed.get(e))],i=this.size,a=e&&e>0&&i>e?Math.ceil(i/e):1,o=a>1&&this.timeMonotonic?this.snapshotRows(e,t):null;if(o)return{meta:``,columnNames:n,units:r,rows:o};let s=this.pushed-i,c=a>1?(a-s%a)%a:0,l=[];for(let e=c;e<i;e+=a)l.push(this.full?(this.head+e)%this.capacity:e);i>0&&(l.length===0||(i-1-c)%a!=0)&&l.push(this.full?(this.head+i-1)%this.capacity:i-1);let u=e=>{let t=new Float64Array(l.length);for(let n=0;n<l.length;n++)t[n]=e[l[n]];return t},d=t.length?new Float64Array(l.length).fill(NaN):null;return{meta:``,columnNames:n,units:r,rows:dn(n,[u(this.time),...this.signalOrder.map(e=>u(this.cols.get(e))),...t.map(()=>d)])}}snapshotRows(e,t){let n=this.size,r=e=>this.full?(this.head+e)%this.capacity:e,i=this.time[r(0)],a=this.time[r(n-1)],o=(a-i)*2/e;if(!(o>0))return null;let s=`${this.signalOrder.join(`
|
|
11
|
+
`)}|${this.clears}`,c=this.signalOrder.map(e=>this.cols.get(e)),l=this.snapDec,u=l?this.pushed-l.pushed:0;if(l&&l.key===s&&l.bucket<o&&u>=0&&u<n){let e=2;for(;l.bucket*e<o;)e*=2;l.store.coarsen(e),l.bucket*=e,l.fromB=Math.floor(l.fromB/e)}if(l&&l.key===s&&l.bucket>=o&&l.bucket<=o*4&&u>=0&&u<n){let e=l.store;u>0&&this.foldBuckets(e,n-u,n,l.bucket,c);let t=Math.floor(i/l.bucket);this.full&&t>=l.fromB&&(e.dropBelow(t),e.reset(e.row(t)),this.foldBuckets(e,0,this.timeIndex((t+1)*l.bucket),l.bucket,c),l.fromB=t),l.pushed=this.pushed}else{let t=2**Math.ceil(Math.log2(o));t<o&&(t*=2);let r=new Xt(c.length,t,Math.ceil(e/2)+8);this.foldBuckets(r,0,n,t,c),l=this.snapDec={key:s,bucket:t,store:r,pushed:this.pushed,fromB:Math.floor(i/t)}}let d=l.store.toPoints(l.store.start,l.store.end,this.signalOrder),f=new Float64Array(d.x),p=f[0],m=f[f.length-1];for(let e=0;e<f.length;e++)f[e]=f[e]===m?a:f[e]===p?i:f[e];let h=t.length?new Float64Array(f.length).fill(NaN):null;return dn([this.timeColumn,...this.signalOrder,...t],[f,...this.signalOrder.map(e=>new Float64Array(d.cols.get(e))),...t.map(()=>h)])}valueBounds(e){let t=this.columnBounds(e);return Number.isFinite(this.boundsCache.get(e)?.lo??NaN)?t:null}hasValues(e){return this.latest.has(e)||this.computed.has(e)}get memoryBudget(){return this.maxBytes}get valuedCount(){return this.latest.size}lastKnown(e){return this.latest.get(e)}list(e,t,n=NaN,r=NaN){e===this.timeColumn||this.computed.has(e)||(this.cols.has(e)||this.listed.set(e,t),Number.isFinite(n)&&Number.isFinite(r)&&this.noteValue(e,n,r))}setLastKnown(e,t,n,r){e===this.timeColumn||this.computed.has(e)||!Number.isFinite(n)||!Number.isFinite(r)||(this.cols.has(e)||(this.cols.set(e,this.newColumn()),this.signalOrder.push(e),this.listed.delete(e),this.unitByName.set(e,t)),this.noteValue(e,n,r))}noteValue(e,t,n){let r=this.latest.get(e);r?t>=r.t&&(r.t=t,r.v=n):this.latest.set(e,{t,v:n})}};function dn(e,t){return a({keys:e,length:t[0]?.length??0,values:t,text:e.map(()=>null)})}new TextEncoder;var fn=4096;function pn(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>=fn&&(o+=String.fromCharCode(...a),a.length=0)}return a.length>0&&(o+=String.fromCharCode(...a)),o}var mn=new TextDecoder,hn=200;function gn(e,t,n){let r=e.subarray(t,t+n);return mn.decode(r)}function _n(e,t,n){return n>hn?gn(e,t,n):pn(e,t,n)}var vn=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 yn(e,t,n){let r=Math.floor(n/4294967296),i=n;e.setUint32(t,r),e.setUint32(t+4,i)}function bn(e,t){let n=e.getInt32(t),r=e.getUint32(t+4);return n*4294967296+r}function xn(e,t){let n=e.getUint32(t),r=e.getUint32(t+4);return n*4294967296+r}var Sn=4294967295,Cn=17179869183;function wn({sec:e,nsec:t}){if(e>=0&&t>=0&&e<=Cn)if(t===0&&e<=Sn){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),yn(r,4,e),n}}function Tn(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 En(e){return e instanceof Date?wn(Tn(e)):null}function Dn(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:bn(t,4),nsec:t.getUint32(0)};default:throw new K(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${e.length}`)}}function On(e){let t=Dn(e);return new Date(t.sec*1e3+t.nsec/1e6)}var kn={type:-1,encode:En,decode:On},An=class e{static defaultCodec=new e;__brand;builtInEncoders=[];builtInDecoders=[];encoders=[];decoders=[];constructor(){this.register(kn)}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 vn(-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 vn(n,i)}}return e instanceof vn?e:null}decode(e,t,n){let r=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return r?r(e,t,n):new vn(t,e)}};function jn(e){return e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer}function Mn(e){return e instanceof Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):jn(e)?new Uint8Array(e):Uint8Array.from(e)}function Nn(e){return`${e<0?`-`:``}0x${Math.abs(e).toString(16).padStart(2,`0`)}`}var Pn=16,Fn=16,In=class{hit=0;miss=0;caches;maxKeyLength;maxLengthPerKey;constructor(e=Pn,t=Fn){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=pn(e,t,n),a=Uint8Array.prototype.slice.call(e,t,t+n);return this.store(a,i),i}},Ln=`array`,Rn=`map_key`,zn=`map_value`,Bn=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)},Vn=class{stack=[];stackHeadPosition=-1;get length(){return this.stackHeadPosition+1}top(){return this.stack[this.stackHeadPosition]}pushArrayState(e){let t=this.getUninitializedStateFromPool();t.type=Ln,t.position=0,t.size=e,t.array=Array(e)}pushMapState(e){let t=this.getUninitializedStateFromPool();t.type=Rn,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===Ln){let t=e;t.size=0,t.array=void 0,t.position=0,t.type=void 0}if(e.type===Rn||e.type===zn){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}},Hn=-1,Un=new DataView(new ArrayBuffer(0)),Wn=new Uint8Array(Un.buffer);try{Un.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 Gn=RangeError(`Insufficient data`),Kn=new In,qn=class e{extensionCodec;context;useBigInt64;rawStrings;maxStrLength;maxBinLength;maxArrayLength;maxMapLength;maxExtLength;keyDecoder;mapKeyConverter;totalPos=0;pos=0;view=Un;bytes=Wn;headByte=Hn;stack=new Vn;entered=!1;constructor(e){this.extensionCodec=e?.extensionCodec??An.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?Kn:e.keyDecoder,this.mapKeyConverter=e?.mapKeyConverter??Bn}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=Hn,this.stack.reset()}setBuffer(e){let t=Mn(e);this.bytes=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength),this.pos=0}appendBuffer(e){if(this.headByte===Hn&&!this.hasRemaining(1))this.setBuffer(e);else{let t=this.bytes.subarray(this.pos),n=Mn(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 ${Nn(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: ${Nn(e)}`);this.complete();let n=this.stack;for(;n.length>0;){let e=n.top();if(e.type===Ln)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===Rn){if(t===`__proto__`)throw new K(`The key __proto__ is not allowed`);e.key=this.mapKeyConverter(t),e.type=zn;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=Rn;continue DECODE}}return t}}readHeadByte(){return this.headByte===Hn&&(this.headByte=this.readU8()),this.headByte}complete(){this.headByte=Hn}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: ${Nn(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 Gn;let n=this.pos+t,r;return r=this.stateIsMapKey()&&this.keyDecoder?.canBeCached(e)?this.keyDecoder.decode(this.bytes,n,e):_n(this.bytes,n,e),this.pos+=t+e,r}stateIsMapKey(){return this.stack.length>0?this.stack.top().type===Rn:!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 Gn;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=xn(this.view,this.pos);return this.pos+=8,e}readI64(){let e=bn(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 Jn(e,t){return new qn(t).decode(e)}var Yn=6,Xn=8,Zn=12,Qn=16;function $n(e){let t=new Uint8Array(e),n=t.length>0&&t[0]&128?2:1;return t.length<n+Zn+Qn?null:{kind:t[0]&-129,keyId:n===2?t[1]:null,iv:t.subarray(n,n+Zn),ciphertext:t.subarray(n+Zn),aad:t.subarray(0,n)}}async function er(e,t){let n=$n(t);if(!n)return null;let r=typeof e==`function`?await e(n.keyId):e;if(!r)return null;let i=await Ke(r,n.iv,n.ciphertext,n.aad);return{kind:n.kind,plaintext:i}}function tr(e,t,n,r){return nr(e,t,n,r)}async function nr(e,t,n,r,i){let a=r===void 0?new Uint8Array([t]):new Uint8Array([t|128,r&255]),o=i??Ue(),s=new Uint8Array(await globalThis.crypto.subtle.encrypt({name:`AES-GCM`,iv:o,additionalData:a},e,n)),c=new Uint8Array(a.length+Zn+s.length);return c.set(a),c.set(o,a.length),c.set(s,a.length+Zn),c.buffer}var rr=1,ir=e=>e+7&-8;function ar(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)!==rr)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=ir(s),s+ir(i*8)+o.reduce((e,t)=>e+ir(i*t.size),0)>e.byteLength)throw t(`short data`);let c=e.byteOffset,l=new Float64Array(e.buffer,c+s,i);s+=ir(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+=ir(i*t.size);return{t:l,columns:u,units:d}}var or=2,sr=1,cr=1,lr=2,ur=3,dr=2,fr=1,pr=2,mr=1e5,hr=513,gr=20,_r=64*1024*1024,q=(e,t)=>RangeError(`${e}: ${t}`);function vr(e,t){let n=new TextDecoder(`utf-8`,{fatal:!0}),r=[],i=0;try{for(let a=0;a<t;a++){if(i+3>e.byteLength)throw q(`block schema`,`short channel table`);let t=e[i],a=e[i+1],o=e[i+2];if(i+=3,t!==4&&t!==8)throw q(`block schema`,`value size ${t}`);if(i+a+o>e.byteLength)throw q(`block schema`,`short channel table`);r.push({name:n.decode(e.subarray(i,i+a)),unit:n.decode(e.subarray(i+a,i+a+o)),size:t}),i+=a+o}}catch(e){throw e instanceof RangeError?e:q(`block schema`,`invalid text`)}return r}async function yr(e){if(e.byteLength<8)throw q(`block schema`,`short`);let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint8(0);if(n===ur){if(e.byteLength<12)throw q(`block schema`,`short`);let n=t.getUint32(4,!0);if(n>mr)throw q(`block schema`,`too many channels`);let r=t.getUint8(1)&cr?await Cr(e.subarray(12),n*hr,!1):e.subarray(12);return{id:t.getUint32(8,!0),channels:vr(r,n)}}if(n!==or)throw q(`block schema`,`version ${n}`);return{id:t.getUint32(4,!0),channels:vr(e.subarray(8),t.getUint16(2,!0))}}function br(e,t,n){let r=n.get(t.getUint32(4,!0));if(!r)return null;let i=t.getUint32(8,!0);if(i!==r.channels.length)throw q(`last values`,`channel count differs from its schema`);if(e.byteLength<12+16*i)throw q(`last values`,`short`);let a=[];for(let e=0;e<i;e++){let n=t.getFloat64(12+8*e,!0),o=t.getFloat64(12+8*(i+e),!0);Number.isFinite(n)&&Number.isFinite(o)&&a.push({name:r.channels[e].name,unit:r.channels[e].unit,t:n,v:o})}return a}async function xr(e,t){if(e.byteLength<12)throw q(`last values`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=n.getUint8(0);if(r===sr)return br(e,n,t);if(r!==dr)throw q(`last values`,`version ${r}`);if(e.byteLength<20)throw q(`last values`,`short`);let i=n.getUint8(1),a=t.get(n.getUint32(4,!0));if(!a)return null;let o=n.getUint32(8,!0),s=n.getFloat64(12,!0),c=a.channels.length,l=(i&pr)!==0;if(o>c||l&&o!==c)throw q(`last values`,`entry count differs from its schema`);let u=(l?0:4*o)+4*o,d=i&cr?await Cr(e.subarray(20),u+8*o,!1):e.subarray(20);if(d.byteLength<u)throw q(`last values`,`short`);let f=new DataView(d.buffer,d.byteOffset,d.byteLength),p=[];for(let e=0;e<o;e++){let t=l?e:f.getUint32(4*e,!0);if(t>=c)throw q(`last values`,`channel outside its schema`);p.push(t)}let m=l?0:4*o,h=p.filter(e=>a.channels[e].size===4),g=p.filter(e=>a.channels[e].size===8);if(d.byteLength!==u+4*h.length+8*g.length)throw q(`last values`,`body size mismatch`);let _=Sr(d,u,h.length,4),v=Sr(d,u+4*h.length,g.length,8),y=new Map;h.forEach((e,t)=>y.set(e,_[t])),g.forEach((e,t)=>y.set(e,v[t]));let b=[];return p.forEach((e,t)=>{let n=s-f.getUint32(m+4*t,!0)/1e3,r=y.get(e);Number.isFinite(n)&&Number.isFinite(r)&&b.push({name:a.channels[e].name,unit:a.channels[e].unit,t:n,v:r})}),b}function Sr(e,t,n,r){let i=new Uint8Array(n*r);for(let a=0;a<r;a++)for(let o=0;o<n;o++)i[o*r+a]=e[t+a*n+o];return r===4?new Float32Array(i.buffer):new Float64Array(i.buffer)}async function Cr(e,t,n=!0){let r=new Blob([e]).stream().pipeThrough(new DecompressionStream(`deflate`)).getReader(),i=new Uint8Array(Math.min(t,e.byteLength*4+1024)),a=0;try{for(;;){let{done:e,value:n}=await r.read();if(e)break;if(a+n.length>t)throw q(`block`,`body larger than its header says`);if(a+n.length>i.length){let e=new Uint8Array(Math.min(t,Math.max(2*i.length,a+n.length)));e.set(i.subarray(0,a)),i=e}i.set(n,a),a+=n.length}}catch(e){throw await r.cancel().catch(()=>{}),e instanceof RangeError?e:q(`block`,`body is not valid deflate`)}if(n&&a!==t)throw q(`block`,`body size mismatch`);return i.length===a?i:i.subarray(0,a)}async function wr(e,t){if(e.byteLength<gr)throw q(`block`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==or)throw q(`block`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint16(2,!0),a=n.getUint32(4,!0),o=n.getUint32(8,!0),s=t.get(o);if(!s)return null;if(s.channels.length!==i)throw q(`block`,`channel count differs from its schema`);let c=n.getFloat64(12,!0),l=Math.max(0,a-1),u=4*l+s.channels.reduce((e,t)=>e+t.size*a,0);if(u>_r)throw q(`block`,`too large`);let d;if(r&cr)d=await Cr(e.subarray(gr),u);else if(d=e.subarray(gr),d.byteLength<u)throw q(`block`,`body size mismatch`);let f=new DataView(d.buffer,d.byteOffset,d.byteLength),p=new Float64Array(a),m=0;a&&(p[0]=c);for(let e=1;e<a;e++)m+=f.getInt32(4*(e-1),!0),p[e]=c+m/1e6;let h=4*l,g={},_={};for(let e of s.channels)g[e.name]=Sr(d,h,a,e.size),e.unit&&(_[e.name]=e.unit),h+=a*e.size;return{block:{t:p,columns:g,units:_},historical:(r&lr)!==0,schemaId:o}}async function Tr(e,t){if(e.byteLength<gr)throw q(`sparse`,`short`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint8(0)!==fr)throw q(`sparse`,`version ${n.getUint8(0)}`);let r=n.getUint8(1),i=n.getUint32(4,!0),a=t.get(i);if(!a)return null;let o=n.getUint32(8,!0),s=n.getFloat64(12,!0),c=4*o+4*Math.max(0,o-1);if(c+8*o>_r)throw q(`sparse`,`too large`);let l=r&cr?await Cr(e.subarray(gr),c+8*o,!1):e.subarray(gr);if(l.byteLength<c)throw q(`sparse`,`body size mismatch`);let u=new Uint32Array(o);for(let e=0;e<o;e++)u[e]=(l[e]|l[o+e]<<8|l[2*o+e]<<16|l[3*o+e]<<24)>>>0;let d=a.channels.length,f=0;for(let e=0;e<o;e++){if(u[e]>=d)throw q(`sparse`,`event outside its schema`);a.channels[u[e]].size===4&&f++}if(l.byteLength!==c+4*f+8*(o-f))throw q(`sparse`,`body size mismatch`);let p=new DataView(l.buffer,l.byteOffset,l.byteLength),m=new Float64Array(o),h=0;o&&(m[0]=s);for(let e=1;e<o;e++)h+=p.getInt32(4*o+4*(e-1),!0),m[e]=s+h/1e6;let g=Sr(l,c,f,4),_=Sr(l,c+4*f,o-f,8),v=new Float64Array(o);for(let e=0,t=0,n=0;e<o;e++)v[e]=a.channels[u[e]].size===4?g[t++]:_[n++];return{schemaId:i,index:u,t:m,v,historical:(r&lr)!==0}}var Er=16,Dr=class{t=new Float64Array(16);v=new Float64Array(16);start=0;end=0;get count(){return this.end-this.start}add(e,t){if(this.end===this.t.length){let e=this.count,t=e*2>this.t.length?this.t.length*2:this.t.length,n=new Float64Array(t),r=new Float64Array(t);n.set(this.t.subarray(this.start,this.end)),r.set(this.v.subarray(this.start,this.end)),this.t=n,this.v=r,this.start=0,this.end=e}this.t[this.end]=e,this.v[this.end++]=t}},Or=class{channels=new Map;events_=0;maxBytes;constructor(e){this.maxBytes=e}get bytes(){return this.events_*Er}add(e,t,n){let r=this.channels.get(e);r||this.channels.set(e,r=new Dr),r.add(t,n),this.events_++,this.bytes>this.maxBytes&&this.evict()}events(e){let t=this.channels.get(e);return t?{t:t.t.subarray(t.start,t.end),v:t.v.subarray(t.start,t.end)}:{t:new Float64Array,v:new Float64Array}}evict(){let e=this.maxBytes*.9/Er;for(;this.events_>e;){let t=[...this.channels.values()].filter(e=>e.count>0).sort((e,t)=>t.count-e.count);if(!t.length)return;let n=t[0].count;for(let r of t){if(this.events_<=e||r.count*2<n)break;let t=Math.max(1,r.count>>1);r.start+=t,this.events_-=t}}}},kr=1e3,Ar=4,jr=4096,Mr=class{buffer;store;schemas=new Map;known=new Map;active=new Set;constructor(e,t){this.buffer=e,this.store=new Or(t)}schema(e,t,n){this.schemas.delete(e),this.schemas.set(e,t),this.schemas.size>Ar&&this.schemas.delete(this.schemas.keys().next().value),t.forEach((e,t)=>{this.known.has(e)||this.known.set(e,{unit:n[t]??``,t:NaN,v:NaN}),this.buffer.list(e,n[t]??``)})}last(e){for(let t of e)this.note(t.name,t.unit,t.t,t.v)}push(e,t,n,r){let i=this.schemas.get(e);return i?this.events(n.length,e=>i[t[e]],n,r):null}pushNamed(e,t,n){return this.events(e.length,e=>this.known.has(t[e])?t[e]:void 0,e,n)}events(e,t,n,r){let i=[];for(let a=0;a<e;a++){let e=t(a);e!==void 0&&(this.store.add(e,n[a],r[a]),this.note(e,this.known.get(e)?.unit??``,n[a],r[a]),this.active.has(e)&&i.push(a))}if(!i.length)return null;i.sort((e,t)=>n[e]-n[t]);let a=Nr(i.map(e=>n[e]),i.map(e=>t(e)),i.map(e=>r[e]));return this.buffer.pushColumns(a),a}activate(e){let t=!1;for(let n of e){if(this.active.size>=1e3)break;this.known.has(n)&&!this.active.has(n)&&(this.active.add(n),t=!0)}return t}replay(){let e=this.buffer;e.clear();for(let[t,n]of this.known)e.list(t,n.unit,n.t,n.v);let t=[...this.active],n=t.map(e=>this.store.events(e)),r=n.map(e=>e.t.length-1),i=new Pr(e=>-n[e].t[r[e]]);n.forEach((e,t)=>{e.t.length&&i.push(t)});let a=0,o=NaN;for(;i.size;){let t=i.top,s=n[t].t[r[t]];if(s!==o){if(a===e.maxSamples)break;a++,o=s}i.pop(),--r[t]>=0&&i.push(t)}let s=new Pr(e=>n[e].t[r[e]]);n.forEach((e,t)=>{++r[t]<e.t.length&&s.push(t)});let c=new Float64Array(jr),l={},u=-1,d=()=>{if(u<0)return;let t=u+1;e.pushColumns({t:c.subarray(0,t),columns:Object.fromEntries(Object.entries(l).map(([e,n])=>[e,n.subarray(0,t)]))}),c=new Float64Array(jr),l={},u=-1};for(;s.size;){let e=s.top,i=n[e].t[r[e]];(u<0||i!==c[u])&&(u+1===jr&&d(),c[++u]=i),(l[t[e]]??=new Float64Array(jr).fill(NaN))[u]=n[e].v[r[e]],s.pop(),++r[e]<n[e].t.length&&s.push(e)}d()}note(e,t,n,r){if(!Number.isFinite(n)||!Number.isFinite(r))return;let i=this.known.get(e);i?n<i.t||(i.t=n,i.v=r):this.known.set(e,{unit:t,t:n,v:r}),this.buffer.list(e,t,n,r)}};function Nr(e,t,n){let r=[];for(let t=0;t<e.length;t++)(t===0||e[t]!==e[t-1])&&r.push(e[t]);let i={},a=-1;for(let o=0;o<e.length;o++){(o===0||e[o]!==e[o-1])&&a++;let s=i[t[o]]??(i[t[o]]=new Float64Array(r.length).fill(NaN));s[a]=n[o]}return{t:Float64Array.from(r),columns:i}}var Pr=class{h=[];key;constructor(e){this.key=e}get size(){return this.h.length}get top(){return this.h[0]}push(e){let t=this.h,n=this.key(e),r=t.push(e)-1;for(;r>0;){let e=r-1>>1;if(this.key(t[e])<=n)break;t[r]=t[e],r=e}t[r]=e}pop(){let e=this.h,t=e.pop();if(!e.length)return;let n=this.key(t),r=e.length,i=0;for(;;){let t=2*i+1;if(t>=r||(t+1<r&&this.key(e[t+1])<this.key(e[t])&&t++,this.key(e[t])>=n))break;e[i]=e[t],i=t}e[i]=t}},Fr=0,Ir=1,Lr=70,Rr=e=>Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``),zr=e=>Uint8Array.from(e.match(/../g)??[],e=>parseInt(e,16)),Br=e=>{let t=zr(e.replace(/-/g,``));if(t.length!==16)throw Error(`bad_stream_id`);return t},Vr=e=>{let t=Rr(e);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`};function Hr(e){let t=new Uint8Array(Lr),n=new DataView(t.buffer);return t[0]=1,t.set(Br(e.streamId),1),t.set(zr(e.run),17),t[25]=e.lane,n.setBigUint64(26,e.seq,!0),n.setUint32(34,e.count,!0),t.set(e.hash,38),t}function Ur(e){if(e.length!==Lr||e[0]!==1)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return{streamId:Vr(e.subarray(1,17)),run:Rr(e.subarray(17,25)),lane:e[25],seq:t.getBigUint64(26,!0),count:t.getUint32(34,!0),hash:e.slice(38,70)}}var Wr=()=>globalThis.crypto.subtle,Gr=e=>Wr().digest(`SHA-256`,e);async function Kr(e){let t=await Promise.all(e),n=new Uint8Array(t.length*32);return t.forEach((e,t)=>n.set(new Uint8Array(e),t*32)),new Uint8Array(await Gr(n))}var qr=()=>Rr(globalThis.crypto.getRandomValues(new Uint8Array(8))),Jr=class{digests=[];seq=0n;streamId;run;lane;constructor(e,t,n){this.streamId=e,this.run=t,this.lane=n}add(e){this.digests.push(Gr(e))}async checkpoint(){let e=this.digests;return this.digests=[],this.seq+=1n,Hr({streamId:this.streamId,run:this.run,lane:this.lane,seq:this.seq,count:e.length,hash:await Kr(e)})}};function Yr(e,t,n=()=>Date.now()){let r={verified:0,broken:0},i=null,a=0n,o=null,s=0,c=0,l=0,u=Promise.resolve(),d=e=>{e?r.verified++:(r.broken++,r.lastBrokenAt=n()),t({...r})};return{integrity:r,reset(){i=null,o=null,c=s,l++},arrive(t,n,r){if(n!==8){o&&(o.push(Gr(t)),s++);return}let f=s;o||(o=[],c=s);let p=l;u=u.then(async()=>{let t=await r.catch(()=>null);if(p!==l)return;let n=t?Ur(t.plaintext):null;if(!n||n.lane!==0||n.streamId!==e.toLowerCase()){i!==null&&d(!1);return}let s=f-c;if(i===null||n.run!==i){i=n.run,a=n.seq,o=o.slice(Math.max(0,s)),c=Math.max(c,f);return}let u=n.seq===a+1n&&n.count<=s&&Xr(await Kr(o.slice(0,n.count)),n.hash);if(p!==l)return;a=n.seq;let m=u?n.count:s;o=o.slice(m),c+=m,d(u)})}}}var Xr=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]),J=Uint8Array,Y=Uint16Array,Zr=Int32Array,Qr=new J([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),$r=new J([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),ei=new J([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),ti=function(e,t){for(var n=new Y(31),r=0;r<31;++r)n[r]=t+=1<<e[r-1];for(var i=new Zr(n[30]),r=1;r<30;++r)for(var a=n[r];a<n[r+1];++a)i[a]=a-n[r]<<5|r;return{b:n,r:i}},ni=ti(Qr,2),ri=ni.b,ii=ni.r;ri[28]=258,ii[258]=28;for(var ai=ti($r,0),oi=ai.b,si=ai.r,ci=new Y(32768),X=0;X<32768;++X){var li=(X&43690)>>1|(X&21845)<<1;li=(li&52428)>>2|(li&13107)<<2,li=(li&61680)>>4|(li&3855)<<4,ci[X]=((li&65280)>>8|(li&255)<<8)>>1}for(var Z=(function(e,t,n){for(var r=e.length,i=0,a=new Y(t);i<r;++i)e[i]&&++a[e[i]-1];var o=new Y(t);for(i=1;i<t;++i)o[i]=o[i-1]+a[i-1]<<1;var s;if(n){s=new Y(1<<t);var c=15-t;for(i=0;i<r;++i)if(e[i])for(var l=i<<4|e[i],u=t-e[i],d=o[e[i]-1]++<<u,f=d|(1<<u)-1;d<=f;++d)s[ci[d]>>c]=l}else for(s=new Y(r),i=0;i<r;++i)e[i]&&(s[i]=ci[o[e[i]-1]++]>>15-e[i]);return s}),ui=new J(288),X=0;X<144;++X)ui[X]=8;for(var X=144;X<256;++X)ui[X]=9;for(var X=256;X<280;++X)ui[X]=7;for(var X=280;X<288;++X)ui[X]=8;for(var di=new J(32),X=0;X<32;++X)di[X]=5;var fi=Z(ui,9,0),pi=Z(ui,9,1),mi=Z(di,5,0),hi=Z(di,5,1),gi=function(e){for(var t=e[0],n=1;n<e.length;++n)e[n]>t&&(t=e[n]);return t},Q=function(e,t,n){var r=t/8|0;return(e[r]|e[r+1]<<8)>>(t&7)&n},_i=function(e,t){var n=t/8|0;return(e[n]|e[n+1]<<8|e[n+2]<<16)>>(t&7)},vi=function(e){return(e+7)/8|0},yi=function(e,t,n){return(t==null||t<0)&&(t=0),(n==null||n>e.length)&&(n=e.length),new J(e.subarray(t,n))},bi=[`unexpected EOF`,`invalid block type`,`invalid length/literal`,`invalid distance`,`stream finished`,`no stream handler`,,`no callback`,`invalid UTF-8 data`,`extra field too long`,`date not in range 1980-2099`,`filename too long`,`stream finishing`,`invalid zip data`],$=function(e,t,n){var r=Error(t||bi[e]);if(r.code=e,Error.captureStackTrace&&Error.captureStackTrace(r,$),!n)throw r;return r},xi=function(e,t,n,r){var i=e.length,a=r?r.length:0;if(!i||t.f&&!t.l)return n||new J(0);var o=!n,s=o||t.i!=2,c=t.i;o&&(n=new J(i*3));var l=function(e){var t=n.length;if(e>t){var r=new J(Math.max(t*2,e));r.set(n),n=r}},u=t.f||0,d=t.p||0,f=t.b||0,p=t.l,m=t.d,h=t.m,g=t.n,_=i*8;do{if(!p){u=Q(e,d,1);var v=Q(e,d+1,3);if(d+=3,!v){var y=vi(d)+4,b=e[y-4]|e[y-3]<<8,x=y+b;if(x>i){c&&$(0);break}s&&l(f+b),n.set(e.subarray(y,x),f),t.b=f+=b,t.p=d=x*8,t.f=u;continue}else if(v==1)p=pi,m=hi,h=9,g=5;else if(v==2){var S=Q(e,d,31)+257,C=Q(e,d+10,15)+4,w=S+Q(e,d+5,31)+1;d+=14;for(var T=new J(w),E=new J(19),D=0;D<C;++D)E[ei[D]]=Q(e,d+D*3,7);d+=C*3;for(var O=gi(E),ee=(1<<O)-1,k=Z(E,O,1),D=0;D<w;){var A=k[Q(e,d,ee)];d+=A&15;var y=A>>4;if(y<16)T[D++]=y;else{var j=0,M=0;for(y==16?(M=3+Q(e,d,3),d+=2,j=T[D-1]):y==17?(M=3+Q(e,d,7),d+=3):y==18&&(M=11+Q(e,d,127),d+=7);M--;)T[D++]=j}}var N=T.subarray(0,S),P=T.subarray(S);h=gi(N),g=gi(P),p=Z(N,h,1),m=Z(P,g,1)}else $(1);if(d>_){c&&$(0);break}}s&&l(f+131072);for(var te=(1<<h)-1,F=(1<<g)-1,ne=d;;ne=d){var j=p[_i(e,d)&te],I=j>>4;if(d+=j&15,d>_){c&&$(0);break}if(j||$(2),I<256)n[f++]=I;else if(I==256){ne=d,p=null;break}else{var L=I-254;if(I>264){var D=I-257,R=Qr[D];L=Q(e,d,(1<<R)-1)+ri[D],d+=R}var z=m[_i(e,d)&F],re=z>>4;z||$(3),d+=z&15;var P=oi[re];if(re>3){var R=$r[re];P+=_i(e,d)&(1<<R)-1,d+=R}if(d>_){c&&$(0);break}s&&l(f+131072);var ie=f+L;if(f<P){var ae=a-P,oe=Math.min(P,ie);for(ae+f<0&&$(3);f<oe;++f)n[f]=r[ae+f]}for(;f<ie;++f)n[f]=n[f-P]}}t.l=p,t.p=ne,t.b=f,t.f=u,p&&(u=1,t.m=h,t.d=m,t.n=g)}while(!u);return f!=n.length&&o?yi(n,0,f):n.subarray(0,f)},Si=function(e,t,n){n<<=t&7;var r=t/8|0;e[r]|=n,e[r+1]|=n>>8},Ci=function(e,t,n){n<<=t&7;var r=t/8|0;e[r]|=n,e[r+1]|=n>>8,e[r+2]|=n>>16},wi=function(e,t){for(var n=[],r=0;r<e.length;++r)e[r]&&n.push({s:r,f:e[r]});var i=n.length,a=n.slice();if(!i)return{t:ji,l:0};if(i==1){var o=new J(n[0].s+1);return o[n[0].s]=1,{t:o,l:1}}n.sort(function(e,t){return e.f-t.f}),n.push({s:-1,f:25001});var s=n[0],c=n[1],l=0,u=1,d=2;for(n[0]={s:-1,f:s.f+c.f,l:s,r:c};u!=i-1;)s=n[n[l].f<n[d].f?l++:d++],c=n[l!=u&&n[l].f<n[d].f?l++:d++],n[u++]={s:-1,f:s.f+c.f,l:s,r:c};for(var f=a[0].s,r=1;r<i;++r)a[r].s>f&&(f=a[r].s);var p=new Y(f+1),m=Ti(n[u-1],p,0);if(m>t){var r=0,h=0,g=m-t,_=1<<g;for(a.sort(function(e,t){return p[t.s]-p[e.s]||e.f-t.f});r<i;++r){var v=a[r].s;if(p[v]>t)h+=_-(1<<m-p[v]),p[v]=t;else break}for(h>>=g;h>0;){var y=a[r].s;p[y]<t?h-=1<<t-p[y]++-1:++r}for(;r>=0&&h;--r){var b=a[r].s;p[b]==t&&(--p[b],++h)}m=t}return{t:new J(p),l:m}},Ti=function(e,t,n){return e.s==-1?Math.max(Ti(e.l,t,n+1),Ti(e.r,t,n+1)):t[e.s]=n},Ei=function(e){for(var t=e.length;t&&!e[--t];);for(var n=new Y(++t),r=0,i=e[0],a=1,o=function(e){n[r++]=e},s=1;s<=t;++s)if(e[s]==i&&s!=t)++a;else{if(!i&&a>2){for(;a>138;a-=138)o(32754);a>2&&(o(a>10?a-11<<5|28690:a-3<<5|12305),a=0)}else if(a>3){for(o(i),--a;a>6;a-=6)o(8304);a>2&&(o(a-3<<5|8208),a=0)}for(;a--;)o(i);a=1,i=e[s]}return{c:n.subarray(0,r),n:t}},Di=function(e,t){for(var n=0,r=0;r<t.length;++r)n+=e[r]*t[r];return n},Oi=function(e,t,n){var r=n.length,i=vi(t+2);e[i]=r&255,e[i+1]=r>>8,e[i+2]=e[i]^255,e[i+3]=e[i+1]^255;for(var a=0;a<r;++a)e[i+a+4]=n[a];return(i+4+r)*8},ki=function(e,t,n,r,i,a,o,s,c,l,u){Si(t,u++,n),++i[256];for(var d=wi(i,15),f=d.t,p=d.l,m=wi(a,15),h=m.t,g=m.l,_=Ei(f),v=_.c,y=_.n,b=Ei(h),x=b.c,S=b.n,C=new Y(19),w=0;w<v.length;++w)++C[v[w]&31];for(var w=0;w<x.length;++w)++C[x[w]&31];for(var T=wi(C,7),E=T.t,D=T.l,O=19;O>4&&!E[ei[O-1]];--O);var ee=l+5<<3,k=Di(i,ui)+Di(a,di)+o,A=Di(i,f)+Di(a,h)+o+14+3*O+Di(C,E)+2*C[16]+3*C[17]+7*C[18];if(c>=0&&ee<=k&&ee<=A)return Oi(t,u,e.subarray(c,c+l));var j,M,N,P;if(Si(t,u,1+(A<k)),u+=2,A<k){j=Z(f,p,0),M=f,N=Z(h,g,0),P=h;var te=Z(E,D,0);Si(t,u,y-257),Si(t,u+5,S-1),Si(t,u+10,O-4),u+=14;for(var w=0;w<O;++w)Si(t,u+3*w,E[ei[w]]);u+=3*O;for(var F=[v,x],ne=0;ne<2;++ne)for(var I=F[ne],w=0;w<I.length;++w){var L=I[w]&31;Si(t,u,te[L]),u+=E[L],L>15&&(Si(t,u,I[w]>>5&127),u+=I[w]>>12)}}else j=fi,M=ui,N=mi,P=di;for(var w=0;w<s;++w){var R=r[w];if(R>255){var L=R>>18&31;Ci(t,u,j[L+257]),u+=M[L+257],L>7&&(Si(t,u,R>>23&31),u+=Qr[L]);var z=R&31;Ci(t,u,N[z]),u+=P[z],z>3&&(Ci(t,u,R>>5&8191),u+=$r[z])}else Ci(t,u,j[R]),u+=M[R]}return Ci(t,u,j[256]),u+M[256]},Ai=new Zr([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),ji=new J(0),Mi=function(e,t,n,r,i,a){var o=a.z||e.length,s=new J(r+o+5*(1+Math.ceil(o/7e3))+i),c=s.subarray(r,s.length-i),l=a.l,u=(a.r||0)&7;if(t){u&&(c[0]=a.r>>3);for(var d=Ai[t-1],f=d>>13,p=d&8191,m=(1<<n)-1,h=a.p||new Y(32768),g=a.h||new Y(m+1),_=Math.ceil(n/3),v=2*_,y=function(t){return(e[t]^e[t+1]<<_^e[t+2]<<v)&m},b=new Zr(25e3),x=new Y(288),S=new Y(32),C=0,w=0,T=a.i||0,E=0,D=a.w||0,O=0;T+2<o;++T){var ee=y(T),k=T&32767,A=g[ee];if(h[k]=A,g[ee]=k,D<=T){var j=o-T;if((C>7e3||E>24576)&&(j>423||!l)){u=ki(e,c,0,b,x,S,w,E,O,T-O,u),E=C=w=0,O=T;for(var M=0;M<286;++M)x[M]=0;for(var M=0;M<30;++M)S[M]=0}var N=2,P=0,te=p,F=k-A&32767;if(j>2&&ee==y(T-F))for(var ne=Math.min(f,j)-1,I=Math.min(32767,T),L=Math.min(258,j);F<=I&&--te&&k!=A;){if(e[T+N]==e[T+N-F]){for(var R=0;R<L&&e[T+R]==e[T+R-F];++R);if(R>N){if(N=R,P=F,R>ne)break;for(var z=Math.min(F,R-2),re=0,M=0;M<z;++M){var ie=T-F+M&32767,ae=ie-h[ie]&32767;ae>re&&(re=ae,A=ie)}}}k=A,A=h[k],F+=k-A&32767}if(P){b[E++]=268435456|ii[N]<<18|si[P];var oe=ii[N]&31,se=si[P]&31;w+=Qr[oe]+$r[se],++x[257+oe],++S[se],D=T+N,++C}else b[E++]=e[T],++x[e[T]]}}for(T=Math.max(T,D);T<o;++T)b[E++]=e[T],++x[e[T]];u=ki(e,c,l,b,x,S,w,E,O,T-O,u),l||(a.r=u&7|c[u/8|0]<<3,u-=7,a.h=g,a.p=h,a.i=T,a.w=D)}else{for(var T=a.w||0;T<o+l;T+=65535){var B=T+65535;B>=o&&(c[u/8|0]=l,B=o),u=Oi(c,u+1,e.subarray(T,B))}a.i=o}return yi(s,0,r+vi(u)+i)},Ni=function(){var e=1,t=0;return{p:function(n){for(var r=e,i=t,a=n.length|0,o=0;o!=a;){for(var s=Math.min(o+2655,a);o<s;++o)i+=r+=n[o];r=(r&65535)+15*(r>>16),i=(i&65535)+15*(i>>16)}e=r,t=i},d:function(){return e%=65521,t%=65521,(e&255)<<24|(e&65280)<<8|(t&255)<<8|t>>8}}},Pi=function(e,t,n,r,i){if(!i&&(i={l:1},t.dictionary)){var a=t.dictionary.subarray(-32768),o=new J(a.length+e.length);o.set(a),o.set(e,a.length),e=o,i.w=a.length}return Mi(e,t.level==null?6:t.level,t.mem==null?i.l?Math.ceil(Math.max(8,Math.min(13,Math.log(e.length)))*1.5):20:12+t.mem,n,r,i)},Fi=function(e,t,n){for(;n;++t)e[t]=n,n>>>=8},Ii=function(e,t){var n=t.level,r=n==0?0:n<6?1:n==9?3:2;if(e[0]=120,e[1]=r<<6|(t.dictionary&&32),e[1]|=31-(e[0]<<8|e[1])%31,t.dictionary){var i=Ni();i.p(t.dictionary),Fi(e,2,i.d())}},Li=function(e,t){return((e[0]&15)!=8||e[0]>>4>7||(e[0]<<8|e[1])%31)&&$(6,`invalid zlib data`),(e[1]>>5&1)==+!t&&$(6,`invalid zlib data: `+(e[1]&32?`need`:`unexpected`)+` dictionary`),(e[1]>>3&4)+2},Ri=function(){function e(e,t){typeof e==`function`&&(t=e,e={}),this.ondata=t;var n=e&&e.dictionary&&e.dictionary.subarray(-32768);this.s={i:0,b:n?n.length:0},this.o=new J(32768),this.p=new J(0),n&&this.o.set(n)}return e.prototype.e=function(e){if(this.ondata||$(5),this.d&&$(4),!this.p.length)this.p=e;else if(e.length){var t=new J(this.p.length+e.length);t.set(this.p),t.set(e,this.p.length),this.p=t}},e.prototype.c=function(e){this.s.i=+(this.d=e||!1);var t=this.s.b,n=xi(this.p,this.s,this.o);this.ondata(yi(n,t,this.s.b),this.d),this.o=yi(n,this.s.b-32768),this.s.b=this.o.length,this.p=yi(this.p,this.s.p/8|0),this.s.p&=7},e.prototype.push=function(e,t){this.e(e),this.c(t)},e}();function zi(e,t){t||={};var n=Ni();n.p(e);var r=Pi(e,t,t.dictionary?6:2,4);return Ii(r,t),Fi(r,r.length-4,n.d()),r}var Bi=function(){function e(e,t){Ri.call(this,e,t),this.v=e&&e.dictionary?2:1}return e.prototype.push=function(e,t){if(Ri.prototype.e.call(this,e),this.v){if(this.p.length<6&&!t)return;this.p=this.p.subarray(Li(this.p,this.v-1)),this.v=0}t&&(this.p.length<4&&$(6,`invalid zlib data`),this.p=this.p.subarray(0,-4)),Ri.prototype.c.call(this,t)},e}();function Vi(e,t){return xi(e.subarray(Li(e,t&&t.dictionary),-4),{i:2},t&&t.out,t&&t.dictionary)}var Hi=typeof TextDecoder<`u`&&new TextDecoder;try{Hi.decode(ji,{stream:!0})}catch{}var Ui=56,Wi=[75,82,67,49];function Gi(e){let t=e.replace(/-/g,``);if(!/^[0-9a-f]{32}$/i.test(t))throw RangeError(`recording id must be a uuid`);return Uint8Array.from({length:16},(e,n)=>parseInt(t.slice(2*n,2*n+2),16))}function Ki(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 qi(e,t,n){let r=56+n.reduce((e,t)=>e+12+t.bytes.length,0),i=new Uint8Array(r),a=new DataView(i.buffer);i.set(Wi,0),a.setUint16(4,1,!0),a.setUint16(6,56,!0),i.set(Gi(e),8),a.setUint32(24,t,!0),a.setUint32(28,n.length,!0),a.setUint32(32,r,!0),a.setFloat64(40,n[0]?.at??0,!0),a.setFloat64(48,n.at(-1)?.at??0,!0);let o=56;for(let e of n)a.setFloat64(o,e.at,!0),a.setUint32(o+8,e.bytes.length,!0),i.set(e.bytes,o+12),o+=12+e.bytes.length;return i}function Ji(e){if(e.length<56||Wi.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:Ki(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 Yi(e){let t=Ji(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}}var Xi=e=>crypto.subtle.digest(`SHA-256`,e),Zi=e=>Array.from(new Uint8Array(e),e=>e.toString(16).padStart(2,`0`)).join(``),Qi=e=>Uint8Array.from(e.replace(/-/g,``).match(/../g)??[],e=>parseInt(e,16));async function $i(e,t,n,r){let i=t[t.length-1];if(!i||(i.bytes[0]&127)!=8)return`unchecked`;let a=await n(i.bytes).catch(()=>null),o=a?Ur(a.plaintext):null,s=Zi(await Xi(Qi(e.recordingId))).slice(0,16);return o&&o.lane===1&&o.run===s&&o.seq===BigInt(e.seq)&&e.seq===r&&o.count===t.length-1&&Zi((await Kr(t.slice(0,-1).map(e=>Xi(e.bytes)))).buffer)===Zi(o.hash.buffer)?`verified`:`broken`}function ea(e){let t=[...e],n=new Set(t.filter(e=>e.check!==`unchecked`).map(e=>e.rid)),r={verified:0,broken:0,unchecked:0};for(let e of t)e.check===`unchecked`&&n.has(e.rid)?r.broken++:r[e.check]++;return r}var ta=(e,t,n)=>`recording_key:${e}|${t}:${n}`,na=(e,t,n)=>n===e.stream_epoch?lt(e.stream_id,t,n):ta(e.id,t,n),ra=12,ia=16;function aa(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+ra+ia)return null;let i=r===2?n[1]:null,a=t.get(i),o=[...a?[a]:[],...e.filter(e=>(e.keyId===i||e.keyId===null)&&e.key!==a).map(e=>e.key)];for(let e of o)try{let a=await Ke(e,n.subarray(r,r+ra),n.subarray(r+ra),n.subarray(0,r));return t.set(i,e),{kind:n[0]&-129,plaintext:a}}catch{}return null}}export{yt as $,L as $t,er as A,St as At,Kt as B,he as Bt,wr as C,$e as Ct,ar as D,Ye as Dt,Tr as E,ot as Et,rn as F,ge as Ft,qt as G,le as Gt,Ut as H,fe as Ht,Bt as I,ye as It,Tt as J,ae as Jt,Pe as K,ce as Kt,Gt as L,Ee as Lt,Jn as M,je as Mt,an as N,Ae as Nt,Xn as O,vt as Ot,un as P,Oe as Pt,Be as Q,se as Qt,Wt as R,De as Rt,Mr as S,_t as St,xr as T,rt as Tt,Vt as U,de as Ut,zt as V,me as Vt,Jt as W,ue as Wt,ut as X,B as Xt,wt as Y,z as Yt,ze as Z,R as Zt,Yr as _,a as _n,Rt as _t,$i as a,D as an,ft as at,qr as b,jt as bt,Yi as c,h as cn,At as ct,Vi as d,g as dn,Ct as dt,ne as en,Ze as et,zi as f,x as fn,ct as ft,Kr as g,n as gn,Et as gt,Jr as h,r as hn,Ft as ht,ea as i,M as in,Xe as it,tr as j,it as jt,Yn as k,xt as kt,Ji as l,v as ln,Nt as lt,Ir as m,k as mn,Dt as mt,ta as n,I as nn,Lt as nt,Ui as o,l as on,Je as ot,Fr as p,b as pn,dt as pt,bt as q,oe as qt,na as r,j as rn,Re as rt,qi as s,u as sn,ht as st,aa as t,N as tn,tt,Bi as u,y as un,It as ut,Ur as v,pt as vt,yr as w,lt as wt,kr as x,gt as xt,Hr as y,Ot as yt,Yt as z,ve as zt};
|