mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-11-25 08:39:25 +08:00
* style: use cloudflare/oxlint-config * style: fix no-known-value-widening * style: fix typescript/only-throw-error * style: fix typescript/no-floating-promises * style: fix typescript/use-unknown-in-catch-callback-variable * style: fix typescript/no-misused-promises * style: fix typescript/no-unsafe-unary-minus * style: fix typescript/no-for-in-array * style: fix react/purity * style: fix typescript/switch-exhaustiveness-check * style: fix typescript/unbound-method * style: fix react/rules-of-hooks * style: fix typescript/return-await * style: fix react/button-has-type * style: fix react/no-unescaped-entities * style: fix import/no-namespace * style: fix typescript/no-import-type-side-effects * style: fix no-promise-executor-return * style: fix no-useless-assignment * style: fix typescript/no-misused-spread * style: fix react/jsx-no-target-blank * style: fix typescript/await-thenable * style: fix typescript/no-unnecessary-type-assertion * style: fix import/consistent-type-specifier-style * style: fix react/no-unknown-property * style: remove unused disable directive * style: merge region * style: enable prefer-smaller-scope * style: add react/no-danger * style: add unicorn-js/prefer-uint8array-base64/hex * style: move non-recommended unicorn rules to custom region
95 lines
3.3 KiB
TypeScript
95 lines
3.3 KiB
TypeScript
import type { MiddlewareHandler } from 'hono';
|
|
|
|
import { config } from '@/config';
|
|
import RequestInProgressError from '@/errors/types/request-in-progress';
|
|
import type { Data } from '@/types';
|
|
import cacheModule from '@/utils/cache/index';
|
|
import { getRouteCacheKey } from '@/utils/cache/route-key';
|
|
import { toRFC822 } from '@/utils/time';
|
|
|
|
const bypassList = new Set(['/', '/robots.txt', '/logo.png', '/favicon.ico']);
|
|
|
|
const middleware: MiddlewareHandler = async (ctx, next) => {
|
|
if (!cacheModule.status.available || bypassList.has(ctx.req.path)) {
|
|
await next();
|
|
return;
|
|
}
|
|
|
|
const requestKey = getRouteCacheKey(ctx);
|
|
const key = 'rsshub:koa-redis-cache:' + requestKey;
|
|
const controlKey = 'rsshub:path-requested:' + requestKey;
|
|
|
|
let value = await cacheModule.globalCache.get(key);
|
|
|
|
// Only atomic backends can coordinate fetchers. HTTP/KV may return stale
|
|
// control keys after a completed request, while their feed cache stays useful.
|
|
let isRequesting = false;
|
|
let ownsClaim = false;
|
|
if (!value && cacheModule.globalCache.supportsAtomicClaims) {
|
|
ownsClaim = await cacheModule.globalCache.claim(controlKey, config.cache.requestTimeout);
|
|
isRequesting = !ownsClaim;
|
|
}
|
|
|
|
if (isRequesting) {
|
|
let retryTimes = process.env.NODE_ENV === 'test' ? 1 : 10;
|
|
let bypass = false;
|
|
while (retryTimes > 0) {
|
|
// eslint-disable-next-line no-await-in-loop
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, process.env.NODE_ENV === 'test' ? 3000 : 6000);
|
|
});
|
|
// eslint-disable-next-line no-await-in-loop
|
|
if ((await cacheModule.globalCache.get(controlKey)) !== '1') {
|
|
bypass = true;
|
|
break;
|
|
}
|
|
retryTimes--;
|
|
}
|
|
if (!bypass) {
|
|
throw new RequestInProgressError('This path is currently fetching, please come back later!');
|
|
}
|
|
value = await cacheModule.globalCache.get(key);
|
|
}
|
|
|
|
if (value) {
|
|
ctx.status(200);
|
|
ctx.header('RSSHub-Cache-Status', 'HIT');
|
|
ctx.set('data', JSON.parse(value));
|
|
await next();
|
|
return;
|
|
}
|
|
|
|
if (isRequesting) {
|
|
// waited out a stale claim without finding a cache entry, take over the fetch
|
|
ownsClaim = await cacheModule.globalCache.claim(controlKey, config.cache.requestTimeout);
|
|
if (!ownsClaim) {
|
|
throw new RequestInProgressError('This path is currently fetching, please come back later!');
|
|
}
|
|
}
|
|
|
|
// let routers control cache
|
|
ctx.set('cacheKey', key);
|
|
if (ownsClaim) {
|
|
ctx.set('cacheControlKey', controlKey);
|
|
}
|
|
|
|
try {
|
|
await next();
|
|
|
|
const data: Data = ctx.get('data');
|
|
if (ctx.res.headers.get('Cache-Control') !== 'no-cache' && data) {
|
|
data.lastBuildDate = toRFC822(Temporal.Now.instant());
|
|
ctx.set('data', data);
|
|
const body = JSON.stringify(data);
|
|
await cacheModule.globalCache.set(key, body, config.cache.routeExpire);
|
|
}
|
|
} finally {
|
|
// Release after writing the feed, including failures after the route ran.
|
|
if (ownsClaim) {
|
|
await cacheModule.globalCache.set(controlKey, '0', config.cache.requestTimeout);
|
|
}
|
|
}
|
|
};
|
|
|
|
export default middleware;
|