massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / request-light / lib / node / main.js
1 /*---------------------------------------------------------------------------------------------
2  *  Copyright (c) Microsoft Corporation. All rights reserved.
3  *  Licensed under the MIT License. See License.txt in the project root for license information.
4  *--------------------------------------------------------------------------------------------*/
5 'use strict';
6 Object.defineProperty(exports, "__esModule", { value: true });
7 var url_1 = require("url");
8 var https = require("https");
9 var http = require("http");
10 var HttpProxyAgent = require("http-proxy-agent");
11 var HttpsProxyAgent = require("https-proxy-agent");
12 var zlib = require("zlib");
13 var nls = require("vscode-nls");
14 if (process.env.VSCODE_NLS_CONFIG) {
15     var VSCODE_NLS_CONFIG = process.env.VSCODE_NLS_CONFIG;
16     nls.config(JSON.parse(VSCODE_NLS_CONFIG));
17 }
18 var localize = nls.loadMessageBundle();
19 var proxyUrl = null;
20 var strictSSL = true;
21 function configure(_proxyUrl, _strictSSL) {
22     proxyUrl = _proxyUrl;
23     strictSSL = _strictSSL;
24 }
25 exports.configure = configure;
26 function xhr(options) {
27     var agent = getProxyAgent(options.url, { proxyUrl: proxyUrl, strictSSL: strictSSL });
28     options = assign({}, options);
29     options = assign(options, { agent: agent, strictSSL: strictSSL });
30     if (typeof options.followRedirects !== 'number') {
31         options.followRedirects = 5;
32     }
33     return request(options).then(function (result) { return new Promise(function (c, e) {
34         var res = result.res;
35         var readable = res;
36         var encoding = res.headers && res.headers['content-encoding'];
37         var isCompleted = false;
38         if (encoding === 'gzip') {
39             var gunzip = zlib.createGunzip();
40             res.pipe(gunzip);
41             readable = gunzip;
42         }
43         else if (encoding === 'deflate') {
44             var inflate = zlib.createInflate();
45             res.pipe(inflate);
46             readable = inflate;
47         }
48         var data = [];
49         readable.on('data', function (c) { return data.push(c); });
50         readable.on('end', function () {
51             if (isCompleted) {
52                 return;
53             }
54             isCompleted = true;
55             if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
56                 var location = res.headers['location'];
57                 if (location) {
58                     var newOptions = {
59                         type: options.type, url: location, user: options.user, password: options.password, responseType: options.responseType, headers: options.headers,
60                         timeout: options.timeout, followRedirects: options.followRedirects - 1, data: options.data
61                     };
62                     xhr(newOptions).then(c, e);
63                     return;
64                 }
65             }
66             var response = {
67                 responseText: data.join(''),
68                 status: res.statusCode,
69                 headers: res.headers || {}
70             };
71             if ((res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 1223) {
72                 c(response);
73             }
74             else {
75                 e(response);
76             }
77         });
78         readable.on('error', function (err) {
79             var response = {
80                 responseText: localize('error', 'Unable to access {0}. Error: {1}', options.url, err.message),
81                 status: 500,
82                 headers: undefined,
83             };
84             isCompleted = true;
85             e(response);
86         });
87     }); }, function (err) {
88         var message;
89         if (agent) {
90             message = localize('error.cannot.connect.proxy', 'Unable to connect to {0} through a proxy . Error: {1}', options.url, err.message);
91         }
92         else {
93             message = localize('error.cannot.connect', 'Unable to connect to {0}. Error: {1}', options.url, err.message);
94         }
95         return Promise.reject({
96             responseText: message,
97             status: 404
98         });
99     });
100 }
101 exports.xhr = xhr;
102 function assign(destination) {
103     var sources = [];
104     for (var _i = 1; _i < arguments.length; _i++) {
105         sources[_i - 1] = arguments[_i];
106     }
107     sources.forEach(function (source) { return Object.keys(source).forEach(function (key) { return destination[key] = source[key]; }); });
108     return destination;
109 }
110 function request(options) {
111     var req;
112     return new Promise(function (c, e) {
113         var endpoint = url_1.parse(options.url);
114         var opts = {
115             hostname: endpoint.hostname,
116             port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
117             path: endpoint.path,
118             method: options.type || 'GET',
119             headers: options.headers,
120             agent: options.agent,
121             rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
122         };
123         if (options.user && options.password) {
124             opts.auth = options.user + ':' + options.password;
125         }
126         var handler = function (res) {
127             if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
128                 c(request(assign({}, options, {
129                     url: res.headers['location'],
130                     followRedirects: options.followRedirects - 1
131                 })));
132             }
133             else {
134                 c({ req: req, res: res });
135             }
136         };
137         if (endpoint.protocol === 'https:') {
138             req = https.request(opts, handler);
139         }
140         else {
141             req = http.request(opts, handler);
142         }
143         req.on('error', e);
144         if (options.timeout) {
145             req.setTimeout(options.timeout);
146         }
147         if (options.data) {
148             req.write(options.data);
149         }
150         req.end();
151     });
152 }
153 function getErrorStatusDescription(status) {
154     if (status < 400) {
155         return void 0;
156     }
157     switch (status) {
158         case 400: return localize('status.400', 'Bad request. The request cannot be fulfilled due to bad syntax.');
159         case 401: return localize('status.401', 'Unauthorized. The server is refusing to respond.');
160         case 403: return localize('status.403', 'Forbidden. The server is refusing to respond.');
161         case 404: return localize('status.404', 'Not Found. The requested location could not be found.');
162         case 405: return localize('status.405', 'Method not allowed. A request was made using a request method not supported by that location.');
163         case 406: return localize('status.406', 'Not Acceptable. The server can only generate a response that is not accepted by the client.');
164         case 407: return localize('status.407', 'Proxy Authentication Required. The client must first authenticate itself with the proxy.');
165         case 408: return localize('status.408', 'Request Timeout. The server timed out waiting for the request.');
166         case 409: return localize('status.409', 'Conflict. The request could not be completed because of a conflict in the request.');
167         case 410: return localize('status.410', 'Gone. The requested page is no longer available.');
168         case 411: return localize('status.411', 'Length Required. The "Content-Length" is not defined.');
169         case 412: return localize('status.412', 'Precondition Failed. The precondition given in the request evaluated to false by the server.');
170         case 413: return localize('status.413', 'Request Entity Too Large. The server will not accept the request, because the request entity is too large.');
171         case 414: return localize('status.414', 'Request-URI Too Long. The server will not accept the request, because the URL is too long.');
172         case 415: return localize('status.415', 'Unsupported Media Type. The server will not accept the request, because the media type is not supported.');
173         case 500: return localize('status.500', 'Internal Server Error.');
174         case 501: return localize('status.501', 'Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfill the request.');
175         case 503: return localize('status.503', 'Service Unavailable. The server is currently unavailable (overloaded or down).');
176         default: return localize('status.416', 'HTTP status code {0}', status);
177     }
178 }
179 exports.getErrorStatusDescription = getErrorStatusDescription;
180 // proxy handling
181 function getSystemProxyURI(requestURL) {
182     if (requestURL.protocol === 'http:') {
183         return process.env.HTTP_PROXY || process.env.http_proxy || null;
184     }
185     else if (requestURL.protocol === 'https:') {
186         return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
187     }
188     return null;
189 }
190 function getProxyAgent(rawRequestURL, options) {
191     if (options === void 0) { options = {}; }
192     var requestURL = url_1.parse(rawRequestURL);
193     var proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
194     if (!proxyURL) {
195         return null;
196     }
197     var proxyEndpoint = url_1.parse(proxyURL);
198     if (!/^https?:$/.test(proxyEndpoint.protocol)) {
199         return null;
200     }
201     var opts = {
202         host: proxyEndpoint.hostname,
203         port: Number(proxyEndpoint.port),
204         auth: proxyEndpoint.auth,
205         rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
206     };
207     return requestURL.protocol === 'http:' ? new HttpProxyAgent(opts) : new HttpsProxyAgent(opts);
208 }