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

Add support for custom headers from origins #261

Merged
merged 6 commits into from
Oct 26, 2021

Conversation

ryanolee
Copy link
Contributor

@ryanolee ryanolee commented Sep 23, 2021

What:

This aims to solve #157 and #7

Why

To better emulate the behaviour surrounding serverless how edge lambdas are handled

How:

For #157 the defined resources are read for custom headers and they are extracted and collapsed on a per distribution basis. This is a compromise as there is no way to really know which distribution origin needs to be used on a distribution.

For #7 the FunctionSet class is now distribution aware. It will refuse to register behaviours that have already been registered under a given pattern for a different distribution.

As part of this logging has been updated to reflect which distribution a given action is coming from.

Other details:

Some of the documentation has been updated to better reflect the current serverless landscape / corrected where errors given.

This also fixes the minor issue given in dherault/serverless-offline#1179 (comment) by @l1b3r about the uri containing query strings.

@ryanolee
Copy link
Contributor Author

Hi @mattstrom @matthewstrom ,
Looks like things are picking back up a bit with this repo 🚀 . Thanks for putting in the extra work for migrating this to GH actions! Would be grateful if you could have a look at this when you get the chance 🙏

@ryanolee
Copy link
Contributor Author

Just as a heads up might have gotten the request behaviour incorrect here. Might need to refactor if so,

private readonly log: (message: string) => void,
public readonly origin: Origin = new Origin()
) {
this.distribution = distribution
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line is unnecessary because TypeScript will automatically assign any public constructor argument as an instance value.

Suggested change
this.distribution = distribution

@@ -49,6 +50,7 @@ export class BehaviorRouter {
this.builder = buildConfig(serverless);
this.context = buildContext();

this.cfResources = serverless.service?.resources?.Resources || {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry to be nitpicky; could you run npm run lint on your code?

Comment on lines 18 to 21
onOriginResponse:
handler: src/handlers.onViewerResponse
distribution: 'WebsiteDistribution'
eventType: 'origin-request'
eventType: 'origin-response'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did this need to change? There is a onOriginResponse just below it already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why I thought this needed changing. Might have mixed things up myself. Will double check why this was changed and revert if not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh I remember now! The handler is wrong (should be onOriginRequest not onViewerRequest)

getCustomHeaders(this.cfResources[handler.distribution]):
{}

const cfEvent = convertToCloudFrontEvent(req, this.builder('viewer-request'), customHeaders);
Copy link
Collaborator

@mattstrom mattstrom Sep 28, 2021

Choose a reason for hiding this comment

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

I'm not familiar with the behavior of custom headers yet? Do the custom headers need to apply from the initial event through the entire lifecycle? Or do they only apply once the request transits to the origin (i.e. somewhere around here)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They get applied to a different part of the request object. I need to recheck to be honest. The current implementation here however is definatley wrong. Misread the CF event. So will need to refactor this in either case. Given I do not think the headers ever get merged. But if they do it is certainly not here 🤔

@ryanolee
Copy link
Contributor Author

This might take a little while to refactor. Should hopefully get the chance to do it later this week... time permitting 🙏

@mattstrom
Copy link
Collaborator

Note about commit messages: this project uses semantic-release for releasing. For that reason, commit messages need to conform the Conventional Commits standard to be handled properly.

No need for you to make any changes now, though. I can update the commit message before this PR is "Squashed and merged". I neglected to change the commit message for your hot reloading PR, so it didn't get released. But it should get picked up when this PR is released.

@ryanolee
Copy link
Contributor Author

ryanolee commented Oct 3, 2021

Hi @matthewstrom ,
Overhauled the PR to hopefully better reflect the CF origin behaviour by having it inject "origin into" origin events. Should be ready for review

@ryanolee
Copy link
Contributor Author

ryanolee commented Oct 8, 2021

Hi @matthewstrom ,
Just rechecked and though it does not cover #7 should be functional in most cases 🤔

@ryanolee
Copy link
Contributor Author

Hi @matthewstrom @mattstrom ,
Please let me know if you need anything else from me to get this over the line / tested 🚀 . The PR has been reduced in scope to not cover #7 but still implements #157. 🎉
Thanks for the initial review!
-Ryan

@mattstrom mattstrom merged commit 3fbe995 into evolv-ai:master Oct 26, 2021
github-actions bot pushed a commit that referenced this pull request Oct 26, 2021
# [1.2.0](1.1.2...1.2.0) (2021-10-26)

### Features

* add support for custom headers from origins ([#261](#261)) ([3fbe995](3fbe995))
@ryanolee
Copy link
Contributor Author

Thanks mattstrom! 🎉

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.

2 participants