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
This commit is contained in:
ITOH
2023-04-02 17:43:08 +02:00
committed by GitHub
parent 7a69c02d13
commit 1cb1b09460

View File

@@ -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<string, any>