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

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

Merged
merged 2 commits into from
Jul 11, 2024
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
27 changes: 27 additions & 0 deletions src/adapter/lambda-edge/conninfo.test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions src/adapter/lambda-edge/conninfo.ts
Original file line number Diff line number Diff line change
@@ -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,
yusukebe marked this conversation as resolved.
Show resolved Hide resolved
},
})
1 change: 1 addition & 0 deletions src/adapter/lambda-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export { handle } from './handler'
export { getConnInfo } from './conninfo'

Check warning on line 7 in src/adapter/lambda-edge/index.ts

View check run for this annotation

Codecov / codecov/patch

src/adapter/lambda-edge/index.ts#L7

Added line #L7 was not covered by tests
export type {
Callback,
CloudFrontConfig,
Expand Down