Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/runtime/webcore/Blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4340,6 +4340,18 @@ pub(crate) extern "C" fn Blob__setAsFile(this: &mut Blob, path_str: &mut BunStri
bytes.stored_name = path_str.to_owned_slice().into_boxed_slice();
}
}
} else if !path_str.is_empty() {
// A zero-byte `Blob` has no store; create an empty `Bytes` store to carry
// the name so `get_file_name()` (and thus `File.prototype.name`) works.
// Same shape as the `new File([], name)` path in `jsdom_file_construct`.
Comment thread
robobun marked this conversation as resolved.
Outdated
this.store.set(Some(StoreRef::from(Store::new(Store {
data: store::Data::Bytes(store::Bytes::init_empty_with_name(
path_str.to_owned_slice().into_boxed_slice(),
)),
ref_count: bun_ptr::ThreadSafeRefCount::init(),
mime_type: bun_http_types::MimeType::NONE,
is_all_ascii: None,
}))));
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/js/web/html/FormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ describe("FormData", () => {
expect(b1.name).toBe("foo.txt");
});

describe("multipart parse of a zero-byte file part", () => {
const headers = { "content-type": "multipart/form-data; boundary=B" };
const part = (body: string) =>
`--B\r\nContent-Disposition: form-data; name="f"; filename="empty.txt"\r\nContent-Type: text/plain\r\n\r\n${body}\r\n--B--\r\n`;

for (const C of [Response, Request] as const) {
it(`preserves filename with ${C.name}`, async () => {
const make = (body: string) =>
C === Response
? new Response(body, { headers })
: new Request("http://x/", { method: "POST", headers, body });

const empty = (await make(part("")).formData()).get("f") as File;
expect(empty).toBeInstanceOf(File);
expect(empty.size).toBe(0);
expect(empty.name).toBe("empty.txt");
expect(await empty.text()).toBe("");

// control: a 1-byte body already worked
const nonEmpty = (await make(part("a")).formData()).get("f") as File;
expect(nonEmpty.name).toBe("empty.txt");
});
}

it("round-trips an empty File through Response.formData()", async () => {
const fd = new FormData();
fd.append("e", new File([], "empty.log", { type: "text/plain" }));

const serialized = await new Response(fd).text();
expect(serialized).toContain('filename="empty.log"');

const parsed = await new Response(fd).formData();
const e = parsed.get("e") as File;
expect(e).toBeInstanceOf(File);
expect(e.size).toBe(0);
expect(e.name).toBe("empty.log");
});
});

const multipartFormDataFixturesRawBody = [
{
name: "simple",
Expand Down
Loading