fix: attachment sending (#11015)

* fix: attachment sending

* test: add tests

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Almeida
2025-07-30 11:53:44 +01:00
committed by GitHub
parent 593369dbb8
commit c3dc58169b
2 changed files with 39 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
import { STATUS_CODES } from 'node:http';
import { URLSearchParams } from 'node:url';
import { types } from 'node:util';
import { type RequestInit, request, Headers } from 'undici';
import { type RequestInit, request, Headers, FormData as UndiciFormData } from 'undici';
import type { HeaderRecord } from 'undici/types/header.js';
import type { ResponseLike } from '../shared.js';
@@ -52,8 +52,10 @@ export async function resolveBody(body: RequestInit['body']): Promise<Exclude<Re
return new Uint8Array(body.buffer);
} else if (body instanceof Blob) {
return new Uint8Array(await body.arrayBuffer());
} else if (body instanceof FormData) {
} else if (body instanceof UndiciFormData) {
return body;
} else if (body instanceof FormData) {
return globalToUndiciFormData(body);
} else if ((body as Iterable<Uint8Array>)[Symbol.iterator]) {
const chunks = [...(body as Iterable<Uint8Array>)];
@@ -70,3 +72,17 @@ export async function resolveBody(body: RequestInit['body']): Promise<Exclude<Re
throw new TypeError(`Unable to resolve body.`);
}
function globalToUndiciFormData(fd: globalThis.FormData): UndiciFormData {
const clone = new UndiciFormData();
for (const [name, value] of fd.entries()) {
if (typeof value === 'string') {
clone.append(name, value);
} else {
clone.append(name, value, value.name);
}
}
return clone;
}