download using node https over curl
This commit is contained in:
parent
37139c9e11
commit
e98307aafa
2 changed files with 83 additions and 6 deletions
|
|
@ -1,10 +1,85 @@
|
|||
const { fork, execSync } = require("child_process");
|
||||
const fs = require("fs");
|
||||
const https = require("https");
|
||||
const path = require("path");
|
||||
|
||||
const { rimrafSync } = require("rimraf");
|
||||
|
||||
const { execCmdSync } = require("../../../scripts/util");
|
||||
|
||||
/**
|
||||
* download a file using nodejs http
|
||||
* @param {string} url
|
||||
* @param {string} outputPath
|
||||
* @param {number} maxRedirects
|
||||
*/
|
||||
async function downloadFile(url, outputPath, maxRedirects = 5) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const downloadWithRedirects = (currentUrl, redirectCount = 0) => {
|
||||
if (redirectCount > maxRedirects) {
|
||||
return reject(new Error(`Too many redirects (${maxRedirects})`));
|
||||
}
|
||||
|
||||
const request = https.get(currentUrl, (response) => {
|
||||
if (
|
||||
response.statusCode >= 300 &&
|
||||
response.statusCode < 400 &&
|
||||
response.headers.location
|
||||
) {
|
||||
return downloadWithRedirects(
|
||||
response.headers.location,
|
||||
redirectCount + 1,
|
||||
);
|
||||
}
|
||||
|
||||
if (response.statusCode !== 200) {
|
||||
return reject(
|
||||
new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`),
|
||||
);
|
||||
}
|
||||
|
||||
const outputDir = path.dirname(outputPath);
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
}
|
||||
|
||||
const writeStream = fs.createWriteStream(outputPath);
|
||||
|
||||
const totalSize = parseInt(response.headers["content-length"], 10);
|
||||
let downloadedSize = 0;
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
downloadedSize += chunk.length;
|
||||
if (totalSize) {
|
||||
const percent = ((downloadedSize / totalSize) * 100).toFixed(1);
|
||||
process.stdout.write(
|
||||
`\rDownloading: ${percent}% (${downloadedSize}/${totalSize} bytes)`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
response.pipe(writeStream);
|
||||
|
||||
writeStream.on("finish", () => {
|
||||
console.log(`\nDownload completed: ${outputPath}`);
|
||||
resolve(outputPath);
|
||||
});
|
||||
|
||||
writeStream.on("error", reject);
|
||||
response.on("error", reject);
|
||||
});
|
||||
|
||||
request.on("error", reject);
|
||||
request.setTimeout(30000, () => {
|
||||
request.destroy();
|
||||
reject(new Error("Request timeout"));
|
||||
});
|
||||
};
|
||||
|
||||
downloadWithRedirects(url);
|
||||
});
|
||||
}
|
||||
|
||||
async function installAndCopySqlite(target) {
|
||||
// Replace the installed with pre-built
|
||||
console.log("[info] Downloading pre-built sqlite3 binary");
|
||||
|
|
@ -18,8 +93,9 @@ async function installAndCopySqlite(target) {
|
|||
"win32-arm64":
|
||||
"https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz",
|
||||
}[target];
|
||||
execCmdSync(
|
||||
`curl -L -o ../../core/node_modules/sqlite3/build.tar.gz ${downloadUrl}`,
|
||||
await downloadFile(
|
||||
downloadUrl,
|
||||
"../../core/node_modules/sqlite3/build.tar.gz",
|
||||
);
|
||||
execCmdSync("cd ../../core/node_modules/sqlite3 && tar -xvzf build.tar.gz");
|
||||
fs.unlinkSync("../../core/node_modules/sqlite3/build.tar.gz");
|
||||
|
|
@ -30,8 +106,9 @@ async function installAndCopyEsbuild(target) {
|
|||
console.log("[info] Downloading pre-built esbuild binary");
|
||||
rimrafSync("node_modules/@esbuild");
|
||||
fs.mkdirSync("node_modules/@esbuild", { recursive: true });
|
||||
execCmdSync(
|
||||
`curl -o node_modules/@esbuild/esbuild.zip https://continue-server-binaries.s3.us-west-1.amazonaws.com/${target}/esbuild.zip`,
|
||||
await downloadFile(
|
||||
`https://continue-server-binaries.s3.us-west-1.amazonaws.com/${target}/esbuild.zip`,
|
||||
"node_modules/@esbuild/esbuild.zip",
|
||||
);
|
||||
execCmdSync("cd node_modules/@esbuild && unzip esbuild.zip");
|
||||
fs.unlinkSync("node_modules/@esbuild/esbuild.zip");
|
||||
|
|
|
|||
|
|
@ -281,9 +281,9 @@ void (async () => {
|
|||
);
|
||||
|
||||
await Promise.all([
|
||||
copyLanceDB(packageToInstall, "@lancedb"),
|
||||
copySqlite(target),
|
||||
copyEsbuild(target),
|
||||
copySqlite(target),
|
||||
copyLanceDB(packageToInstall, "@lancedb"),
|
||||
]);
|
||||
} else {
|
||||
// Download esbuild from npm in tmp and copy over
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue