2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-10-05 15:10:06 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-10-15 19:12:16 +02:00
|
|
|
const VERSION = "%%VERSION%%";
|
2020-10-02 19:54:24 +02:00
|
|
|
const GLOBAL_HASH = "%%GLOBAL_HASH%%";
|
|
|
|
const UNHASHED_PRECACHED_ASSETS = "%%UNHASHED_PRECACHED_ASSETS%%";
|
|
|
|
const HASHED_PRECACHED_ASSETS = "%%HASHED_PRECACHED_ASSETS%%";
|
|
|
|
const HASHED_CACHED_ON_REQUEST_ASSETS = "%%HASHED_CACHED_ON_REQUEST_ASSETS%%";
|
|
|
|
const unhashedCacheName = `hydrogen-assets-${GLOBAL_HASH}`;
|
|
|
|
const hashedCacheName = `hydrogen-assets`;
|
2020-10-20 17:28:42 +02:00
|
|
|
const mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`;
|
2019-09-15 14:32:12 +02:00
|
|
|
|
|
|
|
self.addEventListener('install', function(e) {
|
2020-10-02 19:54:24 +02:00
|
|
|
e.waitUntil((async () => {
|
|
|
|
const unhashedCache = await caches.open(unhashedCacheName);
|
|
|
|
await unhashedCache.addAll(UNHASHED_PRECACHED_ASSETS);
|
|
|
|
const hashedCache = await caches.open(hashedCacheName);
|
|
|
|
await Promise.all(HASHED_PRECACHED_ASSETS.map(async asset => {
|
|
|
|
if (!await hashedCache.match(asset)) {
|
|
|
|
await hashedCache.add(asset);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
})());
|
2019-09-15 14:32:12 +02:00
|
|
|
});
|
|
|
|
|
2020-10-02 19:54:24 +02:00
|
|
|
async function purgeOldCaches() {
|
|
|
|
// remove any caches we don't know about
|
|
|
|
const keyList = await caches.keys();
|
|
|
|
for (const key of keyList) {
|
2020-10-05 15:58:29 +02:00
|
|
|
if (key !== unhashedCacheName && key !== hashedCacheName && key !== mediaThumbnailCacheName) {
|
2020-10-02 19:54:24 +02:00
|
|
|
await caches.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the cache for any old hashed resource
|
|
|
|
const hashedCache = await caches.open(hashedCacheName);
|
|
|
|
const keys = await hashedCache.keys();
|
|
|
|
const hashedAssetURLs =
|
|
|
|
HASHED_PRECACHED_ASSETS
|
|
|
|
.concat(HASHED_CACHED_ON_REQUEST_ASSETS)
|
|
|
|
.map(a => new URL(a, self.registration.scope).href);
|
|
|
|
|
|
|
|
for (const request of keys) {
|
|
|
|
if (!hashedAssetURLs.some(url => url === request.url)) {
|
|
|
|
hashedCache.delete(request);
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
2020-10-02 19:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('activate', (event) => {
|
2020-10-16 14:52:34 +02:00
|
|
|
event.waitUntil(Promise.all([
|
|
|
|
purgeOldCaches(),
|
|
|
|
// on a first page load/sw install,
|
|
|
|
// start using the service worker on all pages straight away
|
|
|
|
self.clients.claim()
|
|
|
|
]));
|
2019-09-15 14:32:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (event) => {
|
2020-10-05 15:10:06 +02:00
|
|
|
event.respondWith(handleRequest(event.request));
|
2019-09-15 14:32:12 +02:00
|
|
|
});
|
2020-10-02 19:54:24 +02:00
|
|
|
|
2020-10-20 17:28:42 +02:00
|
|
|
function isCacheableThumbnail(url) {
|
|
|
|
if (url.pathname.startsWith("/_matrix/media/r0/thumbnail/")) {
|
|
|
|
const width = parseInt(url.searchParams.get("width"), 10);
|
|
|
|
const height = parseInt(url.searchParams.get("height"), 10);
|
|
|
|
if (width <= 50 && height <= 50) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-23 12:58:45 +02:00
|
|
|
const baseURL = new URL(self.registration.scope);
|
2020-10-02 19:54:24 +02:00
|
|
|
async function handleRequest(request) {
|
2020-11-05 22:51:21 +01:00
|
|
|
try {
|
|
|
|
const url = new URL(request.url);
|
|
|
|
if (url.origin === baseURL.origin && url.pathname === baseURL.pathname) {
|
|
|
|
request = new Request(new URL("index.html", baseURL.href));
|
|
|
|
}
|
|
|
|
let response = await readCache(request);
|
|
|
|
if (!response) {
|
|
|
|
// use cors so the resource in the cache isn't opaque and uses up to 7mb
|
|
|
|
// https://developers.google.com/web/tools/chrome-devtools/progressive-web-apps?utm_source=devtools#opaque-responses
|
|
|
|
if (isCacheableThumbnail(url)) {
|
|
|
|
response = await fetch(request, {mode: "cors", credentials: "omit"});
|
|
|
|
} else {
|
|
|
|
response = await fetch(request);
|
|
|
|
}
|
|
|
|
await updateCache(request, response);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
} catch (err) {
|
|
|
|
if (!(err instanceof TypeError)) {
|
|
|
|
console.error("error in service worker", err);
|
2020-10-20 17:28:42 +02:00
|
|
|
}
|
2020-11-05 22:51:21 +01:00
|
|
|
throw err;
|
2020-10-02 19:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateCache(request, response) {
|
2020-11-05 22:31:00 +01:00
|
|
|
// don't write error responses to the cache
|
|
|
|
if (response.status >= 400) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-05 15:58:29 +02:00
|
|
|
const url = new URL(request.url);
|
2020-10-02 19:54:24 +02:00
|
|
|
const baseURL = self.registration.scope;
|
2020-10-20 17:28:42 +02:00
|
|
|
if (isCacheableThumbnail(url)) {
|
|
|
|
const cache = await caches.open(mediaThumbnailCacheName);
|
|
|
|
cache.put(request, response.clone());
|
2020-10-05 15:58:29 +02:00
|
|
|
} else if (request.url.startsWith(baseURL)) {
|
|
|
|
let assetName = request.url.substr(baseURL.length);
|
|
|
|
if (HASHED_CACHED_ON_REQUEST_ASSETS.includes(assetName)) {
|
|
|
|
const cache = await caches.open(hashedCacheName);
|
|
|
|
await cache.put(request, response.clone());
|
|
|
|
}
|
2020-10-02 19:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readCache(request) {
|
|
|
|
const unhashedCache = await caches.open(unhashedCacheName);
|
|
|
|
let response = await unhashedCache.match(request);
|
|
|
|
if (response) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
const hashedCache = await caches.open(hashedCacheName);
|
|
|
|
response = await hashedCache.match(request);
|
2020-10-05 15:58:29 +02:00
|
|
|
if (response) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(request.url);
|
2020-10-20 17:28:42 +02:00
|
|
|
if (isCacheableThumbnail(url)) {
|
2020-10-05 15:58:29 +02:00
|
|
|
const mediaThumbnailCache = await caches.open(mediaThumbnailCacheName);
|
|
|
|
response = await mediaThumbnailCache.match(request);
|
2020-11-05 22:31:00 +01:00
|
|
|
// added in 0.1.26, remove previously cached error responses, remove this in some time
|
2020-11-05 22:45:20 +01:00
|
|
|
if (response?.status >= 400) {
|
2020-11-05 22:31:00 +01:00
|
|
|
await mediaThumbnailCache.delete(request);
|
|
|
|
response = null;
|
|
|
|
}
|
2020-10-05 15:58:29 +02:00
|
|
|
}
|
2020-10-02 19:54:24 +02:00
|
|
|
return response;
|
|
|
|
}
|
2020-10-16 12:50:19 +02:00
|
|
|
|
|
|
|
self.addEventListener('message', (event) => {
|
|
|
|
const reply = payload => event.source.postMessage({replyTo: event.data.id, payload});
|
|
|
|
const {replyTo} = event.data;
|
|
|
|
if (replyTo) {
|
|
|
|
const resolve = pendingReplies.get(replyTo);
|
|
|
|
if (resolve) {
|
|
|
|
pendingReplies.delete(replyTo);
|
|
|
|
resolve(event.data.payload);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (event.data?.type) {
|
|
|
|
case "version":
|
|
|
|
reply({version: VERSION, buildHash: GLOBAL_HASH});
|
|
|
|
break;
|
|
|
|
case "skipWaiting":
|
|
|
|
self.skipWaiting();
|
|
|
|
break;
|
|
|
|
case "closeSession":
|
|
|
|
event.waitUntil(
|
|
|
|
closeSession(event.data.payload.sessionId, event.source.id)
|
|
|
|
.then(() => reply())
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function closeSession(sessionId, requestingClientId) {
|
|
|
|
const clients = await self.clients.matchAll();
|
|
|
|
await Promise.all(clients.map(async client => {
|
|
|
|
if (client.id !== requestingClientId) {
|
|
|
|
await sendAndWaitForReply(client, "closeSession", {sessionId});
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingReplies = new Map();
|
|
|
|
let messageIdCounter = 0;
|
|
|
|
function sendAndWaitForReply(client, type, payload) {
|
|
|
|
messageIdCounter += 1;
|
|
|
|
const id = messageIdCounter;
|
|
|
|
const promise = new Promise(resolve => {
|
|
|
|
pendingReplies.set(id, resolve);
|
|
|
|
});
|
|
|
|
client.postMessage({type, id, payload});
|
|
|
|
return promise;
|
|
|
|
}
|