1/* eslint no-var: off */ 2/* eslint no-console: off */ 3/* eslint no-redeclare: off */ 4/* global fetch createImageBitmap self caches*/ 5var hostReplacer = {} 6function checkReplacer() { 7 return fetch("/.sw_/_host_/_replacer_?" + Date.now()) 8 .then(function(res) {return res.json()}) 9 .then(function(r) {hostReplacer = r}) 10 .catch(function(error) {console.error(error)}) 11} 12setInterval(checkReplacer, 60000) 13 14function replaceHost(host) { 15 var arr = host.split(":") 16 for (var key in hostReplacer) { 17 if (key == arr[0]) return hostReplacer[key] + (arr[1] ? ":" + arr[1] : "") 18 } 19 return host 20} 21 22var supportsWebp = false 23 24function noCache(resp) { 25 resp.__nocache = true 26 return resp 27} 28 29function fetchOptimized(url, host) { 30 var matches = (url.protocol + "//" + host + url.pathname).match( 31 /^(https?:\/\/[^/]+\/res\/[\da-z]+\/[\da-f]{24}_optimized)(?:[^./]+)(\..+)?$/ 32 ) 33 if (!matches) return fetch(url.protocol + "//" + host + url.pathname + url.search) 34 35 return fetch(url.protocol + "//" + host + url.pathname + url.search) 36 .catch(function() {return {status: 404}}) 37 .then(function(resp) { 38 if (resp.status == 404 || resp.status == 403) { 39 return fetch(matches[1] + (matches[2] || '')).then(function(resp) { 40 if (resp.status == 404 || resp.status == 403) { 41 return fetch(matches[1]) 42 } 43 return resp 44 }).then(noCache) 45 } 46 return resp 47 }) 48} 49 50var matchWebpApi = /^https?:\/\/[^/]+\/res\/[\da-z]+\/[\da-f]{24}[^/]*$/ 51 52function fetchWithCache(event, _fetch) { 53 event.respondWith( 54 caches.match(event.request, {ignoreSearch: true}).then(function(result) { 55 if (result && result.ok) return result 56 return _fetch.then(function(resp) { 57 if (resp.status == 200 && !resp.__nocache) { 58 return caches.open("webp-cache").then(function(cache) { 59 cache.put(event.request, resp.clone()) 60 return resp 61 }) 62 } 63 return resp 64 }) 65 }) 66 ) 67} 68 69function processResource(event, url, host) { 70 if (event.request.method != "GET") return 71 72 if (url.search.indexOf("nowebp") >= 0) { 73 return event.respondWith(fetchOptimized(url, host)) 74 } 75 if (!supportsWebp || url.pathname.match(/\.webp$/)) { 76 return fetchWithCache(event, fetchOptimized(url, host)) 77 } 78 79 return fetchWithCache(event, fetch(
80 url.protocol + "//" + host + url.pathname + ".webp" + url.search, 81 ).catch(function(error) { 82 console.error(error) 83 return {status: 666} 84 }).then(function(resp) { 85 if (resp.status == 200) return resp 86 if (resp.status == 404) { 87 var ref = encodeURIComponent(url.pathname.slice(1)) 88 if ((url.protocol + "//" + host + url.pathname).match(matchWebpApi)) { 89 fetch( 90 url.protocol + "//api.weblium.com/api/resource/webp/check?ref=" + ref, 91 {method: "POST", redirect: 'follow'} 92 ).catch(function(error) {console.error(error)}) 93 } 94 } 95 return fetchOptimized(url, host).then(noCache) 96 })) 97} 98 99var matchWebp = /^https?:\/\/[^/]+\/(?:res\/[\da-z]+\/[\da-f]{24}[^/]*|[^/]+\/res\/[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}(?:\/[^/]+)?)$/ 100 101self.addEventListener("fetch", function(event) { 102 var url = new URL(event.request.url) 103 var host = replaceHost(url.host) 104 if (!(url.protocol + "//" + url.host + url.pathname).match(matchWebp)) { 105 if (host != url.host && event.request.method != "GET") {
106 return fetch(url.protocol + "//" + host + url.pathname + url.search) 107 } 108 return 109 } 110 return processResource(event, url, host) 111}) 112 113self.addEventListener("install", function(event) { 114 if (!this.createImageBitmap) { 115 return event.waitUntil(self.skipWaiting()) 116 } 117 var webpData = "data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=" 118 event.waitUntil( 119 checkReplacer().then(function() { 120 return fetch(webpData).then(function(r) {return r.blob()}) 121 }).then(function(blob) { 122 return createImageBitmap(blob).then(function() {supportsWebp = true}) 123 }).catch(function() {}).then(function() {self.skipWaiting()})) 124}) 125 126// self.addEventListener("activate", function() { 127// clients.claim().then(function() { 128// clients.matchAll().then(function(clients) { 129// clients.forEach(function(client) { 130// client.postMessage("active") 131// }) 132// }) 133// }) 134// }) 135
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.