973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | import { statusForKind } from './lib/wire-status'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | import { decodeAttachmentIv, importAttachmentKey, RangeDecryptor } from './lib/media-decrypt'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | import { attachmentContentType } from './lib/media-sniff'; |
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'; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 10 | const AVATAR_CACHE = 'malice-avatars-v2'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | sw.addEventListener('install', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | event.waitUntil( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | (async () => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | const cache = await caches.open(CACHE_NAME); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | await cache.addAll(['/', '/index.html', ...sw.__MALICE_PRECACHE__]); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | })() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | void sw.skipWaiting(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | sw.addEventListener('activate', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | event.waitUntil( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | (async () => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | const keys = await caches.keys(); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 26 | await Promise.all(keys.filter((key) => key !== CACHE_NAME && key !== AVATAR_CACHE).map((key) => caches.delete(key))); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | await sw.clients.claim(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | })() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | interface MediaReply { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 33 | bytes?: Uint8Array<ArrayBuffer>; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | content_type?: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | error?: { kind: string; message: string }; |
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 | interface AttachmentEncryptionWire { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | key: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | iv: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | sha256: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | interface MediaFileWire { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | url: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | mimetype: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | declared_size: number | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | encryption: AttachmentEncryptionWire | null; |
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 | type MediaLocationWire = { File: MediaFileWire } | { ServerThumbnail: { url: string; fallback: MediaFileWire } } | 'Local'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | interface LocateReply { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | location?: MediaLocationWire; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | token?: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | error?: { kind: string; message: string }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | const MEDIA_TIMEOUT_MS = 30000; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | const mediaChannel = new BroadcastChannel('malice-media'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | const pendingMedia = new Map<string, (reply: MediaReply & LocateReply) => void>(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | let nextMediaRequestId = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | mediaChannel.onmessage = (ev: MessageEvent) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | const msg = ev.data as MediaReply & LocateReply & { id: string }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | const resolve = pendingMedia.get(msg.id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | if (!resolve) return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | pendingMedia.delete(msg.id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | resolve(msg); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | function requestMedia(kind: 'avatar' | 'media', fields: Record<string, string>): Promise<MediaReply | null> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | const id = `${Date.now()}-${nextMediaRequestId++}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | return new Promise((resolve) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | const timer = setTimeout(() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | pendingMedia.delete(id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | resolve(null); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | }, MEDIA_TIMEOUT_MS); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | pendingMedia.set(id, (reply) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | clearTimeout(timer); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | resolve(reply); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | mediaChannel.postMessage({ id, kind, ...fields }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | interface LocateEntry { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | location: MediaLocationWire; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | token: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | expiresAt: number; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | const LOCATE_TTL_MS = 5 * 60 * 1000; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | const locateCache = new Map<string, LocateEntry>(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | const sniffCache = new Map<string, { contentType: string | null; expiresAt: number }>(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | function requestLocate(mediaId: string, size: string): Promise<{ location: MediaLocationWire; token: string | null } | null> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | const id = `${Date.now()}-${nextMediaRequestId++}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | return new Promise((resolve) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | const timer = setTimeout(() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | pendingMedia.delete(id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | resolve(null); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | }, MEDIA_TIMEOUT_MS); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | pendingMedia.set(id, (reply) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | clearTimeout(timer); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | resolve(reply.error || !reply.location ? null : { location: reply.location, token: reply.token ?? null }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | mediaChannel.postMessage({ id, kind: 'locate', media_id: mediaId, size }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | /** In-memory only: the access token never touches Cache Storage or IndexedDB, and expires with the entry. */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 113 | async function locate(mediaId: string, size: string, force: boolean): Promise<LocateEntry | null> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 114 | if (!force) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 115 | const cached = locateCache.get(mediaId); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 116 | if (cached && cached.expiresAt > Date.now()) return cached; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | const reply = await requestLocate(mediaId, size); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | if (!reply) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | const entry: LocateEntry = { ...reply, expiresAt: Date.now() + LOCATE_TTL_MS }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | locateCache.set(mediaId, entry); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | return entry; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | interface RangeSlice { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 126 | body: Uint8Array<ArrayBuffer>; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | status: number; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | contentRange?: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | /** The engine hands back the whole attachment at once, so seeking in <video>/<audio> needs this SW to slice it. */ |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 132 | function sliceForRange(bytes: Uint8Array<ArrayBuffer>, rangeHeader: string | null): RangeSlice { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | const match = rangeHeader ? /^bytes=(\d*)-(\d*)$/.exec(rangeHeader) : null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | if (!match) return { body: bytes, status: 200 }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | const total = bytes.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 136 | let start = match[1] ? parseInt(match[1], 10) : undefined; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | let end = match[2] ? parseInt(match[2], 10) : undefined; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | if (start === undefined && end !== undefined) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | start = Math.max(total - end, 0); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | end = total - 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | } else if (end === undefined) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | end = total - 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 143 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | if (start === undefined || start > end || start >= total) return { body: bytes, status: 200 }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 145 | end = Math.min(end, total - 1); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | return { body: bytes.subarray(start, end + 1), status: 206, contentRange: `bytes ${start}-${end}/${total}` }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | function mediaResponse(reply: MediaReply | null, rangeHeader: string | null, attachment: boolean): Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | if (!reply) return new Response(null, { status: 504 }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | if (reply.error) return new Response(reply.error.message, { status: statusForKind(reply.error.kind) }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | const bytes = reply.bytes ?? new Uint8Array(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | const { body, status, contentRange } = sliceForRange(bytes, rangeHeader); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | const headers = new Headers({ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | 'Content-Type': reply.content_type ?? 'application/octet-stream', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | 'Cache-Control': 'private, max-age=3600', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 157 | 'X-Content-Type-Options': 'nosniff' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | if (attachment) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 160 | headers.set('Content-Disposition', reply.content_type ? 'inline' : 'attachment'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 161 | headers.set('Content-Security-Policy', 'sandbox'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 162 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 163 | if (contentRange) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 164 | headers.set('Content-Range', contentRange); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 165 | headers.set('Accept-Ranges', 'bytes'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 166 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 167 | return new Response(body, { status, headers }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 168 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 169 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 170 | interface ByteRange { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 171 | start: number; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 172 | end: number; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 173 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 174 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 175 | /** Unlike sliceForRange, the total comes from the server's declared_size, not a buffer already in hand. */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 176 | function parseByteRange(rangeHeader: string | null, total: number): ByteRange | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 177 | const match = rangeHeader ? /^bytes=(\d*)-(\d*)$/.exec(rangeHeader) : null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 178 | if (!match) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 179 | let start = match[1] ? parseInt(match[1], 10) : undefined; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 180 | let end = match[2] ? parseInt(match[2], 10) : undefined; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 181 | if (start === undefined && end !== undefined) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 182 | start = Math.max(total - end, 0); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 183 | end = total - 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 184 | } else if (end === undefined) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 185 | start = start ?? 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 186 | end = total - 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 187 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 188 | if (start === undefined || start > end || start >= total) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 189 | return { start, end: Math.min(end, total - 1) }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 190 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 191 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 192 | function attachmentHeaders(contentType: string | null): Headers { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 193 | return new Headers({ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 194 | 'Content-Type': contentType ?? 'application/octet-stream', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 195 | 'Cache-Control': 'private, max-age=3600', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 196 | 'X-Content-Type-Options': 'nosniff', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 197 | 'Content-Disposition': contentType ? 'inline' : 'attachment', |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 198 | 'Content-Security-Policy': 'sandbox' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 199 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 200 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 201 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 202 | /** |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 203 | * The Authorization header carries the account's access token, so it's only worth the risk on the |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 204 | * exact authenticated media endpoints, and only when the URL being fetched is the one `locate` named |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 205 | * (never a redirect target, should a future caller pass one through). |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 206 | */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 207 | function shouldAuthorize(fetchUrl: string, locatedUrl: string): boolean { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 208 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 209 | const target = new URL(fetchUrl); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 210 | const located = new URL(locatedUrl); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 211 | if (target.origin !== located.origin) return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 212 | return target.pathname.startsWith('/_matrix/client/v1/media/') || target.pathname.startsWith('/_matrix/media/'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 213 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 214 | return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 215 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 216 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 217 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 218 | function concatChunks(parts: Uint8Array[]): Uint8Array<ArrayBuffer> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 219 | const total = parts.reduce((sum, part) => sum + part.byteLength, 0); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 220 | const out = new Uint8Array(total); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 221 | let offset = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 222 | for (const part of parts) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 223 | out.set(part, offset); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 224 | offset += part.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 225 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 226 | return out; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 227 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 228 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 229 | async function readUpTo( |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 230 | reader: ReadableStreamDefaultReader<Uint8Array<ArrayBuffer>>, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 231 | limit: number |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 232 | ): Promise<Uint8Array<ArrayBuffer>> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 233 | const chunks: Uint8Array[] = []; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 234 | let total = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 235 | while (total < limit) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 236 | const { value, done } = await reader.read(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 237 | if (done) break; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 238 | chunks.push(value); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 239 | total += value.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 240 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 241 | reader.cancel().catch(() => undefined); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 242 | return concatChunks(chunks).subarray(0, Math.min(limit, total)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 243 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 244 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 245 | /** |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 246 | * Ciphertext in, plaintext out: skips the bytes the upstream sent but the caller didn't ask for |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 247 | * (only nonzero when the upstream ignored the Range request), decrypts, drops the sub-16-byte |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 248 | * alignment prefix from the very first output, and stops once `limit` bytes have been produced. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 249 | */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 250 | async function* decryptedChunks( |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 251 | reader: ReadableStreamDefaultReader<Uint8Array<ArrayBuffer>>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 252 | decryptor: RangeDecryptor | null, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 253 | skipBytes: number, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 254 | dropPrefix: number, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 255 | limit: number |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 256 | ): AsyncGenerator<Uint8Array> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 257 | let toSkip = skipBytes; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 258 | let toDrop = dropPrefix; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 259 | let remaining = limit; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 260 | while (remaining > 0) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 261 | const { value, done } = await reader.read(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 262 | if (done) break; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 263 | let chunk = value; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 264 | if (toSkip > 0) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 265 | if (chunk.byteLength <= toSkip) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 266 | toSkip -= chunk.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 267 | continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 268 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 269 | chunk = chunk.subarray(toSkip); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 270 | toSkip = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 271 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 272 | const plain = decryptor ? await decryptor.push(chunk) : chunk; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 273 | if (plain.byteLength === 0) continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 274 | const emitted = emit(plain); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 275 | if (emitted) yield emitted; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 276 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 277 | if (remaining > 0 && decryptor) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 278 | const emitted = emit(await decryptor.finish()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 279 | if (emitted) yield emitted; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 280 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 281 | reader.cancel().catch(() => undefined); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 282 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 283 | function emit(chunk: Uint8Array): Uint8Array | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 284 | let out = chunk; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 285 | if (toDrop > 0) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 286 | if (out.byteLength <= toDrop) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 287 | toDrop -= out.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 288 | return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 289 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 290 | out = out.subarray(toDrop); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 291 | toDrop = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 292 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 293 | if (out.byteLength > remaining) out = out.subarray(0, remaining); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 294 | remaining -= out.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 295 | return out.byteLength > 0 ? out : null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 296 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 297 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 298 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 299 | async function headSniff(mediaId: string, file: MediaFileWire, token: string | null, retried: boolean): Promise<string | null> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 300 | const cached = sniffCache.get(mediaId); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 301 | if (cached && cached.expiresAt > Date.now()) return cached.contentType; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 302 | const headers = new Headers({ Range: 'bytes=0-79' }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 303 | if (shouldAuthorize(file.url, file.url) && token) headers.set('Authorization', `Bearer ${token}`); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 304 | let upstream: Response; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 305 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 306 | upstream = await fetch(file.url, { headers }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 307 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 308 | return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 309 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 310 | if (upstream.status === 401 && !retried) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 311 | const fresh = await locate(mediaId, 'Full', true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 312 | if (fresh && typeof fresh.location === 'object' && 'File' in fresh.location) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 313 | return headSniff(mediaId, fresh.location.File, fresh.token, true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 314 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 315 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 316 | if (!upstream.body || (!upstream.ok && upstream.status !== 206)) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 317 | const head = await readUpTo(upstream.body.getReader(), 80); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 318 | let contentType: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 319 | if (file.encryption) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 320 | const key = await importAttachmentKey(file.encryption.key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 321 | const iv = decodeAttachmentIv(file.encryption.iv); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 322 | const decryptor = new RangeDecryptor(key, iv, 0); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 323 | contentType = attachmentContentType(concatChunks([await decryptor.push(head), await decryptor.finish()]).subarray(0, 64)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 324 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 325 | contentType = attachmentContentType(head.subarray(0, 64)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 326 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 327 | sniffCache.set(mediaId, { contentType, expiresAt: Date.now() + LOCATE_TTL_MS }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 328 | return contentType; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 329 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 330 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 331 | function wholeFileFallback(mediaId: string, rangeHeader: string | null): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 332 | return requestMedia('media', { media_id: mediaId, size: 'Full' }).then((reply) => mediaResponse(reply, rangeHeader, true)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 333 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 334 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 335 | async function streamFullMedia(mediaId: string, rangeHeader: string | null, entry?: LocateEntry, retried = false): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 336 | const located = entry ?? (await locate(mediaId, 'Full', false)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 337 | if (!located) return wholeFileFallback(mediaId, rangeHeader); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 338 | if (typeof located.location !== 'object' || !('File' in located.location)) return wholeFileFallback(mediaId, rangeHeader); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 339 | const file = located.location.File; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 340 | const total = file.declared_size; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 341 | if (total == null) return wholeFileFallback(mediaId, rangeHeader); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 342 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 343 | const requested = parseByteRange(rangeHeader, total); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 344 | const start = requested?.start ?? 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 345 | const end = requested?.end ?? total - 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 346 | const alignedStart = start - (start % 16); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 347 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 348 | const headers = new Headers({ Range: `bytes=${alignedStart}-${end}` }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 349 | if (shouldAuthorize(file.url, file.url) && located.token) headers.set('Authorization', `Bearer ${located.token}`); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 350 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 351 | let upstream: Response; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 352 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 353 | upstream = await fetch(file.url, { headers }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 354 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 355 | return new Response('media upstream unreachable', { status: 502 }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 356 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 357 | if (upstream.status === 401 && !retried) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 358 | const fresh = await locate(mediaId, 'Full', true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 359 | if (fresh) return streamFullMedia(mediaId, rangeHeader, fresh, true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 360 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 361 | if (!upstream.body || (!upstream.ok && upstream.status !== 206)) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 362 | return new Response('media upstream error', { status: 502 }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 363 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 364 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 365 | const decryptor = file.encryption |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 366 | ? new RangeDecryptor(await importAttachmentKey(file.encryption.key), decodeAttachmentIv(file.encryption.iv), alignedStart / 16) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 367 | : null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 368 | const skipBytes = upstream.status === 206 ? 0 : alignedStart; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 369 | const dropPrefix = start - alignedStart; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 370 | const limit = end - start + 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 371 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 372 | const reader = upstream.body.getReader(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 373 | const chunks = decryptedChunks(reader, decryptor, skipBytes, dropPrefix, limit); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 374 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 375 | const prefetched: Uint8Array[] = []; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 376 | let prefetchedLen = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 377 | let chunksDone = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 378 | let contentType: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 379 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 380 | if (alignedStart === 0) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 381 | while (prefetchedLen < 64) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 382 | const step = await chunks.next(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 383 | if (step.done) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 384 | chunksDone = true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 385 | break; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 386 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 387 | prefetched.push(step.value); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 388 | prefetchedLen += step.value.byteLength; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 389 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 390 | contentType = attachmentContentType(concatChunks(prefetched).subarray(0, Math.min(64, prefetchedLen))); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 391 | sniffCache.set(mediaId, { contentType, expiresAt: Date.now() + LOCATE_TTL_MS }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 392 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 393 | contentType = await headSniff(mediaId, file, located.token, retried); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 394 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 395 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 396 | const body = new ReadableStream<Uint8Array>({ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 397 | async pull(controller) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 398 | const next = prefetched.shift(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 399 | if (next) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 400 | controller.enqueue(next); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 401 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 402 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 403 | if (chunksDone) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 404 | controller.close(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 405 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 406 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 407 | const step = await chunks.next(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 408 | if (step.done) controller.close(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 409 | else controller.enqueue(step.value); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 410 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 411 | cancel() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 412 | reader.cancel().catch(() => undefined); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 413 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 414 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 415 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 416 | const responseHeaders = attachmentHeaders(contentType); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 417 | responseHeaders.set('Accept-Ranges', 'bytes'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 418 | if (requested) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 419 | responseHeaders.set('Content-Range', `bytes ${start}-${end}/${total}`); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 420 | return new Response(body, { status: 206, headers: responseHeaders }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 421 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 422 | return new Response(body, { status: 200, headers: responseHeaders }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 423 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 424 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 425 | async function cacheFirst(request: Request): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 426 | const cached = await caches.match(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 427 | if (cached) return cached; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 428 | const response = await fetch(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 429 | if (response.ok) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 430 | const cache = await caches.open(CACHE_NAME); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 431 | void cache.put(request, response.clone()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 432 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 433 | return response; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 434 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 435 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 436 | /** Keyed by mxc alone, so the cached bytes outlive the access token and survive restarts. */ |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 437 | async function cachedAvatar(request: Request, mxc: string): Promise<Response> { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 438 | const cache = await caches.open(AVATAR_CACHE); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 439 | const cached = await cache.match(request); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 440 | if (cached) return cached; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 441 | const response = mediaResponse(await requestMedia('avatar', { mxc }), null, false); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 442 | if (response.ok) void cache.put(request, response.clone()); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 443 | return response; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 444 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 445 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 446 | async function networkFirstNavigation(request: Request): Promise<Response> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 447 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 448 | return await fetch(request); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 449 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 450 | const cached = await caches.match('/index.html'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 451 | if (cached) return cached; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 452 | throw new Error('offline and no cached shell'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 453 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 454 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 455 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 456 | sw.addEventListener('fetch', (event) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 457 | const url = new URL(event.request.url); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 458 | const range = event.request.headers.get('Range'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 459 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 460 | if (url.pathname === '/api/media') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 461 | const mxc = url.searchParams.get('mxc'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 462 | if (mxc) { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 463 | event.respondWith(cachedAvatar(event.request, mxc)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 464 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 465 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 466 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 467 | if (url.pathname === '/api/message-media') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 468 | const mediaId = url.searchParams.get('media_id'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 469 | const size = url.searchParams.get('size'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 470 | if (mediaId && size === 'Full') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 471 | event.respondWith(streamFullMedia(mediaId, range)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 472 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 473 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 474 | if (mediaId && size) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 475 | event.respondWith( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 476 | requestMedia('media', { media_id: mediaId, size }).then((reply) => mediaResponse(reply, range, true)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 477 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 478 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 479 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 480 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 481 | if (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/twemoji/')) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 482 | event.respondWith(cacheFirst(event.request)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 483 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 484 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 485 | if (event.request.mode === 'navigate') { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 486 | event.respondWith(networkFirstNavigation(event.request)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 487 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 488 | }); |