[streams] Add `.bytesRead` to ReadableByteStream.*Reader (#367)

Putting together @domenic's `reader.bytesRead` idea to address https://github.com/yutakahirano/fetch-with-streams/issues/37:
- Fail `cache.put()` when the stream has been partially/fully drained https://github.com/yutakahirano/fetch-with-streams/issues/37#issuecomment-110109499
- Content-Length https://github.com/yutakahirano/fetch-with-streams/issues/37#issuecomment-110945488

Request/Response constructor saves the current value of `.bytesRead` https://github.com/whatwg/streams/issues/362#issuecomment-112984341

```
const rbs = ...;  // BodyInit as-is, or created from ArrayBuffer, etc.
this._bodySize = ...;  // undefined for RBS. The length of BodyInit for the others
const reader = rbs.getReader();
this._bytesReadAtConstruction = reader.bytesRead;
reader.releaseLock();
```

`cache.put()`, etc. checks if `req._bytesReadAtConstruction` has been changed or not.

```
const reader = req.body.getReader();
if (reader.bytesRead !== this._bytesReadAtConstruction) {
  throw new TypeError();
}
// Proceed with consuming body
```

`fetch()` computes the number of bytes remaining to generate the up-to-date value to be used for the Content-Length header.

```
const reader = req.body.getReader();
if (req._bodySize !== undefined) {
  const currentSize = req._bodySize - reader.bytesRead;
  // Generate Content-Length header
}
```


---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/367

Received on Thursday, 18 June 2015 05:24:13 UTC