| import { fetch } from 'undici'; // Node ≥18 has global fetch; otherwise add undici | |
| export async function fetchAndEncode(url: string) { | |
| const res = await fetch(url); | |
| if (!res.ok) throw new Error(`Failed to fetch image: ${url}`); | |
| const buf = Buffer.from(await res.arrayBuffer()); | |
| const mimeType = res.headers.get('content-type') || 'image/png'; | |
| return { mimeType, data: buf.toString('base64') }; | |
| } | |