Re: [whatwg/streams] Various fixes for readable byte streams (#1123)

I noticed [this TODO comment](https://github.com/whatwg/streams/blob/cc29ed3921eceaae700fa9a36cb74c9d8ede0236/reference-implementation/lib/abstract-ops/readable-streams.js#L1244) in `ReadableByteStreamControllerRespondInReadableState`:
```javascript
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
    // TODO: Figure out whether we should detach the buffer or not here.
    return;
  }
```
This seems relevant, so let's discuss it.

I think that after you successfully called `controller.byobRequest.respond()` or `.respondWithNewView()`, the BYOB request should be invalidated and its buffer should be transferred. You should no longer be able to modify the old value of `controller.byobRequest.view`:
```javascript
reader.read(new Uint32Array(1));

const byobRequest1 = controller.byobRequest;
const view1 = byobRequest1.view; // 4-byte view on a 4-byte buffer
view1[0] = 0x01;
byobRequest1.respond(1);

// At this point, `byobRequest1` should invalidated and `view1.buffer` should be detached.
view1.buffer.byteLength === 0;
view1[0] = 42; // should not work
```

Instead, you should always request a new `controller.byobRequest` after calling `respond()` and fill the new view on the remaining part of the pull-into descriptor:
```javascript
const byobRequest2 = controller.byobRequest;
const view2 = byobRequest2.view; // 3-byte view on a 4-byte buffer
view2[0] = 0x02;
view2[1] = 0x03;
view2[2] = 0x04;
byobRequest2.respond(3);

// read() resolves with 0x04030201
```
Thoughts?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/1123#issuecomment-823671467

Received on Tuesday, 20 April 2021 23:46:32 UTC