fix: uws warning when async task

This commit is contained in:
MARCROCK22 2024-03-31 15:10:50 -04:00
parent da7a2ae946
commit 492d373d83

View File

@ -1,4 +1,9 @@
import { type APIInteraction, InteractionResponseType, InteractionType } from 'discord-api-types/v10'; import {
type APIInteractionResponse,
InteractionResponseType,
InteractionType,
type APIInteraction,
} from 'discord-api-types/v10';
import { filetypeinfo } from 'magic-bytes.js'; import { filetypeinfo } from 'magic-bytes.js';
import type { HttpRequest, HttpResponse } from 'uWebSockets.js'; import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
import { OverwrittenMimeTypes } from '../api'; import { OverwrittenMimeTypes } from '../api';
@ -131,44 +136,46 @@ export class HttpClient extends BaseClient {
break; break;
default: default:
await onInteractionCreate(this, rawBody, -1, async ({ body, files }) => { await onInteractionCreate(this, rawBody, -1, async ({ body, files }) => {
let response; res.cork(() => {
const headers: { 'Content-Type'?: string } = {}; let response: FormData | APIInteractionResponse;
const headers: { 'Content-Type'?: string } = {};
if (files) { if (files) {
response = new FormData(); response = new FormData();
for (const [index, file] of files.entries()) { for (const [index, file] of files.entries()) {
const fileKey = file.key ?? `files[${index}]`; const fileKey = file.key ?? `files[${index}]`;
if (isBufferLike(file.data)) { if (isBufferLike(file.data)) {
let contentType = file.contentType; let contentType = file.contentType;
if (!contentType) { if (!contentType) {
const [parsedType] = filetypeinfo(file.data); const [parsedType] = filetypeinfo(file.data);
if (parsedType) { if (parsedType) {
contentType = contentType =
OverwrittenMimeTypes[parsedType.mime as keyof typeof OverwrittenMimeTypes] ?? OverwrittenMimeTypes[parsedType.mime as keyof typeof OverwrittenMimeTypes] ??
parsedType.mime ?? parsedType.mime ??
'application/octet-stream'; 'application/octet-stream';
}
} }
response.append(fileKey, new Blob([file.data], { type: contentType }), file.name);
} else {
response.append(fileKey, new Blob([`${file.data}`], { type: file.contentType }), file.name);
} }
response.append(fileKey, new Blob([file.data], { type: contentType }), file.name);
} else {
response.append(fileKey, new Blob([`${file.data}`], { type: file.contentType }), file.name);
} }
if (body) {
response.append('payload_json', JSON.stringify(body));
}
} else {
response = body ?? {};
headers['Content-Type'] = 'application/json';
} }
if (body) {
response.append('payload_json', JSON.stringify(body)); for (const i in headers) {
res.writeHeader(i, headers[i as keyof typeof headers]!);
} }
} else {
response = body ?? {};
headers['Content-Type'] = 'application/json';
}
for (const i in headers) { return res.end(JSON.stringify(response));
res.writeHeader(i, headers[i as keyof typeof headers]!); });
}
return res.end(JSON.stringify(response));
}); });
break; break;
} }