Skip to content
Myr

feat(lambda-edge): pass the `context` and `callback` as env (#1229)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent f37de25 · Browse files · 3 files+141 added−80 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+141 added−80 removed

  • tests:+60 added−11 removed
  • other:+81 added−69 removed

3 files+141 added−80 removed

modified:runtime_tests/lambda-edge/index.test.ts+60 added-11 removed
1/* eslint-disable quotes */2import type { Callback, CloudFrontRequest } from '../../src/adapter/lambda-edge/handler'3import { handle } from '../../src/adapter/lambda-edge/handler'4import { Hono } from '../../src/hono'5import { basicAuth } from '../../src/middleware/basic-auth'67type Bindings = {8 callback: Callback9 request: CloudFrontRequest10}1112describe('Lambda@Edge Adapter for Hono', () => {6 const app = new Hono()13 const app = new Hono<{ Bindings: Bindings }>()1415 app.get('/', (c) => {16 return c.text('Hello Lambda!')17 })
8 unmodified lines
26 const body = (await c.req.parseBody()) as { message: string }27 return c.text(body.message)28 })2930 app.get('/callback', async (c, next) => {31 await next()32 c.env.callback(null, c.env.request)33 })3435 const username = 'hono-user-a'36 const password = 'hono-password-a'37 app.use('/auth/*', basicAuth({ username, password }))38 app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))
43 unmodified lines
82 }83 const response = await handler(event)84 expect(response.status).toBe('200')85 expect(response.body).toBe('Hello Lambda!')74 if(response.headers && response.headers['content-type']){86 if (response.headers && response.headers['content-type']) {87 expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)88 } else {77 throw new Error("'content-type' header is missing in the response");89 throw new Error("'content-type' header is missing in the response")90 }91 })9293 it('Should handle a GET request and return a 200 response (Lambda@Edge origin request)', async () => {
63 unmodified lines
157 }158 const response = await handler(event)159 expect(response.status).toBe('200')160 expect(response.body).toBe('Hello Lambda!')149 if(response.headers && response.headers['content-type']){161 if (response.headers && response.headers['content-type']) {162 expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)163 } else {152 throw new Error("'content-type' header is missing in the response");164 throw new Error("'content-type' header is missing in the response")165 }166 })167168 it('Should handle a GET request and return a 200 response (Lambda@Edge viewer response)', async () => {
111 unmodified lines
280 }281 const response = await handler(event)282 expect(response.status).toBe('200')283 expect(response.body).toBe('Hello Lambda!')272 if(response.headers && response.headers['content-type']){284 if (response.headers && response.headers['content-type']) {285 expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)286 } else {275 throw new Error("'content-type' header is missing in the response");287 throw new Error("'content-type' header is missing in the response")288 }289 })290291 it('Should handle a GET request and return a 200 response (Lambda@Edge origin response)', async () => {
129 unmodified lines
421 }422 const response = await handler(event)423 expect(response.status).toBe('200')424 expect(response.body).toBe('Hello Lambda!')413 if(response.headers && response.headers['content-type']){425 if (response.headers && response.headers['content-type']) {426 expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)427 } else {416 throw new Error("'content-type' header is missing in the response");428 throw new Error("'content-type' header is missing in the response")429 }430 })431432 it('Should handle a GET request and return a 200 response with binary', async () => {
29 unmodified lines
462 const response = await handler(event)463464 expect(response.status).toBe('200')465 expect(response.body).toBe('RmFrZSBJbWFnZQ==') // base64 encoded fake image454 if(response.headers && response.headers['content-type']){466 if (response.headers && response.headers['content-type']) {467 expect(response.headers['content-type'][0].value).toMatch(/^image\/png/)468 } else {457 throw new Error("'content-type' header is missing in the response");469 throw new Error("'content-type' header is missing in the response")470 }471 })472473 it('Should handle a GET request and return a 404 response', async () => {
162 unmodified lines
636 const response = await handler(event)637638 expect(response.status).toBe('401')639 })640641 it('Should call a callback to continue processing the request', async () => {642 const event = {643 Records: [644 {645 cf: {646 config: {647 distributionDomainName: 'example.com',648 distributionId: 'EXAMPLE123',649 eventType: 'viewer-request',650 requestId: 'exampleRequestId',651 },652 request: {653 clientIp: '123.123.123.123',654 headers: {},655 method: 'GET',656 querystring: '',657 uri: '/callback',658 },659 },660 },661 ],662 }663664 let called = false665 let requestClientIp = ''666667 await handler(event, {}, (_err, result) => {668 if (result && 'clientIp' in result) {669 requestClientIp = result.clientIp670 }671 called = true672 })673674 expect(called).toBe(true)675 expect(requestClientIp).toBe('123.123.123.123')676 })677})
modified:src/adapter/lambda-edge/handler.ts+80 added-69 removed
modified:src/adapter/lambda-edge/index.ts+1 added