server: refactor fetching private key

Especially in the case where the private key is used in an "array deliver",
it makes sense to only get the private key once instead of having the overhead
of fetching the key for each HTTP request.
This commit is contained in:
Johann150 2024-03-27 21:22:25 +01:00
parent 09ff7f0c7d
commit 1af0687423
Signed by: Johann150
GPG Key ID: 9EE6577A2A06F8F1
2 changed files with 7 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import { Instances } from '@/models/index.js';
import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js';
import { toPuny } from '@/misc/convert-host.js';
import { StatusError } from '@/misc/fetch.js';
import { getUserKeypair } from '@/misc/keypair-store.js';
import { shouldSkipInstance } from '@/misc/skipped-instances.js';
import { DeliverJobData } from '@/queue/types.js';
@ -18,13 +19,15 @@ export default async (job: Bull.Job<DeliverJobData>) => {
if (await shouldSkipInstance(puny)) return 'skip';
const keypair = await getUserKeypair(job.data.user.id);
try {
if (Array.isArray(job.data.content)) {
await Promise.all(
job.data.content.map(x => request(job.data.user, job.data.to, x))
job.data.content.map(x => request(job.data.user, job.data.to, x, keypair))
);
} else {
await request(job.data.user, job.data.to, job.data.content);
await request(job.data.user, job.data.to, job.data.content, keypair);
}
// Update stats

View File

@ -2,6 +2,7 @@ import { URL } from 'node:url';
import config from '@/config/index.js';
import { getUserKeypair } from '@/misc/keypair-store.js';
import { User } from '@/models/entities/user.js';
import { UserKeypair } from '@/models/entities/user-keypair.js';
import { getResponse } from '@/misc/fetch.js';
import { createSignedPost, createSignedGet } from './ap-request.js';
import { apRequestChart, federationChart, instanceChart } from '@/services/chart/index.js';
@ -14,11 +15,9 @@ import { apRequestChart, federationChart, instanceChart } from '@/services/chart
* @param url The URL of the inbox.
* @param object The Activity or other object to be posted to the inbox.
*/
export async function request(user: { id: User['id'] }, url: string, object: any): Promise<void> {
export async function request(user: { id: User['id'] }, url: string, object: any, keypair: UserKeypair): Promise<void> {
const body = JSON.stringify(object);
const keypair = await getUserKeypair(user.id);
const req = createSignedPost({
key: {
privateKeyPem: keypair.privateKey,