Comments (6)
ReadableFromIter
consumes the upstream async iterable only if you pull from the ReadableStream it makes. Before that, it's a chain of async generators. So if you feed downloadZip
from a lazy generator, it should never trigger more than one download ahead of its output (or a few downloads if you properly optimize the source for high-latency small files as implied in your other issue).
It's not perfect, because all the generators in the chain will be left hanging and it won't cancel a fetch
that's already started.
Your suggestion (handling cancel
on the output stream) solves the first problem (cleanly ending all the async generators).
However, to solve the second problem (aborting a fetch
), we'd need an AbortSignal
. When you call fetch
, you can give it an optional abort signal (should be the same signal for all the requests feeding one zip). If you also give that signal's controller to downloadZip
, then, upon cancellation of the output stream, we can trigger the signal.
Both are easy to implement.
from client-zip.
Even if we don't have an AbortSignal
handled by the library, it's easy enough to wrap a fetch
in a stream and handle the abort signal in the stream's cancel
callback, so I'd definitely start there.
I'd be happy to put in a PR for this when I get some time. Will be a few days / weeks as this isn't the highest priority issue for me right now but I will get back to it when I'm able to!
from client-zip.
If your source starts multiple downloads (because you want to buffer a few Responses ahead, as discussed elsewhere) but the output stream is cancelled, then the read stream for the file currently being processed would be cancelled.
However, the streams for the other Responses in the source haven't been consumed by downloadZip
yet, and will never be. downloadZip
shouldn't greedily consume all the source just to call cancel
on each stream, that could easily backfire (triggering more requests that necessary, for example).
So the request cancellation needs to be handled at the source. If the source is a generator or a stream, you can detect that it's been cancelled itself and trigger the AbortSignal (which is better than cancelling Response streams, because it will also cancel requests that don't have a resolved Response yet).
So, in fact, you shouldn't even need to give your signal to downloadZip
. If downloadZip
propagates output stream cancellation all the way to its source, the source can do whatever needs to be done to clean up any pending task.
To sum it up, I will definitely propagate output cancellation to the input stream. Anything more than that, once again, "isn't my problem".
from client-zip.
Output stream cancellation now throws an error upstream (versions 1.6.1 and 2.4.1).
from client-zip.
Sorry, I underestimated the work necessary to propagate errors through async generators. I'm reopening this issue until the fix is published (it's already working in my test environment, but I had to do something that seems too complicated and I'm looking for a simpler solution).
from client-zip.
Fixed for real this time !
from client-zip.
Related Issues (20)
- [BUG] Type error with string as input in downloadZip HOT 1
- [FEATURE] Store paths as well as names HOT 2
- [FEATURE] Specify attachment filename HOT 8
- [BUG] Breaks react-native build with obtuse error HOT 1
- [BUG] Firefox downloads ends up being corrupt most of the time HOT 16
- [BUG] downloadZip is not a function when running the build script HOT 10
- [FEATURE] Version without WebAssembly HOT 6
- [BUG] downloadZip creating corrupted zips in demo HOT 2
- [BUG] Non-ASCII characters in file names come out as garbage in Windows Explorer HOT 3
- [BUG] testing with string input leads to uncompressed zip files HOT 4
- [FEATURE] Support parallel downloading HOT 10
- [BUG] Microsoft Office can not read generated ZIP containing ODS HOT 2
- [QUESTION] Lazyfetch array with objects instead of urls HOT 1
- generated xlsx file but office show error when open it HOT 2
- [QUESTION] How to handle Download cancellations with Service Worker HOT 1
- [FEATURE] Generate ranged responses with `downloadZip` HOT 6
- [BUG] You may need an appropriate loader to handle this file type HOT 5
- [HelpWanted] Download progress HOT 15
- [BUG] Blob is not defined error in SvelteKit project HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from client-zip.