-
-
Notifications
You must be signed in to change notification settings - Fork 663
doc deprecate bodymixin.formData #2892
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
Conversation
Since @types/node relies on undici-types, people will notice that .formData() is "deprecated" if they use typescript. I feel very strongly that we should prevent people from using it, even if we did everything correctly, because it's an API that is totally unsuitable for any use case I can think of. Referring to |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2892 +/- ##
=======================================
Coverage 93.81% 93.81%
=======================================
Files 85 85
Lines 23410 23410
=======================================
Hits 21961 21961
Misses 1449 1449 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
(unfortunately)
hi @KhafraDev I'm one of those devs and I've tried to find a good explanation of why it's deprecated but came up empty handed. I admit the form-data API isn't exactly the friendliest, but it feels like overkill to mark it as deprecated. I'm no node-js expert, so I'm assuming there's some good arguments here, I just can't seem to find any. All examples I could find revolve around JSON compatible APIs, but I'm sending a registration form (name, email & avatar).
Using a formPost seemed like the obvious choice for the frontend. The backend can "simply" do the Do you happen to have a resource handy? I'd really like to understand the reasoning behind this before I go ahead and add a library to a web worker. |
It wasn't deprecated for semantics, but because of how it's implemented. It's implemented the same in every other environment FWIW: the body is buffered in memory and then the "form data" body is parsed. The biggest issue therefore, and the reason for the deprecation, is that multipart/form-data is oftentimes used to send large files, which leads to potential DOSing on the backend. Semantically it sucks too, but it wasn't a contributing factor in why it was deprecated. It's called "formData" but parses both multipart/form-data and x-www-form-urlencoded bodies. You cannot limit the body size nor do you have any control over how the body is parsed (busboy and @fastify/busboy both give fine grained control). The spec does not include steps to parse form data bodies, which means most implementations have quirks that do/don't work in other environments. My recommendation is to use either busboy or @fastify/busboy (0 deps) and pipe a Response's body to it. import { Readable } from 'node:stream'
const busboy = ...
const response = new Response(...)
if (response.body !== null) {
Readable.fromWeb(response.body).pipe(busboy)
} There are other libraries such as https://github.com/mjackson/remix-the-web/tree/main/packages/multipart-parser but I have never used it. |
@KhafraDev thank you. You have been my "TIL" moment! Going to switch to |
Considering that mitigating DoS from large payloads is a server-side responsibility and the API is meant for simple convenience rather than fine-grained control, deprecating .formData() doesn't seem to be the right course of action. |
also some misc. doc updates/improvements