Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.0 kB · 80 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081// Shared engine-launch helper for the standalone scripts in this directory. Not part of// the oracle service or its wire protocol (docs/dump-format.md) -- these scripts launch// Playwright directly so each can pick its own deviceScaleFactor per context, which the// oracle's fixed-DPR-1 contexts do not expose.
import { chromium, firefox, webkit } from 'playwright';
export const ENGINE_LAUNCHERS = { chromium, firefox, webkit };export const ENGINE_NAMES = Object.keys(ENGINE_LAUNCHERS);
export const RESET = '* { margin: 0; padding: 0; border: 0; box-sizing: content-box; }\nhtml, body { display: block; }\n';
/** Launches one browser per engine, kept open for the process lifetime. */export async function launchAll() { const browsers = {}; const versions = {}; for (const name of ENGINE_NAMES) { const browser = await ENGINE_LAUNCHERS[name].launch(); browsers[name] = browser; versions[name] = browser.version(); } return { browsers, versions };}
export async function closeAll(browsers) { await Promise.all(Object.values(browsers).map((b) => b.close()));}
/** * Opens one context/page per engine at a fixed `dpr`, kept open across many cases so a * sweep pays context/page setup cost once instead of per case. A fresh context+page per * case measured ~450ms each in practice (mostly Firefox/WebKit startup); reusing one page * per engine and driving it with repeated `setContent` calls (the same pattern * `oracle.mjs`'s `Engine.run` uses) brings that to ~20ms per case. */export async function openPages(browsers, dpr) { const pages = {}; const contexts = {}; for (const name of ENGINE_NAMES) { const context = await browsers[name].newContext({ viewport: { width: 800, height: 600 }, deviceScaleFactor: dpr, }); contexts[name] = context; pages[name] = await context.newPage(); } return { pages, async close() { await Promise.all(Object.values(contexts).map((c) => c.close())); }, };}
/** Renders `html` on each already-open page, returns page.evaluate(fn, fnArg) result per engine. */export async function measureOnPages(pages, html, fn, fnArg) { const out = {}; for (const name of ENGINE_NAMES) { await pages[name].setContent(html, { waitUntil: 'load' }); out[name] = await pages[name].evaluate(fn, fnArg); } return out;}
/** Same as measureOnPages but also returns a PNG screenshot buffer (base64) per engine. */export async function measureAndScreenshotOnPages(pages, html, fn, fnArg, clip) { const out = {}; for (const name of ENGINE_NAMES) { await pages[name].setContent(html, { waitUntil: 'load' }); const measured = await pages[name].evaluate(fn, fnArg); const png = await pages[name].screenshot({ clip }); out[name] = { ...measured, screenshotBase64: png.toString('base64') }; } return out;}
export function doc(css, body) { return `<!doctype html>\n<html>\n<head><meta charset="utf-8"><style>\n${RESET}${css}\n</style></head>\n<body>\n${body}\n</body>\n</html>\n`;}