|
3 | 3 | *--------------------------------------------------------*/ |
4 | 4 | 'use strict'; |
5 | 5 | Object.defineProperty(exports, "__esModule", { value: true }); |
6 | | -var request = require('request'); |
7 | | -var URL = require('url-parse'); |
| 6 | +var url_1 = require("url"); |
| 7 | +var https = require("https"); |
| 8 | +var HttpsProxyAgent = require('https-proxy-agent'); |
| 9 | +var HttpProxyAgent = require('http-proxy-agent'); |
| 10 | +var PROXY_AGENT = undefined; |
| 11 | +var HTTPS_PROXY_AGENT = undefined; |
| 12 | +if (process.env.npm_config_proxy) { |
| 13 | + PROXY_AGENT = new HttpProxyAgent(process.env.npm_config_proxy); |
| 14 | + HTTPS_PROXY_AGENT = new HttpsProxyAgent(process.env.npm_config_proxy); |
| 15 | +} |
| 16 | +if (process.env.npm_config_https_proxy) { |
| 17 | + HTTPS_PROXY_AGENT = new HttpsProxyAgent(process.env.npm_config_https_proxy); |
| 18 | +} |
8 | 19 | function getContents(url, token, headers, callback) { |
9 | | - request.get(toRequestOptions(url, token, headers), function (error, response, body) { |
10 | | - if (!error && response && response.statusCode >= 400) { |
11 | | - error = new Error('Request returned status code: ' + response.statusCode + '\nDetails: ' + response.body); |
| 20 | + var options = toRequestOptions(url, token, headers); |
| 21 | + https.get(options, function (res) { |
| 22 | + if (res && res.statusCode >= 400) { |
| 23 | + callback(new Error('Request returned status code: ' + res.statusCode)); |
12 | 24 | } |
13 | | - callback(error, body); |
| 25 | + var data = ''; |
| 26 | + res.on('data', function (chunk) { |
| 27 | + data += chunk; |
| 28 | + }); |
| 29 | + res.on('end', function () { |
| 30 | + callback(null, data); |
| 31 | + }); |
| 32 | + }).on('error', function (e) { |
| 33 | + callback(e); |
14 | 34 | }); |
15 | 35 | } |
16 | 36 | exports.getContents = getContents; |
17 | 37 | function toRequestOptions(url, token, headers) { |
18 | | - headers = headers || { |
19 | | - 'user-agent': 'nodejs' |
20 | | - }; |
| 38 | + if (headers === void 0) { headers = { 'user-agent': 'nodejs' }; } |
| 39 | + var options = url_1.parse(url); |
| 40 | + if (PROXY_AGENT && options.protocol.startsWith('http:')) { |
| 41 | + options.agent = PROXY_AGENT; |
| 42 | + } |
| 43 | + if (HTTPS_PROXY_AGENT && options.protocol.startsWith('https:')) { |
| 44 | + options.agent = HTTPS_PROXY_AGENT; |
| 45 | + } |
21 | 46 | if (token) { |
22 | 47 | headers['Authorization'] = 'token ' + token; |
23 | 48 | } |
24 | | - var parsedUrl = new URL(url); |
25 | | - var options = { |
26 | | - url: url, |
27 | | - headers: headers |
28 | | - }; |
| 49 | + options.headers = headers; |
29 | 50 | // We need to test the absence of true here because there is an npm bug that will not set boolean |
30 | 51 | // env variables if they are set to false. |
31 | 52 | if (process.env.npm_config_strict_ssl !== 'true') { |
32 | | - options.strictSSL = false; |
33 | | - } |
34 | | - if (process.env.npm_config_proxy && parsedUrl.protocol === 'http:') { |
35 | | - options.proxy = process.env.npm_config_proxy; |
36 | | - } |
37 | | - else if (process.env.npm_config_https_proxy && parsedUrl.protocol === 'https:') { |
38 | | - options.proxy = process.env.npm_config_https_proxy; |
| 53 | + options.rejectUnauthorized = false; |
39 | 54 | } |
40 | 55 | return options; |
41 | 56 | } |
42 | | -exports.toRequestOptions = toRequestOptions; |
|
0 commit comments