d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 1 | import { AVATAR_CACHE, handleMediaFetch } from './sw-media'; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 2 | |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 3 | declare const __MALICE_DEVICE__: boolean; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | // Prepended by vite.config.ts's precacheManifestPlugin: every emitted chunk, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | // asset, and the wasm, as root-relative URLs. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | const sw = self as unknown as ServiceWorkerGlobalScope & { __MALICE_PRECACHE__: string[] }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | const CACHE_NAME = 'malice-shell-v1'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | sw.addEventListener('install', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | event.waitUntil( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | (async () => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | const cache = await caches.open(CACHE_NAME); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | await cache.addAll(['/', '/index.html', ...sw.__MALICE_PRECACHE__]); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | })() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | ); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 18 | }); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 19 | |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 20 | sw.addEventListener('message', (event) => { |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 21 | if (event.data?.type === 'SKIP_WAITING') void sw.skipWaiting(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | sw.addEventListener('activate', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | event.waitUntil( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | (async () => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | const keys = await caches.keys(); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 28 | await Promise.all(keys.filter((key) => key !== CACHE_NAME && key !== AVATAR_CACHE).map((key) => caches.delete(key))); |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 29 | const shell = await caches.open(CACHE_NAME); |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 30 | const keep = new Set(['/', '/index.html', ...sw.__MALICE_PRECACHE__]); |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 31 | for (const request of await shell.keys()) { |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 32 | const path = new URL(request.url).pathname; |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 33 | if (path.startsWith('/assets/') && !keep.has(path)) await shell.delete(request); |
919753ab134c | atagen | 2026-10-26 03:48 UTC | 34 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | await sw.clients.claim(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | })() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | async function cacheFirst(request: Request): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | const cached = await caches.match(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | if (cached) return cached; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | const response = await fetch(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | if (response.ok) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | const cache = await caches.open(CACHE_NAME); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | void cache.put(request, response.clone()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | return response; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | async function networkFirstNavigation(request: Request): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | return await fetch(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | const cached = await caches.match('/index.html'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | if (cached) return cached; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | throw new Error('offline and no cached shell'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | sw.addEventListener('fetch', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | const url = new URL(event.request.url); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 64 | if (__MALICE_DEVICE__ && handleMediaFetch(event)) return; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 65 | if (url.pathname.startsWith('/api/')) return; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 66 | if (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/twemoji/')) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | event.respondWith(cacheFirst(event.request)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | if (event.request.mode === 'navigate') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | event.respondWith(networkFirstNavigation(event.request)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | }); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 74 | |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 75 | sw.addEventListener('notificationclick', (event) => { |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 76 | event.notification.close(); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 77 | const roomId = (event.notification.data as { roomId?: unknown } | null)?.roomId; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 78 | if (typeof roomId !== 'string') return; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 79 | event.waitUntil( |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 80 | (async () => { |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 81 | const windows = await sw.clients.matchAll({ type: 'window', includeUncontrolled: true }); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 82 | const existing = windows.find((client) => client.visibilityState === 'visible') ?? windows[0]; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 83 | if (!existing) { |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 84 | await sw.clients.openWindow(`/?room=${encodeURIComponent(roomId)}`); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 85 | return; |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 86 | } |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 87 | existing.postMessage({ type: 'malice-open-room', roomId }); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 88 | await existing.focus(); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 89 | })() |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 90 | ); |
d8772d4eb408 | atagen | 2026-10-26 04:43 UTC | 91 | }); |