Re: [ServiceWorker] Backpressure on fetch integrated with Streams (#452)

@domenic  and I talked a bit at https://github.com/yutakahirano/fetch-with-streams/issues/13. Now we should revisit `.bodyUsed` and `.locked`, because they are different concepts.

- The current `.bodyUsed` specified at https://fetch.spec.whatwg.org/ represents whether some functions such as `text()` and `json()` are called. For example, `res.text().then(() => res.text())` will be rejected, because `.bodyUsed` is turned into true at the first `text()` call and it will never become false. Note that `.bodyUsed` becomes true even when no data is read - Think about the case when we call `text()` on a response that has an empty body.
- A stream can be locked. The lock will be released when explicitly indicated, the stream gets closed or the stream gets errored. `text()` acquires a lock, but it releases the lock when the stream is closed or errored.
- There is another concept: whether body data is read. Let me call it `.bodyConsumed` for now. It becomes true when some data is read via ReadableStream.prototype.read. `res.text()` will turn this flag into true when the data is actually read (not when the function is called). Note that `res.text()` on a response with an empty body doesn't make this flag true.

What concept do we need? Surely `.locked` is needed, but it seems insufficient. `.bodyConsumed` is good from some security POV (because it is useful to guarantee that the response is not partially consumed). The problem is `.bodyConsumed` is hard to implement. @domenic's [idea](https://github.com/slightlyoff/ServiceWorker/issues/452#issuecomment-56049602) needs a custom ReadableByteStream for FetchAPI, and a custom ExclusiveStreamReader for Fetch API because the standard ExclusiveStreamReader doesn't care about the customized `read()`.
The existing `.bodyUsed` concept doesn't work with `.body` attribute, because we can freely read from stream even when `.bodyUsed` is true.

How about @tyoshino's [old idea](https://github.com/slightlyoff/ServiceWorker/issues/452#issuecomment-56145980)? It is simple, and we can guarantee that the body is not partially consumed when the flag is true. It is easy to implement and easy to understand. Currently I don't have a better idea.


---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/452#issuecomment-70608528

Received on Tuesday, 20 January 2015 06:01:20 UTC