@helia/unixfs 8.0.8 → 8.0.9

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 CHANGED
@@ -9,6 +9,6 @@ ${e}`)}`:n!=null?`${n.split(`
9
9
  ${e}`)}`:r!=null?`${r}`:`${t.toString()}`}function mh(t){return t instanceof AggregateError||t?.name==="AggregateError"&&Array.isArray(t.errors)}function tu(t,e=""){if(mh(t)){let r=Qc(t,e);return t.errors.length>0?(e=`${e} `,r+=`
10
10
  ${e}${t.errors.map(n=>`${tu(n,`${e}`)}`).join(`
11
11
  ${e}`)}`):r+=`
12
- ${e}[Error list was empty]`,r.trim()}return Qc(t,e)}ae.formatters.e=t=>t==null?"undefined":tu(t);function ph(t){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=t,e.destroy=()=>!0,e.extend=()=>e,e.useColors=()=>!1,e}function R(t,e){let r=ph(`${t}:trace`);return ae.enabled(`${t}:trace`)&&ae.names.map(n=>n.toString()).find(n=>n.includes(":trace"))!=null&&(r=ae(`${t}:trace`,e)),Object.assign(ae(t,e),{error:ae(`${t}:error`,e),trace:r,newScope:n=>R(`${t}:${n}`,e)})}function eu(t){if(t!=null&&(t=t.trim(),t.length!==0))return t}var Io=zr(nn(),1);function Zn(t){function e(r){return r instanceof Xn?r:new Xn(r,t)}return e}var Xn=class{_value;_hashFn;_depth;_availableBits;_currentBufferIndex;_buffers;constructor(e,r){if(!(e instanceof Uint8Array))throw new Error("can only hash Uint8Arrays");this._value=e,this._hashFn=r,this._depth=-1,this._availableBits=0,this._currentBufferIndex=0,this._buffers=[]}async take(e){let r=e;for(;this._availableBits<r;)await this._produceMoreBits();let n=0;for(;r>0;){let i=this._buffers[this._currentBufferIndex],o=Math.min(i.availableBits(),r),s=i.take(o);n=(n<<o)+s,r-=o,this._availableBits-=o,i.availableBits()===0&&this._currentBufferIndex++}return n}untake(e){let r=e;for(;r>0;){let n=this._buffers[this._currentBufferIndex],i=Math.min(n.totalBits()-n.availableBits(),r);n.untake(i),r-=i,this._availableBits+=i,this._currentBufferIndex>0&&n.totalBits()===n.availableBits()&&(this._depth--,this._currentBufferIndex--)}}async _produceMoreBits(){this._depth++;let e=this._depth>0?Te([this._value,Uint8Array.from([this._depth])]):this._value,r=await this._hashFn(e),n=new So(r);this._buffers.push(n),this._availableBits+=n.availableBits()}},yh=[255,254,252,248,240,224,192,128],gh=[1,3,7,15,31,63,127,255],So=class{_value;_currentBytePos;_currentBitPos;constructor(e){this._value=e,this._currentBytePos=e.length-1,this._currentBitPos=7}availableBits(){return this._currentBitPos+1+this._currentBytePos*8}totalBits(){return this._value.length*8}take(e){let r=e,n=0;for(;r>0&&this._haveBits();){let i=this._value[this._currentBytePos],o=this._currentBitPos+1,s=Math.min(o,r),a=wh(i,o-s,s);n=(n<<s)+a,r-=s,this._currentBitPos-=s,this._currentBitPos<0&&(this._currentBitPos=7,this._currentBytePos--)}return n}untake(e){for(this._currentBitPos+=e;this._currentBitPos>7;)this._currentBitPos-=8,this._currentBytePos+=1}_haveBits(){return this._currentBytePos>=0}};function wh(t,e,r){let n=bh(e,r);return(t&n)>>>e}function bh(t,e){return yh[t]&gh[Math.min(e+t-1,7)]}var ru=BigInt(pt.code),Be=8;async function Qn(t){return(await pt.encode(t)).subarray(0,8).reverse()}var nu=zr(nn(),1);var qe=async(t,e,r)=>{r.codec==null&&(r.codec=$);let n=await fe.digest(t),i=S.create(r.cidVersion??1,r.codec.code,n);return await e.put(i,t,{...r,signal:r.signal}),i};var vo=R("helia:unixfs:commands:utils:hamt-utils"),ei=(t,e=Be)=>{let r=(Math.pow(2,e)-1).toString(16).length;return t.toString(16).toUpperCase().padStart(r,"0").substring(0,r)},iu=async(t,e,r)=>{let n=new gr({root:!0,dir:!0,parent:void 0,parentKey:void 0,path:"",dirty:!0,flat:!1,mtime:r.mtime,mode:r.mode},r);for(let o=0;o<e.length;o++)await n.bucket.put(e[o].name,{size:e[o].size,cid:e[o].cid});let i=await Se(n.flush(t));if(i==null)throw new Error("Flushing shard yielded no result");return i},ti=async(t,e,r)=>{let n=k.unmarshal(t[0].node.Data??new Uint8Array(0)),i=BigInt(Math.pow(2,r.shardFanoutBits??Be));t.reverse();let o,s;for(let a=0;a<t.length;a++){let c=a===t.length-1,l=t[a],u=Uint8Array.from(l.children.bitField().reverse()),d=new k({type:"hamt-sharded-directory",data:u,fanout:i,hashType:ru});c&&(d.mtime=n.mtime,d.mode=n.mode),s={Data:d.marshal(),Links:l.node.Links};let h=I(z(s));if(o=await qe(h,e,r),!c){let p=t[a+1];if(p==null)throw new Error("Was not operating on shard root but also had no parent?");vo("updating link in parent sub-shard with prefix %s",p.prefix),p.node.Links=p.node.Links.filter(_=>_.Name!==p.prefix),p.node.Links.push({Name:p.prefix,Hash:o,Tsize:l.node.Links.reduce((_,g)=>_+(g.Tsize??0),h.byteLength)})}}if(o==null||s==null)throw new Error("Noting persisted");return{cid:o,node:s}},ri=async(t,e,r,n)=>{let o=Zn(Qn)(P(e)),s=[],a=n.shardFanoutBits??Be;for(;;){let c=await v(r.get(t,n)),l=q(c),u=new nu.default,d=await o.take(a),h=ei(d,a);s.push({prefix:h,children:u,node:l});let p;for(let g of l.Links){let m=g.Name??"";if(m.length<h.length)throw new Error("Invalid HAMT - link name was too short");let w=parseInt(m.substring(0,h.length),16);u.set(w,!0),m.startsWith(h)&&(p=g)}if(p==null){vo("no link found with prefix %s for %s",h,e);break}let _=p.Name??"";if(_.length<h.length)throw new Error("Invalid HAMT - link name was too short");if(_.length===h.length){t=p.Hash,vo("descend into sub-shard with prefix %s",_);continue}break}return{path:s,hash:o}};var ni=class extends Error{static name="InvalidParametersError";constructor(e="Invalid parameters"){super(e),this.name="InvalidParametersError"}};async function ii(t,e,r){if(t.Data==null)throw new Error("DagPB node had no data");let n=r.shardSplitThresholdBytes??262144,i=r.shardSplitStrategy??"links-bytes";r.profile==="unixfs-v0-2015"&&(i="links-bytes");let o=k.unmarshal(t.Data);if(!o.isDirectory())throw new Error("Can only estimate the size of directories or shards");if(i!=="links-bytes"&&i!=="block-bytes")throw new ni(`Invalid shard split threshold "${i}"`);let s=0;return o.type==="directory"?s=i==="links-bytes"?_h(t):Eh(o,t):o.type==="hamt-sharded-directory"&&(s=i==="links-bytes"?await ou(t,0,n,e,r):await su(t,yr(o.mode,o.mtime),n,e,r)),s>n}function _h(t){let e=0;for(let r of t.Links)e+=Oe(r.Name??""),e+=r.Hash.byteLength;return e}function Eh(t,e){let r=yr(t.mode,t.mtime);for(let n of e.Links)r+=pr(Oe(n.Name??""),n.Hash.byteLength,Number(n.Tsize??0));return r}async function ou(t,e,r,n,i){if(e>r)return r;if(t.Data==null||!k.unmarshal(t.Data).isDirectory())return e;let s=(Math.pow(2,i.shardFanoutBits??Be)-1).toString(16).length;for(let a of t.Links){let c=a.Name??"";if(c=c.substring(s),e+=Oe(c),e+=a.Hash.bytes.byteLength,a.Hash.code===M){let l=await v(n.get(a.Hash,i)),u=q(l);e+=await ou(u,e,r,n,i)}}return e}async function su(t,e,r,n,i){if(e>r)return r;if(t.Data==null||!k.unmarshal(t.Data).isDirectory())return e;let s=(Math.pow(2,i.shardFanoutBits??Be)-1).toString(16).length;for(let a of t.Links){let c=a.Name??"";if(c=c.substring(s),e+=pr(Oe(c),a.Hash.byteLength,Number(a.Tsize??0)),a.Hash.code===M){let l=await v(n.get(a.Hash,i)),u=q(l);e+=await su(u,e,r,n,i)}}return e}var Ue=R("helia:unixfs:components:utils:add-link");async function Kt(t,e,r,n){if(t.node.Data==null)throw new V("Invalid parent passed to addLink");if(k.unmarshal(t.node.Data).type==="hamt-sharded-directory")return Ue("adding link to sharded directory"),vh(t,e,r,n);Ue(`adding ${e.Name} (${e.Hash}) to regular directory`);let o=await Sh(t,e,r,n);if(await ii(o.node,r,n)){Ue("converting directory to sharded directory");let s=await kh(o,r,n);o.cid=s.cid,o.node=q(await v(r.get(s.cid,n)))}return o}var kh=async(t,e,r)=>{if(t.node.Data==null)throw new V("Invalid parent passed to convertToShardedDirectory");let n=k.unmarshal(t.node.Data),i=await iu(e,t.node.Links.map(o=>({name:o.Name??"",size:BigInt(o.Tsize??0),cid:o.Hash})),{...r,mode:n.mode,mtime:n.mtime,cidVersion:t.cid.version});return Ue(`converted directory to sharded directory ${i.cid}`),i},Sh=async(t,e,r,n)=>{let i=t.node.Links.filter(u=>{let d=u.Name===e.Name;if(d&&!n.allowOverwriting)throw new wr;return!d});if(i.push(e),t.node.Data==null)throw new ie("Parent node with no data passed to addToDirectory");let o=k.unmarshal(t.node.Data),s;if(o.mtime!=null){let u=Date.now(),d=Math.floor(u/1e3);o.mtime={secs:BigInt(d),nsecs:(u-d*1e3)*1e3},s=o.marshal()}else s=t.node.Data;t.node=z({Data:s,Links:i});let a=I(t.node),c=await fe.digest(a);n?.signal?.throwIfAborted();let l=S.create(t.cid.version,M,c);return await r.put(l,a,n),{node:t.node,cid:l}},vh=async(t,e,r,n)=>{let{path:i,hash:o}=await ri(t.cid,e.Name,r,n),s=i[i.length-1];if(s==null)throw new Error("Invalid HAMT, could not generate path");let a=s.prefix,c=parseInt(a,16);Ue("next prefix for %s is %s",e.Name,a);let l=`${a}${e.Name}`,u=s.node.Links.find(h=>(h.Name??"").startsWith(a)),d=a.length;if(u!=null)if(Ue("link %s was present in shard",l),u.Name===l){if(!n.allowOverwriting)throw new wr;Ue("overwriting %s in sub-shard",e.Name),s.node.Links=s.node.Links.filter(h=>h.Name!==l),s.node.Links.push({Name:l,Hash:e.Hash,Tsize:e.Tsize})}else{if(u.Name?.length===d)throw new Error("Existing link was sub-shard?!");{Ue("prefix %s already exists, creating new sub-shard",a);let h=s.node.Links.findIndex(E=>E.Name?.startsWith(a)),p=s.node.Links.splice(h,1)[0],_=(p.Name??"").substring(d),m=Zn(Qn)(P(_)),w=n.shardFanoutBits??Be;for(let E=0;E<i.length;E++)await m.take(w),n?.signal?.throwIfAborted();for(;;){let E=await m.take(w);n?.signal?.throwIfAborted();let B=ei(E,w);p.Name=`${B}${_}`;let N=await o.take(w);n?.signal?.throwIfAborted();let C=ei(N,w);if(B===C){let O=new Io.default;O.set(N,!0),i.push({prefix:C,children:O,node:{Links:[]}});continue}let Z=new Io.default;Z.set(N,!0),Z.set(E,!0),i.push({prefix:a,children:Z,node:{Links:[p,{Name:`${C}${e.Name}`,Hash:e.Hash,Tsize:e.Tsize}]}});break}}}else Ue("link %s was not present in sub-shard",l),e.Name=l,s.node.Links.push(e),s.children.set(c,!0),Ue("adding %s to existing sub-shard",l);return ti(i,r,n)};async function ct(t,e,r={}){let n=await U(t,e,r);if(n.type!=="directory")throw new Lt(`${t.toString()} was not a UnixFS directory`);return{cid:t,node:n.node}}async function Yt(t,e,r,n){let i=await U(t,r,n);if(i.type!=="directory"&&i.type!=="file"&&i.type!=="raw")throw new Ne(`${t.toString()} was not a UnixFS node`);return{Name:e,Tsize:i.node instanceof Uint8Array?i.node.byteLength:Ih(i.node),Hash:t}}function Ih(t){let e=t.Links.reduce((r,n)=>r+(n.Tsize??0),0);return I(t).byteLength+e}var Bh=R("helia:unixfs:components:utils:resolve");async function Re(t,e,r,n){if(e==null||e==="")return{cid:t};let i=`/ipfs/${t}${e==null?"":`/${e}`}`,o=await Xs(On(i,r,n));if(o.length===0)throw new cn("Could not find path in directory");return Bh("resolved %s to %c",e,t),{cid:o[o.length-1].cid,path:e,segments:o}}async function Jt(t,e,r,n){if(e.segments==null||e.segments.length===0)return t;let i=e.segments.pop();if(i==null)throw new Error("Insufficient segments");i.cid=t,e.segments.reverse();for(let o of e.segments){let[s,a]=await Promise.all([ct(o.cid,r,n),Yt(i.cid,i.name,r,n)]);t=(await Kt(s,a,r,{...n,allowOverwriting:!0,cidVersion:t.version})).cid,o.cid=t,i=o}return t}async function*au(t,e,r={}){let n=await Re(t,r.path,e,r),i=await U(n.cid,e,r);if(i.type!=="file"&&i.type!=="raw")throw new ln;if(i.content==null)throw new un;yield*i.content(r)}var Ah=R("helia:unixfs:chmod");async function cu(t,e,r,n={}){let i=await Re(t,n.path,r,n);if(Ah("chmod %c %d",i.cid,e),n.recursive===!0){let d=await Wt(async function*(){for await(let h of Kn(i.cid,r,n)){let p,_=[],g=await U(h.cid,r,n);if(g.type==="raw")p=new k({type:"file",data:g.node});else if(g.type==="file"||g.type==="directory")p=g.unixfs,_=g.node.Links;else throw new Ne;p.mode=e;let m={Data:p.marshal(),Links:_};yield{path:h.path,content:m}}},h=>yt(h,r,{...n,dagBuilder:async function*(p,_){for await(let g of p)yield async function(){let m=g.content,w=I(m),E=await qe(w,_,{...n,cidVersion:t.version});if(m.Data==null)throw new ie(`${E} had no data`);let B=k.unmarshal(m.Data);return{cid:E,size:BigInt(w.length),path:g.path,unixfs:B}}}}),async h=>Se(h));if(d==null)throw new Fe(`Could not chmod ${i.cid.toString()}`);return Jt(d.cid,i,r,n)}let o=await v(r.get(i.cid,n)),s,a=[];if(i.cid.code===J)s=new k({type:"file",data:o});else{let d=q(o);if(d.Data==null)throw new ie(`${i.cid.toString()} had no data`);a=d.Links,s=k.unmarshal(d.Data)}s.mode=e;let c=I({Data:s.marshal(),Links:a}),l=await fe.digest(c),u=S.create(i.cid.version,M,l);return await r.put(u,c),Jt(u,i,r,n)}var Th=R("helia:unixfs:cp");async function uu(t,e,r,n,i={}){if(r.includes("/"))throw new V("Name must not have slashes");let[o,s]=await Promise.all([ct(e,n,i),Yt(t,r,n,i)]);return Th('Adding %c as "%s" to %c',t,r,e),(await Kt(o,s,n,{allowOverwriting:i.force,cidVersion:e.version,...i})).cid}async function*lu(t,e,r={}){let n=await Re(t,r.path,e,r),i=await U(n.cid,e,r);if(i.type==="directory"){yield*i.entries(r);return}yield i}var fu=R("helia:unixfs:mkdir");async function du(t,e,r,n={}){if(e.includes("/"))throw new V("Path must not have slashes");if((await U(t,r,n)).type!=="directory")throw new Lt(`${t.toString()} was not a UnixFS directory`);fu("creating %s",e);let s={Data:new k({type:"directory",mode:n.mode,mtime:n.mtime}).marshal(),Links:[]},a=I(s),c=await fe.digest(a),l=S.create(n.cidVersion??1,M,c);await r.put(l,a);let[u,d]=await Promise.all([ct(t,r,n),Yt(l,e,r,n)]);return fu("adding empty dir called %s to %c",e,t),(await Kt(u,d,r,{...n,allowOverwriting:n.force})).cid}var oi=R("helia:unixfs:utils:remove-link");async function hu(t,e,r,n){if(t.node.Data==null)throw new ie("Parent node had no data");if(k.unmarshal(t.node.Data).type==="hamt-sharded-directory"){oi(`removing ${e} from sharded directory`);let o=await Dh(t,e,r,n);return await ii(o.node,r,n)?o:(oi("converting shard to flat directory %c",t.cid),Lh(o,r,n))}return oi(`removing link ${e} regular directory`),Ch(t,e,r,n)}var Ch=async(t,e,r,n)=>{t.node.Links=t.node.Links.filter(s=>s.Name!==e);let i=I(t.node),o=await qe(i,r,{...n,cidVersion:t.cid.version});return oi(`Updated regular directory ${o}`),{node:t.node,cid:o}},Dh=async(t,e,r,n)=>{let{path:i}=await ri(t.cid,e,r,n),o=i[i.length-1];if(o==null)throw new Error("Invalid HAMT, could not generate path");let s=(Math.pow(2,n.shardFanoutBits??Be)-1).toString(16).length,a=o.node.Links.filter(u=>(u.Name??"").substring(s)===e).map(u=>u.Name).pop();if(a==null)throw new Error("File not found");let c=a.substring(0,s),l=parseInt(c,16);if(o.node.Links=o.node.Links.filter(u=>u.Name!==a),o.children.unset(l),o.node.Links.length===1)for(;i.length!==1;){let u=i[i.length-1];if(u==null||u.node.Links.length>1)break;i.pop();let d=i[i.length-1];if(d==null)break;let h=u.node.Links[0];d.node.Links=d.node.Links.filter(p=>!(p.Name??"").startsWith(d.prefix)),d.node.Links.push({Hash:h.Hash,Name:`${d.prefix}${(h.Name??"").substring(s)}`,Tsize:h.Tsize})}return ti(i,r,n)},Lh=async(t,e,r)=>{if(t.node.Data==null)throw new V("Invalid parent passed to convertToFlatDirectory");let n={Links:[]},i=await U(t.cid,e,r);if(i.type!=="directory")throw new Error("Unexpected node type");for await(let c of i.entries()){let l=0,u=await U(c.cid,e,r);u.node instanceof Uint8Array?l=u.node.byteLength:l=I(u.node).length,n.Links.push({Hash:c.cid,Name:c.name,Tsize:l})}let o=k.unmarshal(t.node.Data);n.Data=new k({type:"directory",mode:o.mode,mtime:o.mtime}).marshal();let s=I(z(n));return{cid:await qe(s,e,{codec:$,cidVersion:t.cid.version,signal:r.signal}),node:n}};var Nh=R("helia:unixfs:rm");async function mu(t,e,r,n={}){if(e.includes("/"))throw new V("Name must not have slashes");let i=await ct(t,r,n);return Nh("Removing %s from %c",e,t),(await hu(i,e,r,{...n,cidVersion:t.version})).cid}var Bo={32:16777619n,64:1099511628211n,128:309485009821345068724781371n,256:374144419156711147060143317175368453031918731002211n,512:35835915874844867368919076489095108449946327955754392558399825615420669938882575126094039892345713852759n,1024:5016456510113118655434598811035278955030765345404790744303017523831112055108147451509157692220295382716162651878526895249385292291816524375083746691371804094271873160484737966720260389217684476157468082573n},pu={32:2166136261n,64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n},yu=new globalThis.TextEncoder;function Fh(t,e){let r=Bo[e],n=pu[e];for(let i=0;i<t.length;i++)n^=BigInt(t[i]),n=BigInt.asUintN(e,n*r);return n}function zh(t,e,r){if(r.length===0)throw new Error("The `utf8Buffer` option must have a length greater than zero");let n=Bo[e],i=pu[e],o=t;for(;o.length>0;){let s=yu.encodeInto(o,r);o=o.slice(s.read);for(let a=0;a<s.written;a++)i^=BigInt(r[a]),i=BigInt.asUintN(e,i*n)}return i}function Ao(t,{size:e=32,utf8Buffer:r}={}){if(!Bo[e])throw new Error("The `size` option must be one of 32, 64, 128, 256, 512, or 1024");if(typeof t=="string"){if(r)return zh(t,e,r);t=yu.encode(t)}return Fh(t,e)}var Cr={hash:t=>Number(Ao(t,{size:32})),hashV:(t,e)=>Ph(Cr.hash(t,e))};function Ph(t){let e=t.toString(16);return e.length%2===1&&(e=`0${e}`),P(e,"base16")}var gu=64,Ae=class{fp;h;seed;constructor(e,r,n,i=2){if(i>gu)throw new TypeError("Invalid Fingerprint Size");let o=r.hashV(e,n),s=_e(i);for(let a=0;a<s.length;a++)s[a]=o[a];s.length===0&&(s[0]=7),this.fp=s,this.h=r,this.seed=n}hash(){return this.h.hash(this.fp,this.seed)}equals(e){return e?.fp instanceof Uint8Array?Rr(this.fp,e.fp):!1}};function xt(t,e){return Math.floor(Math.random()*(e-t))+t}var _t=class{contents;constructor(e){this.contents=new Array(e).fill(null)}has(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");return this.contents.some(r=>e.equals(r))}add(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");for(let r=0;r<this.contents.length;r++)if(this.contents[r]==null)return this.contents[r]=e,!0;return!0}swap(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");let r=xt(0,this.contents.length-1),n=this.contents[r];return this.contents[r]=e,n}remove(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");let r=this.contents.findIndex(n=>e.equals(n));return r>-1?(this.contents[r]=null,!0):!1}};var Uh=500,Dr=class{bucketSize;filterSize;fingerprintSize;buckets;count;hash;seed;constructor(e){this.filterSize=e.filterSize,this.bucketSize=e.bucketSize??4,this.fingerprintSize=e.fingerprintSize??2,this.count=0,this.buckets=[],this.hash=e.hash??Cr,this.seed=e.seed??xt(0,Math.pow(2,10))}add(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=(n^r.hash())%this.filterSize;if(this.buckets[n]==null&&(this.buckets[n]=new _t(this.bucketSize)),this.buckets[i]==null&&(this.buckets[i]=new _t(this.bucketSize)),this.buckets[n].add(r)||this.buckets[i].add(r))return this.count++,!0;let o=[n,i],s=o[xt(0,o.length-1)];this.buckets[s]==null&&(this.buckets[s]=new _t(this.bucketSize));for(let a=0;a<Uh;a++){let c=this.buckets[s].swap(r);if(c!=null&&(s=(s^c.hash())%this.filterSize,this.buckets[s]==null&&(this.buckets[s]=new _t(this.bucketSize)),this.buckets[s].add(c)))return this.count++,!0}return!1}has(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=this.buckets[n]?.has(r)??!1;if(i)return i;let o=(n^r.hash())%this.filterSize;return this.buckets[o]?.has(r)??!1}remove(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=this.buckets[n]?.remove(r)??!1;if(i)return this.count--,i;let o=(n^r.hash())%this.filterSize,s=this.buckets[o]?.remove(r)??!1;return s&&this.count--,s}get reliable(){return Math.floor(100*(this.count/this.filterSize))<=90}};var Lr=class{filterSize;bucketSize;fingerprintSize;scale;filterSeries;hash;seed;constructor(e){this.bucketSize=e.bucketSize??4,this.filterSize=e.filterSize??(1<<18)/this.bucketSize,this.fingerprintSize=e.fingerprintSize??2,this.scale=e.scale??2,this.hash=e.hash??Cr,this.seed=e.seed??xt(0,Math.pow(2,10)),this.filterSeries=[new Dr({filterSize:this.filterSize,bucketSize:this.bucketSize,fingerprintSize:this.fingerprintSize,hash:this.hash,seed:this.seed})]}add(e){if(typeof e=="string"&&(e=P(e)),this.has(e))return!0;let r=this.filterSeries.find(n=>n.reliable);if(r==null){let n=this.filterSize*Math.pow(this.scale,this.filterSeries.length);r=new Dr({filterSize:n,bucketSize:this.bucketSize,fingerprintSize:this.fingerprintSize,hash:this.hash,seed:this.seed}),this.filterSeries.push(r)}return r.add(e)}has(e){typeof e=="string"&&(e=P(e));for(let r=0;r<this.filterSeries.length;r++)if(this.filterSeries[r].has(e))return!0;return!1}remove(e){typeof e=="string"&&(e=P(e));for(let r=0;r<this.filterSeries.length;r++)if(this.filterSeries[r].remove(e))return!0;return!1}get count(){return this.filterSeries.reduce((e,r)=>e+r.count,0)}};var wu=1877,si=1604,Rh=R("helia:unixfs:stat");async function bu(t,e,r={}){let n=await Re(t,r.path,e,r);Rh("stat %c",n.cid);let i=await U(n.cid,e,r);if(i.type==="raw")return r.extended===!0?Hh(i):Oh(i);if(i.type==="file"||i.type==="directory")return r.extended===!0?$h(i,e,r.filter??new Lr({filterSize:1024}),r):Mh(i);throw new Ne}function Mh(t){return{type:t.type,cid:t.cid,unixfs:t.unixfs,mode:t.unixfs.mode??(t.unixfs.isDirectory()?wu:si),mtime:t.unixfs.mtime,size:t.unixfs.fileSize()}}async function $h(t,e,r,n){let i=await xu(t.cid,e,!1,r,n);return{type:t.type,cid:t.cid,unixfs:t.unixfs,size:t.unixfs.isDirectory()?i.dirSize:t.unixfs.fileSize(),mode:t.unixfs.mode??(t.unixfs.isDirectory()?wu:si),mtime:t.unixfs.mtime,localSize:i.localSize,dagSize:i.dagSize,deduplicatedDagSize:i.deduplicatedDagSize,blocks:i.blocks,uniqueBlocks:i.uniqueBlocks}}function Oh(t){return{type:t.type,cid:t.cid,unixfs:void 0,mode:si,mtime:void 0,size:BigInt(t.node.byteLength)}}function Hh(t){return{type:t.type,cid:t.cid,unixfs:void 0,mode:si,mtime:void 0,size:BigInt(t.node.byteLength),localSize:BigInt(t.node.byteLength),dagSize:BigInt(t.node.byteLength),deduplicatedDagSize:BigInt(t.node.byteLength),blocks:1n,uniqueBlocks:1n}}async function xu(t,e,r,n,i){let o={dirSize:0n,localSize:0n,dagSize:0n,deduplicatedDagSize:0n,blocks:0n,uniqueBlocks:0n};try{let s=n.has(t.bytes);n.add(t.bytes);let a=await v(e.get(t,i));if(o.blocks++,o.dagSize+=BigInt(a.byteLength),s||(o.uniqueBlocks++,o.deduplicatedDagSize+=BigInt(a.byteLength)),t.code===J)o.localSize+=BigInt(a.byteLength),r&&(o.dirSize+=BigInt(a.byteLength));else if(t.code===M){let c=q(a),l;if(c.Data!=null&&(l=k.unmarshal(c.Data)),c.Links.length>0){for(let u of c.Links){let d=await xu(u.Hash,e,Vh(u,l),n,i);o.localSize+=d.localSize,o.dagSize+=d.dagSize,o.deduplicatedDagSize+=d.deduplicatedDagSize,o.blocks+=d.blocks,o.uniqueBlocks+=d.uniqueBlocks,o.dirSize+=d.dirSize}r&&l!=null&&(o.dirSize+=l.fileSize())}else{if(l==null)throw new ie(`PBNode ${t.toString()} had no data`);l.data!=null&&(o.localSize+=BigInt(l.data.byteLength??0)),r&&(o.dirSize+=l.fileSize())}}else throw new Fe(`${t.toString()} was neither DAG_PB nor RAW`)}catch(s){if(s.name!=="NotFoundError"&&s.name!=="BlockNotFoundWhileOfflineError"||i.offline!==!0)throw s}return o}function Vh(t,e){if(e==null)return!1;let r=t.Name;return r==null?!1:e.type==="directory"?!0:e.type==="hamt-sharded-directory"&&r.length>2}var jh=R("helia:unixfs:touch");async function _u(t,e,r={}){let n=await Re(t,r.path,e,r),i=r.mtime??{secs:BigInt(Math.round(Date.now()/1e3)),nsecs:0};if(jh("touch %c %o",n.cid,i),r.recursive){let d=await Wt(async function*(){for await(let h of Kn(n.cid,e)){let p,_,g=await U(h.cid,e,r);if(g.type==="raw")p=new k({data:g.node}),_=[];else if(g.type==="file"||g.type==="directory")p=g.unixfs,_=g.node.Links;else throw new Ne;p.mtime=i;let m={Data:p.marshal(),Links:_};yield{path:h.path,content:m}}},h=>yt(h,e,{...r,dagBuilder:async function*(p,_){for await(let g of p)yield async function(){let m=g.content,w=I(m),E=await qe(w,_,{...r,cidVersion:t.version});if(m.Data==null)throw new ie(`${E} had no data`);let B=k.unmarshal(m.Data);return{cid:E,size:BigInt(w.length),path:g.path,unixfs:B}}}}),async h=>Se(h));if(d==null)throw new Fe(`Could not chmod ${n.cid.toString()}`);return Jt(d.cid,n,e,r)}let o=await v(e.get(n.cid,r)),s,a=[];if(n.cid.code===J)s=new k({data:o});else{let d=q(o);if(a=d.Links,d.Data==null)throw new ie(`${n.cid.toString()} had no data`);s=k.unmarshal(d.Data)}s.mtime=i;let c=I({Data:s.marshal(),Links:a}),l=await fe.digest(c),u=S.create(n.cid.version,M,l);return await e.put(u,c),Jt(u,n,e,r)}var ai=class{components;constructor(e){this.components=e}async*addAll(e,r={}){yield*fn(e,this.components.blockstore,r)}async addBytes(e,r={}){return da(e,this.components.blockstore,r)}async addByteStream(e,r={}){return ha(e,this.components.blockstore,r)}async addFile(e,r={}){return ma(e,this.components.blockstore,r)}async addDirectory(e={},r={}){return pa(e,this.components.blockstore,r)}async*cat(e,r={}){yield*au(e,this.components.blockstore,r)}async chmod(e,r,n={}){return cu(e,r,this.components.blockstore,n)}async cp(e,r,n,i={}){return uu(e,r,n,this.components.blockstore,i)}async*ls(e,r={}){yield*lu(e,this.components.blockstore,r)}async mkdir(e,r,n={}){return du(e,r,this.components.blockstore,n)}async rm(e,r,n={}){return mu(e,r,this.components.blockstore,n)}async stat(e,r={}){return bu(e,this.components.blockstore,r)}async touch(e,r={}){return _u(e,this.components.blockstore,r)}};async function*Eu(){throw new Error("Not supported in browsers")}function ku(t,e){return t=new URL(t),{path:decodeURIComponent(new URL(t).pathname.split("/").pop()??""),content:qh(t,e)}}async function*qh(t,e){let r=await globalThis.fetch(t,e);if(r.body==null)throw new Fe("HTTP response did not have a body");let n=r.body.getReader();try{for(;;){let{done:i,value:o}=await n.read();if(i)return;o!=null&&(yield o)}}finally{n.releaseLock()}}function Wh(t){return new ai(t)}return Tu(Gh);})();
12
+ ${e}[Error list was empty]`,r.trim()}return Qc(t,e)}ae.formatters.e=t=>t==null?"undefined":tu(t);function ph(t){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=t,e.destroy=()=>!0,e.extend=()=>e,e.useColors=()=>!1,e}function R(t,e){let r=ph(`${t}:trace`);return ae.enabled(`${t}:trace`)&&ae.names.map(n=>n.toString()).find(n=>n.includes(":trace"))!=null&&(r=ae(`${t}:trace`,e)),Object.assign(ae(t,e),{error:ae(`${t}:error`,e),trace:r,newScope:n=>R(`${t}:${n}`,e)})}function eu(t){if(t!=null&&(t=t.trim(),t.length!==0))return t}var Io=zr(nn(),1);function Zn(t){function e(r){return r instanceof Xn?r:new Xn(r,t)}return e}var Xn=class{_value;_hashFn;_depth;_availableBits;_currentBufferIndex;_buffers;constructor(e,r){if(!(e instanceof Uint8Array))throw new Error("can only hash Uint8Arrays");this._value=e,this._hashFn=r,this._depth=-1,this._availableBits=0,this._currentBufferIndex=0,this._buffers=[]}async take(e){let r=e;for(;this._availableBits<r;)await this._produceMoreBits();let n=0;for(;r>0;){let i=this._buffers[this._currentBufferIndex],o=Math.min(i.availableBits(),r),s=i.take(o);n=(n<<o)+s,r-=o,this._availableBits-=o,i.availableBits()===0&&this._currentBufferIndex++}return n}untake(e){let r=e;for(;r>0;){let n=this._buffers[this._currentBufferIndex],i=Math.min(n.totalBits()-n.availableBits(),r);n.untake(i),r-=i,this._availableBits+=i,this._currentBufferIndex>0&&n.totalBits()===n.availableBits()&&(this._depth--,this._currentBufferIndex--)}}async _produceMoreBits(){this._depth++;let e=this._depth>0?Te([this._value,Uint8Array.from([this._depth])]):this._value,r=await this._hashFn(e),n=new So(r);this._buffers.push(n),this._availableBits+=n.availableBits()}},yh=[255,254,252,248,240,224,192,128],gh=[1,3,7,15,31,63,127,255],So=class{_value;_currentBytePos;_currentBitPos;constructor(e){this._value=e,this._currentBytePos=e.length-1,this._currentBitPos=7}availableBits(){return this._currentBitPos+1+this._currentBytePos*8}totalBits(){return this._value.length*8}take(e){let r=e,n=0;for(;r>0&&this._haveBits();){let i=this._value[this._currentBytePos],o=this._currentBitPos+1,s=Math.min(o,r),a=wh(i,o-s,s);n=(n<<s)+a,r-=s,this._currentBitPos-=s,this._currentBitPos<0&&(this._currentBitPos=7,this._currentBytePos--)}return n}untake(e){for(this._currentBitPos+=e;this._currentBitPos>7;)this._currentBitPos-=8,this._currentBytePos+=1}_haveBits(){return this._currentBytePos>=0}};function wh(t,e,r){let n=bh(e,r);return(t&n)>>>e}function bh(t,e){return yh[t]&gh[Math.min(e+t-1,7)]}var ru=BigInt(pt.code),Be=8;async function Qn(t){return(await pt.encode(t)).subarray(0,8).reverse()}var nu=zr(nn(),1);var qe=async(t,e,r)=>{r.codec==null&&(r.codec=$);let n=await fe.digest(t),i=S.create(r.cidVersion??1,r.codec.code,n);return await e.put(i,t,{...r,signal:r.signal}),i};var vo=R("helia:unixfs:commands:utils:hamt-utils"),ei=(t,e=Be)=>{let r=(Math.pow(2,e)-1).toString(16).length;return t.toString(16).toUpperCase().padStart(r,"0").substring(0,r)},iu=async(t,e,r)=>{let n=new gr({root:!0,dir:!0,parent:void 0,parentKey:void 0,path:"",dirty:!0,flat:!1,mtime:r.mtime,mode:r.mode},r);for(let o=0;o<e.length;o++)await n.bucket.put(e[o].name,{size:e[o].size,cid:e[o].cid});let i=await Se(n.flush(t));if(i==null)throw new Error("Flushing shard yielded no result");return i},ti=async(t,e,r)=>{let n=k.unmarshal(t[0].node.Data??new Uint8Array(0)),i=BigInt(Math.pow(2,r.shardFanoutBits??Be));t.reverse();let o,s;for(let a=0;a<t.length;a++){let c=a===t.length-1,l=t[a],u=Uint8Array.from(l.children.bitField().reverse()),d=new k({type:"hamt-sharded-directory",data:u,fanout:i,hashType:ru});c&&(d.mtime=n.mtime,d.mode=n.mode),s={Data:d.marshal(),Links:l.node.Links};let h=I(z(s));if(o=await qe(h,e,r),!c){let p=t[a+1];if(p==null)throw new Error("Was not operating on shard root but also had no parent?");vo("updating link in parent sub-shard with prefix %s",p.prefix),p.node.Links=p.node.Links.filter(_=>_.Name!==p.prefix),p.node.Links.push({Name:p.prefix,Hash:o,Tsize:l.node.Links.reduce((_,g)=>_+(g.Tsize??0),h.byteLength)})}}if(o==null||s==null)throw new Error("Noting persisted");return{cid:o,node:s}},ri=async(t,e,r,n)=>{let o=Zn(Qn)(P(e)),s=[],a=n.shardFanoutBits??Be;for(;;){let c=await v(r.get(t,n)),l=q(c),u=new nu.default,d=await o.take(a),h=ei(d,a);s.push({prefix:h,children:u,node:l});let p;for(let g of l.Links){let m=g.Name??"";if(m.length<h.length)throw new Error("Invalid HAMT - link name was too short");let w=parseInt(m.substring(0,h.length),16);u.set(w,!0),m.startsWith(h)&&(p=g)}if(p==null){vo("no link found with prefix %s for %s",h,e);break}let _=p.Name??"";if(_.length<h.length)throw new Error("Invalid HAMT - link name was too short");if(_.length===h.length){t=p.Hash,vo("descend into sub-shard with prefix %s",_);continue}break}return{path:s,hash:o}};var ni=class extends Error{static name="InvalidParametersError";constructor(e="Invalid parameters"){super(e),this.name="InvalidParametersError"}};async function ii(t,e,r){if(t.Data==null)throw new Error("DagPB node had no data");let n=r.shardSplitThresholdBytes??262144,i=r.shardSplitStrategy??"links-bytes";r.profile==="unixfs-v0-2015"&&(i="links-bytes");let o=k.unmarshal(t.Data);if(!o.isDirectory())throw new Error("Can only estimate the size of directories or shards");if(i!=="links-bytes"&&i!=="block-bytes")throw new ni(`Invalid shard split threshold "${i}"`);let s=0;return o.type==="directory"?s=i==="links-bytes"?_h(t):Eh(o,t):o.type==="hamt-sharded-directory"&&(s=i==="links-bytes"?await ou(t,0,n,e,r):await su(t,yr(o.mode,o.mtime),n,e,r)),s>n}function _h(t){let e=0;for(let r of t.Links)e+=Oe(r.Name??""),e+=r.Hash.byteLength;return e}function Eh(t,e){let r=yr(t.mode,t.mtime);for(let n of e.Links)r+=pr(Oe(n.Name??""),n.Hash.byteLength,Number(n.Tsize??0));return r}async function ou(t,e,r,n,i){if(e>r)return r;if(t.Data==null||!k.unmarshal(t.Data).isDirectory())return e;let s=(Math.pow(2,i.shardFanoutBits??Be)-1).toString(16).length;for(let a of t.Links){let c=a.Name??"";if(c=c.substring(s),e+=Oe(c),e+=a.Hash.bytes.byteLength,a.Hash.code===M){let l=await v(n.get(a.Hash,i)),u=q(l);e+=await ou(u,e,r,n,i)}}return e}async function su(t,e,r,n,i){if(e>r)return r;if(t.Data==null||!k.unmarshal(t.Data).isDirectory())return e;let s=(Math.pow(2,i.shardFanoutBits??Be)-1).toString(16).length;for(let a of t.Links){let c=a.Name??"";if(c=c.substring(s),e+=pr(Oe(c),a.Hash.byteLength,Number(a.Tsize??0)),a.Hash.code===M){let l=await v(n.get(a.Hash,i)),u=q(l);e+=await su(u,e,r,n,i)}}return e}var Ue=R("helia:unixfs:components:utils:add-link");async function Kt(t,e,r,n){if(t.node.Data==null)throw new V("Invalid parent passed to addLink");if(k.unmarshal(t.node.Data).type==="hamt-sharded-directory")return Ue("adding link to sharded directory"),vh(t,e,r,n);Ue(`adding ${e.Name} (${e.Hash}) to regular directory`);let o=await Sh(t,e,r,n);if(await ii(o.node,r,n)){Ue("converting directory to sharded directory");let s=await kh(o,r,n);o.cid=s.cid,o.node=q(await v(r.get(s.cid,n)))}return o}var kh=async(t,e,r)=>{if(t.node.Data==null)throw new V("Invalid parent passed to convertToShardedDirectory");let n=k.unmarshal(t.node.Data),i=await iu(e,t.node.Links.map(o=>({name:o.Name??"",size:BigInt(o.Tsize??0),cid:o.Hash})),{...r,mode:n.mode,mtime:n.mtime,cidVersion:t.cid.version});return Ue(`converted directory to sharded directory ${i.cid}`),i},Sh=async(t,e,r,n)=>{let i=t.node.Links.filter(u=>{let d=u.Name===e.Name;if(d&&!n.allowOverwriting)throw new wr;return!d});if(i.push(e),t.node.Data==null)throw new ie("Parent node with no data passed to addToDirectory");let o=k.unmarshal(t.node.Data),s;if(o.mtime!=null){let u=Date.now(),d=Math.floor(u/1e3);o.mtime={secs:BigInt(d),nsecs:(u-d*1e3)*1e3},s=o.marshal()}else s=t.node.Data;t.node=z({Data:s,Links:i});let a=I(t.node),c=await fe.digest(a);n?.signal?.throwIfAborted();let l=S.create(t.cid.version,M,c);return await r.put(l,a,n),{node:t.node,cid:l}},vh=async(t,e,r,n)=>{let{path:i,hash:o}=await ri(t.cid,e.Name,r,n),s=i[i.length-1];if(s==null)throw new Error("Invalid HAMT, could not generate path");let a=s.prefix,c=parseInt(a,16);Ue("next prefix for %s is %s",e.Name,a);let l=`${a}${e.Name}`,u=s.node.Links.find(h=>(h.Name??"").startsWith(a)),d=a.length;if(u!=null)if(Ue("link %s was present in shard",l),u.Name===l){if(!n.allowOverwriting)throw new wr;Ue("overwriting %s in sub-shard",e.Name),s.node.Links=s.node.Links.filter(h=>h.Name!==l),s.node.Links.push({Name:l,Hash:e.Hash,Tsize:e.Tsize})}else{if(u.Name?.length===d)throw new Error("Existing link was sub-shard?!");{Ue("prefix %s already exists, creating new sub-shard",a);let h=s.node.Links.findIndex(E=>E.Name?.startsWith(a)),p=s.node.Links.splice(h,1)[0],_=(p.Name??"").substring(d),m=Zn(Qn)(P(_)),w=n.shardFanoutBits??Be;for(let E=0;E<i.length;E++)await m.take(w),n?.signal?.throwIfAborted();for(;;){let E=await m.take(w);n?.signal?.throwIfAborted();let B=ei(E,w);p.Name=`${B}${_}`;let N=await o.take(w);n?.signal?.throwIfAborted();let C=ei(N,w);if(B===C){let O=new Io.default;O.set(N,!0),i.push({prefix:C,children:O,node:{Links:[]}});continue}let Z=new Io.default;Z.set(N,!0),Z.set(E,!0),i.push({prefix:a,children:Z,node:{Links:[p,{Name:`${C}${e.Name}`,Hash:e.Hash,Tsize:e.Tsize}]}});break}}}else Ue("link %s was not present in sub-shard",l),e.Name=l,s.node.Links.push(e),s.children.set(c,!0),Ue("adding %s to existing sub-shard",l);return ti(i,r,n)};async function ct(t,e,r={}){let n=await U(t,e,r);if(n.type!=="directory")throw new Lt(`${t.toString()} was not a UnixFS directory`);return{cid:t,node:n.node}}async function Yt(t,e,r,n){let i=await U(t,r,n);if(i.type!=="directory"&&i.type!=="file"&&i.type!=="raw")throw new Ne(`${t.toString()} was not a UnixFS node`);return{Name:e,Tsize:i.node instanceof Uint8Array?i.node.byteLength:Ih(i.node),Hash:t}}function Ih(t){let e=t.Links.reduce((r,n)=>r+(n.Tsize??0),0);return I(t).byteLength+e}var Bh=R("helia:unixfs:components:utils:resolve");async function Re(t,e,r,n){if(e==null||e==="")return{cid:t};let i=`/ipfs/${t}${e==null?"":`/${e}`}`,o=await Xs(On(i,r,n));if(o.length===0)throw new cn("Could not find path in directory");return Bh("resolved %s to %c",e,t),{cid:o[o.length-1].cid,path:e,segments:o}}async function Jt(t,e,r,n){if(e.segments==null||e.segments.length===0)return t;let i=e.segments.pop();if(i==null)throw new Error("Insufficient segments");i.cid=t,e.segments.reverse();for(let o of e.segments){let[s,a]=await Promise.all([ct(o.cid,r,n),Yt(i.cid,i.name,r,n)]);t=(await Kt(s,a,r,{...n,allowOverwriting:!0,cidVersion:t.version})).cid,o.cid=t,i=o}return t}async function*au(t,e,r={}){let n=await Re(t,r.path,e,r),i=await U(n.cid,e,r);if(i.type!=="file"&&i.type!=="raw")throw new ln;if(i.content==null)throw new un;yield*i.content(r)}var Ah=R("helia:unixfs:chmod");async function cu(t,e,r,n={}){let i=await Re(t,n.path,r,n);if(Ah("chmod %c %d",i.cid,e),n.recursive===!0){let d=await Wt(async function*(){for await(let h of Kn(i.cid,r,n)){let p,_=[],g=await U(h.cid,r,n);if(g.type==="raw")p=new k({type:"file",data:g.node});else if(g.type==="file"||g.type==="directory")p=g.unixfs,_=g.node.Links;else throw new Ne;p.mode=e;let m={Data:p.marshal(),Links:_};yield{path:h.path,content:m}}},h=>yt(h,r,{...n,dagBuilder:async function*(p,_){for await(let g of p)yield async function(){let m=g.content,w=I(m),E=await qe(w,_,{...n,cidVersion:t.version});if(m.Data==null)throw new ie(`${E} had no data`);let B=k.unmarshal(m.Data);return{cid:E,size:BigInt(w.length),path:g.path,unixfs:B}}}}),async h=>Se(h));if(d==null)throw new Fe(`Could not chmod ${i.cid.toString()}`);return Jt(d.cid,i,r,n)}let o=await v(r.get(i.cid,n)),s,a=[];if(i.cid.code===J)s=new k({type:"file",data:o});else{let d=q(o);if(d.Data==null)throw new ie(`${i.cid.toString()} had no data`);a=d.Links,s=k.unmarshal(d.Data)}s.mode=e;let c=I({Data:s.marshal(),Links:a}),l=await fe.digest(c),u=S.create(i.cid.version,M,l);return await r.put(u,c),Jt(u,i,r,n)}var Th=R("helia:unixfs:cp");async function uu(t,e,r,n,i={}){if(r.includes("/"))throw new V("Name must not have slashes");let[o,s]=await Promise.all([ct(e,n,i),Yt(t,r,n,i)]);return Th('Adding %c as "%s" to %c',t,r,e),(await Kt(o,s,n,{allowOverwriting:i.force,cidVersion:e.version,...i})).cid}async function*lu(t,e,r={}){let n=await Re(t,r.path,e,r),i=await U(n.cid,e,r);if(i.type==="directory"){yield*i.entries(r);return}yield i}var fu=R("helia:unixfs:mkdir");async function du(t,e,r,n={}){if(e.includes("/"))throw new V("Path must not have slashes");if((await U(t,r,n)).type!=="directory")throw new Lt(`${t.toString()} was not a UnixFS directory`);fu("creating %s",e);let s={Data:new k({type:"directory",mode:n.mode,mtime:n.mtime}).marshal(),Links:[]},a=I(s),c=await fe.digest(a),l=S.create(n.cidVersion??1,M,c);await r.put(l,a);let[u,d]=await Promise.all([ct(t,r,n),Yt(l,e,r,n)]);return fu("adding empty dir called %s to %c",e,t),(await Kt(u,d,r,{...n,allowOverwriting:n.force})).cid}var oi=R("helia:unixfs:utils:remove-link");async function hu(t,e,r,n){if(t.node.Data==null)throw new ie("Parent node had no data");if(k.unmarshal(t.node.Data).type==="hamt-sharded-directory"){oi(`removing ${e} from sharded directory`);let o=await Dh(t,e,r,n);return await ii(o.node,r,n)?o:(oi("converting shard to flat directory %c",t.cid),Lh(o,r,n))}return oi(`removing link ${e} regular directory`),Ch(t,e,r,n)}var Ch=async(t,e,r,n)=>{t.node.Links=t.node.Links.filter(s=>s.Name!==e);let i=I(t.node),o=await qe(i,r,{...n,cidVersion:t.cid.version});return oi(`Updated regular directory ${o}`),{node:t.node,cid:o}},Dh=async(t,e,r,n)=>{let{path:i}=await ri(t.cid,e,r,n),o=i[i.length-1];if(o==null)throw new Error("Invalid HAMT, could not generate path");let s=(Math.pow(2,n.shardFanoutBits??Be)-1).toString(16).length,a=o.node.Links.filter(u=>(u.Name??"").substring(s)===e).map(u=>u.Name).pop();if(a==null)throw new Error("File not found");let c=a.substring(0,s),l=parseInt(c,16);if(o.node.Links=o.node.Links.filter(u=>u.Name!==a),o.children.unset(l),o.node.Links.length===1)for(;i.length!==1;){let u=i[i.length-1];if(u==null||u.node.Links.length>1||(u.node.Links[0].Name??"").length===s)break;i.pop();let d=i[i.length-1];if(d==null)break;let h=u.node.Links[0];d.node.Links=d.node.Links.filter(p=>!(p.Name??"").startsWith(d.prefix)),d.node.Links.push({Hash:h.Hash,Name:`${d.prefix}${(h.Name??"").substring(s)}`,Tsize:h.Tsize})}return ti(i,r,n)},Lh=async(t,e,r)=>{if(t.node.Data==null)throw new V("Invalid parent passed to convertToFlatDirectory");let n={Links:[]},i=await U(t.cid,e,r);if(i.type!=="directory")throw new Error("Unexpected node type");for await(let c of i.entries()){let l=0,u=await U(c.cid,e,r);u.node instanceof Uint8Array?l=u.node.byteLength:l=I(u.node).length,n.Links.push({Hash:c.cid,Name:c.name,Tsize:l})}let o=k.unmarshal(t.node.Data);n.Data=new k({type:"directory",mode:o.mode,mtime:o.mtime}).marshal();let s=I(z(n));return{cid:await qe(s,e,{codec:$,cidVersion:t.cid.version,signal:r.signal}),node:n}};var Nh=R("helia:unixfs:rm");async function mu(t,e,r,n={}){if(e.includes("/"))throw new V("Name must not have slashes");let i=await ct(t,r,n);return Nh("Removing %s from %c",e,t),(await hu(i,e,r,{...n,cidVersion:t.version})).cid}var Bo={32:16777619n,64:1099511628211n,128:309485009821345068724781371n,256:374144419156711147060143317175368453031918731002211n,512:35835915874844867368919076489095108449946327955754392558399825615420669938882575126094039892345713852759n,1024:5016456510113118655434598811035278955030765345404790744303017523831112055108147451509157692220295382716162651878526895249385292291816524375083746691371804094271873160484737966720260389217684476157468082573n},pu={32:2166136261n,64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n},yu=new globalThis.TextEncoder;function Fh(t,e){let r=Bo[e],n=pu[e];for(let i=0;i<t.length;i++)n^=BigInt(t[i]),n=BigInt.asUintN(e,n*r);return n}function zh(t,e,r){if(r.length===0)throw new Error("The `utf8Buffer` option must have a length greater than zero");let n=Bo[e],i=pu[e],o=t;for(;o.length>0;){let s=yu.encodeInto(o,r);o=o.slice(s.read);for(let a=0;a<s.written;a++)i^=BigInt(r[a]),i=BigInt.asUintN(e,i*n)}return i}function Ao(t,{size:e=32,utf8Buffer:r}={}){if(!Bo[e])throw new Error("The `size` option must be one of 32, 64, 128, 256, 512, or 1024");if(typeof t=="string"){if(r)return zh(t,e,r);t=yu.encode(t)}return Fh(t,e)}var Cr={hash:t=>Number(Ao(t,{size:32})),hashV:(t,e)=>Ph(Cr.hash(t,e))};function Ph(t){let e=t.toString(16);return e.length%2===1&&(e=`0${e}`),P(e,"base16")}var gu=64,Ae=class{fp;h;seed;constructor(e,r,n,i=2){if(i>gu)throw new TypeError("Invalid Fingerprint Size");let o=r.hashV(e,n),s=_e(i);for(let a=0;a<s.length;a++)s[a]=o[a];s.length===0&&(s[0]=7),this.fp=s,this.h=r,this.seed=n}hash(){return this.h.hash(this.fp,this.seed)}equals(e){return e?.fp instanceof Uint8Array?Rr(this.fp,e.fp):!1}};function xt(t,e){return Math.floor(Math.random()*(e-t))+t}var _t=class{contents;constructor(e){this.contents=new Array(e).fill(null)}has(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");return this.contents.some(r=>e.equals(r))}add(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");for(let r=0;r<this.contents.length;r++)if(this.contents[r]==null)return this.contents[r]=e,!0;return!0}swap(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");let r=xt(0,this.contents.length-1),n=this.contents[r];return this.contents[r]=e,n}remove(e){if(!(e instanceof Ae))throw new TypeError("Invalid Fingerprint");let r=this.contents.findIndex(n=>e.equals(n));return r>-1?(this.contents[r]=null,!0):!1}};var Uh=500,Dr=class{bucketSize;filterSize;fingerprintSize;buckets;count;hash;seed;constructor(e){this.filterSize=e.filterSize,this.bucketSize=e.bucketSize??4,this.fingerprintSize=e.fingerprintSize??2,this.count=0,this.buckets=[],this.hash=e.hash??Cr,this.seed=e.seed??xt(0,Math.pow(2,10))}add(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=(n^r.hash())%this.filterSize;if(this.buckets[n]==null&&(this.buckets[n]=new _t(this.bucketSize)),this.buckets[i]==null&&(this.buckets[i]=new _t(this.bucketSize)),this.buckets[n].add(r)||this.buckets[i].add(r))return this.count++,!0;let o=[n,i],s=o[xt(0,o.length-1)];this.buckets[s]==null&&(this.buckets[s]=new _t(this.bucketSize));for(let a=0;a<Uh;a++){let c=this.buckets[s].swap(r);if(c!=null&&(s=(s^c.hash())%this.filterSize,this.buckets[s]==null&&(this.buckets[s]=new _t(this.bucketSize)),this.buckets[s].add(c)))return this.count++,!0}return!1}has(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=this.buckets[n]?.has(r)??!1;if(i)return i;let o=(n^r.hash())%this.filterSize;return this.buckets[o]?.has(r)??!1}remove(e){typeof e=="string"&&(e=P(e));let r=new Ae(e,this.hash,this.seed,this.fingerprintSize),n=this.hash.hash(e,this.seed)%this.filterSize,i=this.buckets[n]?.remove(r)??!1;if(i)return this.count--,i;let o=(n^r.hash())%this.filterSize,s=this.buckets[o]?.remove(r)??!1;return s&&this.count--,s}get reliable(){return Math.floor(100*(this.count/this.filterSize))<=90}};var Lr=class{filterSize;bucketSize;fingerprintSize;scale;filterSeries;hash;seed;constructor(e){this.bucketSize=e.bucketSize??4,this.filterSize=e.filterSize??(1<<18)/this.bucketSize,this.fingerprintSize=e.fingerprintSize??2,this.scale=e.scale??2,this.hash=e.hash??Cr,this.seed=e.seed??xt(0,Math.pow(2,10)),this.filterSeries=[new Dr({filterSize:this.filterSize,bucketSize:this.bucketSize,fingerprintSize:this.fingerprintSize,hash:this.hash,seed:this.seed})]}add(e){if(typeof e=="string"&&(e=P(e)),this.has(e))return!0;let r=this.filterSeries.find(n=>n.reliable);if(r==null){let n=this.filterSize*Math.pow(this.scale,this.filterSeries.length);r=new Dr({filterSize:n,bucketSize:this.bucketSize,fingerprintSize:this.fingerprintSize,hash:this.hash,seed:this.seed}),this.filterSeries.push(r)}return r.add(e)}has(e){typeof e=="string"&&(e=P(e));for(let r=0;r<this.filterSeries.length;r++)if(this.filterSeries[r].has(e))return!0;return!1}remove(e){typeof e=="string"&&(e=P(e));for(let r=0;r<this.filterSeries.length;r++)if(this.filterSeries[r].remove(e))return!0;return!1}get count(){return this.filterSeries.reduce((e,r)=>e+r.count,0)}};var wu=1877,si=1604,Rh=R("helia:unixfs:stat");async function bu(t,e,r={}){let n=await Re(t,r.path,e,r);Rh("stat %c",n.cid);let i=await U(n.cid,e,r);if(i.type==="raw")return r.extended===!0?Hh(i):Oh(i);if(i.type==="file"||i.type==="directory")return r.extended===!0?$h(i,e,r.filter??new Lr({filterSize:1024}),r):Mh(i);throw new Ne}function Mh(t){return{type:t.type,cid:t.cid,unixfs:t.unixfs,mode:t.unixfs.mode??(t.unixfs.isDirectory()?wu:si),mtime:t.unixfs.mtime,size:t.unixfs.fileSize()}}async function $h(t,e,r,n){let i=await xu(t.cid,e,!1,r,n);return{type:t.type,cid:t.cid,unixfs:t.unixfs,size:t.unixfs.isDirectory()?i.dirSize:t.unixfs.fileSize(),mode:t.unixfs.mode??(t.unixfs.isDirectory()?wu:si),mtime:t.unixfs.mtime,localSize:i.localSize,dagSize:i.dagSize,deduplicatedDagSize:i.deduplicatedDagSize,blocks:i.blocks,uniqueBlocks:i.uniqueBlocks}}function Oh(t){return{type:t.type,cid:t.cid,unixfs:void 0,mode:si,mtime:void 0,size:BigInt(t.node.byteLength)}}function Hh(t){return{type:t.type,cid:t.cid,unixfs:void 0,mode:si,mtime:void 0,size:BigInt(t.node.byteLength),localSize:BigInt(t.node.byteLength),dagSize:BigInt(t.node.byteLength),deduplicatedDagSize:BigInt(t.node.byteLength),blocks:1n,uniqueBlocks:1n}}async function xu(t,e,r,n,i){let o={dirSize:0n,localSize:0n,dagSize:0n,deduplicatedDagSize:0n,blocks:0n,uniqueBlocks:0n};try{let s=n.has(t.bytes);n.add(t.bytes);let a=await v(e.get(t,i));if(o.blocks++,o.dagSize+=BigInt(a.byteLength),s||(o.uniqueBlocks++,o.deduplicatedDagSize+=BigInt(a.byteLength)),t.code===J)o.localSize+=BigInt(a.byteLength),r&&(o.dirSize+=BigInt(a.byteLength));else if(t.code===M){let c=q(a),l;if(c.Data!=null&&(l=k.unmarshal(c.Data)),c.Links.length>0){for(let u of c.Links){let d=await xu(u.Hash,e,Vh(u,l),n,i);o.localSize+=d.localSize,o.dagSize+=d.dagSize,o.deduplicatedDagSize+=d.deduplicatedDagSize,o.blocks+=d.blocks,o.uniqueBlocks+=d.uniqueBlocks,o.dirSize+=d.dirSize}r&&l!=null&&(o.dirSize+=l.fileSize())}else{if(l==null)throw new ie(`PBNode ${t.toString()} had no data`);l.data!=null&&(o.localSize+=BigInt(l.data.byteLength??0)),r&&(o.dirSize+=l.fileSize())}}else throw new Fe(`${t.toString()} was neither DAG_PB nor RAW`)}catch(s){if(s.name!=="NotFoundError"&&s.name!=="BlockNotFoundWhileOfflineError"||i.offline!==!0)throw s}return o}function Vh(t,e){if(e==null)return!1;let r=t.Name;return r==null?!1:e.type==="directory"?!0:e.type==="hamt-sharded-directory"&&r.length>2}var jh=R("helia:unixfs:touch");async function _u(t,e,r={}){let n=await Re(t,r.path,e,r),i=r.mtime??{secs:BigInt(Math.round(Date.now()/1e3)),nsecs:0};if(jh("touch %c %o",n.cid,i),r.recursive){let d=await Wt(async function*(){for await(let h of Kn(n.cid,e)){let p,_,g=await U(h.cid,e,r);if(g.type==="raw")p=new k({data:g.node}),_=[];else if(g.type==="file"||g.type==="directory")p=g.unixfs,_=g.node.Links;else throw new Ne;p.mtime=i;let m={Data:p.marshal(),Links:_};yield{path:h.path,content:m}}},h=>yt(h,e,{...r,dagBuilder:async function*(p,_){for await(let g of p)yield async function(){let m=g.content,w=I(m),E=await qe(w,_,{...r,cidVersion:t.version});if(m.Data==null)throw new ie(`${E} had no data`);let B=k.unmarshal(m.Data);return{cid:E,size:BigInt(w.length),path:g.path,unixfs:B}}}}),async h=>Se(h));if(d==null)throw new Fe(`Could not chmod ${n.cid.toString()}`);return Jt(d.cid,n,e,r)}let o=await v(e.get(n.cid,r)),s,a=[];if(n.cid.code===J)s=new k({data:o});else{let d=q(o);if(a=d.Links,d.Data==null)throw new ie(`${n.cid.toString()} had no data`);s=k.unmarshal(d.Data)}s.mtime=i;let c=I({Data:s.marshal(),Links:a}),l=await fe.digest(c),u=S.create(n.cid.version,M,l);return await e.put(u,c),Jt(u,n,e,r)}var ai=class{components;constructor(e){this.components=e}async*addAll(e,r={}){yield*fn(e,this.components.blockstore,r)}async addBytes(e,r={}){return da(e,this.components.blockstore,r)}async addByteStream(e,r={}){return ha(e,this.components.blockstore,r)}async addFile(e,r={}){return ma(e,this.components.blockstore,r)}async addDirectory(e={},r={}){return pa(e,this.components.blockstore,r)}async*cat(e,r={}){yield*au(e,this.components.blockstore,r)}async chmod(e,r,n={}){return cu(e,r,this.components.blockstore,n)}async cp(e,r,n,i={}){return uu(e,r,n,this.components.blockstore,i)}async*ls(e,r={}){yield*lu(e,this.components.blockstore,r)}async mkdir(e,r,n={}){return du(e,r,this.components.blockstore,n)}async rm(e,r,n={}){return mu(e,r,this.components.blockstore,n)}async stat(e,r={}){return bu(e,this.components.blockstore,r)}async touch(e,r={}){return _u(e,this.components.blockstore,r)}};async function*Eu(){throw new Error("Not supported in browsers")}function ku(t,e){return t=new URL(t),{path:decodeURIComponent(new URL(t).pathname.split("/").pop()??""),content:qh(t,e)}}async function*qh(t,e){let r=await globalThis.fetch(t,e);if(r.body==null)throw new Fe("HTTP response did not have a body");let n=r.body.getReader();try{for(;;){let{done:i,value:o}=await n.read();if(i)return;o!=null&&(yield o)}}finally{n.releaseLock()}}function Wh(t){return new ai(t)}return Tu(Gh);})();
13
13
  return HeliaUnixfs}));
14
14
  //# sourceMappingURL=index.min.js.map