You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently response.body= or ctx.body= support buffers, strings and Nodejs streams. I suggest we also add support to Web stream, Blob and Response (currently just fallback to json which break users expectation), maybe also all async iterables, Nodejs already have those APIs in global.
This is my workaround, it works but it's better to support them in the core.
app.use(async(ctx,next)=>{awaitnext();// Note: need to clear type before set body, or the type might always be json// See Koa impl: https://github.com/koajs/koa/blob/dbf4b8f41286befd53dfd802740f2021441435bf/lib/response.js#L134-L190if(ctx.bodyinstanceofReadableStream){ctx.type="";ctx.body=Readable.fromWeb(ctx.body);}elseif(ctx.bodyinstanceofBlob){constblob=ctx.body;ctx.type=blob.type;ctx.length=blob.size;ctx.body=Readable.fromWeb(blob.stream());}elseif(ctx.bodyinstanceofResponse){constresponse=ctx.body;ctx.status=response.status;ctx.type="";// Note: don't clear all headers so the headers added by middlewares still effectivefor(const[name,value]ofresponse.headers)ctx.set(name,value);if(response.redirected)ctx.redirect(response.url);elsectx.body=Readable.fromWeb(response.body);}// maybe we could also support all async iterables// else if (ctx.body[Symbol.asyncIterator]) {// ctx.type = ""; ctx.body = Readable.from(ctx.body);// }});
Checklist
I have searched through GitHub issues for similar issues.
I have completely read through the README and documentation.
The text was updated successfully, but these errors were encountered:
Describe the feature
Currently
response.body=
orctx.body=
support buffers, strings and Nodejs streams. I suggest we also add support to Web stream, Blob and Response (currently just fallback to json which break users expectation), maybe also all async iterables, Nodejs already have those APIs in global.This is my workaround, it works but it's better to support them in the core.
Checklist
The text was updated successfully, but these errors were encountered: