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

Commit 01933e0

Browse files
authored
fix(snapshot): use request module for http requests (#428)
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
1 parent 50c3ab9 commit 01933e0

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

Diff for: package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
"generate-android-snapshot": "./bin/generate-android-snapshot"
4646
},
4747
"dependencies": {
48-
"minimatch": "^3.0.4",
48+
"minimatch": "3.0.4",
4949
"nativescript-hook": "0.2.2",
50-
"schema-utils": "^0.4.3",
51-
"semver": "^5.4.1",
52-
"shelljs": "^0.6.0"
50+
"proxy-lib": "0.4.0",
51+
"request": "2.83.0",
52+
"schema-utils": "0.4.3",
53+
"semver": "5.4.1",
54+
"shelljs": "0.6.0"
5355
},
5456
"devDependencies": {
5557
"@ngtools/webpack": "~1.9.0",

Diff for: snapshot/android/utils.js

+40-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const { chmodSync, createWriteStream, existsSync } = require("fs");
2-
const { get: httpsGet } = require("https");
32
const { tmpdir } = require("os");
43
const { dirname, join } = require("path");
4+
55
const { mkdir } = require("shelljs");
6+
const { get } = require("request");
7+
const { getProxySettings } = require("proxy-lib");
68

79
const CONSTANTS = {
810
SNAPSHOT_TMP_DIR: join(tmpdir(), "snapshot-tools"),
@@ -12,49 +14,51 @@ const createDirectory = dir => mkdir('-p', dir);
1214

1315
const downloadFile = (url, destinationFilePath) =>
1416
new Promise((resolve, reject) => {
15-
const request = httpsGet(url, response => {
16-
switch (response.statusCode) {
17-
case 200:
18-
const file = createWriteStream(destinationFilePath, {autoClose: true});
19-
file.on('error', function (error) {
20-
return reject(error);
21-
});
22-
file.on("finish", function() {
17+
getRequestOptions(url)
18+
.then(options =>
19+
get(options)
20+
.on("error", reject)
21+
.pipe(createWriteStream(destinationFilePath, { autoClose: true }))
22+
.on("finish", _ => {
2323
chmodSync(destinationFilePath, 0755);
2424
return resolve(destinationFilePath);
25-
});
26-
response.pipe(file);
27-
break;
28-
case 301:
29-
case 302:
30-
case 303:
31-
const redirectUrl = response.headers.location;
32-
return this.downloadExecFile(redirectUrl, destinationFilePath);
33-
default:
34-
return reject(new Error("Unable to download file at " + url + ". Status code: " + response.statusCode));
35-
}
36-
});
25+
})
26+
).catch(reject);
27+
});
3728

38-
request.end();
29+
const getJsonFile = url =>
30+
new Promise((resolve, reject) => {
31+
getRequestOptions(url)
32+
.then(options =>
33+
get(options, (error, response, body) => {
34+
if (error) {
35+
return reject(error);
36+
}
37+
38+
if (!response || response.statusCode !== 200) {
39+
return reject(`Couldn't fetch ${url}! Response:\n${response}`);
40+
}
3941

40-
request.on('error', function(err) {
41-
return reject(err);
42-
});
42+
try {
43+
const data = JSON.parse(body);
44+
resolve(data);
45+
} catch (error) {
46+
reject(`Couldn't parse json data! Original error:\n${error}`);
47+
}
48+
})
49+
).catch(reject);
4350
});
4451

45-
const getJsonFile = url =>
52+
const getRequestOptions = (url) =>
4653
new Promise((resolve, reject) => {
47-
httpsGet(url, res => {
48-
let body = "";
49-
res.on("data", chunk => {
50-
body += chunk;
54+
const options = { url };
55+
getProxySettings()
56+
.then(proxySettings => {
57+
const allOptions = Object.assign(options, proxySettings);
58+
resolve(allOptions);
5159
})
52-
53-
res.on("end", () => {
54-
const data = JSON.parse(body);
55-
return resolve(data);
56-
});
57-
}).on("error", reject);
60+
.catch(error =>
61+
reject(`Couldn't get proxy settings! Original error:\n${error}`));
5862
});
5963

6064
module.exports = {

0 commit comments

Comments
 (0)