UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.del = exports.post = exports.put = exports.get = exports.request = exports.Method = void 0;
4const tslib_1 = require("tslib");
5const https_1 = require("https");
6const http_1 = require("http");
7const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
8const tunnel_1 = tslib_1.__importDefault(require("tunnel"));
9const url_1 = require("url");
10const cache = new Map();
11const globalHttpAgent = new http_1.Agent({ keepAlive: true });
12const globalHttpsAgent = new https_1.Agent({ keepAlive: true });
13function 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}
20var 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 = {}));
27async 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 // @ts-expect-error: The got types only allow an object, but got can stringify any valid JSON
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 //@ts-ignore
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}
71exports.request = request;
72async 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}
90exports.get = get;
91async function put(target, body, options) {
92 const response = await request(target, body, { ...options, method: Method.PUT });
93 return response.body;
94}
95exports.put = put;
96async function post(target, body, options) {
97 const response = await request(target, body, { ...options, method: Method.POST });
98 return response.body;
99}
100exports.post = post;
101async function del(target, options) {
102 const response = await request(target, null, { ...options, method: Method.DELETE });
103 return response.body;
104}
105exports.del = del;