From 021a3485f52e4b8fc3353f3e36dc4b5bb0d926fb Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 24 Jul 2024 18:20:01 +0200 Subject: [PATCH] feat(federation): :sparkles: Automatically try to reach the HTTP version of an URL if HTTPS fails --- federation/requester/index.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/federation/requester/index.ts b/federation/requester/index.ts index 0048e03..8e17867 100644 --- a/federation/requester/index.ts +++ b/federation/requester/index.ts @@ -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 }, @@ -125,7 +125,16 @@ export class FederationRequester { private async request( request: Request, ): Promise> { - 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) { @@ -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(