Upstream: Yes Reason: Without this patch certain tests are failing as these files are dependencies for tests Url: https://github.com/h2o/h2o/issues/2329 --- /dev/null +++ b/misc/cache-digest/cache-digest.js @@ -0,0 +1,247 @@ +/* + * Copyright (c) 2015,2016 Jxck, DeNA Co., Ltd., Kazuho Oku + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * + * Includes a minified SHA256 implementation taken from https://gist.github.com/kazuho/bb8aab1a2946bbf42127d8a6197ad18c, + * licensed under the following copyright: + * + * Copyright (c) 2015,2016 Chen Yi-Cyuan, Kazuho Oku + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +"use strict"; + +if (typeof self !== "undefined" && "ServiceWorkerGlobalScope" in self && + self instanceof ServiceWorkerGlobalScope) { + + /* ServiceWorker */ + self.addEventListener('fetch', function(evt) { + var req = evt.request.clone(); + if (req.method != "GET" || req.url.match(/\/cache-digests?\.js(?:\?|$)/)) { + logInfo(req, "skip"); + return; + } + evt.respondWith(caches.open("v1").then(function (cache) { + return cache.match(req).then(function (res) { + if (res && isFresh(res.headers.entries(), Date.now())) { + logInfo(req, "hit"); + return res; + } + var requestWithDigests = function (digests) { + if (digests != null) { + var err = null; + try { + req = new Request(req); + req.headers.append("cache-digest", digests); + if (req.headers.get("cache-digest") == null) + err = "append failed"; + } catch (e) { + err = e; + } + if (err) + logError(req, e); + } + return fetch(req).then(function (res) { + var cached = false; + if (res.status == 200 && isFresh(res.headers.entries(), Date.now())) { + cache.put(req, res.clone()); + cached = true; + } + logInfo(req, "fetched" + (cached ? " & cached" : "") + " with cache-digest:\"" + digests + "\""); + return res; + }); + }; + if (req.mode == "navigate") { + return generateCacheDigests(cache).then(requestWithDigests); + } else { + return requestWithDigests(null); + } + }); + })); + }); + +} else if (typeof navigator !== "undefined") { + + /* bootstrap, loaded via