Skip to content

Commit

Permalink
Fix recursive redirection of docloader
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 6, 2024
1 parent 2711ed1 commit 4fe33d0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/runtime/docloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async function getRemoteDocument(
fetch: (url: string) => Promise<RemoteDocument>,
): Promise<RemoteDocument> {
const documentUrl = response.url === "" ? url : response.url;
const docUrl = new URL(documentUrl);
if (!response.ok) {
logger.error(
"Failed to fetch document: {status} {url} {headers}",
Expand All @@ -114,6 +115,7 @@ async function getRemoteDocument(
const contentType = response.headers.get("Content-Type");
const jsonLd = contentType == null ||
contentType === "application/activity+json" ||
contentType.startsWith("application/activity+json;") ||
contentType === "application/ld+json" ||
contentType.startsWith("application/ld+json;");
const linkHeader = response.headers.get("Link");
Expand All @@ -135,7 +137,8 @@ async function getRemoteDocument(
"type" in params &&
(params.type === "application/activity+json" ||
params.type === "application/ld+json" ||
params.type.startsWith("application/ld+json;"))
params.type.startsWith("application/ld+json;")) &&
new URL(uri).href !== docUrl.href
) {
logger.debug(
"Found alternate document: {alternateUrl} from {url}",
Expand Down Expand Up @@ -171,13 +174,14 @@ async function getRemoteDocument(
attribs.type === "application/activity+json" ||
attribs.type === "application/ld+json" ||
attribs.type.startsWith("application/ld+json;")
) && "href" in attribs
) && "href" in attribs &&
new URL(attribs.href, docUrl).href !== docUrl.href
) {
logger.debug(
"Found alternate document: {alternateUrl} from {url}",
{ alternateUrl: attribs.href, url: documentUrl },
);
return await fetch(attribs.href);
return await fetch(new URL(attribs.href, docUrl).href);
}
}
}
Expand Down

0 comments on commit 4fe33d0

Please sign in to comment.