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

Support globalThis.FormData #3844

Closed
wants to merge 1 commit into from
Closed

Conversation

mcollina
Copy link
Member

Since #3502, we only support our own classes, and that is the right move. However, we need to relax this condition to allow for globalThis.FormData, as that is a different copy of undici.

In essence, this change make the following fork

// import { fetch, FormData } from 'undici'
import { fetch } from 'undici'
import fastify from 'fastify'
import fastifyMultipart from '@fastify/multipart'

const app = fastify()

app.register(fastifyMultipart, {
  attachFieldsToBody: true
})

app.post('/upload', async (req, reply) => {
  console.log('received', req.body.file.filename)
  return 'ok'
})

await app.listen({ port: 3000 })

const body = new FormData()
body.append('file', new Blob(['hello world'], { type: 'text/plain' }), 'hello.txt')

const res = await fetch('http://localhost:3000/upload', {
  method: 'POST',
  body
})

console.log(res.status)
console.log(await res.text())

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina requested review from KhafraDev and tsctx November 19, 2024 09:59
@mcollina mcollina marked this pull request as ready for review November 19, 2024 09:59
@@ -109,7 +109,7 @@ function extractBody (object, keepalive = false) {

// Set source to a copy of the bytes held by object.
source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength))
} else if (webidl.is.FormData(object)) {
} else if (webidl.is.FormData(object) || (globalThis.FormData && object instanceof FormData)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if (webidl.is.FormData(object) || (globalThis.FormData && object instanceof FormData)) {
} else if (webidl.is.FormData(object)) {

@@ -258,6 +258,13 @@ function makeEntry (name, value, filename) {
return { name, value }
}

webidl.is.FormData = webidl.util.MakeTypeAssertion(FormData)
const _isFormData = webidl.util.MakeTypeAssertion(FormData)
webidl.is.FormData = function (V) {
Copy link
Member

Choose a reason for hiding this comment

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

How about something like this?

// lib/web/fetch/webidl.js
webidl.util.MakeTypeAssertionMultiple = function (List) {
  return (O) => List.some((I) => FunctionPrototypeSymbolHasInstance(I, O))
}

Usage:

webidl.is.FormData = webidl.util.MakeTypeAssertionMultiple([FormData, gloablThis.FormData].filter(Boolean))

@KhafraDev
Copy link
Member

KhafraDev commented Nov 19, 2024

I don't understand the need for this change, it's a known limitation that different versions of undici create different versions of classes. It's also a pain to deal with.

Won't work:

new Request(new globalThis.Request('http://a'))

await fetch(new globalThis.Request(...))

Works because of webidl:

  • Headers (due to the iterators)
  • Response (since the members of a ResponseInit are getters, it's treated like a plain object)

@mcollina
Copy link
Member Author

Ok, let's close this. We would need to document this a bit better, as I suspect a flurry of issues.

@mcollina mcollina closed this Nov 20, 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.

3 participants