const d="localhost",O=8080,y="bafkqaaa",w={host:d,port:8080};function g(t){if(typeof t!="string")return null;const r=t.trim();if(!r)return null;let s=r,e;if(/^[a-z][a-z0-9+.-]*:\/\//i.test(r))try{const n=new URL(r);if(n.protocol!=="http:"||n.username||n.password||n.pathname&&n.pathname!=="/"||n.search||n.hash)return null;s=n.hostname;const i=r.match(/^http:\/\/[^/?#]*:(\d+)(?:\/)?$/i)?.[1];if(i||n.port){const c=m(i??n.port);if(!c)return null;e=c}}catch{return null}const a=s.toLowerCase().replace(/\.$/,"");return!a||/[/:?#@\[\]\\]/.test(a)||!/^[a-z0-9.-]+$/.test(a)||a.startsWith(".")||a.endsWith(".")||a.includes("..")?null:{host:a,port:e}}function b(t){return g(t)?.host??null}function m(t){const r=typeof t=="string"&&t.trim()?Number(t.trim()):typeof t=="number"?t:NaN;return!Number.isInteger(r)||r<1||r>65535?null:r}function _(t){return{host:b(t?.ipfsGatewayHost)??d,port:m(t?.ipfsGatewayPort)??8080}}function k(t,r,s="/",e="",a="",n=w){const i=t==="web3"?"ipfs":t,u=`http://${i==="ipns"?T(r):r}.${i}.${n.host}:${n.port}`,l=s.startsWith("/")?s:`/${s}`;return`${u}${l}${e}${a}`}function P(t=w){return k("ipfs",y,"/","","",t)}function v(t){return[`http://*.ipfs.${t.host}/*`,`http://*.ipns.${t.host}/*`]}function T(t){return t.replace(/-/g,"--").replace(/\./g,"-")}function $(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function I(t,r=d){const s=b(r);if(!s)return null;const e=t.match(new RegExp(`^(.+)\\.(ipfs|ipns)\\.${$(s)}(?::\\d+)?$`,"i"));return!e||!e[1]||!e[2]?null:{kind:e[2],label:e[1]}}const S=2500,A=3e4,p="http://127.0.0.1:5001";let h=null;async function x(t=w,r={}){const s=P(t);if(!r.force&&h&&h.probeUrl===s&&Date.now()-h.checkedAt<A)return h.reachable;const e=new AbortController,a=setTimeout(()=>e.abort(),r.timeoutMs??S);try{return await fetch(s,{mode:"no-cors",cache:"no-store",signal:e.signal}),h={reachable:!0,checkedAt:Date.now(),probeUrl:s},!0}catch{return h={reachable:!1,checkedAt:Date.now(),probeUrl:s},!1}finally{clearTimeout(a)}}function K(){h=null}class o extends Error{constructor(r){super(E(r)),this.detail=r}}function E(t){switch(t.kind){case"unreachable":return`Kubo API at ${p} 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 ${t.op??"/api/v0/add"} returned ${t.status}: ${t.body}`;case"parse":return`Kubo ${t.op??"/api/v0/add"} returned an unparseable response: ${t.body}`}}async function U(){let t;try{t=await fetch(`${p}/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 r=await t.text().catch(()=>"");return{ok:!1,kind:{kind:"http",status:t.status,body:r.slice(0,512)}}}async function G(t,r={}){const s=new URLSearchParams({"cid-version":"1","raw-leaves":"true",pin:"true"});r.mfsPath&&s.set("to-files",r.mfsPath);const e=new FormData;e.append("file",new Blob([t]),"body");const a=`${p}/api/v0/add?${s.toString()}`;let n;try{n=await fetch(a,{method:"POST",body:e})}catch(l){const f=l instanceof Error?l.message:String(l);throw/cors|origin/i.test(f)?new o({kind:"cors",cause:f}):new o({kind:"unreachable",cause:f})}const i=await n.text();if(!n.ok)throw n.status===403||n.status===405?new o({kind:"cors",cause:i||`HTTP ${n.status}`}):new o({kind:"http",status:n.status,body:i.slice(0,512)});const c=i.trim().split(`
`).pop()??"";let u;try{u=JSON.parse(c)}catch{throw new o({kind:"parse",body:i.slice(0,512)})}if(!u.Hash)throw new o({kind:"parse",body:i.slice(0,512)});return{cid:u.Hash,size:Number(u.Size??t.byteLength)}}async function H(t){const r=new URLSearchParams({arg:t,recursive:"true"}),s=`${p}/api/v0/pin/add?${r.toString()}`;let e;try{e=await fetch(s,{method:"POST"})}catch(n){const i=n instanceof Error?n.message:String(n);throw/cors|origin/i.test(i)?new o({kind:"cors",cause:i}):new o({kind:"unreachable",cause:i})}if(e.ok)return;const a=await e.text().catch(()=>"");throw e.status===403||e.status===405?new o({kind:"cors",cause:a||`HTTP ${e.status}`}):new o({kind:"http",op:"/api/v0/pin/add",status:e.status,body:a.slice(0,512)})}async function F(t){const r=new URLSearchParams({arg:t,type:"all"}),s=`${p}/api/v0/pin/ls?${r.toString()}`;let e;try{e=await fetch(s,{method:"POST"})}catch(u){const l=u instanceof Error?u.message:String(u);throw/cors|origin/i.test(l)?new o({kind:"cors",cause:l}):new o({kind:"unreachable",cause:l})}const a=await e.text().catch(()=>"");if(!e.ok){if(/not pinned|is not pinned|not found/i.test(a))return null;throw e.status===403||e.status===405?new o({kind:"cors",cause:a||`HTTP ${e.status}`}):new o({kind:"http",op:"/api/v0/pin/ls",status:e.status,body:a.slice(0,512)})}let n;try{n=JSON.parse(a)}catch{throw new o({kind:"parse",op:"/api/v0/pin/ls",body:a.slice(0,512)})}const c=(n.Keys?.[t]??Object.values(n.Keys??{})[0])?.Type;return c==="recursive"||c==="direct"||c==="indirect"||c==="internal"?c:null}async function L(t){const r=new URLSearchParams({arg:t,progress:"false"}),s=`${p}/api/v0/dag/stat?${r.toString()}`;let e;try{e=await fetch(s,{method:"POST"})}catch(u){const l=u instanceof Error?u.message:String(u);throw/cors|origin/i.test(l)?new o({kind:"cors",cause:l}):new o({kind:"unreachable",cause:l})}const a=await e.text().catch(()=>"");if(!e.ok)throw e.status===403||e.status===405?new o({kind:"cors",cause:a||`HTTP ${e.status}`}):new o({kind:"http",op:"/api/v0/dag/stat",status:e.status,body:a.slice(0,512)});let n;try{n=JSON.parse(a)}catch{throw new o({kind:"parse",op:"/api/v0/dag/stat",body:a.slice(0,512)})}const i=n.TotalSize??n.Size;if(i==null)return null;const c=Number(i);return Number.isFinite(c)?c:null}async function z(t){const r=`${p}/api/v0/pin/rm?arg=${encodeURIComponent(t)}`;let s;try{s=await fetch(r,{method:"POST"})}catch(a){const n=a instanceof Error?a.message:String(a);throw new o({kind:"unreachable",cause:n})}if(s.ok)return;const e=await s.text().catch(()=>"");if(!/not pinned/i.test(e))throw s.status===403||s.status===405?new o({kind:"cors",cause:e||`HTTP ${s.status}`}):new o({kind:"http",status:s.status,body:e.slice(0,512)})}async function R(t){const r=new URLSearchParams({arg:t,recursive:"true",force:"true"}),s=`${p}/api/v0/files/rm?${r.toString()}`;let e;try{e=await fetch(s,{method:"POST"})}catch(n){const i=n instanceof Error?n.message:String(n);throw new o({kind:"unreachable",cause:i})}if(e.ok)return;const a=await e.text().catch(()=>"");if(!/file does not exist/i.test(a))throw e.status===403||e.status===405?new o({kind:"cors",cause:a||`HTTP ${e.status}`}):new o({kind:"http",status:e.status,body:a.slice(0,512)})}export{d as D,o as K,x as a,g as b,P as c,K as d,G as e,T as f,_ as g,I as h,v as i,F as j,k,H as l,O as m,m as n,U as p,R as r,L as s,z as u};
//# sourceMappingURL=chunk-CMnPkCrZ.js.map
