Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Install is not refactored, leave shared, request and url-parse
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Mar 20, 2019
1 parent 470c3da commit 2290809
Show file tree
Hide file tree
Showing 4 changed files with 434 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
var request = require('request');
var URL = require('url-parse');
function getContents(url, token, headers, callback) {
request.get(toRequestOptions(url, token, headers), function (error, response, body) {
if (!error && response && response.statusCode >= 400) {
error = new Error('Request returned status code: ' + response.statusCode + '\nDetails: ' + response.body);
}
callback(error, body);
});
}
exports.getContents = getContents;
function toRequestOptions(url, token, headers) {
headers = headers || {
'user-agent': 'nodejs'
};
if (token) {
headers['Authorization'] = 'token ' + token;
}
var parsedUrl = new URL(url);
var options = {
url: url,
headers: headers
};
// We need to test the absence of true here because there is an npm bug that will not set boolean
// env variables if they are set to false.
if (process.env.npm_config_strict_ssl !== 'true') {
options.strictSSL = false;
}
if (process.env.npm_config_proxy && parsedUrl.protocol === 'http:') {
options.proxy = process.env.npm_config_proxy;
}
else if (process.env.npm_config_https_proxy && parsedUrl.protocol === 'https:') {
options.proxy = process.env.npm_config_https_proxy;
}
return options;
}
exports.toRequestOptions = toRequestOptions;
49 changes: 49 additions & 0 deletions lib/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

'use strict';

var request = require('request');
var URL = require('url-parse');

export function getContents(url, token, headers, callback) {
request.get(toRequestOptions(url, token, headers), function (error, response, body) {
if (!error && response && response.statusCode >= 400) {
error = new Error('Request returned status code: ' + response.statusCode + '\nDetails: ' + response.body);
}

callback(error, body);
});
}

export function toRequestOptions(url, token, headers) {
headers = headers || {
'user-agent': 'nodejs'
};

if (token) {
headers['Authorization'] = 'token ' + token;
}

let parsedUrl = new URL(url);

var options: any = {
url: url,
headers: headers
};

// We need to test the absence of true here because there is an npm bug that will not set boolean
// env variables if they are set to false.
if (process.env.npm_config_strict_ssl !== 'true') {
options.strictSSL = false;
}

if (process.env.npm_config_proxy && parsedUrl.protocol === 'http:') {
options.proxy = process.env.npm_config_proxy;
} else if (process.env.npm_config_https_proxy && parsedUrl.protocol === 'https:') {
options.proxy = process.env.npm_config_https_proxy;
}

return options;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"dependencies": {
"glob": "^7.1.2",
"mocha": "^4.0.1",
"request": "^2.88.0",
"semver": "^5.4.1",
"source-map-support": "^0.5.0",
"url-parse": "^1.4.4",
"vscode-test": "^0.1.1"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2290809

Please sign in to comment.