Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 4894b7d

Browse files
committed
cache mirror list
1 parent 9b2ab31 commit 4894b7d

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

src/zigSetup.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,37 @@ async function updateStatus(context: vscode.ExtensionContext): Promise<void> {
577577
});
578578
}
579579

580+
async function getMirrors(context: vscode.ExtensionContext): Promise<vscode.Uri[]> {
581+
let cached = { time: 0, mirrors: "" };
582+
const key = "mirror-cache";
583+
584+
const cachedStr = context.globalState.get<string>(key);
585+
if (cachedStr !== undefined) {
586+
cached = JSON.parse(cachedStr) as typeof cached;
587+
}
588+
589+
const day = 24 * 60 * 60 * 1000;
590+
if (new Date().getTime() - cached.time > day) {
591+
try {
592+
const response = await fetch("https://ziglang.org/download/community-mirrors.txt");
593+
if (response.status !== 200) throw Error("invalid mirrors");
594+
const mirrorList = await response.text();
595+
cached = {
596+
time: new Date().getTime(),
597+
mirrors: mirrorList,
598+
};
599+
context.globalState.update(key, JSON.stringify(cached));
600+
} catch {
601+
// Cannot fetch mirrors, rely on cache.
602+
}
603+
}
604+
605+
return cached.mirrors
606+
.trim()
607+
.split("\n")
608+
.map((u) => vscode.Uri.parse(u));
609+
}
610+
580611
export async function setupZig(context: vscode.ExtensionContext) {
581612
{
582613
// This check can be removed once enough time has passed so that most users switched to the new value
@@ -674,19 +705,6 @@ export async function setupZig(context: vscode.ExtensionContext) {
674705
break;
675706
}
676707

677-
let mirrors: vscode.Uri[] = [];
678-
try {
679-
const response = await fetch("https://ziglang.org/download/community-mirrors.txt");
680-
if (response.status !== 200) throw Error("invalid mirrors");
681-
const mirrorList = await response.text();
682-
mirrors = mirrorList
683-
.trim()
684-
.split("\n")
685-
.map((u) => vscode.Uri.parse(u));
686-
} catch {
687-
// Cannot fetch mirrors, attempt downloading from canonical source.
688-
}
689-
690708
versionManagerConfig = {
691709
context: context,
692710
title: "Zig",
@@ -695,7 +713,7 @@ export async function setupZig(context: vscode.ExtensionContext) {
695713
/** https://ziglang.org/download */
696714
minisignKey: minisign.parseKey("RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"),
697715
versionArg: "version",
698-
mirrorUrls: mirrors,
716+
mirrorUrls: await getMirrors(context),
699717
canonicalUrl: {
700718
release: vscode.Uri.parse("https://ziglang.org/download"),
701719
nightly: vscode.Uri.parse("https://ziglang.org/builds"),

0 commit comments

Comments
 (0)