Morning improvements

This commit is contained in:
Andras Schmelczer 2026-03-17 13:29:03 +00:00
parent 3e9fba5303
commit 53fff3efaa
41 changed files with 2438 additions and 637 deletions

View file

@ -226,11 +226,22 @@ export async function initialize(appUrl: string): Promise<void> {
await warmPool();
}
export async function takeScreenshot(url: string): Promise<Buffer> {
export async function takeScreenshot(url: string, authHeader?: string): Promise<Buffer> {
const page = await acquirePage();
const t0 = performance.now();
try {
// Inject Authorization header on API requests so the headless browser
// is authenticated (required for licensed users outside the free zone).
// Page-level routes take precedence over the context-level cache route,
// so only /api/ requests are affected — static assets still use the cache.
if (authHeader) {
await page.route('**/api/**', async (route) => {
const headers = { ...route.request().headers(), authorization: authHeader };
await route.continue({ headers });
});
}
const response = await page.goto(url, {
waitUntil: 'domcontentloaded',
timeout: NAVIGATION_TIMEOUT,
@ -265,6 +276,11 @@ export async function takeScreenshot(url: string): Promise<Buffer> {
return Buffer.from(screenshot);
} finally {
// Remove page-level auth route before returning page to pool
// so the next screenshot doesn't inherit stale credentials
if (authHeader) {
await page.unrouteAll({ behavior: 'wait' }).catch(() => {});
}
await releasePage(page);
}
}

View file

@ -57,7 +57,12 @@ app.get('/screenshot', async (req, res) => {
const pagePath = typeof req.query.path === 'string' && req.query.path ? req.query.path : '/';
if (pagePath !== '/') qs.set('path', pagePath);
// Include auth status in cache key so authenticated screenshots
// (with hexagons outside free zone) are cached separately
const authHeader = req.headers.authorization;
if (authHeader) qs.set('_auth', '1');
const cacheKey = cache.buildKey(qs);
qs.delete('_auth');
qs.delete('path');
// Check cache first
@ -74,8 +79,8 @@ app.get('/screenshot', async (req, res) => {
qs.set('screenshot', '1');
const url = `${APP_URL}${pagePath}?${qs}`;
console.log(`Taking screenshot: ${url}`);
const jpeg = await takeScreenshot(url);
console.log(`Taking screenshot: ${url}${authHeader ? ' (authenticated)' : ''}`);
const jpeg = await takeScreenshot(url, authHeader);
// Cache it
cache.set(cacheKey, jpeg);