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