+import{createHash as d}from"node:crypto";import c from"fs-extra";import{homedir as g}from"node:os";import l from"node:path";import W from"@hattip/polyfills/node-fetch";import u from"path";class ${#e=l.join(g(),"cache","fetch");#t(i){const r=d("sha512");r.update(i);const a=r.digest("base64");return l.join(this.#e,a)}async set(i,r){const a=this.#t(i);return await c.ensureFile(a),await c.writeFile(a,r,{encoding:"utf-8"})}async get(i){const r=this.#t(i);if(c.exists(r))return await c.ensureFile(r),await c.readFile(r,"utf-8")}}function p(s){return s?.startsWith("http://")||s?.startsWith("https://")}const m=new Map,x=function({cache:s=m,onRequest:i,onUseCache:r,fetcher:a,resolveIdFallback:h}={}){return W(),{name:"rollup-plugin-http-resolve",async resolveId(t,n){if(/^https?:\/\//.test(t))return t;if(n&&p(n)){if(t.startsWith("https://"))return t;const{pathname:e,protocol:o,host:f}=new URL(n);if(t.startsWith("/"))return`${o}//${f}${t}`;if(t.startsWith(".")){const w=u.join(u.dirname(e),t);return`${o}//${f}${w}`}}else if(h){const e=h(t,n);if(e)return e}},async load(t){if(t!==null&&p(t)){const n=await s.get(t);if(n)return r?.(t),n;if(i?.(t),a){const e=await a(t);return await s.set(t,e),e}else{const e=await fetch(t,{headers:{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54"}});if(!e.ok)throw e.statusText;const o=await e.text();return await s.set(t,o),o}}}}};export{m as defaultCache,$ as fileCache,x as httpResolve};
0 commit comments