Comments (10)
I investigated this.
Actually, code as in example works fine
module.exports = (config) => {
return ({
before: (handler, next) => {
if(handler.event.warmup === true) {
console.log('warmup - done')
return handler.context.succeed()
}
next()
}
})
}
instead of succeed
method we can use done
and fail
I can make a PR with some additions to the docs and middleware code
from middy.
I think this is great example of how to abort if a condition is met
Let's just imagine the case that you have an output serializer and an after middleware, would you assume that an early call of the callback should anyway use the serializer or not?
I think if handler.context.succeed()
doesn't short circuit the function execution it could lead to some unintended consequences.
Like I could have output middleware logging stuff etc that I only want to run when the logic of the function runs and not on ping
events. If I have to also update middleware down the chain with if/else
's to also bail (or next()
) if event.warmup
=== true. It could get messy quick.
If you handler.context.succeed()
or handler.context.fail()
, the function ends execution then and there.
One thing I'd add as a "best practice" is a clearer log message to that example:
module.exports = (config) => {
return ({
before: (handler, next) => {
if (handler.event.warmup === true) {
console.log('👋 Exiting early via warmUpMiddleware')
return handler.context.succeed()
}
next()
}
})
}
How does express/koa handle early middleware bails?
from middy.
@lmammino The point of warming up lambdas is to skip any logic and other things and just make the container running.
So, if you are using serverless-plugin-warmup
, it just executes you lambda from other lambda function and you don't need to execute anything except this middleware. in this case it's a good point to exit from warmup-check middleware.
Other thing here is maybe to provide some sort of check function here as in example:
middy(handler)
.use(warmUpCheckMiddleware(handler => handler.event.source === 'serverless-warmup-plugin'))
So, not everyone uses serverless-warmup-plugin
, and condition in example may change
from middy.
That's definitely a fair point.
We should definitely add an example like this in the doc and a test.
Plus it might be interesting to create a "keep lambda warm" middleware in the default middleware package
from middy.
Great example @NPCRUS, thanks.
The only thing I would be concerned is that when you run this you are going to skip any middleware in any other phase (after/error), so there might be unexpected behaviours.
Let's just imagine the case that you have an output serializer and an after
middleware, would you assume that an early call of the callback should anyway use the serializer or not?
Plus it would be great to have an example like this documented, tested and maybe with a simplified API (rather than having to resort to the old context.succeed
.
So, quite open for discussion here, feel free to provide more inputs and ideas.
Calling also @DavidWells and @dkatavic for discussing this :)
from middy.
Great point @NPCRUS
As @lmammino stated, I would like to figure out what would be the best behaviour after
middlewares. I will give it some thought and report a bit later. Thanks for the contribution @NPCRUS :)
from middy.
Great idea with the warmUpCheckMiddleware
, I would move that to a separate issue and draft a specification for it, so yourself or somebody else can pick it up for implementation.
In this issue, I will keep focusing more on how we make easy to write middlewares that can stop the execution flow
from middy.
Another one thing here is that context.succeed()
thing is not clear for everyone. It was used in previous lambda versions, now official way to return some result from lambda is callback(err, result)
.
So maybe we should pass callback in handler object, so we can use it middlewares like in example:
handler.callback()
And supply it with some documentation, so everyone will be available to exit from his middlewares withput searching on stackoverflow for context.succeed
method
from middy.
@DavidWells Express/koa provide res/req object in any middleware in chain. So in any middleware you can execute return res.end()
and don't call next()
and it will cause quiting from middleware chain.
from middy.
@NPCRUS there's a PR open at #77, feel more than welcome to check it out and give your feedback on it.
from middy.
Related Issues (20)
- error-logger: Call implementation function with await prefix HOT 1
- ApproximateReceiveCount limit on SqsPartialBatch HOT 5
- Conditional middleware HOT 3
- (Aware of other issues) No "exports" main defined in /var/task/node_modules/@middy/core/package.json HOT 7
- input-output-logger: replacer function is not called HOT 3
- Error: No "exports" main defined in /path/to/the/repo/node_modules/@middy/core/package.json HOT 1
- @middy/http-content-encoding not awaiting onError HOT 1
- Consistent multipart body parser response format HOT 1
- ✖ No "exports" main defined in ***i/node_modules/@middy/core/package.json
- `ws-response`: Invalid URL format gave to `ApiGatewayManagementApiClient` HOT 4
- `ws-response`: Invalid `request.response` given to API Gateway HOT 1
- Latest version of @middy/core breaks types in the handler function HOT 9
- TypeScript unable to infer types HOT 4
- TypeScript unable to infer types HOT 3
- Handler return is not type-checked. HOT 3
- Lack of support for hyphenated path parameters in @middy/http-router HOT 6
- Incompatibility of `@middy/core` with Node.js v20.16.0 in Serverless Framework Project HOT 1
- Bug Report: Incompatibility of `@middy/core` with Node.js v20.16.0 in Serverless Framework Project HOT 1
- Support for AsyncLocalStorage store.run HOT 4
- Support CommonJS (again) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from middy.