Skip to content

Commit

Permalink
fix(adapter): run error when without header x-forwarded-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
helbing committed Apr 24, 2023
1 parent 455d2e4 commit 55c419d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
14 changes: 1 addition & 13 deletions packages/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ export default defineConfig({
})
```

## Architecture

After `astro build`, you will get two directories, `dist/server` and `dist/client`. You should deploy them with the following architecture:

### lambda

![lambda](https://raw.githubusercontent.com/helbing/astrojs-aws/main/docs/static/architecture/lambda.png)

### edge

![edge](https://raw.githubusercontent.com/helbing/astrojs-aws/main/docs/static/architecture/edge.png)

Recommended to use this [AWS Constructs Library](https://github.com/helbing/astrojs-aws/tree/main/packages/constructs) to deploy.
Recommended to use the [AWS Constructs Library](https://github.com/helbing/astrojs-aws/tree/main/packages/constructs) to deploy.

## Configuration

Expand Down
26 changes: 26 additions & 0 deletions packages/adapter/src/handlers/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ describe("Test transformRequest", () => {
expect(transformRequest(cfRequest)).toMatchObject(expectRequest)
})

test("Expect transform success without header x-forwarded-protocol", () => {
const cfRequest: CloudFrontRequest = {
uri: "/",
method: "GET",
headers: {
"x-forwarded-host": [
{
key: "x-forwarded-host",
value: "example.com",
},
],
},
querystring: "",
body: undefined,
clientIp: "",
}
const expectRequest = new Request("https://example.com", {
method: "GET",
headers: {
"x-forwarded-host": "example.com",
},
})

expect(transformRequest(cfRequest)).toMatchObject(expectRequest)
})

test("Expect transform success with querystring", () => {
const cfRequest: CloudFrontRequest = {
uri: "/",
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter/src/handlers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export function transformRequestHeaders(headers: CloudFrontHeaders): Headers {
export function transformRequest(cfRequest: CloudFrontRequest): Request {
const { uri, method, headers, querystring, body } = cfRequest
const requestHeaders = transformRequestHeaders(headers)
const scheme = headers["x-forwarded-protocol"][0].value || "https"
const host = (headers["x-forwarded-host"] || headers["host"])[0].value
const scheme = requestHeaders.get("x-forwarded-protocol") ?? "https"
const host =
requestHeaders.get("x-forwarded-host") || requestHeaders.get("host")
const qs = querystring.length > 0 ? `?${querystring}` : ""
const url = new URL(`${uri}${qs}`, `${scheme}://${host}`)

Expand Down

0 comments on commit 55c419d

Please sign in to comment.