/** * Download brand images from the legacy NPI Pipe Count HF space. * HF Spaces reject PNG/ICO in git — assets are fetched at build time instead. * On rate limits or network errors, bundled base64 fallbacks are used. */ import { mkdir, writeFile, access } from 'node:fs/promises' import { constants } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { FALLBACK_ASSETS } from './brand-assets.fallback.mjs' const BASES = [ 'https://npisb-pipecountapp.hf.space', 'https://npisb-pipecountapp-dev.hf.space', ] const PUBLIC = join(fileURLToPath(new URL('.', import.meta.url)), '..', 'public') const FILES = [ ['icon.png', '/images/touch/android-chrome-512x512.png'], ['images/bg.png', '/images/bg.png'], ['images/touch/android-chrome-192x192.png', '/images/touch/android-chrome-192x192.png'], ['images/touch/android-chrome-512x512.png', '/images/touch/android-chrome-512x512.png'], ['images/touch/favicon-16x16.png', '/images/touch/favicon-16x16.png'], ['images/touch/favicon-32x32.png', '/images/touch/favicon-32x32.png'], ['images/touch/favicon.ico', '/images/touch/favicon.ico'], ['images/touch/mstile-150x150.png', '/images/touch/mstile-150x150.png'], ] const RETRYABLE = new Set([429, 502, 503, 504]) async function exists(path) { try { await access(path, constants.F_OK) return true } catch { return false } } function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)) } async function fetchWithRetry(urlPath) { let lastStatus = 0 for (const base of BASES) { for (let attempt = 0; attempt < 4; attempt++) { try { const res = await fetch(`${base}${urlPath}`) lastStatus = res.status if (res.ok) { return Buffer.from(await res.arrayBuffer()) } if (!RETRYABLE.has(res.status)) break } catch { /* try next attempt */ } await sleep(400 * (attempt + 1)) } } return { error: lastStatus || 'network' } } async function writeAsset(dest, buf) { const out = join(PUBLIC, dest) await mkdir(dirname(out), { recursive: true }) await writeFile(out, buf) return out } async function main() { const force = process.env.FORCE_FETCH_ASSETS === '1' let fetched = 0 let fallback = 0 for (const [dest, urlPath] of FILES) { const out = join(PUBLIC, dest) if (!force && (await exists(out))) continue const remote = await fetchWithRetry(urlPath) if (Buffer.isBuffer(remote)) { if (remote.length < 100) { throw new Error(`Suspiciously small file for ${urlPath} (${remote.length} bytes)`) } await writeAsset(dest, remote) fetched++ console.log(`fetched ${dest} (${remote.length} bytes)`) continue } const encoded = FALLBACK_ASSETS[dest] if (!encoded) { throw new Error(`No remote or bundled asset for ${dest} (last status: ${remote.error})`) } const buf = Buffer.from(encoded, 'base64') if (buf.length < 100) { throw new Error(`Bundled asset too small for ${dest}`) } await writeAsset(dest, buf) fallback++ console.warn(`fallback ${dest} (${buf.length} bytes, remote: ${remote.error})`) } if (fetched) { console.log(`Done — ${fetched} asset(s) fetched.`) } else if (fallback) { console.log(`Done — ${fallback} asset(s) from bundled fallback.`) } else { console.log('Brand assets already present.') } } main().catch((err) => { console.error(err) process.exit(1) })