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