From b61e477c0cf18d30d59608f4b5bba31fe1762f95 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Wed, 19 Jul 2023 10:13:33 +0200 Subject: [PATCH] server: fix default arguments for fetch --- packages/backend/src/misc/fetch.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/misc/fetch.ts b/packages/backend/src/misc/fetch.ts index d9c1de384..4224589e6 100644 --- a/packages/backend/src/misc/fetch.ts +++ b/packages/backend/src/misc/fetch.ts @@ -33,11 +33,26 @@ export async function getHtml(url: string, accept = 'text/html, */*', timeout = return await res.text(); } -export async function getResponse(args: { url: string, method: string, body?: string, headers: Record, timeout: number = 10 * SECOND, size?: number, redirect: 'follow' | 'manual' | 'error' = 'follow' }) { +export async function getResponse(_args: { + url: string, + method: string, + body?: string, + headers: Record, + timeout?: number, + size?: number, + redirect?: 'follow' | 'manual' | 'error', +}) { + const args = { + timeout: 10 * SECOND, + size: 10 * 1024 * 1024, // 10 MiB + redirect: 'follow', + ..._args, + }; + const controller = new AbortController(); setTimeout(() => { controller.abort(); - }, timeout); + }, args.timeout); const res = await fetch(args.url, { method: args.method, @@ -46,7 +61,7 @@ export async function getResponse(args: { url: string, method: string, body?: st }, args.headers), body: args.body, redirect: args.redirect, - size: args.size || 10 * 1024 * 1024, // 10 MiB + size: args.size, agent: getAgentByUrl, signal: controller.signal, });