Skip to content

Commit

Permalink
Remove unused install.js and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Nov 26, 2024
1 parent b485c08 commit d9de1d8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 205 deletions.
54 changes: 0 additions & 54 deletions npm/lib/download-release.js

This file was deleted.

83 changes: 47 additions & 36 deletions npm/lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const URL = url.URL;
const child_process = require("child_process");
const proxy_from_env = require("proxy-from-env");

const get = require("./get");

const packageVersion = require("../package.json").version;
const tmpDir = path.join(os.tmpdir(), `rustywind-cache-${packageVersion}`);

Expand Down Expand Up @@ -79,57 +77,68 @@ function downloadWin(url, dest, opts) {
});
}

function download(_url, dest, opts) {
async function download(_url, dest, opts) {
// Handle proxy setup
const proxy = proxy_from_env.getProxyForUrl(url.parse(_url));
if (proxy !== "") {
var HttpsProxyAgent = require("https-proxy-agent");
if (proxy) {
const HttpsProxyAgent = require("https-proxy-agent");
opts = {
...opts,
agent: new HttpsProxyAgent(proxy),
proxy,
};
}

// Windows-specific handling
if (isWindows) {
// This alternative strategy shouldn't be necessary but sometimes on Windows the file does not get closed,
// so unzipping it fails, and I don't know why.
return downloadWin(_url, dest, opts);
}

// Remove auth headers for non-GitHub URLs
if (opts.headers.authorization && !isGithubUrl(_url)) {
delete opts.headers.authorization;
}

return new Promise((resolve, reject) => {
console.log(`Download options: ${JSON.stringify(opts)}`);
const outFile = fs.createWriteStream(dest);
const mergedOpts = {
...url.parse(_url),
...opts,
};
https
.get(mergedOpts, (response) => {
console.log("statusCode: " + response.statusCode);
if (response.statusCode === 302) {
console.log("Following redirect to: " + response.headers.location);
return download(response.headers.location, dest, opts).then(
resolve,
reject
);
} else if (response.statusCode !== 200) {
reject(new Error("Download failed with " + response.statusCode));
return;
}
const mergedOpts = { ...url.parse(_url), ...opts };

response.pipe(outFile);
outFile.on("finish", () => {
resolve();
});
})
.on("error", async (err) => {
await fsUnlink(dest);
reject(err);
});
const request = https.get(mergedOpts, (response) => {
// Handle redirects
if (response.statusCode === 302) {
outFile.destroy();
return download(response.headers.location, dest, opts).then(resolve, reject);
}

// Handle error status codes
if (response.statusCode !== 200) {
outFile.destroy();
reject(new Error(`Download failed with ${response.statusCode}`));
return;
}

// Pipe the response to file
response.pipe(outFile);
});

// Handle write stream errors
outFile.on('error', async (err) => {
outFile.destroy();
await fsUnlink(dest).catch(() => { });
reject(err);
});

// Handle successful completion
outFile.on('finish', () => {
outFile.close(() => resolve(null));
});

// Handle request errors
request.on('error', async (err) => {
outFile.destroy();
await fsUnlink(dest).catch(() => { });
reject(err);
});
});
}

Expand Down Expand Up @@ -229,6 +238,8 @@ function untar(zipPath, destinationDir) {
if (code !== 0) {
reject(new Error(`tar xvf exited with ${code}`));
return;
} else {
process.exit(0);
}

resolve();
Expand Down Expand Up @@ -281,7 +292,7 @@ module.exports = async (opts) => {
console.log("Deleting invalid download cache");
try {
await fsUnlink(assetDownloadPath);
} catch (e) {}
} catch (e) { }

throw e;
}
Expand All @@ -300,7 +311,7 @@ module.exports = async (opts) => {

try {
await fsUnlink(assetDownloadPath);
} catch (e) {}
} catch (e) { }

throw e;
}
Expand Down
107 changes: 0 additions & 107 deletions npm/lib/install.js

This file was deleted.

30 changes: 22 additions & 8 deletions npm/lib/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
"use strict";

// Imports
const os = require("os");
const fs = require("fs");
const path = require("path");
Expand All @@ -19,14 +20,26 @@ if (forceInstall) {
const { VERSION } = require("./constants");
const BIN_PATH = path.join(__dirname, "../bin");


////////////////////////////////////////////////////////////////////////////////
const APP_NAME = "rustywind";
const REPO = "avencera/rustywind";
const GITHUB_REPO = `https://github.com/${REPO}`;
////////////////////////////////////////////////////////////////////////////////

process.on("unhandledRejection", (reason, promise) => {
console.log("Unhandled rejection: ", promise, "reason:", reason);
});

function getTarget() {
const arch = process.env.npm_config_arch || os.arch();
const arch = os.arch();
const platform = os.platform();

switch (os.platform()) {
console.log(`Downloading: ${APP_NAME}`);
console.log(` from: ${GITHUB_REPO}`);
console.log(` for platform: ${arch}-${platform}\n`);

switch (platform) {
case "darwin":
return arch == "x64" ? "x86_64-apple-darwin" : "aarch64-apple-darwin";
case "win32":
Expand All @@ -35,12 +48,12 @@ function getTarget() {
return arch === "x64"
? "x86_64-unknown-linux-musl"
: arch === "arm"
? "arm-unknown-linux-gnueabihf"
: arch === "arm64"
? "aarch64-unknown-linux-gnu"
: arch === "ppc64"
? "powerpc64le-unknown-linux-gnu"
: "i686-unknown-linux-musl";
? "arm-unknown-linux-gnueabihf"
: arch === "arm64"
? "aarch64-unknown-linux-gnu"
: arch === "ppc64"
? "powerpc64le-unknown-linux-gnu"
: "i686-unknown-linux-musl";
default:
throw new Error("Unknown platform: " + os.platform());
}
Expand All @@ -59,6 +72,7 @@ async function main() {
destDir: BIN_PATH,
force: forceInstall,
};

try {
await download(opts);
} catch (err) {
Expand Down

0 comments on commit d9de1d8

Please sign in to comment.