Update utils.ts

This commit is contained in:
tcely 2026-03-14 19:49:22 -04:00
parent 02836b5321
commit 135f29cf99
No known key found for this signature in database
GPG Key ID: 9F1DAF9D9070430F

View File

@ -5,6 +5,8 @@ import { existsSync, readFileSync, renameSync } from "node:fs";
import { resolve, basename } from "node:path"; import { resolve, basename } from "node:path";
import { compareVersions, validate } from "compare-versions"; import { compareVersions, validate } from "compare-versions";
import { getStoredResponse, setStoredResponse } from "./response-storage";
// First Bun version that ships native Windows ARM64 binaries. // First Bun version that ships native Windows ARM64 binaries.
const WINDOWS_ARM64_MIN_VERSION = "1.3.10"; const WINDOWS_ARM64_MIN_VERSION = "1.3.10";
@ -26,6 +28,14 @@ export async function request(
headers.set("User-Agent", "@oven-sh/setup-bun"); headers.set("User-Agent", "@oven-sh/setup-bun");
} }
const canUseResponseCache = "GET" === (init?.method ?? "GET").toUpperCase();
if (canUseResponseCache) {
const stored = getStoredResponse(url);
if (stored) {
return stored;
}
}
const res = await fetch(url, { const res = await fetch(url, {
...init, ...init,
headers, headers,
@ -37,6 +47,10 @@ export async function request(
); );
} }
if (canUseResponseCache) {
await setStoredResponse(url, res);
}
return res; return res;
} }
@ -178,3 +192,20 @@ export function readVersionFromFile(
} }
} }
} }
/**
* Returns the URL with query-string and fragment removed.
* Safe to call on any string; returns the original on parse failure.
*/
export function stripUrlCredentials(url: string): string {
try {
const u = new URL(url);
u.username = "";
u.password = "";
u.search = "";
u.hash = "";
return u.toString();
} catch {
return url;
}
}