-
-
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
reference app.wrappers[] for each mw #718
Conversation
I don't understand what's wrong here with this PR? Locally both
Anyway, will add tests and docs if this PR is an acceptable approach. |
So sort of like a onUsed hook, but a variable instead? |
@PlasmaPower Aye, simpler. Not necessarily better but I prefer the simple solution so I just threw this out there. |
sorry but I don't get the concept of wrappers you want to introduce here. can you give a high level explanation? |
@juliangruber Sure thing! Basically a wrapper replaces any middleware with the middleware called on the function ( app.wrappers = [co.wrap];
app.use(function * (ctx, next) {
ctx.body = 'example';
yield next();
}); This is also useful for profilers, for instance you can do this: app.wrappers = [function (mw) {
return function () {
let start = process.hrtime();
return Promise.resolve().then(() => mw).then(function (val) {
let time = process.hrtime(start);
console.log(mw._name || mw.name + ': ' + time.join('.') + ' seconds';
return val;
});
};
}]; |
Gotcha, thanks! What seems weird to me from an api perspective is that you use Also, do wrappers get cascaded? For example if you have app.wrappers = [profile];
const composed = compose([a, b]);
app.use(composed); would you just get a profile of the composed middleware or the individual ones as well? |
the biggest concern I have though is that koa sets |
@juliangruber there are a couple different implementation PRs around, which do you prefer? As for Most implementations cascade wrappers, yes. That is the expected behavior. In most implementations, that requires changes to those libraries though. |
Unfortunately, there doesn't seem to be a very clean way to do this. Which PR implementing wrappers do you prefer? Each has it's advantages and disadvantages IMO. |
@juliangruber the main problem is; how do we allow wrapping for the entire app - no matter what library user uses. It's handled by compose because had we made this a koa solution, libraries such as koa-router wouldn't be affected properly by the wrapping. This could easily have been a koa/external solution if we made a deep tree of mw's before a compose/equivalent. Though that's not the case today. |
@fl0w I'm still not convinced creating a tree of middleware is possible in a lot of cases without sacrificing performance. |
@PlasmaPower aye, maybe. My point it, I don't think we can avoid wrapping in compose, right? |
@fl0w I'm pretty sure that's a necessary evil, yes. We can still offload the logic though, like I've done in my PR. |
I'm bot sure cascading wrappers is a good idea, what happens inside a mw is its business and its business only. |
@juliangruber it doesn't cascade, it just flat-wraps all mw in Application.middleware? My PR in compose is messed up - wrong rebase apparently, working together a proper one as we speak (slowly, in a meeting) |
@fl0w if i have this: app.wrappers = [(mw) => {
return () => {
return mw().then(console.log('hi'))
}
}];
const composed = compose([a, b]);
app.use(composed); will it log |
@juliangruber the goal is for compose to wrap all wrappers, once per mw. Answer: 2x. The end goal is to wrap each individual mw with all provided wrappers once. |
The equivalent today for koa@2 could be: const wrapper = async (ctx, next) => {
console.log('hi')
}
const wrappedMw1 = async (ctx, next) => {
// .. mw1 logic
await wrapper()
}
const wrappedMw2 = async (ctx, next) => {
// .. mw2 logic
await wrapper()
}
app.use(wrappedMw1)
app.use(wrappedMw2) |
hmmm...curious what @tj and @jonathanong think |
@juliangruber there's lengthy bikeshedding at koajs/compose#51 as well. |
eslint update. might be a separate PR if it's failing master/next right now |
there was some discussion in some PRs:
i'm not sure where i stated it, but my main concern are specifics. like @juliangruber stated, does it nest into |
Current coverage is 100%@@ v2.x #718 diff @@
==========================================
Files 4 4
Lines 414 424 +10
Methods 80 82 +2
Messages 0 0
Branches 99 102 +3
==========================================
+ Hits 414 424 +10
Misses 0 0
Partials 0 0
|
This is for koajs/compose#55