refactor(federation): ♻️ Refactor code to use less fetch calls and instead use FederationRequester

This commit is contained in:
Jesse Wierzbinski 2024-07-24 18:52:30 +02:00
parent 833f261392
commit daba8e8178
No known key found for this signature in database
5 changed files with 33 additions and 24 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -104,7 +104,7 @@
"@json2csv/plainjs": "^7.0.6",
"@logtape/logtape": "npm:@jsr/logtape__logtape@0.4.2",
"@lysand-org/client": "^0.2.5",
"@lysand-org/federation": "2.1.8",
"@lysand-org/federation": "^2.1.9-test2",
"@oclif/core": "^4.0.14",
"@sentry/bun": "^8.19.0",
"@tufjs/canonical-json": "^2.0.0",

View file

@ -1,5 +1,9 @@
import { getLogger } from "@logtape/logtape";
import { EntityValidator, type ValidationError } from "@lysand-org/federation";
import {
EntityValidator,
FederationRequester,
type ValidationError,
} from "@lysand-org/federation";
import type { ServerMetadata } from "@lysand-org/federation/types";
import chalk from "chalk";
import { config } from "config-manager";
@ -135,9 +139,13 @@ export class Instance extends BaseInterface<typeof Instances> {
const logger = getLogger("federation");
try {
const response = await fetch(wellKnownUrl, {
proxy: config.http.proxy.address,
});
const { raw: response } = await FederationRequester.get(
wellKnownUrl,
{
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
},
);
if (
!(
@ -190,9 +198,13 @@ export class Instance extends BaseInterface<typeof Instances> {
const logger = getLogger("federation");
try {
const response = await fetch(wellKnownUrl, {
proxy: config.http.proxy.address,
});
const { raw: response } = await FederationRequester.get(
wellKnownUrl,
{
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
},
);
if (!response.ok) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(
@ -223,9 +235,13 @@ export class Instance extends BaseInterface<typeof Instances> {
return null;
}
const metadataResponse = await fetch(metadataUrl.href, {
proxy: config.http.proxy.address,
});
const { raw: metadataResponse } = await FederationRequester.get(
metadataUrl.href,
{
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
},
);
if (!metadataResponse.ok) {
logger.error`Failed to fetch ActivityPub metadata for instance ${chalk.bold(

View file

@ -6,7 +6,7 @@ import type {
Attachment as ApiAttachment,
Status as ApiStatus,
} from "@lysand-org/client/types";
import { EntityValidator } from "@lysand-org/federation";
import { EntityValidator, FederationRequester } from "@lysand-org/federation";
import type {
ContentFormat,
Note as LysandNote,
@ -591,11 +591,8 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
throw new Error(`Invalid URI to parse ${uri}`);
}
const response = await fetch(uri, {
method: "GET",
headers: {
Accept: "application/json",
},
const { raw: response } = await FederationRequester.get(uri, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
});

View file

@ -6,10 +6,7 @@ import type {
Account as ApiAccount,
Mention as ApiMention,
} from "@lysand-org/client/types";
import {
EntityValidator,
type FederationRequester,
} from "@lysand-org/federation";
import { EntityValidator, FederationRequester } from "@lysand-org/federation";
import type { Entity, User as LysandUser } from "@lysand-org/federation/types";
import {
type InferInsertModel,
@ -343,9 +340,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
uri: string,
instance: Instance,
): Promise<User> {
const response = await fetch(uri, {
method: "GET",
headers: { Accept: "application/json" },
const { raw: response } = await FederationRequester.get(uri, {
// @ts-expect-error Bun extension
proxy: config.http.proxy.address,
});