Skip to content

Commit

Permalink
fix GOPROXY parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jul 27, 2024
1 parent 3c64859 commit 4b4ad24
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ const allowDowngrade = parseMixedArg(args["allow-downgrade"]) as PackageArg;
const enabledModes = parseMixedArg(args.modes) as Set<string> || new Set(["npm", "pypi"]);
const githubApiUrl = args.githubapi ? normalizeUrl(args.githubapi) : "https://api.github.com";
const pypiApiUrl = args.pypiapi ? normalizeUrl(args.pypiapi) : "https://pypi.org";
const goProxies = args.goproxy ? normalizeUrl(args.goproxy) : makeGoProxies();
const defaultGoProxy = "https://proxy.golang.org";
const goProxyUrl = args.goproxy ? normalizeUrl(args.goproxy) : (env.GOPROXY || defaultGoProxy);
const stripV = (str: string) => str.replace(/^v/, "");

function matchesAny(str: string, set: PackageArg) {
Expand All @@ -170,6 +170,14 @@ const registryUrl = memize((scope: string, npmrc: Npmrc) => {
return url.endsWith("/") ? url : `${url}/`;
});

function makeGoProxies(): string[] {
if (env.GOPROXY) {
return env.GOPROXY.split(/[,|]/).map(s => s.trim()).filter(s => (Boolean(s) && s !== "direct"));
} else {
return [defaultGoProxy];
}
}

function findUpSync(filename: string, dir: string): string | null {
const path = join(dir, filename);
try { accessSync(path); return path; } catch {}
Expand Down Expand Up @@ -246,12 +254,19 @@ function splitPlainText(str: string): string[] {
return str.split(/\r?\n/).map(s => s.trim()).filter(Boolean);
}

async function fetchGoVersionInfo(modulePath: string, version: string, agentOpts: AgentOptions, proxyUrl: string) {
async function fetchGoVersionInfo(modulePath: string, version: string, agentOpts: AgentOptions, proxies: string[]) {
const proxyUrl = proxies.shift();
if (!proxyUrl) {
throw new Error("No more go proxies available");
}

const url = `${proxyUrl}/${modulePath.toLowerCase()}/${version === "latest" ? "@latest" : `@v/${version}.info`}`;
const res = await doFetch(url, getFetchOpts(agentOpts));
if ([404, 410].includes(res.status) && proxyUrl !== defaultGoProxy) {
return fetchGoVersionInfo(modulePath, version, agentOpts, defaultGoProxy);

if ([404, 410].includes(res.status) && proxies.length) {
return fetchGoVersionInfo(modulePath, version, agentOpts, proxies);
}

if (res?.ok) {
return res.json();
} else {
Expand Down Expand Up @@ -1034,7 +1049,8 @@ async function main() {
} else if (mode === "pypi") {
return fetchPypiInfo(name, type, agentOpts);
} else {
const data: Record<string, any> = await fetchGoVersionInfo(name, "latest", agentOpts, goProxyUrl);
const proxies = Array.from(goProxies);
const data: Record<string, any> = await fetchGoVersionInfo(name, "latest", agentOpts, proxies);
return [data, "deps", null, name];
}
}), {concurrency});
Expand Down

0 comments on commit 4b4ad24

Please sign in to comment.