Skip to content

Commit

Permalink
fix(body): also inline constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Aug 16, 2019
1 parent 84a1de5 commit fe75247
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function fetch (resource, init) {
}

if (request[1].body) {
request[1].body = (new Body(init.body)).string()
request[1].body = parseBody(init.body).toString()
}

// TODO credentials
Expand Down Expand Up @@ -65,7 +65,7 @@ class Request extends _fetch.Request {
if (init) {
init = { ...init }
if (init.body) {
init.body = (new Body(init.body)).buffer()
init.body = parseBody(init.body)
}
}

Expand Down Expand Up @@ -152,6 +152,30 @@ function defineBuffer (body, buffer) {
})
}

function parseBody (body) {
// body is undefined or null
if (body == null) {
return null

// body is Buffer
} else if (Buffer.isBuffer(body)) {
return body

// body is ArrayBuffer
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
return Buffer.from(body)

// body is ArrayBufferView
} else if (ArrayBuffer.isView(body)) {
return Buffer.from(body.buffer, body.byteOffset, body.byteLength)

// none of the above
// coerce to string then buffer
} else {
return Buffer.from(String(body))
}
}

fetch.Headers = _fetch.Headers
fetch.FetchError = _fetch.FetchError
fetch.Request = Request
Expand Down

0 comments on commit fe75247

Please sign in to comment.