From 72aa46fd312eb6ce797903ad6e22bde253e698aa Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Wed, 20 Sep 2023 23:40:23 -0700 Subject: [PATCH] reimplement getResponse --- packages/backend/src/misc/fetch.ts | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/packages/backend/src/misc/fetch.ts b/packages/backend/src/misc/fetch.ts index 08242f0b52..6ff2170762 100644 --- a/packages/backend/src/misc/fetch.ts +++ b/packages/backend/src/misc/fetch.ts @@ -2,6 +2,7 @@ import * as http from "node:http"; import * as https from "node:https"; import { URL } from "node:url"; import request from "superagent"; +import superagent from "superagent"; import { HttpProxyAgent, HttpsProxyAgent } from "hpagent"; import config from "@/config/index.js"; @@ -22,6 +23,63 @@ export async function getJson( return response.body; } +export async function getResponse(args: { + url: string; + method: string; + body?: string; + headers: Record; + timeout?: number; + size?: number; +}): Promise { + const timeout = args.timeout || 10 * 1000; + const method = args.method.toUpperCase(); + const request = superagent(method, args.url); + if (args.headers) + request.set(args.headers); + if (args.body) + request.send(args.body); + const urlObj = new URL(args.url); + if (urlObj.protocol === "http:") { + request.agent(httpAgent); + } else if (urlObj.protocol === "https:") { + request.agent(httpsAgent); + } + + request.timeout({ + response: timeout, + deadline: timeout * 6, + }); + request.on("response", (res) => { + const contentLength = parseInt(res.header["content-length"] || "0", 10); + if (contentLength > (args.size || 10 * 1024 * 1024)) { + throw new StatusError( + `Response size exceeded: ${contentLength}`, + 413, + "Payload Too Large", + ); + } + }); + + try { + const response = await request; + if (!response.ok) { + throw new StatusError( + `${response.status} ${response.status}`, + response.status, + ); + } + return response; + } catch (error) { + if (error.response) { + throw new StatusError( + `${error.response.status} ${error.response.status}`, + error.response.status, + ); + } + throw error; + } +} + export async function getHtml( url: string, accept = "text/html, */*",