Skip to content

Commit 55c419d

Browse files
committed
fix(adapter): run error when without header x-forwarded-protocol
1 parent 455d2e4 commit 55c419d

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

packages/adapter/README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,7 @@ export default defineConfig({
3434
})
3535
```
3636

37-
## Architecture
38-
39-
After `astro build`, you will get two directories, `dist/server` and `dist/client`. You should deploy them with the following architecture:
40-
41-
### lambda
42-
43-
![lambda](https://raw.githubusercontent.com/helbing/astrojs-aws/main/docs/static/architecture/lambda.png)
44-
45-
### edge
46-
47-
![edge](https://raw.githubusercontent.com/helbing/astrojs-aws/main/docs/static/architecture/edge.png)
48-
49-
Recommended to use this [AWS Constructs Library](https://github.com/helbing/astrojs-aws/tree/main/packages/constructs) to deploy.
37+
Recommended to use the [AWS Constructs Library](https://github.com/helbing/astrojs-aws/tree/main/packages/constructs) to deploy.
5038

5139
## Configuration
5240

packages/adapter/src/handlers/edge.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ describe("Test transformRequest", () => {
9191
expect(transformRequest(cfRequest)).toMatchObject(expectRequest)
9292
})
9393

94+
test("Expect transform success without header x-forwarded-protocol", () => {
95+
const cfRequest: CloudFrontRequest = {
96+
uri: "/",
97+
method: "GET",
98+
headers: {
99+
"x-forwarded-host": [
100+
{
101+
key: "x-forwarded-host",
102+
value: "example.com",
103+
},
104+
],
105+
},
106+
querystring: "",
107+
body: undefined,
108+
clientIp: "",
109+
}
110+
const expectRequest = new Request("https://example.com", {
111+
method: "GET",
112+
headers: {
113+
"x-forwarded-host": "example.com",
114+
},
115+
})
116+
117+
expect(transformRequest(cfRequest)).toMatchObject(expectRequest)
118+
})
119+
94120
test("Expect transform success with querystring", () => {
95121
const cfRequest: CloudFrontRequest = {
96122
uri: "/",

packages/adapter/src/handlers/edge.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ export function transformRequestHeaders(headers: CloudFrontHeaders): Headers {
6767
export function transformRequest(cfRequest: CloudFrontRequest): Request {
6868
const { uri, method, headers, querystring, body } = cfRequest
6969
const requestHeaders = transformRequestHeaders(headers)
70-
const scheme = headers["x-forwarded-protocol"][0].value || "https"
71-
const host = (headers["x-forwarded-host"] || headers["host"])[0].value
70+
const scheme = requestHeaders.get("x-forwarded-protocol") ?? "https"
71+
const host =
72+
requestHeaders.get("x-forwarded-host") || requestHeaders.get("host")
7273
const qs = querystring.length > 0 ? `?${querystring}` : ""
7374
const url = new URL(`${uri}${qs}`, `${scheme}://${host}`)
7475

0 commit comments

Comments
 (0)