Skip to content

Commit

Permalink
Remove request-stream in favor of plain http/https
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Feldman committed Aug 20, 2018
1 parent 6a39d4a commit 1693f22
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
38 changes: 23 additions & 15 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-stream");
var request = require("./request.js");
var tar = require("tar");
var zlib = require("zlib");
var unzip = require("unzip-stream");
Expand Down Expand Up @@ -42,16 +42,13 @@ function untgz(url, path, options) {
reject("Error decompressing " + url + " " + error);
});

request(url, {method: "GET"}, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
var req = request(url, {method: "GET"});

req.on("response", function(response) {
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;

reject(new Error(errorMessage));
return;
throw new Error(errorMessage);
}

if (verbose) {
Expand All @@ -63,7 +60,14 @@ function untgz(url, path, options) {
});

response.pipe(gunzip).pipe(untar);
})
});

req.on("error", function(error) {
reject("Error communicating with URL " + url + " " + error);
return;
});

req.end();
});
}

Expand Down Expand Up @@ -96,12 +100,9 @@ function unzipUrl(url, path, options) {
}
});

request(url, {method: "GET"}, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
var req = request(url, {method: "GET"});

req.on("response", function(response) {
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;

Expand All @@ -118,7 +119,14 @@ function unzipUrl(url, path, options) {
});

response.pipe(writeStream);
})
});

req.on("error", function(error) {
reject("Error communicating with URL " + url + " " + error);
return;
});

req.end();
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"homepage": "https://github.com/avh4/binwrap#readme",
"files": [
"install.js",
"request.js",
"prepare.js",
"test.js",
"binstall.js",
Expand All @@ -40,7 +41,6 @@
},
"dependencies": {
"mustache": "^2.3.0",
"request-stream": "1.2.2",
"tar": "^2.2.1",
"unzip-stream": "^0.3.0"
}
Expand Down
22 changes: 22 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var http = require("http");
var https = require("https");
var parseUrl = require("url").parse;

// Inspired by https://github.com/maxogden/request-stream

module.exports = function request(url, opts) {
var parsed = parseUrl(url);

var finalOpts = Object.assign({
host: parsed.hostname,
path: parsed.path,
port: parsed.port
}, opts);

if (parsed.protocol === "https:") {
return https.request(finalOpts);
} else {
return http.request(finalOpts);
}
}

18 changes: 11 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var request = require("request-stream");
var request = require("./request.js");

// https://nodejs.org/api/os.html#os_os_platform
var validPlatforms = {
Expand Down Expand Up @@ -56,20 +56,24 @@ module.exports = function test(config) {
} else {
return p.then(function() {
return new Promise(function(resolve, reject) {
request(url, {method: "GET"}, function(err, response) {
if (err) {
console.error(" - Failed to fetch " + url + " " + err.message);
errors.push(displayUrl);
return reject();
}
var req = request(url, {method: "GET"});

req.on("response", function(response) {
if (response.statusCode != 200) {
throw new Error("Status code " + response.statusCode);
} else {
console.log("OKAY: " + displayUrl);
return resolve();
}
});

req.on("error", function(err) {
console.error(" - Failed to fetch " + url + " " + err.message);
errors.push(displayUrl);
return reject();
});

req.end();
});
});
}
Expand Down

0 comments on commit 1693f22

Please sign in to comment.