Use custom fetcher for ApRequest / ApResolver

This commit is contained in:
tamaina 2023-01-04 10:56:45 +00:00
parent ab16810655
commit f14150c69d
4 changed files with 18 additions and 11 deletions

View File

@ -32,7 +32,7 @@ export class DownloadService {
) { ) {
this.logger = this.loggerService.getLogger('download'); this.logger = this.loggerService.getLogger('download');
this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherConstructorOption( this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption(
{ {
connect: process.env.NODE_ENV === 'development' ? connect: process.env.NODE_ENV === 'development' ?
this.httpRequestService.clientDefaults.connect this.httpRequestService.clientDefaults.connect

View File

@ -58,6 +58,7 @@ export class UndiciFetcher {
...args.agentOptions, ...args.agentOptions,
connect: (process.env.NODE_ENV !== 'production' && typeof args.agentOptions.connect !== 'function') connect: (process.env.NODE_ENV !== 'production' && typeof args.agentOptions.connect !== 'function')
? (options, cb) => { ? (options, cb) => {
// Custom connector for debug
undici.buildConnector(args.agentOptions.connect as undici.buildConnector.BuildOptions)(options, (err, socket) => { undici.buildConnector(args.agentOptions.connect as undici.buildConnector.BuildOptions)(options, (err, socket) => {
this.logger?.debug('Socket connector called', socket); this.logger?.debug('Socket connector called', socket);
if (err) { if (err) {
@ -65,7 +66,7 @@ export class UndiciFetcher {
cb(new Error(`Error while socket connecting\n${err}`), null); cb(new Error(`Error while socket connecting\n${err}`), null);
return; return;
} }
this.logger?.debug(`Socket connected: ${socket.localPort} => ${socket.remoteAddress}`); this.logger?.debug(`Socket connected: port ${socket.localPort} => remote ${socket.remoteAddress}`);
cb(null, socket); cb(null, socket);
}); });
} : args.agentOptions.connect, } : args.agentOptions.connect,
@ -80,14 +81,15 @@ export class UndiciFetcher {
connect: (process.env.NODE_ENV !== 'production' && typeof (args.proxy?.options?.connect ?? args.agentOptions.connect) !== 'function') connect: (process.env.NODE_ENV !== 'production' && typeof (args.proxy?.options?.connect ?? args.agentOptions.connect) !== 'function')
? (options, cb) => { ? (options, cb) => {
// Custom connector for debug
undici.buildConnector((args.proxy?.options?.connect ?? args.agentOptions.connect) as undici.buildConnector.BuildOptions)(options, (err, socket) => { undici.buildConnector((args.proxy?.options?.connect ?? args.agentOptions.connect) as undici.buildConnector.BuildOptions)(options, (err, socket) => {
this.logger?.debug('Socket connector called', socket); this.logger?.debug('Socket connector called (secure)', socket);
if (err) { if (err) {
this.logger?.debug(`Socket error`, err); this.logger?.debug(`Socket error`, err);
cb(new Error(`Error while socket connecting\n${err}`), null); cb(new Error(`Error while socket connecting\n${err}`), null);
return; return;
} }
this.logger?.debug(`Socket connected: ${socket.localPort} => ${socket.remoteAddress}`); this.logger?.debug(`Socket connected (secure): port ${socket.localPort} => remote ${socket.remoteAddress}`);
cb(null, socket); cb(null, socket);
}); });
} : (args.proxy?.options?.connect ?? args.agentOptions.connect), } : (args.proxy?.options?.connect ?? args.agentOptions.connect),
@ -218,12 +220,12 @@ export class HttpRequestService {
this.maxSockets = Math.max(256, this.config.deliverJobConcurrency ?? 128); this.maxSockets = Math.max(256, this.config.deliverJobConcurrency ?? 128);
this.defaultFetcher = new UndiciFetcher(this.getStandardUndiciFetcherConstructorOption(), this.logger); this.defaultFetcher = new UndiciFetcher(this.getStandardUndiciFetcherOption(), this.logger);
this.fetch = this.defaultFetcher.fetch; this.fetch = this.defaultFetcher.fetch;
this.getHtml = this.defaultFetcher.getHtml; this.getHtml = this.defaultFetcher.getHtml;
this.defaultJsonFetcher = new UndiciFetcher(this.getStandardUndiciFetcherConstructorOption({ this.defaultJsonFetcher = new UndiciFetcher(this.getStandardUndiciFetcherOption({
maxResponseSize: 1024 * 256, maxResponseSize: 1024 * 256,
}), this.logger); }), this.logger);
@ -272,7 +274,7 @@ export class HttpRequestService {
* @param bypassProxy Allways bypass proxy * @param bypassProxy Allways bypass proxy
*/ */
@bindThis @bindThis
public getStandardUndiciFetcherConstructorOption(opts: undici.Agent.Options = {}, proxyOpts: undici.Agent.Options = {}) { public getStandardUndiciFetcherOption(opts: undici.Agent.Options = {}, proxyOpts: undici.Agent.Options = {}) {
return { return {
agentOptions: { agentOptions: {
...this.clientDefaults, ...this.clientDefaults,

View File

@ -5,7 +5,7 @@ import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import type { User } from '@/models/entities/User.js'; import type { User } from '@/models/entities/User.js';
import { UserKeypairStoreService } from '@/core/UserKeypairStoreService.js'; import { UserKeypairStoreService } from '@/core/UserKeypairStoreService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js'; import { HttpRequestService, UndiciFetcher } from '@/core/HttpRequestService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
type Request = { type Request = {
@ -28,6 +28,8 @@ type PrivateKey = {
@Injectable() @Injectable()
export class ApRequestService { export class ApRequestService {
private undiciFetcher: UndiciFetcher;
constructor( constructor(
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
@ -35,6 +37,7 @@ export class ApRequestService {
private userKeypairStoreService: UserKeypairStoreService, private userKeypairStoreService: UserKeypairStoreService,
private httpRequestService: HttpRequestService, private httpRequestService: HttpRequestService,
) { ) {
this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption());
} }
@bindThis @bindThis
@ -152,7 +155,7 @@ export class ApRequestService {
}, },
}); });
await this.httpRequestService.fetch( await this.undiciFetcher.fetch(
url, url,
{ {
method: req.request.method, method: req.request.method,

View File

@ -4,7 +4,7 @@ import { InstanceActorService } from '@/core/InstanceActorService.js';
import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository } from '@/models/index.js'; import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { MetaService } from '@/core/MetaService.js'; import { MetaService } from '@/core/MetaService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js'; import { HttpRequestService, UndiciFetcher } from '@/core/HttpRequestService.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { UtilityService } from '@/core/UtilityService.js'; import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
@ -17,6 +17,7 @@ import type { IObject, ICollection, IOrderedCollection } from './type.js';
export class Resolver { export class Resolver {
private history: Set<string>; private history: Set<string>;
private user?: ILocalUser; private user?: ILocalUser;
private undiciFetcher: UndiciFetcher;
constructor( constructor(
private config: Config, private config: Config,
@ -34,6 +35,7 @@ export class Resolver {
private recursionLimit = 100, private recursionLimit = 100,
) { ) {
this.history = new Set(); this.history = new Set();
this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption());
} }
@bindThis @bindThis
@ -97,7 +99,7 @@ export class Resolver {
const object = (this.user const object = (this.user
? await this.apRequestService.signedGet(value, this.user) as IObject ? await this.apRequestService.signedGet(value, this.user) as IObject
: await this.httpRequestService.getJson<IObject>(value, 'application/activity+json, application/ld+json')); : await this.undiciFetcher.getJson<IObject>(value, 'application/activity+json, application/ld+json'));
if (object == null || ( if (object == null || (
Array.isArray(object['@context']) ? Array.isArray(object['@context']) ?