UNPKG

og-img

Version:

Generate dynamic Open Graph images for your website

55 lines (52 loc) 1.38 kB
// src/index.ts import { html } from "satori-html"; // src/fetchFont.ts async function fetchFont(url) { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch font: ${response.statusText}`); } return response.arrayBuffer(); } // src/ImageResponse.ts import { Resvg, initWasm } from "@resvg/resvg-wasm"; import satori from "satori"; var wasmPromise; var ImageResponse = class extends Response { /** * Creates an image response instance. * * @param content Content to render. * @param options Configuration options. */ constructor(content, options) { const readable = new ReadableStream({ async start(controller) { const svg = await satori(content, options); if (!wasmPromise) { wasmPromise = initWasm( fetch("https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm") ); } await wasmPromise; const image = new Resvg(svg).render().asPng(); controller.enqueue(image); controller.close(); } }); super(readable, { headers: { "content-type": "image/png", "cache-control": "public, immutable, no-transform, max-age=31536000", ...options.headers }, status: options.status, statusText: options.statusText }); } }; export { ImageResponse, fetchFont, html };