refactor: cleanup

This commit is contained in:
Jozef Steinhübl 2026-01-04 23:58:17 +01:00
parent cafd14a538
commit 0df7a9b56c
No known key found for this signature in database
GPG Key ID: 8D9A5E4549AA84FD
3 changed files with 20 additions and 27 deletions

View File

@ -17,7 +17,7 @@ import { Registry } from "./registry";
import { writeBunfig } from "./bunfig";
import { saveState } from "@actions/core";
import { addExtension } from "./utils";
import { DownloadMeta, getDownloadMeta } from "./download-url";
import { getDownloadUrl } from "./download-url";
import { cwd } from "node:process";
export type Input = {
@ -29,7 +29,7 @@ export type Input = {
profile?: boolean;
registries?: Registry[];
noCache?: boolean;
token: string;
token?: string;
};
export type Output = {
@ -51,8 +51,7 @@ export default async (options: Input): Promise<Output> => {
const bunfigPath = join(cwd(), "bunfig.toml");
writeBunfig(bunfigPath, options.registries);
const downloadMeta = await getDownloadMeta(options);
const url = downloadMeta.url;
const url = await getDownloadUrl(options);
const cacheEnabled = isCacheEnabled(options);
@ -110,7 +109,7 @@ export default async (options: Input): Promise<Output> => {
if (!cacheHit) {
info(`Downloading a new version of Bun: ${url}`);
revision = await downloadBun(downloadMeta, bunPath);
revision = await downloadBun(url, bunPath);
}
}
@ -164,11 +163,11 @@ function isVersionMatch(
}
async function downloadBun(
downloadMeta: DownloadMeta,
url: string,
bunPath: string,
): Promise<string | undefined> {
// Workaround for https://github.com/oven-sh/setup-bun/issues/79 and https://github.com/actions/toolkit/issues/1179
const zipPath = addExtension(await downloadTool(downloadMeta.url), ".zip");
const zipPath = addExtension(await downloadTool(url), ".zip");
const extractedZipPath = await extractZip(zipPath);
const extractedBunPath = await extractBun(extractedZipPath);
try {

View File

@ -2,28 +2,16 @@ import { compareVersions, satisfies, validate } from "compare-versions";
import { Input } from "./action";
import { getArchitecture, getPlatform, request } from "./utils";
export interface DownloadMeta {
url: string;
auth?: string;
}
export async function getDownloadMeta(options: Input): Promise<DownloadMeta> {
export async function getDownloadUrl(options: Input): Promise<string> {
const { customUrl } = options;
if (customUrl) {
return {
url: customUrl,
};
return customUrl;
}
return await getSemverDownloadMeta(options);
return await getSemverDownloadUrl(options);
}
interface Run {
id: string;
head_sha: string;
}
async function getSemverDownloadMeta(options: Input): Promise<DownloadMeta> {
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
@ -71,7 +59,5 @@ async function getSemverDownloadMeta(options: Input): Promise<DownloadMeta> {
"https://github.com/oven-sh/bun/releases/download/",
);
return {
url: href,
};
return href;
}

View File

@ -7,7 +7,15 @@ export async function request(
url: string,
init?: RequestInit,
): Promise<Response> {
const res = await fetch(url, init);
const headers = new Headers(init?.headers);
if (!headers.has("User-Agent")) {
headers.set("User-Agent", "@oven-sh/setup-bun");
}
const res = await fetch(url, {
...init,
headers,
});
if (!res.ok) {
const body = await res.text().catch(() => "");
throw new Error(