@teambit/network.agent
Version:
65 lines • 2.5 kB
JavaScript
;
// Based on - https://github.com/pnpm/pnpm/blob/acc1782c6f18e1388e333c6fd44ccd378faba553/packages/npm-registry-agent/src/index.ts#L0-L1
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAgent = void 0;
const url_1 = require("url");
const agentkeepalive_1 = __importDefault(require("agentkeepalive"));
const network_proxy_agent_1 = require("@teambit/network.proxy-agent");
const HttpsAgent = agentkeepalive_1.default.HttpsAgent;
function getAgent(uri, opts) {
var _a, _b;
const noProxy = checkNoProxy(uri, opts);
if (!noProxy && (opts.httpProxy || opts.httpsProxy)) {
const proxy = network_proxy_agent_1.getProxyAgent(uri, opts);
if (proxy) {
return proxy;
}
}
const parsedUri = new url_1.URL(uri);
const isHttps = parsedUri.protocol === 'https:';
const agentTimeout = typeof opts.timeout !== 'number' || opts.timeout === 0 ? 0 : opts.timeout;
const agent = isHttps
? new HttpsAgent({
ca: opts.ca,
cert: opts.cert,
key: opts.key,
maxSockets: (_a = opts.maxSockets) !== null && _a !== void 0 ? _a : 15,
rejectUnauthorized: opts.strictSSL,
timeout: agentTimeout,
}) // eslint-disable-line @typescript-eslint/no-explicit-any
: new agentkeepalive_1.default({
maxSockets: (_b = opts.maxSockets) !== null && _b !== void 0 ? _b : 15,
timeout: agentTimeout,
}); // eslint-disable-line @typescript-eslint/no-explicit-any
return agent;
}
exports.getAgent = getAgent;
function checkNoProxy(uri, opts) {
const host = new url_1.URL(uri).hostname
.split('.')
.filter((x) => x)
.reverse();
if (typeof opts.noProxy === 'string') {
const noproxyArr = opts.noProxy.split(/\s*,\s*/g);
return noproxyArr.some((no) => {
const noParts = no
.split('.')
.filter((x) => x)
.reverse();
if (!noParts.length) {
return false;
}
for (let i = 0; i < noParts.length; i += 1) {
if (host[i] !== noParts[i]) {
return false;
}
}
return true;
});
}
return opts.noProxy;
}
//# sourceMappingURL=agent.js.map