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:
parent
74310f469b
commit
4a4e851d50
27
src/adapter/lambda-edge/conninfo.test.ts
Normal file
27
src/adapter/lambda-edge/conninfo.test.ts
Normal 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)
|
||||
})
|
||||
})
|
15
src/adapter/lambda-edge/conninfo.ts
Normal file
15
src/adapter/lambda-edge/conninfo.ts
Normal 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,
|
||||
},
|
||||
})
|
@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
export { handle } from './handler'
|
||||
export { getConnInfo } from './conninfo'
|
||||
export type {
|
||||
Callback,
|
||||
CloudFrontConfig,
|
||||
|
Loading…
Reference in New Issue
Block a user