1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.del = exports.post = exports.put = exports.get = exports.request = exports.Method = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const https_1 = require("https");
|
6 | const http_1 = require("http");
|
7 | const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
|
8 | const tunnel_1 = tslib_1.__importDefault(require("tunnel"));
|
9 | const url_1 = require("url");
|
10 | const cache = new Map();
|
11 | const globalHttpAgent = new http_1.Agent({ keepAlive: true });
|
12 | const globalHttpsAgent = new https_1.Agent({ keepAlive: true });
|
13 | function parseProxy(specifier) {
|
14 | const url = new url_1.URL(specifier);
|
15 | const proxy = { host: url.hostname, headers: {} };
|
16 | if (url.port)
|
17 | proxy.port = Number(url.port);
|
18 | return { proxy };
|
19 | }
|
20 | var Method;
|
21 | (function (Method) {
|
22 | Method["GET"] = "GET";
|
23 | Method["PUT"] = "PUT";
|
24 | Method["POST"] = "POST";
|
25 | Method["DELETE"] = "DELETE";
|
26 | })(Method = exports.Method || (exports.Method = {}));
|
27 | async function request(target, body, { configuration, headers, json, jsonRequest = json, jsonResponse = json, method = Method.GET }) {
|
28 | if (!configuration.get(`enableNetwork`))
|
29 | throw new Error(`Network access have been disabled by configuration (${method} ${target})`);
|
30 | const url = new url_1.URL(target);
|
31 | if (url.protocol === `http:` && !micromatch_1.default.isMatch(url.hostname, configuration.get(`unsafeHttpWhitelist`)))
|
32 | throw new Error(`Unsafe http requests must be explicitly whitelisted in your configuration (${url.hostname})`);
|
33 | const httpProxy = configuration.get(`httpProxy`);
|
34 | const httpsProxy = configuration.get(`httpsProxy`);
|
35 | const agent = {
|
36 | http: httpProxy
|
37 | ? tunnel_1.default.httpOverHttp(parseProxy(httpProxy))
|
38 | : globalHttpAgent,
|
39 | https: httpsProxy
|
40 | ? tunnel_1.default.httpsOverHttp(parseProxy(httpsProxy))
|
41 | : globalHttpsAgent,
|
42 | };
|
43 | const gotOptions = { agent, headers, method };
|
44 | gotOptions.responseType = jsonResponse
|
45 | ? `json`
|
46 | : `buffer`;
|
47 | if (body !== null) {
|
48 | if (Buffer.isBuffer(body) || (!jsonRequest && typeof body === `string`)) {
|
49 | gotOptions.body = body;
|
50 | }
|
51 | else {
|
52 |
|
53 | gotOptions.json = body;
|
54 | }
|
55 | }
|
56 | const socketTimeout = configuration.get(`httpTimeout`);
|
57 | const retry = configuration.get(`httpRetry`);
|
58 | const { default: got } = await Promise.resolve().then(() => tslib_1.__importStar(require(`got`)));
|
59 |
|
60 | const gotClient = got.extend({
|
61 | timeout: {
|
62 | socket: socketTimeout,
|
63 | },
|
64 | retry,
|
65 | ...gotOptions,
|
66 | });
|
67 | return configuration.getLimit(`networkConcurrency`)(() => {
|
68 | return gotClient(target);
|
69 | });
|
70 | }
|
71 | exports.request = request;
|
72 | async function get(target, { configuration, json, jsonResponse = json, ...rest }) {
|
73 | let entry = cache.get(target);
|
74 | if (!entry) {
|
75 | entry = request(target, null, { configuration, ...rest }).then(response => {
|
76 | cache.set(target, response.body);
|
77 | return response.body;
|
78 | });
|
79 | cache.set(target, entry);
|
80 | }
|
81 | if (Buffer.isBuffer(entry) === false)
|
82 | entry = await entry;
|
83 | if (jsonResponse) {
|
84 | return JSON.parse(entry.toString());
|
85 | }
|
86 | else {
|
87 | return entry;
|
88 | }
|
89 | }
|
90 | exports.get = get;
|
91 | async function put(target, body, options) {
|
92 | const response = await request(target, body, { ...options, method: Method.PUT });
|
93 | return response.body;
|
94 | }
|
95 | exports.put = put;
|
96 | async function post(target, body, options) {
|
97 | const response = await request(target, body, { ...options, method: Method.POST });
|
98 | return response.body;
|
99 | }
|
100 | exports.post = post;
|
101 | async function del(target, options) {
|
102 | const response = await request(target, null, { ...options, method: Method.DELETE });
|
103 | return response.body;
|
104 | }
|
105 | exports.del = del;
|