Skip to content

Commit

Permalink
feat(federation): ✨ Automatically try to reach the HTTP version of an…
Browse files Browse the repository at this point in the history
… URL if HTTPS fails
  • Loading branch information
CPlusPatch committed Jul 24, 2024
1 parent 2313bcb commit 021a348
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions federation/requester/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ResponseError<
export class FederationRequester {
constructor(
private serverUrl: URL,
private signatureConstructor: SignatureConstructor,
private signatureConstructor?: SignatureConstructor,
public globalCatch: (error: ResponseError) => void = () => {
// Do nothing by default
},
Expand Down Expand Up @@ -125,7 +125,16 @@ export class FederationRequester {
private async request<ReturnType>(
request: Request,
): Promise<Output<ReturnType>> {
const result = await fetch(request);
const result = await fetch(request).catch((e) => {
// If using https, try and use http
const url = new URL(request.url);
if (url.protocol === "https") {
url.protocol = "http";
return fetch(url, request);
}

throw e;
});
const isJson = result.headers.get("Content-Type")?.includes("json");

if (!result.ok) {
Expand Down Expand Up @@ -180,7 +189,9 @@ export class FederationRequester {
...extra,
});

return (await this.signatureConstructor.sign(request)).request;
return this.signatureConstructor
? (await this.signatureConstructor.sign(request)).request
: request;
}

public async get<ReturnType>(
Expand Down

0 comments on commit 021a348

Please sign in to comment.