|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { pathToFileURL } from "node:url"; |
| 3 | + |
| 4 | +const sleep = (milliseconds) => |
| 5 | + new Promise((resolve) => setTimeout(resolve, milliseconds)); |
| 6 | + |
| 7 | +export async function refreshCdnCache({ |
| 8 | + repository, |
| 9 | + ref, |
| 10 | + filePath = "nodes.json", |
| 11 | + readFileImpl = readFile, |
| 12 | + fetchImpl = fetch, |
| 13 | + wait = sleep, |
| 14 | + maxAttempts = 21, |
| 15 | + pollIntervalMs = 30_000, |
| 16 | + log = console.log, |
| 17 | +}) { |
| 18 | + const cdnPath = `/gh/${repository}@${ref}/${filePath}`; |
| 19 | + const purgeUrl = `https://purge.jsdelivr.net${cdnPath}`; |
| 20 | + const jsDelivrUrl = `https://cdn.jsdelivr.net${cdnPath}`; |
| 21 | + const mirrorUrl = `https://cdn.jsdmirror.cn${cdnPath}`; |
| 22 | + const expectedContent = await readFileImpl(filePath); |
| 23 | + |
| 24 | + const purgeResponse = await fetchImpl(purgeUrl); |
| 25 | + if (!purgeResponse.ok) { |
| 26 | + throw new Error(`jsDelivr cache purge failed: HTTP ${purgeResponse.status}`); |
| 27 | + } |
| 28 | + |
| 29 | + const purgeResult = await purgeResponse.json(); |
| 30 | + const providers = Object.values( |
| 31 | + purgeResult.paths?.[cdnPath]?.providers ?? {}, |
| 32 | + ); |
| 33 | + if ( |
| 34 | + purgeResult.status !== "finished" || |
| 35 | + providers.length === 0 || |
| 36 | + providers.some((succeeded) => !succeeded) |
| 37 | + ) { |
| 38 | + throw new Error(`jsDelivr cache purge failed: ${JSON.stringify(purgeResult)}`); |
| 39 | + } |
| 40 | + |
| 41 | + log(`jsDelivr cache purged: ${jsDelivrUrl}`); |
| 42 | + |
| 43 | + const contentMatches = async (url) => { |
| 44 | + try { |
| 45 | + const response = await fetchImpl(url, { cache: "no-store" }); |
| 46 | + if (!response.ok) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + const content = Buffer.from(await response.arrayBuffer()); |
| 50 | + return content.equals(expectedContent); |
| 51 | + } catch (error) { |
| 52 | + log(`Cache check failed for ${url}: ${error.message}`); |
| 53 | + return false; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + for (let attempt = 1; attempt <= maxAttempts; attempt += 1) { |
| 58 | + const [jsDelivrReady, mirrorReady] = await Promise.all([ |
| 59 | + contentMatches(jsDelivrUrl), |
| 60 | + contentMatches(mirrorUrl), |
| 61 | + ]); |
| 62 | + |
| 63 | + if (jsDelivrReady && mirrorReady) { |
| 64 | + log(`CDN cache refreshed: ${mirrorUrl}`); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (attempt === maxAttempts) { |
| 69 | + throw new Error( |
| 70 | + `CDN cache did not refresh after ${maxAttempts} checks ` + |
| 71 | + `(jsDelivr: ${jsDelivrReady}, JSDMirror: ${mirrorReady})`, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + log( |
| 76 | + `Waiting for CDN refresh (${attempt}/${maxAttempts}): ` + |
| 77 | + `jsDelivr=${jsDelivrReady}, JSDMirror=${mirrorReady}`, |
| 78 | + ); |
| 79 | + await wait(pollIntervalMs); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +async function main() { |
| 84 | + await refreshCdnCache({ |
| 85 | + repository: process.env.GITHUB_REPOSITORY ?? "DiscoverBox/gitboost", |
| 86 | + ref: process.env.GITHUB_REF_NAME ?? "main", |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 91 | + main().catch((error) => { |
| 92 | + console.error(error.message); |
| 93 | + process.exitCode = 1; |
| 94 | + }); |
| 95 | +} |
0 commit comments