mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 14:07:54 +08:00
feat(core): add edge and last-modified header, close #338
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
const etagCalculate = require('etag');
|
||||||
const logger = require('../utils/logger');
|
const logger = require('../utils/logger');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
const headers = {
|
const headers = {
|
||||||
@@ -11,5 +12,27 @@ const headers = {
|
|||||||
module.exports = async (ctx, next) => {
|
module.exports = async (ctx, next) => {
|
||||||
logger.info(`${ctx.url}, user IP: ${ctx.ips[0] || ctx.ip}`);
|
logger.info(`${ctx.url}, user IP: ${ctx.ips[0] || ctx.ip}`);
|
||||||
ctx.set(headers);
|
ctx.set(headers);
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
|
|
||||||
|
let body = ctx.body;
|
||||||
|
if (!body || typeof body !== 'string' || ctx.response.get('ETag')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const status = (ctx.status / 100) | 0;
|
||||||
|
if (2 !== status) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
|
||||||
|
if (match) {
|
||||||
|
ctx.set({
|
||||||
|
'Last-Modified': body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
body = body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '');
|
||||||
|
|
||||||
|
ctx.response.etag = etagCalculate(body);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,6 +55,7 @@
|
|||||||
"co-redis": "2.1.1",
|
"co-redis": "2.1.1",
|
||||||
"currency-symbol-map": "4.0.4",
|
"currency-symbol-map": "4.0.4",
|
||||||
"dayjs": "1.8.12",
|
"dayjs": "1.8.12",
|
||||||
|
"etag": "1.8.1",
|
||||||
"form-data": "2.3.3",
|
"form-data": "2.3.3",
|
||||||
"git-rev-sync": "1.12.0",
|
"git-rev-sync": "1.12.0",
|
||||||
"googleapis": "39.2.0",
|
"googleapis": "39.2.0",
|
||||||
|
|||||||
@@ -15,5 +15,7 @@ describe('header', () => {
|
|||||||
expect(response.headers['access-control-allow-methods']).toBe('GET');
|
expect(response.headers['access-control-allow-methods']).toBe('GET');
|
||||||
expect(response.headers['content-type']).toBe('application/xml; charset=utf-8');
|
expect(response.headers['content-type']).toBe('application/xml; charset=utf-8');
|
||||||
expect(response.headers['cache-control']).toBe(`max-age=${config.cacheExpire / 2}`);
|
expect(response.headers['cache-control']).toBe(`max-age=${config.cacheExpire / 2}`);
|
||||||
|
expect(response.headers['last-modified']).toBe(response.text.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1]);
|
||||||
|
expect(response.headers.etag).toBe('"b37-MORyrF0tJ8BFw0xLLZL/zBYAFPY"');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3877,7 +3877,7 @@ esutils@^2.0.2:
|
|||||||
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
|
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
|
||||||
integrity sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=
|
integrity sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=
|
||||||
|
|
||||||
etag@~1.8.1:
|
etag@1.8.1, etag@~1.8.1:
|
||||||
version "1.8.1"
|
version "1.8.1"
|
||||||
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
|
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
|
||||||
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
|
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
|
||||||
|
|||||||
Reference in New Issue
Block a user