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

View file

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