-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Speed up FormData multipart serialization #31379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jarred-Sumner
merged 4 commits into
main
from
farm/8abb3918/formdata-serialization-perf
May 25, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
469993f
Speed up FormData multipart serialization
robobun 4ab58f9
Add test that serializing FormData does not copy in-memory blob bytes
robobun 3e8362a
test: assert child exit code after output checks in blob-copy test
robobun 75a3ba0
test: surface child stderr via assertion when the blob-copy child fails
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
test/js/web/html/FormData-multipart-serialization.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| // Serializing a FormData body (`new Response(formData)` / `new Request(..., { body: formData })`) | ||
| // goes through Blob::from_dom_form_data, which joins the multipart parts out of | ||
| // borrowed views of the FormData's strings and blob bytes. These tests lock in | ||
| // the exact wire format (boundary shape, part layout, name/filename escaping, | ||
| // content-type fallback) and verify the serialize → parse round-trip, including | ||
| // non-ASCII strings and large binary payloads. | ||
| describe("multipart serialization (new Response(formData))", () => { | ||
| test("serializes string fields, blobs, unicode and escaped names exactly", async () => { | ||
| const formData = new FormData(); | ||
| formData.append("simple", "value"); | ||
| formData.append("empty", ""); | ||
| formData.append("unicode name ☺", "ünïcode välue 😊"); | ||
| formData.append('quote"name', "v1"); | ||
| formData.append("crlf\r\nname", "v2"); | ||
| formData.append("untyped-blob", new Blob(["blob-bytes"])); | ||
| formData.append("named-blob", new Blob(["named-bytes"]), "file.bin"); | ||
| formData.append("typed-file", new File(["<p>hi</p>"], 'weird"file\r\nname.html', { type: "text/html" })); | ||
|
|
||
| const response = new Response(formData); | ||
| const contentType = response.headers.get("Content-Type")!; | ||
| expect(contentType).toMatch(/^multipart\/form-data; boundary=----WebKitFormBoundary[0-9a-f]{32}$/); | ||
| const boundary = contentType.slice(contentType.indexOf("boundary=") + "boundary=".length); | ||
|
|
||
| const text = await response.text(); | ||
| expect(text).toBe( | ||
| [ | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="simple"\r\n\r\nvalue\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="empty"\r\n\r\n\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="unicode name ☺"\r\n\r\nünïcode välue 😊\r\n`, | ||
| `--${boundary}\r\n`, | ||
| // '"', CR and LF in names/filenames are percent-encoded so they can't | ||
| // break out of the quoted-string or inject part headers. | ||
| `Content-Disposition: form-data; name="quote%22name"\r\n\r\nv1\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="crlf%0D%0Aname"\r\n\r\nv2\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="untyped-blob"; filename=""\r\n`, | ||
| `Content-Type: application/octet-stream\r\n\r\nblob-bytes\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="named-blob"; filename="file.bin"\r\n`, | ||
| `Content-Type: application/octet-stream\r\n\r\nnamed-bytes\r\n`, | ||
| `--${boundary}\r\n`, | ||
| `Content-Disposition: form-data; name="typed-file"; filename="weird%22file%0D%0Aname.html"\r\n`, | ||
| `Content-Type: text/html;charset=utf-8\r\n\r\n<p>hi</p>\r\n`, | ||
| `--${boundary}--\r\n`, | ||
| ].join(""), | ||
| ); | ||
| }); | ||
|
|
||
| test("round-trips every entry kind through Response.formData()", async () => { | ||
| const formData = new FormData(); | ||
| formData.append("simple", "value"); | ||
| formData.append("dup", "first"); | ||
| formData.append("dup", "second"); | ||
| formData.append("unicode name ☺", "ünïcode välue 😊"); | ||
| formData.append("blob", new Blob(["blob-bytes"])); | ||
| formData.append("file", new File(["<p>hi</p>"], "日本語ファイル名.html", { type: "text/html" })); | ||
|
|
||
| const parsed = await new Response(formData).formData(); | ||
| const entries = await Promise.all( | ||
| [...parsed.entries()].map(async ([name, value]) => | ||
| value instanceof Blob | ||
| ? [ | ||
| name, | ||
| { file: value instanceof File, name: (value as File).name, type: value.type, text: await value.text() }, | ||
| ] | ||
| : [name, value], | ||
| ), | ||
| ); | ||
|
|
||
| expect(entries).toEqual([ | ||
| ["simple", "value"], | ||
| ["dup", "first"], | ||
| ["dup", "second"], | ||
| ["unicode name ☺", "ünïcode välue 😊"], | ||
| ["blob", { file: true, name: undefined, type: "", text: "blob-bytes" }], | ||
| ["file", { file: true, name: "日本語ファイル名.html", type: "text/html;charset=utf-8", text: "<p>hi</p>" }], | ||
| ]); | ||
| }); | ||
|
|
||
| test("round-trips large binary blob contents intact", async () => { | ||
| const bytes = new Uint8Array(64 * 1024); | ||
| for (let i = 0; i < bytes.length; i++) { | ||
| bytes[i] = (i * 31 + 7) & 0xff; | ||
| } | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append("payload", new Blob([bytes]), "payload.bin"); | ||
| formData.append("after", "still-parses"); | ||
|
|
||
| const parsed = await new Response(formData).formData(); | ||
| const payload = parsed.get("payload") as File; | ||
| expect(payload.size).toBe(bytes.length); | ||
| expect(new Uint8Array(await payload.arrayBuffer())).toEqual(bytes); | ||
| expect(parsed.get("after")).toBe("still-parses"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.