import { readFile } from 'node:fs/promises'; import { resolve } from 'node:path'; import { setTimeout } from 'node:timers/promises'; import { seoPages } from '../seo.config.js'; import { films, filmUrl, mediaBaseUrl } from '../media.config.js'; // Confirms every R2 video loop and film (both sizes) the pages can request, and every installer and source archive the // release manifest and Sparkle feed name, is live in R2 and range-readable, so a deploy never // points at a missing upload. Mirrors the loop and hero URL rules in main.js; /downloads/* // on the site redirects to the same path on the media host (scripts/prepare-deploy.mjs). // The hero's data-scene controls select live Three.js effects, not video files. const videos = new Set(); for (const film of Object.values(films)) for (const size of ['1080p', '720p']) videos.add(filmUrl(film, size)); for (const path of Object.keys(seoPages)) { const html = await readFile(resolve(`.${path}index.html`), 'utf8'); for (const [, name, size] of html.matchAll(/data-loop="([a-z-]+)"(?:\s+data-size="(\d+)")?/g)) { videos.add(`${mediaBaseUrl}/loops/${name}-${size || 720}.mp4`); } for (const [url] of html.matchAll(new RegExp(`${mediaBaseUrl.replace(/[.]/g, '\\.')}/[^"' )]+`, 'g'))) videos.add(url); } // Expected byte counts come from the manifest and feed, so a truncated upload fails too. const downloads = new Map(); const addDownload = (value, bytes) => { const { pathname } = new URL(value, 'https://pulsecanvas.ai'); if (pathname.startsWith('/downloads/')) downloads.set(`${mediaBaseUrl}${pathname}`, bytes ?? downloads.get(`${mediaBaseUrl}${pathname}`)); }; const optional = (path) => readFile(resolve(path), 'utf8').catch(() => ''); const manifest = await optional('public/mac-release.json'); if (manifest) { const release = JSON.parse(manifest); addDownload(release.downloadUrl, release.bytes); addDownload(release.sourceUrl); } for (const [enclosure] of (await optional('public/appcast.xml')).matchAll(/]*>/g)) { const url = enclosure.match(/\burl="([^"]+)"/)?.[1]; const length = enclosure.match(/\blength="(\d+)"/)?.[1]; if (url) addDownload(url, length && Number(length)); } const failures = []; async function check(url, { video, bytes }) { const attempts = 3; for (let attempt = 1; attempt <= attempts; attempt += 1) { let failure; try { const response = await fetch(url, { headers: { Range: 'bytes=0-1' }, signal: AbortSignal.timeout(15_000), }); await response.body?.cancel(); const type = response.headers.get('content-type') || ''; const total = Number(response.headers.get('content-range')?.split('/')[1]); if (response.status !== 206 || (video && !type.startsWith('video/')) || !total) { failure = `${url}: HTTP ${response.status} ${type}`; } else if (bytes && total !== bytes) { failure = `${url}: ${total} bytes, expected ${bytes}`; } else { return; } } catch (error) { failure = `${url}: ${error.message}`; } if (attempt === attempts) { failures.push(failure); } else { console.warn(`Retrying media check (${attempt}/${attempts}): ${failure}`); await setTimeout(300 * attempt); } } } await Promise.all([ ...[...videos].map((url) => check(url, { video: true })), ...[...downloads].map(([url, bytes]) => check(url, { video: false, bytes })), ]); if (failures.length) { console.error(`Media or downloads missing from R2 or not range-readable:\n${failures.sort().join('\n')}`); process.exit(1); } console.log(`All ${videos.size} videos and ${downloads.size} downloads are live in R2 with range support.`);