From a0fbb61a280eb25b2801b5ef93d6c41e2da337a3 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 19 Aug 2018 23:45:58 -0400 Subject: [PATCH] Try using something simpler than request-stream --- binstall.js | 38 +++++++++++++++++++++++--------------- package.json | 1 - request.js | 22 ++++++++++++++++++++++ test.js | 18 +++++++++++------- 4 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 request.js diff --git a/binstall.js b/binstall.js index e433c26..8f1c0b7 100644 --- a/binstall.js +++ b/binstall.js @@ -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"); @@ -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) { @@ -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(); }); } @@ -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; @@ -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(); }); } diff --git a/package.json b/package.json index 769319f..cf5cdb0 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ }, "dependencies": { "mustache": "^2.3.0", - "request-stream": "1.2.2", "tar": "^2.2.1", "unzip-stream": "^0.3.0" } diff --git a/request.js b/request.js new file mode 100644 index 0000000..9392728 --- /dev/null +++ b/request.js @@ -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); + } +} + diff --git a/test.js b/test.js index 61120bc..ef8aa9c 100644 --- a/test.js +++ b/test.js @@ -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 = { @@ -56,13 +56,9 @@ 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 { @@ -70,6 +66,14 @@ module.exports = function test(config) { return resolve(); } }); + + req.on("error", function(err) { + console.error(" - Failed to fetch " + url + " " + err.message); + errors.push(displayUrl); + return reject(); + }); + + req.end(); }); }); }