Skip to content

Update packages for security, replace request and some tidy up #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
95 changes: 41 additions & 54 deletions binstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var fs = require("fs");
var request = require("request");
var axios = require("axios");
var tar = require("tar");
var zlib = require("zlib");
var unzip = require("unzip-stream");
Expand All @@ -18,18 +18,18 @@ function untgz(url, path, options) {
var verbose = options.verbose;
var verify = options.verify;

return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var untar = tar
.x({ cwd: path })
.on("error", function(error) {
.on("error", function (error) {
reject("Error extracting " + url + " - " + error);
})
.on("end", function() {
.on("end", function () {
var successMessage = "Successfully downloaded and processed " + url;

if (verify) {
verifyContents(verify)
.then(function() {
.then(function () {
resolve(successMessage);
})
.catch(reject);
Expand All @@ -38,39 +38,32 @@ function untgz(url, path, options) {
}
});

var gunzip = zlib.createGunzip().on("error", function(error) {
var gunzip = zlib.createGunzip().on("error", function (error) {
reject("Error decompressing " + url + " " + error);
});

try {
fs.mkdirSync(path);
} catch (error) {
if (error.code !== 'EEXIST') throw error;
if (error.code !== "EEXIST") throw error;
}

request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;

reject(new Error(errorMessage));
return;
}
if (verbose) {
console.log("Downloading binaries from " + url);
}

axios
.get(url, { responseType: "stream" })
.then((response) => {
response.data.pipe(gunzip).pipe(untar);
})
.catch((error) => {
if (verbose) {
console.log("Downloading binaries from " + url);
console.error(error);
} else {
console.error(error.message);
}

response.on("error", function() {
reject("Error receiving " + url);
});
})
.pipe(gunzip)
.pipe(untar);
});
});
}

Expand All @@ -80,21 +73,21 @@ function unzipUrl(url, path, options) {
var verbose = options.verbose;
var verify = options.verify;

return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var writeStream = unzip
.Extract({ path: path })
.on("error", function(error) {
.on("error", function (error) {
reject("Error extracting " + url + " - " + error);
})
.on("entry", function(entry) {
.on("entry", function (entry) {
console.log("Entry: " + entry.path);
})
.on("close", function() {
.on("close", function () {
var successMessage = "Successfully downloaded and processed " + url;

if (verify) {
verifyContents(verify)
.then(function() {
.then(function () {
resolve(successMessage);
})
.catch(reject);
Expand All @@ -103,36 +96,30 @@ function unzipUrl(url, path, options) {
}
});

request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;

reject(new Error(errorMessage));
return;
}
if (verbose) {
console.log("Downloading binaries from " + url);
}

axios
.get(url, { responseType: "stream" })
.then((response) => {
response.data.pipe(writeStream);
})
.catch((error) => {
if (verbose) {
console.log("Downloading binaries from " + url);
console.error(error);
} else {
console.error(error.message);
}

response.on("error", function() {
reject("Error receiving " + url);
});
})
.pipe(writeStream);
});
});
}

function verifyContents(files) {
return Promise.all(
files.map(function(filePath) {
return new Promise(function(resolve, reject) {
fs.stat(filePath, function(err, stats) {
files.map(function (filePath) {
return new Promise(function (resolve, reject) {
fs.stat(filePath, function (err, stats) {
if (err) {
reject(filePath + " was not found.");
} else if (!stats.isFile()) {
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ var install = require(path.join(__dirname, "install"));
var prepare = require(path.join(__dirname, "prepare"));
var test = require(path.join(__dirname, "test"));

module.exports = function(config) {
module.exports = function (config) {
var paths = {};
config.binaries.forEach(function(binary) {
config.binaries.forEach(function (binary) {
paths[binary] = path.resolve(path.join(config.dirname, "bin", binary));
});
return {
paths: paths,
install: function(unpackedBinPath, os, arch) {
install: function (unpackedBinPath, os, arch) {
return install(config, unpackedBinPath, os, arch);
},
prepare: function() {
prepare: function () {
return prepare(config);
},
test: function() {
test: function () {
return test(config);
}
},
};
};
4 changes: 2 additions & 2 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = function install(config, unpackedBinPath, os, arch) {
if (!url) {
throw new Error("No binaries are available for your platform: " + buildId);
}
return binstall(url, unpackedBinPath).then(function() {
config.binaries.forEach(function(bin) {
return binstall(url, unpackedBinPath).then(function () {
config.binaries.forEach(function (bin) {
fs.chmodSync(path.join(unpackedBinPath, bin + binExt), "755");
});
});
Expand Down
Loading