Returning response body as an async-iterable or a readable stream
See original GitHub issueApart from normal data types (string, number, object, etc), Deno.Reader
is the only other data type that is supported as a response body. It would be good if oak
also supported async-iterable/Readable streams as a response body since they can be easily generated and composed using generators.
function getUsers() {
return knex('users').stream()
}
async function* reports() {
for await (const user of getUsers()) {
yield expensiveOperation(user.id)
}
}
async function* json2csv(iterable) {
for await (const obj of iterable) {
yield Object.values(obj).join(',')
}
}
router.get('/reports', (ctx) => {
ctx.response.body = json2csv(reports())
})
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Does the Node.js "request" library support an async-iterable ...
js libraries and I'm trying figure how to use async iteration over an HTTP response stream. My overall goal is to read a...
Read more >Using readable streams - Web APIs | MDN
The Request.body and Response.body properties are available, which are getters exposing the body contents as a readable stream.
Read more >Async iteration for fetch response body · Issue #2520 - GitHub
Currently failing with error TS2504: Type 'Body' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.
Read more >Easier Node.js streams via async iteration - 2ality
At the end, we have several options for handling the async iterable returned by the last generator: We can convert it to a...
Read more >Async iterators, operators and pull based streams. - Medium
This method must return an asyncIterator (see below). ... Note: the fromReadable function simply converts any Nodejs Readable Stream into an async iterable....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ducaale there are a few challenges with the approach… ultimately it needs to be like
BufReader
in the sense that the reader can be required to span a iterator cycle of some number of reads, depending on the size of the.byteLength
of the read buffer passed in.I have a patch about half done that will fix this and allow the oak
response.body
to accept async iterables.I have tested the
AsyncIterReader
class I had in Deno’shttp/server
and it seems responses are being streamed as I was expecting.Edit:
I suspect this line is what preventing from the data to be streamed.