Files
marco/release/assets/negentropy-D4cKvxXZ.js
T
raucao 387fd6efed
CI / Lint (push) Successful in 53s
CI / Test (push) Successful in 1m4s
1.26.0
2026-08-07 07:33:52 -06:00

1 line
8.7 KiB
JavaScript

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<arguments.length;r++)t[r]=arguments[r];return 1===(t=n(t)).length?s(t[0]):new e(function(t){return function(e){for(var r=[],n=function(n){r.push(s(t[n]).subscribe(i(e,function(t){if(r){for(var i=0;i<r.length;i++)i!==n&&r[i].unsubscribe();r=null}e.next(t)})))},a=0;r&&!e.closed&&a<t.length;a++)n(a)}}(t))}var d=32,c=class t{_raw;length;constructor(t){this._raw=new Uint8Array(t||512),this.length=t?t.length:0}unwrap(){return this._raw.subarray(0,this.length)}get capacity(){return this._raw.byteLength}extend(e){if(e instanceof t&&(e=e.unwrap()),"number"!=typeof e.length)throw Error("bad length");const i=e.length+this.length;if(this.capacity<i){const t=this._raw,e=Math.max(2*this.capacity,i);this._raw=new Uint8Array(e),this._raw.set(t)}this._raw.set(e,this.length),this.length+=e.length}shift(){const t=this._raw[0];return this._raw=this._raw.subarray(1),this.length--,t}shiftN(t=1){const e=this._raw.subarray(0,t);return this._raw=this._raw.subarray(t),this.length-=t,e}};function f(t){let e=0;for(;;){if(0===t.length)throw Error("parse ends prematurely");let i=t.shift();if(e=e<<7|127&i,!(128&i))break}return e}function m(t){if(0===t)return new c([0]);let e=[];for(;0!==t;)e.push(127&t),t>>>=7;e.reverse();for(let i=0;i<e.length-1;i++)e[i]|=128;return new c(e)}function g(t,e){if(t.length<e)throw Error("parse ends prematurely");return t.shiftN(e)}var p=class{buf;sha256;constructor(){this.buf=new Uint8Array(d),this.sha256=async t=>u.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;t++)if(0===A(this.items[t-1],this.items[t]))throw Error("duplicate item inserted")}unseal(){this.sealed=!1}size(){return this._checkSealed(),this.items.length}getItem(t){if(this._checkSealed(),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;r<e&&i(this.items[r],r);++r);}findLowerBound(t,e,i){return this._checkSealed(),this._checkBounds(t,e),this._binarySearch(this.items,t,e,t=>A(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<a;t++){let t=g(r,d);this.isInitiator&&(o[t]=t)}if(this.isInitiator){u=!0,this.storage.iterate(w,b,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<a?1:0),u=await this.storage.fingerprint(o,o+h);if(o+=h,o===e)n=i;else{let t,e;this.storage.iterate(o-1,o+1,(i,r)=>(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<d&&r[t]===n[t];t++)i++;return this._bound(e.timestamp,e.id.subarray(0,i+1))}}};function y(t){return"string"==typeof t&&(t=function(t){if(t.startsWith("0x")&&(t=t.substr(2)),t.length%2==1)throw Error("odd length of hex string");let e=new Uint8Array(t.length/2);for(let i=0;i<e.length;i++)e[i]=parseInt(t.substr(2*i,2),16);return e}(t)),t}var _=new Array(256);{const t=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];for(let e=0;e<256;e++)_[e]=t[e>>>4&15]+t[15&e]}function x(t){let e="";for(let i=0,r=t.length;i<r;i++)e+=_[t[i]];return e}function E(t,e){for(let i=0;i<t.byteLength;i++){if(t[i]<e[i])return-1;if(t[i]>e[i])return 1}return t.byteLength>e.byteLength?1:t.byteLength<e.byteLength?-1:0}function A(t,e){return t.timestamp===e.timestamp?E(t.id,e.id):t.timestamp-e.timestamp}var L=a.extend("negentropy");async function U(t,e){const i=new w;for(const r of await t.getByFilters(e))i.insert(r.created_at,r.id);return i.seal(),i}function I(t){const e=new w;for(const i of t)e.insert(i.created_at,i.id);return e.seal(),e}async function O(i,n,s,a,u){let d=r(),c=new b(i,u?.frameSizeLimit),f=await c.initiate(),m=f;const g=n.multiplex(()=>(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};