Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize performance (replace he with entities) #6497

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/middleware/parameter.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/aozora/newbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/patchwork.kernel.org/comments.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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, '<br>'),
description: entities.escape(entities.escape(item.content)).replace(/\n/g, '<br>'),
pubDate: new Date(item.date).toUTCString(),
link: item.web_url,
})),
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/tencent/wechat/wemp.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down