Skip to content

Commit

Permalink
Let document loaders manually follow redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed May 1, 2024
1 parent 2ac508c commit 00458ee
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions runtime/docloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createRequest(url: string): Request {
headers: {
Accept: "application/activity+json, application/ld+json",
},
redirect: "follow",
redirect: "manual",
});
}

Expand Down Expand Up @@ -122,6 +122,13 @@ export async function fetchDocumentLoader(
const request = createRequest(url);
logRequest(request);
const response = await fetch(request);
// Follow redirects manually to get the final URL:
if (
response.status >= 300 && response.status < 400 &&
response.headers.has("Location")
) {
return fetchDocumentLoader(response.headers.get("Location")!);
}
return getRemoteDocument(url, response);
}

Expand All @@ -139,13 +146,21 @@ export function getAuthenticatedDocumentLoader(
identity: { keyId: URL; privateKey: CryptoKey },
): DocumentLoader {
validateCryptoKey(identity.privateKey);
return async (url: string): Promise<RemoteDocument> => {
async function load(url: string): Promise<RemoteDocument> {
let request = createRequest(url);
request = await sign(request, identity.privateKey, identity.keyId);
logRequest(request);
const response = await fetch(request);
// Follow redirects manually to get the final URL:
if (
response.status >= 300 && response.status < 400 &&
response.headers.has("Location")
) {
return load(response.headers.get("Location")!);
}
return getRemoteDocument(url, response);
};
}
return load;
}

/**
Expand Down

0 comments on commit 00458ee

Please sign in to comment.