artifact-engine 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/Engine/artifactEngine.d.ts +12 -12
  2. package/Engine/artifactEngine.js +166 -166
  3. package/Engine/artifactEngineOptions.d.ts +8 -8
  4. package/Engine/artifactEngineOptions.js +13 -15
  5. package/Engine/cilogger.d.ts +6 -6
  6. package/Engine/cilogger.js +44 -46
  7. package/Engine/index.d.ts +2 -2
  8. package/Engine/index.js +6 -8
  9. package/Engine/logger.d.ts +13 -13
  10. package/Engine/logger.js +109 -112
  11. package/Engine/worker.d.ts +10 -10
  12. package/Engine/worker.js +49 -51
  13. package/Models/artifactDownloadTicket.d.ts +11 -11
  14. package/Models/artifactDownloadTicket.js +5 -7
  15. package/Models/artifactItem.d.ts +12 -12
  16. package/Models/artifactItem.js +8 -10
  17. package/Models/artifactprovider.d.ts +10 -11
  18. package/Models/artifactprovider.js +2 -3
  19. package/Models/constants.d.ts +3 -3
  20. package/Models/constants.js +6 -8
  21. package/Models/index.d.ts +6 -6
  22. package/Models/index.js +12 -14
  23. package/Models/itemType.d.ts +5 -5
  24. package/Models/itemType.js +8 -10
  25. package/Models/ticketState.d.ts +7 -7
  26. package/Models/ticketState.js +10 -12
  27. package/Providers/filesystemProvider.d.ts +14 -15
  28. package/Providers/filesystemProvider.js +123 -123
  29. package/Providers/index.d.ts +4 -4
  30. package/Providers/index.js +10 -12
  31. package/Providers/stubProvider.d.ts +20 -21
  32. package/Providers/stubProvider.js +85 -88
  33. package/Providers/typed-rest-client/Handlers.d.ts +4 -4
  34. package/Providers/typed-rest-client/Handlers.js +10 -12
  35. package/Providers/typed-rest-client/HttpClient.d.ts +84 -77
  36. package/Providers/typed-rest-client/HttpClient.js +351 -328
  37. package/Providers/typed-rest-client/Index.d.ts +0 -1
  38. package/Providers/typed-rest-client/Index.js +2 -3
  39. package/Providers/typed-rest-client/Interfaces.d.ts +38 -38
  40. package/Providers/typed-rest-client/Interfaces.js +3 -4
  41. package/Providers/typed-rest-client/RestClient.d.ts +73 -27
  42. package/Providers/typed-rest-client/RestClient.js +186 -134
  43. package/Providers/typed-rest-client/Util.d.ts +7 -1
  44. package/Providers/typed-rest-client/Util.js +24 -18
  45. package/Providers/typed-rest-client/handlers/basiccreds.d.ts +9 -9
  46. package/Providers/typed-rest-client/handlers/basiccreds.js +23 -20
  47. package/Providers/typed-rest-client/handlers/bearertoken.d.ts +8 -8
  48. package/Providers/typed-rest-client/handlers/bearertoken.js +22 -19
  49. package/Providers/typed-rest-client/handlers/ntlm.d.ts +13 -13
  50. package/Providers/typed-rest-client/handlers/ntlm.js +115 -96
  51. package/Providers/typed-rest-client/handlers/personalaccesstoken.d.ts +8 -8
  52. package/Providers/typed-rest-client/handlers/personalaccesstoken.js +22 -19
  53. package/Providers/typed-rest-client/opensource/node-http-ntlm/readme.txt +6 -0
  54. package/Providers/webClient.d.ts +8 -8
  55. package/Providers/webClient.js +60 -60
  56. package/Providers/webClientFactory.d.ts +5 -5
  57. package/Providers/webClientFactory.js +59 -55
  58. package/Providers/webProvider.d.ts +21 -22
  59. package/Providers/webProvider.js +120 -116
  60. package/Providers/zipProvider.d.ts +15 -16
  61. package/Providers/zipProvider.js +53 -55
  62. package/Store/artifactItemStore.d.ts +18 -18
  63. package/Store/artifactItemStore.js +94 -96
  64. package/Store/index.d.ts +1 -1
  65. package/Store/index.js +4 -6
  66. package/Strings/resources.resjson/de-DE/resources.resjson +11 -11
  67. package/Strings/resources.resjson/en-US/resources.resjson +11 -11
  68. package/Strings/resources.resjson/es-ES/resources.resjson +11 -11
  69. package/Strings/resources.resjson/fr-FR/resources.resjson +11 -11
  70. package/Strings/resources.resjson/it-IT/resources.resjson +11 -11
  71. package/Strings/resources.resjson/ja-JP/resources.resjson +11 -11
  72. package/Strings/resources.resjson/ko-KR/resources.resjson +11 -11
  73. package/Strings/resources.resjson/ru-RU/resources.resjson +11 -11
  74. package/Strings/resources.resjson/zh-CN/resources.resjson +11 -11
  75. package/Strings/resources.resjson/zh-TW/resources.resjson +11 -11
  76. package/npm-shrinkwrap.json +1 -1
  77. package/package.json +1 -1
