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)
- Fetching SSM params: InvalidSignatureException: Signature expired HOT 12
- Version 6 [Draft]
- Logo is weird on MacOS Safari
- Json body parser unusable with typescript, typing errors within the package HOT 4
- Missing type-utils.d.ts file in @middy/util package HOT 8
- Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) HOT 8
- How to use version 5 with TypeScript HOT 4
- http-response-serializer documentation incorrect HOT 1
- Typescript error in handler parameters HOT 4
- Module not found: Error: Package path . is not exported from package HOT 1
- Response validation not working HOT 4
- Use @fastify/busboy instead of outdated busboy HOT 2
- Type middy.MiddlewareFn doesn't exist HOT 17
- Cannot find module '@middy/core' when running jest with typescript and esm HOT 15
- ERR_PACKAGE_PATH_NOT_EXPORTED HOT 2
- @middy/dynamodb middleware: Unmarshall on empty item is causing exception HOT 3
- The `inputOutputLogger` middleware fails if used with a streaming handler. HOT 2
- Matching `Access-Control-Allow-Headers` HOT 5
- Documentation: Enable middlewares based on request event parameters HOT 3
- Http router / path mapping HOT 6
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.