-
Notifications
You must be signed in to change notification settings - Fork 10
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
Move handlers so they are lazy loaded #7
Conversation
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.
It works!! thank you!! we can improve it to avoid load the function options every time the lambda is invoked.
src/index.js
Outdated
@@ -43,6 +36,14 @@ class LambdaOffline { | |||
const invocationType = req.headers['x-amz-invocation-type']; | |||
const { functionName } = req.params; | |||
|
|||
const handlers = Object.keys(this.service.functions).reduce((acc, key) => { | |||
const fun = this.service.getFunction(key); | |||
const funOptions = functionHelper.getFunctionOptions(fun, key, servicePath, serviceRuntime); |
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.
I think the function options should be only loaded when serverless offline runs the first time, and also we should create the handler when the lambda function needs to be invoked to hot reload it.
outside the route
const funOptions = Object.keys(this.service.functions).reduce((acc, key) => {
const fun = this.service.getFunction(key);
acc[key] = functionHelper.getFunctionOptions(fun, key, servicePath, serviceRuntime);
return acc;
}, {});
into the route
const options = funOptions[functionName];
if (!options) {
return reply().code(404);
}
const handler = functionHelper.createHandler(options, {});
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.
Sounds good to me 👍 Thank you for looking through the PR!
src/index.js
Outdated
return reply().code(404); | ||
} | ||
|
||
const handler = functionHelper.createHandler(options, {}); | ||
|
||
const handler = handlers[functionName]; |
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.
This handler
constant is not necessary anymore, we should remove it
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.
Updated. Would you be open to adding a linter to help detect things like that?
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.
We already have an ESlint configuration and npm script to run it: https://github.com/marconi1992/serverless-offline-lambda/blob/master/package.json#L11
I'd like to add a CI/CD. I'll create an issue to address that.
Hey @marconi1992 - This PR moves the
handlers
so they are lazy loaded (as discussed in #6). Please let me know if you have any questions.Closes #5
Closes #6