fix(federation): 🚑 Don't re-parse response as JSON when FederationRequester has done so already

This commit is contained in:
Jesse Wierzbinski 2024-07-24 19:25:14 +02:00
parent 0bc6a89706
commit 5a52ac005b
No known key found for this signature in database
3 changed files with 39 additions and 33 deletions

View file

@ -139,7 +139,7 @@ export class Instance extends BaseInterface<typeof Instances> {
const logger = getLogger("federation");
try {
const { raw: response } = await FederationRequester.get(
const { ok, raw, data } = await FederationRequester.get(
wellKnownUrl,
{
// @ts-expect-error Bun extension
@ -147,12 +147,7 @@ export class Instance extends BaseInterface<typeof Instances> {
},
);
if (
!(
response.ok &&
response.headers.get("content-type")?.includes("json")
)
) {
if (!(ok && raw.headers.get("content-type")?.includes("json"))) {
// If the server doesn't have a Lysand well-known endpoint, it's not a Lysand instance
// Try to resolve ActivityPub metadata instead
const data = await Instance.fetchActivityPubMetadata(url);
@ -169,7 +164,7 @@ export class Instance extends BaseInterface<typeof Instances> {
try {
const metadata = await new EntityValidator().ServerMetadata(
await response.json(),
data,
);
return { metadata, protocol: "lysand" };
@ -198,23 +193,24 @@ export class Instance extends BaseInterface<typeof Instances> {
const logger = getLogger("federation");
try {
const { raw: response } = await FederationRequester.get(
wellKnownUrl,
{
const {
raw: response,
ok,
data: wellKnown,
} = await FederationRequester.get<{
links: { rel: string; href: string }[];
}>(wellKnownUrl, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
},
);
});
if (!response.ok) {
if (!ok) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - HTTP ${response.status}`;
return null;
}
const wellKnown = await response.json();
if (!wellKnown.links) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
@ -235,23 +231,30 @@ export class Instance extends BaseInterface<typeof Instances> {
return null;
}
const { raw: metadataResponse } = await FederationRequester.get(
metadataUrl.href,
{
const {
raw: metadataResponse,
ok: ok2,
data: metadata,
} = await FederationRequester.get<{
metadata: {
nodeName?: string;
title?: string;
nodeDescription?: string;
description?: string;
};
software: { version: string };
}>(metadataUrl.href, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
},
);
});
if (!metadataResponse.ok) {
if (!ok2) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
origin,
)} - HTTP ${metadataResponse.status}`;
return null;
}
const metadata = await metadataResponse.json();
return {
name:
metadata.metadata.nodeName || metadata.metadata.title || "",

View file

@ -592,12 +592,12 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
throw new Error(`Invalid URI to parse ${uri}`);
}
const { raw: response } = await FederationRequester.get(uri, {
const { data } = await FederationRequester.get(uri, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
});
note = await new EntityValidator().Note(await response.json());
note = await new EntityValidator().Note(data);
}
if (!note) {

View file

@ -340,16 +340,19 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
uri: string,
instance: Instance,
): Promise<User> {
const { raw: response } = await FederationRequester.get(uri, {
const {
raw: response,
data: json,
ok,
} = await FederationRequester.get<Partial<LysandUser>>(uri, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
});
if (!response.ok) {
if (!ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = (await response.json()) as Partial<LysandUser>;
const validator = new EntityValidator();
const data = await validator.User(json);