Skip to content

Commit

Permalink
add x-forwarded-host
Browse files Browse the repository at this point in the history
  • Loading branch information
yarbsemaj committed Jul 18, 2023
1 parent 8c7eb37 commit 7756af1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Please raise an issue on [Github](https://github.com/yarbsemaj/sveltekit-adapter
## Versions
| Adapter Version| Sveltekit Version |
| ---------------| ----------------- |
| 1.1.x | 1.22.0 (Official) |
| 1.x.x | 1.0.0 (Official) |
| 0.12.x | 1.0.0-next.433 |
| 0.11.x | 1.0.0-next.401 |
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yarbsemaj/adapter-lambda",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",
"description": "An adapter for [SvelteKit](https://kit.svelte.dev/) for AWS Lambda via Lambda Proxy and API Gateway. [Serverless](https://www.serverless.com/) deployment.",
"repository": {
Expand Down
19 changes: 19 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,26 @@ resources:
- HEAD
- OPTIONS
ForwardedValues:
Headers:
- x-forwarded-host
Cookies:
Forward: all
QueryString: True
ViewerProtocolPolicy: 'redirect-to-https'
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt XForwardFunction.FunctionMetadata.FunctionARN

XForwardFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
Name: "${self:provider.stage}-${self:service}-XForwardFunction"
FunctionCode: !Sub |
function handler(event) {
event.request.headers['x-forwarded-host'] = event.request.headers['host']
return event.request
}
FunctionConfig:
Comment: 'Add x-forwarded-host'
Runtime: cloudfront-js-1.0
3 changes: 2 additions & 1 deletion src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ export async function handler(event, context) {
const { rawPath, headers, rawQueryString, body, requestContext, isBase64Encoded, cookies } = event;

const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8';
const domainName = headers['x-forwarded-host']
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;

if (cookies) {
headers['cookie'] = cookies.join('; ')
}

let rawURL = `https://${requestContext.domainName}${rawPath}${rawQueryString ? `?${rawQueryString}` : ''}`
let rawURL = `https://${domainName}${rawPath}${rawQueryString ? `?${rawQueryString}` : ''}`

await app.init({
env: process.env
Expand Down

0 comments on commit 7756af1

Please sign in to comment.