@@ -105,6 +105,8 @@ const {
105105// Experimental
106106let h2ExperimentalWarned = false
107107
108+ let extractBody
109+
108110const FastBuffer = Buffer [ Symbol . species ]
109111
110112const kClosedResolve = Symbol ( 'kClosedResolve' )
@@ -1477,7 +1479,9 @@ function write (client, request) {
14771479 return
14781480 }
14791481
1480- const { body, method, path, host, upgrade, headers, blocking, reset } = request
1482+ const { method, path, host, upgrade, blocking, reset } = request
1483+
1484+ let { body, headers, contentLength } = request
14811485
14821486 // https://tools.ietf.org/html/rfc7231#section-4.3.1
14831487 // https://tools.ietf.org/html/rfc7231#section-4.3.2
@@ -1494,14 +1498,34 @@ function write (client, request) {
14941498 method === 'PATCH'
14951499 )
14961500
1501+ if ( util . isFormDataLike ( body ) ) {
1502+ if ( ! extractBody ) {
1503+ extractBody = require ( './fetch/body.js' ) . extractBody
1504+ }
1505+
1506+ const [ bodyStream , contentType ] = extractBody ( body )
1507+ if ( request . contentType == null ) {
1508+ headers += `content-type: ${ contentType } \r\n`
1509+ }
1510+ body = bodyStream . stream
1511+ contentLength = bodyStream . length
1512+ } else if ( util . isBlobLike ( body ) && request . contentType == null && body . type ) {
1513+ headers += `content-type: ${ body . type } \r\n`
1514+ }
1515+
14971516 if ( body && typeof body . read === 'function' ) {
14981517 // Try to read EOF in order to get length.
14991518 body . read ( 0 )
15001519 }
15011520
15021521 const bodyLength = util . bodyLength ( body )
15031522
1504- let contentLength = bodyLength
1523+ if ( util . isStream ( body ) && util . isDestroyed ( body ) ) {
1524+ errorRequest ( client , request , body . errored ?? new RequestAbortedError ( ) )
1525+ return false
1526+ }
1527+
1528+ contentLength = bodyLength ?? contentLength
15051529
15061530 if ( contentLength === null ) {
15071531 contentLength = request . contentLength
@@ -1544,6 +1568,7 @@ function write (client, request) {
15441568 }
15451569
15461570 if ( request . aborted ) {
1571+ util . destroy ( body )
15471572 return false
15481573 }
15491574
0 commit comments