Skip to content

Commit

Permalink
fix(formdata): handle file names correctly when setting on formdata (o…
Browse files Browse the repository at this point in the history
…ven-sh#12379)

Co-authored-by: Andrew Johnston <andrew@bun.sh>
  • Loading branch information
billywhizz and Andrew Johnston authored Jul 6, 2024
1 parent 6f52b64 commit 050a4b5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/bun.js/bindings/webcore/JSDOMFormData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,7 @@ static inline JSC::EncodedJSValue jsDOMFormDataPrototypeFunction_set2Body(JSC::J
auto name = convert<IDLUSVString>(*lexicalGlobalObject, argument0.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
EnsureStillAliveScope argument1 = callFrame->uncheckedArgument(1);

EnsureStillAliveScope argument2 = callFrame->argument(2);
auto filename = argument2.value().isUndefined() ? String() : convert<IDLUSVString>(*lexicalGlobalObject, argument2.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

RefPtr<Blob> blobValue = nullptr;
if (argument1.value().inherits<JSBlob>()) {
Expand All @@ -473,8 +470,11 @@ static inline JSC::EncodedJSValue jsDOMFormDataPrototypeFunction_set2Body(JSC::J

if (!blobValue) {
throwTypeError(lexicalGlobalObject, throwScope, "Expected argument to be a Blob."_s);
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
}
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

auto filename = argument2.value().isUndefined() ? Blob__getFileNameString(blobValue->impl()).toWTFString(BunString::ZeroCopy) : convert<IDLUSVString>(*lexicalGlobalObject, argument2.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

RELEASE_AND_RETURN(throwScope, JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.set(WTFMove(name), WTFMove(blobValue), WTFMove(filename)); })));
}
Expand Down
63 changes: 63 additions & 0 deletions test/js/bun/http/form-data-set-append.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test } from "bun:test";

// https://github.com/oven-sh/bun/issues/12325

test("formdata set with File works as expected", async () => {
const expected = ["617580375", "text-notes1.txt"];

using server = Bun.serve({
port: 0,
fetch: async req => {
const data = await req.formData();
const chat_id = data.get("chat_id");
const document = data.get("document");
expect(chat_id).toEqual(expected[0]);
expect(document.name).toEqual(expected[1]);
return new Response("");
},
});

async function sendDocument(body) {
const response = await fetch(server.url, {
method: "POST",
body: body,
});
const text = await response.text();
return text;
}

const formDataSet = new FormData();
formDataSet.set("chat_id", expected[0]);
formDataSet.set("document", new File(["some text notes 1"], expected[1]));
await sendDocument(formDataSet);
});

test("formdata apppend with File works as expected", async () => {
const expected = ["617580376", "text-notes2.txt"];

using server = Bun.serve({
port: 0,
fetch: async req => {
const data = await req.formData();
const chat_id = data.get("chat_id");
const document = data.get("document");
expect(chat_id).toEqual(expected[0]);
expect(document.name).toEqual(expected[1]);
return new Response("");
},
});

async function sendDocument(body) {
const response = await fetch(server.url, {
method: "POST",
body: body,
});
const text = await response.text();
return text;
}

const formDataSet = new FormData();
formDataSet.append("chat_id", expected[0]);
formDataSet.append("document", new File(["some text notes 2"], expected[1]));
await sendDocument(formDataSet);
});

0 comments on commit 050a4b5

Please sign in to comment.