mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
perf: lazy require dependencies (#8025)
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
const Redis = require('ioredis');
|
||||
const Lru = require('lru-cache');
|
||||
const md5 = require('@/utils/md5');
|
||||
const config = require('@/config').value;
|
||||
const logger = require('@/utils/logger');
|
||||
|
||||
let Redis;
|
||||
|
||||
module.exports = function (app) {
|
||||
let available = false;
|
||||
const globalCache = {
|
||||
@@ -12,6 +13,7 @@ module.exports = function (app) {
|
||||
};
|
||||
|
||||
if (config.cache.type === 'redis') {
|
||||
Redis = Redis || require('ioredis');
|
||||
const redisClient = new Redis(config.redis.url);
|
||||
|
||||
redisClient.on('error', (error) => {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
const entities = require('entities');
|
||||
const mercury_parser = require('@postlight/mercury-parser');
|
||||
const cheerio = require('cheerio');
|
||||
const { simplecc } = require('simplecc-wasm');
|
||||
const got = require('@/utils/got');
|
||||
const config = require('@/config').value;
|
||||
|
||||
let mercury_parser;
|
||||
|
||||
module.exports = async (ctx, next) => {
|
||||
await next();
|
||||
|
||||
@@ -204,6 +205,8 @@ module.exports = async (ctx, next) => {
|
||||
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
|
||||
// if parser failed, return default description and not report error
|
||||
try {
|
||||
mercury_parser = mercury_parser || require('@postlight/mercury-parser');
|
||||
|
||||
const res = await got(link);
|
||||
const $ = cheerio.load(res.data);
|
||||
const result = await mercury_parser.parse(link, {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user