Skip to content

Commit

Permalink
fix: readable.setEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Dec 7, 2021
1 parent ebea0f7 commit 0cdbcc8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ module.exports = class BodyReadable extends Readable {

// https://fetch.spec.whatwg.org/#dom-body-text
async text () {
return consume(this, 'text')
return toUSVString(await consume(this, 'string'))
}

// https://fetch.spec.whatwg.org/#dom-body-json
async json () {
return consume(this, 'json')
return JSON.parse(await consume(this, 'string'))
}

// https://fetch.spec.whatwg.org/#dom-body-blob
Expand Down Expand Up @@ -231,10 +231,12 @@ function consumeEnd (consume) {
const { type, body, resolve, stream, length } = consume

try {
if (type === 'text') {
resolve(toUSVString(Buffer.concat(body)))
} else if (type === 'json') {
resolve(JSON.parse(Buffer.concat(body)))
if (type === 'string') {
if (stream.readableEncoding) {
resolve(body.join(''))
} else {
resolve(Buffer.concat(body).toString())
}
} else if (type === 'arrayBuffer') {
const dst = new Uint8Array(length)

Expand Down

0 comments on commit 0cdbcc8

Please sign in to comment.