Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
fix(snapshot): use request module for http requests (#428)
Browse files Browse the repository at this point in the history
Use 'request' node package for https requests and 'proxy-lib' for getting configured proxy settings (see: NativeScript/nativescript-cli:docs/man_pages/general/proxy-set.md@master).

fixes #389
  • Loading branch information
sis0k0 authored Feb 15, 2018
1 parent 50c3ab9 commit 01933e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
"generate-android-snapshot": "./bin/generate-android-snapshot"
},
"dependencies": {
"minimatch": "^3.0.4",
"minimatch": "3.0.4",
"nativescript-hook": "0.2.2",
"schema-utils": "^0.4.3",
"semver": "^5.4.1",
"shelljs": "^0.6.0"
"proxy-lib": "0.4.0",
"request": "2.83.0",
"schema-utils": "0.4.3",
"semver": "5.4.1",
"shelljs": "0.6.0"
},
"devDependencies": {
"@ngtools/webpack": "~1.9.0",
Expand Down
76 changes: 40 additions & 36 deletions snapshot/android/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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 { getProxySettings } = require("proxy-lib");

const CONSTANTS = {
SNAPSHOT_TMP_DIR: join(tmpdir(), "snapshot-tools"),
Expand All @@ -12,49 +14,51 @@ 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() {
getRequestOptions(url)
.then(options =>
get(options)
.on("error", reject)
.pipe(createWriteStream(destinationFilePath, { autoClose: true }))
.on("finish", _ => {
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));
}
});
})
).catch(reject);
});

request.end();
const getJsonFile = url =>
new Promise((resolve, reject) => {
getRequestOptions(url)
.then(options =>
get(options, (error, response, body) => {
if (error) {
return reject(error);
}

if (!response || response.statusCode !== 200) {
return reject(`Couldn't fetch ${url}! Response:\n${response}`);
}

request.on('error', function(err) {
return reject(err);
});
try {
const data = JSON.parse(body);
resolve(data);
} catch (error) {
reject(`Couldn't parse json data! Original error:\n${error}`);
}
})
).catch(reject);
});

const getJsonFile = url =>
const getRequestOptions = (url) =>
new Promise((resolve, reject) => {
httpsGet(url, res => {
let body = "";
res.on("data", chunk => {
body += chunk;
const options = { url };
getProxySettings()
.then(proxySettings => {
const allOptions = Object.assign(options, proxySettings);
resolve(allOptions);
})

res.on("end", () => {
const data = JSON.parse(body);
return resolve(data);
});
}).on("error", reject);
.catch(error =>
reject(`Couldn't get proxy settings! Original error:\n${error}`));
});

module.exports = {
Expand Down

0 comments on commit 01933e0

Please sign in to comment.