From dfdb9630b7e8391eded51cd87cbd0ecab279b042 Mon Sep 17 00:00:00 2001
From: oppilate <64395752+oppilate@users.noreply.github.com>
Date: Tue, 22 Dec 2020 18:38:10 +0000
Subject: [PATCH] chore: optimize performance (replace he with entities)
(#6497)
---
lib/middleware/parameter.js | 17 ++++++++++-------
lib/routes/aozora/newbook.js | 4 ++--
lib/routes/patchwork.kernel.org/comments.js | 4 ++--
lib/routes/tencent/wechat/wemp.js | 4 ++--
package.json | 6 +++---
5 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/lib/middleware/parameter.js b/lib/middleware/parameter.js
index d217e32ae1..723e4d98f8 100644
--- a/lib/middleware/parameter.js
+++ b/lib/middleware/parameter.js
@@ -1,4 +1,4 @@
-const he = require('he');
+const entities = require('entities');
const mercury_parser = require('@postlight/mercury-parser');
const cheerio = require('cheerio');
const OpenCC = require('opencc');
@@ -24,14 +24,14 @@ module.exports = async (ctx, next) => {
ctx.state.data.item = ctx.state.data.item || [];
// decode HTML entities
- ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
- ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
+ ctx.state.data.title && (ctx.state.data.title = entities.decodeXML(ctx.state.data.title + ''));
+ ctx.state.data.description && (ctx.state.data.description = entities.decodeXML(ctx.state.data.description + ''));
// sort items
ctx.state.data.item = ctx.state.data.item.sort((a, b) => +new Date(b.pubDate || 0) - +new Date(a.pubDate || 0));
- ctx.state.data.item.forEach((item) => {
- item.title && (item.title = he.decode(item.title + ''));
+ const handleItem = async (item) => {
+ item.title && (item.title = entities.decodeXML(item.title + ''));
// handle pubDate
if (item.pubDate) {
@@ -111,9 +111,12 @@ module.exports = async (ctx, next) => {
$ele.removeAttr(e);
});
});
- item.description = he.decode($('body').html() + '') + (config.suffix || '');
+ item.description = entities.decodeXML($('body').html() + '') + (config.suffix || '');
}
- });
+ return item;
+ };
+
+ ctx.state.data.item = await Promise.all(ctx.state.data.item.map(handleItem));
if (ctx.query) {
// limit
diff --git a/lib/routes/aozora/newbook.js b/lib/routes/aozora/newbook.js
index 4c5a3de34f..92bd1f16af 100644
--- a/lib/routes/aozora/newbook.js
+++ b/lib/routes/aozora/newbook.js
@@ -5,7 +5,7 @@
const got = require('@/utils/got'); // get web content
const cheerio = require('cheerio'); // html parser
-const he = require('he');
+const entities = require('entities');
const base_url = 'https://www.aozora.gr.jp/index_pages/';
module.exports = async (ctx) => {
@@ -50,7 +50,7 @@ module.exports = async (ctx) => {
let title = '';
let title_sub = '';
for (let j = 0; j < title_info.length; ++j) {
- const tmp = he.decode($(title_info[j]).html()); // should convert from escaped to unicode
+ const tmp = entities.decodeXML($(title_info[j]).html()); // should convert from escaped to unicode
if (tmp.includes('作品名:')) {
title = $(title_info[j]).find('td:nth-child(2)').text();
}
diff --git a/lib/routes/patchwork.kernel.org/comments.js b/lib/routes/patchwork.kernel.org/comments.js
index 27259de182..f3168d5153 100644
--- a/lib/routes/patchwork.kernel.org/comments.js
+++ b/lib/routes/patchwork.kernel.org/comments.js
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cache = require('./cache');
-const he = require('he');
+const entities = require('entities');
const queryString = require('query-string');
module.exports = async (ctx) => {
@@ -25,7 +25,7 @@ module.exports = async (ctx) => {
link: host,
item: data.map((item) => ({
title: item.subject,
- description: he.escape(he.escape(item.content)).replace(/\n/g, '
'),
+ description: entities.escape(entities.escape(item.content)).replace(/\n/g, '
'),
pubDate: new Date(item.date).toUTCString(),
link: item.web_url,
})),
diff --git a/lib/routes/tencent/wechat/wemp.js b/lib/routes/tencent/wechat/wemp.js
index 4b7801439b..4e4d04602d 100644
--- a/lib/routes/tencent/wechat/wemp.js
+++ b/lib/routes/tencent/wechat/wemp.js
@@ -1,5 +1,5 @@
const got = require('@/utils/got');
-const he = require('he');
+const entities = require('entities');
const cheerio = require('cheerio');
const date = require('@/utils/date');
@@ -34,7 +34,7 @@ module.exports = async (ctx) => {
const matchs = /"(url|temp_url)":"([^"]+mp\.weixin\.qq\.com[^"]+)"/.exec(response);
let weixinLink = '';
if (matchs && matchs[2]) {
- weixinLink = he.unescape(unescape(matchs[2].replace(/\\u/g, '%u')));
+ weixinLink = entities.decodeXML(unescape(matchs[2].replace(/\\u/g, '%u')));
}
const single = {
diff --git a/package.json b/package.json
index 47591c669d..92f18df0d9 100644
--- a/package.json
+++ b/package.json
@@ -2,13 +2,14 @@
"name": "rsshub",
"version": "1.0.0",
"description": "Make RSS Great Again!",
- "main": "lib/pkg.js",
+ "main": "lib/workers_index.js",
"files": [
"lib"
],
"scripts": {
"start": "node lib/index.js",
"dev": "cross-env NODE_ENV=dev nodemon --inspect lib/index.js",
+ "profiling": "NODE_ENV=production node --prof lib/index.js",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"format": "eslint \"**/*.js\" --fix && node docs/.format/format.js && prettier \"**/*.{js,json}\" --write",
@@ -41,7 +42,6 @@
"@vuepress/plugin-google-analytics": "1.7.1",
"@vuepress/plugin-pwa": "1.7.1",
"cross-env": "7.0.3",
- "entities": "2.1.0",
"eslint": "7.16.0",
"eslint-config-prettier": "7.1.0",
"eslint-plugin-prettier": "3.3.0",
@@ -78,12 +78,12 @@
"dayjs": "1.9.7",
"dotenv": "8.2.0",
"emailjs-imap-client": "3.1.0",
+ "entities": "2.1.0",
"etag": "1.8.1",
"fanfou-sdk": "4.2.0",
"git-rev-sync": "3.0.1",
"googleapis": "66.0.0",
"got": "11.8.1",
- "he": "1.2.0",
"https-proxy-agent": "5.0.0",
"iconv-lite": "0.6.2",
"instagram-private-api": "1.43.3",