-
Notifications
You must be signed in to change notification settings - Fork 168
Description
Proposal
Implement flags to determine in a synchronous way wether the stream is disturbed and cancelled as boolean properties ReadableStream#disturbed and ReadableStream#closed.
Rationale
ReadableStream should have a flag which describes if the stream is disturbed or not. Without this flag it's just impossible to understand if the first chunk of the stream would be the first chunk of the data. That's why behaviors to check is stream has been read already is implemented in all major browsers (Chromium, Safari, Firefox) and is using by FetchAPI (Response and Request constructors).
I know there is an opinion against it. But, as I've already showed, without such flags it's impossible to implement reliable solution. Current design creates asynchronous undefined state and doesn't allow to understand wether the stream is correct constructor argument or not and lead to unpredictable delayed race conditions. As I understand such a solution made due to assumption that stream can be non-exclusively owned and as a result cancelled in any time, what seems incorrect.
Example
I'm implementing a server which works with Web StreamAPI. It requires an ability to check, if the stream contains acceptable body or not, to prevent throwing parsing errors on valid request and to detect race condition occurred in the process of request handling. Currently I solve this in a hacky way using lowlevel API from the Response object implementation, without this I will emit error each time someone else read the stream without being sure where in the code it has happened.
Solution with Response constructor:
function isStreamDisturbed(stream: ReadableStream): Boolean {
try {
new Response(stream)
return false
}
catch {
return true
}
}
const stream = new ReadableStream()
stream.cancel()
isStreamDisturbed(stream) // -> trueOf cause this solution is a hack and couldn't be decided as reliable.