When working with github/fetch polyfill and building a test i found a tiny issue:
rs = new ReadableStream({})
res = new Response(rs)
res.body.getReader()
console.log(res.bodyUsed) // false
try {
res.clone() // throws Failed to execute 'clone' on 'Response': Response body is already used(…)
} catch (err) {
console.log(res.bodyUsed) // still false
}
Think the bodyUsed flag should be set to true when you call getReader() ?
It's not until you call read() as the flag bodyUsed gets set to false
rs = new ReadableStream({})
res = new Response(rs)
reader = res.body.getReader()
reader.read()
console.log(res.bodyUsed) // true
// body used (don't call clone())
Ended up doing something like this:
// this == instance of Response
Object.defineProperty(this, 'bodyUsed', {
enumerable: true,
get: () => stream.locked
})
It sounds like this is a bug report on the wrong repo. The polyfill repo is github/fetch, not whatwg/fetch.
Nah, don't think so. maybe should be reported to chromium instead
Think chrome is doing something wrong.
test this part in the console:
rs = new ReadableStream({})
res = new Response(rs)
res.body.getReader()
console.log(res.bodyUsed) // false
res.clone()
Why would you think that should be anything other than false?
Don't know... when i clone the response after i called getReader it throws an error saying i can't clone a response that has already been used while the flag still says it's not used
Ah, yeah, sounds like a Chrome bug with the error message then. It should be saying that you cannot clone it because the stream is locked to a reader
Ok, can throw a ticket to chromium issue. Is it true that bodyUsed should be set to true when you call getReader?
No, it should be false. Everything is behaving correctly in your example, but I guess the error message is not correct.
That is confusing... I think bodyUsed should be set to true when you call getReader...
rs = new ReadableStream()
res = new Response(rs)
res.body.getReader()
console.log(res.bodyUsed) // false
res.text() // TypeError: Already read ..... means body are used?
That's not the spec.
The error should not say "already read ... means body are used". It should say "The body stream is currently locked to a reader; release the lock if you want to read it in some other way."
Yeah, the body isn't used because the stream can be unlocked and .text() will work fine. Demo: http://jsbin.com/heqocus/edit?js,console
Although the above demo works, I'm getting an odd error. Filed https://bugs.chromium.org/p/chromium/issues/detail?id=654701
Also agree that Chrome is showing the wrong error in @jimmywarting's example. Filed https://bugs.chromium.org/p/chromium/issues/detail?id=654703