Skip to content

Commit

Permalink
Add undici redirect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Sep 13, 2024
1 parent 1917bfc commit f725afd
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/utils/download.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
rmSync,
} from "fs";
import { mkdir } from "fs/promises";
import { arch, homedir, tmpdir } from "os";
import { homedir, tmpdir } from "os";
import { basename, dirname, join } from "path";
import { join as joinPosix } from "path/posix";
import Logger, { LoggerSource } from "../logger.mjs";
Expand Down Expand Up @@ -235,12 +235,24 @@ export async function downloadAndInstallArchive(
let isSuccess = false;

if (process.platform !== "linux" || process.arch !== "arm64") {
isSuccess = await downloadFileUndici(
const undiciRet = await downloadFileUndici(
client,
downloadUrl,
archiveFilePath,
logName
);
if (typeof undiciRet === "string") {
return downloadAndInstallArchive(
url,
targetDirectory,
archiveFileName,
logName,
extraCallback,
undiciRet,
extraHeaders
)
}
isSuccess = undiciRet;
} else {
isSuccess = await downloadFileGot(
downloadUrl,
Expand Down Expand Up @@ -292,8 +304,8 @@ async function downloadFileUndici(
downloadUrl: URL,
archiveFilePath: string,
logName: string
): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
): Promise<boolean | string> {
return new Promise<boolean | string>((resolve, reject) => {
const requestOptions: Dispatcher.RequestOptions = {
path: downloadUrl.pathname + downloadUrl.search,
method: "GET",
Expand Down Expand Up @@ -329,7 +341,9 @@ async function downloadFileUndici(
LoggerSource.downloader,
`Redirecting to ${headers.location.toString()}`
);
resolve(false); // Handle redirects in the calling function
resolve(
headers.location.toString()
); // Handle redirects in the calling function
}

return writeStream; // Return the Writable stream where data is piped
Expand Down

0 comments on commit f725afd

Please sign in to comment.