Skip to content

Commit c189822

Browse files
committed
Mirror no-proxy JSON when available
1 parent ab81ae6 commit c189822

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

scripts/mirror-public-json.mjs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function main () {
3232
await writeFile(path.join(OUTPUT_DIR, '.nojekyll'), '')
3333
await writeFile(path.join(OUTPUT_DIR, 'CNAME'), `${CNAME}\n`)
3434

35-
const modulesText = await fetchText(new URL('/modules.json', source))
35+
const modulesText = await fetchFirstText(source, ['/modules-no-proxy.json', '/modules.json'])
3636
const modules = JSON.parse(modulesText)
3737
if (!Array.isArray(modules)) throw new Error('/modules.json is not an array')
3838

@@ -44,8 +44,12 @@ async function main () {
4444

4545
console.log(`[mirror] Mirroring ${names.length} module JSON files`)
4646
await mapLimit(names, CONCURRENCY, async (name, index) => {
47-
const moduleUrl = new URL(`/module/${encodeURIComponent(name)}.json`, source)
48-
await writeFile(path.join(OUTPUT_DIR, 'module', `${name}.json`), await fetchText(moduleUrl))
47+
const encodedName = encodeURIComponent(name)
48+
const moduleText = await fetchFirstText(source, [
49+
`/module-no-proxy/${encodedName}.json`,
50+
`/module/${encodedName}.json`
51+
])
52+
await writeFile(path.join(OUTPUT_DIR, 'module', `${name}.json`), moduleText)
4953

5054
if ((index + 1) % 50 === 0 || index + 1 === names.length) {
5155
console.log(`[mirror] ${index + 1}/${names.length}`)
@@ -105,10 +109,20 @@ function isWantedDeployment (deployment) {
105109
deployment.deployment_trigger?.metadata?.branch === BRANCH
106110
}
107111

108-
async function fetchText (url) {
109-
const response = await fetchWithRetry(url, { headers: sourceHeaders() })
110-
if (!response.ok) throw new Error(`Failed to fetch ${url.pathname}: ${response.status}`)
111-
return response.text()
112+
async function fetchFirstText (source, paths) {
113+
let firstError
114+
115+
for (const pathname of paths) {
116+
const url = new URL(pathname, source)
117+
const response = await fetchWithRetry(url, { headers: sourceHeaders() })
118+
if (response.ok) return response.text()
119+
120+
const error = new Error(`Failed to fetch ${url.pathname}: ${response.status}`)
121+
firstError ||= error
122+
if (response.status !== 404) throw error
123+
}
124+
125+
throw firstError || new Error('No source path provided')
112126
}
113127

114128
async function fetchJson (url, headers) {

0 commit comments

Comments
 (0)