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