mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-24 21:15:51 +01:00
0a6412ca97
* feat: proxy with got v12 * fix tests * refactor hooks module
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { Agents } from 'got-cjs';
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
|
import { Agent as HttpAgent, AgentOptions as HttpAgentOptions } from 'http';
|
|
import { Agent as HttpsAgent, AgentOptions as HttpsAgentOptions } from 'https';
|
|
import { URL } from 'url';
|
|
|
|
export type AgentOptionsConf = HttpAgentOptions | HttpsAgentOptions;
|
|
|
|
class CustomAgents {
|
|
private url: string;
|
|
private proxy: string | undefined;
|
|
private agentOptions: HttpAgentOptions | HttpsAgentOptions;
|
|
private agent: Agents;
|
|
public constructor(
|
|
url: string,
|
|
proxy: string | undefined,
|
|
agentOptions: HttpAgentOptions | HttpsAgentOptions
|
|
) {
|
|
this.proxy = proxy;
|
|
this.url = url;
|
|
this.agentOptions = agentOptions;
|
|
const { protocol } = this.getParsedUrl();
|
|
this.agent = this.getAgent(protocol);
|
|
}
|
|
|
|
public get() {
|
|
return this.agent;
|
|
}
|
|
|
|
private getAgent(protocol: string): Agents {
|
|
const isHTTPS = protocol === 'https:';
|
|
if (this.proxy) {
|
|
const options = {
|
|
proxy: this.proxy,
|
|
...this.agentOptions,
|
|
};
|
|
// use hpagent
|
|
return isHTTPS
|
|
? { https: new HttpsProxyAgent(options) }
|
|
: { http: new HttpProxyAgent(options) };
|
|
} else {
|
|
// use native http/https agent
|
|
return isHTTPS
|
|
? { https: new HttpsAgent(this.agentOptions) }
|
|
: { http: new HttpAgent(this.agentOptions) };
|
|
}
|
|
}
|
|
|
|
private getParsedUrl() {
|
|
return this.proxy ? new URL(this.proxy) : new URL(this.url);
|
|
}
|
|
}
|
|
|
|
export default CustomAgents;
|