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