Skip to content
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

Merged
merged 3 commits into from
May 30, 2019

Conversation

tclindner
Copy link
Contributor

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

Copy link
Owner

@marconi1992 marconi1992 left a 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);
Copy link
Owner

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, {});

Copy link
Contributor Author

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];
Copy link
Owner

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

Copy link
Contributor Author

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?

Copy link
Owner

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.

@marconi1992 marconi1992 merged commit 149cb90 into marconi1992:master May 30, 2019
@tclindner tclindner deleted the lazy-load branch May 31, 2019 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Is it possible to use function environment variables? Question: Is hot reload supported?
2 participants