Skip to content

Commit 1951fb5

Browse files
committed
fix: handle stale CDN mirrors
1 parent 46a4e5d commit 1951fb5

3 files changed

Lines changed: 72 additions & 10 deletions

File tree

‎scripts/refresh-cdn-cache.mjs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function refreshCdnCache({
1818
const cdnPath = `/gh/${repository}@${ref}/${filePath}`;
1919
const purgeUrl = `https://purge.jsdelivr.net${cdnPath}`;
2020
const jsDelivrUrl = `https://cdn.jsdelivr.net${cdnPath}`;
21-
const mirrorUrl = `https://cdn.jsdmirror.cn${cdnPath}`;
21+
const mirrorUrl = `https://cdn.jsdmirror.com${cdnPath}`;
2222
const expectedContent = await readFileImpl(filePath);
2323

2424
const purgeResponse = await fetchImpl(purgeUrl);
@@ -60,15 +60,21 @@ export async function refreshCdnCache({
6060
contentMatches(mirrorUrl),
6161
]);
6262

63-
if (jsDelivrReady && mirrorReady) {
64-
log(`CDN cache refreshed: ${mirrorUrl}`);
63+
if (jsDelivrReady) {
64+
if (mirrorReady) {
65+
log(`CDN cache refreshed: ${mirrorUrl}`);
66+
} else {
67+
log(
68+
`::warning::JSDMirror cache is still stale; ` +
69+
`jsDelivr has refreshed successfully: ${mirrorUrl}`,
70+
);
71+
}
6572
return;
6673
}
6774

6875
if (attempt === maxAttempts) {
6976
throw new Error(
70-
`CDN cache did not refresh after ${maxAttempts} checks ` +
71-
`(jsDelivr: ${jsDelivrReady}, JSDMirror: ${mirrorReady})`,
77+
`jsDelivr cache did not refresh after ${maxAttempts} checks`,
7278
);
7379
}
7480

‎scripts/refresh-cdn-cache.test.mjs‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const repository = "DiscoverBox/gitboost";
77
const ref = "main";
88
const cdnPath = `/gh/${repository}@${ref}/nodes.enc.json`;
99

10-
test("purges jsDelivr and waits for JSDMirror to match nodes.enc.json", async () => {
10+
test("purges jsDelivr and waits for its content to refresh", async () => {
1111
const expectedContent = '{"version":1}\n';
1212

13+
let jsDelivrChecks = 0;
1314
let mirrorChecks = 0;
1415
const requestedUrls = [];
1516
const fetchImpl = async (url) => {
@@ -20,11 +21,12 @@ test("purges jsDelivr and waits for JSDMirror to match nodes.enc.json", async ()
2021
paths: { [cdnPath]: { providers: { CF: true, FY: true } } },
2122
});
2223
}
23-
if (url.startsWith("https://cdn.jsdmirror.cn/")) {
24+
if (url.startsWith("https://cdn.jsdmirror.com/")) {
2425
mirrorChecks += 1;
2526
return new Response(mirrorChecks === 1 ? "stale" : expectedContent);
2627
}
27-
return new Response(expectedContent);
28+
jsDelivrChecks += 1;
29+
return new Response(jsDelivrChecks === 1 ? "stale" : expectedContent);
2830
};
2931

3032
await refreshCdnCache({
@@ -42,6 +44,61 @@ test("purges jsDelivr and waits for JSDMirror to match nodes.enc.json", async ()
4244
assert.equal(requestedUrls[0], `https://purge.jsdelivr.net${cdnPath}`);
4345
});
4446

47+
test("warns without failing when only JSDMirror remains stale", async () => {
48+
const expectedContent = '{"version":1}\n';
49+
const logs = [];
50+
51+
await refreshCdnCache({
52+
repository,
53+
ref,
54+
readFileImpl: async () => Buffer.from(expectedContent),
55+
fetchImpl: async (url) => {
56+
if (url.startsWith("https://purge.jsdelivr.net/")) {
57+
return Response.json({
58+
status: "finished",
59+
paths: { [cdnPath]: { providers: { CF: true, FY: true } } },
60+
});
61+
}
62+
if (url.startsWith("https://cdn.jsdmirror.com/")) {
63+
return new Response("stale");
64+
}
65+
return new Response(expectedContent);
66+
},
67+
log: (message) => logs.push(message),
68+
});
69+
70+
assert.ok(logs.some((message) => message.startsWith("::warning::")));
71+
});
72+
73+
test("fails when jsDelivr content remains stale", async () => {
74+
const expectedContent = '{"version":1}\n';
75+
76+
await assert.rejects(
77+
refreshCdnCache({
78+
repository,
79+
ref,
80+
readFileImpl: async () => Buffer.from(expectedContent),
81+
fetchImpl: async (url) => {
82+
if (url.startsWith("https://purge.jsdelivr.net/")) {
83+
return Response.json({
84+
status: "finished",
85+
paths: { [cdnPath]: { providers: { CF: true, FY: true } } },
86+
});
87+
}
88+
if (url.startsWith("https://cdn.jsdelivr.net/")) {
89+
return new Response("stale");
90+
}
91+
return new Response(expectedContent);
92+
},
93+
wait: async () => {},
94+
maxAttempts: 2,
95+
pollIntervalMs: 0,
96+
log: () => {},
97+
}),
98+
/jsDelivr cache did not refresh after 2 checks/,
99+
);
100+
});
101+
45102
test("fails when a jsDelivr provider does not purge the cache", async () => {
46103
await assert.rejects(
47104
refreshCdnCache({

‎src-tauri/src/core.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ use std::{
2525
use tempfile::NamedTempFile;
2626
use uuid::Uuid;
2727

28-
const SYSTEM_NODE_CATALOG_URLS: [&str; 4] = [
28+
const SYSTEM_NODE_CATALOG_URLS: [&str; 3] = [
2929
"https://cdn.jsdelivr.net/gh/DiscoverBox/gitboost@main/nodes.enc.json",
30-
"https://cdn.jsdmirror.cn/gh/DiscoverBox/gitboost@main/nodes.enc.json",
3130
"https://cdn.bili33.top/gh/DiscoverBox/gitboost@main/nodes.enc.json",
3231
"https://cdn.jsdmirror.com/gh/DiscoverBox/gitboost@main/nodes.enc.json",
3332
];

0 commit comments

Comments
 (0)