feat: add support for streaming cache responses #3580
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
#1894
β Type of change
π Description
This is one possible way to support binary responses in
cachedEventHandler. Whenstream: trueis set on the cache options, the response body will not be read at all and will instead be teed with one stream sent tosetItemRaw()and the other returned as the response.This can be useful for text responses as well when proxying (such as large JSON data) because the response does not need to be read into memory. It can be streamed directly to the client while writing to the cache does not block the response.
When using the
stream: trueoption it will require special handling of the stream in thesetItemRawmethod of any storage adapter. So I think this is an advanced use-case and doesn't automatically fix #1894.I experimented with another fix that would use
res.arrayBuffer()and convert it to anUint8Arrayso the response could still be serialized as JSON. But for my use-case (tar files that were potentially 100s of MB) it was too slow. I'd be happy to amend this PR to support that as well.π Checklist