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

fix(snapshot): use request module for http requests #428

Merged
merged 5 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about followAllRedirects option? Do you think we should add it by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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