Files
RSSHub/lib/middleware/template.tsx
T
dependabot[bot]andTony 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 68.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-20 23:11:27 +08:00

133 lines
5.3 KiB
TypeScript

import type { MiddlewareHandler } from 'hono';
import { config } from '@/config';
import type { Data } from '@/types';
import cacheModule from '@/utils/cache/index';
import { collapseWhitespace, convertDateToISO8601 } from '@/utils/common-utils';
import { Atom, json, RSS, rss3 } from '@/utils/render';
const middleware: MiddlewareHandler = async (ctx, next) => {
// Set RSS <ttl> (minute) according to the availability of cache
// * available: max(config.cache.routeExpire / 60, 1)
// * unavailable: 1
// The minimum <ttl> is limited to 1 minute to prevent potential misuse
const ttl = (cacheModule.status.available && Math.trunc(config.cache.routeExpire / 60)) || 1;
await next();
const apiData = ctx.get('apiData');
if (apiData) {
return ctx.json(apiData);
}
const data: Data = ctx.get('data');
const outputType = ctx.req.query('format') || 'rss';
// only enable when debugInfo=true
if (config.debugInfo) {
if (outputType === 'debug.json') {
return ctx.json(ctx.get('json') || { message: 'plugin does not set debug json' });
}
if (/\d+\.debug\.html$/.test(outputType)) {
const index = Number.parseInt(outputType.match(/(\d+)\.debug\.html$/)?.[1] || '0');
return ctx.html(data?.item?.[index]?.description || `data.item[${index}].description not found`);
}
}
if (data) {
data.title = collapseWhitespace(data.title) || '';
data.description &&= collapseWhitespace(data.description) || '';
data.author &&= collapseWhitespace(data.author) || '';
if (data.item) {
for (const item of data.item) {
if (item.title) {
item.title = collapseWhitespace(item.title) || '';
// trim title length
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length === 1 ? 1 : 2;
if (length > config.titleLengthLimit) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}
}
if (item.description) {
// https://stackoverflow.com/questions/2507608/error-input-is-not-proper-utf-8-indicate-encoding-using-phps-simplexml-lo/40552083#40552083
// https://stackoverflow.com/questions/1497885/remove-control-characters-from-php-string/1497928#1497928
// remove unicode control characters
// see #14940 #14943 #15262
// oxlint-disable-next-line no-control-regex
item.description = item.description.replaceAll(/[\u{0000}-\u{0009}\v\f\u{000E}-\u{001F}\u{007F}\u{200B}\u{FFFF}]/gu, '');
}
if (typeof item.author === 'string') {
item.author = collapseWhitespace(item.author) || '';
} else if (typeof item.author === 'object' && item.author !== null) {
for (const a of item.author) {
a.name = collapseWhitespace(a.name) || '';
}
if (outputType !== 'json') {
item.author = item.author.map((a: { name: string }) => a.name).join(', ');
}
}
if (item.itunes_duration && ((typeof item.itunes_duration === 'string' && !item.itunes_duration.includes(':')) || (typeof item.itunes_duration === 'number' && !Number.isNaN(item.itunes_duration)))) {
item.itunes_duration = +item.itunes_duration;
item.itunes_duration =
Math.floor(item.itunes_duration / 3600) + ':' + (Math.floor((item.itunes_duration % 3600) / 60) / 100).toFixed(2).slice(-2) + ':' + (((item.itunes_duration % 3600) % 60) / 100).toFixed(2).slice(-2);
}
if (outputType !== 'rss') {
try {
item.pubDate &&= convertDateToISO8601(item.pubDate) || '';
} catch {
item.pubDate = '';
}
try {
item.updated &&= convertDateToISO8601(item.updated) || '';
} catch {
item.updated = '';
}
}
}
}
}
const currentDate = new Date();
const result = {
lastBuildDate: currentDate.toUTCString(),
updated: currentDate.toISOString(),
ttl,
atomlink: ctx.req.url,
...data,
};
if (config.isPackage) {
return ctx.json(result);
}
if (ctx.get('redirect')) {
return ctx.redirect(ctx.get('redirect'), 301);
}
if (ctx.get('no-content')) {
return ctx.body(null);
}
// retain .ums for backward compatibility
switch (outputType) {
case 'ums':
case 'rss3':
return ctx.json(rss3(result));
case 'json':
ctx.header('Content-Type', 'application/feed+json; charset=UTF-8');
return ctx.body(json(result));
case 'atom':
return ctx.render(<Atom data={result} />);
default:
return ctx.render(<RSS data={result} />);
}
};
export default middleware;