3 lines
59 KiB
JavaScript
3 lines
59 KiB
JavaScript
var Kt=Object.defineProperty;var Wt=(i,t,e)=>t in i?Kt(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var d=(i,t,e)=>Wt(i,typeof t!="symbol"?t+"":t,e);import{U as ve,f as Xt,m as He,g as Ue,h as Oe,a3 as pe,s as ee,a4 as _,C as z,a5 as Yt,a6 as N,D as R,q as be,a7 as Gt,v as Qt,w as Jt,t as yt,X as mt,a8 as Zt,a9 as qt,aa as jt,I as L,j as re,P as en,$ as Me,k as Re,a as tn,A as U,Y as nn,H as sn,l as rn,G as an,F as on,N as I,Z as cn,a1 as O}from"./index-BqzxYBEc.js";import{C as x,f as ln,d as un}from"./cid-DnxG1pfC.js";function dn(i){return i[Symbol.asyncIterator]!=null}function gt(i,t=1){return t=Number(t),dn(i)?async function*(){let e=[];if(t<1&&(t=1),t!==Math.round(t))throw new Error("Batch size must be an integer");for await(const n of i)for(e.push(n);e.length>=t;)yield e.slice(0,t),e=e.slice(t);for(;e.length>0;)yield e.slice(0,t),e=e.slice(t)}():function*(){let e=[];if(t<1&&(t=1),t!==Math.round(t))throw new Error("Batch size must be an integer");for(const n of i)for(e.push(n);e.length>=t;)yield e.slice(0,t),e=e.slice(t);for(;e.length>0;)yield e.slice(0,t),e=e.slice(t)}()}async function*wt(i,t=1){for await(const e of gt(i,t)){const n=e.map(async s=>s().then(r=>({ok:!0,value:r}),r=>({ok:!1,err:r})));for(let s=0;s<n.length;s++){const r=await n[s];if(r.ok)yield r.value;else throw r.err}}}const hn=262144,pt=(i={})=>{const t=i.chunkSize??hn;return async function*(n){let s=new ve,r=0,a=!1;for await(const o of n)for(s.append(o),r+=o.length;r>=t;)if(yield s.slice(0,t),a=!0,t===s.length)s=new ve,r=0;else{const c=new ve;c.append(s.sublist(t)),s=c,r-=t}(!a||r>0)&&(yield s.subarray(0,r))}},V=class V extends Error{constructor(e="Invalid type"){super(e);d(this,"name",V.name);d(this,"code",V.code)}};d(V,"name","InvalidTypeError"),d(V,"code","ERR_INVALID_TYPE");let fe=V;const K=class K extends Error{constructor(e="Invalid message"){super(e);d(this,"name",K.name);d(this,"code",K.code)}};d(K,"name","InvalidUnixFSMessageError"),d(K,"code","ERR_INVALID_MESSAGE");let ze=K;var D;(function(i){(function(n){n.Raw="Raw",n.Directory="Directory",n.File="File",n.Metadata="Metadata",n.Symlink="Symlink",n.HAMTShard="HAMTShard"})(i.DataType||(i.DataType={}));let t;(function(n){n[n.Raw=0]="Raw",n[n.Directory=1]="Directory",n[n.File=2]="File",n[n.Metadata=3]="Metadata",n[n.Symlink=4]="Symlink",n[n.HAMTShard=5]="HAMTShard"})(t||(t={})),function(n){n.codec=()=>Xt(t)}(i.DataType||(i.DataType={}));let e;i.codec=()=>(e==null&&(e=He((n,s,r={})=>{if(r.lengthDelimited!==!1&&s.fork(),n.Type!=null&&(s.uint32(8),i.DataType.codec().encode(n.Type,s)),n.Data!=null&&(s.uint32(18),s.bytes(n.Data)),n.filesize!=null&&(s.uint32(24),s.uint64(n.filesize)),n.blocksizes!=null)for(const a of n.blocksizes)s.uint32(32),s.uint64(a);n.hashType!=null&&(s.uint32(40),s.uint64(n.hashType)),n.fanout!=null&&(s.uint32(48),s.uint64(n.fanout)),n.mode!=null&&(s.uint32(56),s.uint32(n.mode)),n.mtime!=null&&(s.uint32(66),ye.codec().encode(n.mtime,s)),r.lengthDelimited!==!1&&s.ldelim()},(n,s)=>{const r={blocksizes:[]},a=s==null?n.len:n.pos+s;for(;n.pos<a;){const o=n.uint32();switch(o>>>3){case 1:r.Type=i.DataType.codec().decode(n);break;case 2:r.Data=n.bytes();break;case 3:r.filesize=n.uint64();break;case 4:r.blocksizes.push(n.uint64());break;case 5:r.hashType=n.uint64();break;case 6:r.fanout=n.uint64();break;case 7:r.mode=n.uint32();break;case 8:r.mtime=ye.codec().decode(n,n.uint32());break;default:n.skipType(o&7);break}}return r})),e),i.encode=n=>Ue(n,i.codec()),i.decode=n=>Oe(n,i.codec())})(D||(D={}));var ye;(function(i){let t;i.codec=()=>(t==null&&(t=He((e,n,s={})=>{s.lengthDelimited!==!1&&n.fork(),e.Seconds!=null&&(n.uint32(8),n.int64(e.Seconds)),e.FractionalNanoseconds!=null&&(n.uint32(21),n.fixed32(e.FractionalNanoseconds)),s.lengthDelimited!==!1&&n.ldelim()},(e,n)=>{const s={},r=n==null?e.len:e.pos+n;for(;e.pos<r;){const a=e.uint32();switch(a>>>3){case 1:s.Seconds=e.int64();break;case 2:s.FractionalNanoseconds=e.fixed32();break;default:e.skipType(a&7);break}}return s})),t),i.encode=e=>Ue(e,i.codec()),i.decode=e=>Oe(e,i.codec())})(ye||(ye={}));var nt;(function(i){let t;i.codec=()=>(t==null&&(t=He((e,n,s={})=>{s.lengthDelimited!==!1&&n.fork(),e.MimeType!=null&&(n.uint32(10),n.string(e.MimeType)),s.lengthDelimited!==!1&&n.ldelim()},(e,n)=>{const s={},r=n==null?e.len:e.pos+n;for(;e.pos<r;){const a=e.uint32();switch(a>>>3){case 1:s.MimeType=e.string();break;default:e.skipType(a&7);break}}return s})),t),i.encode=e=>Ue(e,i.codec()),i.decode=e=>Oe(e,i.codec())})(nt||(nt={}));const it={Raw:"raw",Directory:"directory",File:"file",Metadata:"metadata",Symlink:"symlink",HAMTShard:"hamt-sharded-directory"},fn=["directory","hamt-sharded-directory"],st=parseInt("0644",8),rt=parseInt("0755",8),at=BigInt(1024);let b=class bt{constructor(t={type:"file"}){d(this,"type");d(this,"data");d(this,"blockSizes");d(this,"hashType");d(this,"fanout");d(this,"mtime");d(this,"_mode");d(this,"_originalMode");const{type:e,data:n,blockSizes:s,hashType:r,fanout:a,mtime:o,mode:c}=t;if(e!=null&&!Object.values(it).includes(e))throw new fe("Type: "+e+" is not valid");this.type=e??"file",this.data=n,this.hashType=r,this.fanout=a,this.blockSizes=s??[],this._originalMode=0,this.mode=c,this.mtime=o}static unmarshal(t){const e=D.decode(t);if(e.fanout!=null&&e.fanout>at)throw new ze(`Fanout size was too large - ${e.fanout} > ${at}`);const n=new bt({type:it[e.Type!=null?e.Type.toString():"File"],data:e.Data,blockSizes:e.blocksizes,mode:e.mode,mtime:e.mtime!=null?{secs:e.mtime.Seconds??0n,nsecs:e.mtime.FractionalNanoseconds}:void 0,fanout:e.fanout});return n._originalMode=e.mode??0,n}set mode(t){t==null?this._mode=this.isDirectory()?rt:st:this._mode=t&4095}get mode(){return this._mode}isDirectory(){return fn.includes(this.type)}addBlockSize(t){this.blockSizes.push(t)}removeBlockSize(t){this.blockSizes.splice(t,1)}fileSize(){if(this.isDirectory())return 0n;let t=0n;return this.blockSizes.forEach(e=>{t+=e}),this.data!=null&&(t+=BigInt(this.data.length)),t}marshal(){let t;switch(this.type){case"raw":t=D.DataType.Raw;break;case"directory":t=D.DataType.Directory;break;case"file":t=D.DataType.File;break;case"metadata":t=D.DataType.Metadata;break;case"symlink":t=D.DataType.Symlink;break;case"hamt-sharded-directory":t=D.DataType.HAMTShard;break;default:throw new fe(`Type: ${t} is not valid`)}let e=this.data;(this.data==null||this.data.length===0)&&(e=void 0);let n;this.mode!=null&&(n=this._originalMode&4294963200|(this.mode??0),n===st&&!this.isDirectory()&&(n=void 0),n===rt&&this.isDirectory()&&(n=void 0));let s;return this.mtime!=null&&(s={Seconds:this.mtime.secs,FractionalNanoseconds:this.mtime.nsecs}),D.encode({Type:t,Data:e,filesize:this.isDirectory()?void 0:this.fileSize(),blocksizes:this.blockSizes,hashType:this.hashType,fanout:this.fanout,mode:n,mtime:s})}};const q=async(i,t,e)=>{e.codec==null&&(e.codec=pe);const n=await ee.digest(i),s=x.create(e.cidVersion,e.codec.code,n);return await t.put(s,i,e),s};function yn(i){return async function*(e,n){let s=0n;for await(let r of e.content)yield async()=>{var l;let a;const o={codec:pe,cidVersion:i.cidVersion,onProgress:i.onProgress};i.rawLeaves?(o.codec=Yt,o.cidVersion=1):(a=new b({type:i.leafType,data:r}),r=_({Data:a.marshal(),Links:[]}));const c=await q(r,n,o);return s+=BigInt(r.byteLength),(l=i.onProgress)==null||l.call(i,new z("unixfs:importer:progress:file:write",{bytesWritten:s,cid:c,path:e.path})),{cid:c,unixfs:a,size:BigInt(r.length),block:r}}}}var A;let kt=(A=class extends Error{constructor(e="Invalid parameters"){super(e);d(this,"name",A.name);d(this,"code",A.code)}},d(A,"name","InvalidParametersError"),d(A,"code","ERR_INVALID_PARAMS"),A);const W=class W extends Error{constructor(e="Invalid content"){super(e);d(this,"name",W.name);d(this,"code",W.code)}};d(W,"name","InvalidContentError"),d(W,"code","ERR_INVALID_CONTENT");let j=W;const mn=async(i,t,e)=>{const n=new b({type:"directory",mtime:i.mtime,mode:i.mode}),s=_(N({Data:n.marshal()})),r=await q(s,t,e),a=i.path;return{cid:r,path:a,unixfs:n,size:BigInt(s.length),originalPath:i.originalPath,block:s}};async function*gn(i,t,e){let n=-1,s;for await(const r of wt(e.bufferImporter(i,t),e.blockWriteConcurrency)){if(n++,n===0){s={...r,single:!0};continue}else n===1&&s!=null&&(yield{...s,block:void 0,single:void 0},s=void 0);yield{...r,block:void 0}}s!=null&&(yield s)}function ot(i){return i.single===!0}const wn=(i,t,e)=>async function(s){var h,f;if(s.length===1&&ot(s[0])&&e.reduceSingleLeafToSelf){const u=s[0];let y=u.block;return ot(u)&&(i.mtime!==void 0||i.mode!==void 0)&&(u.unixfs=new b({type:"file",mtime:i.mtime,mode:i.mode,data:u.block}),y={Data:u.unixfs.marshal(),Links:[]},u.block=_(N(y)),u.cid=await q(u.block,t,{...e,cidVersion:e.cidVersion}),u.size=BigInt(u.block.length)),(h=e.onProgress)==null||h.call(e,new z("unixfs:importer:progress:file:layout",{cid:u.cid,path:u.originalPath})),{cid:u.cid,path:i.path,unixfs:u.unixfs,size:u.size,originalPath:u.originalPath}}const r=new b({type:"file",mtime:i.mtime,mode:i.mode}),a=s.filter(u=>{var y,m;return u.cid.code===R&&u.size>0||u.unixfs!=null&&u.unixfs.data==null&&u.unixfs.fileSize()>0n?!0:!!((m=(y=u.unixfs)==null?void 0:y.data)!=null&&m.length)}).map(u=>{var y,m;return u.cid.code===R?(r.addBlockSize(u.size),{Name:"",Tsize:Number(u.size),Hash:u.cid}):(((y=u.unixfs)==null?void 0:y.data)==null?r.addBlockSize(((m=u.unixfs)==null?void 0:m.fileSize())??0n):r.addBlockSize(BigInt(u.unixfs.data.length)),{Name:"",Tsize:Number(u.size),Hash:u.cid})}),o={Data:r.marshal(),Links:a},c=_(N(o)),l=await q(c,t,e);return(f=e.onProgress)==null||f.call(e,new z("unixfs:importer:progress:file:layout",{cid:l,path:i.originalPath})),{cid:l,path:i.path,unixfs:r,size:BigInt(c.length+o.Links.reduce((u,y)=>u+(y.Tsize??0),0)),originalPath:i.originalPath,block:c}},pn=async(i,t,e)=>e.layout(gn(i,t,e),wn(i,t,e));function bn(i){return Symbol.iterator in i}function kn(i){return Symbol.asyncIterator in i}function _n(i){try{if(i instanceof Uint8Array)return async function*(){yield i}();if(bn(i))return async function*(){yield*i}();if(kn(i))return i}catch{throw new j("Content was invalid")}throw new j("Content was invalid")}function Bn(i){return async function*(e,n){for await(const s of e){let r;if(s.path!=null&&(r=s.path,s.path=s.path.split("/").filter(a=>a!=null&&a!==".").join("/")),Sn(s)){const a={path:s.path,mtime:s.mtime,mode:s.mode,content:async function*(){var l;let c=0n;for await(const h of i.chunker(i.chunkValidator(_n(s.content)))){const f=BigInt(h.byteLength);c+=f,(l=i.onProgress)==null||l.call(i,new z("unixfs:importer:progress:file:read",{bytesRead:c,chunkSize:f,path:s.path})),yield h}}(),originalPath:r},o=i.fileBuilder??pn;yield async()=>o(a,n,i)}else if(s.path!=null){const a={path:s.path,mtime:s.mtime,mode:s.mode,originalPath:r},o=i.dirBuilder??mn;yield async()=>o(a,n,i)}else throw new Error("Import candidate must have content or path or both")}}}function Sn(i){return i.content!=null}const xn=()=>async function*(t){for await(const e of t){if(e.length===void 0)throw new j("Content was invalid");if(typeof e=="string"||e instanceof String)yield be(e.toString());else if(Array.isArray(e))yield Uint8Array.from(e);else if(e instanceof Uint8Array)yield e;else throw new j("Content was invalid")}},vn=174;function _t(i){const t=(i==null?void 0:i.maxChildrenPerNode)??vn;return async function e(n,s){const r=[];for await(const a of gt(n,t))r.push(await s(a));return r.length>1?e(r,s):r[0]}}let se=class{constructor(t,e){d(this,"options");d(this,"root");d(this,"dir");d(this,"path");d(this,"dirty");d(this,"flat");d(this,"parent");d(this,"parentKey");d(this,"unixfs");d(this,"mode");d(this,"mtime");d(this,"cid");d(this,"size");d(this,"nodeSize");this.options=e??{},this.root=t.root,this.dir=t.dir,this.path=t.path,this.dirty=t.dirty,this.flat=t.flat,this.parent=t.parent,this.parentKey=t.parentKey,this.unixfs=t.unixfs,this.mode=t.mode,this.mtime=t.mtime}};const Ne=x.parse("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"),De=x.parse("zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi");class Ve extends se{constructor(e,n){super(e,n);d(this,"_children");this._children=new Map}async put(e,n){this.cid=void 0,this.size=void 0,this.nodeSize=void 0,this._children.set(e,n)}async get(e){return Promise.resolve(this._children.get(e))}childCount(){return this._children.size}directChildrenCount(){return this.childCount()}onlyChild(){return this._children.values().next().value}*eachChildSeries(){for(const[e,n]of this._children.entries())yield{key:e,child:n}}estimateNodeSize(){if(this.nodeSize!==void 0)return this.nodeSize;this.nodeSize=0;for(const[e,n]of this._children.entries())n.size!=null&&n.cid!=null&&(this.nodeSize+=e.length+(this.options.cidVersion===1?De.bytes.byteLength:Ne.bytes.byteLength));return this.nodeSize}async*flush(e){const n=[];for(const[l,h]of this._children.entries()){let f=h;if(h instanceof se)for await(const u of h.flush(e))f=u,yield u;f.size!=null&&f.cid!=null&&n.push({Name:l,Tsize:Number(f.size),Hash:f.cid})}const s=new b({type:"directory",mtime:this.mtime,mode:this.mode}),r={Data:s.marshal(),Links:n},a=_(N(r)),o=await q(a,e,this.options),c=a.length+r.Links.reduce((l,h)=>l+(h.Tsize??0),0);this.cid=o,this.size=c,yield{cid:o,unixfs:s,path:this.path,size:BigInt(c)}}}const ke=Gt({name:"murmur3-128",code:34,encode:i=>ln(Qt.x64.hash128(i))}),ue=7;var zn=class{constructor(){this._bitArrays=[],this._data=[],this._length=0,this._changedLength=!1,this._changedData=!1}set(t,e){let n=this._internalPositionFor(t,!1);if(e===void 0)n!==-1&&(this._unsetInternalPos(n),this._unsetBit(t),this._changedLength=!0,this._changedData=!0);else{let s=!1;n===-1?(n=this._data.length,this._setBit(t),this._changedData=!0):s=!0,this._setInternalPos(n,t,e,s),this._changedLength=!0}}unset(t){this.set(t,void 0)}get(t){this._sortData();const e=this._internalPositionFor(t,!0);if(e!==-1)return this._data[e][1]}push(t){return this.set(this.length,t),this.length}get length(){if(this._sortData(),this._changedLength){const t=this._data[this._data.length-1];this._length=t?t[0]+1:0,this._changedLength=!1}return this._length}forEach(t){let e=0;for(;e<this.length;)t(this.get(e),e,this),e++}map(t){let e=0,n=new Array(this.length);for(;e<this.length;)n[e]=t(this.get(e),e,this),e++;return n}reduce(t,e){let n=0,s=e;for(;n<this.length;){const r=this.get(n);s=t(s,r,n),n++}return s}find(t){let e=0,n,s;for(;e<this.length&&!n;)s=this.get(e),n=t(s),e++;return n?s:void 0}_internalPositionFor(t,e){const n=this._bytePosFor(t,e);if(n>=this._bitArrays.length)return-1;const s=this._bitArrays[n],r=t-n*ue;if(!((s&1<<r)>0))return-1;const o=this._bitArrays.slice(0,n).reduce(Nn,0),c=~(4294967295<<r+1),l=Bt(s&c);return o+l-1}_bytePosFor(t,e){const n=Math.floor(t/ue),s=n+1;for(;!e&&this._bitArrays.length<s;)this._bitArrays.push(0);return n}_setBit(t){const e=this._bytePosFor(t,!1);this._bitArrays[e]|=1<<t-e*ue}_unsetBit(t){const e=this._bytePosFor(t,!1);this._bitArrays[e]&=~(1<<t-e*ue)}_setInternalPos(t,e,n,s){const r=this._data,a=[e,n];if(s)this._sortData(),r[t]=a;else{if(r.length)if(r[r.length-1][0]>=e)r.push(a);else if(r[0][0]<=e)r.unshift(a);else{const o=Math.round(r.length/2);this._data=r.slice(0,o).concat(a).concat(r.slice(o))}else this._data.push(a);this._changedData=!0,this._changedLength=!0}}_unsetInternalPos(t){this._data.splice(t,1)}_sortData(){this._changedData&&this._data.sort(Dn),this._changedData=!1}bitField(){const t=[];let e=8,n=0,s=0,r;const a=this._bitArrays.slice();for(;a.length||n;){n===0&&(r=a.shift(),n=7);const c=Math.min(n,e),l=~(255<<c),h=r&l;s|=h<<8-e,r=r>>>c,n-=c,e-=c,(!e||!n&&!a.length)&&(t.push(s),s=0,e=8)}for(var o=t.length-1;o>0&&t[o]===0;o--)t.pop();return t}compactArray(){return this._sortData(),this._data.map(Pn)}};function Nn(i,t){return i+Bt(t)}function Bt(i){let t=i;return t=t-(t>>1&1431655765),t=(t&858993459)+(t>>2&858993459),(t+(t>>4)&252645135)*16843009>>24}function Dn(i,t){return i[0]-t[0]}function Pn(i){return i[1]}const me=Jt(zn);class S{constructor(t,e,n=0){d(this,"_options");d(this,"_popCount");d(this,"_parent");d(this,"_posAtParent");d(this,"_children");d(this,"key");this._options=t,this._popCount=0,this._parent=e,this._posAtParent=n,this._children=new me,this.key=null}async put(t,e){const n=await this._findNewBucketAndPos(t);n.bucket._putAt(n,t,e)}async get(t){const e=await this._findChild(t);if(e!=null)return e.value}async del(t){const e=await this._findPlace(t),n=e.bucket._at(e.pos);n!=null&&n.key===t&&e.bucket._delAt(e.pos)}leafCount(){return this._children.compactArray().reduce((e,n)=>n instanceof S?e+n.leafCount():e+1,0)}childrenCount(){return this._children.length}onlyChild(){return this._children.get(0)}*eachLeafSeries(){const t=this._children.compactArray();for(const e of t)e instanceof S?yield*e.eachLeafSeries():yield e}serialize(t,e){const n=[];return e(this._children.reduce((s,r,a)=>(r!=null&&(r instanceof S?s.push(r.serialize(t,e)):s.push(t(r,a))),s),n))}async asyncTransform(t,e){return St(this,t,e)}toJSON(){return this.serialize(In,Tn)}prettyPrint(){return JSON.stringify(this.toJSON(),null," ")}tableSize(){return Math.pow(2,this._options.bits)}async _findChild(t){const e=await this._findPlace(t),n=e.bucket._at(e.pos);if(!(n instanceof S)&&n!=null&&n.key===t)return n}async _findPlace(t){const e=this._options.hash(typeof t=="string"?be(t):t),n=await e.take(this._options.bits),s=this._children.get(n);return s instanceof S?s._findPlace(e):{bucket:this,pos:n,hash:e,existingChild:s}}async _findNewBucketAndPos(t){const e=await this._findPlace(t);if(e.existingChild!=null&&e.existingChild.key!==t){const n=new S(this._options,e.bucket,e.pos);e.bucket._putObjectAt(e.pos,n);const s=await n._findPlace(e.existingChild.hash);return s.bucket._putAt(s,e.existingChild.key,e.existingChild.value),n._findNewBucketAndPos(e.hash)}return e}_putAt(t,e,n){this._putObjectAt(t.pos,{key:e,value:n,hash:t.hash})}_putObjectAt(t,e){this._children.get(t)==null&&this._popCount++,this._children.set(t,e)}_delAt(t){if(t===-1)throw new Error("Invalid position");this._children.get(t)!=null&&this._popCount--,this._children.unset(t),this._level()}_level(){if(this._parent!=null&&this._popCount<=1)if(this._popCount===1){const t=this._children.find(Ln);if(t!=null&&!(t instanceof S)){const e=t.hash;e.untake(this._options.bits);const n={pos:this._posAtParent,hash:e,bucket:this._parent};this._parent._putAt(n,t.key,t.value)}}else this._parent._delAt(this._posAtParent)}_at(t){return this._children.get(t)}}function Ln(i){return!!i}function In(i,t){return i.key}function Tn(i){return i}async function St(i,t,e){const n=[];for(const s of i._children.compactArray())if(s instanceof S)await St(s,t,e);else{const r=await t(s);n.push({bitField:i._children.bitField(),children:r})}return e(n)}const An=[255,254,252,248,240,224,192,128],Cn=[1,3,7,15,31,63,127,255];let En=class{constructor(t){d(this,"_value");d(this,"_currentBytePos");d(this,"_currentBitPos");this._value=t,this._currentBytePos=t.length-1,this._currentBitPos=7}availableBits(){return this._currentBitPos+1+this._currentBytePos*8}totalBits(){return this._value.length*8}take(t){let e=t,n=0;for(;e>0&&this._haveBits();){const s=this._value[this._currentBytePos],r=this._currentBitPos+1,a=Math.min(r,e),o=$n(s,r-a,a);n=(n<<a)+o,e-=a,this._currentBitPos-=a,this._currentBitPos<0&&(this._currentBitPos=7,this._currentBytePos--)}return n}untake(t){for(this._currentBitPos+=t;this._currentBitPos>7;)this._currentBitPos-=8,this._currentBytePos+=1}_haveBits(){return this._currentBytePos>=0}};function $n(i,t,e){const n=Fn(t,e);return(i&n)>>>t}function Fn(i,t){return An[i]&Cn[Math.min(t+i-1,7)]}function Hn(i){function t(e){return e instanceof ct?e:new ct(e,i)}return t}let ct=class{constructor(t,e){d(this,"_value");d(this,"_hashFn");d(this,"_depth");d(this,"_availableBits");d(this,"_currentBufferIndex");d(this,"_buffers");if(!(t instanceof Uint8Array))throw new Error("can only hash Uint8Arrays");this._value=t,this._hashFn=e,this._depth=-1,this._availableBits=0,this._currentBufferIndex=0,this._buffers=[]}async take(t){let e=t;for(;this._availableBits<e;)await this._produceMoreBits();let n=0;for(;e>0;){const s=this._buffers[this._currentBufferIndex],r=Math.min(s.availableBits(),e),a=s.take(r);n=(n<<r)+a,e-=r,this._availableBits-=r,s.availableBits()===0&&this._currentBufferIndex++}return n}untake(t){let e=t;for(;e>0;){const n=this._buffers[this._currentBufferIndex],s=Math.min(n.totalBits()-n.availableBits(),e);n.untake(s),e-=s,this._availableBits+=s,this._currentBufferIndex>0&&n.totalBits()===n.availableBits()&&(this._depth--,this._currentBufferIndex--)}}async _produceMoreBits(){this._depth++;const t=this._depth>0?yt([this._value,Uint8Array.from([this._depth])]):this._value,e=await this._hashFn(t),n=new En(e);this._buffers.push(n),this._availableBits+=n.availableBits()}};function Ke(i){if(i==null||i.hashFn==null)throw new Error("please define an options.hashFn");const t={bits:i.bits??8,hash:Hn(i.hashFn)};return new S(t)}async function Un(i){return(await ke.encode(i)).slice(0,8).reverse()}const xt=BigInt(34),On=8;let Mn=class extends se{constructor(e,n){super(e,n);d(this,"_bucket");this._bucket=Ke({hashFn:Un,bits:n.shardFanoutBits??On})}async put(e,n){this.cid=void 0,this.size=void 0,this.nodeSize=void 0,await this._bucket.put(e,n)}async get(e){return this._bucket.get(e)}childCount(){return this._bucket.leafCount()}directChildrenCount(){return this._bucket.childrenCount()}onlyChild(){return this._bucket.onlyChild()}*eachChildSeries(){for(const{key:e,value:n}of this._bucket.eachLeafSeries())yield{key:e,child:n}}estimateNodeSize(){return this.nodeSize!==void 0?this.nodeSize:(this.nodeSize=zt(this._bucket,this,this.options),this.nodeSize)}async*flush(e){for await(const n of vt(this._bucket,e,this,this.options))yield{...n,path:this.path}}};async function*vt(i,t,e,n){const s=i._children,r=(i.tableSize()-1).toString(16).length,a=[];let o=0n;for(let m=0;m<s.length;m++){const w=s.get(m);if(w==null)continue;const p=m.toString(16).toUpperCase().padStart(r,"0");if(w instanceof S){let g;for await(const k of vt(w,t,null,n))g=k;if(g==null)throw new Error("Could not flush sharded directory, no subshard found");a.push({Name:p,Tsize:Number(g.size),Hash:g.cid}),o+=g.size}else if(Rn(w.value)){const g=w.value;let k;for await(const T of g.flush(t))k=T,yield k;if(k==null)throw new Error("Did not flush dir");const B=p+w.key;a.push({Name:B,Tsize:Number(k.size),Hash:k.cid}),o+=k.size}else{const g=w.value;if(g.cid==null)continue;const k=p+w.key,B=g.size;a.push({Name:k,Tsize:Number(B),Hash:g.cid}),o+=BigInt(B??0)}}const c=Uint8Array.from(s.bitField().reverse()),l=new b({type:"hamt-sharded-directory",data:c,fanout:BigInt(i.tableSize()),hashType:xt,mtime:e==null?void 0:e.mtime,mode:e==null?void 0:e.mode}),h={Data:l.marshal(),Links:a},f=_(N(h)),u=await q(f,t,n),y=BigInt(f.byteLength)+o;yield{cid:u,unixfs:l,size:y}}function Rn(i){return typeof i.flush=="function"}function zt(i,t,e){const n=i._children,s=(i.tableSize()-1).toString(16).length,r=[];for(let l=0;l<n.length;l++){const h=n.get(l);if(h==null)continue;const f=l.toString(16).toUpperCase().padStart(s,"0");if(h instanceof S){const u=zt(h,null,e);r.push({Name:f,Tsize:Number(u),Hash:e.cidVersion===0?Ne:De})}else if(typeof h.value.flush=="function"){const y=h.value.nodeSize();r.push({Name:f+h.key,Tsize:Number(y),Hash:e.cidVersion===0?Ne:De})}else{const u=h.value;if(u.cid==null)continue;const y=f+h.key,m=u.size;r.push({Name:y,Tsize:Number(m),Hash:u.cid})}}const a=Uint8Array.from(n.bitField().reverse()),o=new b({type:"hamt-sharded-directory",data:a,fanout:BigInt(i.tableSize()),hashType:xt,mtime:t==null?void 0:t.mtime,mode:t==null?void 0:t.mode});return _(N({Data:o.marshal(),Links:r})).length}async function Nt(i,t,e,n){let s=t;t instanceof Ve&&t.estimateNodeSize()>e&&(s=await Vn(t,n));const r=s.parent;if(r!=null){if(s!==t){if(i!=null&&(i.parent=s),s.parentKey==null)throw new Error("No parent key found");await r.put(s.parentKey,s)}return Nt(s,r,e,n)}return s}async function Vn(i,t){const e=new Mn({root:i.root,dir:!0,parent:i.parent,parentKey:i.parentKey,path:i.path,dirty:i.dirty,flat:!1,mtime:i.mtime,mode:i.mode},t);for(const{key:n,child:s}of i.eachChildSeries())await e.put(n,s);return e}const Kn=(i="")=>i.split(new RegExp("(?<!\\\\)\\/")).filter(Boolean);async function Wn(i,t,e){var o,c;const n=Kn(i.path??""),s=n.length-1;let r=t,a="";for(let l=0;l<n.length;l++){const h=n[l];a+=`${a!==""?"/":""}${h}`;const f=l===s;if(r.dirty=!0,r.cid=void 0,r.size=void 0,f)await r.put(h,i),t=await Nt(null,r,e.shardSplitThresholdBytes,e);else{let u=await r.get(h);(u==null||!(u instanceof se))&&(u=new Ve({root:!1,dir:!0,parent:r,parentKey:h,path:a,dirty:!0,flat:!0,mtime:(o=u==null?void 0:u.unixfs)==null?void 0:o.mtime,mode:(c=u==null?void 0:u.unixfs)==null?void 0:c.mode},e)),await r.put(h,u),r=u}}return t}async function*lt(i,t){var e;if(!(i instanceof se)){((e=i.unixfs)==null?void 0:e.isDirectory())===!0&&(yield i);return}yield*i.flush(t)}function Xn(i){return async function*(e,n){var o;let s=new Ve({root:!0,dir:!0,path:"",dirty:!0,flat:!0},i),r,a=!1;for await(const c of e){if(c==null)continue;const l=`${c.originalPath??""}`.split("/")[0];l!=null&&l!==""&&(r==null?(r=l,a=!0):r!==l&&(a=!1)),s=await Wn(c,s,i),((o=c.unixfs)==null?void 0:o.isDirectory())!==!0&&(yield c)}if(i.wrapWithDirectory||a&&s.childCount()>1)yield*lt(s,n);else for(const c of s.eachChildSeries())c!=null&&(yield*lt(c.child,n))}}async function*ae(i,t,e={}){let n;Symbol.asyncIterator in i||Symbol.iterator in i?n=i:n=[i];const s=e.wrapWithDirectory??!1,r=e.shardSplitThresholdBytes??262144,a=e.shardFanoutBits??8,o=e.cidVersion??1,c=e.rawLeaves??!0,l=e.leafType??"file",h=e.fileImportConcurrency??50,f=e.blockWriteConcurrency??10,u=e.reduceSingleLeafToSelf??!0,y=e.chunker??pt(),m=e.chunkValidator??xn(),w=e.dagBuilder??Bn({chunker:y,chunkValidator:m,wrapWithDirectory:s,layout:e.layout??_t(),bufferImporter:e.bufferImporter??yn({cidVersion:o,rawLeaves:c,leafType:l,onProgress:e.onProgress}),blockWriteConcurrency:f,reduceSingleLeafToSelf:u,cidVersion:o,onProgress:e.onProgress,dirBuilder:e.dirBuilder,fileBuilder:e.fileBuilder}),p=e.treeBuilder??Xn({wrapWithDirectory:s,shardSplitThresholdBytes:r,shardFanoutBits:a,cidVersion:o,onProgress:e.onProgress});for await(const g of p(wt(w(n,t),h),t))yield{cid:g.cid,path:g.path,unixfs:g.unixfs,size:g.size}}async function We(i,t,e={}){const n=await mt(ae([i],t,e));if(n==null)throw new kt("Nothing imported");return n}async function Yn(i,t,e={}){const n=await mt(ae([i],t,e));if(n==null)throw new kt("Nothing imported");return n}async function Gn(i,t,e={}){return We({content:i},t,e)}async function Qn(i,t,e={}){return We({content:i},t,e)}const oe={cidVersion:1,rawLeaves:!0,layout:_t({maxChildrenPerNode:1024}),chunker:pt({chunkSize:1048576})};async function*Jn(i,t,e={}){yield*ae(i,t,{...oe,...e})}async function Zn(i,t,e={}){const{cid:n}=await Gn(i,t,{...oe,...e});return n}async function qn(i,t,e={}){const{cid:n}=await Qn(i,t,{...oe,...e});return n}async function jn(i,t,e={}){const{cid:n}=await We(i,t,{...oe,...e});return n}async function ei(i,t,e={}){const{cid:n}=await Yn({...i,path:i.path??"-"},t,{...oe,...e});return n}function ti(i){return i[Symbol.asyncIterator]!=null}function _e(i){if(ti(i))return(async()=>{let e;for await(const n of i)e=n;return e})();let t;for(const e of i)t=e;return t}const X=class X extends Error{constructor(e="Bad path"){super(e);d(this,"name",X.name);d(this,"code",X.code)}};d(X,"name","BadPathError"),d(X,"code","ERR_BAD_PATH");let Pe=X;const Y=class Y extends Error{constructor(e="Not found"){super(e);d(this,"name",Y.name);d(this,"code",Y.code)}};d(Y,"name","NotFoundError"),d(Y,"code","ERR_NOT_FOUND");let F=Y;const G=class G extends Error{constructor(e="No resolver"){super(e);d(this,"name",G.name);d(this,"code",G.code)}};d(G,"name","NoResolverError"),d(G,"code","ERR_NO_RESOLVER");let Le=G;var C;let v=(C=class extends Error{constructor(e="Not UnixFS"){super(e);d(this,"name",C.name);d(this,"code",C.code)}},d(C,"name","NotUnixFSError"),d(C,"code","ERR_NOT_UNIXFS"),C);const Q=class Q extends Error{constructor(e="Over read"){super(e);d(this,"name",Q.name);d(this,"code",Q.code)}};d(Q,"name","OverReadError"),d(Q,"code","ERR_OVER_READ");let Ie=Q;const J=class J extends Error{constructor(e="Under read"){super(e);d(this,"name",J.name);d(this,"code",J.code)}};d(J,"name","UnderReadError"),d(J,"code","ERR_UNDER_READ");let Te=J;const Z=class Z extends Error{constructor(e="No Property found"){super(e);d(this,"name",Z.name);d(this,"code",Z.code)}};d(Z,"name","NoPropError"),d(Z,"code","ERR_NO_PROP");let Ae=Z;var E;let de=(E=class extends Error{constructor(e="Invalid parameters"){super(e);d(this,"name",E.name);d(this,"code",E.code)}},d(E,"name","InvalidParametersError"),d(E,"code","ERR_INVALID_PARAMS"),E);function Xe(i,t,e,n,s,r,a){let o=i,c=s;for(;r.length>0;){const l=r[0];if(l in o){r.shift(),c=`${c}/${l}`;const h=x.asCID(o[l]);if(h!=null)return{entry:{type:"object",name:n,path:s,cid:e,node:t,depth:a,size:BigInt(t.length),content:async function*(){yield i}},next:{cid:h,name:l,path:c,toResolve:r}};o=o[l]}else throw new Ae(`No property named ${l} found in node ${e}`)}return{entry:{type:"object",name:n,path:s,cid:e,node:t,depth:a,size:BigInt(t.length),content:async function*(){yield i}}}}const ni=async(i,t,e,n,s,r,a,o)=>{const c=await a.get(i,o),l=Zt(c);return Xe(l,c,i,t,e,n,r)},ii=async(i,t,e,n,s,r,a,o)=>{const c=await a.get(i,o),l=qt(c);return Xe(l,c,i,t,e,n,r)};function ge(i,t,e,n){const s=BigInt(i.length),r=BigInt(t+s);return e>=r||n<t?new Uint8Array(0):(n>=t&&n<r&&(i=i.subarray(0,Number(n-t))),e>=t&&e<r&&(i=i.subarray(Number(e-t))),i)}const Ye=(i,t=0,e=i)=>{const n=BigInt(i),s=BigInt(t??0);let r=BigInt(e);if(r!==n&&(r=s+r),r>n&&(r=n),s<0n)throw new de("Offset must be greater than or equal to 0");if(s>n)throw new de("Offset must be less than the file size");if(r<0n)throw new de("Length must be greater than or equal to 0");if(r>n)throw new de("Length must be less than the file size");return{start:s,end:r}},si=i=>{async function*t(e={}){var a;const{start:n,end:s}=Ye(i.length,e.offset,e.length),r=ge(i,0n,n,s);(a=e.onProgress)==null||a.call(e,new z("unixfs:exporter:progress:identity",{bytesRead:BigInt(r.byteLength),totalBytes:s-n,fileSize:BigInt(i.byteLength)})),yield r}return t},ri=async(i,t,e,n,s,r,a,o)=>{if(n.length>0)throw new F(`No link named ${e} found in raw node ${i}`);const c=un(i.multihash.bytes);return{entry:{type:"identity",name:t,path:e,cid:i,content:si(c.digest),depth:r,size:BigInt(c.digest.length),node:c.digest}}},ai=async(i,t,e,n,s,r,a,o)=>{const c=await a.get(i,o),l=jt(c);return Xe(l,c,i,t,e,n,r)},oi=i=>{async function*t(e={}){var a;const{start:n,end:s}=Ye(i.length,e.offset,e.length),r=ge(i,0n,n,s);(a=e.onProgress)==null||a.call(e,new z("unixfs:exporter:progress:raw",{bytesRead:BigInt(r.byteLength),totalBytes:s-n,fileSize:BigInt(i.byteLength)})),yield r}return t},ci=async(i,t,e,n,s,r,a,o)=>{if(n.length>0)throw new F(`No link named ${e} found in raw node ${i}`);const c=await a.get(i,o);return{entry:{type:"raw",name:t,path:e,cid:i,content:oi(c),depth:r,size:BigInt(c.length),node:c}}},li=async function(i){return(await ke.encode(i)).slice(0,8).reverse()},ui=async(i,t,e)=>{const n=(t.tableSize()-1).toString(16).length;await Promise.all(i.map(async s=>{if(s.Name==null)throw new Error("Unexpected Link without a Name");if(s.Name.length===n){const r=parseInt(s.Name,16);t._putObjectAt(r,new S({hash:e._options.hash,bits:e._options.bits},t,r));return}await e.put(s.Name.substring(2),!0)}))},ut=(i,t)=>i.toString(16).toUpperCase().padStart(t,"0").substring(0,t),di=i=>{let t=i.bucket;const e=[];for(;t._parent!=null;)e.push(t),t=t._parent;return e.push(t),e.reverse()},Dt=async(i,t,e,n,s)=>{if(n==null){if(i.Data==null)throw new v("no data in PBNode");let f;try{f=b.unmarshal(i.Data)}catch(y){throw new v(y.message)}if(f.type!=="hamt-sharded-directory")throw new v("not a HAMT");if(f.fanout==null)throw new v("missing fanout");const u=Ke({hashFn:li,bits:Math.log2(Number(f.fanout))});n={rootBucket:u,hamtDepth:1,lastBucket:u}}const r=(n.lastBucket.tableSize()-1).toString(16).length;await ui(i.Links,n.lastBucket,n.rootBucket);const a=await n.rootBucket._findNewBucketAndPos(t);let o=ut(a.pos,r);const c=di(a);c.length>n.hamtDepth&&(n.lastBucket=c[n.hamtDepth],o=ut(n.lastBucket._posAtParent,r));const l=i.Links.find(f=>{if(f.Name==null)return!1;const u=f.Name.substring(0,r),y=f.Name.substring(r);return!(u!==o||y!==""&&y!==t)});if(l==null)return;if(l.Name!=null&&l.Name.substring(r)===t)return l.Hash;n.hamtDepth++;const h=await e.get(l.Hash,s);return i=L(h),Dt(i,t,e,n,s)};function Ge(i){return(i==null?void 0:i.extended)===!1}const hi=(i,t,e,n,s,r,a)=>{async function*o(c={}){var u;const l=c.offset??0,h=c.length??t.Links.length,f=t.Links.slice(l,h);(u=c.onProgress)==null||u.call(c,new z("unixfs:exporter:walk:directory",{cid:i})),yield*re(f,y=>Re(y,m=>async()=>{const w=m.Name??"",p=`${n}/${w}`;return Ge(c)?{cid:m.Hash,name:w,path:p}:(await s(m.Hash,w,p,[],r+1,a,c)).entry}),y=>Me(y,{ordered:!0,concurrency:c.blockReadConcurrency}),y=>en(y,m=>m!=null))}return o};async function Pt(i,t,e,n,s,r,a){if(t instanceof Uint8Array){const l=ge(t,n,s,r);e.push(l);return}if(t.Data==null)throw new v("no data in PBNode");let o;try{o=b.unmarshal(t.Data)}catch(l){throw new v(l.message)}if(o.data!=null){const l=o.data,h=ge(l,n,s,r);e.push(h),n+=BigInt(h.byteLength)}const c=[];if(t.Links.length!==o.blockSizes.length)throw new v("Inconsistent block sizes and dag links");for(let l=0;l<t.Links.length;l++){const h=t.Links[l],f=n,u=f+o.blockSizes[l];if((s>=f&&s<u||r>=f&&r<=u||s<f&&r>u)&&c.push({link:h,blockStart:n}),n=u,n>r)break}await re(c,l=>Re(l,h=>async()=>{const f=await i.get(h.link.Hash,a);return{...h,block:f}}),l=>Me(l,{ordered:!0,concurrency:a.blockReadConcurrency}),async l=>{for await(const{link:h,block:f,blockStart:u}of l){let y;switch(h.Hash.code){case U:y=L(f);break;case R:y=f;break;default:e.end(new v(`Unsupported codec: ${h.Hash.code}`));return}const m=new nn({concurrency:1});m.on("error",w=>{e.end(w)}),m.add(async()=>{var w;(w=a.onProgress)==null||w.call(a,new z("unixfs:exporter:walk:file",{cid:h.Hash})),await Pt(i,y,e,u,s,r,a)}),await m.onIdle()}}),n>=r&&e.end()}const dt=(i,t,e,n,s,r,a)=>{async function*o(c={}){var w,p;const l=e.fileSize();if(l===void 0)throw new Error("File was a directory");const{start:h,end:f}=Ye(l,c.offset,c.length);if(f===0n)return;let u=0n;const y=f-h,m=tn();(w=c.onProgress)==null||w.call(c,new z("unixfs:exporter:walk:file",{cid:i})),Pt(a,t,m,0n,h,f,c).catch(g=>{m.end(g)});for await(const g of m)if(g!=null){if(u+=BigInt(g.byteLength),u>y)throw m.end(),new Ie("Read too many bytes - the file size reported by the UnixFS data in the root node may be incorrect");u===y&&m.end(),(p=c.onProgress)==null||p.call(c,new z("unixfs:exporter:progress:unixfs:file",{bytesRead:u,totalBytes:y,fileSize:l})),yield g}if(u<y)throw new Te("Traversed entire DAG but did not read enough bytes")}return o},fi=(i,t,e,n,s,r,a)=>{function o(c={}){var l;return(l=c.onProgress)==null||l.call(c,new z("unixfs:exporter:walk:hamt-sharded-directory",{cid:i})),Lt(t,n,s,r,a,c)}return o};async function*Lt(i,t,e,n,s,r){const a=i.Links;if(i.Data==null)throw new v("no data in PBNode");let o;try{o=b.unmarshal(i.Data)}catch(h){throw new v(h.message)}if(o.fanout==null)throw new v("missing fanout");const c=(o.fanout-1n).toString(16).length,l=re(a,h=>Re(h,f=>async()=>{var y;const u=f.Name!=null?f.Name.substring(c):null;if(u!=null&&u!==""){const m=`${t}/${u}`;return Ge(r)?{entries:[{cid:f.Hash,name:u,path:m}]}:{entries:[(await e(f.Hash,u,m,[],n+1,s,r)).entry].filter(Boolean)}}else{const m=await s.get(f.Hash,r);return i=L(m),(y=r.onProgress)==null||y.call(r,new z("unixfs:exporter:walk:hamt-sharded-directory",{cid:f.Hash})),{entries:Lt(i,t,e,n,s,r)}}}),h=>Me(h,{ordered:!0,concurrency:r.blockReadConcurrency}));for await(const{entries:h}of l)yield*h}const yi=(i,t)=>{const e=i.Links.find(n=>n.Name===t);return e==null?void 0:e.Hash},mi={raw:dt,file:dt,directory:hi,"hamt-sharded-directory":fi,metadata:(i,t,e,n,s,r,a)=>()=>[],symlink:(i,t,e,n,s,r,a)=>()=>[]},gi=async(i,t,e,n,s,r,a,o)=>{if(Ge(o)&&n.length===0)return{entry:{cid:i,name:t,path:e}};const c=await a.get(i,o),l=L(c);let h,f;if(t==null&&(t=i.toString()),l.Data==null)throw new v("no data in PBNode");try{h=b.unmarshal(l.Data)}catch(y){throw new v(y.message)}if(e==null&&(e=t),n.length>0){let y;if((h==null?void 0:h.type)==="hamt-sharded-directory"?y=await Dt(l,n[0],a):y=yi(l,n[0]),y==null)throw new F("file does not exist");const m=n.shift(),w=`${e}/${m}`;f={cid:y,toResolve:n,name:m??"",path:w}}const u=mi[h.type](i,l,h,e,s,r,a);if(u==null)throw new F("could not find content exporter");return h.isDirectory()?{entry:{type:"directory",name:t,path:e,cid:i,content:u,unixfs:h,depth:r,node:l,size:h.fileSize()},next:f}:{entry:{type:"file",name:t,path:e,cid:i,content:u,unixfs:h,depth:r,node:l,size:h.fileSize()},next:f}},wi={[U]:gi,[R]:ci,[on]:ni,[an]:ii,[rn.code]:ri,[sn]:ai},It=async(i,t,e,n,s,r,a)=>{const o=wi[i.code];if(o==null)throw new Le(`No resolver for code ${i.code}`);return o(i,t,e,n,It,s,r,a)},pi=(i="")=>(i.trim().match(/([^\\^/]|\\\/)+/g)??[]).filter(Boolean),bi=i=>{if(i instanceof Uint8Array)return{cid:x.decode(i),toResolve:[]};const t=x.asCID(i);if(t!=null)return{cid:t,toResolve:[]};if(typeof i=="string"){i.indexOf("/ipfs/")===0&&(i=i.substring(6));const e=pi(i);return{cid:x.parse(e[0]),toResolve:e.slice(1)}}throw new Pe(`Unknown path type ${i}`)};async function*Tt(i,t,e={}){let{cid:n,toResolve:s}=bi(i),r=n.toString(),a=r;const o=s.length;for(;;){const c=await It(n,r,a,s,o,t,e);if(c.entry==null&&c.next==null)throw new F(`Could not resolve ${i}`);if(c.entry!=null&&(yield c.entry),c.next==null)return;s=c.next.toResolve,n=c.next.cid,r=c.next.name,a=c.next.path}}async function M(i,t,e={}){const n=await _e(Tt(i,t,e));if(n==null)throw new F(`Could not resolve ${i}`);return n}async function*At(i,t,e={}){const n=await M(i,t,e);if(n==null)return;if(yield n,n.type==="directory")for await(const r of s(n,e))yield r;async function*s(r,a){for await(const o of r.content(a))yield o,!(o instanceof Uint8Array)&&o.type==="directory"&&(yield*s(o,a))}}class $ extends Error{constructor(e,n,s){super(e);d(this,"name");d(this,"code");this.name=n,this.code=s}}class Be extends ${constructor(t="not a Unixfs node"){super(t,"NotUnixFSError","ERR_NOT_UNIXFS")}}class H extends ${constructor(t="invalid PBNode"){super(t,"InvalidPBNodeError","ERR_INVALID_PBNODE")}}class Qe extends ${constructor(t="unknown error"){super(t,"InvalidPBNodeError","ERR_UNKNOWN_ERROR")}}class Ct extends ${constructor(t="path already exists"){super(t,"AlreadyExistsError","ERR_ALREADY_EXISTS")}}class ki extends ${constructor(t="path does not exist"){super(t,"DoesNotExistError","ERR_DOES_NOT_EXIST")}}class Et extends ${constructor(t="no content"){super(t,"NoContentError","ERR_NO_CONTENT")}}class _i extends ${constructor(t="not a file"){super(t,"NotAFileError","ERR_NOT_A_FILE")}}class Je extends ${constructor(t="not a directory"){super(t,"NotADirectoryError","ERR_NOT_A_DIRECTORY")}}class te extends ${constructor(t="invalid parameters"){super(t,"InvalidParametersError","ERR_INVALID_PARAMETERS")}}function $t(i){function t(e){return e instanceof ht?e:new ht(e,i)}return t}class ht{constructor(t,e){d(this,"_value");d(this,"_hashFn");d(this,"_depth");d(this,"_availableBits");d(this,"_currentBufferIndex");d(this,"_buffers");if(!(t instanceof Uint8Array))throw new Error("can only hash Uint8Arrays");this._value=t,this._hashFn=e,this._depth=-1,this._availableBits=0,this._currentBufferIndex=0,this._buffers=[]}async take(t){let e=t;for(;this._availableBits<e;)await this._produceMoreBits();let n=0;for(;e>0;){const s=this._buffers[this._currentBufferIndex],r=Math.min(s.availableBits(),e),a=s.take(r);n=(n<<r)+a,e-=r,this._availableBits-=r,s.availableBits()===0&&this._currentBufferIndex++}return n}untake(t){let e=t;for(;e>0;){const n=this._buffers[this._currentBufferIndex],s=Math.min(n.totalBits()-n.availableBits(),e);n.untake(s),e-=s,this._availableBits+=s,this._currentBufferIndex>0&&n.totalBits()===n.availableBits()&&(this._depth--,this._currentBufferIndex--)}}async _produceMoreBits(){this._depth++;const t=this._depth>0?yt([this._value,Uint8Array.from([this._depth])]):this._value,e=await this._hashFn(t),n=new xi(e);this._buffers.push(n),this._availableBits+=n.availableBits()}}const Bi=[255,254,252,248,240,224,192,128],Si=[1,3,7,15,31,63,127,255];class xi{constructor(t){d(this,"_value");d(this,"_currentBytePos");d(this,"_currentBitPos");this._value=t,this._currentBytePos=t.length-1,this._currentBitPos=7}availableBits(){return this._currentBitPos+1+this._currentBytePos*8}totalBits(){return this._value.length*8}take(t){let e=t,n=0;for(;e>0&&this._haveBits();){const s=this._value[this._currentBytePos],r=this._currentBitPos+1,a=Math.min(r,e),o=vi(s,r-a,a);n=(n<<a)+o,e-=a,this._currentBitPos-=a,this._currentBitPos<0&&(this._currentBitPos=7,this._currentBytePos--)}return n}untake(t){for(this._currentBitPos+=t;this._currentBitPos>7;)this._currentBitPos-=8,this._currentBytePos+=1}_haveBits(){return this._currentBytePos>=0}}function vi(i,t,e){const n=zi(t,e);return(i&n)>>>t}function zi(i,t){return Bi[i]&Si[Math.min(t+i-1,7)]}const Ze=BigInt(ke.code),ie=8;async function qe(i){return(await ke.encode(i)).subarray(0,8).reverse()}const ne=async(i,t,e)=>{e.codec==null&&(e.codec=pe);const n=await ee.digest(i),s=x.create(e.cidVersion,e.codec.code,n);return await t.put(s,i,{...e,signal:e.signal}),s};class Ni{constructor(t,e){d(this,"options");d(this,"root");d(this,"dir");d(this,"path");d(this,"dirty");d(this,"flat");d(this,"parent");d(this,"parentKey");d(this,"unixfs");d(this,"mode");d(this,"mtime");d(this,"cid");d(this,"size");d(this,"nodeSize");this.options=e??{},this.root=t.root,this.dir=t.dir,this.path=t.path,this.dirty=t.dirty,this.flat=t.flat,this.parent=t.parent,this.parentKey=t.parentKey,this.unixfs=t.unixfs,this.mode=t.mode,this.mtime=t.mtime}}class Di extends Ni{constructor(e,n){super(e,n);d(this,"_bucket");this._bucket=Ke({hashFn:qe,bits:8})}async put(e,n){this.cid=void 0,this.size=void 0,this.nodeSize=void 0,await this._bucket.put(e,n)}async get(e){return this._bucket.get(e)}childCount(){return this._bucket.leafCount()}directChildrenCount(){return this._bucket.childrenCount()}onlyChild(){return this._bucket.onlyChild()}async*eachChildSeries(){for await(const{key:e,value:n}of this._bucket.eachLeafSeries())yield{key:e,child:n}}estimateNodeSize(){return this.nodeSize!==void 0?this.nodeSize:(this.nodeSize=Ht(this._bucket,this,this.options),this.nodeSize)}async*flush(e){for await(const n of Ft(this._bucket,e,this,this.options))yield{...n,path:this.path}}}async function*Ft(i,t,e,n){const s=i._children,r=[];let a=0n;for(let y=0;y<s.length;y++){const m=s.get(y);if(m==null)continue;const w=y.toString(16).toUpperCase().padStart(2,"0");if(m instanceof S){let p;for await(const g of Ft(m,t,null,n))p=g;if(p==null)throw new Error("Could not flush sharded directory, no subshard found");r.push({Name:w,Tsize:Number(p.size),Hash:p.cid}),a+=p.size}else if(Pi(m.value)){const p=m.value;let g;for await(const B of p.flush(t))g=B,yield g;if(g==null)throw new Error("Did not flush dir");const k=w+m.key;r.push({Name:k,Tsize:Number(g.size),Hash:g.cid}),a+=g.size}else{const p=m.value;if(p.cid==null)continue;const g=w+m.key,k=p.size;r.push({Name:g,Tsize:Number(k),Hash:p.cid}),a+=BigInt(k??0)}}const o=Uint8Array.from(s.bitField().reverse()),c=new b({type:"hamt-sharded-directory",data:o,fanout:BigInt(i.tableSize()),hashType:Ze,mtime:e==null?void 0:e.mtime,mode:e==null?void 0:e.mode}),l={Data:c.marshal(),Links:r},h=_(N(l)),f=await ne(h,t,n),u=BigInt(h.byteLength)+a;yield{cid:f,unixfs:c,size:u}}function Pi(i){return typeof i.flush=="function"}function Ht(i,t,e){const n=i._children,s=[];for(let c=0;c<n.length;c++){const l=n.get(c);if(l==null)continue;const h=c.toString(16).toUpperCase().padStart(2,"0");if(l instanceof S){const f=Ht(l,null,e);s.push({Name:h,Tsize:Number(f),Hash:e.cidVersion===0?Ce:Ee})}else if(typeof l.value.flush=="function"){const u=l.value.nodeSize();s.push({Name:h+l.key,Tsize:Number(u),Hash:e.cidVersion===0?Ce:Ee})}else{const f=l.value;if(f.cid==null)continue;const u=h+l.key,y=f.size;s.push({Name:u,Tsize:Number(y),Hash:f.cid})}}const r=Uint8Array.from(n.bitField().reverse()),a=new b({type:"hamt-sharded-directory",data:r,fanout:BigInt(i.tableSize()),hashType:Ze,mtime:t==null?void 0:t.mtime,mode:t==null?void 0:t.mode});return _(N({Data:a.marshal(),Links:s})).length}const Ce=x.parse("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"),Ee=x.parse("zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi"),$e=I("helia:unixfs:commands:utils:hamt-utils"),Fe=i=>i.toString(16).toUpperCase().padStart(2,"0").substring(0,2),Li=async(i,t,e)=>{const n=new Di({root:!0,dir:!0,parent:void 0,parentKey:void 0,path:"",dirty:!0,flat:!1,mtime:e.mtime,mode:e.mode},e);for(let r=0;r<t.length;r++)await n._bucket.put(t[r].name,{size:t[r].size,cid:t[r].cid});const s=await _e(n.flush(i));if(s==null)throw new Error("Flushing shard yielded no result");return s},Ut=async(i,t,e)=>{const n=b.unmarshal(i[0].node.Data??new Uint8Array(0)),s=BigInt(Math.pow(2,ie));i.reverse();let r,a;for(let o=0;o<i.length;o++){const c=o===i.length-1,l=i[o],h=Uint8Array.from(l.children.bitField().reverse()),f=new b({type:"hamt-sharded-directory",data:h,fanout:s,hashType:Ze});c&&(f.mtime=n.mtime,f.mode=n.mode),a={Data:f.marshal(),Links:l.node.Links};const u=_(N(a));if(r=await ne(u,t,e),!c){const y=i[o+1];if(y==null)throw new Error("Was not operating on shard root but also had no parent?");$e("updating link in parent sub-shard with prefix %s",y.prefix),y.node.Links=y.node.Links.filter(m=>m.Name!==y.prefix),y.node.Links.push({Name:y.prefix,Hash:r,Tsize:l.node.Links.reduce((m,w)=>m+(w.Tsize??0),u.byteLength)})}}if(r==null||a==null)throw new Error("Noting persisted");return{cid:r,node:a}},Ot=async(i,t,e,n)=>{const r=$t(qe)(be(t)),a=[];for(;;){const o=await e.get(i,n),c=L(o),l=new me,h=await r.take(ie),f=Fe(h);a.push({prefix:f,children:l,node:c});let u;for(const m of c.Links){const w=m.Name??"";if(w.length<2)throw new Error("Invalid HAMT - link name was too short");const p=parseInt(w.substring(0,2),16);l.set(p,!0),w.startsWith(f)&&(u=m)}if(u==null){$e("no link found with prefix %s for %s",f,t);break}const y=u.Name??"";if(y.length<2)throw new Error("Invalid HAMT - link name was too short");if(y.length===2){i=u.Hash,$e("descend into sub-shard with prefix %s",y);continue}break}return{path:a,hash:r}};async function Mt(i,t,e,n){if(i.Data==null)throw new Error("DagPB node had no data");const s=b.unmarshal(i.Data);let r;if(s.type==="directory")r=Ii(i);else if(s.type==="hamt-sharded-directory")r=await Rt(i,0,e,t,n);else throw new Error("Can only estimate the size of directories or shards");return r>e}function Ii(i){let t=0;for(const e of i.Links)t+=(e.Name??"").length,t+=e.Hash.version===1?Ee.bytes.byteLength:Ce.bytes.byteLength;return t}async function Rt(i,t,e,n,s){if(t>e)return e;if(i.Data==null||!b.unmarshal(i.Data).isDirectory())return t;for(const a of i.Links){let o=a.Name??"";if(o=o.substring(2),t+=o.length,t+=a.Hash.bytes.byteLength,a.Hash.code===U){const c=await n.get(a.Hash,s),l=L(c);t+=await Rt(l,t,e,n,s)}}return t}const P=I("helia:unixfs:components:utils:add-link");async function je(i,t,e,n){if(i.node.Data==null)throw new te("Invalid parent passed to addLink");if(b.unmarshal(i.node.Data).type==="hamt-sharded-directory")return P("adding link to sharded directory"),Ci(i,t,e,n);P(`adding ${t.Name} (${t.Hash}) to regular directory`);const r=await Ai(i,t,e,n);if(await Mt(r.node,e,n.shardSplitThresholdBytes,n)){P("converting directory to sharded directory");const a=await Ti(r,e);r.cid=a.cid,r.node=L(await e.get(a.cid,n))}return r}const Ti=async(i,t)=>{if(i.node.Data==null)throw new te("Invalid parent passed to convertToShardedDirectory");const e=b.unmarshal(i.node.Data),n=await Li(t,i.node.Links.map(s=>({name:s.Name??"",size:BigInt(s.Tsize??0),cid:s.Hash})),{mode:e.mode,mtime:e.mtime,cidVersion:i.cid.version});return P(`converted directory to sharded directory ${n.cid}`),n},Ai=async(i,t,e,n)=>{const s=i.node.Links.filter(h=>{const f=h.Name===t.Name;if(f&&!n.allowOverwriting)throw new Ct;return!f});if(s.push(t),i.node.Data==null)throw new H("Parent node with no data passed to addToDirectory");const r=b.unmarshal(i.node.Data);let a;if(r.mtime!=null){const h=Date.now(),f=Math.floor(h/1e3);r.mtime={secs:BigInt(f),nsecs:(h-f*1e3)*1e3},a=r.marshal()}else a=i.node.Data;i.node=N({Data:a,Links:s});const o=_(i.node),c=await ee.digest(o),l=x.create(i.cid.version,U,c);return await e.put(l,o),{node:i.node,cid:l}},Ci=async(i,t,e,n)=>{var f;const{path:s,hash:r}=await Ot(i.cid,t.Name,e,n),a=s[s.length-1];if(a==null)throw new Error("Invalid HAMT, could not generate path");const o=a.prefix,c=parseInt(o,16);P("next prefix for %s is %s",t.Name,o);const l=`${o}${t.Name}`,h=a.node.Links.find(u=>(u.Name??"").startsWith(o));if(h!=null)if(P("link %s was present in shard",l),h.Name===l){if(!n.allowOverwriting)throw new Ct;P("overwriting %s in subshard",t.Name),a.node.Links=a.node.Links.filter(u=>u.Name!==l),a.node.Links.push({Name:l,Hash:t.Hash,Tsize:t.Tsize})}else{if(((f=h.Name)==null?void 0:f.length)===2)throw new Error("Existing link was subshard?!");{P("prefix %s already exists, creating new subshard",o);const u=a.node.Links.findIndex(g=>{var k;return(k=g.Name)==null?void 0:k.startsWith(o)}),y=a.node.Links.splice(u,1)[0],m=(y.Name??"").substring(2),p=$t(qe)(be(m));for(let g=0;g<s.length;g++)await p.take(ie);for(;;){const g=await p.take(ie),k=Fe(g);y.Name=`${k}${m}`;const B=await r.take(ie),T=Fe(B);if(k===T){const tt=new me;tt.set(B,!0),s.push({prefix:T,children:tt,node:{Links:[]}});continue}const xe=new me;xe.set(B,!0),xe.set(g,!0),s.push({prefix:o,children:xe,node:{Links:[y,{Name:`${T}${t.Name}`,Hash:t.Hash,Tsize:t.Tsize}]}});break}}}else P("link %s was not present in sub-shard",l),t.Name=l,a.node.Links.push(t),a.children.set(c,!0),P("adding %s to existing sub-shard",l);return Ut(s,e,n)};async function Se(i,t,e={}){const n=await M(i,t,e);if(n.type!=="directory")throw new Je(`${i.toString()} was not a UnixFS directory`);return{cid:i,node:n.node}}async function et(i,t,e,n){const s=await M(i,e,n);if(s.type!=="directory"&&s.type!=="file"&&s.type!=="raw")throw new Be(`${i.toString()} was not a UnixFS node`);return{Name:t,Tsize:s.node instanceof Uint8Array?s.node.byteLength:Ei(s.node),Hash:i}}function Ei(i){const t=i.Links.reduce((e,n)=>e+(n.Tsize??0),0);return _(i).byteLength+t}const $i=I("helia:unixfs:components:utils:resolve");async function ce(i,t,e,n){if(t==null||t==="")return{cid:i};const s=`/ipfs/${i}${t==null?"":`/${t}`}`,r=await cn(Tt(s,e,n));if(r.length===0)throw new ki("Could not find path in directory");return $i("resolved %s to %c",t,i),{cid:r[r.length-1].cid,path:t,segments:r}}async function we(i,t,e,n){if(t.segments==null||t.segments.length===0)return i;let s=t.segments.pop();if(s==null)throw new Error("Insufficient segments");s.cid=i,t.segments.reverse();for(const r of t.segments){const[a,o]=await Promise.all([Se(r.cid,e,n),et(s.cid,s.name,e,n)]);i=(await je(a,o,e,{...n,allowOverwriting:!0,cidVersion:i.version})).cid,r.cid=i,s=r}return i}const Fi=O.bind({ignoreUndefined:!0}),Hi={};async function*Ui(i,t,e={}){const n=Fi(Hi,e),s=await ce(i,n.path,t,n),r=await M(s.cid,t,n);if(r.type!=="file"&&r.type!=="raw")throw new _i;if(r.content==null)throw new Et;yield*r.content(n)}const le=262144,Oi=O.bind({ignoreUndefined:!0}),Mi=I("helia:unixfs:chmod"),Ri={recursive:!1,shardSplitThresholdBytes:le};async function Vi(i,t,e,n={}){const s=Oi(Ri,n),r=await ce(i,s.path,e,n);if(Mi("chmod %c %d",r.cid,t),s.recursive){const u=await re(async function*(){for await(const y of At(r.cid,e,n)){let m,w=[];if(y.type==="raw")m=new b({type:"file",data:y.node});else if(y.type==="file"||y.type==="directory")m=y.unixfs,w=y.node.Links;else throw new Be;m.mode=t;const p={Data:m.marshal(),Links:w};yield{path:y.path,content:p}}},y=>ae(y,e,{...s,dagBuilder:async function*(m,w){for await(const p of m)yield async function(){const g=p.content,k=_(g),B=await ne(k,w,{...s,cidVersion:i.version});if(g.Data==null)throw new H(`${B} had no data`);const T=b.unmarshal(g.Data);return{cid:B,size:BigInt(k.length),path:p.path,unixfs:T}}}}),async y=>_e(y));if(u==null)throw new Qe(`Could not chmod ${r.cid.toString()}`);return we(u.cid,r,e,s)}const a=await e.get(r.cid,n);let o,c=[];if(r.cid.code===R)o=new b({type:"file",data:a});else{const u=L(a);if(u.Data==null)throw new H(`${r.cid.toString()} had no data`);c=u.Links,o=b.unmarshal(u.Data)}o.mode=t;const l=_({Data:o.marshal(),Links:c}),h=await ee.digest(l),f=x.create(r.cid.version,U,h);return await e.put(f,l),we(f,r,e,s)}const Ki=O.bind({ignoreUndefined:!0}),Wi=I("helia:unixfs:cp"),Xi={force:!1,shardSplitThresholdBytes:le};async function Yi(i,t,e,n,s={}){const r=Ki(Xi,s);if(e.includes("/"))throw new te("Name must not have slashes");const[a,o]=await Promise.all([Se(t,n,r),et(i,e,n,r)]);return Wi('Adding %c as "%s" to %c',i,e,t),(await je(a,o,n,{allowOverwriting:r.force,cidVersion:t.version,...r})).cid}const Gi=O.bind({ignoreUndefined:!0}),Qi={};async function*Ji(i,t,e={}){const n=Gi(Qi,e),s=await ce(i,n.path,t,n),r=await M(s.cid,t);if(r.type==="file"||r.type==="raw"){yield r;return}if(r.content==null)throw new Et;if(r.type!=="directory")throw new Je;yield*r.content({offset:e.offset,length:e.length})}const Zi=O.bind({ignoreUndefined:!0}),ft=I("helia:unixfs:mkdir"),qi={cidVersion:1,force:!1,shardSplitThresholdBytes:le};async function ji(i,t,e,n={}){const s=Zi(qi,n);if(t.includes("/"))throw new te("Path must not have slashes");if((await M(i,e,n)).type!=="directory")throw new Je(`${i.toString()} was not a UnixFS directory`);ft("creating %s",t);const o={Data:new b({type:"directory",mode:s.mode,mtime:s.mtime}).marshal(),Links:[]},c=_(o),l=await ee.digest(c),h=x.create(s.cidVersion,U,l);await e.put(h,c);const[f,u]=await Promise.all([Se(i,e,s),et(h,t,e,s)]);return ft("adding empty dir called %s to %c",t,i),(await je(f,u,e,{...s,allowOverwriting:s.force})).cid}const he=I("helia:unixfs:utils:remove-link");async function es(i,t,e,n){if(i.node.Data==null)throw new H("Parent node had no data");if(b.unmarshal(i.node.Data).type==="hamt-sharded-directory"){he(`removing ${t} from sharded directory`);const r=await ns(i,t,e,n);return await Mt(r.node,e,n.shardSplitThresholdBytes,n)?r:(he("converting shard to flat directory %c",i.cid),is(r,e,n))}return he(`removing link ${t} regular directory`),ts(i,t,e,n)}const ts=async(i,t,e,n)=>{i.node.Links=i.node.Links.filter(a=>a.Name!==t);const s=_(i.node),r=await ne(s,e,{...n,cidVersion:i.cid.version});return he(`Updated regular directory ${r}`),{node:i.node,cid:r}},ns=async(i,t,e,n)=>{const{path:s}=await Ot(i.cid,t,e,n),r=s[s.length-1];if(r==null)throw new Error("Invalid HAMT, could not generate path");const a=r.node.Links.filter(l=>(l.Name??"").substring(2)===t).map(l=>l.Name).pop();if(a==null)throw new Error("File not found");const o=a.substring(0,2),c=parseInt(o,16);if(r.node.Links=r.node.Links.filter(l=>l.Name!==a),r.children.unset(c),r.node.Links.length===1)for(;s.length!==1;){const l=s[s.length-1];if(l==null||l.node.Links.length>1)break;s.pop();const h=s[s.length-1];if(h==null)break;const f=l.node.Links[0];h.node.Links=h.node.Links.filter(u=>!(u.Name??"").startsWith(h.prefix)),h.node.Links.push({Hash:f.Hash,Name:`${h.prefix}${(f.Name??"").substring(2)}`,Tsize:f.Tsize})}return Ut(s,e,n)},is=async(i,t,e)=>{if(i.node.Data==null)throw new te("Invalid parent passed to convertToFlatDirectory");const n={Links:[]},s=await M(i.cid,t);if(s.type!=="directory")throw new Error("Unexpected node type");for await(const c of s.content()){let l=0;c.node instanceof Uint8Array?l=c.node.byteLength:l=_(c.node).length,n.Links.push({Hash:c.cid,Name:c.name,Tsize:l})}const r=b.unmarshal(i.node.Data);n.Data=new b({type:"directory",mode:r.mode,mtime:r.mtime}).marshal();const a=_(N(n));return{cid:await ne(a,t,{codec:pe,cidVersion:i.cid.version,signal:e.signal}),node:n}},ss=O.bind({ignoreUndefined:!0}),rs=I("helia:unixfs:rm"),as={shardSplitThresholdBytes:le};async function os(i,t,e,n={}){const s=ss(as,n);if(t.includes("/"))throw new te("Name must not have slashes");const r=await Se(i,e,s);return rs("Removing %s from %c",t,i),(await es(r,t,e,{...s,cidVersion:i.version})).cid}const cs=O.bind({ignoreUndefined:!0}),ls=I("helia:unixfs:stat"),us={};async function ds(i,t,e={}){var w;const n=cs(us,e),s=await ce(i,e.path,t,n);ls("stat %c",s.cid);const r=await M(s.cid,t,n);if(r.type!=="file"&&r.type!=="directory"&&r.type!=="raw")throw new Be;let a=0n,o=0n,c=0n,l=0n,h=0,f,u;const y=r.type;let m;if(r.type==="raw"&&(a=BigInt(r.node.byteLength),o=BigInt(r.node.byteLength),c=BigInt(r.node.byteLength),l=BigInt(r.node.byteLength),h=1),r.type==="directory"&&(a=0n,o=BigInt(r.unixfs.marshal().byteLength),c=0n,l=o,h=1,f=r.unixfs.mode,u=r.unixfs.mtime,m=r.unixfs),r.type==="file"){const p=await Vt(s.cid,t,n);a=r.unixfs.fileSize(),o=BigInt((((w=r.node.Data)==null?void 0:w.byteLength)??0)+r.node.Links.reduce((g,k)=>g+(k.Tsize??0),0)),c=BigInt(p.localFileSize),l=BigInt(p.localDagSize),h=p.blocks,f=r.unixfs.mode,u=r.unixfs.mtime,m=r.unixfs}return{cid:s.cid,mode:f,mtime:u,fileSize:a,dagSize:o,localFileSize:c,localDagSize:l,blocks:h,type:y,unixfs:m}}async function Vt(i,t,e){const n={localFileSize:0,localDagSize:0,blocks:0};if(await t.has(i,e)){const s=await t.get(i,e);if(n.blocks++,n.localDagSize+=s.byteLength,i.code===R)n.localFileSize+=s.byteLength;else if(i.code===U){const r=L(s);if(r.Links.length>0)for(const a of r.Links){const o=await Vt(a.Hash,t,e);n.localFileSize+=o.localFileSize,n.localDagSize+=o.localDagSize,n.blocks+=o.blocks}else{if(r.Data==null)throw new H(`PBNode ${i.toString()} had no data`);const a=b.unmarshal(r.Data);if(a.data==null)throw new H(`UnixFS node ${i.toString()} had no data`);n.localFileSize+=a.data.byteLength??0}}else throw new Qe(`${i.toString()} was neither DAG_PB nor RAW`)}return n}const hs=O.bind({ignoreUndefined:!0}),fs=I("helia:unixfs:touch"),ys={recursive:!1,shardSplitThresholdBytes:le};async function ms(i,t,e={}){const n=hs(ys,e),s=await ce(i,n.path,t,n),r=n.mtime??{secs:BigInt(Math.round(Date.now()/1e3)),nsecs:0};if(fs("touch %c %o",s.cid,r),n.recursive){const u=await re(async function*(){for await(const y of At(s.cid,t)){let m,w;if(y.type==="raw")m=new b({data:y.node}),w=[];else if(y.type==="file"||y.type==="directory")m=y.unixfs,w=y.node.Links;else throw new Be;m.mtime=r;const p={Data:m.marshal(),Links:w};yield{path:y.path,content:p}}},y=>ae(y,t,{...n,dagBuilder:async function*(m,w){for await(const p of m)yield async function(){const g=p.content,k=_(g),B=await ne(k,w,{...n,cidVersion:i.version});if(g.Data==null)throw new H(`${B} had no data`);const T=b.unmarshal(g.Data);return{cid:B,size:BigInt(k.length),path:p.path,unixfs:T}}}}),async y=>_e(y));if(u==null)throw new Qe(`Could not chmod ${s.cid.toString()}`);return we(u.cid,s,t,n)}const a=await t.get(s.cid,e);let o,c=[];if(s.cid.code===R)o=new b({data:a});else{const u=L(a);if(c=u.Links,u.Data==null)throw new H(`${s.cid.toString()} had no data`);o=b.unmarshal(u.Data)}o.mtime=r;const l=_({Data:o.marshal(),Links:c}),h=await ee.digest(l),f=x.create(s.cid.version,U,h);return await t.put(f,l),we(f,s,t,n)}class gs{constructor(t){d(this,"components");this.components=t}async*addAll(t,e={}){yield*Jn(t,this.components.blockstore,e)}async addBytes(t,e={}){return Zn(t,this.components.blockstore,e)}async addByteStream(t,e={}){return qn(t,this.components.blockstore,e)}async addFile(t,e={}){return jn(t,this.components.blockstore,e)}async addDirectory(t={},e={}){return ei(t,this.components.blockstore,e)}async*cat(t,e={}){yield*Ui(t,this.components.blockstore,e)}async chmod(t,e,n={}){return Vi(t,e,this.components.blockstore,n)}async cp(t,e,n,s={}){return Yi(t,e,n,this.components.blockstore,s)}async*ls(t,e={}){yield*Ji(t,this.components.blockstore,e)}async mkdir(t,e,n={}){return ji(t,e,this.components.blockstore,n)}async rm(t,e,n={}){return os(t,e,this.components.blockstore,n)}async stat(t,e={}){return ds(t,this.components.blockstore,e)}async touch(t,e={}){return ms(t,this.components.blockstore,e)}}function vs(i){return new gs(i)}export{vs as unixfs};
|
|
//# sourceMappingURL=index-CBW3VqNG.js.map
|