Skip to content

Commit

Permalink
fix(body): do not consume body on clone
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Aug 28, 2019
1 parent dd0b3c2 commit 52dd003
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,27 @@ class Body {

arrayBuffer () {
checkBody(this)
const buf = this[_body]
const buf = consumeBody(this)
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
}

text () {
checkBody(this)
return this[_body].toString()
return consumeBody(this).toString()
}

json () {
checkBody(this)
try {
return JSON.parse(this[_body].toString())
return JSON.parse(consumeBody(this).toString())
} catch (err) {
throw new fetch.FetchError(`invalid json response body at ${this.url} reason: ${err.message}`, 'invalid-json')
}
}

buffer () {
checkBody(this)
return Buffer.from(this[_body])
return Buffer.from(consumeBody(this))
}
}

Expand All @@ -165,8 +165,11 @@ function checkBody (body) {
if (body.bodyUsed) {
throw new TypeError(`body used already for: ${body.url}`)
}
}

function consumeBody (body) {
_super(body, 'buffer')()
return body[_body]
}

function deserializeError (name, init) {
Expand Down

0 comments on commit 52dd003

Please sign in to comment.