q78kgblog / js /sw-toolbox.js
Q78KG's picture
Site updated: 2024-10-23 23:23:31
779c5fe
(function(u){if(typeof exports=="object"&&typeof module<"u")module.exports=u();else if(typeof define=="function"&&define.amd)define([],u);else{var l;l=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:this,l.toolbox=u()}})(function(){return function u(l,$,m){function g(c,e){if(!$[c]){if(!l[c]){var d=typeof require=="function"&&require;if(!e&&d)return d(c,!0);if(p)return p(c,!0);var n=new Error("Cannot find module '"+c+"'");throw n.code="MODULE_NOT_FOUND",n}var o=$[c]={exports:{}};l[c][0].call(o.exports,function(f){var y=l[c][1][f];return g(y||f)},o,o.exports,u,l,$,m)}return $[c].exports}for(var p=typeof require=="function"&&require,h=0;h<m.length;h++)g(m[h]);return g}({1:[function(u,l,$){"use strict";function m(s,a){a=a||{};var t=a.debug||v.debug;t&&console.log("[sw-toolbox] "+s)}function g(s){var a;return s&&s.cache&&(a=s.cache.name),a=a||v.cache.name,caches.open(a)}function p(s,a){a=a||{};var t=a.successResponses||v.successResponses;return fetch(s.clone()).then(function(r){return s.method==="GET"&&t.test(r.status)&&g(a).then(function(i){i.put(s,r).then(function(){var x=a.cache||v.cache;(x.maxEntries||x.maxAgeSeconds)&&x.name&&h(s,i,x)})}),r.clone()})}function h(s,a,t){var r=c.bind(null,s,a,t);k=k?k.then(r):r()}function c(s,a,t){var r=s.url,i=t.maxAgeSeconds,x=t.maxEntries,S=t.name,w=Date.now();return m("Updating LRU order for "+r+". Max entries is "+x+", max age is "+i),R.getDb(S).then(function(b){return R.setTimestampForUrl(b,r,w)}).then(function(b){return R.expireEntries(b,x,i,w)}).then(function(b){m("Successfully updated IDB.");var C=b.map(function(T){return a.delete(T)});return Promise.all(C).then(function(){m("Done with cache cleanup.")})}).catch(function(b){m(b)})}function e(s,a,t){return m("Renaming cache: ["+s+"] to ["+a+"]",t),caches.delete(a).then(function(){return Promise.all([caches.open(s),caches.open(a)]).then(function(r){var i=r[0],x=r[1];return i.keys().then(function(S){return Promise.all(S.map(function(w){return i.match(w).then(function(b){return x.put(w,b)})}))}).then(function(){return caches.delete(s)})})})}function d(s,a){return g(a).then(function(t){return t.add(s)})}function n(s,a){return g(a).then(function(t){return t.delete(s)})}function o(s){s instanceof Promise||f(s),v.preCacheItems=v.preCacheItems.concat(s)}function f(s){var a=Array.isArray(s);if(a&&s.forEach(function(t){typeof t=="string"||t instanceof Request||(a=!1)}),!a)throw new TypeError("The precache method expects either an array of strings and/or Requests or a Promise that resolves to an array of strings and/or Requests.");return s}function y(s,a,t){if(!s)return!1;if(a){var r=s.headers.get("date");if(r){var i=new Date(r);if(i.getTime()+1e3*a<t)return!1}}return!0}var k,v=u("./options"),R=u("./idb-cache-expiration");l.exports={debug:m,fetchAndCache:p,openCache:g,renameCache:e,cache:d,uncache:n,precache:o,validatePrecacheInput:f,isResponseFresh:y}},{"./idb-cache-expiration":2,"./options":4}],2:[function(u,l,$){"use strict";function m(v){return new Promise(function(R,s){var a=indexedDB.open(d+v,n);a.onupgradeneeded=function(){var t=a.result.createObjectStore(o,{keyPath:f});t.createIndex(y,y,{unique:!1})},a.onsuccess=function(){R(a.result)},a.onerror=function(){s(a.error)}})}function g(v){return v in k||(k[v]=m(v)),k[v]}function p(v,R,s){return new Promise(function(a,t){var r=v.transaction(o,"readwrite"),i=r.objectStore(o);i.put({url:R,timestamp:s}),r.oncomplete=function(){a(v)},r.onabort=function(){t(r.error)}})}function h(v,R,s){return R?new Promise(function(a,t){var r=1e3*R,i=[],x=v.transaction(o,"readwrite"),S=x.objectStore(o),w=S.index(y);w.openCursor().onsuccess=function(b){var C=b.target.result;if(C&&s-r>C.value[y]){var T=C.value[f];i.push(T),S.delete(T),C.continue()}},x.oncomplete=function(){a(i)},x.onabort=t}):Promise.resolve([])}function c(v,R){return R?new Promise(function(s,a){var t=[],r=v.transaction(o,"readwrite"),i=r.objectStore(o),x=i.index(y),S=x.count();x.count().onsuccess=function(){var w=S.result;w>R&&(x.openCursor().onsuccess=function(b){var C=b.target.result;if(C){var T=C.value[f];t.push(T),i.delete(T),w-t.length>R&&C.continue()}})},r.oncomplete=function(){s(t)},r.onabort=a}):Promise.resolve([])}function e(v,R,s,a){return h(v,s,a).then(function(t){return c(v,R).then(function(r){return t.concat(r)})})}var d="sw-toolbox-",n=1,o="store",f="url",y="timestamp",k={};l.exports={getDb:g,setTimestampForUrl:p,expireEntries:e}},{}],3:[function(u,l,$){"use strict";function m(n){var o=e.match(n.request);o?n.respondWith(o(n.request)):e.default&&n.request.method==="GET"&&n.request.url.indexOf("http")===0&&n.respondWith(e.default(n.request))}function g(n){c.debug("activate event fired");var o=d.cache.name+"$$$inactive$$$";n.waitUntil(c.renameCache(o,d.cache.name))}function p(n){return n.reduce(function(o,f){return o.concat(f)},[])}function h(n){var o=d.cache.name+"$$$inactive$$$";c.debug("install event fired"),c.debug("creating cache ["+o+"]"),n.waitUntil(c.openCache({cache:{name:o}}).then(function(f){return Promise.all(d.preCacheItems).then(p).then(c.validatePrecacheInput).then(function(y){return c.debug("preCache list: "+(y.join(", ")||"(none)")),f.addAll(y)})}))}u("serviceworker-cache-polyfill");var c=u("./helpers"),e=u("./router"),d=u("./options");l.exports={fetchListener:m,activateListener:g,installListener:h}},{"./helpers":1,"./options":4,"./router":6,"serviceworker-cache-polyfill":16}],4:[function(u,l,$){"use strict";var m;m=self.registration?self.registration.scope:self.scope||new URL("./",self.location).href,l.exports={cache:{name:"$$$toolbox-cache$$$"+m+"$$$",maxAgeSeconds:null,maxEntries:null},debug:!1,networkTimeoutSeconds:null,preCacheItems:[],successResponses:/^0|([123]\d\d)|(40[14567])|410$/}},{}],5:[function(u,l,$){"use strict";var m=new URL("./",self.location),g=m.pathname,p=u("path-to-regexp"),h=function(c,e,d,n){e instanceof RegExp?this.fullUrlRegExp=e:(e.indexOf("/")!==0&&(e=g+e),this.keys=[],this.regexp=p(e,this.keys)),this.method=c,this.options=n,this.handler=d};h.prototype.makeHandler=function(c){var e;if(this.regexp){var d=this.regexp.exec(c);e={},this.keys.forEach(function(n,o){e[n.name]=d[o+1]})}return function(n){return this.handler(n,e,this.options)}.bind(this)},l.exports=h},{"path-to-regexp":15}],6:[function(u,l,$){"use strict";function m(e){return e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}var g=u("./route"),p=u("./helpers"),h=function(e,d){for(var n=e.entries(),o=n.next(),f=[];!o.done;){var y=new RegExp(o.value[0]);y.test(d)&&f.push(o.value[1]),o=n.next()}return f},c=function(){this.routes=new Map,this.routes.set(RegExp,new Map),this.default=null};["get","post","put","delete","head","any"].forEach(function(e){c.prototype[e]=function(d,n,o){return this.add(e,d,n,o)}}),c.prototype.add=function(e,d,n,o){o=o||{};var f;d instanceof RegExp?f=RegExp:(f=o.origin||self.location.origin,f=f instanceof RegExp?f.source:m(f)),e=e.toLowerCase();var y=new g(e,d,n,o);this.routes.has(f)||this.routes.set(f,new Map);var k=this.routes.get(f);k.has(e)||k.set(e,new Map);var v=k.get(e),R=y.regexp||y.fullUrlRegExp;v.has(R.source)&&p.debug('"'+d+'" resolves to same regex as existing route.'),v.set(R.source,y)},c.prototype.matchMethod=function(e,d){var n=new URL(d),o=n.origin,f=n.pathname;return this._match(e,h(this.routes,o),f)||this._match(e,[this.routes.get(RegExp)],d)},c.prototype._match=function(e,d,n){if(d.length===0)return null;for(var o=0;o<d.length;o++){var f=d[o],y=f&&f.get(e.toLowerCase());if(y){var k=h(y,n);if(k.length>0)return k[0].makeHandler(n)}}return null},c.prototype.match=function(e){return this.matchMethod(e.method,e.url)||this.matchMethod("any",e.url)},l.exports=new c},{"./helpers":1,"./route":5}],7:[function(u,l,$){"use strict";function m(h,c,e){return e=e||{},p.debug("Strategy: cache first ["+h.url+"]",e),p.openCache(e).then(function(d){return d.match(h).then(function(n){var o=e.cache||g.cache,f=Date.now();return p.isResponseFresh(n,o.maxAgeSeconds,f)?n:p.fetchAndCache(h,e)})})}var g=u("../options"),p=u("../helpers");l.exports=m},{"../helpers":1,"../options":4}],8:[function(u,l,$){"use strict";function m(h,c,e){return e=e||{},p.debug("Strategy: cache only ["+h.url+"]",e),p.openCache(e).then(function(d){return d.match(h).then(function(n){var o=e.cache||g.cache,f=Date.now();if(p.isResponseFresh(n,o.maxAgeSeconds,f))return n})})}var g=u("../options"),p=u("../helpers");l.exports=m},{"../helpers":1,"../options":4}],9:[function(u,l,$){"use strict";function m(h,c,e){return g.debug("Strategy: fastest ["+h.url+"]",e),new Promise(function(d,n){var o=!1,f=[],y=function(v){f.push(v.toString()),o?n(new Error('Both cache and network failed: "'+f.join('", "')+'"')):o=!0},k=function(v){v instanceof Response?d(v):y("No result returned")};g.fetchAndCache(h.clone(),e).then(k,y),p(h,c,e).then(k,y)})}var g=u("../helpers"),p=u("./cacheOnly");l.exports=m},{"../helpers":1,"./cacheOnly":8}],10:[function(u,l,$){l.exports={networkOnly:u("./networkOnly"),networkFirst:u("./networkFirst"),cacheOnly:u("./cacheOnly"),cacheFirst:u("./cacheFirst"),fastest:u("./fastest")}},{"./cacheFirst":7,"./cacheOnly":8,"./fastest":9,"./networkFirst":11,"./networkOnly":12}],11:[function(u,l,$){"use strict";function m(h,c,e){e=e||{};var d=e.successResponses||g.successResponses,n=e.networkTimeoutSeconds||g.networkTimeoutSeconds;return p.debug("Strategy: network first ["+h.url+"]",e),p.openCache(e).then(function(o){var f,y,k=[];if(n){var v=new Promise(function(s){f=setTimeout(function(){o.match(h).then(function(a){var t=e.cache||g.cache,r=Date.now(),i=t.maxAgeSeconds;p.isResponseFresh(a,i,r)&&s(a)})},1e3*n)});k.push(v)}var R=p.fetchAndCache(h,e).then(function(s){if(f&&clearTimeout(f),d.test(s.status))return s;throw p.debug("Response was an HTTP error: "+s.statusText,e),y=s,new Error("Bad response")}).catch(function(s){return p.debug("Network or response error, fallback to cache ["+h.url+"]",e),o.match(h).then(function(a){if(a)return a;if(y)return y;throw s})});return k.push(R),Promise.race(k)})}var g=u("../options"),p=u("../helpers");l.exports=m},{"../helpers":1,"../options":4}],12:[function(u,l,$){"use strict";function m(p,h,c){return g.debug("Strategy: network only ["+p.url+"]",c),fetch(p)}var g=u("../helpers");l.exports=m},{"../helpers":1}],13:[function(u,l,$){"use strict";var m=u("./options"),g=u("./router"),p=u("./helpers"),h=u("./strategies"),c=u("./listeners");p.debug("Service Worker Toolbox is loading"),self.addEventListener("install",c.installListener),self.addEventListener("activate",c.activateListener),self.addEventListener("fetch",c.fetchListener),l.exports={networkOnly:h.networkOnly,networkFirst:h.networkFirst,cacheOnly:h.cacheOnly,cacheFirst:h.cacheFirst,fastest:h.fastest,router:g,options:m,cache:p.cache,uncache:p.uncache,precache:p.precache}},{"./helpers":1,"./listeners":3,"./options":4,"./router":6,"./strategies":10}],14:[function(u,l,$){l.exports=Array.isArray||function(m){return Object.prototype.toString.call(m)=="[object Array]"}},{}],15:[function(u,l,$){function m(t,r){for(var i,x=[],S=0,w=0,b="",C=r&&r.delimiter||"/";(i=a.exec(t))!=null;){var T=i[0],A=i[1],E=i.index;if(b+=t.slice(w,E),w=E+T.length,A)b+=A[1];else{var O=t[w],P=i[2],F=i[3],j=i[4],D=i[5],U=i[6],L=i[7];b&&(x.push(b),b="");var M=P!=null&&O!=null&&O!==P,N=U==="+"||U==="*",_=U==="?"||U==="*",q=i[2]||C,I=j||D;x.push({name:F||S++,prefix:P||"",delimiter:q,optional:_,repeat:N,partial:M,asterisk:!!L,pattern:I?d(I):L?".*":"[^"+e(q)+"]+?"})}}return w<t.length&&(b+=t.substr(w)),b&&x.push(b),x}function g(t,r){return c(m(t,r))}function p(t){return encodeURI(t).replace(/[\/?#]/g,function(r){return"%"+r.charCodeAt(0).toString(16).toUpperCase()})}function h(t){return encodeURI(t).replace(/[?#]/g,function(r){return"%"+r.charCodeAt(0).toString(16).toUpperCase()})}function c(t){for(var r=new Array(t.length),i=0;i<t.length;i++)typeof t[i]=="object"&&(r[i]=new RegExp("^(?:"+t[i].pattern+")$"));return function(x,S){for(var w="",b=x||{},C=S||{},T=C.pretty?p:encodeURIComponent,A=0;A<t.length;A++){var E=t[A];if(typeof E!="string"){var O,P=b[E.name];if(P==null){if(E.optional){E.partial&&(w+=E.prefix);continue}throw new TypeError('Expected "'+E.name+'" to be defined')}if(s(P)){if(!E.repeat)throw new TypeError('Expected "'+E.name+'" to not repeat, but received `'+JSON.stringify(P)+"`");if(P.length===0){if(E.optional)continue;throw new TypeError('Expected "'+E.name+'" to not be empty')}for(var F=0;F<P.length;F++){if(O=T(P[F]),!r[A].test(O))throw new TypeError('Expected all "'+E.name+'" to match "'+E.pattern+'", but received `'+JSON.stringify(O)+"`");w+=(F===0?E.prefix:E.delimiter)+O}}else{if(O=E.asterisk?h(P):T(P),!r[A].test(O))throw new TypeError('Expected "'+E.name+'" to match "'+E.pattern+'", but received "'+O+'"');w+=E.prefix+O}}else w+=E}return w}}function e(t){return t.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function d(t){return t.replace(/([=!:$\/()])/g,"\\$1")}function n(t,r){return t.keys=r,t}function o(t){return t.sensitive?"":"i"}function f(t,r){var i=t.source.match(/\((?!\?)/g);if(i)for(var x=0;x<i.length;x++)r.push({name:x,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,asterisk:!1,pattern:null});return n(t,r)}function y(t,r,i){for(var x=[],S=0;S<t.length;S++)x.push(R(t[S],r,i).source);var w=new RegExp("(?:"+x.join("|")+")",o(i));return n(w,r)}function k(t,r,i){return v(m(t,i),r,i)}function v(t,r,i){s(r)||(i=r||i,r=[]),i=i||{};for(var x=i.strict,S=i.end!==!1,w="",b=0;b<t.length;b++){var C=t[b];if(typeof C=="string")w+=e(C);else{var T=e(C.prefix),A="(?:"+C.pattern+")";r.push(C),C.repeat&&(A+="(?:"+T+A+")*"),A=C.optional?C.partial?T+"("+A+")?":"(?:"+T+"("+A+"))?":T+"("+A+")",w+=A}}var E=e(i.delimiter||"/"),O=w.slice(-E.length)===E;return x||(w=(O?w.slice(0,-E.length):w)+"(?:"+E+"(?=$))?"),w+=S?"$":x&&O?"":"(?="+E+"|$)",n(new RegExp("^"+w,o(i)),r)}function R(t,r,i){return s(r)||(i=r||i,r=[]),i=i||{},t instanceof RegExp?f(t,r):s(t)?y(t,r,i):k(t,r,i)}var s=u("isarray");l.exports=R,l.exports.parse=m,l.exports.compile=g,l.exports.tokensToFunction=c,l.exports.tokensToRegExp=v;var a=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g")},{isarray:14}],16:[function(u,l,$){(function(){var m=Cache.prototype.addAll,g=navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/);if(g)var p=g[1],h=parseInt(g[2]);m&&(!g||p==="Firefox"&&h>=46||p==="Chrome"&&h>=50)||(Cache.prototype.addAll=function(c){function e(n){this.name="NetworkError",this.code=19,this.message=n}var d=this;return e.prototype=Object.create(Error.prototype),Promise.resolve().then(function(){if(arguments.length<1)throw new TypeError;return c=c.map(function(n){return n instanceof Request?n:String(n)}),Promise.all(c.map(function(n){typeof n=="string"&&(n=new Request(n));var o=new URL(n.url).protocol;if(o!=="http:"&&o!=="https:")throw new e("Invalid scheme");return fetch(n.clone())}))}).then(function(n){if(n.some(function(o){return!o.ok}))throw new e("Incorrect response status");return Promise.all(n.map(function(o,f){return d.put(c[f],o)}))}).then(function(){})},Cache.prototype.add=function(c){return this.addAll([c])})})()},{}]},{},[13])(13)});