From d59ca1066c0c952a26298195dbd07e5dea725f51 Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Thu, 15 Apr 2021 15:00:25 +0200 Subject: [PATCH] Bump dist --- dist/bundle-core.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dist/bundle-core.js b/dist/bundle-core.js index 9eaa384..60eb4dc 100644 --- a/dist/bundle-core.js +++ b/dist/bundle-core.js @@ -132,7 +132,7 @@ const Cache=require("hashlru"),RAF=require("polyraf"),Obv=require("obz"),debounc }).call(this)}).call(this,require("buffer").Buffer) },{"./stream":40,"buffer":106,"debug":134,"hashlru":240,"lodash.debounce":335,"obz":402,"polyraf":437}],40:[function(require,module,exports){ -var ltgt=require("ltgt"),looper=require("looper");function Stream(i,t){t=t||{},this.blocks=i,this.live=!!t.live,this.offsets=!1!==t.offsets,this.values=!1!==t.values,this.limit=t.limit||0,this.min=this.max=this.min_inclusive=this.max_inclusive=null,this.cursor=-1,this.count=0,this.hasWritten=!1,this.writing=!1,this.ended=!1,this.skipFirst=!1,this.opts=t,this.blocks.onReady(this._ready.bind(this))}module.exports=Stream,Stream.prototype._ready=function(){this.min=ltgt.lowerBound(this.opts,null),ltgt.lowerBoundInclusive(this.opts)&&(this.min_inclusive=this.min),this.max=ltgt.upperBound(this.opts,null),ltgt.upperBoundInclusive(this.opts)&&(this.max_inclusive=this.max),this.cursor=ltgt.lowerBound(this.opts,0),this.cursor<0&&(this.cursor=0),this.opts.gt>=0&&(this.skipFirst=!0),this.live||0!==this.cursor||-1!==this.blocks.since.value||(this.ended=!0),this.live&&0===this.cursor&&-1===this.blocks.since.value&&(this.cursor=-1),this.resume()},Stream.prototype._writeToSink=function(i){this.hasWritten||(this.hasWritten=!0),this.values?this.offsets?this.sink.write({offset:this.cursor,value:i}):this.sink.write(i):this.sink.write(this.cursor)},Stream.prototype._handleBlock=function(i){for(;;){if(this.sink.paused)return null;const[t,s]=this.blocks.getDataNextOffset(i,this.cursor),e=this.cursor;if(this.skipFirst){if(this.skipFirst=!1,t>0){this.cursor=t;continue}if(0===t)return!0;if(-1===t)return!0===this.live&&(this.writing=!1),!1}if(this.count++,!(null===this.min||this.mine||this.max_inclusive===e))return!1;if(this._writeToSink(s),t>0)this.cursor=t;else{if(0===t)return!0;if(-1===t)return!0===this.live&&(this.writing=!1),!1}if(this.limit>0&&this.count>=this.limit)return!1}},Stream.prototype._resume=function(){if(this.sink&&!this.sink.paused)if(this.ended){if(!this.sink.ended){if(!0===this.ended&&!this.live)return this.abort();if(this.sink.end)return this.sink.end(!0===this.ended?null:this.ended)}}else-1!==this.cursor&&(this.live&&!this.writing&&this.hasWritten||(this.writing=!0,this.blocks.getBlock(this.cursor,(i,t)=>{if(i)return void console.error(i);const s=this._handleBlock(t);if(!0===s)this.cursor=this.blocks.getNextBlockIndex(this.cursor),this._next();else{if(null===s)return;!0!==this.live&&this.abort()}})))},Stream.prototype.resume=function(){this._next=looper(this._resume.bind(this)),this._next()},Stream.prototype.abort=function(i){this.ended=i||!0;var t=this.blocks.streams.indexOf(this);~t&&this.blocks.streams.splice(t,1),!this.sink.ended&&this.sink.end&&(this.sink.ended=!0,this.sink.end(!0===i?null:i))},Stream.prototype.pipe=require("push-stream/pipe"); +var ltgt=require("ltgt"),looper=require("looper");function Stream(i,t){t=t||{},this.blocks=i,this.live=!!t.live,this.offsets=!1!==t.offsets,this.values=!1!==t.values,this.limit=t.limit||0,this.min=this.max=this.min_inclusive=this.max_inclusive=null,this.cursor=-1,this.count=0,this.hasWritten=!1,this.writing=!1,this.ended=!1,this.skipFirst=!1,this.opts=t,this._resumeCallback=this._resumeCallback.bind(this),this.blocks.onReady(this._ready.bind(this))}module.exports=Stream,Stream.prototype._ready=function(){this.min=ltgt.lowerBound(this.opts,null),ltgt.lowerBoundInclusive(this.opts)&&(this.min_inclusive=this.min),this.max=ltgt.upperBound(this.opts,null),ltgt.upperBoundInclusive(this.opts)&&(this.max_inclusive=this.max),this.cursor=ltgt.lowerBound(this.opts,0),this.cursor<0&&(this.cursor=0),this.opts.gt>=0&&(this.skipFirst=!0),this.live||0!==this.cursor||-1!==this.blocks.since.value||(this.ended=!0),this.live&&0===this.cursor&&-1===this.blocks.since.value&&(this.cursor=-1),this.resume()},Stream.prototype._writeToSink=function(i){this.hasWritten||(this.hasWritten=!0),this.values?this.offsets?this.sink.write({offset:this.cursor,value:i}):this.sink.write(i):this.sink.write(this.cursor)},Stream.prototype._handleBlock=function(i){for(;;){if(this.sink.paused)return null;const[t,s]=this.blocks.getDataNextOffset(i,this.cursor),e=this.cursor;if(this.skipFirst){if(this.skipFirst=!1,t>0){this.cursor=t;continue}if(0===t)return!0;if(-1===t)return!0===this.live&&(this.writing=!1),!1}if(this.count++,!(null===this.min||this.mine||this.max_inclusive===e))return!1;if(this._writeToSink(s),t>0)this.cursor=t;else{if(0===t)return!0;if(-1===t)return!0===this.live&&(this.writing=!1),!1}if(this.limit>0&&this.count>=this.limit)return!1}},Stream.prototype._resume=function(){if(this.sink&&!this.sink.paused)if(this.ended){if(!this.sink.ended){if(!0===this.ended&&!this.live)return this.abort();if(this.sink.end)return this.sink.end(!0===this.ended?null:this.ended)}}else-1!==this.cursor&&(this.live&&!this.writing&&this.hasWritten||(this.writing=!0,this.blocks.getBlock(this.cursor,this._resumeCallback)))},Stream.prototype._resumeCallback=function(i,t){if(i)return void console.error(i);const s=this._handleBlock(t);if(!0===s)this.cursor=this.blocks.getNextBlockIndex(this.cursor),this._next();else{if(null===s)return;!0!==this.live&&this.abort()}},Stream.prototype.resume=function(){this._next=looper(this._resume.bind(this)),this._next()},Stream.prototype.abort=function(i){this.ended=i||!0;var t=this.blocks.streams.indexOf(this);~t&&this.blocks.streams.splice(t,1),!this.sink.ended&&this.sink.end&&(this.sink.ended=!0,this.sink.end(!0===i?null:i))},Stream.prototype.pipe=require("push-stream/pipe"); },{"looper":336,"ltgt":337,"push-stream/pipe":576}],41:[function(require,module,exports){ function Single(t,i){if(!(this instanceof Single))return new Single(t,i);this.writing=!1,this.value=null,this.onDrain=null,this._async=t,this._options=i||{},this._setTimeout=i&&i.setTimeout||function(t,i){return setTimeout(t,i)}}module.exports=Single,Single.prototype.write=function(t){this.value=t,this.writing||this._timeout()},Single.prototype._write=function(){this.writing=!0;var t=this.value;this.value=null,this._async(t,this._written.bind(this))},Single.prototype._timeout=function(t){clearTimeout(this._timer),this._timer=this._setTimeout(this._write.bind(this),null==t?Math.max(this._options.min,this._options.max-(Date.now()-this._ts)):t)},Single.prototype._written=function(){if(this._ts=Date.now(),this.writing=!1,this.value)this.write(this.value);else{this.onDrain&&this.onDrain();var t=this._cb;this._cb=null,t&&t()}},Single.prototype.close=function(t){this.writing?this._cb=t:this.value?(this._cb=t,this._timeout(0)):t()}; @@ -1078,7 +1078,7 @@ function getStack(e){return e.stack&&e.name&&e.message?e.stack.substring(e.name. "use strict";function FastIntegerCompression(){}function bytelog(e){return e<128?1:e<16384?2:e<1<<21?3:e<1<<28?4:5}function zigzag_encode(e){return e+e^e>>31}function zigzag_decode(e){return e>>1^-(1&e)}FastIntegerCompression.computeCompressedSizeInBytes=function(e){for(var n=e.length,r=0,t=0;t>>7):g<1<<21?(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14):g<1<<28?(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14&127|128,t[o++]=g>>>21):(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14&127|128,t[o++]=g>>>21&127|128,t[o++]=g>>>28)}return r},FastIntegerCompression.computeHowManyIntegers=function(e){for(var n=new Int8Array(e).length,r=0,t=0;t>>7;return n-r},FastIntegerCompression.uncompress=function(e){for(var n=[],r=new Int8Array(e),t=r.length,o=0;t>o;){var s=r[o++],g=127&s;s>=0?n.push(g):(g|=(127&(s=r[o++]))<<7,s>=0?n.push(g):(g|=(127&(s=r[o++]))<<14,s>=0?n.push(g):(g|=(127&(s=r[o++]))<<21,s>=0?n.push(g):(g|=(s=r[o++])<<28,g>>>=0,n.push(g)))))}return n},FastIntegerCompression.compressSigned=function(e){for(var n=e.length,r=new ArrayBuffer(FastIntegerCompression.computeCompressedSizeInBytesSigned(e)),t=new Int8Array(r),o=0,s=0;s>>7):g<1<<21?(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14):g<1<<28?(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14&127|128,t[o++]=g>>>21):(t[o++]=127&g|128,t[o++]=g>>>7&127|128,t[o++]=g>>>14&127|128,t[o++]=g>>>21&127|128,t[o++]=g>>>28)}return r},FastIntegerCompression.uncompressSigned=function(e){for(var n=[],r=new Int8Array(e),t=r.length,o=0;t>o;){var s=r[o++],g=127&s;s>=0?n.push(zigzag_decode(g)):(g|=(127&(s=r[o++]))<<7,s>=0?n.push(zigzag_decode(g)):(g|=(127&(s=r[o++]))<<14,s>=0?n.push(zigzag_decode(g)):(g|=(127&(s=r[o++]))<<21,s>=0?n.push(zigzag_decode(g)):(g|=(s=r[o++])<<28,n.push(zigzag_decode(g))))))}return n},module.exports=FastIntegerCompression; },{}],193:[function(require,module,exports){ -"use strict";var defaultcomparator=function(t,r){return t0&&(r=e-1>>1,i=this.array[r],this.compare(t,i));)this.array[e]=i,e=r;this.array[e]=t},FastPriorityQueue.prototype.heapify=function(t){var r;for(this.array=t,this.size=t.length,r=this.size>>1;r>=0;r--)this._percolateDown(r)},FastPriorityQueue.prototype._percolateUp=function(t,r){for(var i,e,s=this.array[t];t>0&&(i=t-1>>1,e=this.array[i],r||this.compare(s,e));)this.array[t]=e,t=i;this.array[t]=s},FastPriorityQueue.prototype._percolateDown=function(t){for(var r,i,e,s=this.size,a=this.size>>>1,o=this.array[t];tthis.size-1||t<0))return this._percolateUp(t,!0),this.poll()},FastPriorityQueue.prototype.remove=function(t){for(var r=0;r>=1):s++;return i.length=e,i},FastPriorityQueue.prototype.removeOne=function(t){var r=this._batchRemove(t,1);return r.length>0?r[0]:void 0},FastPriorityQueue.prototype.removeMany=function(t,r){return this._batchRemove(t,r)},FastPriorityQueue.prototype.peek=function(){if(0!=this.size)return this.array[0]},FastPriorityQueue.prototype.poll=function(){if(0!=this.size){var t=this.array[0];return this.size>1?(this.array[0]=this.array[--this.size],this._percolateDown(0)):this.size-=1,t}},FastPriorityQueue.prototype.replaceTop=function(t){if(0!=this.size){var r=this.array[0];return this.array[0]=t,this._percolateDown(0),r}},FastPriorityQueue.prototype.trim=function(){this.array=this.array.slice(0,this.size)},FastPriorityQueue.prototype.isEmpty=function(){return 0===this.size},FastPriorityQueue.prototype.forEach=function(t){if(!this.isEmpty()&&"function"==typeof t)for(var r=0,i=this.clone();!i.isEmpty();)t(i.poll(),r++)},FastPriorityQueue.prototype.kSmallest=function(t){if(0==this.size)return[];t=Math.min(this.size,t);var r=new FastPriorityQueue(this.compare);const i=Math.min((t>0?Math.pow(2,t-1):0)+1,this.size);r.size=i,r.array=this.array.slice(0,i);for(var e=new Array(t),s=0;s0&&(r=e-1>>1,i=this.array[r],this.compare(t,i));)this.array[e]=i,e=r;this.array[e]=t},FastPriorityQueue.prototype.heapify=function(t){var r;for(this.array=t,this.size=t.length,r=this.size>>1;r>=0;r--)this._percolateDown(r)},FastPriorityQueue.prototype._percolateUp=function(t,r){for(var i,e,s=this.array[t];t>0&&(i=t-1>>1,e=this.array[i],r||this.compare(s,e));)this.array[t]=e,t=i;this.array[t]=s},FastPriorityQueue.prototype._percolateDown=function(t){for(var r,i,e,s=this.size,a=this.size>>>1,o=this.array[t];tthis.size-1||t<0))return this._percolateUp(t,!0),this.poll()},FastPriorityQueue.prototype.remove=function(t){for(var r=0;r1?(this.array[0]=this.array[--this.size],this._percolateDown(0)):this.size-=1,t}},FastPriorityQueue.prototype.replaceTop=function(t){if(0!=this.size){var r=this.array[0];return this.array[0]=t,this._percolateDown(0),r}},FastPriorityQueue.prototype.trim=function(){this.array=this.array.slice(0,this.size)},FastPriorityQueue.prototype.isEmpty=function(){return 0===this.size},FastPriorityQueue.prototype.forEach=function(t){if(!this.isEmpty()&&"function"==typeof t)for(var r=0,i=this.clone();!i.isEmpty();)t(i.poll(),r++)},FastPriorityQueue.prototype.kSmallest=function(t){if(0==this.size)return[];t=Math.min(this.size,t);var r=new FastPriorityQueue(this.compare);const i=Math.min((t>0?Math.pow(2,t-1):0)+1,this.size);r.size=i,r.array=this.array.slice(0,i);for(var e=new Array(t),s=0;s{const n=path.parse(e).name;"seq.index"===e?loadTypedArrayFile(path.join(t,e),Uint32Array,(e,t)=>{r[n]=t,a()}):"timestamp.index"===e?loadTypedArrayFile(path.join(t,e),Float64Array,(e,t)=>{r[n]=t,a()}):"sequence.index"===e?loadTypedArrayFile(path.join(t,e),Uint32Array,(e,t)=>{r[n]=t,a()}):e.endsWith(".32prefix")?(r[n]={offset:-1,count:0,tarr:new Uint32Array(16e3),lazy:!0,prefix:32,filepath:path.join(t,e)},a()):e.endsWith(".32prefixmap")?(r[n]={offset:-1,count:0,map:{},lazy:!0,prefix:32,filepath:path.join(t,e)},a()):e.endsWith(".index")?(r[n]={offset:0,bitset:new TypedFastBitSet,lazy:!0,filepath:path.join(t,e)},a()):a()}),push.collect(e))}"undefined"!=typeof window?listFilesIDB(t,a):listFilesFS(t,a)}(()=>{debug("loaded indexes",Object.keys(r)),r.seq||(r.seq={offset:-1,count:0,tarr:new Uint32Array(16e3)}),r.timestamp||(r.timestamp={offset:-1,count:0,tarr:new Float64Array(16e3)}),r.sequence||(r.sequence={offset:-1,count:0,tarr:new Uint32Array(16e3)}),s.batchUpdate(r,["seq","timestamp","sequence"]),o=!0;for(let e=0;ei&&(i=e.since.value,a=new WeakMap,n.ascending=new WeakMap,n.descending=new WeakMap)}function y(e,a,n){if(a.offset<0)return;debug("saving core index: %s",e);const i=path.join(t,e+".index");saveTypedArrayFile(i,a.version||1,a.offset,n,a.tarr)}function m(e,a,n){if(a.offset<0||0===a.bitset.size())return;debug("saving index: %s",e);const i=path.join(t,e+".index");saveBitsetFile(i,a.version||1,a.offset,a.bitset,n)}function g(e,a,n,i){if(a.offset<0)return;debug("saving prefix index: %s",e);const s=a.prefix,r=path.join(t,e+`.${s}prefix`);saveTypedArrayFile(r,a.version||1,a.offset,n,a.tarr,i)}function h(e,a,n,i){if(a.offset<0)return;debug("saving prefix map index: %s",e);const s=a.prefix,r=path.join(t,e+`.${s}prefixmap`);savePrefixMapFile(r,a.version||1,a.offset,n,a.map,i)}function b(e,t){debug("growing index");const a=new t(2*e.tarr.length);a.set(e.tarr),e.tarr=a}function x(e,t){if(e>r.seq.count-1)return e>r.seq.tarr.length-1&&b(r.seq,Uint32Array),r.seq.offset=t,r.seq.tarr[e]=t,r.seq.count=e+1,!0}function q(e,t,a){if(e>r.timestamp.count-1){e>r.timestamp.tarr.length-1&&b(r.timestamp,Float64Array),r.timestamp.offset=t;var n=0;n=bipf.seekKey(a,n,d);const i=bipf.decode(a,n);n=0,n=bipf.seekKey(a,n,l),n=bipf.seekKey(a,n,d);const s=bipf.decode(a,n),o=Math.min(i,s);return r.timestamp.tarr[e]=o,r.timestamp.count=e+1,!0}}function v(e,t,a){if(e>r.sequence.count-1){e>r.sequence.tarr.length-1&&b(r.sequence,Uint32Array),r.sequence.offset=t;var n=0;return n=bipf.seekKey(a,n,l),n=bipf.seekKey(a,n,p),r.sequence.tarr[e]=bipf.decode(a,n),r.sequence.count=e+1,!0}}function F(e,t){const a=e.seek(t);return e.value?!(!~a||0!==bipf.compareString(t,a,e.value)):-1===a}function S(e,t){const a=e.seek(t);if(!~a)return!1;const n=bipf.getEncodedType(t,a);if(n===bipf.types.string)return F(e,t);if(n===bipf.types.array){let n=!1;return bipf.iterate(t,a,(a,i)=>{const s=e.pluck?e.pluck(t,i):i;if(0===bipf.compareString(t,s,e.value))return n=!0,!0}),n}return!1}function E(e,t=0){if(e.length<4){const t=Buffer.alloc(4);return e.copy(t),t.readUInt32LE(0)}return 4===e.length?e.readUInt32LE(0):e.readUInt32LE(t)}function w(e,t,a,n,i){if(n>t.count-1){const s=e.seek(a);if(~s){const i=bipf.slice(a,s);if(i.length){const a=E(i,e.prefixOffset);!function(e,t,a){if(0===a)return;(e[a]||(e[a]=[])).push(t)}(t.map,n,a)}}t.offset=i,t.count=n+1}}function N(e,t,a,n,i){if(n>t.count-1){n>t.tarr.length-1&&b(t,Uint32Array);const s=e.seek(a);if(~s){const i=bipf.slice(a,s);t.tarr[n]=i.length?E(i,e.prefixOffset):0}else t.tarr[n]=0;t.offset=i,t.count=n+1}}function T(e,t,a,n){"EQUAL"===e.type&&F(e.data,a)?t.bitset.add(n):"INCLUDES"===e.type&&S(e.data,a)&&t.bitset.add(n)}function A(e,t,a){if(e.has(t))return e.get(t).push(a),!0;e.set(t,[])}function $(e,t){e.get(t).forEach(e=>e()),e.delete(t)}const U=new Map;const D=new Map;function M(t,a){const n={},i=["seq","timestamp","sequence"],o=t.map(e=>e.data.indexName),f=o.join("|");if(A(D,f,a))return;t.forEach(e=>{e.data.prefix&&e.data.useMap?n[e.data.indexName]={offset:0,count:0,map:{},prefix:"number"==typeof e.data.prefix?e.data.prefix:32}:e.data.prefix?n[e.data.indexName]={offset:0,count:0,tarr:new Uint32Array(16e3),prefix:"number"==typeof e.data.prefix?e.data.prefix:32}:n[e.data.indexName]={offset:0,bitset:new TypedFastBitSet}});let u=0,d=!1,p=!1,l=!1;const c=Date.now();let b=c;function F(e,t,a){for(var i in d&&y("seq",r.seq,e),p&&y("timestamp",r.timestamp,e),l&&y("sequence",r.sequence,e),n){const s=n[i];a&&(r[i]=s),s.offset=t,s.prefix&&s.map?h(i,s,e):s.prefix?g(i,s,e):m(i,s)}}const S=Math.ceil(1e3*Math.random());debug(`log.stream #${S} started, to create indexes ${f}`),e.stream({}).pipe({paused:!1,write:function(e){const a=e.offset,f=e.value;if(x(u,a)&&(d=!0),f){if(q(u,a,f)&&(p=!0),v(u,a,f)&&(l=!0),t.forEach(e=>{e.data.prefix&&e.data.useMap&&w(e.data,n[e.data.indexName],f,u,a),e.data.prefix?N(e.data,n[e.data.indexName],f,u,a):e.data.indexAll?function(e,t,a,n){const i=e.seek(a),s=bipf.decode(a,i),r=safeFilename(e.indexType+"_"+s);t[r]||(t[r]={offset:0,bitset:new TypedFastBitSet}),t[r].bitset.add(n)}(e.data,n,f,u):T(e,n[e.data.indexName],f,u)}),u%1e3==0){s.batchUpdate(r,i),s.batchUpdate(n,o);const e=Date.now();e-b>=6e4&&(b=e,F(u,a,!1))}u++}else u++},end:()=>{const e=u;debug(`log.stream #${S} done ${e} records in ${Date.now()-c}ms`),F(e,r.seq.offset,!0),s.batchUpdate(r,i),s.batchUpdate(n,o),$(D,f),a()}})}const L=new Map;function Q(e,t){if(A(L,e,t))return;debug("lazy loading %s",e);let a=r[e];a.prefix&&a.map?loadPrefixMapFile(a.filepath,(n,i)=>{if(n)return t(n);const{version:s,offset:r,count:o,map:f}=i;a.version=s,a.offset=r,a.count=o,a.map=f,a.lazy=!1,$(L,e),t()}):a.prefix?loadTypedArrayFile(a.filepath,Uint32Array,(n,i)=>{if(n)return t(n);const{version:s,offset:r,count:o,tarr:f}=i;a.version=s,a.offset=r,a.count=o,a.tarr=f,a.lazy=!1,$(L,e),t()}):loadBitsetFile(a.filepath,(n,i)=>{if(n)return t(n);const{version:s,offset:r,bitset:o}=i;a.version=s,a.offset=r,a.bitset=o,a.lazy=!1,$(L,e),t()})}function k(t,a){e.since.value>r[t.data.indexName].offset?function(t,a){const n=r[t.data.indexName],i=["seq","timestamp","sequence",t.data.indexName],o=t.data.indexName;if(A(U,o,a))return;let f=0;if(-1!==n.offset){const{tarr:e}=r.seq,t=n.offset;for(const a=e.length;f=6e4&&(b=e,S(f,a))}f++}else f++},end:()=>{const e=f;debug(`log.stream #${E} done ${f-l} records in ${Date.now()-c}ms`),S(e,r.seq.offset),s.batchUpdate(r,i),$(U,o),a()}})}(t,a):(debug("ensureIndexSync %s is already synced",t.data.indexName),a())}function B(e){k({data:{indexName:"seq"}},e)}function O(e,t,a){k(e,()=>{if("sequence"===e.data.indexName){const n=new TypedFastBitSet,{tarr:i,count:s}=r.sequence;for(let a=0;a{const t=new TypedFastBitSet,{count:a}=r.sequence;t.addRange(0,a),e(t)})}function j(e,t){let a=e[0];return e.slice(1).forEach(e=>{a={type:t,data:[a,e]}}),a}function z(e){if("EQUAL"===e.type||"INCLUDES"===e.type){const t=e.data.value?e.data.value.toString().substring(0,10):"";return`${e.data.indexType}(${t})`}if("GT"===e.type||"GTE"===e.type||"LT"===e.type||"LTE"===e.type){const t=e.data.value?e.data.value.toString().substring(0,10):"";return`${e.type}(${t})`}if("SEQS"===e.type)return`SEQS(${e.seqs.toString().substring(0,10)})`;if("OFFSETS"===e.type)return`OFFSETS(${e.offsets.toString().substring(0,10)})`;if("LIVESEQS"===e.type)return"LIVESEQS()";if("AND"===e.type){e.data.length>2&&(e=j(e.data,"AND"));const t=z(e.data[0]),a=z(e.data[1]);return t?a?`AND(${t},${a})`:t:a}if("OR"===e.type){e.data.length>2&&(e=j(e.data,"AND"));const t=z(e.data[0]),a=z(e.data[1]);return t?a?`OR(${t},${a})`:t:a}return"NOT"===e.type?`NOT(${z(e.data[0])})`:"*"}function P(e,t){"EQUAL"===e.type||"INCLUDES"===e.type?e.data.prefix?k(e,()=>{!function(e,t,a){const n=e.data.value,i=n?E(n,e.data.prefixOffset):0,s=new TypedFastBitSet,r=multicb({pluck:1});if(t.map)t.map[i]&&t.map[i].forEach(e=>{s.add(e),W(e,r())});else{const e=t.count,a=t.tarr;for(let t=0;t{if(t)return console.error(t);const r=e.data.seek;for(let e=0,t=i.length;e{t(r[e.data.indexName].bitset)}):"GT"===e.type?O(e,(e,t)=>e>t.data.value,t):"GTE"===e.type?O(e,(e,t)=>e>=t.data.value,t):"LT"===e.type?O(e,(e,t)=>ee<=t.data.value,t):"OFFSETS"===e.type?B(()=>{!function(e,t){const a=[];e.sort((e,t)=>e-t);const n=e.length,{tarr:i}=r.seq;for(let t=0,s=i.length;t{t(new TypedFastBitSet(e.seqs))}):"LIVESEQS"===e.type?t(new TypedFastBitSet):"AND"===e.type?(e.data.length>2&&(e=j(e.data,"AND")),P(e.data[0],a=>{P(e.data[1],e=>{t(a.new_intersection(e))})})):"OR"===e.type?(e.data.length>2&&(e=j(e.data,"OR")),P(e.data[0],a=>{P(e.data[1],e=>{t(a.new_union(e))})})):"NOT"===e.type?P(e.data[0],e=>{I(a=>{t(a.difference(e))})}):e.type?console.error("Unknown type",e):I(t)}function R(e,t){if(c(),a.has(e))return t(a.get(e));const n=[],i=[];function s(){P(e,n=>{a.set(e,n),t(n)})}function o(){n.length>0?M(n,s):s()}!function e(t){t.forEach(t=>{if("EQUAL"===t.type||"INCLUDES"===t.type){const e=t.data.indexName;r[e]?r[e].lazy&&i.push(e):n.push(t)}else"AND"===t.type||"OR"===t.type||"NOT"===t.type?e(t.data):"SEQS"!==t.type&&"LIVESEQS"!==t.type&&"OFFSETS"!==t.type&&"LT"!==t.type&&"LTE"!==t.type&&"GT"!==t.type&&"GTE"!==t.type&&t.type&&debug("Unknown operator type: "+t.type)})}([e]),B(()=>{n.length>0&&debug("missing indexes: %o",n),i.length>0?function(e,t){push(push.values(e),push.asyncMap(Q),push.collect(t))}(i,o):o()})}function W(t,a){const n=r.seq.tarr[t];e.get(n,(e,i)=>{e&&"flumelog:deleted"===e.code?a(null,{seq:t,offset:n}):a(e,{offset:n,value:i,seq:t})})}function C(e,t){return t.timestamp>e.timestamp}function G(e,t){return e.timestamp>t.timestamp}function V(t,a,i,s,o,f){a=a||0;let u=function(e,t){c();const a=t?"descending":"ascending";if(n[a].has(e))return n[a].get(e);const i=new FastPriorityQueue(t?G:C);return e.array().forEach(e=>{i.add({seq:e,timestamp:r.timestamp.tarr[e]})}),i.trim(),n[a].set(e,i),i}(t,s);const d=u.size;let p;0===d||i<=0?p=[]:0===a&&1===i?p=[u.peek()]:(a>0&&(u=u.clone()).removeMany(()=>!0,a),p=u.kSmallest(i||1/0)),push(push.values(p),push.asyncMap(({seq:t},a)=>{o?a(null,r.seq.tarr[t]):function(t,a){const n=r.seq.tarr[t];e.get(n,(e,t)=>{e&&"flumelog:deleted"===e.code?a():a(e,bipf.decode(t,0))})}(t,a)}),push.filter(e=>!!o||e),push.collect((e,t)=>{f(e,{results:t,total:d})}))}return{onReady:u,paginate:function(e,t,a,n,i,s){u(()=>{const r=Date.now();R(e,o=>{V(o,t,a,n,i,(n,i)=>{n?s(n):(i.duration=Date.now()-r,debugQuery.enabled&&debugQuery(`paginate(${z(e)}), seq: ${t}, limit: ${a}: ${i.duration}ms, total messages: ${i.total}`.replace(/%/g,"%% ")),s(n,i))})})})},all:function(e,t,a,n,i){u(()=>{const s=Date.now();R(e,r=>{V(r,t,1/0,a,n,(t,a)=>{t?i(t):(a.duration=Date.now()-s,debugQuery.enabled&&debugQuery(`all(${z(e)}): ${a.duration}ms, total messages: ${a.total}`.replace(/%/g,"%% ")),i(t,a.results))})})})},count:function(e,t,a,n){u(()=>{const a=Date.now();R(e,i=>{const s=function(e,t,a){return t?e.size()-t:e.size()}(i,t),r=Date.now()-a;debugQuery.enabled&&debugQuery(`count(${z(e)}): ${r}ms, total messages: ${s}`.replace(/%/g,"%% ")),n(null,s)})})},live:function(t){return pull(pullAsync(e=>u(()=>{R(t,t=>{e()})})),pull.map(()=>{let a,n,i=-1;if(function e(t){t.forEach(t=>{if("EQUAL"===t.type||"INCLUDES"===t.type)i=r[t.data.indexName]?r[t.data.indexName].offset:-1;else if("AND"===t.type||"OR"===t.type||"NOT"===t.type)e(t.data);else if("LIVESEQS"===t.type){if(a)throw new Error("Only one seq stream in live supported");a=t.stream}})}([t]),a)n=pull(a,pull.asyncMap((e,t)=>{B(()=>{W(e,t)})}));else{const t=-1===i?{live:!0,gt:r.seq.offset}:{live:!0,gt:i},a=Math.ceil(1e3*Math.random());debug(`log.stream #${a} started, for a live query`),n=toPull(e.stream(t))}return n}),pull.flatten(),pull.filter(e=>(function e(t,a,n){for(let i=0;i{debugQuery.enabled&&debugQuery(`live(${z(t)}): 1 new msg`.replace(/%/g,"%% "))}),pull.map(e=>bipf.decode(e.value,0)))},status:s.obv,indexes:r}}; +const path=require("path"),bipf=require("bipf"),push=require("push-stream"),pull=require("pull-stream"),toPull=require("push-stream-to-pull-stream"),pullAsync=require("pull-async"),TypedFastBitSet=require("typedfastbitset"),bsb=require("binary-search-bounds"),multicb=require("multicb"),FastPriorityQueue=require("fastpriorityqueue"),debug=require("debug")("jitdb"),debugQuery=debug.extend("query"),Status=require("./status"),{saveTypedArrayFile:saveTypedArrayFile,loadTypedArrayFile:loadTypedArrayFile,savePrefixMapFile:savePrefixMapFile,loadPrefixMapFile:loadPrefixMapFile,saveBitsetFile:saveBitsetFile,loadBitsetFile:loadBitsetFile,safeFilename:safeFilename,listFilesIDB:listFilesIDB,listFilesFS:listFilesFS}=require("./files");module.exports=function(e,t){debug("indexes path",t);let n=new WeakMap,a={ascending:new WeakMap,descending:new WeakMap},s=-1;const i=Status(),r={};let o=!1,u=[];function f(e){o?e():u.push(e)}!function(e){function n(n,a){push(push.values(a),push.asyncMap((e,n)=>{const a=path.parse(e).name;"seq.index"===e?loadTypedArrayFile(path.join(t,e),Uint32Array,(e,t)=>{r[a]=t,n()}):"timestamp.index"===e?loadTypedArrayFile(path.join(t,e),Float64Array,(e,t)=>{r[a]=t,n()}):"sequence.index"===e?loadTypedArrayFile(path.join(t,e),Uint32Array,(e,t)=>{r[a]=t,n()}):e.endsWith(".32prefix")?(r[a]={offset:-1,count:0,tarr:new Uint32Array(16e3),lazy:!0,prefix:32,filepath:path.join(t,e)},n()):e.endsWith(".32prefixmap")?(r[a]={offset:-1,count:0,map:{},lazy:!0,prefix:32,filepath:path.join(t,e)},n()):e.endsWith(".index")?(r[a]={offset:0,bitset:new TypedFastBitSet,lazy:!0,filepath:path.join(t,e)},n()):n()}),push.collect(e))}"undefined"!=typeof window?listFilesIDB(t,n):listFilesFS(t,n)}(()=>{debug("loaded indexes",Object.keys(r)),r.seq||(r.seq={offset:-1,count:0,tarr:new Uint32Array(16e3)}),r.timestamp||(r.timestamp={offset:-1,count:0,tarr:new Float64Array(16e3)}),r.sequence||(r.sequence={offset:-1,count:0,tarr:new Uint32Array(16e3)}),i.batchUpdate(r,["seq","timestamp","sequence"]),o=!0;for(let e=0;es&&(s=e.since.value,n=new WeakMap,a.ascending=new WeakMap,a.descending=new WeakMap)}function y(e,n,a){if(n.offset<0)return;debug("saving core index: %s",e);const s=path.join(t,e+".index");saveTypedArrayFile(s,n.version||1,n.offset,a,n.tarr)}function m(e,n,a){if(n.offset<0||0===n.bitset.size())return;debug("saving index: %s",e);const s=path.join(t,e+".index");saveBitsetFile(s,n.version||1,n.offset,n.bitset,a)}function g(e,n,a,s){if(n.offset<0)return;debug("saving prefix index: %s",e);const i=n.prefix,r=path.join(t,e+`.${i}prefix`);saveTypedArrayFile(r,n.version||1,n.offset,a,n.tarr,s)}function h(e,n,a,s){if(n.offset<0)return;debug("saving prefix map index: %s",e);const i=n.prefix,r=path.join(t,e+`.${i}prefixmap`);savePrefixMapFile(r,n.version||1,n.offset,a,n.map,s)}function b(e,t){debug("growing index");const n=new t(2*e.tarr.length);n.set(e.tarr),e.tarr=n}function x(e,t){if(e>r.seq.count-1)return e>r.seq.tarr.length-1&&b(r.seq,Uint32Array),r.seq.offset=t,r.seq.tarr[e]=t,r.seq.count=e+1,!0}function q(e,t,n){if(e>r.timestamp.count-1){e>r.timestamp.tarr.length-1&&b(r.timestamp,Float64Array),r.timestamp.offset=t;var a=0;a=bipf.seekKey(n,a,p);const s=bipf.decode(n,a);a=0,a=bipf.seekKey(n,a,l),a=bipf.seekKey(n,a,p);const i=bipf.decode(n,a),o=Math.min(s,i);return r.timestamp.tarr[e]=o,r.timestamp.count=e+1,!0}}function v(e,t,n){if(e>r.sequence.count-1){e>r.sequence.tarr.length-1&&b(r.sequence,Uint32Array),r.sequence.offset=t;var a=0;return a=bipf.seekKey(n,a,l),a=bipf.seekKey(n,a,d),r.sequence.tarr[e]=bipf.decode(n,a),r.sequence.count=e+1,!0}}function F(e,t){const n=e.seek(t);return e.value?!(!~n||0!==bipf.compareString(t,n,e.value)):-1===n}function S(e,t){const n=e.seek(t);if(!~n)return!1;const a=bipf.getEncodedType(t,n);if(a===bipf.types.string)return F(e,t);if(a===bipf.types.array){let a=!1;return bipf.iterate(t,n,(n,s)=>{const i=e.pluck?e.pluck(t,s):s;if(0===bipf.compareString(t,i,e.value))return a=!0,!0}),a}return!1}function w(e,t=0){if(e.length<4){const t=Buffer.alloc(4);return e.copy(t),t.readUInt32LE(0)}return 4===e.length?e.readUInt32LE(0):e.readUInt32LE(t)}function E(e,t,n,a,s){if(a>t.count-1){const i=e.seek(n);if(~i){const s=bipf.slice(n,i);if(s.length){const n=w(s,e.prefixOffset);!function(e,t,n){if(0===n)return;(e[n]||(e[n]=[])).push(t)}(t.map,a,n)}}t.offset=s,t.count=a+1}}function N(e,t,n,a,s){if(a>t.count-1){a>t.tarr.length-1&&b(t,Uint32Array);const i=e.seek(n);if(~i){const s=bipf.slice(n,i);t.tarr[a]=s.length?w(s,e.prefixOffset):0}else t.tarr[a]=0;t.offset=s,t.count=a+1}}function T(e,t,n,a){"EQUAL"===e.type&&F(e.data,n)?t.bitset.add(a):"INCLUDES"===e.type&&S(e.data,n)&&t.bitset.add(a)}function A(e,t,n){if(e.has(t))return e.get(t).push(n),!0;e.set(t,[])}function $(e,t){e.get(t).forEach(e=>e()),e.delete(t)}const M=new Map;const U=new Map;function D(t,n){const a={},s=["seq","timestamp","sequence"],o=t.map(e=>e.data.indexName),u=o.join("|");if(A(U,u,n))return;t.forEach(e=>{e.data.prefix&&e.data.useMap?a[e.data.indexName]={offset:0,count:0,map:{},prefix:"number"==typeof e.data.prefix?e.data.prefix:32}:e.data.prefix?a[e.data.indexName]={offset:0,count:0,tarr:new Uint32Array(16e3),prefix:"number"==typeof e.data.prefix?e.data.prefix:32}:a[e.data.indexName]={offset:0,bitset:new TypedFastBitSet}});let f=0,p=!1,d=!1,l=!1;const c=Date.now();let b=c;function F(e,t,n){for(var s in p&&y("seq",r.seq,e),d&&y("timestamp",r.timestamp,e),l&&y("sequence",r.sequence,e),a){const i=a[s];n&&(r[s]=i),i.offset=t,i.prefix&&i.map?h(s,i,e):i.prefix?g(s,i,e):m(s,i)}}const S=Math.ceil(1e3*Math.random());debug(`log.stream #${S} started, to create indexes ${u}`),e.stream({}).pipe({paused:!1,write:function(e){const n=e.offset,u=e.value;if(x(f,n)&&(p=!0),u){if(q(f,n,u)&&(d=!0),v(f,n,u)&&(l=!0),t.forEach(e=>{e.data.prefix&&e.data.useMap&&E(e.data,a[e.data.indexName],u,f,n),e.data.prefix?N(e.data,a[e.data.indexName],u,f,n):e.data.indexAll?function(e,t,n,a){const s=e.seek(n),i=bipf.decode(n,s),r=safeFilename(e.indexType+"_"+i);t[r]||(t[r]={offset:0,bitset:new TypedFastBitSet}),t[r].bitset.add(a)}(e.data,a,u,f):T(e,a[e.data.indexName],u,f)}),f%1e3==0){i.batchUpdate(r,s),i.batchUpdate(a,o);const e=Date.now();e-b>=6e4&&(b=e,F(f,n,!1))}f++}else f++},end:()=>{const e=f;debug(`log.stream #${S} done ${e} records in ${Date.now()-c}ms`),F(e,r.seq.offset,!0),i.batchUpdate(r,s),i.batchUpdate(a,o),$(U,u),n()}})}const k=new Map;function L(e,t){if(A(k,e,t))return;debug("lazy loading %s",e);let n=r[e];n.prefix&&n.map?loadPrefixMapFile(n.filepath,(a,s)=>{if(a)return t(a);const{version:i,offset:r,count:o,map:u}=s;n.version=i,n.offset=r,n.count=o,n.map=u,n.lazy=!1,$(k,e),t()}):n.prefix?loadTypedArrayFile(n.filepath,Uint32Array,(a,s)=>{if(a)return t(a);const{version:i,offset:r,count:o,tarr:u}=s;n.version=i,n.offset=r,n.count=o,n.tarr=u,n.lazy=!1,$(k,e),t()}):loadBitsetFile(n.filepath,(a,s)=>{if(a)return t(a);const{version:i,offset:r,bitset:o}=s;n.version=i,n.offset=r,n.bitset=o,n.lazy=!1,$(k,e),t()})}function Q(t,n){e.since.value>r[t.data.indexName].offset?function(t,n){const a=r[t.data.indexName],s=["seq","timestamp","sequence",t.data.indexName],o=t.data.indexName;if(A(M,o,n))return;let u=0;if(-1!==a.offset){const{tarr:e}=r.seq,t=a.offset;for(const n=e.length;u=6e4&&(b=e,S(u,n))}u++}else u++},end:()=>{const e=u;debug(`log.stream #${w} done ${u-l} records in ${Date.now()-c}ms`),S(e,r.seq.offset),i.batchUpdate(r,s),$(M,o),n()}})}(t,n):(debug("ensureIndexSync %s is already synced",t.data.indexName),n())}function B(e){Q({data:{indexName:"seq"}},e)}function O(e,t,n){Q(e,()=>{if("sequence"===e.data.indexName){const a=new TypedFastBitSet,{tarr:s,count:i}=r.sequence;for(let n=0;n{const t=new TypedFastBitSet,{count:n}=r.sequence;t.addRange(0,n),e(t)})}function z(e,t){let n=e[0];return e.slice(1).forEach(e=>{n={type:t,data:[n,e]}}),n}function j(e){if("EQUAL"===e.type||"INCLUDES"===e.type){const t=e.data.value?e.data.value.toString().substring(0,10):"";return`${e.data.indexType}(${t})`}if("GT"===e.type||"GTE"===e.type||"LT"===e.type||"LTE"===e.type){const t=e.data.value?e.data.value.toString().substring(0,10):"";return`${e.type}(${t})`}if("SEQS"===e.type)return`SEQS(${e.seqs.toString().substring(0,10)})`;if("OFFSETS"===e.type)return`OFFSETS(${e.offsets.toString().substring(0,10)})`;if("LIVESEQS"===e.type)return"LIVESEQS()";if("AND"===e.type){e.data.length>2&&(e=z(e.data,"AND"));const t=j(e.data[0]),n=j(e.data[1]);return t?n?`AND(${t},${n})`:t:n}if("OR"===e.type){e.data.length>2&&(e=z(e.data,"AND"));const t=j(e.data[0]),n=j(e.data[1]);return t?n?`OR(${t},${n})`:t:n}return"NOT"===e.type?`NOT(${j(e.data[0])})`:"*"}function P(e,t){if(e||t){if(e&&!t)return e;if(!e&&t)return t;{const n=new Map(e);for(let a of t.keys()){const s=e.get(a)||[],i=t.get(a);n.set(a,[...s,...i])}return n}}return null}function R(e,t){"EQUAL"===e.type||"INCLUDES"===e.type?e.data.prefix?Q(e,()=>{!function(e,t,n){const a=e.data.value,s=a?w(a,e.data.prefixOffset):0,i=new TypedFastBitSet,r=new Map,o=e.data.seek;function u(e){if(!e)return!1;const t=o(e),n=bipf.slice(e,t);if(a){if(Buffer.compare(n,a))return!1}else if(~t)return!1;return!0}if(t.map)t.map[s]&&t.map[s].forEach(e=>{i.add(e),r.set(e,[u])});else{const e=t.count,n=t.tarr;for(let t=0;t{t(r[e.data.indexName].bitset)}):"GT"===e.type?O(e,(e,t)=>e>t.data.value,t):"GTE"===e.type?O(e,(e,t)=>e>=t.data.value,t):"LT"===e.type?O(e,(e,t)=>ee<=t.data.value,t):"OFFSETS"===e.type?B(()=>{!function(e,t){const n=[];e.sort((e,t)=>e-t);const a=e.length,{tarr:s}=r.seq;for(let t=0,i=s.length;t{t(new TypedFastBitSet(e.seqs))}):"LIVESEQS"===e.type?t(new TypedFastBitSet):"AND"===e.type?(e.data.length>2&&(e=z(e.data,"AND")),R(e.data[0],(n,a)=>{R(e.data[1],(e,s)=>{t(n.new_intersection(e),P(a,s))})})):"OR"===e.type?(e.data.length>2&&(e=z(e.data,"OR")),R(e.data[0],(n,a)=>{R(e.data[1],(e,s)=>{t(n.new_union(e),P(a,s))})})):"NOT"===e.type?R(e.data[0],(e,n)=>{I(a=>{t(a.difference(e),n)})}):e.type?console.error("Unknown type",e):I(t)}function W(e,t){if(c(),n.has(e))return t(null,n.get(e));const[a,s]=function(e){const t=[],n=[];return function e(a){a.forEach(a=>{if("EQUAL"===a.type||"INCLUDES"===a.type){const e=a.data.indexName;r[e]?r[e].lazy&&n.push(e):t.push(a)}else"AND"===a.type||"OR"===a.type||"NOT"===a.type?e(a.data):"SEQS"!==a.type&&"LIVESEQS"!==a.type&&"OFFSETS"!==a.type&&"LT"!==a.type&&"LTE"!==a.type&&"GT"!==a.type&&"GTE"!==a.type&&a.type&&debug("Unknown operator type: "+a.type)})}([e]),[t,n]}(e);a.length>0&&debug("missing indexes: %o",a),push(push.values([null]),push.asyncMap((e,t)=>B(t)),push.asyncMap((e,t)=>{if(0===s.length)return t();push(push.values(s),push.asyncMap(L),push.collect(t))}),push.asyncMap((e,t)=>{if(0===a.length)return t();D(a,t)}),push.asyncMap((t,a)=>{R(e,(t,s)=>{n.set(e,[t,s]),a(null,[t,s])})}),push.collect((e,n)=>{e?t(e):t(null,n[0])}))}function C(t,n){const a=r.seq.tarr[t];e.get(a,(e,s)=>{e&&"flumelog:deleted"===e.code?n(null,{seq:t,offset:a}):n(e,{offset:a,value:s,seq:t})})}function G(e,t){return t.timestamp>e.timestamp}function V(e,t){return e.timestamp>t.timestamp}function K(e,t,n,a){const s=t.get(e);if(!s)return a(null,e);C(e,(t,i)=>{if(t)return a(t);const r=i.value;let o=!0;s&&(o=s.every(e=>e(r))),o?(n[e]=r,a(null,e)):a()})}function _(e,t,n){if(0===e.size||n<=0)return[];if(0===t&&1===n)return[e.peek()];if(t>0){e=e.clone();for(let n=0;n{s.add({seq:e,timestamp:r.timestamp.tarr[e]})}),s.trim(),a[n].set(e,s),s}(t,o),d=p.size,l={};function y(t,n){push(push.values(t),push.asyncMap((t,n)=>{u?n(null,r.seq.tarr[t]):function(t,n,a){if(n[t]){const e=n[t],s=bipf.decode(e,0);return void a(null,s)}const s=r.seq.tarr[t];e.get(s,(e,t)=>{e&&"flumelog:deleted"===e.code?a():a(e,bipf.decode(t,0))})}(t,l,n)}),push.filter(e=>!!u||e),push.collect((e,t)=>{f(e,{results:t,total:n})}))}if(n){function m(e,t,a,s){const i=multicb({pluck:1});for(let e=0;e(function e(t,n,a){if(t)return f(t);const s=a.length;a=a.filter(e=>void 0!==e);const r=n+a.lengthe(t,n+s,a)):y(a,d)})(e,s,t))}else{y(_(p,s,i).map(e=>e.seq),d)}}return{onReady:f,paginate:function(e,t,n,a,s,i){f(()=>{const r=Date.now();W(e,(o,u)=>{if(o)return i(o);const[f,p]=u;H(f,p,t,n,a,s,(a,s)=>{a?i(a):(s.duration=Date.now()-r,debugQuery.enabled&&debugQuery(`paginate(${j(e)}), seq: ${t}, limit: ${n}: ${s.duration}ms, total messages: ${s.total}`.replace(/%/g,"%% ")),i(null,s))})})})},all:function(e,t,n,a,s){f(()=>{const i=Date.now();W(e,(r,o)=>{if(r)return s(r);const[u,f]=o;H(u,f,t,1/0,n,a,(t,n)=>{t?s(t):(n.duration=Date.now()-i,debugQuery.enabled&&debugQuery(`all(${j(e)}): ${n.duration}ms, total messages: ${n.total}`.replace(/%/g,"%% ")),s(null,n.results))})})})},count:function(e,t,n,a){f(()=>{const n=Date.now();W(e,(s,i)=>{if(s)return a(s);const[r]=i,o=function(e,t,n){return t?e.size()-t:e.size()}(r,t),u=Date.now()-n;debugQuery.enabled&&debugQuery(`count(${j(e)}): ${u}ms, total messages: ${o}`.replace(/%/g,"%% ")),a(null,o)})})},live:function(t){return pull(pullAsync(e=>f(()=>{W(t,t=>e(t))})),pull.map(()=>{let n,a,s=-1;if(function e(t){t.forEach(t=>{if("EQUAL"===t.type||"INCLUDES"===t.type)s=r[t.data.indexName]?r[t.data.indexName].offset:-1;else if("AND"===t.type||"OR"===t.type||"NOT"===t.type)e(t.data);else if("LIVESEQS"===t.type){if(n)throw new Error("Only one seq stream in live supported");n=t.stream}})}([t]),n)a=pull(n,pull.asyncMap((e,t)=>{B(()=>{C(e,t)})}));else{const t=-1===s?{live:!0,gt:r.seq.offset}:{live:!0,gt:s},n=Math.ceil(1e3*Math.random());debug(`log.stream #${n} started, for a live query`),a=toPull(e.stream(t))}return a}),pull.flatten(),pull.filter(e=>(function e(t,n,a){for(let s=0;s{debugQuery.enabled&&debugQuery(`live(${j(t)}): 1 new msg`.replace(/%/g,"%% "))}),pull.map(e=>bipf.decode(e.value,0)))},status:i.obv,indexes:r}}; }).call(this)}).call(this,require("buffer").Buffer) },{"./files":276,"./status":279,"binary-search-bounds":58,"bipf":59,"buffer":106,"debug":134,"fastpriorityqueue":193,"multicb":354,"path":421,"pull-async":459,"pull-stream":525,"push-stream":575,"push-stream-to-pull-stream":572,"typedfastbitset":841}],278:[function(require,module,exports){ (function (Buffer){(function (){ -const bipf=require("bipf"),traverse=require("traverse"),promisify=require("promisify-4loc"),pull=require("pull-stream"),pullAsync=require("pull-async"),pullAwaitable=require("pull-awaitable"),cat=require("pull-cat"),{safeFilename:safeFilename}=require("./files");function copyMeta(e,t){e.meta&&(t.meta=e.meta)}function updateMeta(e,t,n){const r=Object.assign({},e);return r.meta[t]=n,r}function extractMeta(e){return e.meta}function toBufferOrFalsy(e){return e?Buffer.isBuffer(e)?e:Buffer.from(e):e}function seekFromDesc(e){const t=e.split(".");return(e,n=0)=>{var r=n;for(let n of t)if(!~(r=bipf.seekKey(e,r,Buffer.from(n))))return;return r}}function query(...e){let t=e[0];for(let n=1,r=e.length;n{const t=JSON.stringify(e.meta,(e,t)=>"jitdb"===e?void 0:t);return console.log("debug",JSON.stringify(e,(e,t)=>"meta"===e?void 0:"task"===e&&"function"==typeof t?"[Function]":"value"===e&&"Buffer"===t.type?Buffer.from(t.data).toString():t,2),"{}"===t?"":"meta: "+t),e}}function getIndexName(e,t,n){return safeFilename(e.prefix?e.useMap?t+"__map":t:t+"_"+n)}function slowEqual(e,t,n){n=n||{};const r=seekFromDesc(e),a=toBufferOrFalsy(t),u=a?a.toString():"",i=e.replace(/\./g,"_");return{type:"EQUAL",data:{seek:r,value:a,indexType:i,indexName:getIndexName(n,i,u),useMap:n.useMap,indexAll:n.indexAll,prefix:n.prefix,prefixOffset:n.prefixOffset}}}function equal(e,t,n){if(!(n=n||{}).indexType)throw new Error("equal() operator needs an indexType in the 3rd arg");const r=toBufferOrFalsy(t),a=r?r.toString():"",u=n.indexType;return{type:"EQUAL",data:{seek:e,value:r,indexType:u,indexName:getIndexName(n,u,a),useMap:n.useMap,indexAll:n.indexAll,prefix:n.prefix,prefixOffset:n.prefixOffset}}}function slowIncludes(e,t,n){n=n||{};const r=seekFromDesc(e),a=toBufferOrFalsy(t);if(!a)throw new Error("slowIncludes() 2nd arg needs to be truthy");const u=a.toString(),i=e.replace(/\./g,"_"),o=safeFilename(i+"_"+u),l=n.pluck&&"string"==typeof n.pluck?seekFromDesc(n.pluck):n.pluck;return{type:"INCLUDES",data:{seek:r,value:a,indexType:i,indexName:o,indexAll:n.indexAll,pluck:l}}}function includes(e,t,n){if(!(n=n||{}).indexType)throw new Error("includes() operator needs an indexType in the 3rd arg");const r=toBufferOrFalsy(t);if(!r)throw new Error("includes() 2nd arg needs to be truthy");const a=r.toString(),u=n.indexType;return{type:"INCLUDES",data:{seek:e,value:r,indexType:u,indexName:safeFilename(u+"_"+a),indexAll:n.indexAll,pluck:n.pluck}}}function gt(e,t){if("number"!=typeof e)throw new Error("gt() needs a number arg");return{type:"GT",data:{value:e,indexName:t}}}function gte(e,t){if("number"!=typeof e)throw new Error("gte() needs a number arg");return{type:"GTE",data:{value:e,indexName:t}}}function lt(e,t){if("number"!=typeof e)throw new Error("lt() needs a number arg");return{type:"LT",data:{value:e,indexName:t}}}function lte(e,t){if("number"!=typeof e)throw new Error("lte() needs a number arg");return{type:"LTE",data:{value:e,indexName:t}}}function seqs(e){return{type:"SEQS",seqs:e}}function liveSeqs(e){return{type:"LIVESEQS",stream:e}}function offsets(e){return{type:"OFFSETS",offsets:e}}function deferred(e){return{type:"DEFERRED",task:e}}function not(e){return{type:"NOT",data:[e]}}function and(...e){return(t,n)=>{const r=e.map(e=>"function"==typeof e?e(t,!0):e).filter(e=>!!e),a=t&&t.type&&!n?{type:"AND",data:[t,...r]}:r.length>1?{type:"AND",data:r}:r[0];return t&©Meta(t,a),a}}function or(...e){return(t,n)=>{const r=e.map(e=>"function"==typeof e?e(t,!0):e).filter(e=>!!e),a=t&&t.type&&!n?{type:"OR",data:[t,...r]}:r.length>1?{type:"OR",data:r}:r[0];return t&©Meta(t,a),a}}function fromDB(e){return{meta:{jitdb:e}}}function live(e){return e&&e.old?e=>updateMeta(e,"live","liveAndOld"):e=>updateMeta(e,"live","liveOnly")}function count(){return e=>updateMeta(e,"count",!0)}function descending(){return e=>updateMeta(e,"descending",!0)}function startFrom(e){return t=>updateMeta(t,"seq",e)}function paginate(e){return t=>updateMeta(t,"pageSize",e)}function asOffsets(){return e=>updateMeta(e,"asOffsets",!0)}async function executeDeferredOps(e,t){const n=[];return traverse.forEach(e,function(e){if(e){if("meta"===this.key&&e.jitdb)return this.block();"DEFERRED"===e.type&&e.task&&n.push([this.path,e]),Array.isArray(e)||"AND"===e.type||"OR"===e.type||this.block()}}),0===n.length?e:((await Promise.all(n.map(([e,n])=>promisify(n.task)(t).then(t=>t?"function"==typeof t?[e,t()]:[e,t]:[e,{}])))).forEach(([n,r])=>{r.meta=t,0===n.length?e=r:traverse.set(e,n,r)}),e)}function toCallback(e){return t=>{const n=extractMeta(t);executeDeferredOps(t,n).then(t=>{const r=n.seq||0,{pageSize:a,descending:u,asOffsets:i}=n;n.count?n.jitdb.count(t,r,u,e):a?n.jitdb.paginate(t,r,a,u,i,e):n.jitdb.all(t,r,u,i,e)}).catch(t=>{e(t)})}}function toPromise(){return e=>promisify(t=>toCallback(t)(e))()}function toPullStream(){return e=>{const t=extractMeta(e);function n(e){let n=t.seq||0,r=1/0;const a=t.pageSize||1;let u=!1;return function(i,o){return i?o(i):n>=r||u?o(!0):void(t.count?(u=!0,t.jitdb.count(e,n,t.descending,o)):t.jitdb.paginate(e,n,a,t.descending,t.asOffsets,(e,u)=>{if(e)return o(e);0===u.total?o(!0):(r=u.total,n+=a,o(null,t.pageSize?u.results:u.results[0]))}))}}return pull(pullAsync(n=>{executeDeferredOps(e,t).then(e=>n(null,e),e=>n(e))}),pull.map(e=>"liveOnly"===t.live?t.jitdb.live(e):"liveAndOld"===t.live?cat([n(e),t.jitdb.live(e)]):n(e)),pull.flatten())}}function toAsyncIter(){return async function*(e){const t=toPullStream()(e);for await(let e of pullAwaitable(t))yield e}}module.exports={fromDB:fromDB,query:query,live:live,slowEqual:slowEqual,equal:equal,slowIncludes:slowIncludes,includes:includes,not:not,gt:gt,gte:gte,lt:lt,lte:lte,and:and,or:or,deferred:deferred,liveSeqs:liveSeqs,seqs:seqs,offsets:offsets,descending:descending,count:count,startFrom:startFrom,paginate:paginate,asOffsets:asOffsets,toCallback:toCallback,toPullStream:toPullStream,toPromise:toPromise,toAsyncIter:toAsyncIter,debug:debug}; +const bipf=require("bipf"),traverse=require("traverse"),promisify=require("promisify-4loc"),pull=require("pull-stream"),pullAsync=require("pull-async"),pullAwaitable=require("pull-awaitable"),cat=require("pull-cat"),{safeFilename:safeFilename}=require("./files");function copyMeta(e,t){e.meta&&(t.meta=e.meta)}function updateMeta(e,t,n){const r=Object.assign({},e);return r.meta[t]=n,r}function extractMeta(e){return e.meta}function toBufferOrFalsy(e){return e?Buffer.isBuffer(e)?e:Buffer.from(e):e}function seekFromDesc(e){const t=e.split(".");return(e,n=0)=>{var r=n;for(let n of t)if(!~(r=bipf.seekKey(e,r,Buffer.from(n))))return;return r}}function getIndexName(e,t,n){return safeFilename(e.prefix?e.useMap?t+"__map":t:t+"_"+n)}function query(...e){let t=e[0];for(let n=1,r=e.length;n{const t=JSON.stringify(e.meta,(e,t)=>"jitdb"===e?void 0:t);return console.log("debug",JSON.stringify(e,(e,t)=>"meta"===e?void 0:"task"===e&&"function"==typeof t?"[Function]":"value"===e&&"Buffer"===t.type?Buffer.from(t.data).toString():t,2),"{}"===t?"":"meta: "+t),e}}function slowEqual(e,t,n){n=n||{};const r=seekFromDesc(e),a=toBufferOrFalsy(t),u=a?a.toString():"",i=e.replace(/\./g,"_");return{type:"EQUAL",data:{seek:r,value:a,indexType:i,indexName:getIndexName(n,i,u),useMap:n.useMap,indexAll:n.indexAll,prefix:n.prefix,prefixOffset:n.prefixOffset}}}function equal(e,t,n){if(!(n=n||{}).indexType)throw new Error("equal() operator needs an indexType in the 3rd arg");const r=toBufferOrFalsy(t),a=r?r.toString():"",u=n.indexType;return{type:"EQUAL",data:{seek:e,value:r,indexType:u,indexName:getIndexName(n,u,a),useMap:n.useMap,indexAll:n.indexAll,prefix:n.prefix,prefixOffset:n.prefixOffset}}}function slowIncludes(e,t,n){n=n||{};const r=seekFromDesc(e),a=toBufferOrFalsy(t);if(!a)throw new Error("slowIncludes() 2nd arg needs to be truthy");const u=a.toString(),i=e.replace(/\./g,"_"),o=safeFilename(i+"_"+u),l=n.pluck&&"string"==typeof n.pluck?seekFromDesc(n.pluck):n.pluck;return{type:"INCLUDES",data:{seek:r,value:a,indexType:i,indexName:o,indexAll:n.indexAll,pluck:l}}}function includes(e,t,n){if(!(n=n||{}).indexType)throw new Error("includes() operator needs an indexType in the 3rd arg");const r=toBufferOrFalsy(t);if(!r)throw new Error("includes() 2nd arg needs to be truthy");const a=r.toString(),u=n.indexType;return{type:"INCLUDES",data:{seek:e,value:r,indexType:u,indexName:safeFilename(u+"_"+a),indexAll:n.indexAll,pluck:n.pluck}}}function gt(e,t){if("number"!=typeof e)throw new Error("gt() needs a number arg");return{type:"GT",data:{value:e,indexName:t}}}function gte(e,t){if("number"!=typeof e)throw new Error("gte() needs a number arg");return{type:"GTE",data:{value:e,indexName:t}}}function lt(e,t){if("number"!=typeof e)throw new Error("lt() needs a number arg");return{type:"LT",data:{value:e,indexName:t}}}function lte(e,t){if("number"!=typeof e)throw new Error("lte() needs a number arg");return{type:"LTE",data:{value:e,indexName:t}}}function seqs(e){return{type:"SEQS",seqs:e}}function liveSeqs(e){return{type:"LIVESEQS",stream:e}}function offsets(e){return{type:"OFFSETS",offsets:e}}function deferred(e){return{type:"DEFERRED",task:e}}function not(e){return{type:"NOT",data:[e]}}function and(...e){const t=e.filter(e=>!!e);return 0===t.length?{}:1===t.length?t[0]:{type:"AND",data:t}}function or(...e){const t=e.filter(e=>!!e);return 0===t.length?{}:1===t.length?t[0]:{type:"OR",data:t}}function where(...e){return t=>{if(1!==e.length)throw new Error("where() accepts only one argument");const n=e[0];if(!n)return t;const r=t.type?{type:"AND",data:[t,n]}:n;return copyMeta(t,r),r}}function fromDB(e){return{meta:{jitdb:e}}}function live(e){return e&&e.old?e=>updateMeta(e,"live","liveAndOld"):e=>updateMeta(e,"live","liveOnly")}function count(){return e=>updateMeta(e,"count",!0)}function descending(){return e=>updateMeta(e,"descending",!0)}function startFrom(e){return t=>updateMeta(t,"seq",e)}function paginate(e){return t=>updateMeta(t,"pageSize",e)}function asOffsets(){return e=>updateMeta(e,"asOffsets",!0)}async function executeDeferredOps(e,t){const n=[];return traverse.forEach(e,function(e){if(e){if("meta"===this.key&&e.jitdb)return this.block();"DEFERRED"===e.type&&e.task&&n.push([this.path,e]),Array.isArray(e)||"AND"===e.type||"OR"===e.type||this.block()}}),0===n.length?e:((await Promise.all(n.map(([e,n])=>promisify(n.task)(t).then(t=>t?"function"==typeof t?[e,t()]:[e,t]:[e,{}])))).forEach(([n,r])=>{r.meta=t,0===n.length?e=r:traverse.set(e,n,r)}),e)}function toCallback(e){return t=>{const n=extractMeta(t);executeDeferredOps(t,n).then(t=>{const r=n.seq||0,{pageSize:a,descending:u,asOffsets:i}=n;n.count?n.jitdb.count(t,r,u,e):a?n.jitdb.paginate(t,r,a,u,i,e):n.jitdb.all(t,r,u,i,e)}).catch(t=>{e(t)})}}function toPromise(){return e=>promisify(t=>toCallback(t)(e))()}function toPullStream(){return e=>{const t=extractMeta(e);function n(e){let n=t.seq||0,r=1/0;const a=t.pageSize||1;let u=!1;return function(i,o){return i?o(i):n>=r||u?o(!0):void(t.count?(u=!0,t.jitdb.count(e,n,t.descending,o)):t.jitdb.paginate(e,n,a,t.descending,t.asOffsets,(e,u)=>{if(e)return o(e);0===u.total?o(!0):(r=u.total,n+=a,o(null,t.pageSize?u.results:u.results[0]))}))}}return pull(pullAsync(n=>{executeDeferredOps(e,t).then(e=>n(null,e),e=>n(e))}),pull.map(e=>"liveOnly"===t.live?t.jitdb.live(e):"liveAndOld"===t.live?cat([n(e),t.jitdb.live(e)]):n(e)),pull.flatten())}}function toAsyncIter(){return async function*(e){const t=toPullStream()(e);for await(let e of pullAwaitable(t))yield e}}module.exports={fromDB:fromDB,query:query,live:live,slowEqual:slowEqual,equal:equal,slowIncludes:slowIncludes,includes:includes,where:where,not:not,gt:gt,gte:gte,lt:lt,lte:lte,and:and,or:or,deferred:deferred,liveSeqs:liveSeqs,seqs:seqs,offsets:offsets,descending:descending,count:count,startFrom:startFrom,paginate:paginate,asOffsets:asOffsets,toCallback:toCallback,toPullStream:toPullStream,toPromise:toPromise,toAsyncIter:toAsyncIter,debug:debug}; }).call(this)}).call(this,require("buffer").Buffer) },{"./files":276,"bipf":59,"buffer":106,"promisify-4loc":442,"pull-async":459,"pull-awaitable":460,"pull-cat":462,"pull-stream":525,"traverse":837}],279:[function(require,module,exports){ @@ -4337,15 +4337,15 @@ var isCanonicalBase64=require("is-canonical-base64"),isDomain=require("is-valid- "use strict";const pull=require("pull-stream"),cat=require("pull-cat"),Notify=require("pull-notify"),msAddress=require("multiserver-address"),debug=require("debug")("ssb:conn-staging");class ConnStaging{constructor(){this._peers=new Map,this._closed=!1,this._notifyEvent=Notify(),this._notifyEntries=Notify()}_assertNotClosed(){if(this._closed)throw new Error("This ConnStaging instance is closed, create a new one.")}_assertValidAddress(e){if(!msAddress.check(e))throw new Error("The given address is not a valid multiserver-address")}_updateLiveEntries(){this._notifyEntries(Array.from(this._peers.entries()))}stage(e,s){this._assertNotClosed(),this._assertValidAddress(e);const t=Date.now();if(this._peers.has(e)){const i=this._peers.get(e);return this._peers.set(e,{...i,stagingUpdated:t,...s}),this._updateLiveEntries(),!1}return this._peers.set(e,{stagingBirth:t,stagingUpdated:t,...s}),debug("staged peer %s",e),this._notifyEvent({type:"staged",address:e}),this._updateLiveEntries(),!0}unstage(e){return this._assertNotClosed(),this._assertValidAddress(e),!!this._peers.has(e)&&(this._peers.delete(e),debug("unstaged peer %s",e),this._notifyEvent({type:"unstaged",address:e}),this._updateLiveEntries(),!0)}get(e){return this._assertNotClosed(),this._assertValidAddress(e),this._peers.get(e)}entries(){return this._assertNotClosed(),this._peers.entries()}liveEntries(){return this._assertNotClosed(),cat([pull.values([Array.from(this._peers.entries())]),this._notifyEntries.listen()])}listen(){return this._assertNotClosed(),this._notifyEvent.listen()}close(){this._closed=!0,this._notifyEvent.end(),this._peers.clear(),debug("closed the ConnStaging instance")}}module.exports=ConnStaging; },{"debug":134,"multiserver-address":355,"pull-cat":462,"pull-notify":516,"pull-stream":525}],720:[function(require,module,exports){ -"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var n,i=arguments.length,r=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,s,o);else for(var a=e.length-1;a>=0;a--)(n=e[a])&&(r=(i<3?n(r):i>3?n(t,s,r):n(t,s))||r);return i>3&&r&&Object.defineProperty(t,s,r),r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ConnScheduler=void 0;const z=require("ziii"),secret_stack_decorators_1=require("secret-stack-decorators"),ConnQuery=require("ssb-conn-query"),pull=require("pull-stream"),Pausable=require("pull-pause"),ip=require("ip"),onWakeup=require("on-wakeup"),onNetwork=require("on-change-network-strict"),hasNetwork=require("has-network2"),Ref=require("ssb-ref"),debug=require("debug")("ssb:conn:scheduler");let lastCheck=0,lastValue=null;function hasNetworkDebounced(){return lastCheck+1e3!isOffline(e);function isLegacy(e){return hasSuccessfulAttempts(e)&&!hasPinged(e)}function notRoom(e){return"room"!==e[1].type}function isDefunct(e){return!0===e[1].defunct}function take(e){return t=>t.slice(0,Math.max(e,0))}function detectType(e){const[t,s]=e;return"bt"===s.type?"bt":"lan"===s.type?"lan":"internet"===s.type?"internet":"dht"===s.type?"dht":"pub"===s.type?"pub":"room"===s.type?"room":"room-endpoint"===s.type?"room-endpoint":"local"===s.source?"lan":"pub"===s.source?"pub":"internet"===s.source?"internet":"dht"===s.source?"dht":"bt"===s.inferredType?"bt":"lan"===s.inferredType?"lan":"dht"===s.inferredType?"dht":"internet"===s.inferredType?"internet":t.startsWith("bt:")?"bt":t.startsWith("dht:")?"dht":"?"}const{passesExpBackoff:passesExpBackoff,passesGroupDebounce:passesGroupDebounce,hasNoAttempts:hasNoAttempts,hasOnlyFailedAttempts:hasOnlyFailedAttempts,hasPinged:hasPinged,hasSuccessfulAttempts:hasSuccessfulAttempts,sortByStateChange:sortByStateChange}=ConnQuery;function shufflePeers(e){return e.sort(()=>Math.random()-.5)}const minute=6e4,hour=36e5;let ConnScheduler=class{constructor(e,t){var s,o;this.weBlockThem=(([e,t])=>!(null==t||!t.key)&&-1===this.hops[t.key]),this.weFollowThem=(([e,t])=>{var s,o;if(!(null==t?void 0:t.key))return!1;const n=this.hops[t.key],i=null!==(o=null===(s=this.config.conn)||void 0===s?void 0:s.hops)&&void 0!==o?o:1;return n>0&&n<=i}),this.start=(()=>{var e,t;if(this.closed){this.closed=!1;for(let e of this.ssb.conn.dbPeers()){const[t,{source:s,type:o}]=e;"local"!==s&&"bt"!==s&&"lan"!==o&&"bt"!==o||this.ssb.conn.forget(t),isDefunct(e)&&this.removeDefunct(t)}this.loadHops(),this.populateWithSeeds(),this.setupPubDiscovery(),null===(e=this.pubDiscoveryPausable)||void 0===e||e.resume(),this.setupLanDiscovery(),this.setupBluetoothDiscovery(),this.intervalForUpdate=setInterval(()=>this.updateSoon(),2e3),(null===(t=this.intervalForUpdate)||void 0===t?void 0:t.unref)&&this.intervalForUpdate.unref(),onWakeup(()=>{this.closed||this.ssb.conn.hub().reset()}),onNetwork(()=>{this.closed||this.ssb.conn.hub().reset()}),pull(this.ssb.conn.hub().listen(),pull.filter(e=>"disconnected"===e.type),pull.drain(()=>this.updateSoon(200))),this.updateSoon()}}),this.stop=(()=>{var e,t,s;null===(e=this.pubDiscoveryPausable)||void 0===e||e.pause(),null===(s=null===(t=this.ssb.lan)||void 0===t?void 0:t.stop)||void 0===s||s.call(t),this.intervalForUpdate&&(clearInterval(this.intervalForUpdate),this.intervalForUpdate=void 0),this.ssb.conn.hub().reset(),this.closed=!0}),this.ssb=e,this.config=t,this.hasSsbDb2=!!(null===(s=this.ssb.db)||void 0===s?void 0:s.post)&&!!(null===(o=this.ssb.db)||void 0===o?void 0:o.query),this.closed=!0,this.lastMessageAt=0,this.hasScheduledAnUpdate=!1,this.isLoadingHops=!1,this.hops={},this.hasSsbDb2&&this.ssb.db.post(e=>{var t;e.value.author!==this.ssb.id&&(this.lastMessageAt=Date.now()),"contact"===(null===(t=e.value.content)||void 0===t?void 0:t.type)&&this.loadHops(()=>this.updateNow())})}loadHops(e){var t;(null===(t=this.ssb.friends)||void 0===t?void 0:t.hops)?(this.isLoadingHops=!0,this.ssb.friends.hops((t,s)=>{t?debug("unable to call ssb.friends.hops: %s",t):(this.hops=s,this.isLoadingHops=!1,e&&e())})):debug("Warning: ssb-friends is missing, scheduling will miss some info")}isCurrentlyDownloading(){return this.lastMessageAt&&this.lastMessageAt>Date.now()-500}maxWaitToConnect(e){switch(detectType(e)){case"lan":return 3e4;case"bt":return 6e4;case"dht":return 3e5;default:return 1e4}}updateTheseConnections(e,t){const s=this.ssb.conn.query(),o=s.peersInConnection().filter(e),n=s.peersConnectable("db").filter(e),{quota:i,backoffStep:r,backoffMax:a,groupMin:c}=t,l=o.length>2*i?o.length-i:0,u=Math.max(i-o.length,0);z(o).z(sortByStateChange).z(take(l)).forEach(([e])=>this.ssb.conn.disconnect(e)),z(n).z(e=>e.filter(e=>!this.weBlockThem(e))).z(e=>e.filter(canBeConnected)).z(e=>e.filter(([,e])=>!1!==e.autoconnect)).z(passesGroupDebounce(c)).z(e=>e.filter(passesExpBackoff(r,a))).z(e=>Math.random()<=.3?shufflePeers(e):sortByStateChange(e)).z(take(u)).forEach(([e,t])=>this.ssb.conn.connect(e,t))}updateStagingNow(){this.ssb.conn.query().peersConnectable("db").filter(e=>!this.weBlockThem(e)).filter(([,e])=>!1===e.autoconnect).forEach(([e,t])=>this.ssb.conn.stage(e,t)),this.ssb.conn.query().peersConnectable("staging").filter(this.weBlockThem).forEach(([e])=>this.ssb.conn.unstage(e)),this.ssb.conn.query().peersConnectable("staging").filter(([,e])=>"lan"===e.type).filter(([,e])=>e.stagingUpdated+1e4this.ssb.conn.unstage(e)),this.ssb.conn.query().peersConnectable("staging").filter(([,e])=>"bt"===e.type).filter(([,e])=>e.stagingUpdated+3e4this.ssb.conn.unstage(e))}updateHubNow(){var e;const t=this.ssb.conn;(null===(e=this.config.seed)||void 0===e||e)&&this.updateTheseConnections(e=>"seed"===e[1].source,{quota:3,backoffStep:2e3,backoffMax:6e5,groupMin:1e3}),0===t.query().peersInConnection().length&&this.updateTheseConnections(()=>!0,{quota:1,backoffStep:1e3,backoffMax:6e3,groupMin:0}),this.updateTheseConnections(e=>"room"===e[1].type,{quota:5,backoffStep:5e3,backoffMax:3e5,groupMin:5e3}),this.updateTheseConnections(e=>notRoom(e)&&hasPinged(e),{quota:2,backoffStep:1e4,backoffMax:6e5,groupMin:5e3}),this.updateTheseConnections(e=>notRoom(e)&&hasNoAttempts(e),{quota:2,backoffStep:3e4,backoffMax:18e5,groupMin:15e3}),this.updateTheseConnections(e=>notRoom(e)&&hasOnlyFailedAttempts(e),{quota:3,backoffStep:6e4,backoffMax:108e5,groupMin:3e5}),this.updateTheseConnections(e=>notRoom(e)&&isLegacy(e),{quota:1,backoffStep:24e4,backoffMax:108e5,groupMin:3e5}),z(t.query().peersConnectable("staging").filter(this.weFollowThem)).z(take(3-t.query().peersInConnection().filter(this.weFollowThem).length)).forEach(([e,s])=>t.connect(e,s)),t.query().peersInConnection().filter(this.weBlockThem).forEach(([e])=>t.disconnect(e)),t.query().peersInConnection().filter(e=>"connecting"===t.hub().getState(e[0])).filter(e=>e[1].stateChange+this.maxWaitToConnect(e)t.disconnect(e)),t.query().peersConnected().filter(e=>"bt"!==e[1].type&&"lan"!==e[1].type).filter(e=>e[1].stateChange+18e5t.disconnect(e))}updateNow(){this.closed||this.isCurrentlyDownloading()||this.isLoadingHops||(this.updateStagingNow(),this.updateHubNow())}updateSoon(e=1e3){if(this.closed)return;if(this.hasScheduledAnUpdate)return;const t=.5*e+e*Math.random();this.hasScheduledAnUpdate=!0;const s=setTimeout(()=>{this.updateNow(),this.hasScheduledAnUpdate=!1},t);s.unref&&s.unref()}removeDefunct(e){this.ssb.conn.db().update(e,{defunct:void 0,autoconnect:void 0})}populateWithSeeds(){var e;const t=null!==(e=this.config.seeds)&&void 0!==e?e:[];(Array.isArray(t)?t:[t]).filter(Boolean).forEach(e=>{const t=Ref.getKeyFromAddress(e);this.ssb.conn.remember(e,{key:t,source:"seed"})})}setupPubDiscovery(){var e;!1!==(null===(e=this.config.conn)||void 0===e?void 0:e.populatePubs)&&(this.hasSsbDb2?setTimeout(()=>{var e;if(this.closed)return;const{and:t,type:s,live:o,toPullStream:n}=this.ssb.db.operators;this.pubDiscoveryPausable=null!==(e=this.pubDiscoveryPausable)&&void 0!==e?e:Pausable(),pull(this.ssb.db.query(t(s("pub")),o({old:!0}),n()),pull.asyncMap((e,t)=>setTimeout(()=>t(null,e),250)),pull.filter(e=>{var t;return Ref.isAddress(null===(t=e.value.content)||void 0===t?void 0:t.address)}),this.pubDiscoveryPausable,pull.drain(e=>{try{const t=Ref.toMultiServerAddress(e.value.content.address),s=Ref.getKeyFromAddress(t);this.weBlockThem([t,{key:s}])?this.ssb.conn.forget(t):this.ssb.conn.db().has(t)||(this.ssb.conn.stage(t,{key:s,type:"pub"}),this.ssb.conn.remember(t,{key:s,type:"pub",autoconnect:!1}))}catch(e){debug("cannot process discovered pub because: %s",e)}})),pull(this.ssb.conn.staging().liveEntries(),pull.drain(e=>{var t,s;if(this.closed)return;e.filter(([,e])=>"pub"===e.type).length>=3?null===(t=this.pubDiscoveryPausable)||void 0===t||t.pause():null===(s=this.pubDiscoveryPausable)||void 0===s||s.resume()}))},1e3):debug("Warning: ssb-db2 is missing, scheduling will miss some info"))}setupBluetoothDiscovery(){var e;(null===(e=this.ssb.bluetooth)||void 0===e?void 0:e.nearbyScuttlebuttDevices)?pull(this.ssb.bluetooth.nearbyScuttlebuttDevices(1e3),pull.drain(({discovered:e})=>{if(!this.closed)for(const t of e){const e=`bt:${t.remoteAddress.split(":").join("")}`+"~"+`shs:${t.id.replace(/^\@/,"").replace(/\.ed25519$/,"")}`,s={type:"bt",note:t.displayName,key:t.id};this.weFollowThem([e,s])?this.ssb.conn.connect(e,s):this.ssb.conn.stage(e,s)}})):debug("Warning: ssb-bluetooth is missing, scheduling will miss some info")}setupLanDiscovery(){var e,t;(null===(e=this.ssb.lan)||void 0===e?void 0:e.start)&&(null===(t=this.ssb.lan)||void 0===t?void 0:t.discoveredPeers)?(pull(this.ssb.lan.discoveredPeers(),pull.drain(({address:e,verified:t})=>{const s=Ref.getKeyFromAddress(e);if(!s)return;const o={type:"lan",key:s,verified:t};this.weFollowThem([e,o])?this.ssb.conn.connect(e,o):this.ssb.conn.stage(e,o)})),this.ssb.lan.start()):debug("Warning: ssb-lan is missing, scheduling will miss some info")}};__decorate([secret_stack_decorators_1.muxrpc("sync")],ConnScheduler.prototype,"start",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],ConnScheduler.prototype,"stop",void 0),ConnScheduler=__decorate([secret_stack_decorators_1.plugin("1.0.0")],ConnScheduler),exports.ConnScheduler=ConnScheduler; +"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var n,i=arguments.length,r=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,s,o);else for(var a=e.length-1;a>=0;a--)(n=e[a])&&(r=(i<3?n(r):i>3?n(t,s,r):n(t,s))||r);return i>3&&r&&Object.defineProperty(t,s,r),r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ConnScheduler=void 0;const z=require("ziii"),secret_stack_decorators_1=require("secret-stack-decorators"),ConnQuery=require("ssb-conn-query"),pull=require("pull-stream"),Pausable=require("pull-pause"),ip=require("ip"),onWakeup=require("on-wakeup"),onNetwork=require("on-change-network-strict"),hasNetwork=require("has-network2"),Ref=require("ssb-ref"),debug=require("debug")("ssb:conn:scheduler");let lastCheck=0,lastValue=null;function hasNetworkDebounced(){return lastCheck+1e3!isOffline(e);function isLegacy(e){return hasSuccessfulAttempts(e)&&!hasPinged(e)}function notRoom(e){return"room"!==e[1].type}function isDefunct(e){return!0===e[1].defunct}function take(e){return t=>t.slice(0,Math.max(e,0))}function detectType(e){const[t,s]=e;return"bt"===s.type?"bt":"lan"===s.type?"lan":"internet"===s.type?"internet":"dht"===s.type?"dht":"pub"===s.type?"pub":"room"===s.type?"room":"room-endpoint"===s.type?"room-endpoint":"local"===s.source?"lan":"pub"===s.source?"pub":"internet"===s.source?"internet":"dht"===s.source?"dht":"bt"===s.inferredType?"bt":"lan"===s.inferredType?"lan":"dht"===s.inferredType?"dht":"internet"===s.inferredType?"internet":t.startsWith("bt:")?"bt":t.startsWith("dht:")?"dht":"?"}const{passesExpBackoff:passesExpBackoff,passesGroupDebounce:passesGroupDebounce,hasNoAttempts:hasNoAttempts,hasOnlyFailedAttempts:hasOnlyFailedAttempts,hasPinged:hasPinged,hasSuccessfulAttempts:hasSuccessfulAttempts,sortByStateChange:sortByStateChange}=ConnQuery;function shufflePeers(e){return e.sort(()=>Math.random()-.5)}const minute=6e4,hour=36e5;let ConnScheduler=class{constructor(e,t){var s,o;this.weBlockThem=(([e,t])=>!(null==t||!t.key)&&-1===this.hops[t.key]),this.weFollowThem=(([e,t])=>{var s,o;if(!(null==t?void 0:t.key))return!1;const n=this.hops[t.key],i=null!==(o=null===(s=this.config.conn)||void 0===s?void 0:s.hops)&&void 0!==o?o:1;return n>0&&n<=i}),this.start=(()=>{var e,t;if(this.closed){this.closed=!1;for(let e of this.ssb.conn.dbPeers()){const[t,{source:s,type:o}]=e;"local"!==s&&"bt"!==s&&"lan"!==o&&"bt"!==o||this.ssb.conn.forget(t),isDefunct(e)&&this.removeDefunct(t)}this.loadHops(),this.populateWithSeeds(),this.setupPubDiscovery(),null===(e=this.pubDiscoveryPausable)||void 0===e||e.resume(),this.setupLanDiscovery(),this.setupBluetoothDiscovery(),this.intervalForUpdate=setInterval(()=>this.updateSoon(),2e3),(null===(t=this.intervalForUpdate)||void 0===t?void 0:t.unref)&&this.intervalForUpdate.unref(),onWakeup(()=>{this.closed||this.ssb.conn.hub().reset()}),onNetwork(()=>{this.closed||this.ssb.conn.hub().reset()}),pull(this.ssb.conn.hub().listen(),pull.filter(e=>"disconnected"===e.type),pull.drain(()=>this.updateSoon(200))),this.updateSoon()}}),this.stop=(()=>{var e,t,s;null===(e=this.pubDiscoveryPausable)||void 0===e||e.pause(),null===(s=null===(t=this.ssb.lan)||void 0===t?void 0:t.stop)||void 0===s||s.call(t),this.intervalForUpdate&&(clearInterval(this.intervalForUpdate),this.intervalForUpdate=void 0),this.ssb.conn.hub().reset(),this.closed=!0}),this.ssb=e,this.config=t,this.hasSsbDb2=!!(null===(s=this.ssb.db)||void 0===s?void 0:s.post)&&!!(null===(o=this.ssb.db)||void 0===o?void 0:o.query),this.closed=!0,this.lastMessageAt=0,this.hasScheduledAnUpdate=!1,this.isLoadingHops=!1,this.hops={},this.hasSsbDb2&&this.ssb.db.post(e=>{var t;e.value.author!==this.ssb.id&&(this.lastMessageAt=Date.now()),"contact"===(null===(t=e.value.content)||void 0===t?void 0:t.type)&&this.loadHops(()=>this.updateNow())})}loadHops(e){var t;(null===(t=this.ssb.friends)||void 0===t?void 0:t.hops)?(this.isLoadingHops=!0,this.ssb.friends.hops((t,s)=>{t?debug("unable to call ssb.friends.hops: %s",t):(this.hops=s,this.isLoadingHops=!1,e&&e())})):debug("Warning: ssb-friends is missing, scheduling will miss some info")}isCurrentlyDownloading(){return this.lastMessageAt&&this.lastMessageAt>Date.now()-500}maxWaitToConnect(e){switch(detectType(e)){case"lan":return 3e4;case"bt":return 6e4;case"dht":return 3e5;default:return 1e4}}updateTheseConnections(e,t){const s=this.ssb.conn.query(),o=s.peersInConnection().filter(e),n=s.peersConnectable("db").filter(e),{quota:i,backoffStep:r,backoffMax:a,groupMin:c}=t,l=o.length>2*i?o.length-i:0,u=Math.max(i-o.length,0);z(o).z(sortByStateChange).z(take(l)).forEach(([e])=>this.ssb.conn.disconnect(e)),z(n).z(e=>e.filter(e=>!this.weBlockThem(e))).z(e=>e.filter(canBeConnected)).z(e=>e.filter(([,e])=>!1!==e.autoconnect)).z(passesGroupDebounce(c)).z(e=>e.filter(passesExpBackoff(r,a))).z(e=>Math.random()<=.3?shufflePeers(e):sortByStateChange(e)).z(take(u)).forEach(([e,t])=>this.ssb.conn.connect(e,t))}updateStagingNow(){this.ssb.conn.query().peersConnectable("db").filter(e=>!this.weBlockThem(e)).filter(([,e])=>!1===e.autoconnect).forEach(([e,t])=>this.ssb.conn.stage(e,t)),this.ssb.conn.query().peersConnectable("staging").filter(this.weBlockThem).forEach(([e])=>this.ssb.conn.unstage(e)),this.ssb.conn.query().peersConnectable("staging").filter(([,e])=>"lan"===e.type).filter(([,e])=>e.stagingUpdated+1e4this.ssb.conn.unstage(e)),this.ssb.conn.query().peersConnectable("staging").filter(([,e])=>"bt"===e.type).filter(([,e])=>e.stagingUpdated+3e4this.ssb.conn.unstage(e))}updateHubNow(){var e;const t=this.ssb.conn;(null===(e=this.config.seed)||void 0===e||e)&&this.updateTheseConnections(e=>"seed"===e[1].source,{quota:3,backoffStep:2e3,backoffMax:6e5,groupMin:1e3}),0===t.query().peersInConnection().length&&this.updateTheseConnections(()=>!0,{quota:1,backoffStep:1e3,backoffMax:6e3,groupMin:0}),this.updateTheseConnections(e=>"room"===e[1].type,{quota:5,backoffStep:5e3,backoffMax:3e5,groupMin:5e3}),this.updateTheseConnections(e=>notRoom(e)&&hasPinged(e),{quota:2,backoffStep:1e4,backoffMax:6e5,groupMin:5e3}),this.updateTheseConnections(e=>notRoom(e)&&hasNoAttempts(e),{quota:2,backoffStep:3e4,backoffMax:18e5,groupMin:15e3}),this.updateTheseConnections(e=>notRoom(e)&&hasOnlyFailedAttempts(e),{quota:3,backoffStep:6e4,backoffMax:108e5,groupMin:3e5}),this.updateTheseConnections(e=>notRoom(e)&&isLegacy(e),{quota:1,backoffStep:24e4,backoffMax:108e5,groupMin:3e5}),z(t.query().peersConnectable("staging").filter(this.weFollowThem)).z(take(3-t.query().peersInConnection().filter(this.weFollowThem).length)).forEach(([e,s])=>t.connect(e,s)),t.query().peersInConnection().filter(this.weBlockThem).forEach(([e])=>t.disconnect(e)),t.query().peersInConnection().filter(e=>"connecting"===t.hub().getState(e[0])).filter(e=>e[1].stateChange+this.maxWaitToConnect(e)t.disconnect(e)),t.query().peersConnected().filter(e=>"bt"!==e[1].type&&"lan"!==e[1].type).filter(e=>e[1].stateChange+18e5t.disconnect(e))}updateNow(){this.closed||this.isCurrentlyDownloading()||this.isLoadingHops||(this.updateStagingNow(),this.updateHubNow())}updateSoon(e=1e3){if(this.closed)return;if(this.hasScheduledAnUpdate)return;const t=.5*e+e*Math.random();this.hasScheduledAnUpdate=!0;const s=setTimeout(()=>{this.updateNow(),this.hasScheduledAnUpdate=!1},t);s.unref&&s.unref()}removeDefunct(e){this.ssb.conn.db().update(e,{defunct:void 0,autoconnect:void 0})}populateWithSeeds(){var e;const t=null!==(e=this.config.seeds)&&void 0!==e?e:[];(Array.isArray(t)?t:[t]).filter(Boolean).forEach(e=>{const t=Ref.getKeyFromAddress(e);this.ssb.conn.remember(e,{key:t,source:"seed"})})}setupPubDiscovery(){var e;!1!==(null===(e=this.config.conn)||void 0===e?void 0:e.populatePubs)&&(this.hasSsbDb2?setTimeout(()=>{var e;if(this.closed)return;const{where:t,type:s,live:o,toPullStream:n}=this.ssb.db.operators;this.pubDiscoveryPausable=null!==(e=this.pubDiscoveryPausable)&&void 0!==e?e:Pausable(),pull(this.ssb.db.query(t(s("pub")),o({old:!0}),n()),pull.filter(e=>{var t;return Ref.isAddress(null===(t=e.value.content)||void 0===t?void 0:t.address)}),pull.asyncMap((e,t)=>setTimeout(()=>t(null,e),250)),this.pubDiscoveryPausable,pull.drain(e=>{try{const t=Ref.toMultiServerAddress(e.value.content.address),s=Ref.getKeyFromAddress(t);this.weBlockThem([t,{key:s}])?this.ssb.conn.forget(t):this.ssb.conn.db().has(t)||(this.ssb.conn.stage(t,{key:s,type:"pub"}),this.ssb.conn.remember(t,{key:s,type:"pub",autoconnect:!1}))}catch(e){debug("cannot process discovered pub because: %s",e)}})),pull(this.ssb.conn.staging().liveEntries(),pull.drain(e=>{var t,s;if(this.closed)return;e.filter(([,e])=>"pub"===e.type).length>=3?null===(t=this.pubDiscoveryPausable)||void 0===t||t.pause():null===(s=this.pubDiscoveryPausable)||void 0===s||s.resume()}))},1e3):debug("Warning: ssb-db2 is missing, scheduling will miss some info"))}setupBluetoothDiscovery(){var e;(null===(e=this.ssb.bluetooth)||void 0===e?void 0:e.nearbyScuttlebuttDevices)?pull(this.ssb.bluetooth.nearbyScuttlebuttDevices(1e3),pull.drain(({discovered:e})=>{if(!this.closed)for(const t of e){const e=`bt:${t.remoteAddress.split(":").join("")}`+"~"+`shs:${t.id.replace(/^\@/,"").replace(/\.ed25519$/,"")}`,s={type:"bt",note:t.displayName,key:t.id};this.weFollowThem([e,s])?this.ssb.conn.connect(e,s):this.ssb.conn.stage(e,s)}})):debug("Warning: ssb-bluetooth is missing, scheduling will miss some info")}setupLanDiscovery(){var e,t;(null===(e=this.ssb.lan)||void 0===e?void 0:e.start)&&(null===(t=this.ssb.lan)||void 0===t?void 0:t.discoveredPeers)?(pull(this.ssb.lan.discoveredPeers(),pull.drain(({address:e,verified:t})=>{const s=Ref.getKeyFromAddress(e);if(!s)return;const o={type:"lan",key:s,verified:t};this.weFollowThem([e,o])?this.ssb.conn.connect(e,o):this.ssb.conn.stage(e,o)})),this.ssb.lan.start()):debug("Warning: ssb-lan is missing, scheduling will miss some info")}};__decorate([secret_stack_decorators_1.muxrpc("sync")],ConnScheduler.prototype,"start",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],ConnScheduler.prototype,"stop",void 0),ConnScheduler=__decorate([secret_stack_decorators_1.plugin("1.0.0")],ConnScheduler),exports.ConnScheduler=ConnScheduler; },{"debug":134,"has-network2":208,"ip":262,"on-change-network-strict":403,"on-wakeup":404,"pull-pause":520,"pull-stream":525,"secret-stack-decorators":638,"ssb-conn-query":715,"ssb-ref":777,"ziii":862}],721:[function(require,module,exports){ -"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var r,n=arguments.length,c=n<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)c=Reflect.decorate(e,t,s,o);else for(var i=e.length-1;i>=0;i--)(r=e[i])&&(c=(n<3?r(c):n>3?r(t,s,c):r(t,s))||c);return n>3&&c&&Object.defineProperty(t,s,c),c};Object.defineProperty(exports,"__esModule",{value:!0}),exports.CONN=void 0;const ConnDB=require("ssb-conn-db"),ConnHub=require("ssb-conn-hub"),ConnStaging=require("ssb-conn-staging"),ConnQuery=require("ssb-conn-query"),secret_stack_decorators_1=require("secret-stack-decorators"),interpool_glue_1=require("./interpool-glue"),ping=require("pull-ping");let CONN=class{constructor(e,t){this.remember=((e,t={})=>{this._db.set(e,t)}),this.forget=(e=>{this._db.delete(e)}),this.dbPeers=(()=>this._db.entries()),this.connect=((e,t,s)=>{if(s&&("function"==typeof t||!t))throw new Error("CONN.connect() received incorrect arguments");const o=s||t,r="function"==typeof o?o:null,n="object"==typeof t?t:{};this._hub.connect(e,n).then(e=>null==r?void 0:r(null,e),e=>null==r?void 0:r(e))}),this.disconnect=((e,t)=>{this._hub.disconnect(e).then(e=>null==t?void 0:t(null,e),e=>null==t?void 0:t(e))}),this.peers=(()=>this._hub.liveEntries()),this.stage=((e,t={type:"internet"})=>{if(this._hub.getState(e))return!1;if(t.key)for(const e of this._hub.entries())if(e[1].key===t.key)return!1;return this._staging.stage(e,t)}),this.unstage=(e=>this._staging.unstage(e)),this.stagedPeers=(()=>this._staging.liveEntries()),this.start=(()=>this.startScheduler()),this.stop=(()=>{this.stopScheduler()}),this.ping=(()=>{var e,t;let s=null!==(t=null===(e=this.config.timers)||void 0===e?void 0:e.ping)&&void 0!==t?t:3e5;return s=Math.max(1e4,Math.min(s,18e5)),ping({timeout:s})}),this.db=(()=>this._db),this.hub=(()=>this._hub),this.staging=(()=>this._staging),this.query=(()=>this._query),this.internalConnDB=(()=>(console.error("DEPRECATED conn.internalConnDB(), use conn.db() instead"),this._db)),this.internalConnHub=(()=>(console.error("DEPRECATED conn.internalConnHub(), use conn.hub() instead"),this._hub)),this.internalConnStaging=(()=>(console.error("DEPRECATED conn.internalConnStaging(), use conn.staging() instead"),this._staging)),this.ssb=e,this.config=t,this._db=new ConnDB({path:this.config.path,writeTimeout:1e3}),this._hub=new ConnHub(this.ssb),this._staging=new ConnStaging,this._query=new ConnQuery(this._db,this._hub,this._staging),this.initialize()}initialize(){this.setupCloseHook(),this.maybeAutoStartScheduler(),interpool_glue_1.interpoolGlue(this._db,this._hub,this._staging)}setupCloseHook(){const e=this;this.ssb.close.hook(function(t,s){return e.stopScheduler(),e._db.close(),e._hub.close(),e._staging.close(),t.apply(this,s)})}maybeAutoStartScheduler(){var e;!1===(null===(e=this.config.conn)||void 0===e?void 0:e.autostart)||this.startScheduler()}async startScheduler(){await this._db.loaded(),this.ssb.connScheduler?this.ssb.connScheduler.start():setTimeout(()=>{this.ssb.connScheduler?this.ssb.connScheduler.start():console.error("There is no ConnScheduler! The CONN plugin will remain in manual mode.")},100)}stopScheduler(){this.ssb.connScheduler&&this.ssb.connScheduler.stop()}};__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"remember",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"forget",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"dbPeers",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],CONN.prototype,"connect",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],CONN.prototype,"disconnect",void 0),__decorate([secret_stack_decorators_1.muxrpc("source")],CONN.prototype,"peers",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"stage",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"unstage",void 0),__decorate([secret_stack_decorators_1.muxrpc("source")],CONN.prototype,"stagedPeers",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"start",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"stop",void 0),__decorate([secret_stack_decorators_1.muxrpc("duplex",{anonymous:"allow"})],CONN.prototype,"ping",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"db",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"hub",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"staging",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"query",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"internalConnDB",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"internalConnHub",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"internalConnStaging",void 0),CONN=__decorate([secret_stack_decorators_1.plugin("1.0.0")],CONN),exports.CONN=CONN; +"use strict";var __decorate=this&&this.__decorate||function(e,t,s,r){var o,c=arguments.length,i=c<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,s):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,s,r);else for(var n=e.length-1;n>=0;n--)(o=e[n])&&(i=(c<3?o(i):c>3?o(t,s,i):o(t,s))||i);return c>3&&i&&Object.defineProperty(t,s,i),i};Object.defineProperty(exports,"__esModule",{value:!0}),exports.CONN=void 0;const ConnDB=require("ssb-conn-db"),ConnHub=require("ssb-conn-hub"),ConnStaging=require("ssb-conn-staging"),ConnQuery=require("ssb-conn-query"),secret_stack_decorators_1=require("secret-stack-decorators"),interpool_glue_1=require("./interpool-glue"),ping=require("pull-ping");let CONN=class{constructor(e,t){this.remember=((e,t={})=>{this._db.set(e,t)}),this.forget=(e=>{this._db.delete(e)}),this.dbPeers=(()=>this._db.entries()),this.connect=((e,t,s)=>{if(s&&("function"==typeof t||!t))throw new Error("CONN.connect() received incorrect arguments");const r=s||t,o="function"==typeof r?r:null,c="object"==typeof t?t:{};this._hub.connect(e,c).then(e=>null==o?void 0:o(null,e),e=>null==o?void 0:o(e))}),this.disconnect=((e,t)=>{this._hub.disconnect(e).then(e=>null==t?void 0:t(null,e),e=>null==t?void 0:t(e))}),this.peers=(()=>this._hub.liveEntries()),this.stage=((e,t={type:"internet"})=>{if(this._hub.getState(e))return!1;if(t.key)for(const e of this._hub.entries())if(e[1].key===t.key)return!1;return this._staging.stage(e,t)}),this.unstage=(e=>this._staging.unstage(e)),this.stagedPeers=(()=>this._staging.liveEntries()),this.start=(()=>this.startScheduler()),this.stop=(()=>{this.stopScheduler()}),this.ping=(()=>{var e,t;let s=null!==(t=null===(e=this.config.timers)||void 0===e?void 0:e.ping)&&void 0!==t?t:3e5;return s=Math.max(1e4,Math.min(s,18e5)),ping({timeout:s})}),this.db=(()=>this._db),this.hub=(()=>this._hub),this.staging=(()=>this._staging),this.query=(()=>this._query),this.ssb=e,this.config=t,this._db=new ConnDB({path:this.config.path,writeTimeout:1e3}),this._hub=new ConnHub(this.ssb),this._staging=new ConnStaging,this._query=new ConnQuery(this._db,this._hub,this._staging),this.initialize()}initialize(){this.setupCloseHook(),this.maybeAutoStartScheduler(),interpool_glue_1.interpoolGlue(this._db,this._hub,this._staging)}setupCloseHook(){const e=this;this.ssb.close.hook(function(t,s){return e.stopScheduler(),e._db.close(),e._hub.close(),e._staging.close(),t.apply(this,s)})}maybeAutoStartScheduler(){var e;!1===(null===(e=this.config.conn)||void 0===e?void 0:e.autostart)||this.startScheduler()}async startScheduler(){await this._db.loaded(),this.ssb.connScheduler?this.ssb.connScheduler.start():setTimeout(()=>{this.ssb.connScheduler?this.ssb.connScheduler.start():console.error("There is no ConnScheduler! The CONN plugin will remain in manual mode.")},100)}stopScheduler(){this.ssb.connScheduler&&this.ssb.connScheduler.stop()}};__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"remember",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"forget",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"dbPeers",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],CONN.prototype,"connect",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],CONN.prototype,"disconnect",void 0),__decorate([secret_stack_decorators_1.muxrpc("source")],CONN.prototype,"peers",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"stage",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"unstage",void 0),__decorate([secret_stack_decorators_1.muxrpc("source")],CONN.prototype,"stagedPeers",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"start",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"stop",void 0),__decorate([secret_stack_decorators_1.muxrpc("duplex",{anonymous:"allow"})],CONN.prototype,"ping",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"db",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"hub",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"staging",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],CONN.prototype,"query",void 0),CONN=__decorate([secret_stack_decorators_1.plugin("1.0.0")],CONN),exports.CONN=CONN; },{"./interpool-glue":724,"pull-ping":521,"secret-stack-decorators":638,"ssb-conn-db":711,"ssb-conn-hub":714,"ssb-conn-query":715,"ssb-conn-staging":719}],722:[function(require,module,exports){ -"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var r,n=arguments.length,i=n<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,s,o);else for(var c=e.length-1;c>=0;c--)(r=e[c])&&(i=(n<3?r(i):n>3?r(t,s,i):r(t,s))||i);return n>3&&i&&Object.defineProperty(t,s,i),i};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Gossip=void 0;const secret_stack_decorators_1=require("secret-stack-decorators"),pull=require("pull-stream"),Notify=require("pull-notify"),ref=require("ssb-ref");function isPeerObject(e){return e&&"object"==typeof e}function toBase64(e){return"string"==typeof e?e.substring(1,e.indexOf(".")):e.toString("base64")}function toAddressString(e){var t;if(isPeerObject(e)){if(ref.isAddress(e.address))return e.address;let s="net";return(null===(t=e.host)||void 0===t?void 0:t.endsWith(".onion"))&&(s="onion"),[s,e.host,e.port].join(":")+"~"+["shs",toBase64(e.key)].join(":")}return e}function parseAddress(e){const t=ref.parseAddress(e);return t||(ref.isAddress(e)?{key:ref.getKeyFromAddress(e)}:void 0)}function validateAddr(e){if(!e||"object"!=typeof e&&"string"!=typeof e)throw new Error("address should be an object or string");const t="string"==typeof e?e:toAddressString(e),s="object"==typeof e?e:parseAddress(t);if(!s.key)throw new Error("address must have ed25519 key");if(!ref.isFeed(s.key))throw new Error("key must be ed25519 public id");return[t,s]}function inferSource(e){return e.startsWith("net:")?"local":"manual"}let Gossip=class{constructor(e,t){var s;this.peers=(()=>{this.latestWarning+1e4({...t,address:e,state:this.conn.hub().getState(e)}));for(const[t,s]of this.conn.hub().entries())if(!this.conn.db().has(t)){const[,o]=validateAddr(t);e.push({...s,...o,address:t,source:inferSource(t)})}return e}),this.get=(e=>{if(this.deprecationWarning("get"),ref.isFeed(e)){for(let[t,s]of this.conn.db().entries())if(s.key===e)return{...s,address:t};return}const[t]=validateAddr(e),s=this.conn.db().get(t);return s?{address:t,state:this.conn.hub().getState(t),...s}:void 0}),this.connect=((e,t)=>{var s,o;let r;this.deprecationWarning("connect");try{const s=ref.isFeed(e)?this.idToAddr(e):e;[r]=validateAddr(s)}catch(e){return t(e)}this.add(r,"manual");const n=null!==(s=this.conn.staging().get(r))&&void 0!==s?s:{},i={...null!==(o=this.conn.db().get(r))&&void 0!==o?o:{},...n};this.conn.connect(r,i,t)}),this.disconnect=((e,t)=>{let s;this.deprecationWarning("disconnect");try{const o=ref.isFeed(e)?this.idToAddr(e):e;[s]=validateAddr(o)}catch(e){return t(e)}this.conn.disconnect(s,t)}),this.changes=(()=>(this.deprecationWarning("changes"),this.notify.listen())),this.add=((e,t)=>{var s;this.deprecationWarning("add");const[o,r]=validateAddr(e);if(r.key!==this.ssb.id){if("local"!==t)return this.conn.db().has(o)?this.conn.db().get(o):(this.conn.db().set(o,{host:r.host,port:r.port,key:r.key,address:o,source:t}),this.notify({type:"discover",peer:{...r,state:this.conn.hub().getState(o),source:null!=t?t:"manual"},source:null!=t?t:"manual"}),null!==(s=this.conn.db().get(o))&&void 0!==s?s:r);console.error('gossip.add(p, "local") from ssb-local is deprecated, this only supports ssb-lan')}}),this.remove=(e=>{this.deprecationWarning("remove");const[t]=validateAddr(e);this.conn.hub().disconnect(t),this.conn.staging().unstage(t);const s=this.conn.db().get(t);s&&(this.conn.db().delete(t),this.notify({type:"remove",peer:s}))}),this.ping=(()=>this.conn.ping()),this.reconnect=(()=>{this.deprecationWarning("reconnect"),this.conn.hub().reset()}),this.enable=(e=>{console.error('UNSUPPORTED gossip.enable("'+e+'") was ignored')}),this.disable=(e=>{console.error('UNSUPPORTED gossip.disable("'+e+'") was ignored')}),this.ssb=e,this.notify=Notify(),this.conn=this.ssb.conn,this.latestWarning=0,this.setupConnectionListeners(),!1===(null===(s=t.conn)||void 0===s?void 0:s.autostart)||this.conn.start(),this.deprecationWarned={}}setupConnectionListeners(){pull(this.conn.hub().listen(),pull.drain(e=>{"connecting-failed"===e.type&&this.onConnectingFailed(e),"connected"===e.type&&this.onConnected(e),"disconnected"===e.type&&this.onDisconnected(e)}))}onConnectingFailed(e){const t={state:e.type,address:e.address,key:e.key,...this.conn.db().get(e.address)};this.notify({type:"connect-failure",peer:t})}onConnected(e){const t={state:e.type,address:e.address,key:e.key,...this.conn.db().get(e.address)};this.conn.db().has(e.address)||(t.source=inferSource(e.address)),this.notify({type:"connect",peer:t})}onDisconnected(e){const t={state:e.type,address:e.address,key:e.key,...this.conn.db().get(e.address)};this.notify({type:"disconnect",peer:t})}idToAddr(e){const t=this.conn.db().getAddressForId(e);if(!t)throw new Error("no known address for peer:"+e);return t}deprecationWarning(e){this.deprecationWarned[e]||(console.error(`DEPRECATED gossip.${e}() was called. Use ssb-conn instead`),this.deprecationWarned[e]=!0)}};__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"peers",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"get",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],Gossip.prototype,"connect",void 0),__decorate([secret_stack_decorators_1.muxrpc("async")],Gossip.prototype,"disconnect",void 0),__decorate([secret_stack_decorators_1.muxrpc("source")],Gossip.prototype,"changes",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"add",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"remove",void 0),__decorate([secret_stack_decorators_1.muxrpc("duplex",{anonymous:"allow"})],Gossip.prototype,"ping",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"reconnect",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"enable",void 0),__decorate([secret_stack_decorators_1.muxrpc("sync")],Gossip.prototype,"disable",void 0),Gossip=__decorate([secret_stack_decorators_1.plugin("1.0.0")],Gossip),exports.Gossip=Gossip; +"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var r,c=arguments.length,i=c<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,s,o);else for(var p=e.length-1;p>=0;p--)(r=e[p])&&(i=(c<3?r(i):c>3?r(t,s,i):r(t,s))||i);return c>3&&i&&Object.defineProperty(t,s,i),i};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Gossip=void 0;const secret_stack_decorators_1=require("secret-stack-decorators");let Gossip=class{constructor(e){this.ping=(()=>this.ssb.conn.ping()),this.ssb=e}};__decorate([secret_stack_decorators_1.muxrpc("duplex",{anonymous:"allow"})],Gossip.prototype,"ping",void 0),Gossip=__decorate([secret_stack_decorators_1.plugin("1.0.0")],Gossip),exports.Gossip=Gossip; -},{"pull-notify":516,"pull-stream":525,"secret-stack-decorators":638,"ssb-ref":777}],723:[function(require,module,exports){ +},{"secret-stack-decorators":638}],723:[function(require,module,exports){ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const gossip_1=require("./gossip"),conn_1=require("./conn"),conn_scheduler_1=require("./conn-scheduler");module.exports=[conn_1.CONN,gossip_1.Gossip,conn_scheduler_1.ConnScheduler]; },{"./conn":721,"./conn-scheduler":720,"./gossip":722}],724:[function(require,module,exports){ @@ -4367,7 +4367,7 @@ module.exports=[require("./db"),require("./ebt"),require("./history-stream"),req const pull=require("pull-stream"),cat=require("pull-cat"),{live:live,toPullStream:toPullStream}=require("../operators");exports.manifest={createLogStream:"source"},exports.init=function(e){e.createLogStream=function(l){const t=!1!==(l=l||{}).keys,r=!1!==l.values,u=!1!==l.sync,a=!0===l.live,o=!0===l.old;function c(e){return!t&&r?e.value:t&&!r?e.key:e}const n=pull(e.db.query(toPullStream()),pull.map(c)),i=pull.values([{sync:!0}]),s=pull(e.db.query(live(),toPullStream()),pull.map(c));return a?o&&u?cat([n,i,s]):o&&!u?cat([n,s]):!o&&u?cat([i,s]):o||u?void 0:s:n}}; },{"../operators":740,"pull-cat":462,"pull-stream":525}],730:[function(require,module,exports){ -const push=require("push-stream"),ssbKeys=require("ssb-keys"),hash=require("ssb-keys/util").hash,validate=require("ssb-validate"),Obv=require("obz"),promisify=require("promisify-4loc"),jitdbOperators=require("jitdb/operators"),operators=require("./operators"),JITDb=require("jitdb"),Debug=require("debug"),{indexesPath:indexesPath}=require("./defaults"),{onceWhen:onceWhen}=require("./utils"),Log=require("./log"),Status=require("./status"),makeBaseIndex=require("./indexes/base"),KeysIndex=require("./indexes/keys"),PrivateIndex=require("./indexes/private"),{and:and,fromDB:fromDB,key:key,author:author,deferred:deferred,toCallback:toCallback,asOffsets:asOffsets}=operators;function getId(e){return"%"+hash(JSON.stringify(e,null,2))}exports.name="db",exports.version="1.9.1",exports.manifest={get:"async",add:"async",publish:"async",del:"async",deleteFeed:"async",addOOO:"async",addOOOStrictOrder:"async",getStatus:"sync"},exports.init=function(e,t){let s;(t=t||{}).db2=t.db2||{};const n={},r=t.path,a=PrivateIndex(r,t.keys),o=Log(r,t,a),i=JITDb(o,indexesPath(r)),d=Status(o,i),u=Debug("ssb:db2"),l=Obv(),c=null,f=Obv().set(!1);let p=validate.initial();function b(e){x("base",()=>{n.base.getAllLatest((t,s)=>{for(const e in s)p.feeds[e]={id:s[e].id,timestamp:s[e].timestamp,sequence:s[e].sequence,queue:[]};u("getAllLatest is done setting up initial validate state"),f.value||f.set(!0),e&&e()})})}function h(t){if(e.db2migrate&&e.db2migrate.doesOldLogExist())return new Error("ssb-db2: refusing to "+t+" because the old log still exists. This is to protect your feed from forking into an irrecoverable state.")}function m(e,t,n){s.query(and(key(e)),toCallback((s,r)=>s?n(s):r.length?n(null,t?r[0].value:r[0]):n(new Error("Key not found in database "+e))))}function g(e,t){m(e,!0,t)}function y(e,t,s){const n=getId(e);t?o.add(n,e,s):g(n,(t,r)=>{r?s(null,r):o.add(n,e,s)})}function O(e){const t=new e(o,r);if(n[t.name])throw"Index already exists";t.offset(e=>d.updateIndex(t.name,e)),n[t.name]=t}function v(){const e=Date.now(),t=Object.values(n),s=Math.min(...t.map(e=>e.offset.value));u(`lowest offset for all indexes is ${s}`),o.stream({gt:s}).pipe({paused:!1,write(e){t.forEach(t=>t.onRecord(e,!1))},end(){u(`updateIndexes() scan time: ${Date.now()-e}ms`);const s=t.map(e=>promisify(e.flush.bind(e))());Promise.all(s).then(()=>{u("updateIndexes() live streaming"),o.stream({gt:n.base.offset.value,live:!0}).pipe({paused:!1,write(e){t.forEach(t=>t.onRecord(e,!0))}})})}})}function x(e,t){t||(t=e,e="base"),setTimeout(()=>{q(()=>{o.onDrain(()=>{const s=n[e];if(!s)return t("Unknown index:"+e);if(d.updateLog(),s.offset.value===o.since.value)d.updateIndex(e,s.offset.value),t();else{const n=s.offset(()=>{s.offset.value===o.since.value&&(n(),d.updateIndex(e,s.offset.value),t())})}})})})}function q(e){if(!q.promise){const e=[a.stateLoaded];for(const t in n)e.push(n[t].stateLoaded);q.promise=Promise.all(e)}q.promise.then(e)}e.close.hook(function(e,t){!function(e){const t=[];for(const e in n){const s=n[e];t.push(promisify(s.close.bind(s))())}Promise.all(t).then(()=>promisify(o.close)()).then(e)}(()=>{e.apply(this,t)})}),O(makeBaseIndex(a)),O(KeysIndex),b(),d.obv(t=>{const s=Math.max(1,t.log),n=Object.values(t.indexes).concat(Object.values(t.jit)),r=Math.max(1,n.length),a=Math.min(n.map(e=>Math.max(0,e)).map(e=>e/s).reduce((e,t)=>e+t,0)/r,1);e.emit("ssb:db2:indexing:progress",a)});const I=setTimeout(()=>{q(v)});return I.unref&&I.unref(),s={get:g,getMsg:function(e,t){m(e,!1,t)},query:function(t,...s){const n=deferred((t,s)=>{e.db2migrate?e.db2migrate.synchronized(e=>{e&&o.onDrain(s)}):o.onDrain(s)});if(0===s.length){const e=fromDB(i);return e.meta.db=this,jitdbOperators.query(e,and(n),t)}if(t.meta)return jitdbOperators.query(t,and(n),...s);{const e=fromDB(i);return e.meta.db=this,jitdbOperators.query(e,and(n,t),...s)}},del:function(e,t){const r=h("del()");if(r)return t(r);s.query(and(key(e)),asOffsets(),toCallback((s,r)=>s?t(s):0===r.length?t(new Error("cannot delete "+e+" because it was not found")):(n.keys.delMsg(e),void o.del(r[0],t))))},deleteFeed:function(e,t){const s=h("deleteFeed()");if(s)return t(s);i.all(author(e),0,!1,!0,(s,r)=>{push(push.values(r),push.asyncMap((e,t)=>{o.del(e,t)}),push.collect(s=>{s?t(s):(delete p.feeds[e],n.base.removeFeedFromLatest(e,t))}))})},add:function(e,t){const s=h("add()");if(s)return t(s);onceWhen(f,e=>!0===e,()=>{try{if((p=validate.append(p,c,e)).error)return t(p.error);y(e,!0,t)}catch(e){return t(e)}})},publish:function(e,s){const n=h("publish()");if(n)return s(n);onceWhen(f,e=>!0===e,()=>{e.recps&&(e=ssbKeys.box(e,e.recps)),p.queue=[],y((p=validate.appendNew(p,null,t.keys,e,Date.now())).queue[0].value,!0,(e,t)=>{l.set(t),s(e,t)})})},addOOO:function(e,t){const s=h("addOOO()");if(s)return t(s);try{let s=validate.initial();if(validate.appendOOO(s,c,e),s.error)return t(s.error);y(e,!1,t)}catch(e){return t(e)}},addOOOStrictOrder:function(e,t,s){const n=h("addOOOStrictOrder()");if(n)return s(n);const r=e.author in t.feeds;try{if((t=r?validate.append(t,c,e):validate.appendOOO(t,c,e)).error)return s(t.error);y(e,!0,s)}catch(e){return s(e)}},getStatus:()=>d.obv,operators:operators,post:l,getLatest:n.base.getLatest.bind(n.base),getAllLatest:n.base.getAllLatest.bind(n.base),getLog:()=>o,registerIndex:O,setStateFeedsReady:function(e){f.set(e)},loadStateFeeds:b,getIndexes:()=>n,getIndex:e=>n[e],clearIndexes:function(){for(const e in n)n[e].remove(()=>{})},onDrain:x,getJITDB:()=>i}}; +const push=require("push-stream"),ssbKeys=require("ssb-keys"),hash=require("ssb-keys/util").hash,validate=require("ssb-validate"),Obv=require("obz"),promisify=require("promisify-4loc"),jitdbOperators=require("jitdb/operators"),operators=require("./operators"),JITDb=require("jitdb"),Debug=require("debug"),{indexesPath:indexesPath}=require("./defaults"),{onceWhen:onceWhen}=require("./utils"),Log=require("./log"),Status=require("./status"),makeBaseIndex=require("./indexes/base"),KeysIndex=require("./indexes/keys"),PrivateIndex=require("./indexes/private"),{where:where,fromDB:fromDB,key:key,author:author,deferred:deferred,toCallback:toCallback,asOffsets:asOffsets}=operators;function getId(e){return"%"+hash(JSON.stringify(e,null,2))}exports.name="db",exports.version="1.9.1",exports.manifest={get:"async",add:"async",publish:"async",del:"async",deleteFeed:"async",addOOO:"async",addOOOStrictOrder:"async",getStatus:"sync"},exports.init=function(e,t){let s;(t=t||{}).db2=t.db2||{};const n={},r=t.path,a=PrivateIndex(r,t.keys),o=Log(r,t,a),i=JITDb(o,indexesPath(r)),d=Status(o,i),u=Debug("ssb:db2"),l=Obv(),c=null,f=Obv().set(!1);let p=validate.initial();function b(e){x("base",()=>{n.base.getAllLatest((t,s)=>{for(const e in s)p.feeds[e]={id:s[e].id,timestamp:s[e].timestamp,sequence:s[e].sequence,queue:[]};u("getAllLatest is done setting up initial validate state"),f.value||f.set(!0),e&&e()})})}function h(t){if(e.db2migrate&&e.db2migrate.doesOldLogExist())return new Error("ssb-db2: refusing to "+t+" because the old log still exists. This is to protect your feed from forking into an irrecoverable state.")}function m(e,t,n){s.query(where(key(e)),toCallback((s,r)=>s?n(s):r.length?n(null,t?r[0].value:r[0]):n(new Error("Key not found in database "+e))))}function g(e,t){m(e,!0,t)}function y(e,t,s){const n=getId(e);t?o.add(n,e,s):g(n,(t,r)=>{r?s(null,r):o.add(n,e,s)})}function O(e){const t=new e(o,r);if(n[t.name])throw"Index already exists";t.offset(e=>d.updateIndex(t.name,e)),n[t.name]=t}function v(){const e=Date.now(),t=Object.values(n),s=Math.min(...t.map(e=>e.offset.value));u(`lowest offset for all indexes is ${s}`),o.stream({gt:s}).pipe({paused:!1,write(e){t.forEach(t=>t.onRecord(e,!1))},end(){u(`updateIndexes() scan time: ${Date.now()-e}ms`);const s=t.map(e=>promisify(e.flush.bind(e))());Promise.all(s).then(()=>{u("updateIndexes() live streaming"),o.stream({gt:n.base.offset.value,live:!0}).pipe({paused:!1,write(e){t.forEach(t=>t.onRecord(e,!0))}})})}})}function x(e,t){t||(t=e,e="base"),setTimeout(()=>{q(()=>{o.onDrain(()=>{const s=n[e];if(!s)return t("Unknown index:"+e);if(d.updateLog(),s.offset.value===o.since.value)d.updateIndex(e,s.offset.value),t();else{const n=s.offset(()=>{s.offset.value===o.since.value&&(n(),d.updateIndex(e,s.offset.value),t())})}})})})}function q(e){if(!q.promise){const e=[a.stateLoaded];for(const t in n)e.push(n[t].stateLoaded);q.promise=Promise.all(e)}q.promise.then(e)}e.close.hook(function(e,t){!function(e){const t=[];for(const e in n){const s=n[e];t.push(promisify(s.close.bind(s))())}Promise.all(t).then(()=>promisify(o.close)()).then(e)}(()=>{e.apply(this,t)})}),O(makeBaseIndex(a)),O(KeysIndex),b(),d.obv(t=>{const s=Math.max(1,t.log),n=Object.values(t.indexes).concat(Object.values(t.jit)),r=Math.max(1,n.length),a=Math.min(n.map(e=>Math.max(0,e)).map(e=>e/s).reduce((e,t)=>e+t,0)/r,1);e.emit("ssb:db2:indexing:progress",a)});const I=setTimeout(()=>{q(v)});return I.unref&&I.unref(),s={get:g,getMsg:function(e,t){m(e,!1,t)},query:function(t,...s){const n=deferred((t,s)=>{e.db2migrate?e.db2migrate.synchronized(e=>{e&&o.onDrain(s)}):o.onDrain(s)});if(t.meta)return jitdbOperators.query(t,where(n),...s);{const e=fromDB(i);return e.meta.db=this,jitdbOperators.query(e,where(n),t,...s)}},del:function(e,t){const r=h("del()");if(r)return t(r);s.query(where(key(e)),asOffsets(),toCallback((s,r)=>s?t(s):0===r.length?t(new Error("cannot delete "+e+" because it was not found")):(n.keys.delMsg(e),void o.del(r[0],t))))},deleteFeed:function(e,t){const s=h("deleteFeed()");if(s)return t(s);i.all(author(e),0,!1,!0,(s,r)=>{push(push.values(r),push.asyncMap((e,t)=>{o.del(e,t)}),push.collect(s=>{s?t(s):(delete p.feeds[e],n.base.removeFeedFromLatest(e,t))}))})},add:function(e,t){const s=h("add()");if(s)return t(s);onceWhen(f,e=>!0===e,()=>{try{if((p=validate.append(p,c,e)).error)return t(p.error);y(e,!0,t)}catch(e){return t(e)}})},publish:function(e,s){const n=h("publish()");if(n)return s(n);onceWhen(f,e=>!0===e,()=>{e.recps&&(e=ssbKeys.box(e,e.recps)),p.queue=[],y((p=validate.appendNew(p,null,t.keys,e,Date.now())).queue[0].value,!0,(e,t)=>{l.set(t),s(e,t)})})},addOOO:function(e,t){const s=h("addOOO()");if(s)return t(s);try{let s=validate.initial();if(validate.appendOOO(s,c,e),s.error)return t(s.error);y(e,!1,t)}catch(e){return t(e)}},addOOOStrictOrder:function(e,t,s){const n=h("addOOOStrictOrder()");if(n)return s(n);const r=e.author in t.feeds;try{if((t=r?validate.append(t,c,e):validate.appendOOO(t,c,e)).error)return s(t.error);y(e,!0,s)}catch(e){return s(e)}},getStatus:()=>d.obv,operators:operators,post:l,getLatest:n.base.getLatest.bind(n.base),getAllLatest:n.base.getAllLatest.bind(n.base),getLog:()=>o,registerIndex:O,setStateFeedsReady:function(e){f.set(e)},loadStateFeeds:b,getIndexes:()=>n,getIndex:e=>n[e],clearIndexes:function(){for(const e in n)n[e].remove(()=>{})},onDrain:x,getJITDB:()=>i}}; },{"./defaults":731,"./indexes/base":732,"./indexes/keys":734,"./indexes/private":736,"./log":737,"./operators":740,"./status":742,"./utils":743,"debug":134,"jitdb":277,"jitdb/operators":278,"obz":402,"promisify-4loc":442,"push-stream":575,"ssb-keys":769,"ssb-keys/util":775,"ssb-validate":786}],731:[function(require,module,exports){ const path=require("path");exports.BLOCK_SIZE=65536,exports.flumePath=(t=>path.join(t,"flume")),exports.oldLogPath=(t=>path.join(t,"flume","log.offset")),exports.newLogPath=(t=>path.join(t,"db2","log.bipf")),exports.indexesPath=(t=>path.join(t,"db2","indexes")),exports.tooHotOpts=(t=>t.db2?{ceiling:t.db2.maxCpu||1/0,wait:t.db2.maxCpuWait||90,maxPause:t.db2.maxCpuMaxPause||300}:{ceiling:1/0,wait:90,maxPause:300}); @@ -4408,7 +4408,7 @@ const{deferred:deferred}=require("jitdb/operators");module.exports=function(e){r },{"jitdb/operators":278}],740:[function(require,module,exports){ (function (Buffer){(function (){ -const jitdbOperators=require("jitdb/operators"),{seekType:seekType,seekAuthor:seekAuthor,seekChannel:seekChannel,seekRoot:seekRoot,seekFork:seekFork,seekPrivate:seekPrivate,seekMeta:seekMeta,seekVoteLink:seekVoteLink,seekMentions:seekMentions,pluckLink:pluckLink,seekContact:seekContact,seekBranch:seekBranch,seekAbout:seekAbout}=require("../seekers"),{and:and,equal:equal,includes:includes,deferred:deferred}=jitdbOperators;function key(e){return deferred((t,n)=>{t.db.onDrain("keys",()=>{t.db.getIndex("keys").getMsgByKey(e,n)})})}function type(e){return equal(seekType,e,{indexType:"value_content_type"})}function author(e,t){return t&&t.dedicated?equal(seekAuthor,e,{indexType:"value_author"}):equal(seekAuthor,e,{prefix:32,prefixOffset:1,indexType:"value_author"})}function channel(e){return equal(seekChannel,e,{indexType:"value_content_channel"})}function votesFor(e){return and(type("vote"),equal(seekVoteLink,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_vote_link"}))()}function contact(e){return and(type("contact"),equal(seekContact,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_contact"}))()}function mentions(e){return includes(seekMentions,e,{pluck:pluckLink,indexType:"value_content_mentions_link"})}function about(e){return and(type("about"),equal(seekAbout,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_about"}))}function hasRoot(e){return equal(seekRoot,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_root"})}function hasFork(e){return equal(seekFork,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_fork"})}function hasBranch(e){return equal(seekBranch,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_branch"})}function isRoot(){return equal(seekRoot,null,{indexType:"value_content_root"})}const bTrue=Buffer.alloc(1,1);function isPrivate(){return equal(seekPrivate,bTrue,{indexType:"meta_private"})}function isPublic(){return equal(seekMeta,void 0,{indexType:"meta"})}module.exports=Object.assign({},jitdbOperators,{type:type,author:author,channel:channel,key:key,votesFor:votesFor,contact:contact,mentions:mentions,about:about,hasRoot:hasRoot,hasFork:hasFork,hasBranch:hasBranch,isRoot:isRoot,isPrivate:isPrivate,isPublic:isPublic}); +const jitdbOperators=require("jitdb/operators"),{seekType:seekType,seekAuthor:seekAuthor,seekChannel:seekChannel,seekRoot:seekRoot,seekFork:seekFork,seekPrivate:seekPrivate,seekMeta:seekMeta,seekVoteLink:seekVoteLink,seekMentions:seekMentions,pluckLink:pluckLink,seekContact:seekContact,seekBranch:seekBranch,seekAbout:seekAbout}=require("../seekers"),{and:and,equal:equal,includes:includes,deferred:deferred}=jitdbOperators;function key(e){return deferred((t,n)=>{t.db.onDrain("keys",()=>{t.db.getIndex("keys").getMsgByKey(e,n)})})}function type(e){return equal(seekType,e,{indexType:"value_content_type"})}function author(e,t){return t&&t.dedicated?equal(seekAuthor,e,{indexType:"value_author"}):equal(seekAuthor,e,{prefix:32,prefixOffset:1,indexType:"value_author"})}function channel(e){return equal(seekChannel,e,{indexType:"value_content_channel"})}function votesFor(e){return and(type("vote"),equal(seekVoteLink,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_vote_link"}))}function contact(e){return and(type("contact"),equal(seekContact,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_contact"}))}function mentions(e){return includes(seekMentions,e,{pluck:pluckLink,indexType:"value_content_mentions_link"})}function about(e){return and(type("about"),equal(seekAbout,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_about"}))}function hasRoot(e){return equal(seekRoot,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_root"})}function hasFork(e){return equal(seekFork,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_fork"})}function hasBranch(e){return equal(seekBranch,e,{prefix:32,prefixOffset:1,useMap:!0,indexType:"value_content_branch"})}function isRoot(){return equal(seekRoot,null,{indexType:"value_content_root"})}const bTrue=Buffer.alloc(1,1);function isPrivate(){return equal(seekPrivate,bTrue,{indexType:"meta_private"})}function isPublic(){return equal(seekMeta,void 0,{indexType:"meta"})}module.exports=Object.assign({},jitdbOperators,{type:type,author:author,channel:channel,key:key,votesFor:votesFor,contact:contact,mentions:mentions,about:about,hasRoot:hasRoot,hasFork:hasFork,hasBranch:hasBranch,isRoot:isRoot,isPrivate:isPrivate,isPublic:isPublic}); }).call(this)}).call(this,require("buffer").Buffer) },{"../seekers":741,"buffer":106,"jitdb/operators":278}],741:[function(require,module,exports){