3 lines
13 KiB
JavaScript
3 lines
13 KiB
JavaScript
import{ci as t,cj as e,ck as i,cl as r,cm as n,cn as s,co as o,cp as a}from"./main-AsE4IKjj.js";var h=Array.isArray;function u(){for(var r,n=[],s=0;s<arguments.length;s++)n[s]=arguments[s];return 1===(n=1===(r=n).length&&h(r[0])?r[0]:r).length?t(n[0]):new e(function(e){return function(r){for(var n=[],s=function(s){n.push(t(e[s]).subscribe(i(r,function(t){if(n){for(var e=0;e<n.length;e++)e!==s&&n[e].unsubscribe();n=null}r.next(t)})))},o=0;n&&!r.closed&&o<e.length;o++)s(o)}}
|
|
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */(n))}function l(t,...e){if(!((i=t)instanceof Uint8Array||ArrayBuffer.isView(i)&&"Uint8Array"===i.constructor.name))throw new Error("Uint8Array expected");var i;if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}function c(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function d(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function f(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function g(t,e){return t<<32-e|t>>>e}function p(t){return"string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new Error("string expected");return new Uint8Array((new TextEncoder).encode(t))}(t)),l(t),t}class m{}function w(t){const e=e=>t().update(p(e)).digest(),i=t();return e.outputLen=i.outputLen,e.blockLen=i.blockLen,e.create=()=>t(),e}function b(t,e,i){return t&e^~t&i}function y(t,e,i){return t&e^t&i^e&i}class E extends m{constructor(t,e,i,r){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=t,this.outputLen=e,this.padOffset=i,this.isLE=r,this.buffer=new Uint8Array(t),this.view=f(this.buffer)}update(t){c(this),l(t=p(t));const{view:e,buffer:i,blockLen:r}=this,n=t.length;for(let s=0;s<n;){const o=Math.min(r-this.pos,n-s);if(o===r){const e=f(t);for(;r<=n-s;s+=r)this.process(e,s);continue}i.set(t.subarray(s,s+o),this.pos),this.pos+=o,s+=o,this.pos===r&&(this.process(e,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){c(this),function(t,e){l(t);const i=e.outputLen;if(t.length<i)throw new Error("digestInto() expects output buffer of length at least "+i)}(t,this),this.finished=!0;const{buffer:e,view:i,blockLen:r,isLE:n}=this;let{pos:s}=this;e[s++]=128,d(this.buffer.subarray(s)),this.padOffset>r-s&&(this.process(i,0),s=0);for(let l=s;l<r;l++)e[l]=0;!function(t,e,i,r){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,i,r);const n=BigInt(32),s=BigInt(4294967295),o=Number(i>>n&s),a=Number(i&s),h=r?4:0,u=r?0:4;t.setUint32(e+h,o,r),t.setUint32(e+u,a,r)}(i,r-8,BigInt(8*this.length),n),this.process(i,0);const o=f(t),a=this.outputLen;if(a%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const h=a/4,u=this.get();if(h>u.length)throw new Error("_sha2: outputLen bigger than state");for(let l=0;l<h;l++)o.setUint32(4*l,u[l],n)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const i=t.slice(0,e);return this.destroy(),i}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:e,buffer:i,length:r,finished:n,destroyed:s,pos:o}=this;return t.destroyed=s,t.finished=n,t.length=r,t.pos=o,r%e&&t.buffer.set(i),t}clone(){return this._cloneInto()}}const _=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),x=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),A=new Uint32Array(64);class L extends E{constructor(t=32){super(64,t,8,!1),this.A=0|_[0],this.B=0|_[1],this.C=0|_[2],this.D=0|_[3],this.E=0|_[4],this.F=0|_[5],this.G=0|_[6],this.H=0|_[7]}get(){const{A:t,B:e,C:i,D:r,E:n,F:s,G:o,H:a}=this;return[t,e,i,r,n,s,o,a]}set(t,e,i,r,n,s,o,a){this.A=0|t,this.B=0|e,this.C=0|i,this.D=0|r,this.E=0|n,this.F=0|s,this.G=0|o,this.H=0|a}process(t,e){for(let l=0;l<16;l++,e+=4)A[l]=t.getUint32(e,!1);for(let l=16;l<64;l++){const t=A[l-15],e=A[l-2],i=g(t,7)^g(t,18)^t>>>3,r=g(e,17)^g(e,19)^e>>>10;A[l]=r+A[l-7]+i+A[l-16]|0}let{A:i,B:r,C:n,D:s,E:o,F:a,G:h,H:u}=this;for(let l=0;l<64;l++){const t=u+(g(o,6)^g(o,11)^g(o,25))+b(o,a,h)+x[l]+A[l]|0,e=(g(i,2)^g(i,13)^g(i,22))+y(i,r,n)|0;u=h,h=a,a=o,o=s+t|0,s=n,n=r,r=i,i=t+e|0}i=i+this.A|0,r=r+this.B|0,n=n+this.C|0,s=s+this.D|0,o=o+this.E|0,a=a+this.F|0,h=h+this.G|0,u=u+this.H|0,this.set(i,r,n,s,o,a,h,u)}roundClean(){d(A)}destroy(){this.set(0,0,0,0,0,0,0,0),d(this.buffer)}}const U=w(()=>new L),I=32;class B{_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(t){if(t instanceof B&&(t=t.unwrap()),"number"!=typeof t.length)throw Error("bad length");const e=t.length+this.length;if(this.capacity<e){const t=this._raw,i=Math.max(2*this.capacity,e);this._raw=new Uint8Array(i),this._raw.set(t)}this._raw.set(t,this.length),this.length+=t.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 O(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 T(t){if(0===t)return new B([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 B(e)}function N(t,e){if(t.length<e)throw Error("parse ends prematurely");return t.shiftN(e)}class S{buf;sha256;constructor(){this.buf=new Uint8Array(I),this.sha256=async t=>U.create().update(t).digest()}setToZero(){this.buf=new Uint8Array(I)}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,o=r.getUint32(t,!0);o+=e,o+=n.getUint32(t,!0),o>4294967295&&(i=1),r.setUint32(t,4294967295&o,!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(I);e[0]=1,this.add(e)}async getFingerprint(t){let e=new B;return e.extend(this.buf),e.extend(T(t)),(await this.sha256(e.unwrap())).subarray(0,16)}}class k{items;sealed;constructor(){this.items=[],this.sealed=!1}insert(t,e){if(this.sealed)throw Error("already sealed");if((e=M(e)).byteLength!==I)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(G);for(let t=1;t<this.items.length;t++)if(0===G(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=>G(t,i)<0)}async fingerprint(t,e){let i=new S;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}}class v{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 B;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 B(M(t));this.lastTimestampIn=this.lastTimestampOut=0;let n=new B;n.extend([97]);let s=N(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 o=this.storage.size(),a=this._bound(0),h=0,u=!1;for(;0!==r.length;){let t=new B,s=()=>{u&&(u=!1,t.extend(this.encodeBound(a)),t.extend(T(0)))},l=this.decodeBound(r),c=O(r),d=h,f=this.storage.findLowerBound(h,o,l);if(0===c)u=!0;else if(1===c)0!==F(N(r,16),await this.storage.fingerprint(d,f))?(s(),await this.splitRange(d,f,l,t)):u=!0;else{if(2!==c)throw Error("unexpected mode");{let o=O(r),a={};for(let t=0;t<o;t++){let t=N(r,I);this.isInitiator&&(a[t]=t)}if(this.isInitiator){u=!0,this.storage.iterate(d,f,t=>{let i=t.id;return a[i]?delete a[i]:this.isInitiator&&e.push(this.wantUint8ArrayOutput?i:z(i)),!0});for(let t of Object.values(a))i.push(this.wantUint8ArrayOutput?t:z(t))}else{s();let e=new B,i=0,r=l;this.storage.iterate(d,f,(t,s)=>this.exceededFrameSizeLimit(n.length+e.length)?(r=t,f=s,!1):(e.extend(t.id),i++,!0)),t.extend(this.encodeBound(r)),t.extend(T(2)),t.extend(T(i)),t.extend(e),n.extend(t),t=new B}}}if(this.exceededFrameSizeLimit(n.length+t.length)){let t=await this.storage.fingerprint(f,o);n.extend(this.encodeBound(this._bound(Number.MAX_VALUE))),n.extend(T(1)),n.extend(t);break}n.extend(t),h=f,a=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(T(2)),r.extend(T(n)),this.storage.iterate(t,e,t=>(r.extend(t.id),!0));else{let s=Math.floor(n/16),o=n%16,a=t;for(let t=0;t<16;t++){let n,h=s+(t<o?1:0),u=await this.storage.fingerprint(a,a+h);if(a+=h,a===e)n=i;else{let t,e;this.storage.iterate(a-1,a+1,(i,r)=>(r===a-1?t=i:e=i,!0)),n=this.getMinimalBound(t,e)}r.extend(this.encodeBound(n)),r.extend(T(1)),r.extend(u)}}}_renderOutput(t){let e=t.unwrap();return this.wantUint8ArrayOutput?e:z(e)}exceededFrameSizeLimit(t){return this.frameSizeLimit&&t>this.frameSizeLimit-200}decodeTimestampIn(t){let e=O(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=O(t);if(i>I)throw Error("bound key too long");return{timestamp:e,id:N(t,i)}}encodeTimestampOut(t){if(t===Number.MAX_VALUE)return this.lastTimestampOut=Number.MAX_VALUE,T(0);let e=t;return t-=this.lastTimestampOut,this.lastTimestampOut=e,T(t+1)}encodeBound(t){let e=new B;return e.extend(this.encodeTimestampOut(t.timestamp)),e.extend(T(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<I&&r[t]===n[t];t++)i++;return this._bound(e.timestamp,e.id.subarray(0,i+1))}}}function M(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}const V=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++)V[e]=t[e>>>4&15]+t[15&e]}function z(t){let e="";for(let i=0,r=t.length;i<r;i++)e+=V[t[i]];return e}function F(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 G(t,e){return t.timestamp===e.timestamp?F(t.id,e.id):t.timestamp-e.timestamp}const C=n.extend("negentropy");async function D(t,e){const i=new k;for(const r of await t.getByFilters(e))i.insert(r.created_at,r.id);return i.seal(),i}function X(t){const e=new k;for(const i of t)e.insert(i.created_at,i.id);return e.seal(),e}async function H(t,i,n,h,l){let c=r(),d=new v(t,l?.frameSizeLimit),f=await d.initiate(),g=f;const p=i.multiplex(()=>(C("Sending initial message",c,n,f),["NEG-OPEN",c,n,f]),()=>(C("Closing sync",c),["NEG-CLOSE",c]),t=>("NEG-MSG"===t[0]||"NEG-ERR"===t[0])&&t[1]===c).pipe(s(t=>{if("NEG-ERR"===t[0])throw new Error(t[2]);return t[2]}),o());if(l?.signal?.aborted)return!1;const m=new e(t=>{if(l?.signal?.aborted)return t.next("abort"),void t.complete();const e=()=>{t.next("abort"),t.complete()};return l?.signal?.addEventListener("abort",e),()=>l?.signal?.removeEventListener("abort",e)}),w=p.subscribe({next:t=>C(t),error:()=>{}});try{for(;g&&!0!==l?.signal?.aborted;)try{const t=await a(u(p.pipe(s(t=>({type:"message",data:t}))),m.pipe(s(()=>({type:"abort"})))));if("abort"===t.type||l?.signal?.aborted)return w.unsubscribe(),!1;const[e,i,r]=await d.reconcile(t.data);await h(i,r),g=e}catch(b){if(l?.signal?.aborted)return w.unsubscribe(),!1;throw b}}catch(b){throw w.unsubscribe(),b}finally{w.unsubscribe()}return!0}export{D as buildStorageFromFilter,X as buildStorageVector,H as negentropySync};
|