I did some testing and I have some problems with streaming a file meant to be downloaded (with Content-Disposition header set to attachment in the response). The following behaviour is common to both Firefox et Chrome and I'll call 'browser' the entity that executes code that is not code written by the user.
The stream starts being consumed straight away when the response is sent back with
event.respondWith()
, meaning that while the popup appears to ask the user if he wants and where to download the file or if he simply wants to refuse the download, the stream is being consumed and pulling data non-stop. This seems like a crazy behavior, is it intended, or a bug in both browsers?If the user refuses the download or accept and cancel it later, the browser just stops consuming the stream and that's it. It never calls
cancel()
on itsReadableStreamDefaultReader
instance, nor does it callreleaseLock()
. So the stream just pull data from the underlying source until the queue is full and wait there without knowing anything. How are we supposed to deal with it?
From the Streams specification (https://streams.spec.whatwg.org/#rs-cancel):
The cancel method cancels the stream, signaling a loss of interest in the stream by a consumer. The supplied reason argument will be given to the underlying source’s cancel() method, which might or might not use it.
So I think the browser should call cancel on its ReadableStreamDefaultReader
instance but neither Firefox nor Chrome does.
- Moreover, if I call the error method on the stream's underlying
ReadableStreamDefaultController
instance, the lock is still never released by the browser.