Skip to content

Commit 7829177

Browse files
committed
feat: Add guesser fallback
This will send requests to likely URLs for an RSS feed to check if it exists. This is opt-in per request with a argument to the findFeed resolver since it will send 8 parallel requests.
1 parent a9c62bf commit 7829177

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/handlers/findFeed.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import type { CheerioAPI } from "cheerio";
33
import type { Element, Node } from "domhandler";
44
import normalizeUrl from "normalize-url";
55

6+
import isUrl from "is-url";
67
import { InvalidUrlError } from "../errors.js";
78
import { logger } from "../logger.js";
89
import request from "../request.js";
910
import { parseFromQuery, parseFromString } from "./feed.js";
10-
import isUrl from "is-url";
1111

1212
type FindFeedResponse = {
1313
title: string;
@@ -106,6 +106,8 @@ export async function findFeed({
106106
throw new InvalidUrlError(url);
107107
}
108108

109+
logger.info({ url, normalizedUrl, normalize, withGuessFallback }, "findFeed");
110+
109111
const response = await request(normalizedUrl);
110112
const content = response.text;
111113

@@ -151,7 +153,7 @@ export async function findFeed({
151153
}
152154
}
153155

154-
const result = [
156+
let result = [
155157
...(await findRssFeedsInDom(dom, normalizedUrl)),
156158
...(await findJsonFeedsInDom(dom, normalizedUrl)),
157159
];
@@ -160,5 +162,24 @@ export async function findFeed({
160162
return findFeed({ url, normalize: false });
161163
}
162164

165+
if (result.length === 0 && withGuessFallback) {
166+
const url = new URL(normalizedUrl);
167+
url.pathname = "";
168+
const urlWithoutPath = url.toString();
169+
result = (
170+
await Promise.all([
171+
findFeed({ url: `${normalizedUrl}/feed.xml` }).catch(() => []),
172+
findFeed({ url: `${normalizedUrl}/atom.xml` }).catch(() => []),
173+
findFeed({ url: `${normalizedUrl}/rss.xml` }).catch(() => []),
174+
findFeed({ url: `${normalizedUrl}/feed.json` }).catch(() => []),
175+
//////////
176+
findFeed({ url: `${urlWithoutPath}/feed.xml` }).catch(() => []),
177+
findFeed({ url: `${urlWithoutPath}/atom.xml` }).catch(() => []),
178+
findFeed({ url: `${urlWithoutPath}/rss.xml` }).catch(() => []),
179+
findFeed({ url: `${urlWithoutPath}/feed.json` }).catch(() => []),
180+
])
181+
).flat();
182+
}
183+
163184
return result;
164185
}

src/logger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ export function createLogger(options?: LoggingOptions): pino.Logger {
1818
});
1919
return logger;
2020
}
21+
22+
createLogger();

src/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const typeDefs = `
4141
}
4242
4343
type Query {
44-
findFeed(url: String!): [FindFeedResult]!
44+
findFeed(url: String!, withGuessFallback: Boolean): [FindFeedResult]!
4545
feed(url: String!, parser: Parser, startTime: String, endTime: String): Feed
4646
}
4747
`;

0 commit comments

Comments
 (0)