Branchwell

PulseCanvas / website / scripts / check-media.mjs

1import { readFile } from 'node:fs/promises';
2import { resolve } from 'node:path';
3import { setTimeout } from 'node:timers/promises';
4import { seoPages } from '../seo.config.js';
5import { films, filmUrl, mediaBaseUrl } from '../media.config.js';
6
7// Confirms every R2 video loop and film (both sizes) the pages can request, and every installer and source archive the
8// release manifest and Sparkle feed name, is live in R2 and range-readable, so a deploy never
9// points at a missing upload. Mirrors the loop and hero URL rules in main.js; /downloads/*
10// on the site redirects to the same path on the media host (scripts/prepare-deploy.mjs).
11// The hero's data-scene controls select live Three.js effects, not video files.
12const videos = new Set();
13for (const film of Object.values(films)) for (const size of ['1080p', '720p']) videos.add(filmUrl(film, size));
14for (const path of Object.keys(seoPages)) {
15 const html = await readFile(resolve(`.${path}index.html`), 'utf8');
16 for (const [, name, size] of html.matchAll(/data-loop="([a-z-]+)"(?:\s+data-size="(\d+)")?/g)) {
17 videos.add(`${mediaBaseUrl}/loops/${name}-${size || 720}.mp4`);
18 }
19 for (const [url] of html.matchAll(new RegExp(`${mediaBaseUrl.replace(/[.]/g, '\\.')}/[^"' )]+`, 'g'))) videos.add(url);
20}
21
22// Expected byte counts come from the manifest and feed, so a truncated upload fails too.
23const downloads = new Map();
24const addDownload = (value, bytes) => {
25 const { pathname } = new URL(value, 'https://pulsecanvas.ai');
26 if (pathname.startsWith('/downloads/')) downloads.set(`${mediaBaseUrl}${pathname}`, bytes ?? downloads.get(`${mediaBaseUrl}${pathname}`));
27};
28const optional = (path) => readFile(resolve(path), 'utf8').catch(() => '');
29const manifest = await optional('public/mac-release.json');
30if (manifest) {
31 const release = JSON.parse(manifest);
32 addDownload(release.downloadUrl, release.bytes);
33 addDownload(release.sourceUrl);
34}
35for (const [enclosure] of (await optional('public/appcast.xml')).matchAll(/<enclosure\b[^>]*>/g)) {
36 const url = enclosure.match(/\burl="([^"]+)"/)?.[1];
37 const length = enclosure.match(/\blength="(\d+)"/)?.[1];
38 if (url) addDownload(url, length && Number(length));
39}
40
41const failures = [];
42async function check(url, { video, bytes }) {
43 const attempts = 3;
44 for (let attempt = 1; attempt <= attempts; attempt += 1) {
45 let failure;
46 try {
47 const response = await fetch(url, {
48 headers: { Range: 'bytes=0-1' },
49 signal: AbortSignal.timeout(15_000),
50 });
51 await response.body?.cancel();
52 const type = response.headers.get('content-type') || '';
53 const total = Number(response.headers.get('content-range')?.split('/')[1]);
54 if (response.status !== 206 || (video && !type.startsWith('video/')) || !total) {
55 failure = `${url}: HTTP ${response.status} ${type}`;
56 } else if (bytes && total !== bytes) {
57 failure = `${url}: ${total} bytes, expected ${bytes}`;
58 } else {
59 return;
60 }
61 } catch (error) {
62 failure = `${url}: ${error.message}`;
63 }
64 if (attempt === attempts) {
65 failures.push(failure);
66 } else {
67 console.warn(`Retrying media check (${attempt}/${attempts}): ${failure}`);
68 await setTimeout(300 * attempt);
69 }
70 }
71}
72await Promise.all([
73 ...[...videos].map((url) => check(url, { video: true })),
74 ...[...downloads].map(([url, bytes]) => check(url, { video: false, bytes })),
75]);
76if (failures.length) {
77 console.error(`Media or downloads missing from R2 or not range-readable:\n${failures.sort().join('\n')}`);
78 process.exit(1);
79}
80console.log(`All ${videos.size} videos and ${downloads.size} downloads are live in R2 with range support.`);