Files
splatoon3.ink/app/screenshots/drivers/LocalScreenshotDriver.mjs
Matt Isenhower 6df93806c2 Prepare the backend for Cloudflare Workers
Groundwork for running the existing updaters and social posters in a Worker:

- Derive IDs with @noble/hashes shake256; the Workers runtime's node:crypto
  lacks shake256. A test pins an ID produced by node:crypto.
- Split updaters into fast (schedules, gear, co-op, festivals, stages; run
  concurrently) and slow (X-Rank, Splatfest rankings; run one at a time).
  The cron posts after the fast updaters, before the slow ones.
- Fetch X-Rank seasons and detail types sequentially, cutting peak heap per
  division from ~85 MB to ~38 MB.
- Move Splatfest rankings into FestivalRankingsUpdater, driven by the
  published festivals data, so results never wait on ranking pagination.
- Coalesce concurrent downloads of the same SplatNet image.
- Bring over the Browser-binding screenshot work from develop (f7293b6):
  Cloudflare captures use the BROWSER binding with retries, Browserless is
  removed, and workers/screenshots offers capture-only local testing. The
  local driver keeps puppeteer-core with an installed Chrome, always serves
  dist/, and fails on page-reported screenshot errors.
- Remove the Docker image, compose files, and GHCR deploy workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 15:40:51 -08:00

82 lines
2.6 KiB
JavaScript

import { existsSync } from 'node:fs';
import puppeteer from 'puppeteer-core';
import HttpServer from '../HttpServer.mjs';
import { screenshotReadyTimeout } from '../../../src/common/screenshot.mjs';
/**
* Captures the built frontend (dist/) with a locally installed Chrome, served on loopback.
* It deliberately ignores SITE_URL, which normally points at the public site.
*/
export default class LocalScreenshotDriver {
constructor({
env = process.env,
root = './dist',
puppeteerClient = puppeteer,
httpServerFactory = () => new HttpServer({ root, host: '127.0.0.1' }),
} = {}) {
this.env = env;
this.puppeteer = puppeteerClient;
this.httpServerFactory = httpServerFactory;
}
async open() {
const executablePath = this.env.CHROME_PATH || [
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/usr/bin/chromium',
'/usr/bin/google-chrome',
].find(path => existsSync(path));
if (!executablePath) throw new Error('Set CHROME_PATH to a local Chrome or Chromium executable');
try {
this.server = this.httpServerFactory();
await this.server.open();
this.browser = await this.puppeteer.launch({ executablePath, headless: true });
this.page = await this.browser.newPage();
} catch (error) {
await this.close();
throw error;
}
}
async capture(route, viewport) {
const url = new URL(`http://127.0.0.1:${this.server.port}/screenshots/`);
url.hash = route;
await this.page.setViewport(viewport);
// Start fresh so a hash-only route change cannot reuse the previous page's ready marker.
await this.page.goto('about:blank');
const response = await this.page.goto(url.href, { waitUntil: 'load' });
if (!response?.ok()) {
throw new Error(`Screenshot page failed (${response?.status() ?? 'no response'}): ${url}`);
}
await this.page.waitForFunction(() => {
const { screenshotReady, screenshotError } = document.documentElement.dataset;
return screenshotReady === 'true' || !!screenshotError;
}, { timeout: screenshotReadyTimeout });
const error = await this.page.evaluate(() => document.documentElement.dataset.screenshotError);
if (error) throw new Error(error);
return this.page.screenshot();
}
async close() {
try {
// Closing the browser also closes any pages, including partially opened ones.
await this.browser?.close();
} finally {
const server = this.server;
this.browser = null;
this.page = null;
this.server = null;
await server?.close();
}
}
}