0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 11:07:29 +01:00

feat(lambda-edge): add getConnInfo helper for Lambda@Edge (#3099)

* add conninfo helper for lambda@edge

* export getConnInfo
This commit is contained in:
yasuaki640 2024-07-11 17:08:36 +09:00 committed by GitHub
parent 74310f469b
commit 4a4e851d50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import { Context } from '../../context'
import type { CloudFrontEdgeEvent } from './handler'
import { getConnInfo } from './conninfo'
describe('getConnInfo', () => {
it('Should info is valid', () => {
const clientIp = Math.random().toString()
const env = {
event: {
Records: [
{
cf: {
request: {
clientIp,
},
},
},
],
} as CloudFrontEdgeEvent,
}
const c = new Context(new Request('http://localhost/'), { env })
const info = getConnInfo(c)
expect(info.remote.address).toBe(clientIp)
})
})

View File

@ -0,0 +1,15 @@
import type { GetConnInfo } from '../../helper/conninfo'
import type { CloudFrontEdgeEvent } from './handler'
import type { Context } from '../../context'
type Env = {
Bindings: {
event: CloudFrontEdgeEvent
}
}
export const getConnInfo: GetConnInfo = (c: Context<Env>) => ({
remote: {
address: c.env.event.Records[0].cf.request.clientIp,
},
})

View File

@ -4,6 +4,7 @@
*/
export { handle } from './handler'
export { getConnInfo } from './conninfo'
export type {
Callback,
CloudFrontConfig,