Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should ReadableStreamClose(stream) also close pending read requests in case of ReadableStreamBYOBReader #1294

Closed
debadree25 opened this issue Sep 18, 2023 · 2 comments

Comments

@debadree25
Copy link

On reading the spec for https://streams.spec.whatwg.org/#readable-stream-close we see it asks to perform close steps in case of ReadableStreamDefaultReader but what about the case when its a byob reader? this was first noted when trying to implement nodejs/node#47993 a simple example:

async function test() {
  const toPull = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
  const st = new ReadableStream({
    type: 'bytes',
    start(controller) {},

    pull(controller) {
      const chunk = toPull.shift();
      if (chunk === undefined) {
        controller.close();
        return;
      }

      controller.enqueue(new TextEncoder().encode(chunk));
    },
  });

  const reader = st.getReader({ mode: 'byob' });
  const decoder = new TextDecoder();

  console.log('start');
  let result;
  do {
    result = await reader.read(new Uint8Array(100));
    if (result.value !== undefined) console.log(decoder.decode(result.value));
  } while (!result.done);
  console.log('end');
}

await test();

would expect both start and end to be printed to console but both node and chrome give the following output:

start
a
b
c
d
e
f
g
h
i
j

this in contrast to ReadableStreamError() which branches for both byob and normal. Is this behaviour intended? i am unable to understand unfortunately 😅 any help in understanding would be greatly appreciated!

Thank You!

@ricea
Copy link
Collaborator

ricea commented Sep 19, 2023

Yes, this is hard to understand. For a while I thought it was a bug, and then I remembered we did it this way so that the buffer can be returned to the user. I think example 10.5 does it correctly. You need to call controller.byobRequest.respond(0) after controller.close().

@debadree25
Copy link
Author

ahah! understood thanks so much for explaining @ricea probably need to include such an example in the node docs too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants