From ec99dabb4c84a6e01b6f676cabb0163db799c107 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 19 Aug 2018 23:13:19 -0400 Subject: [PATCH 1/6] Use request-stream over request-promise --- package-lock.json | 42 ++++++++++-------------------------------- package.json | 2 +- test.js | 22 +++++++++++----------- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7ce561d..37b9027 100644 --- a/package-lock.json +++ b/package-lock.json @@ -171,11 +171,6 @@ "inherits": "~2.0.0" } }, - "bluebird": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", - "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" - }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -376,7 +371,6 @@ "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "dev": true, "requires": { "ms": "2.0.0" } @@ -1137,7 +1131,8 @@ "lodash": { "version": "4.17.10", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" + "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", + "dev": true }, "lru-cache": { "version": "4.1.3", @@ -1231,8 +1226,7 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", - "dev": true + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "mustache": { "version": "2.3.0", @@ -1500,23 +1494,13 @@ "uuid": "^3.1.0" } }, - "request-promise": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.2.tgz", - "integrity": "sha1-0epG1lSm7k+O5qT+oQGMIpEZBLQ=", - "requires": { - "bluebird": "^3.5.0", - "request-promise-core": "1.1.1", - "stealthy-require": "^1.1.0", - "tough-cookie": ">=2.3.3" - } - }, - "request-promise-core": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.1.tgz", - "integrity": "sha1-Pu4AssWqgyOc+wTFcA2jb4HNCLY=", + "request-stream": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/request-stream/-/request-stream-1.2.2.tgz", + "integrity": "sha1-G60CxiD1rz1gRAAlRm6c4CwbbdU=", "requires": { - "lodash": "^4.13.1" + "debug": "^2.2.0", + "xtend": "^4.0.1" } }, "require-uncached": { @@ -1632,11 +1616,6 @@ "tweetnacl": "~0.14.0" } }, - "stealthy-require": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", - "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" - }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", @@ -1905,8 +1884,7 @@ "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=", - "dev": true + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" }, "yallist": { "version": "2.1.2", diff --git a/package.json b/package.json index f18e9bb..078ea0c 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "dependencies": { "mustache": "^2.3.0", "request": "^2.87.0", - "request-promise": "^4.2.0", + "request-stream": "1.2.2", "tar": "^2.2.1", "unzip-stream": "^0.3.0" } diff --git a/test.js b/test.js index 29ef739..61120bc 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -var request = require("request-promise"); +var request = require("request-stream"); // https://nodejs.org/api/os.html#os_os_platform var validPlatforms = { @@ -55,22 +55,22 @@ module.exports = function test(config) { }); } else { return p.then(function() { - return request({ - method: "GET", - uri: url, - resolveWithFullResponse: true - }) - .then(function(response) { + 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(); + } + if (response.statusCode != 200) { throw new Error("Status code " + response.statusCode); } else { console.log("OKAY: " + displayUrl); + return resolve(); } - }) - .catch(function(err) { - console.error(" - Failed to fetch " + url + " " + err.message); - errors.push(displayUrl); }); + }); }); } }, From 6a39d4aa809d24a6a3079e461b2ca53461fd24d9 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 19 Aug 2018 23:21:25 -0400 Subject: [PATCH 2/6] Replace request with request-stream --- binstall.js | 80 ++++++------ package-lock.json | 316 +--------------------------------------------- package.json | 1 - 3 files changed, 46 insertions(+), 351 deletions(-) diff --git a/binstall.js b/binstall.js index b182306..e433c26 100644 --- a/binstall.js +++ b/binstall.js @@ -1,5 +1,5 @@ var fs = require("fs"); -var request = require("request"); +var request = require("request-stream"); var tar = require("tar"); var zlib = require("zlib"); var unzip = require("unzip-stream"); @@ -42,29 +42,28 @@ function untgz(url, path, options) { reject("Error decompressing " + url + " " + 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; + request(url, {method: "GET"}, 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; - } + reject(new Error(errorMessage)); + return; + } - if (verbose) { - console.log("Downloading binaries from " + url); - } + if (verbose) { + console.log("Downloading binaries from " + url); + } - response.on("error", function() { - reject("Error receiving " + url); - }); - }) - .pipe(gunzip) - .pipe(untar); + response.on("error", function() { + reject("Error receiving " + url); + }); + + response.pipe(gunzip).pipe(untar); + }) }); } @@ -97,28 +96,29 @@ 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; + request(url, {method: "GET"}, function(error, response) { + if (error) { + reject("Error communicating with URL " + url + " " + error); + return; + } - reject(new Error(errorMessage)); - return; - } + if (response.statusCode == 404) { + var errorMessage = options.errorMessage || "Not Found: " + url; - if (verbose) { - console.log("Downloading binaries from " + url); - } + reject(new Error(errorMessage)); + return; + } - response.on("error", function() { - reject("Error receiving " + url); - }); - }) - .pipe(writeStream); + if (verbose) { + console.log("Downloading binaries from " + url); + } + + response.on("error", function() { + reject("Error receiving " + url); + }); + + response.pipe(writeStream); + }) }); } diff --git a/package-lock.json b/package-lock.json index 37b9027..6f5d3db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,17 +27,6 @@ } } }, - "ajv": { - "version": "5.5.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz", - "integrity": "sha1-c7Xuyj+rZT49P5Qis0GtQiBdyWU=", - "requires": { - "co": "^4.6.0", - "fast-deep-equal": "^1.0.0", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.3.0" - } - }, "ajv-keywords": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-1.5.1.tgz", @@ -92,16 +81,6 @@ "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", "dev": true }, - "asn1": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.3.tgz", - "integrity": "sha1-2sh4dxPJlmhJ/IGAd36+nB3fO4Y=" - }, - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" - }, "assertion-error": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", @@ -114,21 +93,6 @@ "integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=", "dev": true }, - "asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" - }, - "aws-sign2": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", - "integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg=" - }, - "aws4": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.7.0.tgz", - "integrity": "sha512-32NDda82rhwD9/JBCCkB+MRYDp0oSvlo2IL6rQWA10PQi7tDUM3eqMSltXmY+Oyl/7N3P3qNtAlv7X0d9bI28w==" - }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -145,15 +109,6 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, - "bcrypt-pbkdf": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz", - "integrity": "sha1-Y7xdy2EzG5K8Bf1SiVPDNGKgb40=", - "optional": true, - "requires": { - "tweetnacl": "^0.14.3" - } - }, "binary": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz", @@ -212,11 +167,6 @@ "integrity": "sha1-r6uWJikQp/M8GaV3WCXGnzTjUMo=", "dev": true }, - "caseless": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", - "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" - }, "chai": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", @@ -284,7 +234,8 @@ "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", - "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" + "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", + "dev": true }, "code-point-at": { "version": "1.1.0", @@ -298,14 +249,6 @@ "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=", "dev": true }, - "combined-stream": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", - "integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=", - "requires": { - "delayed-stream": "~1.0.0" - } - }, "commander": { "version": "2.15.1", "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", @@ -332,7 +275,8 @@ "core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", + "dev": true }, "corser": { "version": "2.0.1", @@ -359,14 +303,6 @@ "es5-ext": "^0.10.9" } }, - "dashdash": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", - "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=", - "requires": { - "assert-plus": "^1.0.0" - } - }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -413,11 +349,6 @@ "rimraf": "^2.2.8" } }, - "delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" - }, "diff": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", @@ -433,15 +364,6 @@ "esutils": "^2.0.2" } }, - "ecc-jsbn": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz", - "integrity": "sha1-D8c6ntXw1Tw4GTOYUj735UN3dQU=", - "optional": true, - "requires": { - "jsbn": "~0.1.0" - } - }, "ecstatic": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ecstatic/-/ecstatic-3.2.1.tgz", @@ -662,26 +584,6 @@ "integrity": "sha1-8FyiM7SMBdVP/wd2XfhQfpXAL/g=", "dev": true }, - "extend": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", - "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=" - }, - "extsprintf": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz", - "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=" - }, - "fast-deep-equal": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz", - "integrity": "sha1-wFNHeBfIa1HaqFPIHgWbcz0CNhQ=" - }, - "fast-json-stable-stringify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", - "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" - }, "fast-levenshtein": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", @@ -740,21 +642,6 @@ } } }, - "forever-agent": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", - "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" - }, - "form-data": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz", - "integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=", - "requires": { - "asynckit": "^0.4.0", - "combined-stream": "1.0.6", - "mime-types": "^2.1.12" - } - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -786,14 +673,6 @@ "is-property": "^1.0.0" } }, - "getpass": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", - "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=", - "requires": { - "assert-plus": "^1.0.0" - } - }, "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", @@ -838,20 +717,6 @@ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", "dev": true }, - "har-schema": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", - "integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI=" - }, - "har-validator": { - "version": "5.0.3", - "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.0.3.tgz", - "integrity": "sha1-ukAsJmGU8VlW7xXg/PJCmT9qff0=", - "requires": { - "ajv": "^5.1.0", - "har-schema": "^2.0.0" - } - }, "has-ansi": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", @@ -900,16 +765,6 @@ "union": "~0.4.3" } }, - "http-signature": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", - "integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=", - "requires": { - "assert-plus": "^1.0.0", - "jsprim": "^1.2.2", - "sshpk": "^1.7.0" - } - }, "ignore": { "version": "3.3.8", "resolved": "https://registry.npmjs.org/ignore/-/ignore-3.3.8.tgz", @@ -1027,11 +882,6 @@ "integrity": "sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==", "dev": true }, - "is-typedarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" - }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -1044,11 +894,6 @@ "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", "dev": true }, - "isstream": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", - "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" - }, "js-tokens": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", @@ -1065,22 +910,6 @@ "esprima": "^4.0.0" } }, - "jsbn": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", - "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=", - "optional": true - }, - "json-schema": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", - "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=" - }, - "json-schema-traverse": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz", - "integrity": "sha1-NJptRMU6Ud6JtAgFxdXlm0F9M0A=" - }, "json-stable-stringify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", @@ -1090,11 +919,6 @@ "jsonify": "~0.0.0" } }, - "json-stringify-safe": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", - "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" - }, "jsonify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -1107,17 +931,6 @@ "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", "dev": true }, - "jsprim": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", - "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=", - "requires": { - "assert-plus": "1.0.0", - "extsprintf": "1.3.0", - "json-schema": "0.2.3", - "verror": "1.10.0" - } - }, "levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", @@ -1150,19 +963,6 @@ "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", "dev": true }, - "mime-db": { - "version": "1.33.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.33.0.tgz", - "integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ==" - }, - "mime-types": { - "version": "2.1.18", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.18.tgz", - "integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==", - "requires": { - "mime-db": "~1.33.0" - } - }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -1263,11 +1063,6 @@ "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", "dev": true }, - "oauth-sign": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.8.2.tgz", - "integrity": "sha1-Rqarfwrq2N6unsBWV4C31O/rnUM=" - }, "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -1349,11 +1144,6 @@ "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", "dev": true }, - "performance-now": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", - "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" - }, "pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", @@ -1422,16 +1212,6 @@ "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", "dev": true }, - "punycode": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", - "integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4=" - }, - "qs": { - "version": "6.5.2", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", - "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" - }, "readable-stream": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", @@ -1467,33 +1247,6 @@ "resolve": "^1.1.6" } }, - "request": { - "version": "2.87.0", - "resolved": "https://registry.npmjs.org/request/-/request-2.87.0.tgz", - "integrity": "sha512-fcogkm7Az5bsS6Sl0sibkbhcKsnyon/jV1kF3ajGmF0c8HrttdKTPRT9hieOaQHA5HEq6r8OyWOo/o781C1tNw==", - "requires": { - "aws-sign2": "~0.7.0", - "aws4": "^1.6.0", - "caseless": "~0.12.0", - "combined-stream": "~1.0.5", - "extend": "~3.0.1", - "forever-agent": "~0.6.1", - "form-data": "~2.3.1", - "har-validator": "~5.0.3", - "http-signature": "~1.2.0", - "is-typedarray": "~1.0.0", - "isstream": "~0.1.2", - "json-stringify-safe": "~5.0.1", - "mime-types": "~2.1.17", - "oauth-sign": "~0.8.2", - "performance-now": "^2.1.0", - "qs": "~6.5.1", - "safe-buffer": "^5.1.1", - "tough-cookie": "~2.3.3", - "tunnel-agent": "^0.6.0", - "uuid": "^3.1.0" - } - }, "request-stream": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/request-stream/-/request-stream-1.2.2.tgz", @@ -1570,12 +1323,8 @@ "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - }, - "safer-buffer": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", - "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true }, "shelljs": { "version": "0.7.8", @@ -1600,22 +1349,6 @@ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, - "sshpk": { - "version": "1.14.2", - "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.14.2.tgz", - "integrity": "sha1-xvxhZIo9nE52T9P8306hBeSSupg=", - "requires": { - "asn1": "~0.2.3", - "assert-plus": "^1.0.0", - "bcrypt-pbkdf": "^1.0.0", - "dashdash": "^1.12.0", - "ecc-jsbn": "~0.1.1", - "getpass": "^0.1.1", - "jsbn": "~0.1.0", - "safer-buffer": "^2.0.2", - "tweetnacl": "~0.14.0" - } - }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", @@ -1742,33 +1475,11 @@ "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", "dev": true }, - "tough-cookie": { - "version": "2.3.4", - "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.3.4.tgz", - "integrity": "sha512-TZ6TTfI5NtZnuyy/Kecv+CnoROnyXn2DN97LontgQpCwsX2XyLYCC0ENhYkehSOwAp8rTQKc/NUIF7BkQ5rKLA==", - "requires": { - "punycode": "^1.4.1" - } - }, "traverse": { "version": "0.3.9", "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.3.9.tgz", "integrity": "sha1-cXuPIgzAu3tE5AUUwisui7xw2Lk=" }, - "tunnel-agent": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", - "integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=", - "requires": { - "safe-buffer": "^5.0.1" - } - }, - "tweetnacl": { - "version": "0.14.5", - "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", - "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=", - "optional": true - }, "type-check": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", @@ -1837,21 +1548,6 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "dev": true }, - "uuid": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.2.1.tgz", - "integrity": "sha512-jZnMwlb9Iku/O3smGWvZhauCf6cvvpKi4BKRiliS3cxnI+Gz9j5MEpTz2UFuXiKPJocb7gnsLHwiS05ige5BEA==" - }, - "verror": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz", - "integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=", - "requires": { - "assert-plus": "^1.0.0", - "core-util-is": "1.0.2", - "extsprintf": "^1.2.0" - } - }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index 078ea0c..769319f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ }, "dependencies": { "mustache": "^2.3.0", - "request": "^2.87.0", "request-stream": "1.2.2", "tar": "^2.2.1", "unzip-stream": "^0.3.0" From 1693f22b0f1fa843d7b44b4934823722b77b6d18 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 19 Aug 2018 23:45:58 -0400 Subject: [PATCH 3/6] Remove request-stream in favor of plain http/https --- binstall.js | 38 +++++++++++++++++++++++--------------- package.json | 2 +- request.js | 22 ++++++++++++++++++++++ test.js | 18 +++++++++++------- 4 files changed, 57 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..dbd6e1b 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "homepage": "https://github.com/avh4/binwrap#readme", "files": [ "install.js", + "request.js", "prepare.js", "test.js", "binstall.js", @@ -40,7 +41,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(); }); }); } From cd10131d457410fa9932289a49ee830c9ede2b95 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 17 Sep 2018 14:52:17 -0400 Subject: [PATCH 4/6] Refactor request() to take callbacks. --- binstall.js | 88 ++++++++++++++++++++++++++--------------------------- request.js | 16 ++++++---- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/binstall.js b/binstall.js index 8f1c0b7..622c037 100644 --- a/binstall.js +++ b/binstall.js @@ -42,32 +42,31 @@ function untgz(url, path, options) { reject("Error decompressing " + url + " " + error); }); - var req = request(url, {method: "GET"}); + request( + url, + {method: "GET"}, + function(response) { + if (response.statusCode == 404) { + var errorMessage = options.errorMessage || "Not Found: " + url; + + throw new Error(errorMessage); + } - req.on("response", function(response) { - if (response.statusCode == 404) { - var errorMessage = options.errorMessage || "Not Found: " + url; + if (verbose) { + console.log("Downloading binaries from " + url); + } - throw new Error(errorMessage); - } + response.on("error", function() { + reject("Error receiving " + url); + }); - if (verbose) { - console.log("Downloading binaries from " + url); + response.pipe(gunzip).pipe(untar); + }, + function(error) { + reject("Error communicating with URL " + url + " " + error); + return; } - - response.on("error", function() { - reject("Error receiving " + url); - }); - - response.pipe(gunzip).pipe(untar); - }); - - req.on("error", function(error) { - reject("Error communicating with URL " + url + " " + error); - return; - }); - - req.end(); + ); }); } @@ -100,33 +99,32 @@ function unzipUrl(url, path, options) { } }); - var req = request(url, {method: "GET"}); - - req.on("response", function(response) { - if (response.statusCode == 404) { - var errorMessage = options.errorMessage || "Not Found: " + url; + request( + url, + {method: "GET"}, + function(response) { + if (response.statusCode == 404) { + var errorMessage = options.errorMessage || "Not Found: " + url; - reject(new Error(errorMessage)); - return; - } - - if (verbose) { - console.log("Downloading binaries from " + url); - } - - response.on("error", function() { - reject("Error receiving " + url); - }); + reject(new Error(errorMessage)); + return; + } - response.pipe(writeStream); - }); + if (verbose) { + console.log("Downloading binaries from " + url); + } - req.on("error", function(error) { - reject("Error communicating with URL " + url + " " + error); - return; - }); + response.on("error", function() { + reject("Error receiving " + url); + }); - req.end(); + response.pipe(writeStream); + }, + function(error) { + reject("Error communicating with URL " + url + " " + error); + return; + } + ); }); } diff --git a/request.js b/request.js index 9392728..21a834e 100644 --- a/request.js +++ b/request.js @@ -4,7 +4,7 @@ var parseUrl = require("url").parse; // Inspired by https://github.com/maxogden/request-stream -module.exports = function request(url, opts) { +module.exports = function request(url, opts, onSuccess, onError) { var parsed = parseUrl(url); var finalOpts = Object.assign({ @@ -13,10 +13,14 @@ module.exports = function request(url, opts) { port: parsed.port }, opts); - if (parsed.protocol === "https:") { - return https.request(finalOpts); - } else { - return http.request(finalOpts); - } + var req = + parsed.protocol === "https:" + ? https.request(finalOpts) + : http.request(finalOpts); + + req.on("response", onSuccess); + req.on("error", onError); + + return req.end(); } From 890a8ae56c2e37a19776f5a4c8599c1de3c0f2c9 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 17 Sep 2018 18:45:30 -0400 Subject: [PATCH 5/6] Check for redirects. --- binstall.js | 20 ++++++++++---------- request.js | 53 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/binstall.js b/binstall.js index 622c037..67da9a0 100644 --- a/binstall.js +++ b/binstall.js @@ -45,7 +45,11 @@ function untgz(url, path, options) { request( url, {method: "GET"}, - function(response) { + function(err, response) { + if (err) { + return reject("Error communicating with URL " + url + " " + err); + } + if (response.statusCode == 404) { var errorMessage = options.errorMessage || "Not Found: " + url; @@ -61,10 +65,6 @@ function untgz(url, path, options) { }); response.pipe(gunzip).pipe(untar); - }, - function(error) { - reject("Error communicating with URL " + url + " " + error); - return; } ); }); @@ -102,7 +102,11 @@ function unzipUrl(url, path, options) { request( url, {method: "GET"}, - function(response) { + function(err, response) { + if (err) { + return reject("Error communicating with URL " + url + " " + err); + } + if (response.statusCode == 404) { var errorMessage = options.errorMessage || "Not Found: " + url; @@ -119,10 +123,6 @@ function unzipUrl(url, path, options) { }); response.pipe(writeStream); - }, - function(error) { - reject("Error communicating with URL " + url + " " + error); - return; } ); }); diff --git a/request.js b/request.js index 21a834e..8c76562 100644 --- a/request.js +++ b/request.js @@ -4,23 +4,52 @@ var parseUrl = require("url").parse; // Inspired by https://github.com/maxogden/request-stream -module.exports = function request(url, opts, onSuccess, onError) { +module.exports = function request(url, opts, callback) { var parsed = parseUrl(url); - var finalOpts = Object.assign({ - host: parsed.hostname, - path: parsed.path, - port: parsed.port - }, opts); + return requestHelp( + parsed.protocol, + Object.assign({ + host: parsed.hostname, + path: parsed.path, + port: parsed.port + }, opts), + (callback || function() {}) + ); +} +function requestHelp(protocol, opts, callback) { var req = - parsed.protocol === "https:" - ? https.request(finalOpts) - : http.request(finalOpts); + protocol === "https:" + ? https.request(opts) + : http.request(opts); + + req.on("response", function(response) { + // Check for redirects + var redirectPath = response.headers['location']; + + if (typeof redirectPath === 'string' && response.statusCode >= 300 && response.statusCode < 400) { + if (opts.maxRedirects === 0) { + return callback(new Error("Exceeded maxRedirects"), response); + } else { + var redirectsRemaining = (opts.maxRedirects || 10) - 1; + var newOpts = {path: redirectPath, maxRedirects: redirectsRemaining}; + + return requestHelp( + protocol, + Object.assign({}, opts, newOpts), + callback + ); + } + } else { + return callback(null, response); + } + }); - req.on("response", onSuccess); - req.on("error", onError); + req.on("error", function(error) { + callback(error, null); + }); - return req.end(); + req.end(); } From 58421aa8a63c23f0b894c40ea897cf852a445b6d Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 17 Sep 2018 19:14:07 -0400 Subject: [PATCH 6/6] Add tests for redirect logic --- package-lock.json | 173 ++++++++++++++++++++++++++++++++++++++++--- package.json | 1 + test/redirectTest.js | 90 ++++++++++++++++++++++ 3 files changed, 253 insertions(+), 11 deletions(-) create mode 100644 test/redirectTest.js diff --git a/package-lock.json b/package-lock.json index 6f5d3db..f938140 100644 --- a/package-lock.json +++ b/package-lock.json @@ -307,6 +307,7 @@ "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, "requires": { "ms": "2.0.0" } @@ -328,12 +329,33 @@ } } }, + "deep-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", + "dev": true + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", "dev": true }, + "define-properties": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", + "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, + "requires": { + "object-keys": "^1.0.12" + } + }, + "defined": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", + "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", + "dev": true + }, "del": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/del/-/del-2.2.2.tgz", @@ -384,6 +406,30 @@ } } }, + "es-abstract": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz", + "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", + "dev": true, + "requires": { + "es-to-primitive": "^1.1.1", + "function-bind": "^1.1.1", + "has": "^1.0.1", + "is-callable": "^1.1.3", + "is-regex": "^1.0.4" + } + }, + "es-to-primitive": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.1.1.tgz", + "integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=", + "dev": true, + "requires": { + "is-callable": "^1.1.1", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.1" + } + }, "es5-ext": { "version": "0.10.44", "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.44.tgz", @@ -642,6 +688,15 @@ } } }, + "for-each": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", + "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", + "dev": true, + "requires": { + "is-callable": "^1.1.3" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -658,6 +713,12 @@ "rimraf": "2" } }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true + }, "generate-function": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.0.0.tgz", @@ -717,6 +778,15 @@ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", "dev": true }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, + "requires": { + "function-bind": "^1.1.1" + } + }, "has-ansi": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", @@ -818,6 +888,18 @@ "integrity": "sha1-ftGxQQxqDg94z5XTuEQMY/eLhhQ=", "dev": true }, + "is-callable": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", + "dev": true + }, + "is-date-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", + "dev": true + }, "is-fullwidth-code-point": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", @@ -876,12 +958,27 @@ "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", "dev": true }, + "is-regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", + "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "dev": true, + "requires": { + "has": "^1.0.1" + } + }, "is-resolvable": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.1.0.tgz", "integrity": "sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==", "dev": true }, + "is-symbol": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.1.tgz", + "integrity": "sha1-PMWfAAJRlLarLjjbrmaJJWtmBXI=", + "dev": true + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -1026,7 +1123,8 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true }, "mustache": { "version": "2.3.0", @@ -1069,6 +1167,18 @@ "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", "dev": true }, + "object-inspect": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", + "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", + "dev": true + }, + "object-keys": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", + "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", + "dev": true + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1247,15 +1357,6 @@ "resolve": "^1.1.6" } }, - "request-stream": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/request-stream/-/request-stream-1.2.2.tgz", - "integrity": "sha1-G60CxiD1rz1gRAAlRm6c4CwbbdU=", - "requires": { - "debug": "^2.2.0", - "xtend": "^4.0.1" - } - }, "require-uncached": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", @@ -1297,6 +1398,15 @@ "onetime": "^1.0.0" } }, + "resumer": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", + "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", + "dev": true, + "requires": { + "through": "~2.3.4" + } + }, "rimraf": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz", @@ -1360,6 +1470,17 @@ "strip-ansi": "^3.0.0" } }, + "string.prototype.trim": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", + "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "es-abstract": "^1.5.0", + "function-bind": "^1.0.2" + } + }, "string_decoder": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", @@ -1453,6 +1574,35 @@ } } }, + "tape": { + "version": "4.9.1", + "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.1.tgz", + "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", + "dev": true, + "requires": { + "deep-equal": "~1.0.1", + "defined": "~1.0.0", + "for-each": "~0.3.3", + "function-bind": "~1.1.1", + "glob": "~7.1.2", + "has": "~1.0.3", + "inherits": "~2.0.3", + "minimist": "~1.2.0", + "object-inspect": "~1.6.0", + "resolve": "~1.7.1", + "resumer": "~0.0.0", + "string.prototype.trim": "~1.1.2", + "through": "~2.3.8" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true + } + } + }, "tar": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz", @@ -1580,7 +1730,8 @@ "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=", + "dev": true }, "yallist": { "version": "2.1.2", diff --git a/package.json b/package.json index dbd6e1b..b220d9c 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "chai": "^3.5.0", "child-process-promise": "^2.2.1", "eslint": "^3.19.0", + "tape": "^4.2.2", "http-server": "^0.11.1", "mocha": "^5.2.0" }, diff --git a/test/redirectTest.js b/test/redirectTest.js new file mode 100644 index 0000000..e6117a8 --- /dev/null +++ b/test/redirectTest.js @@ -0,0 +1,90 @@ +var http = require('http') +var test = require('tape') +var concat = require('concat-stream') +var request = require('../request.js') +var url = 'http://localhost:31337' + +// Original tests: https://github.com/maxogden/request-stream/blob/master/test.js + +serverTest('get', function (t, done) { + request(url, {}, function (err, res) { + t.notOk(err, 'no err') + res.pipe(concat(function (bod) { + t.equal(bod.toString(), 'root', 'root') + done() + })) + }) +}) + +serverTest('redirect', function (t, done) { + request(url + '/redirect', {}, function (err, res) { + t.notOk(err, 'no err') + res.pipe(concat(function (bod) { + t.equal(bod.toString(), 'root', 'redirected to root') + done() + })) + }) +}) + +serverTest('infinite redirect', function (t, done) { + request(url + '/infiniteredirect', {}, function (err, res) { + t.ok(err.message.indexOf('Exceeded') > -1, 'got err') + res.pipe(concat(function (bod) { + t.equal(bod.toString(), 'infiniteredirect', 'infiniteredirect') + done() + })) + }) +}) + +serverTest('maxRedirects: 3', function (t, done) { + request(url + '/infiniteredirect', {maxRedirects: 3}, function (err, res) { + t.ok(err.message.indexOf('Exceeded') > -1, 'got err') + res.pipe(concat(function (bod) { + t.equal(bod.toString(), 'infiniteredirect', 'infiniteredirect') + done() + })) + }) +}) + +serverTest('maxRedirects: 0', function (t, done) { + request(url + '/infiniteredirect', {maxRedirects: 0}, function (err, res) { + t.ok(err.message.indexOf('Exceeded') > -1, 'got err') + res.pipe(concat(function (bod) { + t.equal(bod.toString(), 'infiniteredirect', 'infiniteredirect') + done() + })) + }) +}) + +function serverTest (msg, cb) { + test(msg, function (t) { + startServer(function (server) { + cb(t, function () { + server.close(function () { + t.end(); + }); + }) + }) + }) +} + +function startServer (cb) { + var server = http.createServer(function (req, res) { + if (req.url === '/') return res.end('root') + if (req.url === '/redirect') { + res.statusCode = 302 + res.setHeader('location', '/') + return res.end('redirect') + } + if (req.url === '/infiniteredirect') { + res.statusCode = 302 + res.setHeader('location', '/infiniteredirect') + return res.end('infiniteredirect') + } + res.end() + }); + server.listen(31337, function (err) { + if (err) throw err + cb(server) + }) +}