(element: ReactElement, options: ImageResponseOptions = {})
| 186 | |
| 187 | export class ImageResponse extends Response { |
| 188 | constructor(element: ReactElement, options: ImageResponseOptions = {}) { |
| 189 | const extendedOptions = Object.assign( |
| 190 | { |
| 191 | width: 1200, |
| 192 | height: 630, |
| 193 | debug: false, |
| 194 | }, |
| 195 | options, |
| 196 | ); |
| 197 | |
| 198 | const result = new ReadableStream({ |
| 199 | async start(controller) { |
| 200 | await initializedYoga; |
| 201 | await initializedResvg; |
| 202 | const fontData = await fallbackFont; |
| 203 | |
| 204 | const svg = await satori(element, { |
| 205 | width: extendedOptions.width, |
| 206 | height: extendedOptions.height, |
| 207 | debug: extendedOptions.debug, |
| 208 | fonts: extendedOptions.fonts || [ |
| 209 | { |
| 210 | name: "sans serif", |
| 211 | data: fontData, |
| 212 | weight: 700, |
| 213 | style: "normal", |
| 214 | }, |
| 215 | ], |
| 216 | loadAdditionalAsset: loadDynamicAsset({ |
| 217 | emoji: extendedOptions.emoji, |
| 218 | }), |
| 219 | }); |
| 220 | |
| 221 | const resvgJS = new Resvg(svg, { |
| 222 | fitTo: { |
| 223 | mode: "width", |
| 224 | value: extendedOptions.width, |
| 225 | }, |
| 226 | }); |
| 227 | |
| 228 | controller.enqueue(resvgJS.render()); |
| 229 | controller.close(); |
| 230 | }, |
| 231 | }); |
| 232 | |
| 233 | super(result, { |
| 234 | headers: { |
| 235 | "content-type": "image/png", |
| 236 | "cache-control": isDev |
| 237 | ? "no-cache, no-store" |
| 238 | : "public, max-age=31536000, no-transform, immutable", |
| 239 | ...extendedOptions.headers, |
| 240 | }, |
| 241 | status: extendedOptions.status, |
| 242 | statusText: extendedOptions.statusText, |
| 243 | }); |
| 244 | } |
| 245 | } |
nothing calls this directly
no outgoing calls
no test coverage detected