perf: lazy require dependencies (#8025)

This commit is contained in:
Sukka
2021-08-20 14:05:57 +08:00
committed by GitHub
parent 72dd0193c1
commit 31720bbb1b
3 changed files with 16 additions and 5 deletions

View File

@@ -1,17 +1,20 @@
const config = require('@/config').value;
const HttpsProxyAgent = require('https-proxy-agent');
const SocksProxyAgent = require('socks-proxy-agent');
const tunnel = require('tunnel');
const logger = require('./logger');
const http = require('http');
const https = require('https');
let tunnel;
let HttpsProxyAgent;
let SocksProxyAgent;
let agent = null;
if (config.proxyUri && typeof config.proxyUri === 'string') {
let proxy = null;
if (config.proxyUri.startsWith('http')) {
HttpsProxyAgent = HttpsProxyAgent || require('https-proxy-agent');
proxy = new HttpsProxyAgent(config.proxyUri);
} else if (config.proxyUri.startsWith('socks')) {
SocksProxyAgent = SocksProxyAgent || require('socks-proxy-agent');
proxy = new SocksProxyAgent(config.proxyUri);
} else {
throw 'Unknown proxy-uri format';
@@ -27,10 +30,12 @@ if (config.proxyUri && typeof config.proxyUri === 'string') {
switch (config.proxy.protocol) {
case 'socks':
SocksProxyAgent = SocksProxyAgent || require('socks-proxy-agent');
agent.http = new SocksProxyAgent(proxyUrl);
agent.https = new SocksProxyAgent(proxyUrl);
break;
case 'http':
tunnel = tunnel || require('tunnel');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
agent.http = tunnel.httpOverHttp({
proxy: {
@@ -46,6 +51,7 @@ if (config.proxyUri && typeof config.proxyUri === 'string') {
});
break;
case 'https':
tunnel = tunnel || require('tunnel');
agent.http = tunnel.httpOverHttps({
proxy: {
host: config.proxy.host,