Files
splatoon3.ink/app/data/ImageProcessor.mjs
Matt Isenhower db2e68b847 Add the Cloudflare updater Worker with fast and slow lanes
Run the shared app/ updaters and social posters from workers/updater:

- Scheduler Durable Object lanes: "fast" keeps the Node cron cadence
  (quick at :00-:04, default at :05/:10/:15/:20/:30/:45) and posts to
  social after checking SITE_URL serves the new data; "slow" runs X-Rank,
  Splatfest rankings, and avatar updates hourly. Bounded retries, queued
  manual runs, pause, and persisted automatic-scheduling state.
- RegionAuth Durable Object per region: the single owner of each
  Nintendo login, serving bullet tokens to both lanes and renewing them
  hourly at :35-:38.
- A five-minute cron watchdog that only re-arms alarms, bearer-token
  control endpoints, and dev/production environments. DO migrations keep
  Scheduler at v1 to match the earlier dev deployment.
- Per-run limits (RunLimits) and per-account NsoClient locks instead of
  module-level limiters, since Durable Objects sharing an isolate cannot
  share in-flight requests. Updaters without localization rules no longer
  rewrite locale files.
- 20 workerd tests (npm run updater:test, included in npm test) with all
  network access blocked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:13:30 -08:00

98 lines
2.7 KiB
JavaScript

import prefixedConsole from '../common/prefixedConsole.mjs';
import { normalizeSplatnetResourcePath } from '../common/util.mjs';
import { storage } from '../common/storage/index.mjs';
import RunLimits from './RunLimits.mjs';
export default class ImageProcessor
{
// Public storage key prefix (served at ASSET_URL/splatnet/)
storagePrefix = 'splatnet';
// Path on the site for local runs without ASSET_URL
outputDirectory = 'assets/splatnet';
/** @param {RunLimits} [limits] Shared with the other updaters in the same run */
constructor(limits = new RunLimits) {
this.limits = limits;
this.console = prefixedConsole('Images');
this.siteUrl = process.env.SITE_URL;
this.assetUrl = process.env.ASSET_URL?.replace(/\/+$/, '');
}
async process(url, defer = true) {
// Normalize the path
let destination = this.normalize(url);
// Download the image if necessary
let job = () => this.maybeDownload(url, destination);
// defer ? queue.add(job) : await job();
if (defer) {
this.limits.images.add(job);
} else {
await job();
}
// Return the new public URL
return [destination, this.publicUrl(destination)];
}
onIdle() {
return this.limits.images.onIdle();
}
normalize(url) {
return normalizeSplatnetResourcePath(url);
}
storageKey(file) {
return `${this.storagePrefix}/${file}`;
}
publicUrl(file) {
if (this.assetUrl) {
return `${this.assetUrl}/splatnet/${file}`;
}
return `${this.siteUrl ?? ''}/${this.outputDirectory}/${file}`;
}
maybeDownload(url, destination) {
// Updaters run concurrently and often reference the same images; without this, each
// would see the file as missing and download it again.
let inFlight = this.limits.imageDownloads;
let pending = inFlight.get(destination);
if (!pending) {
pending = this._maybeDownload(url, destination)
.finally(() => inFlight.delete(destination));
inFlight.set(destination, pending);
}
return pending;
}
async _maybeDownload(url, destination) {
// If the file already exists, we don't need to download it again
if (await storage().public.exists(this.storageKey(destination))) {
return;
}
return await this.download(url, destination);
}
async download(url, destination) {
this.console.info(`Downloading image: ${destination}`);
try {
let result = await fetch(url);
if (!result.ok) {
throw new Error(`Invalid image response code: ${result.status}`);
}
await storage().public.writeBytes(this.storageKey(destination), new Uint8Array(await result.arrayBuffer()));
} catch (e) {
this.console.error(`Image download failed for ${destination}`, e);
}
}
}