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

fix(adapter/aws-lambda): add alb event requestContext undefined check for testing convenience #3691

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

ospatil
Copy link
Contributor

@ospatil ospatil commented Nov 19, 2024

Issue Summary

While testing a lambda using aws lambda invoke or from the AWS Console, the current aws-lambda adapter isProxyEventALB implementation results in following error:

{
    "errorType": "TypeError",
    "errorMessage": "Cannot convert undefined or null to object",
    "stack": [
        "TypeError: Cannot convert undefined or null to object",
        "    at Function.hasOwn (<anonymous>)",
        "    at isProxyEventALB (/var/task/index.js:1816:17)",
        "    at getProcessor (/var/task/index.js:1807:7)",
        "    at Runtime.handler (/var/task/index.js:1620:23)",
        "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
    ]
}

This can be reproduced by scaffolding a lambda project using npm create hono@latest and deploying it in an AWS account without any changes to the code.

Benefits of the fix

A fix by adding an undefined check for event.requestContext will significantly ease the testing of lambda using simple tools like aws lambda invoke and AWS Console.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@ospatil ospatil changed the title aws-lambda adapter - add alb event requestContext undefined check for testing convenience fix(aws-lambda adapter) - add alb event requestContext undefined check for testing convenience Nov 19, 2024
@ospatil ospatil changed the title fix(aws-lambda adapter) - add alb event requestContext undefined check for testing convenience fix(adapter/aws-lambda) - add alb event requestContext undefined check for testing convenience Nov 19, 2024
@ospatil ospatil changed the title fix(adapter/aws-lambda) - add alb event requestContext undefined check for testing convenience fix(adapter/aws-lambda): add alb event requestContext undefined check for testing convenience Nov 19, 2024
Copy link

codecov bot commented Nov 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.70%. Comparing base (163657a) to head (d3376e9).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3691   +/-   ##
=======================================
  Coverage   91.70%   91.70%           
=======================================
  Files         159      159           
  Lines       10145    10162   +17     
  Branches     2910     2883   -27     
=======================================
+ Hits         9303     9319   +16     
- Misses        840      842    +2     
+ Partials        2        1    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@yusukebe
Copy link
Member

Hi @ospatil

Thank you for the PR. Looks good, but one thing. Can you write a test for this change? There may not be enough tests for this AWS Lambda adapter currently, but we have to add a test for the patch.

@ospatil
Copy link
Contributor Author

ospatil commented Nov 23, 2024

@yusukebe I have added tests for getProcessor function. Since the Processor class types are not exported, I had to limit the assertions to the processor being defined after calling the function.

A big shout-out to you and the team for this amazing project 🙌🏼 .

if (event.requestContext) {
return Object.hasOwn(event.requestContext, 'elb')
}
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's from the report before I updated the PR with the tests. After adding the tests, the coverage report has not run as it's awaiting maintainer approval.

I ran the coverage locally (bun coverage) after adding the tests and it's all green.
hono-lambda-coverage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
New tests are not executed since approval needed

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@ospatil

Nice job! I'll merge this and release a new version. Thank you for your contribution!

@yusukebe yusukebe merged commit 7ebf854 into honojs:main Nov 25, 2024
16 checks passed
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.

3 participants