UNPKG

@nxrocks/common

Version:

Common library to share code among the `@nxrocks/*` plugins.

59 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackageLatestNpmVersion = getPackageLatestNpmVersion; exports.getHttpProxyAgent = getHttpProxyAgent; exports.getCommonHttpHeaders = getCommonHttpHeaders; const child_process_1 = require("child_process"); const hpagent_1 = require("hpagent"); function getPackageLatestNpmVersion(pkg) { try { return ((0, child_process_1.execSync)(`npm show ${pkg} version`, { stdio: 'ignore', }) .toString() .trim() || 'latest'); } catch (e) { return 'latest'; } } function getHttpProxyAgent(targetUrl, proxyUrl) { const { http_proxy: httpProxy, https_proxy: httpsProxy, HTTP_PROXY, HTTPS_PROXY, } = process.env; const proxy = (proxyUrl || httpsProxy || HTTPS_PROXY || httpProxy || HTTP_PROXY)?.trim(); if (!proxy) { return undefined; } const proxyAgentOpts = { keepAlive: true, keepAliveMsecs: 1000, maxSockets: 256, maxFreeSockets: 256, //scheduling: 'lifo', proxy: proxy, }; if (targetUrl?.startsWith('https')) { return new hpagent_1.HttpsProxyAgent(proxyAgentOpts); } else if (targetUrl?.startsWith('http')) { return new hpagent_1.HttpProxyAgent(proxyAgentOpts); } else { return undefined; } } function getCommonHttpHeaders(pkgName, targetUrl, proxyUrl) { const pkgVersion = getPackageLatestNpmVersion(pkgName); const userAgent = `${pkgName.replace('/', '_')}/${pkgVersion}`; const proxyAgent = getHttpProxyAgent(targetUrl, proxyUrl); return { headers: { 'User-Agent': userAgent, }, ...(proxyAgent ? { agent: proxyAgent } : {}), }; } //# sourceMappingURL=utils.js.map