mirror of
https://github.com/versia-pub/api.git
synced 2025-12-06 08:28:19 +01:00
feat(federation): ✨ Automatically try to reach the HTTP version of an URL if HTTPS fails
This commit is contained in:
parent
2313bcbbcc
commit
021a3485f5
|
|
@ -58,7 +58,7 @@ export class ResponseError<
|
||||||
export class FederationRequester {
|
export class FederationRequester {
|
||||||
constructor(
|
constructor(
|
||||||
private serverUrl: URL,
|
private serverUrl: URL,
|
||||||
private signatureConstructor: SignatureConstructor,
|
private signatureConstructor?: SignatureConstructor,
|
||||||
public globalCatch: (error: ResponseError) => void = () => {
|
public globalCatch: (error: ResponseError) => void = () => {
|
||||||
// Do nothing by default
|
// Do nothing by default
|
||||||
},
|
},
|
||||||
|
|
@ -125,7 +125,16 @@ export class FederationRequester {
|
||||||
private async request<ReturnType>(
|
private async request<ReturnType>(
|
||||||
request: Request,
|
request: Request,
|
||||||
): Promise<Output<ReturnType>> {
|
): 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");
|
const isJson = result.headers.get("Content-Type")?.includes("json");
|
||||||
|
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
|
|
@ -180,7 +189,9 @@ export class FederationRequester {
|
||||||
...extra,
|
...extra,
|
||||||
});
|
});
|
||||||
|
|
||||||
return (await this.signatureConstructor.sign(request)).request;
|
return this.signatureConstructor
|
||||||
|
? (await this.signatureConstructor.sign(request)).request
|
||||||
|
: request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get<ReturnType>(
|
public async get<ReturnType>(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue