| 1 | const port = Number(process.env.PORT ?? 3000); |
| 2 | const width = 768; |
| 3 | |
| 4 | const getBrowser = RefCountedExpirable( |
| 5 | () => |
| 6 | playwright.chromium.launch({ |
| 7 | args: ["--no-sandbox", "--disable-setuid-sandbox"], |
| 8 | }), |
| 9 | (b) => b.close(), |
| 10 | ); |
| 11 | |
| 12 | /** a question as a picture, for embeds that only show one */ |
| 13 | export function serveQuestionImage(c: Context, question: Question) { |
| 14 | const page = view.cached("q+a/image-embed", { question }); |
| 15 | const image = sg.graph.lazy(renderQuestionImage, question.id, page); |
| 16 | return view.serveStep(c, image, { "Content-Type": "image/png", "Last-Modified": question.date.toUTCString() }); |
| 17 | } |
| 18 | |
| 19 | /** a picture of `page` as this server serves it, taken again when the page changes */ |
| 20 | async function renderQuestionImage(b: incremental.Build, id: string, page: incremental.Step<string>) { |
| 21 | await b.readStep(page); |
| 22 | using sharedBrowser = await getBrowser(); |
| 23 | const ctx = await sharedBrowser.value.newContext({ |
| 24 | userAgent: "clover-qa-image", |
| 25 | deviceScaleFactor: 1.5, |
| 26 | }); |
| 27 | try { |
| 28 | const p = await ctx.newPage(); |
| 29 | await p.setViewportSize({ width, height: 15000 }); |
| 30 | await p.goto(`http://localhost:${port}/q+a/${id}`); |
| 31 | try { |
| 32 | await p.waitForLoadState("networkidle", { timeout: 500 }); |
| 33 | } catch {} |
| 34 | const height = await p.evaluate(() => { |
| 35 | document.body.style.background = "#1a1b1c"; |
| 36 | return document.querySelector("main")!.offsetHeight; |
| 37 | }); |
| 38 | return new Uint8Array( |
| 39 | await p.screenshot({ |
| 40 | type: "png", |
| 41 | clip: { x: 0, y: 0, width, height }, |
| 42 | }), |
| 43 | ); |
| 44 | } finally { |
| 45 | await ctx.close(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | import * as sg from "#sitegen"; |
| 50 | import * as view from "#sitegen/view"; |
| 51 | import type { Question } from "#src/q+a/models/questions.ts"; |
| 52 | import { RefCountedExpirable } from "@clo/lib/async"; |
| 53 | import type * as incremental from "@clo/lib/incremental"; |
| 54 | import type { Context } from "hono"; |
| 55 | import * as playwright from "playwright"; |