Files
splatoon3.ink/app/screenshots/HttpServer.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

46 lines
1020 B
JavaScript

import http from 'http';
import { access } from 'node:fs/promises';
import sirv from 'sirv';
export default class HttpServer
{
constructor({ root = './dist', host } = {}) {
this.root = root;
this.host = host;
}
/** @member {http.Server} */
_server = null;
get port() {
return this._server.address().port;
}
async open() {
if (this._server) {
return;
}
await access(`${this.root}/screenshots/index.html`).catch(() => {
throw new Error(`Missing ${this.root}/screenshots/index.html. Run npm run build first.`);
});
await new Promise((resolve, reject) => {
const handler = sirv(this.root);
this._server = http.createServer(handler);
this._server.once('error', reject);
this._server.listen(0, this.host, resolve);
});
}
async close() {
if (this._server) {
let server = this._server;
this._server = null;
server.closeAllConnections();
await new Promise(resolve => server.close(resolve));
}
}
}