From 1cb1b094609ea5b8ff418ee680e664c46de3f885 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sun, 2 Apr 2023 17:43:08 +0200 Subject: [PATCH] refactor!(rest): rest proxy attachment sending (#2924) * refactor!(rest): rest proxy attachment sending Currently attachments get encoded as base64 before being send to the proxy. This is not really necessary, instead we can just send `FormData` to the proxy. * fix lint --- packages/rest/src/manager.ts | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index 0e689e71c..5b5e276d1 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -6,7 +6,6 @@ import { camelize, camelToSnakeCase, delay, - encode, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, @@ -59,7 +58,6 @@ import type { DiscordVoiceRegion, DiscordWebhook, DiscordWelcomeScreen, - FileContent, GetMessagesOptions, GetScheduledEventUsers, MfaLevels, @@ -954,34 +952,7 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage async makeRequest(method, url, options) { if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') { - // Special handling for sending blobs across http to proxy - // TODO: fix this hacky handling - if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) { - if (!Array.isArray(options.body.file)) { - options.body.file = [options.body.file] - } - // convert blobs to string before sending to proxy - options.body.file = await Promise.all( - (options.body.file as FileContent[]).map(async (f: FileContent) => { - const url = encode(await f.blob.arrayBuffer()) - - return { name: f.name, blob: `data:${f.blob.type};base64,${url}` } - }), - ) - } - - const headers: HeadersInit = { - Authorization: rest.authorization ?? '', - } - if (options?.body) { - headers['Content-Type'] = 'application/json' - } - - const result = await fetch(`${rest.baseUrl}${url}`, { - body: options?.body ? JSON.stringify(options.body) : undefined, - headers, - method, - }) + const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options)) if (!result.ok) { const err = (await result.json().catch(() => {})) as Record