const c="http://127.0.0.1:5001";class n extends Error{constructor(a){super(f(a)),this.detail=a}}function f(t){switch(t.kind){case"unreachable":return`Kubo API at ${c} is unreachable: ${t.cause}. Is IPFS Desktop running?`;case"cors":return`Kubo rejected the request (CORS / Origin not allowed): ${t.cause}. Allow the extension origin in Kubo's API.HTTPHeaders.Access-Control-Allow-Origin.`;case"http":return`Kubo /api/v0/add returned ${t.status}: ${t.body}`;case"parse":return`Kubo /api/v0/add returned an unparseable response: ${t.body}`}}async function l(){let t;try{t=await fetch(`${c}/api/v0/version`,{method:"POST"})}catch(s){const e=s instanceof Error?s.message:String(s);return/cors|origin/i.test(e)?{ok:!1,kind:{kind:"cors",cause:e}}:{ok:!1,kind:{kind:"unreachable",cause:e}}}if(t.ok){let s;try{const e=await t.json();typeof e?.Version=="string"&&(s=e.Version)}catch{}return{ok:!0,version:s}}if(t.status===403||t.status===405)return{ok:!1,kind:{kind:"cors",cause:await t.text().catch(()=>"")||`HTTP ${t.status}`}};const a=await t.text().catch(()=>"");return{ok:!1,kind:{kind:"http",status:t.status,body:a.slice(0,512)}}}async function p(t,a={}){const s=new URLSearchParams({"cid-version":"1","raw-leaves":"true",pin:"true"});a.mfsPath&&s.set("to-files",a.mfsPath);const e=new FormData;e.append("file",new Blob([t]),"body");const o=`${c}/api/v0/add?${s.toString()}`;let r;try{r=await fetch(o,{method:"POST",body:e})}catch(d){const h=d instanceof Error?d.message:String(d);throw/cors|origin/i.test(h)?new n({kind:"cors",cause:h}):new n({kind:"unreachable",cause:h})}const i=await r.text();if(!r.ok)throw r.status===403||r.status===405?new n({kind:"cors",cause:i||`HTTP ${r.status}`}):new n({kind:"http",status:r.status,body:i.slice(0,512)});const w=i.trim().split(`
`).pop()??"";let u;try{u=JSON.parse(w)}catch{throw new n({kind:"parse",body:i.slice(0,512)})}if(!u.Hash)throw new n({kind:"parse",body:i.slice(0,512)});return{cid:u.Hash,size:Number(u.Size??t.byteLength)}}async function b(t){const a=`${c}/api/v0/pin/rm?arg=${encodeURIComponent(t)}`;let s;try{s=await fetch(a,{method:"POST"})}catch(o){const r=o instanceof Error?o.message:String(o);throw new n({kind:"unreachable",cause:r})}if(s.ok)return;const e=await s.text().catch(()=>"");if(!/not pinned/i.test(e))throw s.status===403||s.status===405?new n({kind:"cors",cause:e||`HTTP ${s.status}`}):new n({kind:"http",status:s.status,body:e.slice(0,512)})}async function k(t){const a=new URLSearchParams({arg:t,recursive:"true",force:"true"}),s=`${c}/api/v0/files/rm?${a.toString()}`;let e;try{e=await fetch(s,{method:"POST"})}catch(r){const i=r instanceof Error?r.message:String(r);throw new n({kind:"unreachable",cause:i})}if(e.ok)return;const o=await e.text().catch(()=>"");if(!/file does not exist/i.test(o))throw e.status===403||e.status===405?new n({kind:"cors",cause:o||`HTTP ${e.status}`}):new n({kind:"http",status:e.status,body:o.slice(0,512)})}export{n as K,p as a,l as p,k as r,b as u};
//# sourceMappingURL=chunk-BvQ1SBzo.js.map
