Read from KV as type Stream not Arraybuffer to avoid resource limits.
Closed this issue · 2 comments
i-infra commented
eg with key=01FDJX6D44NZDH67B2GHFZ5WK1&raw.
https://developers.cloudflare.com/workers/runtime-apis/kv#types
https://developers.cloudflare.com/workers/learning/using-streams
addEventListener("fetch", event => {
event.respondWith(fetchAndStream(event.request))
})
async function fetchAndStream(request) {
// Fetch from origin server.
let response = await fetch(request)
// Create an identity TransformStream (a.k.a. a pipe).
// The readable side will become our new response body.
let { readable, writable } = new TransformStream()
// Start pumping the body. NOTE: No await!
response.body.pipeTo(writable)
// ... and deliver our Response while that’s running.
return new Response(readable, response)
}
i-infra commented
i-infra commented
Closing for now... Stream tradeoffs can be avoided with better filetype detection.