perf: avoid unnecessary api calls (#161)

* perf: avoid unnecessary api calls

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Jozef Steinhübl
2026-02-05 16:04:59 +01:00
committed by GitHub
parent 3d267786b1
commit 196aaa2bd2
3 changed files with 249 additions and 75 deletions

View File

@@ -1,4 +1,9 @@
import { compareVersions, satisfies, validate } from "compare-versions";
import {
compareVersions,
satisfies,
validate,
validateStrict,
} from "compare-versions";
import { Input } from "./action";
import { getArchitecture, getAvx2, getPlatform, request } from "./utils";
@@ -12,40 +17,48 @@ export async function getDownloadUrl(options: Input): Promise<string> {
}
async function getSemverDownloadUrl(options: Input): Promise<string> {
const res = (await (
await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags", {
headers: options.token
? { "Authorization": `Bearer ${options.token}` }
: {},
})
).json()) as { ref: string }[];
let tags = res
.filter(
(tag) =>
tag.ref.startsWith("refs/tags/bun-v") || tag.ref === "refs/tags/canary",
)
.map((item) => item.ref.replace(/refs\/tags\/(bun-v)?/g, ""))
.filter(Boolean);
const { version, os, arch, avx2, profile } = options;
let tag: string | undefined;
if (validateStrict(version)) {
tag = `bun-v${version}`;
}
let tag = tags.find((t) => t === version);
if (!tag) {
tags = tags.filter((t) => validate(t)).sort(compareVersions);
const res = (await (
await request("https://api.github.com/repos/oven-sh/bun/git/refs/tags", {
headers: options.token
? { "Authorization": `Bearer ${options.token}` }
: {},
})
).json()) as { ref: string }[];
const matchedTag =
version === "latest" || !version
? tags.at(-1)
: tags.filter((t) => satisfies(t, version)).at(-1);
let tags = res
.filter(
(tag) =>
tag.ref.startsWith("refs/tags/bun-v") ||
tag.ref === "refs/tags/canary",
)
.map((item) => item.ref.replace(/refs\/tags\/(bun-v)?/g, ""))
.filter(Boolean);
if (!matchedTag) {
throw new Error(`No Bun release found matching version '${version}'`);
tag = tags.find((t) => t === version);
if (!tag) {
tags = tags.filter((t) => validate(t)).sort(compareVersions);
const matchedTag =
version === "latest" || !version
? tags.at(-1)
: tags.filter((t) => satisfies(t, version)).at(-1);
if (!matchedTag) {
throw new Error(`No Bun release found matching version '${version}'`);
}
tag = `bun-v${matchedTag}`;
} else if (validate(tag)) {
tag = `bun-v${tag}`;
}
tag = `bun-v${matchedTag}`;
} else if (validate(tag)) {
tag = `bun-v${tag}`;
}
const eversion = encodeURIComponent(tag ?? version);