fix: middleware errors

This commit is contained in:
DIYgod
2024-01-21 18:35:11 +08:00
parent e7e3f689bd
commit 4591116733
14 changed files with 126 additions and 322 deletions

View File

@@ -29,31 +29,25 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
await next();
if (!ctx.res.body || ctx.res.headers.get('ETag')) {
const data = ctx.get('data');
if (!data || ctx.res.headers.get('ETag')) {
return;
}
const status = Math.trunc(ctx.res.status / 100);
if (2 !== status) {
return;
}
const lastBuildDate = data.lastBuildDate;
delete data.lastBuildDate;
const etag = etagCalculate(JSON.stringify(data));
const res = ctx.res as Response;
const body = await res.clone().text();
const etag = etagCalculate(body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '').replace(/<atom:link(.*)\/>/, ''));
ctx.set('ETag', etag);
ctx.header('ETag', etag);
const ifNoneMatch = ctx.req.header('If-None-Match') ?? null;
if (etagMatches(etag, ifNoneMatch)) {
console.log('in')
ctx.status(304);
ctx.body(null);
return ctx.body(null);
} else {
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
if (match) {
ctx.header('Last-Modified', match[1]);
}
ctx.header('Last-Modified', lastBuildDate);
}
};
export default middleware;
export default middleware;