Skip to content
This repository has been 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

Conversation

sis0k0
Copy link
Contributor

@sis0k0 sis0k0 commented Jan 29, 2018

Use 'request' node package for https requests and 'proxy-lib' for getting configured proxy settings (see: https://github.com/NativeScript/nativescript-cli/blob/master/docs/man_pages/general/proxy-set.md).

fixes #389

Use 'request' node package, which respects environment proxy variables
(i.e. HTTPS_PROXY).

fixes #389
Copy link
Contributor

@rosen-vladimirov rosen-vladimirov left a comment

Choose a reason for hiding this comment

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

This PR is a step in the right direction, but I see one problem at the moment. CLI uses proxy-lib to get the settings for proxy and passes them to request module. So CLI does not support HTTPS_PROXY variable (as proxy-lib does not support it yet). So in current situation, users who have proxy will have to set CLI proxy (via commands) and HTTPS_PROXY variable(s).
Maybe we should add proxy-lib as dependency here and pass the settings it returns to request module. This way, when CLI proxy is set, everything will work. Also, once we add support for HTTPS_PROXY in proxy-lib, it will be propagated to all packages that depend on this library.

package.json Outdated
@@ -42,6 +42,7 @@
"dependencies": {
"minimatch": "^3.0.4",
"nativescript-hook": "0.2.2",
"request": "^2.83.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO we should use strict versions in package.json. This minimizes the risk of being broken by release of another package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll pin the versions.

}

const { statusCode } = response;
if (statusCode !== 200) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should accept all 2xx codes as success. Also you may receive 3xx (redirect). You may handle this by passing followAllRedirects option to request module - this way you'll not receive 3xx here, as request module will handle them for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

2xx - Correct me if I'm wrong but I'm sure that all the 2xx codes will denote success in this case. For example, status code 204 ('No content') doesn't seem to be ok when we are expecting to fetch a file or receive a json.
3xx - According to the docs, following GET HTTP responses is turned on by default. I'm not sure if we should follow non-GET HTTP responses (followAllRedirects: true), but I may be missing something. Is there a reason to turn it on?


const { statusCode } = response;
if (statusCode !== 200) {
reject(`Couldn't fetch ${url}! Response status code: ${statusCode}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case it is a good idea to show the real response. For example when you have proxy that has blocked access to this resource, the response may contain information that the admins of the user have blocked the access and who is responsible for enabling the access.

Copy link
Contributor

@Plamen5kov Plamen5kov Feb 13, 2018

Choose a reason for hiding this comment

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

return reject(...) again to prevent unexpected behavior with json parse

})
get(url, (error, response, body) => {
if (error) {
reject(error);
Copy link
Contributor

@Plamen5kov Plamen5kov Feb 13, 2018

Choose a reason for hiding this comment

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

return reject(error)

request.on('error', function(err) {
return reject(err);
});
get(url)
Copy link
Contributor

Choose a reason for hiding this comment

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

any tests for this functionality are preferable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left it without tests, because I thought that the tests for the get method inside the request package are enough. I agree that it would be good to test our specific functionality. Can you suggest some scenarios that I can add?

Copy link
Contributor

@rosen-vladimirov rosen-vladimirov left a comment

Choose a reason for hiding this comment

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

Great work!

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.

@sis0k0 sis0k0 merged commit 01933e0 into master Feb 15, 2018
@sis0k0 sis0k0 deleted the use-request-package branch February 15, 2018 09:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snapshots not working behind corporate proxy
3 participants