mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 02:58:08 +08:00
feat: support etag
This commit is contained in:
@@ -39,15 +39,13 @@ app.use(favicon(__dirname + '/favicon.png'));
|
||||
// global error handing
|
||||
app.use(onerror);
|
||||
|
||||
// 1 set header
|
||||
app.use(header);
|
||||
|
||||
app.use(accessControl);
|
||||
|
||||
// 6 debug
|
||||
// 7 debug
|
||||
app.context.debug = {
|
||||
hitCache: 0,
|
||||
request: 0,
|
||||
etag: 0,
|
||||
paths: [],
|
||||
routes: [],
|
||||
ips: [],
|
||||
@@ -56,6 +54,9 @@ app.context.debug = {
|
||||
};
|
||||
app.use(debug);
|
||||
|
||||
// 6 set header
|
||||
app.use(header);
|
||||
|
||||
// 5 fix incorrect `utf-8` characters
|
||||
app.use(utf8);
|
||||
|
||||
|
||||
@@ -23,4 +23,8 @@ module.exports = async (ctx, next) => {
|
||||
}
|
||||
|
||||
ctx.state.debuged = true;
|
||||
|
||||
if (ctx.status === 304) {
|
||||
ctx.debug.etag++;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -19,8 +19,7 @@ module.exports = async (ctx, next) => {
|
||||
|
||||
await next();
|
||||
|
||||
let body = ctx.body;
|
||||
if (!body || typeof body !== 'string' || ctx.response.get('ETag')) {
|
||||
if (!ctx.body || typeof ctx.body !== 'string' || ctx.response.get('ETag')) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -29,14 +28,17 @@ module.exports = async (ctx, next) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
|
||||
ctx.set('ETag', etagCalculate(ctx.body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '').replace(/<atom:link(.*)\/>/, '')));
|
||||
|
||||
if (ctx.fresh) {
|
||||
ctx.status = 304;
|
||||
ctx.body = null;
|
||||
} else {
|
||||
const match = ctx.body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
|
||||
if (match) {
|
||||
ctx.set({
|
||||
'Last-Modified': body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1],
|
||||
'Last-Modified': match[1],
|
||||
});
|
||||
}
|
||||
|
||||
body = body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '');
|
||||
|
||||
ctx.response.etag = etagCalculate(body);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -85,12 +85,16 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
{
|
||||
name: 'request frequency',
|
||||
value: ((ctx.debug.request / (stats.elapsed / 1000)) * 60).toFixed(3) + ' 次/分钟',
|
||||
value: ((ctx.debug.request / (stats.elapsed / 1000)) * 60).toFixed(3) + ' times/minute',
|
||||
},
|
||||
{
|
||||
name: 'cache hit ratio',
|
||||
value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0,
|
||||
},
|
||||
{
|
||||
name: 'ETag Matched',
|
||||
value: ctx.debug.etag,
|
||||
},
|
||||
{
|
||||
name: 'memory usage',
|
||||
value: stats.memory / 1000000 + ' MB',
|
||||
@@ -101,7 +105,7 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
{
|
||||
name: 'run time',
|
||||
value: (stats.elapsed / 3600000).toFixed(2) + ' 小时',
|
||||
value: (stats.elapsed / 3600000).toFixed(2) + ' hour(s)',
|
||||
},
|
||||
{
|
||||
name: 'hot routes',
|
||||
|
||||
Reference in New Issue
Block a user