Skip to content
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

fix(fetch): allow third-party FormData/Blob bodies #1643

Merged
merged 1 commit into from
Sep 11, 2022

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Sep 11, 2022

webidl.converters.XMLHttpRequestBodyInit rejected third-party FormData/Blobs, meaning they were passed through webidl.converters.DOMString. This meant Request/Responses constructed with third-party FormData/Blob bodies ended up with a plain-text [object FormData]/[object Blob] body.

Undici has code for extracting a body from third-party Blobs and FormDatas. This was just being called after Web IDL conversion.

import { Request as UndiciRequest, Response as UndiciResponse, FormData as UndiciFormData } from "undici";
import { FormData as AlternativeFormData } from "@web-std/form-data";

let req = new UndiciRequest("http://a", { method: "POST", body: new UndiciFormData() });
console.log(req.headers.get("Content-Type")); // multipart/form-data; boundary=...
req = new UndiciRequest("http://a", { method: "POST", body: new FormData() }); // From Node
console.log(req.headers.get("Content-Type")); // text/plain;charset=UTF-8
req = new UndiciRequest("http://a", { method: "POST", body: new AlternativeFormData() });
console.log(req.headers.get("Content-Type")); // text/plain;charset=UTF-8

let res = new UndiciResponse(new UndiciFormData());
console.log(res.headers.get("Content-Type")); // multipart/form-data; boundary=...
res = new UndiciResponse(new FormData()); // From Node
console.log(res.headers.get("Content-Type")); // text/plain;charset=UTF-8
res = new UndiciResponse(new AlternativeFormData());
console.log(res.headers.get("Content-Type")); // text/plain;charset=UTF-8

`webidl.converters.XMLHttpRequestBodyInit` rejected third-party
`FormData`/`Blob`s, meaning they were passed through
`webidl.converters.DOMString`. This meant `Request`/`Response`s
constructed with third-party `FormData`/`Blob` bodies ended up with a
plain-text `[object FormData]`/`[object Blob]` body.
@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2022

Codecov Report

Merging #1643 (5c2ca96) into main (0862fba) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1643   +/-   ##
=======================================
  Coverage   94.60%   94.60%           
=======================================
  Files          53       53           
  Lines        4803     4803           
=======================================
  Hits         4544     4544           
  Misses        259      259           
Impacted Files Coverage Δ
lib/fetch/response.js 94.28% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 88eca8d into nodejs:main Sep 11, 2022
@mrbbot mrbbot deleted the fetch-third-party-form-data-blob branch September 11, 2022 19:23
mrbbot added a commit to cloudflare/miniflare that referenced this pull request Dec 23, 2022
`5.11.0` includes nodejs/undici#1643.

There are some non-trivial changes required to upgrade to `5.14.0`:

- Since `undici@5.12.0` (nodejs/undici#1697),
  `structuredClone` is used on bodies, which may be byte streams.
  Due to a Node bug (nodejs/node#45955),
  readable byte streams cannot be transferred, breaking `fetch`.
- Since `undici@5.14.0` (nodejs/undici#1793),
  global `ReadableStream` and `TransformStream` are used if
  available. In the Vitest environment, (which modifies the global
  scope unlike Jest which runs tests in a VM context), if the
  `streams_enable_constructors` compatibility flag isn't enabled,
  `fetch` breaks as `ReadableStream`s can't be constructed.
metcoder95 pushed a commit to metcoder95/undici that referenced this pull request Dec 26, 2022
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants