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(lambda-edge): update schema and refactor #1230

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions runtime_tests/lambda-edge/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ describe('Lambda@Edge Adapter for Hono', () => {
const response = await handler(event)
expect(response.status).toBe('200')
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
if(response.headers && response.headers['content-type']){
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
} else {
throw new Error("'content-type' header is missing in the response");
}
})

it('Should handle a GET request and return a 200 response (Lambda@Edge origin request)', async () => {
Expand Down Expand Up @@ -142,7 +146,11 @@ describe('Lambda@Edge Adapter for Hono', () => {
const response = await handler(event)
expect(response.status).toBe('200')
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
if(response.headers && response.headers['content-type']){
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
} else {
throw new Error("'content-type' header is missing in the response");
}
})

it('Should handle a GET request and return a 200 response (Lambda@Edge viewer response)', async () => {
Expand Down Expand Up @@ -261,7 +269,11 @@ describe('Lambda@Edge Adapter for Hono', () => {
const response = await handler(event)
expect(response.status).toBe('200')
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
if(response.headers && response.headers['content-type']){
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
} else {
throw new Error("'content-type' header is missing in the response");
}
})

it('Should handle a GET request and return a 200 response (Lambda@Edge origin response)', async () => {
Expand Down Expand Up @@ -398,7 +410,11 @@ describe('Lambda@Edge Adapter for Hono', () => {
const response = await handler(event)
expect(response.status).toBe('200')
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
if(response.headers && response.headers['content-type']){
expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)
} else {
throw new Error("'content-type' header is missing in the response");
}
})

it('Should handle a GET request and return a 200 response with binary', async () => {
Expand Down Expand Up @@ -435,7 +451,11 @@ describe('Lambda@Edge Adapter for Hono', () => {

expect(response.status).toBe('200')
expect(response.body).toBe('RmFrZSBJbWFnZQ==') // base64 encoded fake image
expect(response.headers['content-type'][0].value).toMatch(/^image\/png/)
if(response.headers && response.headers['content-type']){
expect(response.headers['content-type'][0].value).toMatch(/^image\/png/)
} else {
throw new Error("'content-type' header is missing in the response");
}
})

it('Should handle a GET request and return a 404 response', async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/adapter/lambda-edge/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ interface CloudFrontEdgeEvent {
Records: CloudFrontEvent[];
}

// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-programming-model
interface CloudFrontResult {
status: string;
statusDescription?: string;
headers: {
headers?: {
[header: string]: {
key?: string;
key: string;
value: string;
}[];
};
body?: string;
bodyEncoding?: 'text' | 'base64';
}

/**
Expand Down Expand Up @@ -117,9 +119,7 @@ const createRequest = (

const headers = new Headers()
for (const [k, v] of Object.entries(event.Records[0].cf.request.headers)) {
if (Array.isArray(v)) {
v.forEach(header => headers.set(k, header.value))
}
v.forEach(header => headers.set(k, header.value))
}
const method = event.Records[0].cf.request.method
const requestInit: RequestInit = {
Expand Down