Files
splatoon3.ink/app/common/util.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

98 lines
2.5 KiB
JavaScript

// node:crypto in the Workers runtime has no shake256; this pure-JS version is byte-identical.
import { shake256 } from '@noble/hashes/sha3.js';
import { bytesToHex } from '@noble/hashes/utils.js';
export function getTopOfCurrentHour(date = null) {
date ??= new Date;
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date;
}
function leadingZero(value) {
return value.toString().padStart(2, '0');
}
export function getDateParts(date = null) {
date ??= new Date;
return {
year: date.getUTCFullYear(),
month: leadingZero(date.getUTCMonth() + 1),
day: leadingZero(date.getUTCDate()),
hour: leadingZero(date.getUTCHours()),
minute: leadingZero(date.getUTCMinutes()),
second: leadingZero(date.getUTCSeconds()),
};
}
export function getGearIcon(gear) {
switch (gear.gear.__typename) {
case 'HeadGear': return '🧢';
case 'ClothingGear': return '👕';
case 'ShoesGear': return '👟';
default: return null;
}
}
export function normalizeSplatnetResourcePath(url) {
// Parse the URL
let u = new URL(url);
// Get just the pathname (without the host, query string, etc.)
let result = u.pathname;
// Remove "/resources/prod" from the beginning if it exists
result = result.replace(/^\/resources\/prod/, '');
// Remove the leading slash
result = result.replace(/^\//, '');
return result;
}
export function deriveId(node) {
// Unfortunately, SplatNet doesn't return IDs for a lot of gear properties.
// Derive IDs from image URLs instead.
let path = normalizeSplatnetResourcePath(node.image.url);
let id = bytesToHex(shake256(new TextEncoder().encode(path), { dkLen: 8 }));
return {
'__splatoon3ink_id': id,
...node,
};
}
export function getFestId(id) {
return Buffer.from(id, 'base64').toString().match(/^Fest-[A-Z]+:(.+)$/)?.[1] ?? id;
}
export function getFestTeamId(id) {
return Buffer.from(id, 'base64').toString().match(/^FestTeam-[A-Z]+:((.+):(.+))$/)?.[1] ?? id;
}
export function getXRankSeasonId(id) {
let parts = Buffer.from(id, 'base64').toString().match(/^XRankingSeason-([A-Z]+):(.+)$/i);
return parts
? `${parts[1]}-${parts[2]}`
: id;
}
/**
* Calculate cache expiry timestamp with 5-minute buffer.
* @param {number} expiresIn - Seconds until expiry
* @returns {number} Timestamp to expire the cache (5 minutes early)
*/
export function calculateCacheExpiry(expiresIn) {
let expires = Date.now() + expiresIn * 1000;
// Expire 5min early to make sure we have time to execute requests
return expires - 5 * 60 * 1000;
}