Skip to content

Commit

Permalink
feat(common): add utility method to get http[s] proxy agent
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Aug 17, 2022
1 parent 15202d3 commit da61925
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -94,4 +95,3 @@
"url": "https://github.com/tinesoft/nxrocks.git"
}
}

30 changes: 30 additions & 0 deletions packages/common/src/lib/core/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from 'child_process';
import { HttpsProxyAgent, HttpProxyAgent } from 'hpagent';

export function getPackageLatestNpmVersion(pkg: string): string {
try {
Expand All @@ -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
}

}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit da61925

Please sign in to comment.