Skip to content

Commit

Permalink
fix: formdata cleanup (nodejs#1336)
Browse files Browse the repository at this point in the history
* fix: formdata cleanup

* Update lib/core/request.js
  • Loading branch information
ronag authored and metcoder95 committed Dec 26, 2022
1 parent 7561d7c commit 1485b25
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
24 changes: 1 addition & 23 deletions lib/client.js
Expand Up @@ -67,12 +67,6 @@ const {
const kClosedResolve = Symbol('kClosedResolve')

const channels = {}
const [nodeMajor, nodeMinor] = process.version
.slice(1) // remove 'v'
.split('.', 2)
.map(v => Number(v))

let extractBody

try {
const diagnosticsChannel = require('diagnostics_channel')
Expand Down Expand Up @@ -1586,26 +1580,10 @@ async function writeIterable ({ body, client, request, socket, contentLength, he
.on('close', onDrain)
.on('drain', onDrain)

let bodyAsyncIterable = body

if (util.isFormDataLike(body)) {
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 5)) {
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.5 and newer.')
}

if (!extractBody) {
extractBody = require('./fetch/body.js').extractBody
}

const [bodyStream, contentType] = extractBody(body)
header += `content-type: ${contentType}\r\n`
bodyAsyncIterable = bodyStream.stream
}

const writer = new AsyncWriter({ socket, request, contentLength, client, expectsPayload, header })
try {
// It's up to the user to somehow abort the async iterable.
for await (const chunk of bodyAsyncIterable) {
for await (const chunk of body) {
if (socket[kError]) {
throw socket[kError]
}
Expand Down
26 changes: 24 additions & 2 deletions lib/core/request.js
Expand Up @@ -11,6 +11,13 @@ const kHandler = Symbol('handler')

const channels = {}

let extractBody

const [nodeMajor, nodeMinor] = process.version
.slice(1) // remove 'v'
.split('.', 2)
.map(v => Number(v))

try {
const diagnosticsChannel = require('diagnostics_channel')
channels.create = diagnosticsChannel.channel('undici:request:create')
Expand Down Expand Up @@ -79,7 +86,7 @@ class Request {
this.body = body.byteLength ? body : null
} else if (typeof body === 'string') {
this.body = body.length ? Buffer.from(body) : null
} else if (util.isIterable(body) || util.isBlobLike(body)) {
} else if (util.isFormDataLike(body) || util.isIterable(body) || util.isBlobLike(body)) {
this.body = body
} else {
throw new InvalidArgumentError('body must be a string, a Buffer, a Readable stream, an iterable, or an async iterable')
Expand Down Expand Up @@ -126,7 +133,22 @@ class Request {
throw new InvalidArgumentError('headers must be an object or an array')
}

if (util.isBlobLike(body) && this.contentType == null && body.type) {
if (util.isFormDataLike(this.body)) {
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 5)) {
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.5 and newer.')
}

if (!extractBody) {
extractBody = require('../fetch/body.js').extractBody
}

const [bodyStream, contentType] = extractBody(body)
if (this.contentType == null) {
this.contentType = contentType
this.headers += `content-type: ${contentType}\r\n`
}
this.body = bodyStream.stream
} else if (util.isBlobLike(body) && this.contentType == null && body.type) {
this.contentType = body.type
this.headers += `content-type: ${body.type}\r\n`
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/body.js
Expand Up @@ -71,7 +71,7 @@ function extractBody (object, keepalive = false) {

// Set source to a copy of the bytes held by object.
source = new Uint8Array(object)
} else if (object instanceof FormData) {
} else if (util.isFormDataLike(object)) {
const boundary = '----formdata-undici-' + Math.random()
const prefix = `--${boundary}\r\nContent-Disposition: form-data`

Expand Down

0 comments on commit 1485b25

Please sign in to comment.