|
1 | | -// A handler mounted into the video image and invoked through its own Lambda bootstrap, which sets |
2 | | -// AWS_EXECUTION_ENV as Lambda does: the bundled ffmpeg answers and the image's chrome-headless-shell |
3 | | -// renders, found where src/chromium.ts finds it and launched with the engine's Lambda flags. |
| 1 | +// A handler mounted into a Lambda Node 22 image and invoked through its own Lambda bootstrap, which sets |
| 2 | +// AWS_EXECUTION_ENV as Lambda does. In the arm64 video image it runs the image's chrome-headless-shell, found |
| 3 | +// where src/chromium.ts finds it; in the x86 base over the unzipped handler ZIP, @sparticuz/chromium. |
| 4 | +// It reports the platform fonts each line of a mixed-script page renders with, so the two can be compared. |
4 | 5 | import { execFileSync } from "node:child_process"; |
5 | 6 | import puppeteer from "puppeteer-core"; |
6 | 7 |
|
| 8 | +const LINES = { |
| 9 | + latin: "sans-serif|Quick brown fox ÀÉÎÕÜ ß œ", |
| 10 | + serif: "serif|Quick brown fox ÀÉÎÕÜ ß œ", |
| 11 | + mono: "monospace|Quick brown fox 0123456789", |
| 12 | + greekCyrillic: "sans-serif|Ελληνικά Кириллица", |
| 13 | + cjk: "sans-serif|漢字 かな カナ 한국어", |
| 14 | + arabic: "sans-serif|العربية", |
| 15 | + emoji: "sans-serif|😀 🎬 ✅", |
| 16 | +}; |
| 17 | + |
| 18 | +const textPage = Object.entries(LINES) |
| 19 | + .map(([id, line]) => { |
| 20 | + const [family, text] = line.split("|"); |
| 21 | + return `<p id="${id}" style="font:24px ${family};margin:4px">${text}</p>`; |
| 22 | + }) |
| 23 | + .join(""); |
| 24 | + |
| 25 | +async function launch() { |
| 26 | + if (process.env.HYPERFRAMES_LAMBDA_CHROME_PATH) { |
| 27 | + return puppeteer.launch({ |
| 28 | + executablePath: process.env.HYPERFRAMES_LAMBDA_CHROME_PATH, |
| 29 | + args: ["--no-sandbox", "--disable-setuid-sandbox", "--no-zygote", "--disable-dev-shm-usage"], |
| 30 | + }); |
| 31 | + } |
| 32 | + const { default: chromium } = await import("@sparticuz/chromium"); |
| 33 | + return puppeteer.launch({ executablePath: await chromium.executablePath(), args: chromium.args }); |
| 34 | +} |
| 35 | + |
| 36 | +async function platformFonts(page) { |
| 37 | + const cdp = await page.createCDPSession(); |
| 38 | + await cdp.send("DOM.enable"); |
| 39 | + await cdp.send("CSS.enable"); |
| 40 | + const { root } = await cdp.send("DOM.getDocument", { depth: -1 }); |
| 41 | + const fonts = {}; |
| 42 | + for (const id of Object.keys(LINES)) { |
| 43 | + const { nodeId } = await cdp.send("DOM.querySelector", { |
| 44 | + nodeId: root.nodeId, |
| 45 | + selector: `#${id}`, |
| 46 | + }); |
| 47 | + const { fonts: used } = await cdp.send("CSS.getPlatformFontsForNode", { nodeId }); |
| 48 | + fonts[id] = used.map((font) => `${font.familyName}:${font.glyphCount}`).sort(); |
| 49 | + } |
| 50 | + return fonts; |
| 51 | +} |
| 52 | + |
7 | 53 | export const handler = async () => { |
8 | 54 | const ffmpeg = execFileSync("/var/task/bin/ffmpeg", ["-hide_banner", "-version"], { |
9 | 55 | encoding: "utf-8", |
10 | 56 | }).split("\n")[0]; |
11 | | - const browser = await puppeteer.launch({ |
12 | | - executablePath: process.env.HYPERFRAMES_LAMBDA_CHROME_PATH, |
13 | | - args: ["--no-sandbox", "--disable-setuid-sandbox", "--no-zygote", "--disable-dev-shm-usage"], |
14 | | - }); |
| 57 | + const browser = await launch(); |
15 | 58 | try { |
16 | 59 | const page = await browser.newPage(); |
17 | 60 | await page.setContent("<div style='width:64px;height:64px;background:#0a0'></div>"); |
18 | 61 | const shot = await page.screenshot({ clip: { x: 0, y: 0, width: 64, height: 64 } }); |
| 62 | + await page.setViewport({ width: 640, height: 360 }); |
| 63 | + await page.setContent(textPage); |
19 | 64 | return { |
20 | 65 | executionEnv: process.env.AWS_EXECUTION_ENV, |
21 | 66 | arch: process.arch, |
22 | 67 | ffmpeg, |
23 | 68 | chrome: await browser.version(), |
24 | 69 | screenshotBytes: shot.length, |
| 70 | + fonts: await platformFonts(page), |
| 71 | + textPng: Buffer.from(await page.screenshot()).toString("base64"), |
25 | 72 | }; |
26 | 73 | } finally { |
27 | 74 | await browser.close(); |
|
0 commit comments