From 5a52ac005bcea99adcbf80ea47551be242080e10 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 24 Jul 2024 19:25:14 +0200 Subject: [PATCH] fix(federation): :ambulance: Don't re-parse response as JSON when FederationRequester has done so already --- packages/database-interface/instance.ts | 59 +++++++++++++------------ packages/database-interface/note.ts | 4 +- packages/database-interface/user.ts | 9 ++-- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/packages/database-interface/instance.ts b/packages/database-interface/instance.ts index 9effa534..1414f95a 100644 --- a/packages/database-interface/instance.ts +++ b/packages/database-interface/instance.ts @@ -139,7 +139,7 @@ export class Instance extends BaseInterface { 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 { }, ); - 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 { try { const metadata = await new EntityValidator().ServerMetadata( - await response.json(), + data, ); return { metadata, protocol: "lysand" }; @@ -198,23 +193,24 @@ export class Instance extends BaseInterface { const logger = getLogger("federation"); try { - const { raw: response } = await FederationRequester.get( - wellKnownUrl, - { - // @ts-expect-error Bun extension - proxy: config.http.proxy.address, - }, - ); + 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 { return null; } - const { raw: metadataResponse } = await FederationRequester.get( - metadataUrl.href, - { - // @ts-expect-error Bun extension - proxy: config.http.proxy.address, - }, - ); + 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 || "", diff --git a/packages/database-interface/note.ts b/packages/database-interface/note.ts index 9c6c00d0..bd76daf3 100644 --- a/packages/database-interface/note.ts +++ b/packages/database-interface/note.ts @@ -592,12 +592,12 @@ export class Note extends BaseInterface { 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) { diff --git a/packages/database-interface/user.ts b/packages/database-interface/user.ts index ad5d5104..8fb38f2d 100644 --- a/packages/database-interface/user.ts +++ b/packages/database-interface/user.ts @@ -340,16 +340,19 @@ export class User extends BaseInterface { uri: string, instance: Instance, ): Promise { - const { raw: response } = await FederationRequester.get(uri, { + const { + raw: response, + data: json, + ok, + } = await FederationRequester.get>(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; const validator = new EntityValidator(); const data = await validator.User(json);