helia 7.0.2-4f472034 → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +2 -2
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +7 -0
- package/package.json +6 -6
- package/src/version.ts +1 -1
package/dist/index.min.js
CHANGED
|
@@ -110,7 +110,7 @@ ${e}`)}`:n!=null?`${n.split(`
|
|
|
110
110
|
${e}`)}`:t!=null?`${t}`:`${r.toString()}`}function _Y(r){return r instanceof AggregateError||r?.name==="AggregateError"&&Array.isArray(r.errors)}function _L(r,e=""){if(_Y(r)){let t=CL(r,e);return r.errors.length>0?(e=`${e} `,t+=`
|
|
111
111
|
${e}${r.errors.map(n=>`${_L(n,`${e}`)}`).join(`
|
|
112
112
|
${e}`)}`):t+=`
|
|
113
|
-
${e}[Error list was empty]`,t.trim()}return CL(r,e)}function PY(r){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=r,e.destroy=()=>!0,e.extend=()=>e,e.useColors=()=>!1,e}function PL(r){return{forComponent(e){return LL(e,r)}}}function LL(r,e){let t=PY(`${r}:trace`);return je.enabled(`${r}:trace`)&&je.names.map(n=>n.toString()).find(n=>n.includes(":trace"))!=null&&(t=je(`${r}:trace`,e)),Object.assign(je(r,e),{error:je(`${r}:error`,e),trace:t,newScope:n=>LL(`${r}:${n}`,e)})}function kL(r){if(r!=null&&(r=r.trim(),r.length!==0))return r}var Jo=class{has(e,t){return Promise.reject(new Error(".has is not implemented"))}put(e,t,n){return Promise.reject(new Error(".put is not implemented"))}async*putMany(e,t){for await(let{cid:n,bytes:o}of e)await this.put(n,o,t),yield n}get(e,t){throw new Error(".get is not implemented")}async*getMany(e,t){for await(let n of e)yield{cid:n,bytes:this.get(n,t)}}delete(e,t){return Promise.reject(new Error(".delete is not implemented"))}async*deleteMany(e,t){for await(let n of e)await this.delete(n,t),yield n}async*getAll(e){throw new Error(".getAll is not implemented")}};function LY(r){return typeof r?.then=="function"}var ih=class extends Jo{data;constructor(){super(),this.data=new Map}put(e,t,n){n?.signal?.throwIfAborted();let o;if(t instanceof Uint8Array)o=[t];else{let s=Lo(t);if(LY(s))return s.then(i=>this._put(e,i,n));o=s}return this._put(e,o,n)}_put(e,t,n){return n?.signal?.throwIfAborted(),this.data.set(pt.encode(e.multihash.bytes),t),e}*get(e,t){t?.signal?.throwIfAborted();let n=this.data.get(pt.encode(e.multihash.bytes));if(n==null)throw new tn;yield*n}has(e,t){return t?.signal?.throwIfAborted(),this.data.has(pt.encode(e.multihash.bytes))}async delete(e,t){t?.signal?.throwIfAborted(),this.data.delete(pt.encode(e.multihash.bytes))}*getAll(e){e?.signal?.throwIfAborted();for(let[t,n]of this.data.entries())yield{cid:K.createV1(po,Ee(pt.decode(t))),bytes:(async function*(){yield*n})()},e?.signal?.throwIfAborted()}};var RLe=rc("blockstore:core:tiered");var DL="SHARDING";var eDe=new Me(DL);var pDe=rc("datastore:core:tiered");var Hg=class{lock;child;pins;routing;started;constructor(e,t,n,o={}){this.child=e,this.pins=t,this.routing=n,this.lock=wd({singleProcess:o.holdGcLock}),this.started=!1}isStarted(){return this.started}async start(){await ht(this.child),this.started=!0}async stop(){await Ct(this.child),this.started=!1}unwrap(){return this.child}async put(e,t,n={}){n?.signal?.throwIfAborted();let o=await this.lock.readLock();try{return await this.child.put(e,t,n)}finally{o()}}async*putMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.putMany(e,t)}finally{n()}}async*get(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.get(e,t)}finally{n()}}async*getMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.getMany(e,t)}finally{n()}}async delete(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.writeLock();try{if(await this.pins.isPinned(e))throw new Nu("Block was pinned - please unpin and try again");await this.routing.cancelReprovide(e,t),await this.child.delete(e,t)}finally{n()}}async*deleteMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.writeLock();try{let o=this;yield*this.child.deleteMany((async function*(){for await(let s of e){if(await o.pins.isPinned(s))throw new Nu("Block was pinned - please unpin and try again");await o.routing.cancelReprovide(s,t),yield s}})(),t)}finally{n()}}async has(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{return await this.child.has(e,t)}finally{n()}}async*getAll(e={}){e?.signal?.throwIfAborted();let t=await this.lock.readLock();try{yield*this.child.getAll(e)}finally{t()}}createSession(e,t){return t?.signal?.throwIfAborted(),this.child.createSession(e,t)}};var sb=new Me("/version"),NL=1;async function OL(r){if(!await r.has(sb)){await r.put(sb,O(`${NL}`));return}let e=await r.get(sb),t=M(e);if(parseInt(t,10)!==NL)throw new L1("Invalid datastore version, a datastore migration may be required")}var db={};V(db,{code:()=>ub,createLink:()=>zL,createNode:()=>qL,decode:()=>zY,encode:()=>qY,name:()=>HY,prepare:()=>cb,validate:()=>lb});var RY=new TextDecoder;function ib(r,e){let t=0;for(let n=0;;n+=7){if(n>=64)throw new Error("protobuf: varint overflow");if(e>=r.length)throw new Error("protobuf: unexpected end of data");let o=r[e++];if(t+=n<28?(o&127)<<n:(o&127)*2**n,o<128)break}return[t,e]}function qg(r,e){let t;[t,e]=ib(r,e);let n=e+t;if(t<0||n<0)throw new Error("protobuf: invalid length");if(n>r.length)throw new Error("protobuf: unexpected end of data");return[r.subarray(e,n),n]}function BL(r,e){let t;return[t,e]=ib(r,e),[t&7,t>>3,e]}function NY(r){let e={},t=r.length,n=0;for(;n<t;){let o,s;if([o,s,n]=BL(r,n),s===1){if(e.Hash)throw new Error("protobuf: (PBLink) duplicate Hash section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Hash`);if(e.Name!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Name before Hash");if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Hash");[e.Hash,n]=qg(r,n)}else if(s===2){if(e.Name!==void 0)throw new Error("protobuf: (PBLink) duplicate Name section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Name`);if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Name");let i;[i,n]=qg(r,n),e.Name=RY.decode(i)}else if(s===3){if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) duplicate Tsize section");if(o!==0)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Tsize`);[e.Tsize,n]=ib(r,n)}else throw new Error(`protobuf: (PBLink) invalid fieldNumber, expected 1, 2 or 3, got ${s}`)}if(n>t)throw new Error("protobuf: (PBLink) unexpected end of data");return e}function ML(r){let e=r.length,t=0,n,o=!1,s;for(;t<e;){let a,c;if([a,c,t]=BL(r,t),a!==2)throw new Error(`protobuf: (PBNode) invalid wireType, expected 2, got ${a}`);if(c===1){if(s)throw new Error("protobuf: (PBNode) duplicate Data section");[s,t]=qg(r,t),n&&(o=!0)}else if(c===2){if(o)throw new Error("protobuf: (PBNode) duplicate Links section");n||(n=[]);let l;[l,t]=qg(r,t),n.push(NY(l))}else throw new Error(`protobuf: (PBNode) invalid fieldNumber, expected 1 or 2, got ${c}`)}if(t>e)throw new Error("protobuf: (PBNode) unexpected end of data");let i={};return s&&(i.Data=s),i.Links=n||[],i}var FL=new TextEncoder,UL=2**32,OY=2**31;function BY(r,e){let t=e.length;if(typeof r.Tsize=="number"){if(r.Tsize<0)throw new Error("Tsize cannot be negative");if(!Number.isSafeInteger(r.Tsize))throw new Error("Tsize too large for encoding");t=ah(e,t,r.Tsize)-1,e[t]=24}if(typeof r.Name=="string"){let n=FL.encode(r.Name);t-=n.length,e.set(n,t),t=ah(e,t,n.length)-1,e[t]=18}return r.Hash&&(t-=r.Hash.length,e.set(r.Hash,t),t=ah(e,t,r.Hash.length)-1,e[t]=10),e.length-t}function $L(r){let e=UY(r),t=new Uint8Array(e),n=e;if(r.Data&&(n-=r.Data.length,t.set(r.Data,n),n=ah(t,n,r.Data.length)-1,t[n]=10),r.Links)for(let o=r.Links.length-1;o>=0;o--){let s=BY(r.Links[o],t.subarray(0,n));n-=s,n=ah(t,n,s)-1,t[n]=18}return t}function MY(r){let e=0;if(r.Hash){let t=r.Hash.length;e+=1+t+Ol(t)}if(typeof r.Name=="string"){let t=FL.encode(r.Name).length;e+=1+t+Ol(t)}return typeof r.Tsize=="number"&&(e+=1+Ol(r.Tsize)),e}function UY(r){let e=0;if(r.Data){let t=r.Data.length;e+=1+t+Ol(t)}if(r.Links)for(let t of r.Links){let n=MY(t);e+=1+n+Ol(n)}return e}function ah(r,e,t){e-=Ol(t);let n=e;for(;t>=OY;)r[e++]=t&127|128,t/=128;for(;t>=128;)r[e++]=t&127|128,t>>>=7;return r[e]=t,n}function Ol(r){return r%2===0&&r++,Math.floor((FY(r)+6)/7)}function FY(r){let e=0;return r>=UL&&(r=Math.floor(r/UL),e=32),r>=65536&&(r>>>=16,e+=16),r>=256&&(r>>>=8,e+=8),e+$Y[r]}var $Y=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8];var KY=["Data","Links"],VY=["Hash","Name","Tsize"],ab=new TextEncoder;function VL(r,e){if(r===e)return 0;let t=r.Name?ab.encode(r.Name):[],n=e.Name?ab.encode(e.Name):[],o=t.length,s=n.length;for(let i=0,a=Math.min(o,s);i<a;++i)if(t[i]!==n[i]){o=t[i],s=n[i];break}return o<s?-1:s<o?1:0}function KL(r,e){return!Object.keys(r).some(t=>!e.includes(t))}function HL(r){if(typeof r.asCID=="object"){let t=K.asCID(r);if(!t)throw new TypeError("Invalid DAG-PB form");return{Hash:t}}if(typeof r!="object"||Array.isArray(r))throw new TypeError("Invalid DAG-PB form");let e={};if(r.Hash){let t=K.asCID(r.Hash);try{t||(typeof r.Hash=="string"?t=K.parse(r.Hash):r.Hash instanceof Uint8Array&&(t=K.decode(r.Hash)))}catch(n){throw new TypeError(`Invalid DAG-PB form: ${n.message}`)}t&&(e.Hash=t)}if(!e.Hash)throw new TypeError("Invalid DAG-PB form");return typeof r.Name=="string"&&(e.Name=r.Name),typeof r.Tsize=="number"&&(e.Tsize=r.Tsize),e}function cb(r){if((r instanceof Uint8Array||typeof r=="string")&&(r={Data:r}),typeof r!="object"||Array.isArray(r))throw new TypeError("Invalid DAG-PB form");let e={};if(r.Data!==void 0)if(typeof r.Data=="string")e.Data=ab.encode(r.Data);else if(r.Data instanceof Uint8Array)e.Data=r.Data;else throw new TypeError("Invalid DAG-PB form");if(r.Links!==void 0)if(Array.isArray(r.Links))e.Links=r.Links.map(HL),e.Links.sort(VL);else throw new TypeError("Invalid DAG-PB form");else e.Links=[];return e}function lb(r){if(!r||typeof r!="object"||Array.isArray(r)||r instanceof Uint8Array||r["/"]&&r["/"]===r.bytes)throw new TypeError("Invalid DAG-PB form");if(!KL(r,KY))throw new TypeError("Invalid DAG-PB form (extraneous properties)");if(r.Data!==void 0&&!(r.Data instanceof Uint8Array))throw new TypeError("Invalid DAG-PB form (Data must be bytes)");if(!Array.isArray(r.Links))throw new TypeError("Invalid DAG-PB form (Links must be a list)");for(let e=0;e<r.Links.length;e++){let t=r.Links[e];if(!t||typeof t!="object"||Array.isArray(t)||t instanceof Uint8Array||t["/"]&&t["/"]===t.bytes)throw new TypeError("Invalid DAG-PB form (bad link)");if(!KL(t,VY))throw new TypeError("Invalid DAG-PB form (extraneous properties on link)");if(t.Hash===void 0)throw new TypeError("Invalid DAG-PB form (link must have a Hash)");if(t.Hash==null||!t.Hash["/"]||t.Hash["/"]!==t.Hash.bytes)throw new TypeError("Invalid DAG-PB form (link Hash must be a CID)");if(t.Name!==void 0&&typeof t.Name!="string")throw new TypeError("Invalid DAG-PB form (link Name must be a string)");if(t.Tsize!==void 0){if(typeof t.Tsize!="number"||t.Tsize%1!==0)throw new TypeError("Invalid DAG-PB form (link Tsize must be an integer)");if(t.Tsize<0)throw new TypeError("Invalid DAG-PB form (link Tsize cannot be negative)")}if(e>0&&VL(t,r.Links[e-1])===-1)throw new TypeError("Invalid DAG-PB form (links must be sorted by Name bytes)")}}function qL(r,e=[]){return cb({Data:r,Links:e})}function zL(r,e,t){return HL({Hash:t,Name:r,Tsize:e})}function WL(r){return r instanceof ArrayBuffer?new Uint8Array(r,0,r.byteLength):r}var HY="dag-pb",ub=112;function qY(r){lb(r);let e={};return r.Links&&(e.Links=r.Links.map(t=>{let n={};return t.Hash&&(n.Hash=t.Hash.bytes),t.Name!==void 0&&(n.Name=t.Name),t.Tsize!==void 0&&(n.Tsize=t.Tsize),n})),r.Data&&(e.Data=r.Data),$L(e)}function zY(r){let e=WL(r),t=ML(e),n={};return t.Data&&(n.Data=t.Data),t.Links&&(n.Links=t.Links.map(o=>{let s={};try{s.Hash=K.decode(o.Hash)}catch{}if(!s.Hash)throw new Error("Invalid Hash field found in link, expected CID");return o.Name!==void 0&&(s.Name=o.Name),o.Tsize!==void 0&&(s.Tsize=o.Tsize),s})),n}function GL(r=[],e){let t={[ub]:db,[po]:fi};return r.forEach(n=>{t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.code]=o}if(o!=null)return o;throw new lu(`Could not load codec for ${n}`)}}function jL(r=[],e){let t={};return r=[$3(),V3(),iu(),...r],r.forEach(n=>{t[n.type]=n,t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.type]=o,t[o.code]=o}if(o!=null)return o;throw new uu(`Could not load crypto for ${o}`)}}function YL(r=[],e){let t={[He.code]:He,[Ze.code]:Ze};return r.forEach(n=>{t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.code]=o}if(o!=null)return o;throw new cu(`No hasher configured for multihash code 0x${n.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`)}}var zg=0,Bl=class extends Error{static name="IdentityHashDigestTooLongError";name="IdentityHashDigestTooLongError"},Wg=class extends Jo{child;maxDigestLength;constructor(e,t){super(),this.child=e,this.maxDigestLength=t?.maxDigestLength}put(e,t,n){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);return n?.signal?.throwIfAborted(),e}return this.child==null?(n?.signal?.throwIfAborted(),e):this.child.put(e,t,n)}async*get(e,t){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);t?.signal?.throwIfAborted(),yield e.multihash.digest;return}if(this.child==null)throw t?.signal?.throwIfAborted(),new tn;yield*this.child.get(e,t)}has(e,t){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);return t?.signal?.throwIfAborted(),!0}return this.child==null?(t?.signal?.throwIfAborted(),!1):this.child.has(e,t)}delete(e,t){if(e.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);t?.signal?.throwIfAborted();return}if(this.child!=null)return this.child.delete(e,t)}async*getAll(e){this.child!=null&&(yield*this.child.getAll(e)),e?.signal?.throwIfAborted()}};function WY(r){return r[Symbol.asyncIterator]!=null}function XL(r){return r?.then!=null}function GY(r,e){let t=0;if(WY(r))return(async function*(){for await(let c of r){let l=e(c,t++);XL(l)&&await l,yield c}})();let n=ic(r),{value:o,done:s}=n.next();if(s===!0)return(function*(){})();let i=e(o,t++);if(typeof i?.then=="function")return(async function*(){await i,yield o;for(let c of n){let l=e(c,t++);XL(l)&&await l,yield c}})();let a=e;return(function*(){yield o;for(let c of n)a(c,t++),yield c})()}var fb=GY;var jY=128,Ml=class{child;getHasher;log;logger;blockBrokers;constructor(e,t={}){this.log=e.logger.forComponent("helia:networked-storage"),this.logger=e.logger,this.blockBrokers=e.blockBrokers,this.child=new Wg(e.blockstore,{maxDigestLength:t.maxIdentityHashDigestLength??jY}),this.getHasher=e.getHasher}async put(e,t,n={}){return await this.child.has(e,n)?(n.onProgress?.(new R("blocks:put:duplicate",e)),e):(n.onProgress?.(new R("blocks:put:providers:notify",e)),await Promise.all(this.blockBrokers.map(async o=>o.announce?.(e,n))),n.onProgress?.(new R("blocks:put:blockstore:put",e)),this.child.put(e,t,n))}async*putMany(e,t={}){let n=Br(e,async({cid:s})=>{let i=await this.child.has(s,t);return i&&t.onProgress?.(new R("blocks:put-many:duplicate",s)),!i}),o=fb(n,async({cid:s})=>{t.onProgress?.(new R("blocks:put-many:providers:notify",s)),await Promise.all(this.blockBrokers.map(async i=>i.announce?.(s,t)))});t.onProgress?.(new R("blocks:put-many:blockstore:put-many")),yield*this.child.putMany(o,t)}async*get(e,t={}){let n=await this.child.has(e,t),o=t.offline===!0;if(!n){if(o)throw new Ou("The block was present in the blockstore and the node is running offline so cannot fetch it");let s=await this.getHasher(e.multihash.code);t?.signal?.throwIfAborted(),t.onProgress?.(new R("blocks:get:providers:get",e));let i=await QL(e,this.blockBrokers,s,{...t,log:this.log});t.onProgress?.(new R("blocks:get:blockstore:put",e)),await this.child.put(e,i,t),t.onProgress?.(new R("blocks:get:providers:notify",e)),await Promise.all(this.blockBrokers.map(async a=>a.announce?.(e,t))),yield i;return}t.onProgress?.(new R("blocks:get:blockstore:get",e)),yield*this.child.get(e,t)}async*getMany(e,t={}){t.onProgress?.(new R("blocks:get-many:blockstore:get-many")),yield*this.child.getMany(fb(e,async n=>{let o=await this.child.has(n,t),s=t.offline===!0;if(!o){if(s)throw new Ou("The block was present in the blockstore and the node is running offline so cannot fetch it");let i=await this.getHasher(n.multihash.code);t?.signal?.throwIfAborted(),t.onProgress?.(new R("blocks:get-many:providers:get",n));let a=await QL(n,this.blockBrokers,i,{...t,log:this.log});t.onProgress?.(new R("blocks:get-many:blockstore:put",n)),await this.child.put(n,a,t),t.onProgress?.(new R("blocks:get-many:providers:notify",n)),await Promise.all(this.blockBrokers.map(async c=>c.announce?.(n,t)))}}))}async delete(e,t={}){t.onProgress?.(new R("blocks:delete:blockstore:delete",e)),await this.child.delete(e,t)}async*deleteMany(e,t={}){t.onProgress?.(new R("blocks:delete-many:blockstore:delete-many")),yield*this.child.deleteMany((async function*(){for await(let n of e)yield n})(),t)}async has(e,t={}){return this.child.has(e,t)}async*getAll(e={}){e.onProgress?.(new R("blocks:get-all:blockstore:get-many")),yield*this.child.getAll(e)}};async function QL(r,e,t,n){let o=XY(r,t),s=new AbortController,i=de([s.signal,n.signal]);s.signal;let a=[];for(let c of e)YY(c)&&a.push(c);if(a.length===0)throw new vo(`No block brokers capable of retrieving blocks are configured, the CID ${r} cannot be fetched from the network`);try{return await Promise.any(a.map(async c=>{try{let l=!1,u=await c.retrieve(r,{...n,signal:i,validateFn:async d=>{await o(d),n.signal?.throwIfAborted(),l=!0}});return l||(await o(u),n.signal?.throwIfAborted()),u}catch(l){throw n.log.error("could not retrieve verified block for %c from %s - %e",r,c.name,l),l}}))}catch(c){throw new R1(c.errors,`Failed to load block for ${r}`)}finally{s.abort(),i.clear()}}function YY(r){return typeof r.retrieve=="function"}var XY=(r,e)=>{if(e==null)throw new A(`No hasher configured for multihash code 0x${r.multihash.code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`);return async t=>{let n,o=e.digest(t,{truncate:r.multihash.digest.byteLength});if(gs(o)?n=await o:n=o,!te(n.digest,r.multihash.digest))throw new va("Hash of downloaded block did not match multihash from passed CID")}};var Gg=class extends Ml{closeController;constructor(e,t){super(e),this.closeController=new AbortController,this.closeController.signal,this.log=e.logger.forComponent(`helia:session-storage:${t.root}`)}close(){this.closeController.abort()}async addPeer(e,t){await Promise.all(this.blockBrokers.map(n=>n.addPeer(e,t)))}async put(e,t,n={}){let o=de([this.closeController.signal,n.signal]);try{return await super.put(e,t,{...n,signal:o})}finally{o.clear()}}async*putMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.putMany(e,{...t,signal:n})}finally{n.clear()}}async*get(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.get(e,{...t,signal:n})}finally{n.clear()}}async*getMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.getMany(e,{...t,signal:n})}finally{n.clear()}}async delete(e,t={}){let n=de([this.closeController.signal,t.signal]);try{await super.delete(e,{...t,signal:n})}finally{n.clear()}}async*deleteMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.deleteMany(e,{...t,signal:n})}finally{n.clear()}}async has(e,t={}){let n=de([this.closeController.signal,t.signal]);try{return await super.has(e,{...t,signal:n})}finally{n.clear()}}async*getAll(e={}){let t=de([this.closeController.signal,e.signal]);try{yield*super.getAll({...e,signal:t})}finally{t.clear()}}};var jg=class extends Ml{started;constructor(e,t={}){super(e,t),this.started=!1}isStarted(){return this.started}async start(){await ht(this.child,...this.blockBrokers),this.started=!0}async stop(){await Ct(this.child,...this.blockBrokers),this.started=!1}unwrap(){return this.child}createSession(e,t){if(this.blockBrokers.length===0)throw new vo("No block brokers configured");let n=this.blockBrokers.map(o=>o.createSession?.(t)).filter(o=>o!=null);if(n.length===0)throw new vo(`No configured block brokers support sessions - tried ${this.blockBrokers.map(o=>o.name).join(", ")}`);return new Gg({blockstore:this.child,blockBrokers:n,getHasher:this.getHasher,logger:this.logger},{root:e})}};var ZL="/pin/",JL="/pinned-block/",hb=or;function Yg(r){return r.version===0&&(r=r.toV1()),new Me(`${ZL}${r.toString(hb)}`)}var Xg=class{datastore;blockstore;getCodec;constructor(e,t,n){this.datastore=e,this.blockstore=t,this.getCodec=n}async*add(e,t={}){let n=Yg(e);if(await this.datastore.has(n))throw new P1("Already pinned");let o=Math.round(t.depth??1/0);if(o<0)throw new A("Depth must be greater than or equal to 0");let s=(t.walker??a6())({blockstore:this.blockstore,getCodec:this.getCodec});for await(let a of s.walk(e,{...t,depth:o})){let c=a.block.cid;await this.#e(c,l=>l.pinnedBy.find(u=>te(u,e.bytes))!=null?!1:(l.pinCount++,l.pinnedBy.push(e.bytes),!0),t),yield c}let i={depth:o,metadata:t.metadata??{}};await this.datastore.put(n,ki(i),t)}async#e(e,t,n){let o=new Me(`${JL}${hb.encode(e.multihash.bytes)}`),s={pinCount:0,pinnedBy:[]};try{s=xn(await this.datastore.get(o,n))}catch(a){if(a.name!=="NotFoundError")throw a}if(t(s)){if(s.pinCount===0&&await this.datastore.has(o)){await this.datastore.delete(o);return}await this.datastore.put(o,ki(s),n),n.onProgress?.(new R("helia:pin:add",e))}}async*rm(e,t={}){let n=Yg(e),o=await this.datastore.get(n,t),s=xn(o);await this.datastore.delete(n,t);let i=(t.walker??a6())({blockstore:this.blockstore,getCodec:this.getCodec});for await(let a of i.walk(e,{...t,depth:s.depth})){let c=a.block.cid;await this.#e(c,l=>(l.pinCount--,l.pinnedBy=l.pinnedBy.filter(u=>te(u,e.bytes)),!0),{...t,depth:s.depth}),yield c}}async*ls(e={}){for await(let{key:t,value:n}of this.datastore.query({prefix:ZL+(e.cid!=null?`${e.cid.toString(or)}`:"")},e)){let o=K.parse(t.toString().substring(5),or),s=xn(n);yield{cid:o,...s}}}async isPinned(e,t={}){let n=new Me(`${JL}${hb.encode(e.multihash.bytes)}`);return this.datastore.has(n,t)}async get(e,t){let n=Yg(e),o=await this.datastore.get(n,t);return xn(o)}async setMetadata(e,t,n){let o=Yg(e),s=await this.datastore.get(o,n),i=xn(s);i.metadata=t??{},await this.datastore.put(o,ki(i),n)}};var QY=5,Qg=class{name;log;routers;providerLookupConcurrency;constructor(e,t){this.name="helia",this.log=e.logger.forComponent("helia:routing"),this.routers=t.routers??[],this.providerLookupConcurrency=t.providerLookupConcurrency??QY,this.findProviders=e.metrics?.traceFunction("helia.routing.findProviders",this.findProviders.bind(this),{optionsIndex:1})??this.findProviders,this.provide=e.metrics?.traceFunction("helia.routing.provide",this.provide.bind(this),{optionsIndex:1})??this.provide,this.cancelReprovide=e.metrics?.traceFunction("helia.routing.cancelReprovide",this.cancelReprovide.bind(this),{optionsIndex:1})??this.cancelReprovide,this.put=e.metrics?.traceFunction("helia.routing.put",this.put.bind(this),{optionsIndex:2})??this.put,this.get=e.metrics?.traceFunction("helia.routing.get",this.get.bind(this),{optionsIndex:1})??this.get,this.findPeer=e.metrics?.traceFunction("helia.routing.findPeer",this.findPeer.bind(this),{optionsIndex:1})??this.findPeer,this.getClosestPeers=e.metrics?.traceFunction("helia.routing.getClosestPeers",this.getClosestPeers.bind(this),{optionsIndex:1})??this.getClosestPeers}async start(){await ht(...this.routers)}async stop(){await Ct(...this.routers)}hasRouter(e){return this.routers.findIndex(t=>t.name===e)!==-1}addRouter(e){this.routers.push(e)}async*findProviders(e,t={}){if(this.routers.length===0)throw new os("No content routers available");let n=new mt({concurrency:this.providerLookupConcurrency}),o=0,s=[],i=this,a=0;this.log("findProviders for %c start using routers %s",e,this.routers.map(l=>l.toString()).join(", "));let c=ya(this.routers,"findProviders").map(async function*(l){let u=0;t?.onProgress?.(new R("helia:routing:find-providers:start",{routing:l.name,cid:e}));try{for await(let d of l.findProviders(e,t))u++,t?.onProgress?.(new R("helia:routing:find-providers:provider",{routing:l.name,cid:e,provider:d})),yield d}catch(d){s.push(d)}finally{i.log("router %s found %d providers for %c",l,u,e),t?.onProgress?.(new R("helia:routing:find-providers:end",{routing:l.name,cid:e,found:u})),a++,a===c.length&&n.size===0&&n.emitIdle()}});for await(let l of lr(n.toGenerator(),...c))if(l!=null){if(l.multiaddrs.length===0){if(n.queue.find(u=>u.options.peer.equals(l.id))!=null)continue;n.add(async()=>{try{let u=await this.findPeer(l.id,t);return u.multiaddrs.length===0?null:{...u,protocols:l.protocols,routing:l.routing}}catch(u){return this.log.error("could not load multiaddrs for peer %p - %e",l.id,u),null}},{peer:l.id,signal:t.signal}).catch(u=>{this.log.error("could not load multiaddrs for peer %p - %e",l.id,u)});continue}o++,yield l}this.log("findProviders finished, found %d providers for %c",o,e)}async provide(e,t={}){if(this.routers.length===0)throw new os("No content routers available");await Promise.all(ya(this.routers,"provide").map(async n=>{t?.onProgress?.(new R("helia:routing:provide:start",{routing:n.name,cid:e})),await n.provide(e,t),t?.onProgress?.(new R("helia:routing:provide:end",{routing:n.name,cid:e}))}))}async cancelReprovide(e,t={}){await Promise.all(ya(this.routers,"cancelReprovide").map(async n=>{t?.onProgress?.(new R("helia:routing:cancel-reprovide:start",{routing:n.name,cid:e})),await n.cancelReprovide(e,t),t?.onProgress?.(new R("helia:routing:cancel-reprovide:end",{routing:n.name,cid:e}))}))}async put(e,t,n){await Promise.all(ya(this.routers,"put").map(async o=>{n?.onProgress?.(new R("helia:routing:put:start",{routing:o.name,key:e,value:t})),await o.put(e,t,n),n?.onProgress?.(new R("helia:routing:put:end",{routing:o.name,key:e,value:t}))}))}async get(e,t){let n=[],o;try{o=await Promise.any(ya(this.routers,"get").map(async s=>{t?.onProgress?.(new R("helia:routing:get:start",{routing:s.name,key:e}));try{return await s.get(e,t)}catch(i){this.log("router %s failed with %e",s,i),n.push(i)}finally{t?.onProgress?.(new R("helia:routing:get:end",{routing:s.name,key:e}))}}))}catch{}if(o==null)throw new D1(n,`Failed to get value key ${M(e,"base58btc")}`);return o}async findPeer(e,t){if(this.routers.length===0)throw new os("No peer routers available");let n=this,o=lr(...ya(this.routers,"findPeer").map(s=>(async function*(){t?.onProgress?.(new R("helia:routing:find-peer:start",{routing:s.name,peerId:e}));try{yield await s.findPeer(e,t)}catch(i){n.log.error(i)}finally{t?.onProgress?.(new R("helia:routing:find-peer:end",{routing:s.name,peerId:e}))}})()));for await(let s of o)if(s!=null)return s;throw new Te("Could not find peer in routing")}async*getClosestPeers(e,t={}){if(this.routers.length===0)throw new os("No peer routers available");for await(let n of lr(...ya(this.routers,"getClosestPeers").map(async function*(o){t?.onProgress?.(new R("helia:routing:get-closest-peers:start",{routing:o.name,key:e}));try{yield*o.getClosestPeers(e,t)}finally{t?.onProgress?.(new R("helia:routing:get-closest-peers:end",{routing:o.name,key:e}))}})))n!=null&&(yield n)}};function ya(r,e){return r.filter(t=>t[e]!=null)}var Jg=class{info;blockstore;datastore;events;pins;logger;routing;getCodec;getHasher;getCrypto;dns;keychain;metrics;status;log;blockBrokers;mixins;components;constructor(e){this.info={name:e.name,version:e.version},this.logger=e.logger??PL(),this.log=this.logger.forComponent("helia"),this.getHasher=YL(e.hashers,e.loadHasher),this.getCodec=GL(e.codecs,e.loadCodec),this.getCrypto=jL(e.cryptos,e.loadCrypto),this.dns=e.dns??Am(),this.metrics=e.metrics,this.events=new le,this.status="stopped",this.mixins=[],this.components={blockstore:e.blockstore??new ih,datastore:e.datastore??new ta,logger:this.logger,blockBrokers:[],getHasher:this.getHasher,getCodec:this.getCodec,getCrypto:this.getCrypto,dns:this.dns,metrics:this.metrics,...e.components??{}},this.keychain=this.components.keychain=TL()(this.components),this.routing=this.components.routing=new Qg(this.components,{routers:(e.routers??[]).flatMap(n=>(typeof n=="function"&&(n=n(this.components)),[n])),providerLookupConcurrency:e.providerLookupConcurrency}),this.blockBrokers=this.components.blockBrokers=(e.blockBrokers??[]).map(n=>(typeof n=="function"&&(n=n(this.components)),n));let t=new jg(this.components,e);this.pins=new Xg(this.components.datastore,t,this.getCodec),this.blockstore=new Hg(t,this.pins,this.routing,{holdGcLock:e.holdGcLock??!0}),this.datastore=this.components.datastore}hasRouter(e){return this.routing.hasRouter(e)}addRouter(e){typeof e=="function"&&(e=e(this.components)),this.routing.addRouter(e)}hasBlockBroker(e){return this.blockBrokers.findIndex(t=>t.name===e)!==-1}addBlockBroker(e){typeof e=="function"&&(e=e(this.components)),this.blockBrokers.push(e)}addMixin(e){if(this.mixins.find(t=>t.name===e.name)!=null)throw new vo(`A mixin with the name "${e.name} is already present`);this.mixins.push(e)}async start(){this.status="starting",await OL(this.datastore),await ht(this.blockstore,this.datastore,this.routing,...this.blockBrokers);for(let e of this.mixins)await e.start?.(this);return this.status="started",this.events.dispatchEvent(new CustomEvent("start",{detail:this})),this}async stop(){this.status="stopping";for(let e of this.mixins)await e.stop?.(this);return await Ct(this.blockstore,this.datastore,this.routing,...this.blockBrokers),this.status="stopped",this.events.dispatchEvent(new CustomEvent("stop",{detail:this})),this}async gc(e={}){let t=await this.blockstore.lock.writeLock();try{let n=this,o=this.blockstore.unwrap();this.log("gc start"),await Er(o.deleteMany((async function*(){for await(let{cid:s}of o.getAll())try{if(await n.pins.isPinned(s,e))continue;yield s,e.onProgress?.(new R("helia:gc:deleted",s))}catch(i){n.log.error("error during gc - %e",i),e.onProgress?.(new R("helia:gc:error",i))}})()))}finally{t()}this.log("gc finished")}};var eD="7.0.2-4f472034",tD="helia";function JY(r={}){return cS(pL(PS(rD({...r,codecs:[Jw,nb,Sh,...r.codecs??[]],hashers:[_3,...r.hashers??[]]}))))}function rD(r={}){return new Jg({name:tD,version:eD,...r})}return cD(ZY);})();
|
|
113
|
+
${e}[Error list was empty]`,t.trim()}return CL(r,e)}function PY(r){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=r,e.destroy=()=>!0,e.extend=()=>e,e.useColors=()=>!1,e}function PL(r){return{forComponent(e){return LL(e,r)}}}function LL(r,e){let t=PY(`${r}:trace`);return je.enabled(`${r}:trace`)&&je.names.map(n=>n.toString()).find(n=>n.includes(":trace"))!=null&&(t=je(`${r}:trace`,e)),Object.assign(je(r,e),{error:je(`${r}:error`,e),trace:t,newScope:n=>LL(`${r}:${n}`,e)})}function kL(r){if(r!=null&&(r=r.trim(),r.length!==0))return r}var Jo=class{has(e,t){return Promise.reject(new Error(".has is not implemented"))}put(e,t,n){return Promise.reject(new Error(".put is not implemented"))}async*putMany(e,t){for await(let{cid:n,bytes:o}of e)await this.put(n,o,t),yield n}get(e,t){throw new Error(".get is not implemented")}async*getMany(e,t){for await(let n of e)yield{cid:n,bytes:this.get(n,t)}}delete(e,t){return Promise.reject(new Error(".delete is not implemented"))}async*deleteMany(e,t){for await(let n of e)await this.delete(n,t),yield n}async*getAll(e){throw new Error(".getAll is not implemented")}};function LY(r){return typeof r?.then=="function"}var ih=class extends Jo{data;constructor(){super(),this.data=new Map}put(e,t,n){n?.signal?.throwIfAborted();let o;if(t instanceof Uint8Array)o=[t];else{let s=Lo(t);if(LY(s))return s.then(i=>this._put(e,i,n));o=s}return this._put(e,o,n)}_put(e,t,n){return n?.signal?.throwIfAborted(),this.data.set(pt.encode(e.multihash.bytes),t),e}*get(e,t){t?.signal?.throwIfAborted();let n=this.data.get(pt.encode(e.multihash.bytes));if(n==null)throw new tn;yield*n}has(e,t){return t?.signal?.throwIfAborted(),this.data.has(pt.encode(e.multihash.bytes))}async delete(e,t){t?.signal?.throwIfAborted(),this.data.delete(pt.encode(e.multihash.bytes))}*getAll(e){e?.signal?.throwIfAborted();for(let[t,n]of this.data.entries())yield{cid:K.createV1(po,Ee(pt.decode(t))),bytes:(async function*(){yield*n})()},e?.signal?.throwIfAborted()}};var RLe=rc("blockstore:core:tiered");var DL="SHARDING";var eDe=new Me(DL);var pDe=rc("datastore:core:tiered");var Hg=class{lock;child;pins;routing;started;constructor(e,t,n,o={}){this.child=e,this.pins=t,this.routing=n,this.lock=wd({singleProcess:o.holdGcLock}),this.started=!1}isStarted(){return this.started}async start(){await ht(this.child),this.started=!0}async stop(){await Ct(this.child),this.started=!1}unwrap(){return this.child}async put(e,t,n={}){n?.signal?.throwIfAborted();let o=await this.lock.readLock();try{return await this.child.put(e,t,n)}finally{o()}}async*putMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.putMany(e,t)}finally{n()}}async*get(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.get(e,t)}finally{n()}}async*getMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{yield*this.child.getMany(e,t)}finally{n()}}async delete(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.writeLock();try{if(await this.pins.isPinned(e))throw new Nu("Block was pinned - please unpin and try again");await this.routing.cancelReprovide(e,t),await this.child.delete(e,t)}finally{n()}}async*deleteMany(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.writeLock();try{let o=this;yield*this.child.deleteMany((async function*(){for await(let s of e){if(await o.pins.isPinned(s))throw new Nu("Block was pinned - please unpin and try again");await o.routing.cancelReprovide(s,t),yield s}})(),t)}finally{n()}}async has(e,t={}){t?.signal?.throwIfAborted();let n=await this.lock.readLock();try{return await this.child.has(e,t)}finally{n()}}async*getAll(e={}){e?.signal?.throwIfAborted();let t=await this.lock.readLock();try{yield*this.child.getAll(e)}finally{t()}}createSession(e,t){return t?.signal?.throwIfAborted(),this.child.createSession(e,t)}};var sb=new Me("/version"),NL=1;async function OL(r){if(!await r.has(sb)){await r.put(sb,O(`${NL}`));return}let e=await r.get(sb),t=M(e);if(parseInt(t,10)!==NL)throw new L1("Invalid datastore version, a datastore migration may be required")}var db={};V(db,{code:()=>ub,createLink:()=>zL,createNode:()=>qL,decode:()=>zY,encode:()=>qY,name:()=>HY,prepare:()=>cb,validate:()=>lb});var RY=new TextDecoder;function ib(r,e){let t=0;for(let n=0;;n+=7){if(n>=64)throw new Error("protobuf: varint overflow");if(e>=r.length)throw new Error("protobuf: unexpected end of data");let o=r[e++];if(t+=n<28?(o&127)<<n:(o&127)*2**n,o<128)break}return[t,e]}function qg(r,e){let t;[t,e]=ib(r,e);let n=e+t;if(t<0||n<0)throw new Error("protobuf: invalid length");if(n>r.length)throw new Error("protobuf: unexpected end of data");return[r.subarray(e,n),n]}function BL(r,e){let t;return[t,e]=ib(r,e),[t&7,t>>3,e]}function NY(r){let e={},t=r.length,n=0;for(;n<t;){let o,s;if([o,s,n]=BL(r,n),s===1){if(e.Hash)throw new Error("protobuf: (PBLink) duplicate Hash section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Hash`);if(e.Name!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Name before Hash");if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Hash");[e.Hash,n]=qg(r,n)}else if(s===2){if(e.Name!==void 0)throw new Error("protobuf: (PBLink) duplicate Name section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Name`);if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Name");let i;[i,n]=qg(r,n),e.Name=RY.decode(i)}else if(s===3){if(e.Tsize!==void 0)throw new Error("protobuf: (PBLink) duplicate Tsize section");if(o!==0)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Tsize`);[e.Tsize,n]=ib(r,n)}else throw new Error(`protobuf: (PBLink) invalid fieldNumber, expected 1, 2 or 3, got ${s}`)}if(n>t)throw new Error("protobuf: (PBLink) unexpected end of data");return e}function ML(r){let e=r.length,t=0,n,o=!1,s;for(;t<e;){let a,c;if([a,c,t]=BL(r,t),a!==2)throw new Error(`protobuf: (PBNode) invalid wireType, expected 2, got ${a}`);if(c===1){if(s)throw new Error("protobuf: (PBNode) duplicate Data section");[s,t]=qg(r,t),n&&(o=!0)}else if(c===2){if(o)throw new Error("protobuf: (PBNode) duplicate Links section");n||(n=[]);let l;[l,t]=qg(r,t),n.push(NY(l))}else throw new Error(`protobuf: (PBNode) invalid fieldNumber, expected 1 or 2, got ${c}`)}if(t>e)throw new Error("protobuf: (PBNode) unexpected end of data");let i={};return s&&(i.Data=s),i.Links=n||[],i}var FL=new TextEncoder,UL=2**32,OY=2**31;function BY(r,e){let t=e.length;if(typeof r.Tsize=="number"){if(r.Tsize<0)throw new Error("Tsize cannot be negative");if(!Number.isSafeInteger(r.Tsize))throw new Error("Tsize too large for encoding");t=ah(e,t,r.Tsize)-1,e[t]=24}if(typeof r.Name=="string"){let n=FL.encode(r.Name);t-=n.length,e.set(n,t),t=ah(e,t,n.length)-1,e[t]=18}return r.Hash&&(t-=r.Hash.length,e.set(r.Hash,t),t=ah(e,t,r.Hash.length)-1,e[t]=10),e.length-t}function $L(r){let e=UY(r),t=new Uint8Array(e),n=e;if(r.Data&&(n-=r.Data.length,t.set(r.Data,n),n=ah(t,n,r.Data.length)-1,t[n]=10),r.Links)for(let o=r.Links.length-1;o>=0;o--){let s=BY(r.Links[o],t.subarray(0,n));n-=s,n=ah(t,n,s)-1,t[n]=18}return t}function MY(r){let e=0;if(r.Hash){let t=r.Hash.length;e+=1+t+Ol(t)}if(typeof r.Name=="string"){let t=FL.encode(r.Name).length;e+=1+t+Ol(t)}return typeof r.Tsize=="number"&&(e+=1+Ol(r.Tsize)),e}function UY(r){let e=0;if(r.Data){let t=r.Data.length;e+=1+t+Ol(t)}if(r.Links)for(let t of r.Links){let n=MY(t);e+=1+n+Ol(n)}return e}function ah(r,e,t){e-=Ol(t);let n=e;for(;t>=OY;)r[e++]=t&127|128,t/=128;for(;t>=128;)r[e++]=t&127|128,t>>>=7;return r[e]=t,n}function Ol(r){return r%2===0&&r++,Math.floor((FY(r)+6)/7)}function FY(r){let e=0;return r>=UL&&(r=Math.floor(r/UL),e=32),r>=65536&&(r>>>=16,e+=16),r>=256&&(r>>>=8,e+=8),e+$Y[r]}var $Y=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8];var KY=["Data","Links"],VY=["Hash","Name","Tsize"],ab=new TextEncoder;function VL(r,e){if(r===e)return 0;let t=r.Name?ab.encode(r.Name):[],n=e.Name?ab.encode(e.Name):[],o=t.length,s=n.length;for(let i=0,a=Math.min(o,s);i<a;++i)if(t[i]!==n[i]){o=t[i],s=n[i];break}return o<s?-1:s<o?1:0}function KL(r,e){return!Object.keys(r).some(t=>!e.includes(t))}function HL(r){if(typeof r.asCID=="object"){let t=K.asCID(r);if(!t)throw new TypeError("Invalid DAG-PB form");return{Hash:t}}if(typeof r!="object"||Array.isArray(r))throw new TypeError("Invalid DAG-PB form");let e={};if(r.Hash){let t=K.asCID(r.Hash);try{t||(typeof r.Hash=="string"?t=K.parse(r.Hash):r.Hash instanceof Uint8Array&&(t=K.decode(r.Hash)))}catch(n){throw new TypeError(`Invalid DAG-PB form: ${n.message}`)}t&&(e.Hash=t)}if(!e.Hash)throw new TypeError("Invalid DAG-PB form");return typeof r.Name=="string"&&(e.Name=r.Name),typeof r.Tsize=="number"&&(e.Tsize=r.Tsize),e}function cb(r){if((r instanceof Uint8Array||typeof r=="string")&&(r={Data:r}),typeof r!="object"||Array.isArray(r))throw new TypeError("Invalid DAG-PB form");let e={};if(r.Data!==void 0)if(typeof r.Data=="string")e.Data=ab.encode(r.Data);else if(r.Data instanceof Uint8Array)e.Data=r.Data;else throw new TypeError("Invalid DAG-PB form");if(r.Links!==void 0)if(Array.isArray(r.Links))e.Links=r.Links.map(HL),e.Links.sort(VL);else throw new TypeError("Invalid DAG-PB form");else e.Links=[];return e}function lb(r){if(!r||typeof r!="object"||Array.isArray(r)||r instanceof Uint8Array||r["/"]&&r["/"]===r.bytes)throw new TypeError("Invalid DAG-PB form");if(!KL(r,KY))throw new TypeError("Invalid DAG-PB form (extraneous properties)");if(r.Data!==void 0&&!(r.Data instanceof Uint8Array))throw new TypeError("Invalid DAG-PB form (Data must be bytes)");if(!Array.isArray(r.Links))throw new TypeError("Invalid DAG-PB form (Links must be a list)");for(let e=0;e<r.Links.length;e++){let t=r.Links[e];if(!t||typeof t!="object"||Array.isArray(t)||t instanceof Uint8Array||t["/"]&&t["/"]===t.bytes)throw new TypeError("Invalid DAG-PB form (bad link)");if(!KL(t,VY))throw new TypeError("Invalid DAG-PB form (extraneous properties on link)");if(t.Hash===void 0)throw new TypeError("Invalid DAG-PB form (link must have a Hash)");if(t.Hash==null||!t.Hash["/"]||t.Hash["/"]!==t.Hash.bytes)throw new TypeError("Invalid DAG-PB form (link Hash must be a CID)");if(t.Name!==void 0&&typeof t.Name!="string")throw new TypeError("Invalid DAG-PB form (link Name must be a string)");if(t.Tsize!==void 0){if(typeof t.Tsize!="number"||t.Tsize%1!==0)throw new TypeError("Invalid DAG-PB form (link Tsize must be an integer)");if(t.Tsize<0)throw new TypeError("Invalid DAG-PB form (link Tsize cannot be negative)")}if(e>0&&VL(t,r.Links[e-1])===-1)throw new TypeError("Invalid DAG-PB form (links must be sorted by Name bytes)")}}function qL(r,e=[]){return cb({Data:r,Links:e})}function zL(r,e,t){return HL({Hash:t,Name:r,Tsize:e})}function WL(r){return r instanceof ArrayBuffer?new Uint8Array(r,0,r.byteLength):r}var HY="dag-pb",ub=112;function qY(r){lb(r);let e={};return r.Links&&(e.Links=r.Links.map(t=>{let n={};return t.Hash&&(n.Hash=t.Hash.bytes),t.Name!==void 0&&(n.Name=t.Name),t.Tsize!==void 0&&(n.Tsize=t.Tsize),n})),r.Data&&(e.Data=r.Data),$L(e)}function zY(r){let e=WL(r),t=ML(e),n={};return t.Data&&(n.Data=t.Data),t.Links&&(n.Links=t.Links.map(o=>{let s={};try{s.Hash=K.decode(o.Hash)}catch{}if(!s.Hash)throw new Error("Invalid Hash field found in link, expected CID");return o.Name!==void 0&&(s.Name=o.Name),o.Tsize!==void 0&&(s.Tsize=o.Tsize),s})),n}function GL(r=[],e){let t={[ub]:db,[po]:fi};return r.forEach(n=>{t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.code]=o}if(o!=null)return o;throw new lu(`Could not load codec for ${n}`)}}function jL(r=[],e){let t={};return r=[$3(),V3(),iu(),...r],r.forEach(n=>{t[n.type]=n,t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.type]=o,t[o.code]=o}if(o!=null)return o;throw new uu(`Could not load crypto for ${o}`)}}function YL(r=[],e){let t={[He.code]:He,[Ze.code]:Ze};return r.forEach(n=>{t[n.code]=n}),async n=>{let o=t[n];if(o==null&&e!=null){let s=e(n);gs(s)?o=await s:o=s,t[o.code]=o}if(o!=null)return o;throw new cu(`No hasher configured for multihash code 0x${n.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`)}}var zg=0,Bl=class extends Error{static name="IdentityHashDigestTooLongError";name="IdentityHashDigestTooLongError"},Wg=class extends Jo{child;maxDigestLength;constructor(e,t){super(),this.child=e,this.maxDigestLength=t?.maxDigestLength}put(e,t,n){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);return n?.signal?.throwIfAborted(),e}return this.child==null?(n?.signal?.throwIfAborted(),e):this.child.put(e,t,n)}async*get(e,t){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);t?.signal?.throwIfAborted(),yield e.multihash.digest;return}if(this.child==null)throw t?.signal?.throwIfAborted(),new tn;yield*this.child.get(e,t)}has(e,t){if(e.multihash.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);return t?.signal?.throwIfAborted(),!0}return this.child==null?(t?.signal?.throwIfAborted(),!1):this.child.has(e,t)}delete(e,t){if(e.code===zg){if(this.maxDigestLength!=null&&e.multihash.digest.byteLength>this.maxDigestLength)throw new Bl(`Identity digest too long - ${e.multihash.digest.byteLength} > this.maxDigestLength`);t?.signal?.throwIfAborted();return}if(this.child!=null)return this.child.delete(e,t)}async*getAll(e){this.child!=null&&(yield*this.child.getAll(e)),e?.signal?.throwIfAborted()}};function WY(r){return r[Symbol.asyncIterator]!=null}function XL(r){return r?.then!=null}function GY(r,e){let t=0;if(WY(r))return(async function*(){for await(let c of r){let l=e(c,t++);XL(l)&&await l,yield c}})();let n=ic(r),{value:o,done:s}=n.next();if(s===!0)return(function*(){})();let i=e(o,t++);if(typeof i?.then=="function")return(async function*(){await i,yield o;for(let c of n){let l=e(c,t++);XL(l)&&await l,yield c}})();let a=e;return(function*(){yield o;for(let c of n)a(c,t++),yield c})()}var fb=GY;var jY=128,Ml=class{child;getHasher;log;logger;blockBrokers;constructor(e,t={}){this.log=e.logger.forComponent("helia:networked-storage"),this.logger=e.logger,this.blockBrokers=e.blockBrokers,this.child=new Wg(e.blockstore,{maxDigestLength:t.maxIdentityHashDigestLength??jY}),this.getHasher=e.getHasher}async put(e,t,n={}){return await this.child.has(e,n)?(n.onProgress?.(new R("blocks:put:duplicate",e)),e):(n.onProgress?.(new R("blocks:put:providers:notify",e)),await Promise.all(this.blockBrokers.map(async o=>o.announce?.(e,n))),n.onProgress?.(new R("blocks:put:blockstore:put",e)),this.child.put(e,t,n))}async*putMany(e,t={}){let n=Br(e,async({cid:s})=>{let i=await this.child.has(s,t);return i&&t.onProgress?.(new R("blocks:put-many:duplicate",s)),!i}),o=fb(n,async({cid:s})=>{t.onProgress?.(new R("blocks:put-many:providers:notify",s)),await Promise.all(this.blockBrokers.map(async i=>i.announce?.(s,t)))});t.onProgress?.(new R("blocks:put-many:blockstore:put-many")),yield*this.child.putMany(o,t)}async*get(e,t={}){let n=await this.child.has(e,t),o=t.offline===!0;if(!n){if(o)throw new Ou("The block was present in the blockstore and the node is running offline so cannot fetch it");let s=await this.getHasher(e.multihash.code);t?.signal?.throwIfAborted(),t.onProgress?.(new R("blocks:get:providers:get",e));let i=await QL(e,this.blockBrokers,s,{...t,log:this.log});t.onProgress?.(new R("blocks:get:blockstore:put",e)),await this.child.put(e,i,t),t.onProgress?.(new R("blocks:get:providers:notify",e)),await Promise.all(this.blockBrokers.map(async a=>a.announce?.(e,t))),yield i;return}t.onProgress?.(new R("blocks:get:blockstore:get",e)),yield*this.child.get(e,t)}async*getMany(e,t={}){t.onProgress?.(new R("blocks:get-many:blockstore:get-many")),yield*this.child.getMany(fb(e,async n=>{let o=await this.child.has(n,t),s=t.offline===!0;if(!o){if(s)throw new Ou("The block was present in the blockstore and the node is running offline so cannot fetch it");let i=await this.getHasher(n.multihash.code);t?.signal?.throwIfAborted(),t.onProgress?.(new R("blocks:get-many:providers:get",n));let a=await QL(n,this.blockBrokers,i,{...t,log:this.log});t.onProgress?.(new R("blocks:get-many:blockstore:put",n)),await this.child.put(n,a,t),t.onProgress?.(new R("blocks:get-many:providers:notify",n)),await Promise.all(this.blockBrokers.map(async c=>c.announce?.(n,t)))}}))}async delete(e,t={}){t.onProgress?.(new R("blocks:delete:blockstore:delete",e)),await this.child.delete(e,t)}async*deleteMany(e,t={}){t.onProgress?.(new R("blocks:delete-many:blockstore:delete-many")),yield*this.child.deleteMany((async function*(){for await(let n of e)yield n})(),t)}async has(e,t={}){return this.child.has(e,t)}async*getAll(e={}){e.onProgress?.(new R("blocks:get-all:blockstore:get-many")),yield*this.child.getAll(e)}};async function QL(r,e,t,n){let o=XY(r,t),s=new AbortController,i=de([s.signal,n.signal]);s.signal;let a=[];for(let c of e)YY(c)&&a.push(c);if(a.length===0)throw new vo(`No block brokers capable of retrieving blocks are configured, the CID ${r} cannot be fetched from the network`);try{return await Promise.any(a.map(async c=>{try{let l=!1,u=await c.retrieve(r,{...n,signal:i,validateFn:async d=>{await o(d),n.signal?.throwIfAborted(),l=!0}});return l||(await o(u),n.signal?.throwIfAborted()),u}catch(l){throw n.log.error("could not retrieve verified block for %c from %s - %e",r,c.name,l),l}}))}catch(c){throw new R1(c.errors,`Failed to load block for ${r}`)}finally{s.abort(),i.clear()}}function YY(r){return typeof r.retrieve=="function"}var XY=(r,e)=>{if(e==null)throw new A(`No hasher configured for multihash code 0x${r.multihash.code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`);return async t=>{let n,o=e.digest(t,{truncate:r.multihash.digest.byteLength});if(gs(o)?n=await o:n=o,!te(n.digest,r.multihash.digest))throw new va("Hash of downloaded block did not match multihash from passed CID")}};var Gg=class extends Ml{closeController;constructor(e,t){super(e),this.closeController=new AbortController,this.closeController.signal,this.log=e.logger.forComponent(`helia:session-storage:${t.root}`)}close(){this.closeController.abort()}async addPeer(e,t){await Promise.all(this.blockBrokers.map(n=>n.addPeer(e,t)))}async put(e,t,n={}){let o=de([this.closeController.signal,n.signal]);try{return await super.put(e,t,{...n,signal:o})}finally{o.clear()}}async*putMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.putMany(e,{...t,signal:n})}finally{n.clear()}}async*get(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.get(e,{...t,signal:n})}finally{n.clear()}}async*getMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.getMany(e,{...t,signal:n})}finally{n.clear()}}async delete(e,t={}){let n=de([this.closeController.signal,t.signal]);try{await super.delete(e,{...t,signal:n})}finally{n.clear()}}async*deleteMany(e,t={}){let n=de([this.closeController.signal,t.signal]);try{yield*super.deleteMany(e,{...t,signal:n})}finally{n.clear()}}async has(e,t={}){let n=de([this.closeController.signal,t.signal]);try{return await super.has(e,{...t,signal:n})}finally{n.clear()}}async*getAll(e={}){let t=de([this.closeController.signal,e.signal]);try{yield*super.getAll({...e,signal:t})}finally{t.clear()}}};var jg=class extends Ml{started;constructor(e,t={}){super(e,t),this.started=!1}isStarted(){return this.started}async start(){await ht(this.child,...this.blockBrokers),this.started=!0}async stop(){await Ct(this.child,...this.blockBrokers),this.started=!1}unwrap(){return this.child}createSession(e,t){if(this.blockBrokers.length===0)throw new vo("No block brokers configured");let n=this.blockBrokers.map(o=>o.createSession?.(t)).filter(o=>o!=null);if(n.length===0)throw new vo(`No configured block brokers support sessions - tried ${this.blockBrokers.map(o=>o.name).join(", ")}`);return new Gg({blockstore:this.child,blockBrokers:n,getHasher:this.getHasher,logger:this.logger},{root:e})}};var ZL="/pin/",JL="/pinned-block/",hb=or;function Yg(r){return r.version===0&&(r=r.toV1()),new Me(`${ZL}${r.toString(hb)}`)}var Xg=class{datastore;blockstore;getCodec;constructor(e,t,n){this.datastore=e,this.blockstore=t,this.getCodec=n}async*add(e,t={}){let n=Yg(e);if(await this.datastore.has(n))throw new P1("Already pinned");let o=Math.round(t.depth??1/0);if(o<0)throw new A("Depth must be greater than or equal to 0");let s=(t.walker??a6())({blockstore:this.blockstore,getCodec:this.getCodec});for await(let a of s.walk(e,{...t,depth:o})){let c=a.block.cid;await this.#e(c,l=>l.pinnedBy.find(u=>te(u,e.bytes))!=null?!1:(l.pinCount++,l.pinnedBy.push(e.bytes),!0),t),yield c}let i={depth:o,metadata:t.metadata??{}};await this.datastore.put(n,ki(i),t)}async#e(e,t,n){let o=new Me(`${JL}${hb.encode(e.multihash.bytes)}`),s={pinCount:0,pinnedBy:[]};try{s=xn(await this.datastore.get(o,n))}catch(a){if(a.name!=="NotFoundError")throw a}if(t(s)){if(s.pinCount===0&&await this.datastore.has(o)){await this.datastore.delete(o);return}await this.datastore.put(o,ki(s),n),n.onProgress?.(new R("helia:pin:add",e))}}async*rm(e,t={}){let n=Yg(e),o=await this.datastore.get(n,t),s=xn(o);await this.datastore.delete(n,t);let i=(t.walker??a6())({blockstore:this.blockstore,getCodec:this.getCodec});for await(let a of i.walk(e,{...t,depth:s.depth})){let c=a.block.cid;await this.#e(c,l=>(l.pinCount--,l.pinnedBy=l.pinnedBy.filter(u=>te(u,e.bytes)),!0),{...t,depth:s.depth}),yield c}}async*ls(e={}){for await(let{key:t,value:n}of this.datastore.query({prefix:ZL+(e.cid!=null?`${e.cid.toString(or)}`:"")},e)){let o=K.parse(t.toString().substring(5),or),s=xn(n);yield{cid:o,...s}}}async isPinned(e,t={}){let n=new Me(`${JL}${hb.encode(e.multihash.bytes)}`);return this.datastore.has(n,t)}async get(e,t){let n=Yg(e),o=await this.datastore.get(n,t);return xn(o)}async setMetadata(e,t,n){let o=Yg(e),s=await this.datastore.get(o,n),i=xn(s);i.metadata=t??{},await this.datastore.put(o,ki(i),n)}};var QY=5,Qg=class{name;log;routers;providerLookupConcurrency;constructor(e,t){this.name="helia",this.log=e.logger.forComponent("helia:routing"),this.routers=t.routers??[],this.providerLookupConcurrency=t.providerLookupConcurrency??QY,this.findProviders=e.metrics?.traceFunction("helia.routing.findProviders",this.findProviders.bind(this),{optionsIndex:1})??this.findProviders,this.provide=e.metrics?.traceFunction("helia.routing.provide",this.provide.bind(this),{optionsIndex:1})??this.provide,this.cancelReprovide=e.metrics?.traceFunction("helia.routing.cancelReprovide",this.cancelReprovide.bind(this),{optionsIndex:1})??this.cancelReprovide,this.put=e.metrics?.traceFunction("helia.routing.put",this.put.bind(this),{optionsIndex:2})??this.put,this.get=e.metrics?.traceFunction("helia.routing.get",this.get.bind(this),{optionsIndex:1})??this.get,this.findPeer=e.metrics?.traceFunction("helia.routing.findPeer",this.findPeer.bind(this),{optionsIndex:1})??this.findPeer,this.getClosestPeers=e.metrics?.traceFunction("helia.routing.getClosestPeers",this.getClosestPeers.bind(this),{optionsIndex:1})??this.getClosestPeers}async start(){await ht(...this.routers)}async stop(){await Ct(...this.routers)}hasRouter(e){return this.routers.findIndex(t=>t.name===e)!==-1}addRouter(e){this.routers.push(e)}async*findProviders(e,t={}){if(this.routers.length===0)throw new os("No content routers available");let n=new mt({concurrency:this.providerLookupConcurrency}),o=0,s=[],i=this,a=0;this.log("findProviders for %c start using routers %s",e,this.routers.map(l=>l.toString()).join(", "));let c=ya(this.routers,"findProviders").map(async function*(l){let u=0;t?.onProgress?.(new R("helia:routing:find-providers:start",{routing:l.name,cid:e}));try{for await(let d of l.findProviders(e,t))u++,t?.onProgress?.(new R("helia:routing:find-providers:provider",{routing:l.name,cid:e,provider:d})),yield d}catch(d){s.push(d)}finally{i.log("router %s found %d providers for %c",l,u,e),t?.onProgress?.(new R("helia:routing:find-providers:end",{routing:l.name,cid:e,found:u})),a++,a===c.length&&n.size===0&&n.emitIdle()}});for await(let l of lr(n.toGenerator(),...c))if(l!=null){if(l.multiaddrs.length===0){if(n.queue.find(u=>u.options.peer.equals(l.id))!=null)continue;n.add(async()=>{try{let u=await this.findPeer(l.id,t);return u.multiaddrs.length===0?null:{...u,protocols:l.protocols,routing:l.routing}}catch(u){return this.log.error("could not load multiaddrs for peer %p - %e",l.id,u),null}},{peer:l.id,signal:t.signal}).catch(u=>{this.log.error("could not load multiaddrs for peer %p - %e",l.id,u)});continue}o++,yield l}this.log("findProviders finished, found %d providers for %c",o,e)}async provide(e,t={}){if(this.routers.length===0)throw new os("No content routers available");await Promise.all(ya(this.routers,"provide").map(async n=>{t?.onProgress?.(new R("helia:routing:provide:start",{routing:n.name,cid:e})),await n.provide(e,t),t?.onProgress?.(new R("helia:routing:provide:end",{routing:n.name,cid:e}))}))}async cancelReprovide(e,t={}){await Promise.all(ya(this.routers,"cancelReprovide").map(async n=>{t?.onProgress?.(new R("helia:routing:cancel-reprovide:start",{routing:n.name,cid:e})),await n.cancelReprovide(e,t),t?.onProgress?.(new R("helia:routing:cancel-reprovide:end",{routing:n.name,cid:e}))}))}async put(e,t,n){await Promise.all(ya(this.routers,"put").map(async o=>{n?.onProgress?.(new R("helia:routing:put:start",{routing:o.name,key:e,value:t})),await o.put(e,t,n),n?.onProgress?.(new R("helia:routing:put:end",{routing:o.name,key:e,value:t}))}))}async get(e,t){let n=[],o;try{o=await Promise.any(ya(this.routers,"get").map(async s=>{t?.onProgress?.(new R("helia:routing:get:start",{routing:s.name,key:e}));try{return await s.get(e,t)}catch(i){this.log("router %s failed with %e",s,i),n.push(i)}finally{t?.onProgress?.(new R("helia:routing:get:end",{routing:s.name,key:e}))}}))}catch{}if(o==null)throw new D1(n,`Failed to get value key ${M(e,"base58btc")}`);return o}async findPeer(e,t){if(this.routers.length===0)throw new os("No peer routers available");let n=this,o=lr(...ya(this.routers,"findPeer").map(s=>(async function*(){t?.onProgress?.(new R("helia:routing:find-peer:start",{routing:s.name,peerId:e}));try{yield await s.findPeer(e,t)}catch(i){n.log.error(i)}finally{t?.onProgress?.(new R("helia:routing:find-peer:end",{routing:s.name,peerId:e}))}})()));for await(let s of o)if(s!=null)return s;throw new Te("Could not find peer in routing")}async*getClosestPeers(e,t={}){if(this.routers.length===0)throw new os("No peer routers available");for await(let n of lr(...ya(this.routers,"getClosestPeers").map(async function*(o){t?.onProgress?.(new R("helia:routing:get-closest-peers:start",{routing:o.name,key:e}));try{yield*o.getClosestPeers(e,t)}finally{t?.onProgress?.(new R("helia:routing:get-closest-peers:end",{routing:o.name,key:e}))}})))n!=null&&(yield n)}};function ya(r,e){return r.filter(t=>t[e]!=null)}var Jg=class{info;blockstore;datastore;events;pins;logger;routing;getCodec;getHasher;getCrypto;dns;keychain;metrics;status;log;blockBrokers;mixins;components;constructor(e){this.info={name:e.name,version:e.version},this.logger=e.logger??PL(),this.log=this.logger.forComponent("helia"),this.getHasher=YL(e.hashers,e.loadHasher),this.getCodec=GL(e.codecs,e.loadCodec),this.getCrypto=jL(e.cryptos,e.loadCrypto),this.dns=e.dns??Am(),this.metrics=e.metrics,this.events=new le,this.status="stopped",this.mixins=[],this.components={blockstore:e.blockstore??new ih,datastore:e.datastore??new ta,logger:this.logger,blockBrokers:[],getHasher:this.getHasher,getCodec:this.getCodec,getCrypto:this.getCrypto,dns:this.dns,metrics:this.metrics,...e.components??{}},this.keychain=this.components.keychain=TL()(this.components),this.routing=this.components.routing=new Qg(this.components,{routers:(e.routers??[]).flatMap(n=>(typeof n=="function"&&(n=n(this.components)),[n])),providerLookupConcurrency:e.providerLookupConcurrency}),this.blockBrokers=this.components.blockBrokers=(e.blockBrokers??[]).map(n=>(typeof n=="function"&&(n=n(this.components)),n));let t=new jg(this.components,e);this.pins=new Xg(this.components.datastore,t,this.getCodec),this.blockstore=new Hg(t,this.pins,this.routing,{holdGcLock:e.holdGcLock??!0}),this.datastore=this.components.datastore}hasRouter(e){return this.routing.hasRouter(e)}addRouter(e){typeof e=="function"&&(e=e(this.components)),this.routing.addRouter(e)}hasBlockBroker(e){return this.blockBrokers.findIndex(t=>t.name===e)!==-1}addBlockBroker(e){typeof e=="function"&&(e=e(this.components)),this.blockBrokers.push(e)}addMixin(e){if(this.mixins.find(t=>t.name===e.name)!=null)throw new vo(`A mixin with the name "${e.name} is already present`);this.mixins.push(e)}async start(){this.status="starting",await OL(this.datastore),await ht(this.blockstore,this.datastore,this.routing,...this.blockBrokers);for(let e of this.mixins)await e.start?.(this);return this.status="started",this.events.dispatchEvent(new CustomEvent("start",{detail:this})),this}async stop(){this.status="stopping";for(let e of this.mixins)await e.stop?.(this);return await Ct(this.blockstore,this.datastore,this.routing,...this.blockBrokers),this.status="stopped",this.events.dispatchEvent(new CustomEvent("stop",{detail:this})),this}async gc(e={}){let t=await this.blockstore.lock.writeLock();try{let n=this,o=this.blockstore.unwrap();this.log("gc start"),await Er(o.deleteMany((async function*(){for await(let{cid:s}of o.getAll())try{if(await n.pins.isPinned(s,e))continue;yield s,e.onProgress?.(new R("helia:gc:deleted",s))}catch(i){n.log.error("error during gc - %e",i),e.onProgress?.(new R("helia:gc:error",i))}})()))}finally{t()}this.log("gc finished")}};var eD="7.0.3",tD="helia";function JY(r={}){return cS(pL(PS(rD({...r,codecs:[Jw,nb,Sh,...r.codecs??[]],hashers:[_3,...r.hashers??[]]}))))}function rD(r={}){return new Jg({name:tD,version:eD,...r})}return cD(ZY);})();
|
|
114
114
|
/*! Bundled license information:
|
|
115
115
|
|
|
116
116
|
pvtsutils/build/index.js:
|