From 4d092ed1ec9ba309fa32dea6e787528c05abf4e0 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Mon, 29 Jan 2018 17:26:16 +0200 Subject: [PATCH 1/5] fix(snapshot): use request module for http request Use 'request' node package, which respects environment proxy variables (i.e. HTTPS_PROXY). fixes #389 --- package.json | 1 + snapshot/android/utils.js | 62 +++++++++++++++------------------------ 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index e4695acc..2daaeb67 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "minimatch": "^3.0.4", "nativescript-hook": "0.2.2", + "request": "^2.83.0", "schema-utils": "^0.4.3", "semver": "^5.4.1", "shelljs": "^0.6.0" diff --git a/snapshot/android/utils.js b/snapshot/android/utils.js index bcbb04aa..1e868e77 100644 --- a/snapshot/android/utils.js +++ b/snapshot/android/utils.js @@ -1,8 +1,9 @@ const { chmodSync, createWriteStream, existsSync } = require("fs"); -const { get: httpsGet } = require("https"); const { tmpdir } = require("os"); const { dirname, join } = require("path"); + const { mkdir } = require("shelljs"); +const { get } = require("request"); const CONSTANTS = { SNAPSHOT_TMP_DIR: join(tmpdir(), "snapshot-tools"), @@ -12,49 +13,34 @@ const createDirectory = dir => mkdir('-p', dir); const downloadFile = (url, destinationFilePath) => new Promise((resolve, reject) => { - const request = httpsGet(url, response => { - switch (response.statusCode) { - case 200: - const file = createWriteStream(destinationFilePath, {autoClose: true}); - file.on('error', function (error) { - return reject(error); - }); - file.on("finish", function() { - chmodSync(destinationFilePath, 0755); - return resolve(destinationFilePath); - }); - response.pipe(file); - break; - case 301: - case 302: - case 303: - const redirectUrl = response.headers.location; - return this.downloadExecFile(redirectUrl, destinationFilePath); - default: - return reject(new Error("Unable to download file at " + url + ". Status code: " + response.statusCode)); - } - }); - - request.end(); - - request.on('error', function(err) { - return reject(err); - }); + get(url) + .on("error", reject) + .pipe(createWriteStream(destinationFilePath, { autoClose: true })) + .on("finish", _ => { + chmodSync(destinationFilePath, 0755); + resolve(destinationFilePath); + }); }); const getJsonFile = url => new Promise((resolve, reject) => { - httpsGet(url, res => { - let body = ""; - res.on("data", chunk => { - body += chunk; - }) + get(url, (error, response, body) => { + if (error) { + reject(error); + } + + const { statusCode } = response; + if (statusCode !== 200) { + reject(`Couldn't fetch ${url}! Response status code: ${statusCode}`) + } - res.on("end", () => { + try { const data = JSON.parse(body); - return resolve(data); - }); - }).on("error", reject); + resolve(data); + } catch (e) { + reject(`Couldn't parse json data! Original error:\n${e}`); + } + }); }); module.exports = { From 011ce3ce7560691f6a59d4327721bb88cea6ea7d Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 13 Feb 2018 15:25:18 +0200 Subject: [PATCH 2/5] chore: pin dependencies versions --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 2daaeb67..cc7f26c0 100644 --- a/package.json +++ b/package.json @@ -40,12 +40,12 @@ "generate-android-snapshot": "./bin/generate-android-snapshot" }, "dependencies": { - "minimatch": "^3.0.4", + "minimatch": "3.0.4", "nativescript-hook": "0.2.2", - "request": "^2.83.0", - "schema-utils": "^0.4.3", - "semver": "^5.4.1", - "shelljs": "^0.6.0" + "request": "2.83.0", + "schema-utils": "0.4.3", + "semver": "5.4.1", + "shelljs": "0.6.0" }, "devDependencies": { "@ngtools/webpack": "~1.9.0", From 9b8b92208c87492bb46bc1410e3b7502b75525b9 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 13 Feb 2018 15:45:02 +0200 Subject: [PATCH 3/5] fix(snapshot): reject with full response when request in not successful --- snapshot/android/utils.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snapshot/android/utils.js b/snapshot/android/utils.js index 1e868e77..d9c1970e 100644 --- a/snapshot/android/utils.js +++ b/snapshot/android/utils.js @@ -29,9 +29,8 @@ const getJsonFile = url => reject(error); } - const { statusCode } = response; - if (statusCode !== 200) { - reject(`Couldn't fetch ${url}! Response status code: ${statusCode}`) + if (!response || response.statusCode !== 200) { + reject(`Couldn't fetch ${url}! Response:\n${response}`); } try { From 0733d8e92ef9033b443e8219f952a3707969494b Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 13 Feb 2018 17:29:15 +0200 Subject: [PATCH 4/5] refactor: use proxy-lib to get request options --- package.json | 1 + snapshot/android/utils.js | 65 +++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index cc7f26c0..94745b3c 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "minimatch": "3.0.4", "nativescript-hook": "0.2.2", + "proxy-lib": "0.4.0", "request": "2.83.0", "schema-utils": "0.4.3", "semver": "5.4.1", diff --git a/snapshot/android/utils.js b/snapshot/android/utils.js index d9c1970e..11a532e0 100644 --- a/snapshot/android/utils.js +++ b/snapshot/android/utils.js @@ -4,6 +4,7 @@ const { dirname, join } = require("path"); const { mkdir } = require("shelljs"); const { get } = require("request"); +const { getProxySettings } = require("proxy-lib"); const CONSTANTS = { SNAPSHOT_TMP_DIR: join(tmpdir(), "snapshot-tools"), @@ -13,33 +14,51 @@ const createDirectory = dir => mkdir('-p', dir); const downloadFile = (url, destinationFilePath) => new Promise((resolve, reject) => { - get(url) - .on("error", reject) - .pipe(createWriteStream(destinationFilePath, { autoClose: true })) - .on("finish", _ => { - chmodSync(destinationFilePath, 0755); - resolve(destinationFilePath); - }); + getRequestOptions(url) + .then(options => + get(options) + .on("error", reject) + .pipe(createWriteStream(destinationFilePath, { autoClose: true })) + .on("finish", _ => { + chmodSync(destinationFilePath, 0755); + return resolve(destinationFilePath); + }) + ).catch(reject); }); const getJsonFile = url => new Promise((resolve, reject) => { - get(url, (error, response, body) => { - if (error) { - reject(error); - } - - if (!response || response.statusCode !== 200) { - reject(`Couldn't fetch ${url}! Response:\n${response}`); - } - - try { - const data = JSON.parse(body); - resolve(data); - } catch (e) { - reject(`Couldn't parse json data! Original error:\n${e}`); - } - }); + getRequestOptions(url) + .then(options => + get(options, (error, response, body) => { + if (error) { + reject(error); + } + + if (!response || response.statusCode !== 200) { + reject(`Couldn't fetch ${url}! Response:\n${response}`); + } + + try { + const data = JSON.parse(body); + resolve(data); + } catch (error) { + reject(`Couldn't parse json data! Original error:\n${error}`); + } + }) + ).catch(reject); + }); + +const getRequestOptions = (url) => + new Promise((resolve, reject) => { + const options = { url }; + getProxySettings() + .then(proxySettings => { + const allOptions = Object.assign(options, proxySettings); + resolve(allOptions); + }) + .catch(error => + reject(`Couldn't get proxy settings! Original error:\n${error}`)); }); module.exports = { From b6239e20ccefc8c898a48c7b3065c995e1c4cd00 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 13 Feb 2018 17:31:13 +0200 Subject: [PATCH 5/5] fix: return on reject --- snapshot/android/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snapshot/android/utils.js b/snapshot/android/utils.js index 11a532e0..7cc5676a 100644 --- a/snapshot/android/utils.js +++ b/snapshot/android/utils.js @@ -32,11 +32,11 @@ const getJsonFile = url => .then(options => get(options, (error, response, body) => { if (error) { - reject(error); + return reject(error); } if (!response || response.statusCode !== 200) { - reject(`Couldn't fetch ${url}! Response:\n${response}`); + return reject(`Couldn't fetch ${url}! Response:\n${response}`); } try {