Files
DIYgodandClaude Opus 5.5 2661d161ba feat(utils): limit the decoded size of upstream response bodies
Add REQUEST_MAX_RESPONSE_SIZE (default 16 MiB), applied to every fetch,
ofetch and got response and to rss-parser's parseURL. A request can raise
it with the maxResponseSize option.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 01:21:13 +08:00

39 lines
1.1 KiB
TypeScript

import Parser from 'rss-parser';
import { config } from '@/config';
import type { DataItem, Language } from '@/types';
interface ParsedFeed {
author?: string;
copyright?: string;
creator?: string;
generator?: string;
language?: Language;
lastBuildDate?: string;
managingEditor?: string;
pubDate?: string;
publisher?: string;
webMaster?: string;
}
type ParsedItem = Pick<DataItem, 'author' | 'category' | 'description' | 'enclosure_type' | 'enclosure_url' | 'id'> & {
magnet?: string;
};
// A Parser reuses a single xml2js parser, whose state can outlive a malformed document and leak into the next one, so every document gets its own
const createParser = () =>
new Parser<ParsedFeed, ParsedItem>({
customFields: {
item: ['magnet'],
},
headers: {
'User-Agent': config.ua,
},
maxResponseSize: config.requestMaxResponseSize,
});
export default {
parseString: (xml: string) => createParser().parseString(xml),
parseURL: (feedUrl: string) => createParser().parseURL(feedUrl),
};