Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 62 additions & 16 deletions src/commands/install/download-asset.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,78 @@
import { createWriteStream } from "node:fs";
import { createWriteStream, rmSync } from "node:fs";
import { pipeline } from "node:stream/promises";
import { EnvHttpProxyAgent, fetch } from "undici";
import type { Asset } from "./types";
import { ERR_INSTALL_FAILED_TO_DOWNLOAD_ASSET } from "../../errors";

const dispatcher = new EnvHttpProxyAgent();

const DOWNLOAD_BACKOFFS = [500, 1000, 2000, 5000, 8000];

function isRetryableDownloadError(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
}

const code = "code" in error ? error.code : undefined;

return [
"ECONNRESET",
"ECONNREFUSED",
"ECONNABORTED",
"ETIMEDOUT",
"EAI_AGAIN",
].includes(code as string);
}

function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function downloadAsset(
asset: Asset,
source: string,
destination: string,
): Promise<void> {
const response = await fetch(source, {
dispatcher,
});
let lastError: unknown;

if (!response.ok || !response.body) {
throw new Error(
`${ERR_INSTALL_FAILED_TO_DOWNLOAD_ASSET}: ${asset.asset} (${response.status} ${response.statusText})`,
);
}
for (let attempt = 0; attempt <= DOWNLOAD_BACKOFFS.length; attempt++) {
try {
const response = await fetch(source, {
dispatcher,
});

try {
await pipeline(response.body, createWriteStream(destination));
} catch (cause) {
throw new Error(
`${ERR_INSTALL_FAILED_TO_DOWNLOAD_ASSET}: ${asset.asset} (${response.status} ${response.statusText})`,
{ cause },
);
if (!response.ok || !response.body) {
if (response.status >= 500 && attempt < DOWNLOAD_BACKOFFS.length) {
await delay(DOWNLOAD_BACKOFFS[attempt]);

continue;
}

throw new Error(
`${ERR_INSTALL_FAILED_TO_DOWNLOAD_ASSET}: ${asset.asset} (${response.status} ${response.statusText})`,
);
}

await pipeline(response.body, createWriteStream(destination));

return;
} catch (cause) {
lastError = cause;

rmSync(destination, { force: true });

if (
!isRetryableDownloadError(cause) ||
attempt >= DOWNLOAD_BACKOFFS.length
) {
break;
}

await delay(DOWNLOAD_BACKOFFS[attempt]);
}
}

throw new Error(`${ERR_INSTALL_FAILED_TO_DOWNLOAD_ASSET}: ${asset.asset}`, {
cause: lastError,
});
}
Loading