Skip to content

Commit dfdb963

Browse files
authored
chore: optimize performance (replace he with entities) (#6497)
1 parent e735aef commit dfdb963

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

lib/middleware/parameter.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const he = require('he');
1+
const entities = require('entities');
22
const mercury_parser = require('@postlight/mercury-parser');
33
const cheerio = require('cheerio');
44
const OpenCC = require('opencc');
@@ -24,14 +24,14 @@ module.exports = async (ctx, next) => {
2424
ctx.state.data.item = ctx.state.data.item || [];
2525

2626
// decode HTML entities
27-
ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
28-
ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
27+
ctx.state.data.title && (ctx.state.data.title = entities.decodeXML(ctx.state.data.title + ''));
28+
ctx.state.data.description && (ctx.state.data.description = entities.decodeXML(ctx.state.data.description + ''));
2929

3030
// sort items
3131
ctx.state.data.item = ctx.state.data.item.sort((a, b) => +new Date(b.pubDate || 0) - +new Date(a.pubDate || 0));
3232

33-
ctx.state.data.item.forEach((item) => {
34-
item.title && (item.title = he.decode(item.title + ''));
33+
const handleItem = async (item) => {
34+
item.title && (item.title = entities.decodeXML(item.title + ''));
3535

3636
// handle pubDate
3737
if (item.pubDate) {
@@ -111,9 +111,12 @@ module.exports = async (ctx, next) => {
111111
$ele.removeAttr(e);
112112
});
113113
});
114-
item.description = he.decode($('body').html() + '') + (config.suffix || '');
114+
item.description = entities.decodeXML($('body').html() + '') + (config.suffix || '');
115115
}
116-
});
116+
return item;
117+
};
118+
119+
ctx.state.data.item = await Promise.all(ctx.state.data.item.map(handleItem));
117120

118121
if (ctx.query) {
119122
// limit

lib/routes/aozora/newbook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const got = require('@/utils/got'); // get web content
77
const cheerio = require('cheerio'); // html parser
8-
const he = require('he');
8+
const entities = require('entities');
99

1010
const base_url = 'https://www.aozora.gr.jp/index_pages/';
1111
module.exports = async (ctx) => {
@@ -50,7 +50,7 @@ module.exports = async (ctx) => {
5050
let title = '';
5151
let title_sub = '';
5252
for (let j = 0; j < title_info.length; ++j) {
53-
const tmp = he.decode($(title_info[j]).html()); // should convert from escaped to unicode
53+
const tmp = entities.decodeXML($(title_info[j]).html()); // should convert from escaped to unicode
5454
if (tmp.includes('作品名:')) {
5555
title = $(title_info[j]).find('td:nth-child(2)').text();
5656
}

lib/routes/patchwork.kernel.org/comments.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const got = require('@/utils/got');
22
const cache = require('./cache');
3-
const he = require('he');
3+
const entities = require('entities');
44
const queryString = require('query-string');
55

66
module.exports = async (ctx) => {
@@ -25,7 +25,7 @@ module.exports = async (ctx) => {
2525
link: host,
2626
item: data.map((item) => ({
2727
title: item.subject,
28-
description: he.escape(he.escape(item.content)).replace(/\n/g, '<br>'),
28+
description: entities.escape(entities.escape(item.content)).replace(/\n/g, '<br>'),
2929
pubDate: new Date(item.date).toUTCString(),
3030
link: item.web_url,
3131
})),

lib/routes/tencent/wechat/wemp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const got = require('@/utils/got');
2-
const he = require('he');
2+
const entities = require('entities');
33
const cheerio = require('cheerio');
44
const date = require('@/utils/date');
55

@@ -34,7 +34,7 @@ module.exports = async (ctx) => {
3434
const matchs = /"(url|temp_url)":"([^"]+mp\.weixin\.qq\.com[^"]+)"/.exec(response);
3535
let weixinLink = '';
3636
if (matchs && matchs[2]) {
37-
weixinLink = he.unescape(unescape(matchs[2].replace(/\\u/g, '%u')));
37+
weixinLink = entities.decodeXML(unescape(matchs[2].replace(/\\u/g, '%u')));
3838
}
3939

4040
const single = {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "rsshub",
33
"version": "1.0.0",
44
"description": "Make RSS Great Again!",
5-
"main": "lib/pkg.js",
5+
"main": "lib/workers_index.js",
66
"files": [
77
"lib"
88
],
99
"scripts": {
1010
"start": "node lib/index.js",
1111
"dev": "cross-env NODE_ENV=dev nodemon --inspect lib/index.js",
12+
"profiling": "NODE_ENV=production node --prof lib/index.js",
1213
"docs:dev": "vuepress dev docs",
1314
"docs:build": "vuepress build docs",
1415
"format": "eslint \"**/*.js\" --fix && node docs/.format/format.js && prettier \"**/*.{js,json}\" --write",
@@ -41,7 +42,6 @@
4142
"@vuepress/plugin-google-analytics": "1.7.1",
4243
"@vuepress/plugin-pwa": "1.7.1",
4344
"cross-env": "7.0.3",
44-
"entities": "2.1.0",
4545
"eslint": "7.16.0",
4646
"eslint-config-prettier": "7.1.0",
4747
"eslint-plugin-prettier": "3.3.0",
@@ -78,12 +78,12 @@
7878
"dayjs": "1.9.7",
7979
"dotenv": "8.2.0",
8080
"emailjs-imap-client": "3.1.0",
81+
"entities": "2.1.0",
8182
"etag": "1.8.1",
8283
"fanfou-sdk": "4.2.0",
8384
"git-rev-sync": "3.0.1",
8485
"googleapis": "66.0.0",
8586
"got": "11.8.1",
86-
"he": "1.2.0",
8787
"https-proxy-agent": "5.0.0",
8888
"iconv-lite": "0.6.2",
8989
"instagram-private-api": "1.43.3",

0 commit comments

Comments
 (0)