-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
2 lines (2 loc) · 3.28 KB
/
sw.js
1
2
const CACHE_NAME="KaitakuCDNCache";let cachelist=[],flag=!1;const cdn={gh:{jsdelivr:{url:"https://cdn.jsdelivr.net/gh"},jsdelivr_fastly:{url:"https://fastly.jsdelivr.net/gh"},jsdelivr_kaitaku:{url:"https://jsd.kaitaku.xyz/gh"}},cdnjs:{staticfile:{url:"https://cdn.staticfile.org"},cdnjs_cf:{url:"https://cdnjs.cloudflare.com/ajax/libs"},baomitu:{url:"https://lib.baomitu.com"},bytedance:{url:"https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M"}},npm:{sourcegcdn:{url:"https://npm.sourcegcdn.com"},unpkg:{url:"https://unpkg.com"}}},info=t=>{console.log(`%c INFO %c ${t}`,"color: white; background: #32cd32; padding: 5px 3px;","padding: 4px;border:1px solid #32cd32")},warn=t=>{console.log(`%c WARN %c ${t}`,"color: white; background: #ffd700; padding: 5px 3px;","padding: 4px;border:1px solid #ffd700")},err=t=>{console.log(`%c ERROR %c ${t}`,"color: white; background: #ff0000; padding: 5px 3px;","padding: 4px;border:1px solid #ff0000")},handleerr=async(t,n)=>new Response(`<h1>KaitakuCDNCache\u9047\u5230\u4E86\u81F4\u547D\u9519\u8BEF</h1>
<b>${n}</b>`,{headers:{"content-type":"text/html; charset=utf-8"}}),file_found=(t,n)=>t.indexOf(n)>-1,handle=async t=>{const n=t.url,s=t.url.split("/")[2];let a=[];if(file_found(n,"woff")||file_found(n,"ico"))return caches.match(t).then(function(e){return e||fetch(n).then(function(r){return caches.open(CACHE_NAME).then(function(l){return l.put(t,r.clone()),r})})});if(s.match("www.kaitaku.xyz"))return file_found(n,".jpg")?t.url.replace(".jpg",".webp"):file_found(n,".png")&&t.url.replace(".png",".webp"),fetch(t.url).then(function(e){if(!e)throw"error";return caches.open(CACHE_NAME).then(function(r){return r.delete(t),r.put(t,e.clone()),e})}).catch(function(e){return caches.match(t).then(function(r){return r||caches.match(new Request("/offline.html"))})});for(let e in cdn)for(let r in cdn[e])if(s===cdn[e][r].url.split("https://")[1].split("/")[0]&&n.match(cdn[e][r].url)){info(`capture ${n}`),a=[];for(let l in cdn[e])a.push(n.replace(cdn[e][r].url,cdn[e][l].url));return n.indexOf("@latest/")>-1?lfetch(a,n):caches.match(t).then(function(l){return l||lfetch(a,n).then(function(c){return caches.open(CACHE_NAME).then(function(i){return i.put(t,c.clone()),c})})})}return fetch(t)},lfetch=async(t,n)=>{info(`lfetch handle! | mirrors: ${t.length}`);let s=new AbortController;const a=async e=>new Response(await e.arrayBuffer(),{status:e.status,headers:e.headers});return Promise.any||(Promise.any=function(e){return new Promise((r,l)=>{e=Array.isArray(e)?e:[];let c=e.length,i=[];if(c===0)return l(new AggregateError("All promises were rejected"));e.forEach(u=>{u.then(o=>{r(o)},o=>{c--,i.push(o),c===0&&l(new AggregateError(i))})})})}),Promise.any(t.map(e=>new Promise((r,l)=>{fetch(e,{signal:s.signal}).then(a).then(async c=>{c.status===200?(s.abort(),r(c)):l(c)}).catch(async c=>{flag||(warn("fetch: "+c),flag=!0)})})))};self.addEventListener("install",async t=>{self.skipWaiting(),t.waitUntil(caches.open(CACHE_NAME).then(n=>(info(`${CACHE_NAME} Opened`),info(`${CACHE_NAME} start running`),n.addAll(cachelist))))}),self.addEventListener("activate",async function(t){self.clients.claim()}),self.addEventListener("fetch",async t=>{if(!(t.request.url.indexOf("cravatar")!==-1||t.request.url.indexOf("qweather")!==-1))try{t.respondWith(handle(t.request))}catch(n){t.respondWith(handleerr(t.request,n))}});