@@ -1,328 +1,351 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.HttpClient = exports.isHttps = exports.HttpClientResponse = exports.HttpCodes = void 0;
13
- const url = require("url");
14
- const http = require("http");
15
- const https = require("https");
16
- var tunnel = require("tunnel");
17
- const fs = require("fs");
18
- var HttpCodes;
19
- (function (HttpCodes) {
20
- HttpCodes[HttpCodes["OK"] = 200] = "OK";
21
- HttpCodes[HttpCodes["MultipleChoices"] = 300] = "MultipleChoices";
22
- HttpCodes[HttpCodes["MovedPermanently"] = 301] = "MovedPermanently";
23
- HttpCodes[HttpCodes["ResourceMoved"] = 302] = "ResourceMoved";
24
- HttpCodes[HttpCodes["NotModified"] = 304] = "NotModified";
25
- HttpCodes[HttpCodes["UseProxy"] = 305] = "UseProxy";
26
- HttpCodes[HttpCodes["SwitchProxy"] = 306] = "SwitchProxy";
27
- HttpCodes[HttpCodes["TemporaryRedirect"] = 307] = "TemporaryRedirect";
28
- HttpCodes[HttpCodes["PermanentRedirect"] = 308] = "PermanentRedirect";
29
- HttpCodes[HttpCodes["BadRequest"] = 400] = "BadRequest";
30
- HttpCodes[HttpCodes["Unauthorized"] = 401] = "Unauthorized";
31
- HttpCodes[HttpCodes["PaymentRequired"] = 402] = "PaymentRequired";
32
- HttpCodes[HttpCodes["Forbidden"] = 403] = "Forbidden";
33
- HttpCodes[HttpCodes["NotFound"] = 404] = "NotFound";
34
- HttpCodes[HttpCodes["MethodNotAllowed"] = 405] = "MethodNotAllowed";
35
- HttpCodes[HttpCodes["NotAcceptable"] = 406] = "NotAcceptable";
36
- HttpCodes[HttpCodes["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
37
- HttpCodes[HttpCodes["RequestTimeout"] = 408] = "RequestTimeout";
38
- HttpCodes[HttpCodes["Conflict"] = 409] = "Conflict";
39
- HttpCodes[HttpCodes["Gone"] = 410] = "Gone";
40
- HttpCodes[HttpCodes["InternalServerError"] = 500] = "InternalServerError";
41
- HttpCodes[HttpCodes["NotImplemented"] = 501] = "NotImplemented";
42
- HttpCodes[HttpCodes["BadGateway"] = 502] = "BadGateway";
43
- HttpCodes[HttpCodes["ServiceUnavailable"] = 503] = "ServiceUnavailable";
44
- HttpCodes[HttpCodes["GatewayTimeout"] = 504] = "GatewayTimeout";
45
- })(HttpCodes = exports.HttpCodes || (exports.HttpCodes = {}));
46
- const HttpRedirectCodes = [HttpCodes.MovedPermanently, HttpCodes.ResourceMoved, HttpCodes.TemporaryRedirect, HttpCodes.PermanentRedirect];
47
- class HttpClientResponse {
48
- constructor(message) {
49
- this.message = message;
50
- }
51
- readBody() {
52
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
53
- let output = '';
54
- this.message.on('data', (chunk) => {
55
- output += chunk;
56
- });
57
- this.message.on('end', () => {
58
- resolve(output);
59
- });
60
- }));
61
- }
62
- }
63
- exports.HttpClientResponse = HttpClientResponse;
64
- function isHttps(requestUrl) {
65
- let parsedUrl = url.parse(requestUrl);
66
- return parsedUrl.protocol === 'https:';
67
- }
68
- exports.isHttps = isHttps;
69
- var EnvironmentVariables;
70
- (function (EnvironmentVariables) {
71
- EnvironmentVariables[EnvironmentVariables["HTTP_PROXY"] = "HTTP_PROXY"] = "HTTP_PROXY";
72
- EnvironmentVariables[EnvironmentVariables["HTTPS_PROXY"] = "HTTPS_PROXY"] = "HTTPS_PROXY";
73
- })(EnvironmentVariables || (EnvironmentVariables = {}));
74
- class HttpClient {
75
- constructor(userAgent, handlers, requestOptions) {
76
- this._ignoreSslError = false;
77
- this._allowRedirects = true;
78
- this._maxRedirects = 50;
79
- this._keepAlive = false;
80
- this._disposed = false;
81
- this.userAgent = userAgent;
82
- this.handlers = handlers;
83
- this.requestOptions = requestOptions;
84
- if (requestOptions) {
85
- if (requestOptions.ignoreSslError != null) {
86
- this._ignoreSslError = requestOptions.ignoreSslError;
87
- }
88
- this._socketTimeout = requestOptions.socketTimeout;
89
- this._httpProxy = requestOptions.proxy;
90
- if (requestOptions.proxy && requestOptions.proxy.proxyBypassHosts) {
91
- this._httpProxyBypassHosts = [];
92
- requestOptions.proxy.proxyBypassHosts.forEach(bypass => {
93
- this._httpProxyBypassHosts.push(new RegExp(bypass, 'i'));
94
- });
95
- }
96
- this._certConfig = requestOptions.cert;
97
- if (this._certConfig && this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
98
- this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
99
- }
100
- if (this._certConfig && this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
101
- this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
102
- }
103
- if (this._certConfig && this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
104
- this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
105
- }
106
- if (requestOptions.allowRedirects != null) {
107
- this._allowRedirects = requestOptions.allowRedirects;
108
- }
109
- if (requestOptions.maxRedirects != null) {
110
- this._maxRedirects = Math.max(requestOptions.maxRedirects, 0);
111
- }
112
- if (requestOptions.keepAlive != null) {
113
- this._keepAlive = requestOptions.keepAlive;
114
- }
115
- }
116
- }
117
- options(requestUrl, additionalHeaders) {
118
- return this.request('OPTIONS', requestUrl, null, additionalHeaders || {});
119
- }
120
- get(requestUrl, additionalHeaders) {
121
- return this.request('GET', requestUrl, null, additionalHeaders || {});
122
- }
123
- del(requestUrl, additionalHeaders) {
124
- return this.request('DELETE', requestUrl, null, additionalHeaders || {});
125
- }
126
- post(requestUrl, data, additionalHeaders) {
127
- return this.request('POST', requestUrl, data, additionalHeaders || {});
128
- }
129
- patch(requestUrl, data, additionalHeaders) {
130
- return this.request('PATCH', requestUrl, data, additionalHeaders || {});
131
- }
132
- put(requestUrl, data, additionalHeaders) {
133
- return this.request('PUT', requestUrl, data, additionalHeaders || {});
134
- }
135
- head(requestUrl, additionalHeaders) {
136
- return this.request('HEAD', requestUrl, null, additionalHeaders || {});
137
- }
138
- sendStream(verb, requestUrl, stream, additionalHeaders) {
139
- return this.request(verb, requestUrl, stream, additionalHeaders);
140
- }
141
- request(verb, requestUrl, data, headers) {
142
- return __awaiter(this, void 0, void 0, function* () {
143
- if (this._disposed) {
144
- throw new Error("Client has already been disposed");
145
- }
146
- let info = this._prepareRequest(verb, requestUrl, headers);
147
- let response = yield this._requestRaw(info, data);
148
- let redirectsRemaining = this._maxRedirects;
149
- while (HttpRedirectCodes.indexOf(response.message.statusCode) != -1
150
- && this._allowRedirects
151
- && redirectsRemaining > 0) {
152
- const redirectUrl = response.message.headers["location"];
153
- if (!redirectUrl) {
154
- break;
155
- }
156
- yield response.readBody();
157
- info = this._prepareRequest(verb, redirectUrl, headers);
158
- response = yield this._requestRaw(info, data);
159
- redirectsRemaining--;
160
- }
161
- return response;
162
- });
163
- }
164
- dispose() {
165
- if (this._agent) {
166
- this._agent.destroy();
167
- }
168
- this._disposed = true;
169
- }
170
- _requestRaw(info, data) {
171
- return new Promise((resolve, reject) => {
172
- let socket;
173
- let isDataString = typeof (data) === 'string';
174
- if (typeof (data) === 'string') {
175
- info.options.headers["Content-Length"] = Buffer.byteLength(data, 'utf8');
176
- }
177
- let req = info.httpModule.request(info.options, (msg) => {
178
- let res = new HttpClientResponse(msg);
179
- resolve(res);
180
- });
181
- req.on('socket', (sock) => {
182
- socket = sock;
183
- });
184
- req.setTimeout(this._socketTimeout || 3 * 60000, () => {
185
- if (socket) {
186
- socket.end();
187
- }
188
- reject(new Error('Request timeout: ' + info.options.path));
189
- });
190
- req.on('error', function (err) {
191
- reject(err);
192
- });
193
- if (data && typeof (data) === 'string') {
194
- req.write(data, 'utf8');
195
- }
196
- if (data && typeof (data) !== 'string') {
197
- data.on('close', function () {
198
- req.end();
199
- });
200
- data.pipe(req);
201
- }
202
- else {
203
- req.end();
204
- }
205
- });
206
- }
207
- _prepareRequest(method, requestUrl, headers) {
208
- let info = {};
209
- info.parsedUrl = url.parse(requestUrl);
210
- let usingSsl = info.parsedUrl.protocol === 'https:';
211
- info.httpModule = usingSsl ? https : http;
212
- var defaultPort = usingSsl ? 443 : 80;
213
- info.options = {};
214
- info.options.host = info.parsedUrl.hostname;
215
- info.options.port = info.parsedUrl.port ? parseInt(info.parsedUrl.port) : defaultPort;
216
- info.options.path = (info.parsedUrl.pathname || '') + (info.parsedUrl.search || '');
217
- info.options.method = method;
218
- info.options.headers = headers || {};
219
- info.options.headers["User-Agent"] = this.userAgent;
220
- info.options.agent = this._getAgent(requestUrl);
221
- if (this.handlers) {
222
- this.handlers.forEach((handler) => {
223
- handler.prepareRequest(info.options);
224
- });
225
- }
226
- return info;
227
- }
228
- _getAgent(requestUrl) {
229
- let agent;
230
- let proxy = this._getProxy(requestUrl);
231
- let useProxy = proxy.proxyUrl && proxy.proxyUrl.hostname && !this._isBypassProxy(requestUrl);
232
- if (this._keepAlive && useProxy) {
233
- agent = this._proxyAgent;
234
- }
235
- if (this._keepAlive && !useProxy) {
236
- agent = this._agent;
237
- }
238
- if (!!agent) {
239
- return agent;
240
- }
241
- var parsedUrl = url.parse(requestUrl);
242
- let usingSsl = parsedUrl.protocol === 'https:';
243
- let maxSockets = 100;
244
- if (!!this.requestOptions) {
245
- maxSockets = this.requestOptions.maxSockets || http.globalAgent.maxSockets;
246
- }
247
- if (useProxy) {
248
- var agentOptions = {
249
- maxSockets: maxSockets,
250
- keepAlive: this._keepAlive,
251
- proxy: {
252
- proxyAuth: proxy.proxyAuth,
253
- host: proxy.proxyUrl.hostname,
254
- port: proxy.proxyUrl.port
255
- },
256
- };
257
- var tunnelAgent;
258
- var overHttps = proxy.proxyUrl.protocol === 'https:';
259
- if (usingSsl) {
260
- tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp;
261
- }
262
- else {
263
- tunnelAgent = overHttps ? tunnel.httpOverHttps : tunnel.httpOverHttp;
264
- }
265
- agent = tunnelAgent(agentOptions);
266
- this._proxyAgent = agent;
267
- }
268
- if (this._keepAlive && !agent) {
269
- var options = { keepAlive: this._keepAlive, maxSockets: maxSockets };
270
- agent = usingSsl ? new https.Agent(options) : new http.Agent(options);
271
- this._agent = agent;
272
- }
273
- if (!agent) {
274
- agent = usingSsl ? https.globalAgent : http.globalAgent;
275
- }
276
- if (usingSsl && this._ignoreSslError) {
277
- agent.options = Object.assign(agent.options || {}, { rejectUnauthorized: false });
278
- }
279
- if (usingSsl && this._certConfig) {
280
- agent.options = Object.assign(agent.options || {}, { ca: this._ca, cert: this._cert, key: this._key, passphrase: this._certConfig.passphrase });
281
- }
282
- return agent;
283
- }
284
- _getProxy(requestUrl) {
285
- var parsedUrl = url.parse(requestUrl);
286
- let usingSsl = parsedUrl.protocol === 'https:';
287
- let proxyConfig = this._httpProxy;
288
- let https_proxy = process.env[EnvironmentVariables.HTTPS_PROXY];
289
- let http_proxy = process.env[EnvironmentVariables.HTTP_PROXY];
290
- if (!proxyConfig) {
291
- if (https_proxy && usingSsl) {
292
- proxyConfig = {
293
- proxyUrl: https_proxy
294
- };
295
- }
296
- else if (http_proxy) {
297
- proxyConfig = {
298
- proxyUrl: http_proxy
299
- };
300
- }
301
- }
302
- let proxyUrl;
303
- let proxyAuth;
304
- if (proxyConfig) {
305
- if (proxyConfig.proxyUrl.length > 0) {
306
- proxyUrl = url.parse(proxyConfig.proxyUrl);
307
- }
308
- if (proxyConfig.proxyUsername || proxyConfig.proxyPassword) {
309
- proxyAuth = proxyConfig.proxyUsername + ":" + proxyConfig.proxyPassword;
310
- }
311
- }
312
- return { proxyUrl: proxyUrl, proxyAuth: proxyAuth };
313
- }
314
- _isBypassProxy(requestUrl) {
315
- if (!this._httpProxyBypassHosts) {
316
- return false;
317
- }
318
- let bypass = false;
319
- this._httpProxyBypassHosts.forEach(bypassHost => {
320
- if (bypassHost.test(requestUrl)) {
321
- bypass = true;
322
- }
323
- });
324
- return bypass;
325
- }
326
- }
327
- exports.HttpClient = HttpClient;
328
- //# sourceMappingURL=HttpClient.js.map
1
+ "use strict";
2
+ // Copyright (c) Microsoft. All rights reserved.
3
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ const url = require("url");
14
+ const http = require("http");
15
+ const https = require("https");
16
+ var tunnel = require("tunnel");
17
+ const fs = require("fs");
18
+ var HttpCodes;
19
+ (function (HttpCodes) {
20
+ HttpCodes[HttpCodes["OK"] = 200] = "OK";
21
+ HttpCodes[HttpCodes["MultipleChoices"] = 300] = "MultipleChoices";
22
+ HttpCodes[HttpCodes["MovedPermanently"] = 301] = "MovedPermanently";
23
+ HttpCodes[HttpCodes["ResourceMoved"] = 302] = "ResourceMoved";
24
+ HttpCodes[HttpCodes["NotModified"] = 304] = "NotModified";
25
+ HttpCodes[HttpCodes["UseProxy"] = 305] = "UseProxy";
26
+ HttpCodes[HttpCodes["SwitchProxy"] = 306] = "SwitchProxy";
27
+ HttpCodes[HttpCodes["TemporaryRedirect"] = 307] = "TemporaryRedirect";
28
+ HttpCodes[HttpCodes["PermanentRedirect"] = 308] = "PermanentRedirect";
29
+ HttpCodes[HttpCodes["BadRequest"] = 400] = "BadRequest";
30
+ HttpCodes[HttpCodes["Unauthorized"] = 401] = "Unauthorized";
31
+ HttpCodes[HttpCodes["PaymentRequired"] = 402] = "PaymentRequired";
32
+ HttpCodes[HttpCodes["Forbidden"] = 403] = "Forbidden";
33
+ HttpCodes[HttpCodes["NotFound"] = 404] = "NotFound";
34
+ HttpCodes[HttpCodes["MethodNotAllowed"] = 405] = "MethodNotAllowed";
35
+ HttpCodes[HttpCodes["NotAcceptable"] = 406] = "NotAcceptable";
36
+ HttpCodes[HttpCodes["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
37
+ HttpCodes[HttpCodes["RequestTimeout"] = 408] = "RequestTimeout";
38
+ HttpCodes[HttpCodes["Conflict"] = 409] = "Conflict";
39
+ HttpCodes[HttpCodes["Gone"] = 410] = "Gone";
40
+ HttpCodes[HttpCodes["InternalServerError"] = 500] = "InternalServerError";
41
+ HttpCodes[HttpCodes["NotImplemented"] = 501] = "NotImplemented";
42
+ HttpCodes[HttpCodes["BadGateway"] = 502] = "BadGateway";
43
+ HttpCodes[HttpCodes["ServiceUnavailable"] = 503] = "ServiceUnavailable";
44
+ HttpCodes[HttpCodes["GatewayTimeout"] = 504] = "GatewayTimeout";
45
+ })(HttpCodes = exports.HttpCodes || (exports.HttpCodes = {}));
46
+ const HttpRedirectCodes = [HttpCodes.MovedPermanently, HttpCodes.ResourceMoved, HttpCodes.TemporaryRedirect, HttpCodes.PermanentRedirect];
47
+ class HttpClientResponse {
48
+ constructor(message) {
49
+ this.message = message;
50
+ }
51
+ readBody() {
52
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
53
+ let output = '';
54
+ this.message.on('data', (chunk) => {
55
+ output += chunk;
56
+ });
57
+ this.message.on('end', () => {
58
+ resolve(output);
59
+ });
60
+ }));
61
+ }
62
+ }
63
+ exports.HttpClientResponse = HttpClientResponse;
64
+ function isHttps(requestUrl) {
65
+ let parsedUrl = url.parse(requestUrl);
66
+ return parsedUrl.protocol === 'https:';
67
+ }
68
+ exports.isHttps = isHttps;
69
+ var EnvironmentVariables;
70
+ (function (EnvironmentVariables) {
71
+ EnvironmentVariables[EnvironmentVariables["HTTP_PROXY"] = "HTTP_PROXY"] = "HTTP_PROXY";
72
+ EnvironmentVariables[EnvironmentVariables["HTTPS_PROXY"] = "HTTPS_PROXY"] = "HTTPS_PROXY";
73
+ })(EnvironmentVariables || (EnvironmentVariables = {}));
74
+ class HttpClient {
75
+ constructor(userAgent, handlers, requestOptions) {
76
+ this._ignoreSslError = false;
77
+ this._allowRedirects = true;
78
+ this._maxRedirects = 50;
79
+ this._keepAlive = false;
80
+ this._disposed = false;
81
+ this.userAgent = userAgent;
82
+ this.handlers = handlers;
83
+ this.requestOptions = requestOptions;
84
+ if (requestOptions) {
85
+ if (requestOptions.ignoreSslError != null) {
86
+ this._ignoreSslError = requestOptions.ignoreSslError;
87
+ }
88
+ this._socketTimeout = requestOptions.socketTimeout;
89
+ this._httpProxy = requestOptions.proxy;
90
+ if (requestOptions.proxy && requestOptions.proxy.proxyBypassHosts) {
91
+ this._httpProxyBypassHosts = [];
92
+ requestOptions.proxy.proxyBypassHosts.forEach(bypass => {
93
+ this._httpProxyBypassHosts.push(new RegExp(bypass, 'i'));
94
+ });
95
+ }
96
+ this._certConfig = requestOptions.cert;
97
+ // cache the cert content into memory, so we don't have to read it from disk every time
98
+ if (this._certConfig && this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
99
+ this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
100
+ }
101
+ if (this._certConfig && this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
102
+ this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
103
+ }
104
+ if (this._certConfig && this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
105
+ this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
106
+ }
107
+ if (requestOptions.allowRedirects != null) {
108
+ this._allowRedirects = requestOptions.allowRedirects;
109
+ }
110
+ if (requestOptions.maxRedirects != null) {
111
+ this._maxRedirects = Math.max(requestOptions.maxRedirects, 0);
112
+ }
113
+ if (requestOptions.keepAlive != null) {
114
+ this._keepAlive = requestOptions.keepAlive;
115
+ }
116
+ }
117
+ }
118
+ options(requestUrl, additionalHeaders) {
119
+ return this.request('OPTIONS', requestUrl, null, additionalHeaders || {});
120
+ }
121
+ get(requestUrl, additionalHeaders) {
122
+ return this.request('GET', requestUrl, null, additionalHeaders || {});
123
+ }
124
+ del(requestUrl, additionalHeaders) {
125
+ return this.request('DELETE', requestUrl, null, additionalHeaders || {});
126
+ }
127
+ post(requestUrl, data, additionalHeaders) {
128
+ return this.request('POST', requestUrl, data, additionalHeaders || {});
129
+ }
130
+ patch(requestUrl, data, additionalHeaders) {
131
+ return this.request('PATCH', requestUrl, data, additionalHeaders || {});
132
+ }
133
+ put(requestUrl, data, additionalHeaders) {
134
+ return this.request('PUT', requestUrl, data, additionalHeaders || {});
135
+ }
136
+ head(requestUrl, additionalHeaders) {
137
+ return this.request('HEAD', requestUrl, null, additionalHeaders || {});
138
+ }
139
+ sendStream(verb, requestUrl, stream, additionalHeaders) {
140
+ return this.request(verb, requestUrl, stream, additionalHeaders);
141
+ }
142
+ /**
143
+ * Makes a raw http request.
144
+ * All other methods such as get, post, patch, and request ultimately call this.
145
+ * Prefer get, del, post and patch
146
+ */
147
+ request(verb, requestUrl, data, headers) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ if (this._disposed) {
150
+ throw new Error("Client has already been disposed");
151
+ }
152
+ let info = this._prepareRequest(verb, requestUrl, headers);
153
+ let response = yield this._requestRaw(info, data);
154
+ let redirectsRemaining = this._maxRedirects;
155
+ while (HttpRedirectCodes.indexOf(response.message.statusCode) != -1
156
+ && this._allowRedirects
157
+ && redirectsRemaining > 0) {
158
+ const redirectUrl = response.message.headers["location"];
159
+ if (!redirectUrl) {
160
+ // if there's no location to redirect to, we won't
161
+ break;
162
+ }
163
+ // we need to finish reading the response before reassigning response
164
+ // which will leak the open socket.
165
+ yield response.readBody();
166
+ // let's make the request with the new redirectUrl
167
+ info = this._prepareRequest(verb, redirectUrl, headers);
168
+ response = yield this._requestRaw(info, data);
169
+ redirectsRemaining--;
170
+ }
171
+ return response;
172
+ });
173
+ }
174
+ /**
175
+ * Needs to be called if keepAlive is set to true in request options.
176
+ */
177
+ dispose() {
178
+ if (this._agent) {
179
+ this._agent.destroy();
180
+ }
181
+ this._disposed = true;
182
+ }
183
+ _requestRaw(info, data) {
184
+ return new Promise((resolve, reject) => {
185
+ let socket;
186
+ let isDataString = typeof (data) === 'string';
187
+ if (typeof (data) === 'string') {
188
+ info.options.headers["Content-Length"] = Buffer.byteLength(data, 'utf8');
189
+ }
190
+ let req = info.httpModule.request(info.options, (msg) => {
191
+ let res = new HttpClientResponse(msg);
192
+ resolve(res);
193
+ });
194
+ req.on('socket', (sock) => {
195
+ socket = sock;
196
+ });
197
+ // If we ever get disconnected, we want the socket to timeout eventually
198
+ req.setTimeout(this._socketTimeout || 3 * 60000, () => {
199
+ if (socket) {
200
+ socket.end();
201
+ }
202
+ reject(new Error('Request timeout: ' + info.options.path));
203
+ });
204
+ req.on('error', function (err) {
205
+ // err has statusCode property
206
+ // res should have headers
207
+ reject(err);
208
+ });
209
+ if (data && typeof (data) === 'string') {
210
+ req.write(data, 'utf8');
211
+ }
212
+ if (data && typeof (data) !== 'string') {
213
+ data.on('close', function () {
214
+ req.end();
215
+ });
216
+ data.pipe(req);
217
+ }
218
+ else {
219
+ req.end();
220
+ }
221
+ });
222
+ }
223
+ _prepareRequest(method, requestUrl, headers) {
224
+ let info = {};
225
+ info.parsedUrl = url.parse(requestUrl);
226
+ let usingSsl = info.parsedUrl.protocol === 'https:';
227
+ info.httpModule = usingSsl ? https : http;
228
+ var defaultPort = usingSsl ? 443 : 80;
229
+ info.options = {};
230
+ info.options.host = info.parsedUrl.hostname;
231
+ info.options.port = info.parsedUrl.port ? parseInt(info.parsedUrl.port) : defaultPort;
232
+ info.options.path = (info.parsedUrl.pathname || '') + (info.parsedUrl.search || '');
233
+ info.options.method = method;
234
+ info.options.headers = headers || {};
235
+ info.options.headers["User-Agent"] = this.userAgent;
236
+ info.options.agent = this._getAgent(requestUrl);
237
+ // gives handlers an opportunity to participate
238
+ if (this.handlers) {
239
+ this.handlers.forEach((handler) => {
240
+ handler.prepareRequest(info.options);
241
+ });
242
+ }
243
+ return info;
244
+ }
245
+ _getAgent(requestUrl) {
246
+ let agent;
247
+ let proxy = this._getProxy(requestUrl);
248
+ let useProxy = proxy.proxyUrl && proxy.proxyUrl.hostname && !this._isBypassProxy(requestUrl);
249
+ if (this._keepAlive && useProxy) {
250
+ agent = this._proxyAgent;
251
+ }
252
+ if (this._keepAlive && !useProxy) {
253
+ agent = this._agent;
254
+ }
255
+ // if agent is already assigned use that agent.
256
+ if (!!agent) {
257
+ return agent;
258
+ }
259
+ var parsedUrl = url.parse(requestUrl);
260
+ let usingSsl = parsedUrl.protocol === 'https:';
261
+ let maxSockets = 100;
262
+ if (!!this.requestOptions) {
263
+ maxSockets = this.requestOptions.maxSockets || http.globalAgent.maxSockets;
264
+ }
265
+ if (useProxy) {
266
+ var agentOptions = {
267
+ maxSockets: maxSockets,
268
+ keepAlive: this._keepAlive,
269
+ proxy: {
270
+ proxyAuth: proxy.proxyAuth,
271
+ host: proxy.proxyUrl.hostname,
272
+ port: proxy.proxyUrl.port
273
+ },
274
+ };
275
+ var tunnelAgent;
276
+ var overHttps = proxy.proxyUrl.protocol === 'https:';
277
+ if (usingSsl) {
278
+ tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp;
279
+ }
280
+ else {
281
+ tunnelAgent = overHttps ? tunnel.httpOverHttps : tunnel.httpOverHttp;
282
+ }
283
+ agent = tunnelAgent(agentOptions);
284
+ this._proxyAgent = agent;
285
+ }
286
+ // if reusing agent across request and tunneling agent isn't assigned create a new agent
287
+ if (this._keepAlive && !agent) {
288
+ var options = { keepAlive: this._keepAlive, maxSockets: maxSockets };
289
+ agent = usingSsl ? new https.Agent(options) : new http.Agent(options);
290
+ this._agent = agent;
291
+ }
292
+ // if not using private agent and tunnel agent isn't setup then use global agent
293
+ if (!agent) {
294
+ agent = usingSsl ? https.globalAgent : http.globalAgent;
295
+ }
296
+ if (usingSsl && this._ignoreSslError) {
297
+ // we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process
298
+ // http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options
299
+ // we have to cast it to any and change it directly
300
+ agent.options = Object.assign(agent.options || {}, { rejectUnauthorized: false });
301
+ }
302
+ if (usingSsl && this._certConfig) {
303
+ agent.options = Object.assign(agent.options || {}, { ca: this._ca, cert: this._cert, key: this._key, passphrase: this._certConfig.passphrase });
304
+ }
305
+ return agent;
306
+ }
307
+ _getProxy(requestUrl) {
308
+ var parsedUrl = url.parse(requestUrl);
309
+ let usingSsl = parsedUrl.protocol === 'https:';
310
+ let proxyConfig = this._httpProxy;
311
+ // fallback to http_proxy and https_proxy env
312
+ let https_proxy = process.env[EnvironmentVariables.HTTPS_PROXY];
313
+ let http_proxy = process.env[EnvironmentVariables.HTTP_PROXY];
314
+ if (!proxyConfig) {
315
+ if (https_proxy && usingSsl) {
316
+ proxyConfig = {
317
+ proxyUrl: https_proxy
318
+ };
319
+ }
320
+ else if (http_proxy) {
321
+ proxyConfig = {
322
+ proxyUrl: http_proxy
323
+ };
324
+ }
325
+ }
326
+ let proxyUrl;
327
+ let proxyAuth;
328
+ if (proxyConfig) {
329
+ if (proxyConfig.proxyUrl.length > 0) {
330
+ proxyUrl = url.parse(proxyConfig.proxyUrl);
331
+ }
332
+ if (proxyConfig.proxyUsername || proxyConfig.proxyPassword) {
333
+ proxyAuth = proxyConfig.proxyUsername + ":" + proxyConfig.proxyPassword;
334
+ }
335
+ }
336
+ return { proxyUrl: proxyUrl, proxyAuth: proxyAuth };
337
+ }
338
+ _isBypassProxy(requestUrl) {
339
+ if (!this._httpProxyBypassHosts) {
340
+ return false;
341
+ }
342
+ let bypass = false;
343
+ this._httpProxyBypassHosts.forEach(bypassHost => {
344
+ if (bypassHost.test(requestUrl)) {
345
+ bypass = true;
346
+ }
347
+ });
348
+ return bypass;
349
+ }
350
+ }
351
+ exports.HttpClient = HttpClient;