Spaces:
Runtime error
Runtime error
Khunndev Cursor commited on
Commit ·
86defa3
1
Parent(s): 43b8802
Fix Docker build when brand asset fetch returns 429 by using bundled fallbacks.
Browse files
frontend/scripts/brand-assets.fallback.mjs
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
frontend/scripts/fetch-brand-assets.mjs
CHANGED
|
@@ -1,13 +1,19 @@
|
|
| 1 |
/**
|
| 2 |
* Download brand images from the legacy NPI Pipe Count HF space.
|
| 3 |
* HF Spaces reject PNG/ICO in git — assets are fetched at build time instead.
|
|
|
|
| 4 |
*/
|
| 5 |
import { mkdir, writeFile, access } from 'node:fs/promises'
|
| 6 |
import { constants } from 'node:fs'
|
| 7 |
import { dirname, join } from 'node:path'
|
| 8 |
import { fileURLToPath } from 'node:url'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
const BASE = 'https://npisb-pipecountapp.hf.space'
|
| 11 |
const PUBLIC = join(fileURLToPath(new URL('.', import.meta.url)), '..', 'public')
|
| 12 |
|
| 13 |
const FILES = [
|
|
@@ -21,6 +27,8 @@ const FILES = [
|
|
| 21 |
['images/touch/mstile-150x150.png', '/images/touch/mstile-150x150.png'],
|
| 22 |
]
|
| 23 |
|
|
|
|
|
|
|
| 24 |
async function exists(path) {
|
| 25 |
try {
|
| 26 |
await access(path, constants.F_OK)
|
|
@@ -30,29 +38,80 @@ async function exists(path) {
|
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
async function main() {
|
| 34 |
const force = process.env.FORCE_FETCH_ASSETS === '1'
|
| 35 |
let fetched = 0
|
|
|
|
| 36 |
|
| 37 |
for (const [dest, urlPath] of FILES) {
|
| 38 |
const out = join(PUBLIC, dest)
|
| 39 |
if (!force && (await exists(out))) continue
|
| 40 |
|
| 41 |
-
const
|
| 42 |
-
if (
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
-
|
| 46 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if (buf.length < 100) {
|
| 48 |
-
throw new Error(`
|
| 49 |
}
|
| 50 |
-
await
|
| 51 |
-
|
| 52 |
-
console.
|
| 53 |
}
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
main().catch((err) => {
|
|
|
|
| 1 |
/**
|
| 2 |
* Download brand images from the legacy NPI Pipe Count HF space.
|
| 3 |
* HF Spaces reject PNG/ICO in git — assets are fetched at build time instead.
|
| 4 |
+
* On rate limits or network errors, bundled base64 fallbacks are used.
|
| 5 |
*/
|
| 6 |
import { mkdir, writeFile, access } from 'node:fs/promises'
|
| 7 |
import { constants } from 'node:fs'
|
| 8 |
import { dirname, join } from 'node:path'
|
| 9 |
import { fileURLToPath } from 'node:url'
|
| 10 |
+
import { FALLBACK_ASSETS } from './brand-assets.fallback.mjs'
|
| 11 |
+
|
| 12 |
+
const BASES = [
|
| 13 |
+
'https://npisb-pipecountapp.hf.space',
|
| 14 |
+
'https://npisb-pipecountapp-dev.hf.space',
|
| 15 |
+
]
|
| 16 |
|
|
|
|
| 17 |
const PUBLIC = join(fileURLToPath(new URL('.', import.meta.url)), '..', 'public')
|
| 18 |
|
| 19 |
const FILES = [
|
|
|
|
| 27 |
['images/touch/mstile-150x150.png', '/images/touch/mstile-150x150.png'],
|
| 28 |
]
|
| 29 |
|
| 30 |
+
const RETRYABLE = new Set([429, 502, 503, 504])
|
| 31 |
+
|
| 32 |
async function exists(path) {
|
| 33 |
try {
|
| 34 |
await access(path, constants.F_OK)
|
|
|
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
+
function sleep(ms) {
|
| 42 |
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
async function fetchWithRetry(urlPath) {
|
| 46 |
+
let lastStatus = 0
|
| 47 |
+
|
| 48 |
+
for (const base of BASES) {
|
| 49 |
+
for (let attempt = 0; attempt < 4; attempt++) {
|
| 50 |
+
try {
|
| 51 |
+
const res = await fetch(`${base}${urlPath}`)
|
| 52 |
+
lastStatus = res.status
|
| 53 |
+
if (res.ok) {
|
| 54 |
+
return Buffer.from(await res.arrayBuffer())
|
| 55 |
+
}
|
| 56 |
+
if (!RETRYABLE.has(res.status)) break
|
| 57 |
+
} catch {
|
| 58 |
+
/* try next attempt */
|
| 59 |
+
}
|
| 60 |
+
await sleep(400 * (attempt + 1))
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return { error: lastStatus || 'network' }
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
async function writeAsset(dest, buf) {
|
| 68 |
+
const out = join(PUBLIC, dest)
|
| 69 |
+
await mkdir(dirname(out), { recursive: true })
|
| 70 |
+
await writeFile(out, buf)
|
| 71 |
+
return out
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
async function main() {
|
| 75 |
const force = process.env.FORCE_FETCH_ASSETS === '1'
|
| 76 |
let fetched = 0
|
| 77 |
+
let fallback = 0
|
| 78 |
|
| 79 |
for (const [dest, urlPath] of FILES) {
|
| 80 |
const out = join(PUBLIC, dest)
|
| 81 |
if (!force && (await exists(out))) continue
|
| 82 |
|
| 83 |
+
const remote = await fetchWithRetry(urlPath)
|
| 84 |
+
if (Buffer.isBuffer(remote)) {
|
| 85 |
+
if (remote.length < 100) {
|
| 86 |
+
throw new Error(`Suspiciously small file for ${urlPath} (${remote.length} bytes)`)
|
| 87 |
+
}
|
| 88 |
+
await writeAsset(dest, remote)
|
| 89 |
+
fetched++
|
| 90 |
+
console.log(`fetched ${dest} (${remote.length} bytes)`)
|
| 91 |
+
continue
|
| 92 |
}
|
| 93 |
+
|
| 94 |
+
const encoded = FALLBACK_ASSETS[dest]
|
| 95 |
+
if (!encoded) {
|
| 96 |
+
throw new Error(`No remote or bundled asset for ${dest} (last status: ${remote.error})`)
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
const buf = Buffer.from(encoded, 'base64')
|
| 100 |
if (buf.length < 100) {
|
| 101 |
+
throw new Error(`Bundled asset too small for ${dest}`)
|
| 102 |
}
|
| 103 |
+
await writeAsset(dest, buf)
|
| 104 |
+
fallback++
|
| 105 |
+
console.warn(`fallback ${dest} (${buf.length} bytes, remote: ${remote.error})`)
|
| 106 |
}
|
| 107 |
|
| 108 |
+
if (fetched) {
|
| 109 |
+
console.log(`Done — ${fetched} asset(s) fetched.`)
|
| 110 |
+
} else if (fallback) {
|
| 111 |
+
console.log(`Done — ${fallback} asset(s) from bundled fallback.`)
|
| 112 |
+
} else {
|
| 113 |
+
console.log('Brand assets already present.')
|
| 114 |
+
}
|
| 115 |
}
|
| 116 |
|
| 117 |
main().catch((err) => {
|