-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handling Objects for streaming bodies #1405
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1405 +/- ##
==========================================
+ Coverage 99.37% 99.39% +0.02%
==========================================
Files 4 4
Lines 479 499 +20
Branches 128 136 +8
==========================================
+ Hits 476 496 +20
Partials 3 3
Continue to review full report at Codecov.
|
I'm not sure there is a real world example that need this feature, and people can transform object streams to normal streams and used with koa. I'd prefer not implement it in koa core and leave it to community. |
@dead-horse I think the wording transform object streams to normal streams is the key to understand what's wrong with not having object streams in Koa core from the initial Koa architecture point of view. Object mode streams are normal streams, if under normal you mean that they are created by Node built-in The simplicity Let's see what Koa, as a framework, adding and promising to a developer here: Sync (ctx.body = string, ctx.body = buffer; ctx.body = object)Sync way to pass data simply delegates to standard Streaming (ctx.body = stream)Streaming way to serve response delegates to So, what happened here, that Koa declares it's support for easy JSON handling, but while fully supporting two ways of delivering data - sync and stream, actually supports object-to-JSON conversion for response, for some unknown reason, only for the synchronous way of delivering response. Object mode streams didn't feel like first-class citizens in early Node versions, so, we haven't seen so much real-world use cases. But now, with documented Supporting object for sync bodies is even simpler: ctx.type = 'json'
ctx.body = JSON.stringify(object) But Koa founders added that two lines into core, without delegating to the community. They just forgot (but remembered later on) to do the same on streaming way of delivering a response body, I assume due to lack of documentation or popularity of object mode streams. So, this PR (20 lines of code) is not adding a new feature, nor handling non-normal streams - it closes the gap of original Koa promise - you can assign objects to response and Koa will do the rest of magic. Contrary to merging this PR will be adding to documentation some warning: Koa supports, like standard `http.ServerResponse`, two ways to set response body - sync and streaming.
Also like a standard response, it supports two types of data - string or Buffer.
It also adds support for any object via JSON.stringify, but only for sync way of setting body. |
Better to use module like https://github.com/stream-utils/streaming-json-stringify to do the stringify, also need to set |
@dead-horse the module you suggesting wasn't updated in the last 3 years (so, not sure it will be maintained, and it uses deprecated I don't want to bring a lot of third-party dependencies in for just 20 lines of code. Also, my next PR will be exposing a way to stringify body (ie, create |
This PR sets content-type to |
Actually I'm not care about import some "one-liner module", many "one-liner module" have hundreds lines of test code. I don't want to re-implement these code in koa's code base.
I'm not sure this is really helpful, change stringify method seems useless to 99.99% of developers. Even though |
@dead-horse There is open and approved PR in The code of my implementation is completely different and easier (no extra options, etc), so, it will be rewrite of |
I mean, Koa itself has an "open and approved PR since 2017" (#1011) so by that measurement Koa is not maintained, either :) |
@dead-horse found very similar to mine implementation - https://github.com/big-kahuna-burger/json-stringify-trough. Are you OK to introduce such dependency? |
Nope, it's not exactly the same. The main difference between my implementation and any module I've found on the internet that in case of no data my implementation will output empty array (that's correct implementation of empty stream in my opinion and will be parsed well if content-type is |
As I wrote above, we soon will see more libraries adding support for object stream. Here another example of recent update of very popular open source library adding |
this belongs in a separate module. i can give you access to https://github.com/stream-utils/streaming-json-stringify if you want @tinovyatkin |
@jonathanong I would love to refactor |
done |
@tinovyatkin you can use http://oboejs.com/ |
Koa currently throws while attempting to pipe an object mode stream into HTTP response.
This PR fixes that behavior allowing to serve that streams as JSON body.
Node.js 12.3 introduced a documented way to check that a stream in object mode stream, so, I think Koa must support such streams as first-class citizens out of the box.