diff --git a/package.json b/package.json index d8edac1e..5207b581 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "private": true, "dependencies": { "@swc/helpers": "~0.3.3", + "hpagent": "^1.0.0", "inquirer": "^8.2.0", "js-yaml": "^4.1.0", "node-fetch": "^2.6.1", @@ -94,4 +95,3 @@ "url": "https://github.com/tinesoft/nxrocks.git" } } - diff --git a/packages/common/src/lib/core/utils/utils.ts b/packages/common/src/lib/core/utils/utils.ts index e0b37c81..f429edb3 100644 --- a/packages/common/src/lib/core/utils/utils.ts +++ b/packages/common/src/lib/core/utils/utils.ts @@ -1,4 +1,5 @@ import { execSync } from 'child_process'; +import { HttpsProxyAgent, HttpProxyAgent } from 'hpagent'; export function getPackageLatestNpmVersion(pkg: string): string { try { @@ -9,3 +10,32 @@ export function getPackageLatestNpmVersion(pkg: string): string { return 'latest'; } } + +export function getHttpProxyAgent(proxyUrl?: string): HttpProxyAgent | HttpsProxyAgent | undefined { + const proxyAgentOpts = { + keepAlive: true, + keepAliveMsecs: 1000, + maxSockets: 256, + maxFreeSockets: 256, + //scheduling: 'lifo', + proxy: 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?.startsWith('https')) { + return new HttpsProxyAgent(proxyAgentOpts); + } else if(proxy?.startsWith('http')){ + return new HttpProxyAgent(proxyAgentOpts); + } else { + return undefined + } + +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 2019c44d..d20df184 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4467,6 +4467,11 @@ hosted-git-info@^5.0.0: dependencies: lru-cache "^7.5.1" +hpagent@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/hpagent/-/hpagent-1.0.0.tgz#c68f68b3df845687dbdc4896546713ce09cc6bee" + integrity sha512-SCleE2Uc1bM752ymxg8QXYGW0TWtAV4ZW3TqH1aOnyi6T6YW2xadCcclm5qeVjvMvfQ2RKNtZxO7uVb9CTPt1A== + html-encoding-sniffer@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/html-encoding-sniffer/-/html-encoding-sniffer-1.0.2.tgz#e70d84b94da53aa375e11fe3a351be6642ca46f8"