File size: 3,532 Bytes
e205988
 
 
86defa3
e205988
 
 
 
 
86defa3
 
 
 
 
 
e205988
 
 
 
 
 
 
 
 
 
 
 
 
 
86defa3
 
e205988
 
 
 
 
 
 
 
 
86defa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e205988
 
 
86defa3
e205988
 
 
 
 
86defa3
 
 
 
 
 
 
 
 
e205988
86defa3
 
 
 
 
 
 
e205988
86defa3
e205988
86defa3
 
 
e205988
 
86defa3
 
 
 
 
 
 
e205988
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * 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)
})