-
-
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
async (ctx, next) => await next() #530
Conversation
9ee8a6a
to
eac7fd9
Compare
@@ -55,7 +55,7 @@ describe('ctx.type', function(){ | |||
describe('with no Content-Type', function(){ | |||
it('should return ""', function(){ | |||
const ctx = context(); | |||
// TODO: this is lame | |||
// TODO: ctx is lame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
fixed a few issues in #531 // ok
app.use(function (ctx, next) {
// ...
})
// ok
app.use(co.wrap(function* (ctx, next) {
yield next()
})
// ok (legacy generator middleware)
app.use(function* (next) {
yield next
})
// not ok
app.use(function* (ctx, next) {
// ...
}) |
are you guys okay with above? supporting legacy middleware but not the following? app.use(function* (ctx, next) {
// ...
}) i'd personally prefer to only support |
How about this ? // ok
app.use(function (ctx, next) {
return next()
})
// ok
app.use(async function (ctx, next) {
await next()
})
// ok, implicitly wrap generator function using `co.wrap`
app.use(function* (ctx, next) {
yield next()
})
// ok, legacy support, may have a better name ? `app.useGenerator` ?
app.useLegacy(function* (next) {
yield next
}) |
👍 to this since we're breaking the api anyway. |
Actually, I am 👍 to only support It's also very easy to support legacy middleware like this: app.use(convert(function* (next) {
yield next
})) |
The key APIs to support are I suspect anything else like |
@jonathanong Should keep old style middleware There're so many |
we should keep all the koa's middlewares working both in koa@1 and koa@2. |
If Koa 1 generators end up being slow or expensive you could add a console warning (that could be disabled via a flag) when people use the legacy generator API. But definitely good to have a deprecation phase before removing a core API. |
+1 for supporting both but showing deprecation warnings. We could also look at function.length to check if the function takes only one parameter and if yes, call it with this/next. I know this is dirty but it is just for the transition phase. Or if we don't "support" the old style, still check function.length to directly throw so people know why their middleware doesn't work anymore. |
+1 for support both. |
@atian25 exactly. |
@felixfbecker a middleware function with a single argument could very well be the new style:
So checking function.length doesn't help. |
@smcmurray You're right of course. Is backwards compatibility even possible then? We would have to assume that all generator functions work with |
@felixfbecker We can convert any |
I am kind of worried about fragmentation. @jonathanong I think we would deprecate Just checked the status of ES7 async/await in V8 at here. Really hope it lands ASAP (may we should vote / star for it?) |
Re: V8 and Node. Babel is really good now and can convert async functions to generators (or to plain functions - your choice), or you can simulate an async function with co.wrap + generators. Async functions are production-ready today. |
I'm curious, why did we switch to having the context passed in as a |
The reason for not binding |
First, thank you @jonathanong for this branch. It is very helpful. Second, I get a stack overflow because co gets caught in a spiral between toPromise() and objectToPromise(). |
@smcmurray do you have a test case? |
|
like this? 77f1d2a oh you know what. those middleware have the old signature, so they aren't going to work right now. |
anyone have any ideas how to support the old middleware style? i think promoting i'm against passing different arguments based on whether a function or a generator function is passed though. |
otherwise, i can push an alpha as soon as i have time |
A separate module like koa-convert seems preferable because in Koa 3 (for example) when
Fair point. What do you think of this set up? Modern API: koa.use(async (ctx, next) => { ... })
// or when you don't need to await anything for example:
koa.use((ctx, next) => { ... }) Legacy API: import convert from 'koa-convert';
koa.use(convert(function*(next) { /* this === ctx */ }));
// or for synchronous functions that don't need to yield
koa.use(convert(function(next) { /* this === ctx */ })); This is a breaking change, but with a clear migration path (I think? maybe there are edge cases we'll find). Publishing 2.0.0-yolo.1 would be great. If you believe |
Maybe only support modern API in Koa 2.0 and people can override const use = app.use
app.use = (mw) => use.call(app, convert(mw)) |
It's probably sane to have some kind of deprecation warning for 2.x personally, then people at leasta have a chance to migrate the deps, apps themselves would be a quick scripted refactor but publishing all the modules back to npm is a big pain |
Re: deprecation warning -- I'm not sure how to programmatically distinguish between old and new middleware, especially if some people are using Babel to convert generator functions to regular functions. Migration: Based on my understanding of So I'm assuming that if Koa 2 ships without support for Koa 1 middleware (and potentially without support for generators at all, for that matter), there's a straightforward migration path:
|
If most of current apps using koa 1.x, all the koa middlewares authors need to maintain two branchs to support koa 1.x and 2.x two. But we need to move on, |
@ide in Koa v0.x and v1.x (without experimental flag), Therefore, we can distinguish old and new middleware with |
@gyson Cool, good to know! Then maybe we can support old functions too (include koa-convert with Koa for Koa 2, and remove it in Koa 3?). |
👍 I agree w/ @fengmk2. Most of the Koa users before were on an experimental version of Node anyways. I honestly think we need to take this and move fast without holding back for koa 1.x.x backwards-compatibility but following semver so it's all clear what goes back / forward to what. |
+1 for koa-convert. We could also name it koa-legacy. |
cool i think the consensus is to not include |
5b04a8a
to
6e545ca
Compare
6e545ca
to
57e7cf5
Compare
merging and releasing an alpha, then putting the pending issues into separate issues. i'd really like help on docs! |
I'll help proofread if someone wants feedback on the content and how the docs read. |
closes #530
please test this. if you guys wanna benchmark this, that would be helpful too!
koa-compose
versionkoa@1
about the middleware signature change