vibes
This commit is contained in:
parent
80c093b7ba
commit
f72c43a9fa
101 changed files with 2168 additions and 1177 deletions
|
|
@ -1,25 +1,23 @@
|
|||
import { chromium, type Browser, type Page } from 'playwright';
|
||||
import { chromium, type Browser, type BrowserContext, type Page, type Route } from 'playwright';
|
||||
import { existsSync, readdirSync } from 'fs';
|
||||
import { NetworkCache } from './network-cache.js';
|
||||
|
||||
const VIEWPORT = { width: 1200, height: 630 };
|
||||
const NAVIGATION_TIMEOUT = 15_000;
|
||||
const TILE_BUFFER_MS = 500;
|
||||
const READY_TIMEOUT = 15_000;
|
||||
const RENDER_BUFFER_MS = 200;
|
||||
const POOL_SIZE = 3;
|
||||
|
||||
let browser: Browser | null = null;
|
||||
let sharedContext: BrowserContext | null = null;
|
||||
const pagePool: Page[] = [];
|
||||
let warmingUp = false;
|
||||
const networkCache = new NetworkCache();
|
||||
|
||||
/**
|
||||
* Detect if a GPU is available for hardware-accelerated rendering.
|
||||
* Checks for DRI devices on Linux which indicate GPU + driver availability.
|
||||
*/
|
||||
function detectGpu(): boolean {
|
||||
try {
|
||||
// Check for DRI render nodes (Linux)
|
||||
if (existsSync('/dev/dri')) {
|
||||
const devices = readdirSync('/dev/dri');
|
||||
// Look for renderD* (render nodes) or card* (display cards)
|
||||
const hasGpu = devices.some((d) => d.startsWith('renderD') || d.startsWith('card'));
|
||||
if (hasGpu) {
|
||||
console.log(`GPU detected: /dev/dri contains ${devices.join(', ')}`);
|
||||
|
|
@ -27,7 +25,7 @@ function detectGpu(): boolean {
|
|||
}
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors - fall back to software rendering
|
||||
// Fall through to software rendering
|
||||
}
|
||||
console.log('No GPU detected, using SwiftShader software rendering');
|
||||
return false;
|
||||
|
|
@ -41,14 +39,31 @@ function getBrowserArgs(): string[] {
|
|||
'--disable-dev-shm-usage',
|
||||
'--enable-webgl',
|
||||
'--ignore-gpu-blocklist',
|
||||
// Prevent timer/renderer throttling in headless mode
|
||||
'--disable-background-timer-throttling',
|
||||
'--disable-backgrounding-occluded-windows',
|
||||
'--disable-renderer-backgrounding',
|
||||
// Ensure compositor finishes all stages before screenshot capture
|
||||
'--run-all-compositor-stages-before-draw',
|
||||
// Skip color profile conversion overhead
|
||||
'--force-color-profile=srgb',
|
||||
// Reduce non-essential browser features
|
||||
'--disable-domain-reliability',
|
||||
'--disable-features=TranslateUI',
|
||||
];
|
||||
|
||||
if (hasGpu) {
|
||||
// Use hardware GPU acceleration
|
||||
return [...baseArgs, '--enable-gpu', '--use-gl=egl'];
|
||||
} else {
|
||||
// Fall back to SwiftShader software rendering
|
||||
return [...baseArgs, '--disable-gpu', '--use-gl=swiftshader'];
|
||||
return [
|
||||
...baseArgs,
|
||||
'--disable-gpu',
|
||||
'--use-gl=swiftshader',
|
||||
// Run the GPU (SwiftShader) work in the browser process to eliminate
|
||||
// IPC overhead between browser and GPU processes — significant win
|
||||
// when all GL calls are CPU-bound anyway
|
||||
'--in-process-gpu',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -61,22 +76,71 @@ async function ensureBrowser(): Promise<Browser> {
|
|||
return browser;
|
||||
}
|
||||
|
||||
async function createPage(): Promise<Page> {
|
||||
async function ensureContext(): Promise<BrowserContext> {
|
||||
if (sharedContext) return sharedContext;
|
||||
|
||||
const instance = await ensureBrowser();
|
||||
const context = await instance.newContext({
|
||||
sharedContext = await instance.newContext({
|
||||
viewport: VIEWPORT,
|
||||
deviceScaleFactor: 1,
|
||||
});
|
||||
|
||||
// Set up response caching at context level — all pages share the cache.
|
||||
// This means tiles fetched during pre-warm or a previous screenshot are
|
||||
// served instantly for subsequent screenshots.
|
||||
await sharedContext.route('**/*', async (route: Route) => {
|
||||
const url = route.request().url();
|
||||
|
||||
// Non-cacheable requests (API data, etc.) pass through directly
|
||||
if (!networkCache.shouldCache(url)) {
|
||||
await route.continue();
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache hit — fulfill from memory, zero network
|
||||
const cached = networkCache.get(url);
|
||||
if (cached) {
|
||||
await route.fulfill({
|
||||
status: cached.status,
|
||||
headers: cached.headers,
|
||||
body: cached.body,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache miss — fetch, cache, fulfill
|
||||
try {
|
||||
const response = await route.fetch();
|
||||
const body = await response.body();
|
||||
const entry = {
|
||||
body,
|
||||
headers: response.headers(),
|
||||
status: response.status(),
|
||||
};
|
||||
networkCache.set(url, entry);
|
||||
await route.fulfill({
|
||||
status: entry.status,
|
||||
headers: entry.headers,
|
||||
body: entry.body,
|
||||
});
|
||||
} catch {
|
||||
await route.continue().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
return sharedContext;
|
||||
}
|
||||
|
||||
async function createPage(): Promise<Page> {
|
||||
const context = await ensureContext();
|
||||
return context.newPage();
|
||||
}
|
||||
|
||||
async function warmPool(): Promise<void> {
|
||||
if (warmingUp) return;
|
||||
warmingUp = true;
|
||||
|
||||
try {
|
||||
const instance = await ensureBrowser();
|
||||
while (pagePool.length < POOL_SIZE && instance.isConnected()) {
|
||||
while (pagePool.length < POOL_SIZE) {
|
||||
const page = await createPage();
|
||||
pagePool.push(page);
|
||||
}
|
||||
|
|
@ -86,17 +150,12 @@ async function warmPool(): Promise<void> {
|
|||
}
|
||||
|
||||
async function acquirePage(): Promise<Page> {
|
||||
// Try to get a page from the pool
|
||||
const page = pagePool.shift();
|
||||
if (page && !page.isClosed()) {
|
||||
// Refill pool in background
|
||||
warmPool().catch(() => {});
|
||||
return page;
|
||||
}
|
||||
|
||||
// No pooled page available, create one
|
||||
const newPage = await createPage();
|
||||
// Start warming pool in background
|
||||
warmPool().catch(() => {});
|
||||
return newPage;
|
||||
}
|
||||
|
|
@ -104,62 +163,88 @@ async function acquirePage(): Promise<Page> {
|
|||
async function releasePage(page: Page): Promise<void> {
|
||||
try {
|
||||
if (page.isClosed()) return;
|
||||
|
||||
// Reset page state for reuse
|
||||
// Navigate to blank to free rendered page resources (DOM, WebGL context)
|
||||
// while keeping the page object alive for V8 code cache reuse
|
||||
await page.goto('about:blank', { timeout: 5000 }).catch(() => {});
|
||||
|
||||
if (!page.isClosed() && pagePool.length < POOL_SIZE) {
|
||||
pagePool.push(page);
|
||||
} else {
|
||||
await page.context().close().catch(() => {});
|
||||
await page.close().catch(() => {});
|
||||
}
|
||||
} catch {
|
||||
await page.context().close().catch(() => {});
|
||||
await page.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-warm the browser and populate the network cache by loading the app once.
|
||||
* Subsequent screenshots benefit from cached JS/CSS bundles, map tiles,
|
||||
* and V8 compiled bytecode — eliminating cold-start latency.
|
||||
*/
|
||||
export async function initialize(appUrl: string): Promise<void> {
|
||||
console.log('Pre-warming browser and caches...');
|
||||
const page = await createPage();
|
||||
try {
|
||||
await page.goto(`${appUrl}/?screenshot=1`, {
|
||||
waitUntil: 'load',
|
||||
timeout: 30_000,
|
||||
});
|
||||
// Wait for the app to fully load and cache all resources
|
||||
try {
|
||||
await page.waitForFunction('window.__screenshot_ready === true', {
|
||||
timeout: READY_TIMEOUT,
|
||||
});
|
||||
} catch {
|
||||
// Non-fatal — cache will still have JS/CSS/tiles from the partial load
|
||||
}
|
||||
console.log(`Pre-warm complete. Cache: ${networkCache.stats()}`);
|
||||
} catch (err) {
|
||||
console.warn('Pre-warm failed (non-fatal):', err);
|
||||
} finally {
|
||||
await page.close().catch(() => {});
|
||||
}
|
||||
await warmPool();
|
||||
}
|
||||
|
||||
export async function takeScreenshot(url: string): Promise<Buffer> {
|
||||
const page = await acquirePage();
|
||||
|
||||
// Log browser console messages for diagnostics
|
||||
page.on('console', (msg) => {
|
||||
if (msg.type() === 'error' || msg.type() === 'warning') {
|
||||
console.log(`[browser ${msg.type()}] ${msg.text()}`);
|
||||
}
|
||||
});
|
||||
page.on('pageerror', (err) => {
|
||||
console.log(`[browser exception] ${err.message}`);
|
||||
});
|
||||
const t0 = performance.now();
|
||||
|
||||
try {
|
||||
// Use domcontentloaded instead of networkidle - let __screenshot_ready handle readiness
|
||||
const response = await page.goto(url, {
|
||||
waitUntil: 'domcontentloaded',
|
||||
timeout: NAVIGATION_TIMEOUT,
|
||||
});
|
||||
const t1 = performance.now();
|
||||
if (response) {
|
||||
console.log(`Page loaded: ${response.status()} ${response.statusText()}`);
|
||||
console.log(` Navigate: ${(t1 - t0).toFixed(0)}ms (status ${response.status()})`);
|
||||
}
|
||||
|
||||
// Wait for the frontend to signal readiness
|
||||
// Wait for the frontend to signal that data is loaded and layers created
|
||||
try {
|
||||
await page.waitForFunction('window.__screenshot_ready === true', {
|
||||
timeout: NAVIGATION_TIMEOUT,
|
||||
timeout: READY_TIMEOUT,
|
||||
});
|
||||
console.log('Frontend signalled ready');
|
||||
} catch {
|
||||
console.warn('Timed out waiting for __screenshot_ready, proceeding with partial screenshot');
|
||||
console.warn(' Timed out waiting for __screenshot_ready');
|
||||
}
|
||||
const t2 = performance.now();
|
||||
console.log(` Ready: ${(t2 - t1).toFixed(0)}ms`);
|
||||
|
||||
// Extra buffer for map tiles to finish rendering
|
||||
await page.waitForTimeout(TILE_BUFFER_MS);
|
||||
// Brief buffer for SwiftShader to finish rendering the WebGL frame.
|
||||
// Reduced from 500ms → 200ms since tiles now load from the in-memory
|
||||
// cache and don't need network round-trips.
|
||||
await page.waitForTimeout(RENDER_BUFFER_MS);
|
||||
|
||||
// JPEG at quality 85: ~3-5x faster encoding than PNG with negligible
|
||||
// visual difference for screenshot content (map + text overlay)
|
||||
const screenshot = await page.screenshot({ type: 'jpeg', quality: 85 });
|
||||
const t3 = performance.now();
|
||||
console.log(` Capture: ${(t3 - t2).toFixed(0)}ms | Total: ${(t3 - t0).toFixed(0)}ms`);
|
||||
console.log(` Cache: ${networkCache.stats()}`);
|
||||
|
||||
const screenshot = await page.screenshot({ type: 'png' });
|
||||
return Buffer.from(screenshot);
|
||||
} finally {
|
||||
// Remove listeners before releasing page back to pool
|
||||
page.removeAllListeners('console');
|
||||
page.removeAllListeners('pageerror');
|
||||
await releasePage(page);
|
||||
}
|
||||
}
|
||||
|
|
@ -182,24 +267,25 @@ export async function checkWebGL(): Promise<Record<string, unknown>> {
|
|||
vendor: debugExt ? g.getParameter(debugExt.UNMASKED_VENDOR_WEBGL) : 'unknown',
|
||||
};
|
||||
});
|
||||
return info;
|
||||
return { ...info, cache: networkCache.stats() };
|
||||
} finally {
|
||||
await releasePage(page);
|
||||
}
|
||||
}
|
||||
|
||||
export async function closeBrowser(): Promise<void> {
|
||||
// Close all pooled pages
|
||||
for (const page of pagePool) {
|
||||
await page.context().close().catch(() => {});
|
||||
await page.close().catch(() => {});
|
||||
}
|
||||
pagePool.length = 0;
|
||||
|
||||
if (sharedContext) {
|
||||
await sharedContext.close().catch(() => {});
|
||||
sharedContext = null;
|
||||
}
|
||||
|
||||
if (browser) {
|
||||
await browser.close().catch(() => {});
|
||||
browser = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-warm the pool on module load
|
||||
warmPool().catch(() => {});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue