1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*
This file is part of keks-meet (https://codeberg.org/metamuffin/keks-meet)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin@disroot.org>
*/
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
import { handle_download_request, port_to_readable, streams } from "./download.ts";
import { SWMessage } from "./protocol.ts";
declare const self: ServiceWorkerGlobalScope; export { };
console.log("hello from the keks-meet service worker");
console.log(self.origin)
self.addEventListener("install", event => {
console.log("install");
self.skipWaiting()
event.waitUntil(caches.delete("v1"))
})
self.addEventListener("activate", _event => {
console.log("activate");
self.clients.claim()
})
self.addEventListener("unload", () => {
console.log("unload")
})
self.addEventListener("message", async ev => {
const message: SWMessage = ev.data;
console.log("incoming message", message);
if (message.download) {
const { path, size } = message.download, port = ev.ports[0]
const readable = port_to_readable(port)
streams.set(path, { readable, size })
}
if (message.check_version) {
broadcast_response(await check_for_updates())
}
if (message.update) {
broadcast_response(await update())
}
})
async function broadcast_response(message: SWMessage) {
const clients = await self.clients.matchAll({})
console.log(clients);
clients.forEach(c => c.postMessage(message))
}
self.addEventListener("fetch", async event => {
const { request } = event;
if (!request.url.startsWith(self.origin)) return
const path = request.url.substring(self.origin.length)
console.log(request.method, path);
if (path.startsWith("/download")) return handle_download_request(path, event)
if (path.startsWith("/signaling")) return event.respondWith(fetch(request))
if (path == "/swtest") return event.respondWith(new Response("works!", { headers: new Headers({ "content-type": "text/plain" }) }))
const cache = await caches.open("v1")
// const cached = await cache.match(request)
// if (cached) {
// console.log("-> cached");
// return cached
// }
console.log("-> forwarding to the server");
const response = await fetch(request);
cache.put(request, response.clone())
event.respondWith(response.clone())
})
async function update(): Promise<SWMessage> {
console.log("updating...");
await caches.delete("v1")
await Promise.all(
[
"/",
"/room",
"/config.json",
"/assets/bundle.js",
"/favicon.ico"
]
.map(cache_preload)
)
return { updated: true }
}
async function cache_preload(path: string) {
const cache = await caches.open("v1")
const req = new Request(path)
const res = await fetch(req)
await cache.put(req, res)
}
async function check_for_updates(): Promise<SWMessage> {
console.log("checking for updates");
const cache = await caches.open("v1")
const res = await fetch("/version")
const res2 = await cache.match(new Request("/version"));
const available_version = await res.text()
const installed_version = res2 ? await res2.text() : "none";
console.log({ available_version, installed_version });
return {
version_info: {
available_version,
installed_version
}
}
}
|