import{C as t,D as e,E as i,O as r,S as n,T as s,b as a,w as o,x as h,y as u}from"./main-B2gk-G51.js";function l(){for(var t=[],r=0;r>>=7;e.reverse();for(let i=0;iu.create().update(t).digest()}setToZero(){this.buf=new Uint8Array(d)}add(t){let e=0,i=0,r=new DataView(this.buf.buffer),n=new DataView(t.buffer);for(let s=0;s<8;s++){let t=4*s,a=r.getUint32(t,!0);a+=e,a+=n.getUint32(t,!0),a>4294967295&&(i=1),r.setUint32(t,4294967295&a,!0),e=i,i=0}}negate(){let t=new DataView(this.buf.buffer);for(let i=0;i<8;i++){let e=4*i;t.setUint32(e,~t.getUint32(e,!0))}let e=new Uint8Array(d);e[0]=1,this.add(e)}async getFingerprint(t){let e=new c;return e.extend(this.buf),e.extend(m(t)),(await this.sha256(e.unwrap())).subarray(0,16)}},w=class{items;sealed;constructor(){this.items=[],this.sealed=!1}insert(t,e){if(this.sealed)throw Error("already sealed");if((e=y(e)).byteLength!==d)throw Error("bad id size for added item");this.items.push({timestamp:t,id:e})}seal(){if(this.sealed)throw Error("already sealed");this.sealed=!0,this.items.sort(A);for(let t=1;t=this.items.length)throw Error("out of range");return this.items[t]}iterate(t,e,i){this._checkSealed(),this._checkBounds(t,e);for(let r=t;rA(t,i)<0)}async fingerprint(t,e){let i=new p;return i.setToZero(),this.iterate(t,e,t=>(i.add(t.id),!0)),await i.getFingerprint(e-t)}_checkSealed(){if(!this.sealed)throw Error("not sealed")}_checkBounds(t,e){if(t>e||e>this.items.length)throw Error("bad range")}_binarySearch(t,e,i,r){let n=i-e;for(;n>0;){let i=e,s=Math.floor(n/2);i+=s,r(t[i])?(e=++i,n-=s+1):n=s}return e}},b=class{storage;frameSizeLimit;lastTimestampIn;lastTimestampOut;isInitiator;wantUint8ArrayOutput;constructor(t,e=0){if(0!==e&&e<4096)throw Error("frameSizeLimit too small");this.storage=t,this.frameSizeLimit=e,this.lastTimestampIn=0,this.lastTimestampOut=0}_bound(t,e){return{timestamp:t,id:e||new Uint8Array(0)}}async initiate(){if(this.isInitiator)throw Error("already initiated");this.isInitiator=!0;let t=new c;return t.extend([97]),await this.splitRange(0,this.storage.size(),this._bound(Number.MAX_VALUE),t),this._renderOutput(t)}setInitiator(){this.isInitiator=!0}async reconcile(t){let e=[],i=[],r=new c(y(t));this.lastTimestampIn=this.lastTimestampOut=0;let n=new c;n.extend([97]);let s=g(r,1)[0];if(s<96||s>111)throw Error("invalid negentropy protocol version byte");if(97!==s){if(this.isInitiator)throw Error("unsupported negentropy protocol version requested: "+(s-96));return[this._renderOutput(n),e,i]}let a=this.storage.size(),o=this._bound(0),h=0,u=!1;for(;0!==r.length;){let t=new c,s=()=>{u&&(u=!1,t.extend(this.encodeBound(o)),t.extend(m(0)))},l=this.decodeBound(r),p=f(r),w=h,b=this.storage.findLowerBound(h,a,l);if(0===p)u=!0;else if(1===p)0!==E(g(r,16),await this.storage.fingerprint(w,b))?(s(),await this.splitRange(w,b,l,t)):u=!0;else{if(2!==p)throw Error("unexpected mode");{let a=f(r),o={};for(let t=0;t{let i=t.id;return o[i]?delete o[i]:this.isInitiator&&e.push(this.wantUint8ArrayOutput?i:x(i)),!0});for(let t of Object.values(o))i.push(this.wantUint8ArrayOutput?t:x(t))}else{s();let e=new c,i=0,r=l;this.storage.iterate(w,b,(t,s)=>this.exceededFrameSizeLimit(n.length+e.length)?(r=t,b=s,!1):(e.extend(t.id),i++,!0)),t.extend(this.encodeBound(r)),t.extend(m(2)),t.extend(m(i)),t.extend(e),n.extend(t),t=new c}}}if(this.exceededFrameSizeLimit(n.length+t.length)){let t=await this.storage.fingerprint(b,a);n.extend(this.encodeBound(this._bound(Number.MAX_VALUE))),n.extend(m(1)),n.extend(t);break}n.extend(t),h=b,o=l}return[1===n.length&&this.isInitiator?null:this._renderOutput(n),e,i]}async splitRange(t,e,i,r){let n=e-t;if(n<32)r.extend(this.encodeBound(i)),r.extend(m(2)),r.extend(m(n)),this.storage.iterate(t,e,t=>(r.extend(t.id),!0));else{let s=Math.floor(n/16),a=n%16,o=t;for(let t=0;t<16;t++){let n,h=s+(t(r===o-1?t=i:e=i,!0)),n=this.getMinimalBound(t,e)}r.extend(this.encodeBound(n)),r.extend(m(1)),r.extend(u)}}}_renderOutput(t){let e=t.unwrap();return this.wantUint8ArrayOutput?e:x(e)}exceededFrameSizeLimit(t){return this.frameSizeLimit&&t>this.frameSizeLimit-200}decodeTimestampIn(t){let e=f(t);return e=0===e?Number.MAX_VALUE:e-1,this.lastTimestampIn===Number.MAX_VALUE||e===Number.MAX_VALUE?(this.lastTimestampIn=Number.MAX_VALUE,Number.MAX_VALUE):(e+=this.lastTimestampIn,this.lastTimestampIn=e,e)}decodeBound(t){let e=this.decodeTimestampIn(t),i=f(t);if(i>d)throw Error("bound key too long");return{timestamp:e,id:g(t,i)}}encodeTimestampOut(t){if(t===Number.MAX_VALUE)return this.lastTimestampOut=Number.MAX_VALUE,m(0);let e=t;return t-=this.lastTimestampOut,this.lastTimestampOut=e,m(t+1)}encodeBound(t){let e=new c;return e.extend(this.encodeTimestampOut(t.timestamp)),e.extend(m(t.id.length)),e.extend(t.id),e}getMinimalBound(t,e){if(e.timestamp!==t.timestamp)return this._bound(e.timestamp);{let i=0,r=e.id,n=t.id;for(let t=0;t>>4&15]+t[15&e]}function x(t){let e="";for(let i=0,r=t.length;ie[i])return 1}return t.byteLength>e.byteLength?1:t.byteLength(L("Sending initial message",d,s,f),["NEG-OPEN",d,s,f]),()=>(L("Closing sync",d),["NEG-CLOSE",d]),t=>("NEG-MSG"===t[0]||"NEG-ERR"===t[0])&&t[1]===d).pipe(t(t=>{if("NEG-ERR"===t[0])throw new Error(t[2]);return t[2]}),h());if(u?.signal?.aborted)return!1;const p=new e(t=>{if(u?.signal?.aborted)return t.next("abort"),void t.complete();const e=()=>{t.next("abort"),t.complete()};return u?.signal?.addEventListener("abort",e),()=>u?.signal?.removeEventListener("abort",e)}),w=g.subscribe({next:t=>L(t),error:()=>{}});try{for(;m&&!0!==u?.signal?.aborted;)try{const e=await o(l(g.pipe(t(t=>({type:"message",data:t}))),p.pipe(t(()=>({type:"abort"})))));if("abort"===e.type||u?.signal?.aborted)return w.unsubscribe(),!1;const[i,r,n]=await c.reconcile(e.data);await a(r,n),m=i}catch(y){if(u?.signal?.aborted)return w.unsubscribe(),!1;throw y}}catch(y){throw w.unsubscribe(),y}finally{w.unsubscribe()}return!0}export{U as buildStorageFromFilter,I as buildStorageVector,O as negentropySync};