StreamAPI requires properties to determine wether stream is alive, closed or disturbed

Proposal

  1. Add methods or properties to determine wether Stream instance is readable or writable.
  2. Add method or property to determine what amount of data has already written to/read from the stream.
  3. Add method or property to determine if Stream is canceled.

Explanation

Today ReadableStream and WritableStream classes have no properties or methods to determine are they alive, closed or disturbed on JS side. I found some opinions of core members that this behavior is unnecessary. In the same time it is using by browser itself for example in Response constructor. And I don’t see any reasonable explanation for this .

Such behavior makes impossible to implement some APIs which works with streams. Because a lack of this flags makes code unreliable and unpredictable.

I’m working on a web server compatible with WebAPI and it’s near to impossible to create Response object which could receive ReadableStream as constructor argument and to throw an exception if it was disturbed.

There is one hack which can be used to check if stream was disturbed by using Response’s constructor:

var stream = new ReadableStream()
stream.cancel()

try {
  new Response(stream);
} catch (err) {
 // Is disturbed
}

But I think it should be fixed in specification.

I think it might be best to take this one directly to the WHATWG’s repository. @domenic, agree? Or do you want to discuss here?

When a ReadableStream is read there are value and done. There is also ReadableStreamDefaultReader.closed property which is a Promise. What methods are you proposing?

There should be boolean flags ‘disturbed’, ‘opened’ (due to ‘closed’ is taken), not a promises. Because you couldn’t use promises in constructors. Requirement of such flags is proven by Response implementation, which relies on them.

Also, If you rely on value and done, then you should to disturb the stream and thus couldn’t use it in Response. Property closed is blocking execution flow and could create infinite Promise if the stream never closes.