Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy Markdown
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)) {

const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`
const prefix = `--${boundary}\r\nContent-Disposition: form-data`

Expand Down
9 changes: 8 additions & 1 deletion lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy Markdown
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))

// This is a compatibility fix to support the FormData instance that is shipped in Node.js
if (globalThis.FormData && V instanceof globalThis.FormData) {
return true
}
return _isFormData(V)
}

module.exports = { FormData, makeEntry, setFormDataState }
22 changes: 22 additions & 0 deletions test/fetch/client-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,25 @@ test('Receiving non-Latin1 headers', async (t) => {
assert.deepStrictEqual(cdHeaders, ContentDisposition)
assert.deepStrictEqual(lengths, [30, 34, 94, 104, 90])
})

test('post globalThis.FormData with Blob', (t, done) => {
const { ok } = tspl(t, { plan: 1 })

// We use the FormData that is shipped in Node.js
const body = new globalThis.FormData()
body.append('field1', new Blob(['asd1']))

const server = createServer((req, res) => {
req.pipe(res)
})
t.after(closeServerAsPromise(server))

server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`, {
method: 'PUT',
body
})
ok(/asd1/.test(await res.text()))
done()
})
})