TDoG-Skin/public/sw.js
2024-08-17 19:13:54 +08:00

1 line
14 KiB
JavaScript
Executable File

!function(){"use strict";var e={913:function(){try{self["workbox:core:5.1.3"]&&_()}catch(e){}},550:function(){try{self["workbox:expiration:5.1.3"]&&_()}catch(e){}},80:function(){try{self["workbox:routing:5.1.3"]&&_()}catch(e){}},873:function(){try{self["workbox:strategies:5.1.3"]&&_()}catch(e){}}},t={};function s(n){var a=t[n];if(void 0!==a)return a.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,s),i.exports}!function(){s(913);class e extends Error{constructor(e,t){super(((e,...t)=>{let s=e;return t.length>0&&(s+=` :: ${JSON.stringify(t)}`),s})(e,t)),this.name=e,this.details=t}}s(80);const t=e=>e&&"object"==typeof e?e:{handle:e};class n{constructor(e,s,n="GET"){this.handler=t(s),this.match=e,this.method=n}}class a extends n{constructor(e,t,s){super((({url:t})=>{const s=e.exec(t.href);if(s&&(t.origin===location.origin||0===s.index))return s.slice(1)}),t,s)}}class i{constructor(){this._routes=new Map}get routes(){return this._routes}addFetchListener(){self.addEventListener("fetch",(e=>{const{request:t}=e,s=this.handleRequest({request:t,event:e});s&&e.respondWith(s)}))}addCacheListener(){self.addEventListener("message",(e=>{if(e.data&&"CACHE_URLS"===e.data.type){const{payload:t}=e.data,s=Promise.all(t.urlsToCache.map((e=>{"string"==typeof e&&(e=[e]);const t=new Request(...e);return this.handleRequest({request:t})})));e.waitUntil(s),e.ports&&e.ports[0]&&s.then((()=>e.ports[0].postMessage(!0)))}}))}handleRequest({request:e,event:t}){const s=new URL(e.url,location.href);if(!s.protocol.startsWith("http"))return;const{params:n,route:a}=this.findMatchingRoute({url:s,request:e,event:t});let i,r=a&&a.handler;if(!r&&this._defaultHandler&&(r=this._defaultHandler),r){try{i=r.handle({url:s,request:e,event:t,params:n})}catch(e){i=Promise.reject(e)}return i instanceof Promise&&this._catchHandler&&(i=i.catch((n=>this._catchHandler.handle({url:s,request:e,event:t})))),i}}findMatchingRoute({url:e,request:t,event:s}){const n=this._routes.get(t.method)||[];for(const a of n){let n;const i=a.match({url:e,request:t,event:s});if(i)return n=i,(Array.isArray(i)&&0===i.length||i.constructor===Object&&0===Object.keys(i).length||"boolean"==typeof i)&&(n=void 0),{route:a,params:n}}return{}}setDefaultHandler(e){this._defaultHandler=t(e)}setCatchHandler(e){this._catchHandler=t(e)}registerRoute(e){this._routes.has(e.method)||this._routes.set(e.method,[]),this._routes.get(e.method).push(e)}unregisterRoute(t){if(!this._routes.has(t.method))throw new e("unregister-route-but-not-found-with-method",{method:t.method});const s=this._routes.get(t.method).indexOf(t);if(!(s>-1))throw new e("unregister-route-route-not-registered");this._routes.get(t.method).splice(s,1)}}let r;function o(t,s,o){let c;if("string"==typeof t){const e=new URL(t,location.href);c=new n((({url:t})=>t.href===e.href),s,o)}else if(t instanceof RegExp)c=new a(t,s,o);else if("function"==typeof t)c=new n(t,s,o);else{if(!(t instanceof n))throw new e("unsupported-route-type",{moduleName:"workbox-routing",funcName:"registerRoute",paramName:"capture"});c=t}return(r||(r=new i,r.addFetchListener(),r.addCacheListener()),r).registerRoute(c),c}const c={googleAnalytics:"googleAnalytics",precache:"precache-v2",prefix:"workbox",runtime:"runtime",suffix:"undefined"!=typeof registration?registration.scope:""},h=e=>{return e||(t=c.runtime,[c.prefix,t,c.suffix].filter((e=>e&&e.length>0)).join("-"));var t},u=new Set,l=(e,t)=>e.filter((e=>t in e)),p=async({request:e,mode:t,plugins:s=[]})=>{const n=l(s,"cacheKeyWillBeUsed");let a=e;for(const e of n)a=await e.cacheKeyWillBeUsed.call(e,{mode:t,request:a}),"string"==typeof a&&(a=new Request(a));return a},d=async({cacheName:e,request:t,event:s,matchOptions:n,plugins:a=[]})=>{const i=await self.caches.open(e),r=await p({plugins:a,request:t,mode:"read"});let o=await i.match(r,n);for(const t of a)if("cachedResponseWillBeUsed"in t){const a=t.cachedResponseWillBeUsed;o=await a.call(t,{cacheName:e,event:s,matchOptions:n,cachedResponse:o,request:r})}return o},m=async({cacheName:t,request:s,response:n,event:a,plugins:i=[],matchOptions:r})=>{const o=await p({plugins:i,request:s,mode:"write"});if(!n)throw new e("cache-put-with-no-response",{url:(c=o.url,new URL(String(c),location.href).href.replace(new RegExp(`^${location.origin}`),""))});var c;const h=await(async({request:e,response:t,event:s,plugins:n=[]})=>{let a=t,i=!1;for(const t of n)if("cacheWillUpdate"in t){i=!0;const n=t.cacheWillUpdate;if(a=await n.call(t,{request:e,response:a,event:s}),!a)break}return i||(a=a&&200===a.status?a:void 0),a||null})({event:a,plugins:i,response:n,request:o});if(!h)return;const m=await self.caches.open(t),g=l(i,"cacheDidUpdate"),f=g.length>0?await d({cacheName:t,matchOptions:r,request:o}):null;try{await m.put(o,h)}catch(e){throw"QuotaExceededError"===e.name&&await async function(){for(const e of u)await e()}(),e}for(const e of g)await e.cacheDidUpdate.call(e,{cacheName:t,event:a,oldResponse:f,newResponse:h,request:o})},g=d,f=async({request:t,fetchOptions:s,event:n,plugins:a=[]})=>{if("string"==typeof t&&(t=new Request(t)),n instanceof FetchEvent&&n.preloadResponse){const e=await n.preloadResponse;if(e)return e}const i=l(a,"fetchDidFail"),r=i.length>0?t.clone():null;try{for(const e of a)if("requestWillFetch"in e){const s=e.requestWillFetch,a=t.clone();t=await s.call(e,{request:a,event:n})}}catch(t){throw new e("plugin-error-request-will-fetch",{thrownError:t})}const o=t.clone();try{let e;e="navigate"===t.mode?await fetch(t):await fetch(t,s);for(const t of a)"fetchDidSucceed"in t&&(e=await t.fetchDidSucceed.call(t,{event:n,request:o,response:e}));return e}catch(e){for(const t of i)await t.fetchDidFail.call(t,{error:e,event:n,originalRequest:r.clone(),request:o.clone()});throw e}};s(873);class w{constructor(e={}){this._cacheName=h(e.cacheName),this._plugins=e.plugins||[],this._fetchOptions=e.fetchOptions,this._matchOptions=e.matchOptions}async handle({event:t,request:s}){"string"==typeof s&&(s=new Request(s));let n,a=await g({cacheName:this._cacheName,request:s,event:t,matchOptions:this._matchOptions,plugins:this._plugins});if(a);else try{a=await this._getFromNetwork(s,t)}catch(e){n=e}if(!a)throw new e("no-response",{url:s.url,error:n});return a}async _getFromNetwork(e,t){const s=await f({request:e,event:t,fetchOptions:this._fetchOptions,plugins:this._plugins}),n=s.clone(),a=m({cacheName:this._cacheName,request:e,response:n,event:t,plugins:this._plugins});if(t)try{t.waitUntil(a)}catch(e){}return s}}const _={cacheWillUpdate:async({response:e})=>200===e.status||0===e.status?e:null};class y{constructor(e={}){if(this._cacheName=h(e.cacheName),this._plugins=e.plugins||[],e.plugins){const t=e.plugins.some((e=>!!e.cacheWillUpdate));this._plugins=t?e.plugins:[_,...e.plugins]}else this._plugins=[_];this._fetchOptions=e.fetchOptions,this._matchOptions=e.matchOptions}async handle({event:t,request:s}){"string"==typeof s&&(s=new Request(s));const n=this._getFromNetwork({request:s,event:t});let a,i=await g({cacheName:this._cacheName,request:s,event:t,matchOptions:this._matchOptions,plugins:this._plugins});if(i){if(t)try{t.waitUntil(n)}catch(a){}}else try{i=await n}catch(e){a=e}if(!i)throw new e("no-response",{url:s.url,error:a});return i}async _getFromNetwork({request:e,event:t}){const s=await f({request:e,event:t,fetchOptions:this._fetchOptions,plugins:this._plugins}),n=m({cacheName:this._cacheName,request:e,response:s.clone(),event:t,plugins:this._plugins});if(t)try{t.waitUntil(n)}catch(e){}return s}}function x(e){e.then((()=>{}))}class v{constructor(e,t,{onupgradeneeded:s,onversionchange:n}={}){this._db=null,this._name=e,this._version=t,this._onupgradeneeded=s,this._onversionchange=n||(()=>this.close())}get db(){return this._db}async open(){if(!this._db)return this._db=await new Promise(((e,t)=>{let s=!1;setTimeout((()=>{s=!0,t(new Error("The open request was blocked and timed out"))}),this.OPEN_TIMEOUT);const n=indexedDB.open(this._name,this._version);n.onerror=()=>t(n.error),n.onupgradeneeded=e=>{s?(n.transaction.abort(),n.result.close()):"function"==typeof this._onupgradeneeded&&this._onupgradeneeded(e)},n.onsuccess=()=>{const t=n.result;s?t.close():(t.onversionchange=this._onversionchange.bind(this),e(t))}})),this}async getKey(e,t){return(await this.getAllKeys(e,t,1))[0]}async getAll(e,t,s){return await this.getAllMatching(e,{query:t,count:s})}async getAllKeys(e,t,s){return(await this.getAllMatching(e,{query:t,count:s,includeKeys:!0})).map((e=>e.key))}async getAllMatching(e,{index:t,query:s=null,direction:n="next",count:a,includeKeys:i=!1}={}){return await this.transaction([e],"readonly",((r,o)=>{const c=r.objectStore(e),h=t?c.index(t):c,u=[],l=h.openCursor(s,n);l.onsuccess=()=>{const e=l.result;e?(u.push(i?e:e.value),a&&u.length>=a?o(u):e.continue()):o(u)}}))}async transaction(e,t,s){return await this.open(),await new Promise(((n,a)=>{const i=this._db.transaction(e,t);i.onabort=()=>a(i.error),i.oncomplete=()=>n(),s(i,(e=>n(e)))}))}async _call(e,t,s,...n){return await this.transaction([t],s,((s,a)=>{const i=s.objectStore(t),r=i[e].apply(i,n);r.onsuccess=()=>a(r.result)}))}close(){this._db&&(this._db.close(),this._db=null)}}v.prototype.OPEN_TIMEOUT=2e3;const q={readonly:["get","count","getKey","getAll","getAllKeys"],readwrite:["add","put","clear","delete"]};for(const[e,t]of Object.entries(q))for(const s of t)s in IDBObjectStore.prototype&&(v.prototype[s]=async function(t,...n){return await this._call(s,t,e,...n)});s(550);const N="cache-entries",O=e=>{const t=new URL(e,location.href);return t.hash="",t.href};class E{constructor(e){this._cacheName=e,this._db=new v("workbox-expiration",1,{onupgradeneeded:e=>this._handleUpgrade(e)})}_handleUpgrade(e){const t=e.target.result.createObjectStore(N,{keyPath:"id"});t.createIndex("cacheName","cacheName",{unique:!1}),t.createIndex("timestamp","timestamp",{unique:!1}),(async e=>{await new Promise(((t,s)=>{const n=indexedDB.deleteDatabase(e);n.onerror=()=>{s(n.error)},n.onblocked=()=>{s(new Error("Delete blocked"))},n.onsuccess=()=>{t()}}))})(this._cacheName)}async setTimestamp(e,t){const s={url:e=O(e),timestamp:t,cacheName:this._cacheName,id:this._getId(e)};await this._db.put(N,s)}async getTimestamp(e){return(await this._db.get(N,this._getId(e))).timestamp}async expireEntries(e,t){const s=await this._db.transaction(N,"readwrite",((s,n)=>{const a=s.objectStore(N).index("timestamp").openCursor(null,"prev"),i=[];let r=0;a.onsuccess=()=>{const s=a.result;if(s){const n=s.value;n.cacheName===this._cacheName&&(e&&n.timestamp<e||t&&r>=t?i.push(s.value):r++),s.continue()}else n(i)}})),n=[];for(const e of s)await this._db.delete(N,e.id),n.push(e.url);return n}_getId(e){return this._cacheName+"|"+O(e)}}class R{constructor(e,t={}){this._isRunning=!1,this._rerunRequested=!1,this._maxEntries=t.maxEntries,this._maxAgeSeconds=t.maxAgeSeconds,this._cacheName=e,this._timestampModel=new E(e)}async expireEntries(){if(this._isRunning)return void(this._rerunRequested=!0);this._isRunning=!0;const e=this._maxAgeSeconds?Date.now()-1e3*this._maxAgeSeconds:0,t=await this._timestampModel.expireEntries(e,this._maxEntries),s=await self.caches.open(this._cacheName);for(const e of t)await s.delete(e);this._isRunning=!1,this._rerunRequested&&(this._rerunRequested=!1,x(this.expireEntries()))}async updateTimestamp(e){await this._timestampModel.setTimestamp(e,Date.now())}async isURLExpired(e){return!!this._maxAgeSeconds&&await this._timestampModel.getTimestamp(e)<Date.now()-1e3*this._maxAgeSeconds}async delete(){this._rerunRequested=!1,await this._timestampModel.expireEntries(1/0)}}class b{constructor(e={}){var t;this.cachedResponseWillBeUsed=async({event:e,request:t,cacheName:s,cachedResponse:n})=>{if(!n)return null;const a=this._isResponseDateFresh(n),i=this._getCacheExpiration(s);x(i.expireEntries());const r=i.updateTimestamp(t.url);if(e)try{e.waitUntil(r)}catch(e){}return a?n:null},this.cacheDidUpdate=async({cacheName:e,request:t})=>{const s=this._getCacheExpiration(e);await s.updateTimestamp(t.url),await s.expireEntries()},this._config=e,this._maxAgeSeconds=e.maxAgeSeconds,this._cacheExpirations=new Map,e.purgeOnQuotaError&&(t=()=>this.deleteCacheAndMetadata(),u.add(t))}_getCacheExpiration(t){if(t===h())throw new e("expire-custom-caches-only");let s=this._cacheExpirations.get(t);return s||(s=new R(t,this._config),this._cacheExpirations.set(t,s)),s}_isResponseDateFresh(e){if(!this._maxAgeSeconds)return!0;const t=this._getDateHeaderTimestamp(e);return null===t||t>=Date.now()-1e3*this._maxAgeSeconds}_getDateHeaderTimestamp(e){if(!e.headers.has("date"))return null;const t=e.headers.get("date"),s=new Date(t).getTime();return isNaN(s)?null:s}async deleteCacheAndMetadata(){for(const[e,t]of this._cacheExpirations)await self.caches.delete(e),await t.delete();this._cacheExpirations=new Map}}const A=604800;o(/\/preview\/\d+/,new w({cacheName:"texture-preview-v2",fetchOptions:{credentials:"omit"},plugins:[new b({maxAgeSeconds:A,purgeOnQuotaError:!0})]})),o(/\/app\/.*\.webp/,new y({cacheName:"webp-resource-v1",fetchOptions:{credentials:"omit"}})),o(/\/avatar\/\d+/,new w({cacheName:"avatar-v2",fetchOptions:{credentials:"omit"},plugins:[new b({maxAgeSeconds:A})]})),o(/.+\/app\/\w{2,3}\.\w{7}\.js$/,new w({cacheName:"javascript-v1",fetchOptions:{credentials:"omit",mode:"cors"},plugins:[new b({maxAgeSeconds:A,purgeOnQuotaError:!0})]})),o(/.+\/plugins\/.+\.js$/,new y({cacheName:"javascript-v1",fetchOptions:{credentials:"omit",mode:"cors"},plugins:[new b({maxAgeSeconds:A,purgeOnQuotaError:!0})]})),o(/.+\/app\/.*\.css$/,new w({cacheName:"stylesheet-v1",fetchOptions:{credentials:"omit",mode:"cors"},plugins:[new b({maxAgeSeconds:A,purgeOnQuotaError:!0})]})),o(/.+\/plugins\/.+\.css$/,new y({cacheName:"stylesheet-v1",fetchOptions:{credentials:"omit",mode:"cors"},plugins:[new b({maxAgeSeconds:A,purgeOnQuotaError:!0})]})),o((({request:e})=>"font"===e.destination),new y({cacheName:"font-v1",fetchOptions:{credentials:"omit",mode:"cors"},plugins:[new b({maxEntries:12})]}))}()}();